def test_read_length_block(): data = b'\x00\x00\x00\x07\x01\x01\x01\x01\x01\x01\x01\x00' body = data[4:11] with io.BytesIO(data) as f: assert read_length_block(f, padding=1) == body assert f.tell() == 11 with io.BytesIO(data) as f: assert read_length_block(f, padding=2) == body assert f.tell() == 12
def read(cls, fp): data = read_length_block(fp) if len(data) == 0: return None with io.BytesIO(data) as f: return cls._read_body(f, len(data))
def read(cls, fp, encoding='macroman'): signature, key = read_fmt('4sH', fp) try: key = Resource(key) except ValueError: if Resource.is_path_info(key): logger.debug('Undefined PATH_INFO found: %d' % (key)) elif Resource.is_plugin_resource(key): logger.debug('Undefined PLUGIN_RESOURCE found: %d' % (key)) else: logger.warning('Unknown image resource %d' % (key)) name = read_pascal_string(fp, encoding, padding=2) raw_data = read_length_block(fp, padding=2) if key in TYPES: data = TYPES[key].frombytes(raw_data) # try: # _raw_data = data.tobytes(padding=1) # assert _raw_data == raw_data, '%r vs %r' % ( # _raw_data, raw_data # ) # except AssertionError as e: # logger.error(e) # raise else: data = raw_data return cls(signature, key, name, data)
def read(cls, fp, encoding='macroman', version=1): """Read the element from a file-like object. :param fp: file-like object :param encoding: encoding of the string :param version: psd file version :rtype: :py:class:`.LayerRecord` """ start_pos = fp.tell() top, left, bottom, right, num_channels = read_fmt('4iH', fp) channel_info = [ ChannelInfo.read(fp, version) for i in range(num_channels) ] signature, blend_mode, opacity, clipping = read_fmt('4s4sBB', fp) flags = LayerFlags.read(fp) data = read_length_block(fp, fmt='xI') logger.debug(' read layer record, len=%d' % (fp.tell() - start_pos)) with io.BytesIO(data) as f: self = cls( top, left, bottom, right, channel_info, signature, blend_mode, opacity, clipping, flags, *cls._read_extra(f, encoding, version) ) # with io.BytesIO() as f: # self._write_extra(f, encoding, version) # assert data == f.getvalue() return self
def read(cls, fp, version=1, padding=1): signature = read_fmt('4s', fp)[0] if signature not in cls._SIGNATURES: logger.warning('Invalid signature (%r)' % (signature)) fp.seek(-4, 1) return None key = read_fmt('4s', fp)[0] try: key = TaggedBlockID(key) except ValueError: message = 'Unknown key: %r' % (key) warn(message) logger.warning(message) fmt = cls._length_format(key, version) raw_data = read_length_block(fp, fmt=fmt, padding=padding) kls = TYPES.get(key) if kls: data = kls.frombytes(raw_data, version=version) # _raw_data = data.tobytes(version=version, # padding=1 if padding == 4 else 4) # assert raw_data == _raw_data, '%r: %s vs %s' % ( # kls, trimmed_repr(raw_data), trimmed_repr(_raw_data) # ) else: message = 'Unknown tagged block: %r, %s' % (key, trimmed_repr(raw_data)) warn(message) logger.warning(message) data = raw_data return cls(signature, key, data)
def read(cls, fp, **kwargs): items = [] while is_readable(fp, 8): data = read_length_block(fp, fmt='Q', padding=4) with io.BytesIO(data) as f: items.append(LinkedLayer.read(f)) return cls(items)
def read(cls, fp, **kwargs): items = [] while is_readable(fp, 4): data = read_length_block(fp, padding=4) with io.BytesIO(data) as f: items.append(Pattern.read(f)) return cls(items)
def read(cls, fp, encoding='macroman', version=1): data = read_length_block(fp, fmt=('I', 'Q')[version - 1]) logger.debug('reading layer info, len=%d' % (len(data))) if len(data) == 0: return LayerInfo() with io.BytesIO(data) as f: return cls._read_body(f, encoding, version)
def read(cls, fp): data = read_length_block(fp) # fmt? logger.debug('reading global layer mask info, len=%d' % (len(data))) if len(data) == 0: return cls(overlay_color=None) with io.BytesIO(data) as f: return cls._read_body(f)
def read(cls, fp, **kwargs): version = read_fmt('I', fp)[0] assert version in (1, 2, 3), 'Invalid version %d' % (version) items = [] while is_readable(fp, 8): with io.BytesIO(read_length_block(fp, fmt='Q', padding=4)) as f: items.append(FilterEffect.read(f)) return cls(version=version, items=items)
def read(cls, fp, **kwargs): uuid = read_pascal_string(fp, encoding='ascii', padding=1) version = read_fmt('I', fp)[0] assert version <= 1, 'Invalid version %d' % (version) with io.BytesIO(read_length_block(fp, fmt='Q')) as f: rectangle, depth, max_channels, channels = cls._read_body(f) # Documentation is incorrect here. extra = FilterEffectExtra.read(fp) if is_readable(fp) else None return cls(uuid, version, rectangle, depth, max_channels, channels, extra)
def read(cls, fp, encoding='macroman', version=1): start_pos = fp.tell() data = read_length_block(fp, fmt=('I', 'Q')[version - 1]) logger.debug('reading layer and mask info, len=%d, offset=%d' % (len(data), start_pos)) if len(data) == 0: return cls() with io.BytesIO(data) as f: return cls._read_body(f, encoding, version)
def read(cls, fp, encoding='macroman'): """Read the element from a file-like object. :param fp: file-like object :rtype: :py:class:`.ImageResources` """ data = read_length_block(fp) logger.debug('reading image resources, len=%d' % (len(data))) with io.BytesIO(data) as f: return cls._read_body(f, encoding=encoding)
def read(cls, fp): """Read the element from a file-like object. :param fp: file-like object :rtype: ColorModeData """ value = read_length_block(fp) logger.debug('reading color mode data, len=%d' % (len(value))) # TODO: Parse color table. return cls(value)
def read(cls, fp, **kwargs): version, count = read_fmt('2H', fp) items = [] for _ in range(count): signature = read_fmt('4s', fp)[0] assert signature == b'8BIM', 'Invalid signature %r' % (signature) ostype = EffectOSType(read_fmt('4s', fp)[0]) kls = cls.EFFECT_TYPES.get(ostype) items.append((ostype, kls.frombytes(read_length_block(fp)))) return cls(version=version, items=items)
def read(cls, fp, **kwargs): is_written = read_fmt('I', fp)[0] if is_written == 0: return cls(is_written=is_written) data = read_length_block(fp, fmt='Q') if len(data) == 0: return cls(is_written=is_written) with io.BytesIO(data) as f: compression = read_fmt('H', f)[0] data = f.read() return cls(is_written, compression, data)
def read(cls, fp): """Read the element from a file-like object. :param fp: file-like object :rtype: :py:class:`.MaskData` or None """ data = read_length_block(fp) if len(data) == 0: return None with io.BytesIO(data) as f: return cls._read_body(f, len(data))
def read(cls, fp, **kwargs): kind, is_open, flags, optional_blocks = read_fmt('4s2BH', fp) icon_location = read_fmt('4i', fp) popup_location = read_fmt('4i', fp) color = Color.read(fp) author = read_pascal_string(fp, 'macroman', padding=2) name = read_pascal_string(fp, 'macroman', padding=2) mod_date = read_pascal_string(fp, 'macroman', padding=2) length, marker = read_fmt('I4s', fp) data = read_length_block(fp) return cls(kind, is_open, flags, optional_blocks, icon_location, popup_location, color, author, name, mod_date, marker, data)
def read(cls, fp): """Read the element from a file-like object. :param fp: file-like object :rtype: :py:class:`.LayerBlendingRanges` """ data = read_length_block(fp) if len(data) == 0: return cls(None, None) with io.BytesIO(data) as f: return cls._read_body(f)
def read(cls, fp): """Read the element from a file-like object. :param fp: file-like object :rtype: :py:class:`.GlobalLayerMaskInfo` """ data = read_length_block(fp) logger.debug('reading global layer mask info, len=%d' % (len(data))) if len(data) == 0: return cls(overlay_color=None) with io.BytesIO(data) as f: return cls._read_body(f)
def read(cls, fp): is_written = read_fmt('B', fp)[0] if not is_written: return cls(is_written=is_written) rectangle = read_fmt('4i', fp) compression = 0 data = b'' with io.BytesIO(read_length_block(fp, fmt='Q')) as f: compression = read_fmt('H', f)[0] data = f.read() return cls(is_written, rectangle, compression, data)
def read(cls, fp, **kwargs): version = read_fmt('I', fp)[0] assert version == 3, 'Invalid version %d' % (version) data = read_length_block(fp) with io.BytesIO(data) as f: rectangle = read_fmt('4I', f) num_channels = read_fmt('I', f)[0] channels = [] for _ in range(num_channels + 2): channels.append(VirtualMemoryArray.read(f)) return cls(version, rectangle, channels)
def read(cls, fp, **kwargs): signature = read_fmt('4s', fp)[0] assert signature == b'8BIM', 'Invalid signature %r' % signature key, copy_on_sheet = read_fmt("4s?3x", fp) data = read_length_block(fp) if key in (b'mdyn', b'sgrp'): with io.BytesIO(data) as f: data = read_fmt('I', f)[0] elif key in cls._KNOWN_KEYS: data = DescriptorBlock.frombytes(data, padding=4) else: message = 'Unknown metadata key %r' % (key) logger.warning(message) data = data return cls(signature, key, copy_on_sheet, data)
def read(cls, fp): pos = fp.tell() data = read_length_block(fp) # fmt? logger.debug('reading global layer mask info, len=%d' % (len(data))) if len(data) == 0: return cls(overlay_color=None) elif len(data) < 13: logger.warning( 'global layer mask info is broken, expected 13 bytes but found ' 'only %d' % (len(data))) fp.seek(pos) return cls(overlay_color=None) with io.BytesIO(data) as f: return cls._read_body(f)
def read(cls, fp, encoding='macroman', version=1): """Read the element from a file-like object. :param fp: file-like object :param encoding: encoding of the string :param version: psd file version :rtype: LayerAndMaskInformation """ start_pos = fp.tell() data = read_length_block(fp, fmt=('I', 'Q')[version - 1]) logger.debug('reading layer and mask info, len=%d, offset=%d' % (len(data), start_pos)) if len(data) == 0: return cls() with io.BytesIO(data) as f: return cls._read_body(f, encoding, version)
def read(cls, fp, encoding='macroman', version=1): """Read the element from a file-like object. :param fp: file-like object :param encoding: encoding of the string :param version: psd file version :rtype: :py:class:`.LayerInfo` """ data = read_length_block(fp, fmt=('I', 'Q')[version - 1]) logger.debug('reading layer info, len=%d' % (len(data))) if len(data) == 0: return LayerInfo() with io.BytesIO(data) as f: return cls._read_body(f, encoding, version)
def read(cls, fp, encoding='macroman', version=1): start_pos = fp.tell() top, left, bottom, right, num_channels = read_fmt('4iH', fp) channel_info = [ ChannelInfo.read(fp, version) for i in range(num_channels) ] signature, blend_mode, opacity, clipping = read_fmt('4s4sBB', fp) flags = LayerFlags.read(fp) data = read_length_block(fp, fmt='xI') logger.debug(' read layer record, len=%d' % (fp.tell() - start_pos)) with io.BytesIO(data) as f: self = cls(top, left, bottom, right, channel_info, signature, blend_mode, opacity, clipping, flags, *cls._read_extra(f, encoding, version)) # with io.BytesIO() as f: # self._write_extra(f, encoding, version) # assert data == f.getvalue() return self
def read(cls, fp, encoding='macroman'): data = read_length_block(fp) logger.debug('reading image resources, len=%d' % (len(data))) with io.BytesIO(data) as f: return cls._read_body(f, encoding=encoding)
def read(cls, fp, **kwargs): items = [] while is_readable(fp, 8): items.append(read_length_block(fp, fmt='Q')) return cls(items)
def read(cls, fp): return cls(read_length_block(fp))