404

[ Avaa Bypassed ]




Upload:

Command:

botdev@18.190.158.76: ~ $
#
# The Python Imaging Library.
# $Id$
#
# PCX file handling
#
# This format was originally used by ZSoft's popular PaintBrush
# program for the IBM PC.  It is also supported by many MS-DOS and
# Windows applications, including the Windows PaintBrush program in
# Windows 3.
#
# history:
# 1995-09-01 fl   Created
# 1996-05-20 fl   Fixed RGB support
# 1997-01-03 fl   Fixed 2-bit and 4-bit support
# 1999-02-03 fl   Fixed 8-bit support (broken in 1.0b1)
# 1999-02-07 fl   Added write support
# 2002-06-09 fl   Made 2-bit and 4-bit support a bit more robust
# 2002-07-30 fl   Seek from to current position, not beginning of file
# 2003-06-03 fl   Extract DPI settings (info["dpi"])
#
# Copyright (c) 1997-2003 by Secret Labs AB.
# Copyright (c) 1995-2003 by Fredrik Lundh.
#
# See the README file for information on usage and redistribution.
#

import io
import logging

from . import Image, ImageFile, ImagePalette
from ._binary import i8
from ._binary import i16le as i16
from ._binary import o8
from ._binary import o16le as o16

logger = logging.getLogger(__name__)


def _accept(prefix):
    return i8(prefix[0]) == 10 and i8(prefix[1]) in [0, 2, 3, 5]


##
# Image plugin for Paintbrush images.


class PcxImageFile(ImageFile.ImageFile):

    format = "PCX"
    format_description = "Paintbrush"

    def _open(self):

        # header
        s = self.fp.read(128)
        if not _accept(s):
            raise SyntaxError("not a PCX file")

        # image
        bbox = i16(s, 4), i16(s, 6), i16(s, 8) + 1, i16(s, 10) + 1
        if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]:
            raise SyntaxError("bad PCX image size")
        logger.debug("BBox: %s %s %s %s", *bbox)

        # format
        version = i8(s[1])
        bits = i8(s[3])
        planes = i8(s[65])
        stride = i16(s, 66)
        logger.debug(
            "PCX version %s, bits %s, planes %s, stride %s",
            version,
            bits,
            planes,
            stride,
        )

        self.info["dpi"] = i16(s, 12), i16(s, 14)

        if bits == 1 and planes == 1:
            mode = rawmode = "1"

        elif bits == 1 and planes in (2, 4):
            mode = "P"
            rawmode = "P;%dL" % planes
            self.palette = ImagePalette.raw("RGB", s[16:64])

        elif version == 5 and bits == 8 and planes == 1:
            mode = rawmode = "L"
            # FIXME: hey, this doesn't work with the incremental loader !!!
            self.fp.seek(-769, io.SEEK_END)
            s = self.fp.read(769)
            if len(s) == 769 and i8(s[0]) == 12:
                # check if the palette is linear greyscale
                for i in range(256):
                    if s[i * 3 + 1 : i * 3 + 4] != o8(i) * 3:
                        mode = rawmode = "P"
                        break
                if mode == "P":
                    self.palette = ImagePalette.raw("RGB", s[1:])
            self.fp.seek(128)

        elif version == 5 and bits == 8 and planes == 3:
            mode = "RGB"
            rawmode = "RGB;L"

        else:
            raise OSError("unknown PCX mode")

        self.mode = mode
        self._size = bbox[2] - bbox[0], bbox[3] - bbox[1]

        bbox = (0, 0) + self.size
        logger.debug("size: %sx%s", *self.size)

        self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))]


# --------------------------------------------------------------------
# save PCX files


SAVE = {
    # mode: (version, bits, planes, raw mode)
    "1": (2, 1, 1, "1"),
    "L": (5, 8, 1, "L"),
    "P": (5, 8, 1, "P"),
    "RGB": (5, 8, 3, "RGB;L"),
}


def _save(im, fp, filename):

    try:
        version, bits, planes, rawmode = SAVE[im.mode]
    except KeyError as e:
        raise ValueError(f"Cannot save {im.mode} images as PCX") from e

    # bytes per plane
    stride = (im.size[0] * bits + 7) // 8
    # stride should be even
    stride += stride % 2
    # Stride needs to be kept in sync with the PcxEncode.c version.
    # Ideally it should be passed in in the state, but the bytes value
    # gets overwritten.

    logger.debug(
        "PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d",
        im.size[0],
        bits,
        stride,
    )

    # under windows, we could determine the current screen size with
    # "Image.core.display_mode()[1]", but I think that's overkill...

    screen = im.size

    dpi = 100, 100

    # PCX header
    fp.write(
        o8(10)
        + o8(version)
        + o8(1)
        + o8(bits)
        + o16(0)
        + o16(0)
        + o16(im.size[0] - 1)
        + o16(im.size[1] - 1)
        + o16(dpi[0])
        + o16(dpi[1])
        + b"\0" * 24
        + b"\xFF" * 24
        + b"\0"
        + o8(planes)
        + o16(stride)
        + o16(1)
        + o16(screen[0])
        + o16(screen[1])
        + b"\0" * 54
    )

    assert fp.tell() == 128

    ImageFile._save(im, fp, [("pcx", (0, 0) + im.size, 0, (rawmode, bits * planes))])

    if im.mode == "P":
        # colour palette
        fp.write(o8(12))
        fp.write(im.im.getpalette("RGB", "RGB"))  # 768 bytes
    elif im.mode == "L":
        # greyscale palette
        fp.write(o8(12))
        for i in range(256):
            fp.write(o8(i) * 3)


# --------------------------------------------------------------------
# registry


Image.register_open(PcxImageFile.format, PcxImageFile, _accept)
Image.register_save(PcxImageFile.format, _save)

Image.register_extension(PcxImageFile.format, ".pcx")

Image.register_mime(PcxImageFile.format, "image/x-pcx")

Filemanager

Name Type Size Permission Actions
__pycache__ Folder 2755
BdfFontFile.py File 2.75 KB 0644
BlpImagePlugin.py File 14 KB 0644
BmpImagePlugin.py File 13.92 KB 0644
BufrStubImagePlugin.py File 1.48 KB 0644
ContainerIO.py File 2.82 KB 0644
CurImagePlugin.py File 1.68 KB 0644
DcxImagePlugin.py File 2.09 KB 0644
DdsImagePlugin.py File 5.34 KB 0644
EpsImagePlugin.py File 11.82 KB 0644
ExifTags.py File 8.8 KB 0644
FitsStubImagePlugin.py File 1.59 KB 0644
FliImagePlugin.py File 4.23 KB 0644
FontFile.py File 2.7 KB 0644
FpxImagePlugin.py File 6.53 KB 0644
FtexImagePlugin.py File 3.23 KB 0644
GbrImagePlugin.py File 2.73 KB 0644
GdImageFile.py File 2.47 KB 0644
GifImagePlugin.py File 28.25 KB 0644
GimpGradientFile.py File 3.27 KB 0644
GimpPaletteFile.py File 1.24 KB 0644
GribStubImagePlugin.py File 1.51 KB 0644
Hdf5StubImagePlugin.py File 1.48 KB 0644
IcnsImagePlugin.py File 11.44 KB 0644
IcoImagePlugin.py File 9.94 KB 0644
ImImagePlugin.py File 10.53 KB 0644
Image.py File 113.4 KB 0644
ImageChops.py File 7.13 KB 0644
ImageCms.py File 36.22 KB 0644
ImageColor.py File 8.44 KB 0644
ImageDraw.py File 29.94 KB 0644
ImageDraw2.py File 4.9 KB 0644
ImageEnhance.py File 3.12 KB 0644
ImageFile.py File 20.74 KB 0644
ImageFilter.py File 15.46 KB 0644
ImageFont.py File 43.49 KB 0644
ImageGrab.py File 3.54 KB 0644
ImageMath.py File 6.88 KB 0644
ImageMode.py File 1.6 KB 0644
ImageMorph.py File 7.67 KB 0644
ImageOps.py File 18.03 KB 0644
ImagePalette.py File 6.2 KB 0644
ImagePath.py File 336 B 0644
ImageQt.py File 5.67 KB 0644
ImageSequence.py File 1.81 KB 0644
ImageShow.py File 6.15 KB 0644
ImageStat.py File 3.81 KB 0644
ImageTk.py File 9.11 KB 0644
ImageTransform.py File 2.78 KB 0644
ImageWin.py File 7.02 KB 0644
ImtImagePlugin.py File 2.15 KB 0644
IptcImagePlugin.py File 5.6 KB 0644
Jpeg2KImagePlugin.py File 8.52 KB 0644
JpegImagePlugin.py File 27.16 KB 0644
JpegPresets.py File 12.41 KB 0644
McIdasImagePlugin.py File 1.71 KB 0644
MicImagePlugin.py File 2.54 KB 0644
MpegImagePlugin.py File 1.76 KB 0644
MpoImagePlugin.py File 4.14 KB 0644
MspImagePlugin.py File 5.43 KB 0644
PSDraw.py File 6.51 KB 0644
PaletteFile.py File 1.08 KB 0644
PalmImagePlugin.py File 8.89 KB 0644
PcdImagePlugin.py File 1.47 KB 0644
PcfFontFile.py File 6.2 KB 0644
PcxImagePlugin.py File 5.41 KB 0644
PdfImagePlugin.py File 7.49 KB 0644
PdfParser.py File 33.58 KB 0644
PixarImagePlugin.py File 1.61 KB 0644
PngImagePlugin.py File 42.79 KB 0644
PpmImagePlugin.py File 4.34 KB 0644
PsdImagePlugin.py File 7.56 KB 0644
PyAccess.py File 9.37 KB 0644
SgiImagePlugin.py File 5.96 KB 0644
SpiderImagePlugin.py File 9.31 KB 0644
SunImagePlugin.py File 4.2 KB 0644
TarIO.py File 1.41 KB 0644
TgaImagePlugin.py File 6.18 KB 0644
TiffImagePlugin.py File 66.86 KB 0644
TiffTags.py File 14.22 KB 0644
WalImageFile.py File 5.4 KB 0644
WebPImagePlugin.py File 10.54 KB 0644
WmfImagePlugin.py File 4.56 KB 0644
XVThumbImagePlugin.py File 1.9 KB 0644
XbmImagePlugin.py File 2.37 KB 0644
XpmImagePlugin.py File 3 KB 0644
__init__.py File 3.18 KB 0644
__main__.py File 41 B 0644
_binary.py File 1.75 KB 0644
_imaging.cpython-36m-x86_64-linux-gnu.so File 650.13 KB 0755
_imagingcms.cpython-36m-x86_64-linux-gnu.so File 38.05 KB 0755
_imagingft.cpython-36m-x86_64-linux-gnu.so File 41.98 KB 0755
_imagingmath.cpython-36m-x86_64-linux-gnu.so File 24.43 KB 0755
_imagingmorph.cpython-36m-x86_64-linux-gnu.so File 8.12 KB 0755
_imagingtk.cpython-36m-x86_64-linux-gnu.so File 9.37 KB 0755
_tkinter_finder.py File 224 B 0644
_util.py File 359 B 0644
_version.py File 50 B 0644
_webp.cpython-36m-x86_64-linux-gnu.so File 40.82 KB 0755
features.py File 8.8 KB 0644