Esempio n. 1
0
def _channels_data_to_image(channels_data, mode, size, depth):

    if size == (0, 0):
        return

    w, h = size
    num_channels = mode.length
    assert depth == 8
    assert len(channels_data) == num_channels

    total_size = w * h * num_channels
    image_bytes = array.array(str("B"), [0] * total_size)

    for index, channel in enumerate(channels_data):

        # zip and zip-with-prediction data is already decoded
        data = channel.data
        if channel.compression == Compression.PACK_BITS:
            data = packbits.decode(data)

        image_bytes[index::num_channels] = array.array(str("B"), data)

    pixels = get_pixel_array(image_bytes, w, h, mode.length)

    return LoadedImage(mode, w, h, pixels)
Esempio n. 2
0
def decode_packbits(data, height, version):
    with io.BytesIO(data) as fp:
        bytes_counts = read_be_array(('H', 'I')[version - 1], height, fp)
        result = b''
        for count in bytes_counts:
            result += packbits.decode(fp.read(count))
    return result
Esempio n. 3
0
def channel_decode(channel, depth, width, height):
    '''psd channel data to full list data
    [psd channel] ->    [
                            [line]
                            [line]
                            ...
                        ]
    '''
    channel_data = packbits.decode(
        channel.data
    ) if channel.compression == Compression.PACK_BITS else channel.data
    channel_data = array_from_raw(channel_data, depth)
    bl_channel = numpy.reshape(channel_data, (height, width))
    bl_channel = numpy.flipud(bl_channel)
    return bl_channel
Esempio n. 4
0
def decompress_rle(
        data,  # type: bytes
        shape,  # type: Tuple[int, int]
        depth,  # type: int
        version  # type: int
):  # type: (...) -> np.ndarray
    """
    Decompress run length encoded data.

{}
    """
    output = packbits.decode(data, shape[0], shape[1],
                             color_depth_size_map[depth], version)

    # Now pass along to the raw decoder to get a Numpy array
    return decompress_raw(output, shape, depth, version)
Esempio n. 5
0
def _decompress_pattern_channel(channel):
    depth = channel.depth
    size = (channel.rectangle[3], channel.rectangle[2])
    if channel.compression in (Compression.RAW, Compression.ZIP,
                               Compression.ZIP_WITH_PREDICTION):
        if depth == 8:
            im = _from_8bit_raw(channel.data.value, size)
        elif depth == 16:
            im = _from_16bit_raw(channel.data.value, size)
        elif depth == 32:
            im = _from_32bit_raw(channel.data.value, size)
        else:
            warnings.warn("Unsupported depth (%s)" % depth)
            return None
    elif channel.compression == Compression.PACK_BITS:
        if depth != 8:
            warnings.warn(
                "Depth %s is unsupported for PackBits compression" % depth)
        try:
            import packbits
            channel_data = packbits.decode(channel.data.value)
        except ImportError as e:
            warnings.warn("Install packbits (%s)" % e)
            channel_data = b'\x00' * (size[0] * size[1])  # Default fill
        except IndexError as e:
            warnings.warn("Failed to decode pattern (%s)" % e)
            channel_data = b'\x00' * (size[0] * size[1])  # Default fill
        # Packbit pattern tends not to have the correct size ???
        padding = len(channel_data) - size[0] * size[1]
        if padding < 0:
            warnings.warn('Broken pattern data (%g for %g)' % (
                len(channel_data), size[0] * size[1]))
            channel_data += b'\x00' * -padding  # Append default fill
            padding = 0
        im = frombytes('L', size, channel_data[padding:], "raw", 'L')
    else:
        if Compression.is_known(channel.compression):
            warnings.warn(
                "Compression method is not implemented "
                "(%s)" % channel.compression)
        else:
            warnings.warn(
                "Unknown compression method (%s)" % channel.compression)
        return None
    return im.convert('L')
Esempio n. 6
0
def _channels_data_to_image(channels_data, mode, size, depth):

    if size == (0, 0):
        return

    num_channels = mode.length
    assert depth == 8
    assert len(channels_data) == num_channels

    total_size = size[0]*size[1]*num_channels
    image_bytes = array.array(str("B"), [0]*total_size)

    for index, channel in enumerate(channels_data):

        data = channel.data # zip and zip-with-prediction data is already decoded
        if channel.compression == Compression.PACK_BITS:
            data = packbits.decode(data)

        image_bytes[index::num_channels] = array.array(str("B"), data)

    pixels = get_pixel_array(image_bytes, size[0], size[1], mode.length)

    return Image(pixels, mode)
Esempio n. 7
0
def test_raw():
    encoded = packbits.encode(b'123')
    assert encoded == b'\x02123'
    assert packbits.decode(encoded) == b'123'
Esempio n. 8
0
def test_encode_long_raw():
    data = b"12345678" * 17
    encoded = packbits.encode(data)
    print(encoded)
    assert packbits.decode(encoded) == data
Esempio n. 9
0
def test_encode_long_rle2():
    data = b"1" * 127
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 10
0
def test_encode_long():
    data = b'1' * 128 + b'12345678' * 17
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 11
0
def test_encode_long_raw():
    data = b'12345678' * 17
    encoded = packbits.encode(data)
    print(encoded)
    assert packbits.decode(encoded) == data
Esempio n. 12
0
def test_encode_long_rle3():
    data = b'1' * 128
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 13
0
def test_encode_long_rle3():
    data = b'1' * 128
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 14
0
def test_encode_switching_rle():
    encoded = packbits.encode(b'1122')
    assert packbits.decode(encoded) == b'1122'
Esempio n. 15
0
def test_encode2():
    encoded = packbits.encode(b'112112')
    assert packbits.decode(encoded) == b'112112'
Esempio n. 16
0
def test_raw():
    encoded = packbits.encode(b'123')
    assert encoded == b'\x02123'
    assert packbits.decode(encoded) == b'123'
Esempio n. 17
0
def test_encode_single():
    encoded = packbits.encode(b'X')
    assert encoded == b'\x00X'
    assert packbits.decode(encoded) == b'X'
Esempio n. 18
0
def test_encode_decode():
    encoded = packbits.encode(RESULT)
    decoded = packbits.decode(encoded)
    assert decoded == RESULT
Esempio n. 19
0
def test_encode2():
    encoded = packbits.encode(b'112112')
    assert packbits.decode(encoded) == b'112112'
Esempio n. 20
0
def test_encode_switching_rle():
    encoded = packbits.encode(b'1122')
    assert packbits.decode(encoded) == b'1122'
Esempio n. 21
0
def test_restart_rle():
    data = b'1' * 127 + b'foo'
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 22
0
def test_restart_rle():
    data = b'1' * 127 + b'foo'
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 23
0
def test_encode_long_raw():
    data = b'12345678' * 16
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 24
0
def test_encode_long_raw2():
    data = b'12345678' * 16
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 25
0
def test_encode_long():
    data = b'1' * 128 + b'12345678' * 17
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 26
0
def test_decode():
    decoded = packbits.decode(DATA)
    assert decoded == RESULT
Esempio n. 27
0
def test_raw():
    encoded = packbits.encode(b"123")
    assert encoded == b"\x02123"
    assert packbits.decode(encoded) == b"123"
Esempio n. 28
0
def test_encode2():
    encoded = packbits.encode(b"112112")
    assert packbits.decode(encoded) == b"112112"
Esempio n. 29
0
def test_encode_single():
    encoded = packbits.encode(b'X')
    assert encoded == b'\x00X'
    assert packbits.decode(encoded) == b'X'
Esempio n. 30
0
def test_decode():
    decoded = packbits.decode(DATA)
    assert decoded == RESULT
Esempio n. 31
0
def test_restart_rle():
    data = b"1" * 127 + b"foo"
    encoded = packbits.encode(data)
    assert packbits.decode(encoded) == data
Esempio n. 32
0
def test_encode_decode():
    encoded = packbits.encode(RESULT)
    decoded = packbits.decode(encoded)
    assert decoded == RESULT
Esempio n. 33
0
def test_encode_single():
    encoded = packbits.encode(b"X")
    assert encoded == b"\x00X"
    assert packbits.decode(encoded) == b"X"