Ejemplo n.º 1
0
 def read(cls, fp, encoding='macroman'):
     signature, key = read_fmt('4sH', fp)
     try:
         key = ImageResourceID(key)
     except ValueError:
         if ImageResourceID.is_path_info(key):
             logger.debug('Undefined PATH_INFO found: %d' % (key))
         elif ImageResourceID.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)
Ejemplo n.º 2
0
 def __repr__(self):
     return "ImageResource(%r %s, %r, %s)" % (
         self.resource_id,
         ImageResourceID.name_of(self.resource_id),
         self.name,
         trimmed_repr(self.data),
     )
Ejemplo n.º 3
0
 def image_resource_blocks(self):
     """Returns dict of the underlying image resource blocks."""
     if not self._image_resource_blocks:
         self._image_resource_blocks = {
             ImageResourceID.name_of(block.resource_id).lower(): block.data
             for block in self.decoded_data.image_resource_blocks
         }
     return self._image_resource_blocks
Ejemplo n.º 4
0
def parse_image_resource(resource):
    """
    Replaces ``data`` of image resource block with a parsed data structure.
    """
    if not ImageResourceID.is_known(resource.resource_id):
        warnings.warn("Unknown resource_id (%s)" % resource.resource_id)

    decoder = _image_resource_decoders.get(resource.resource_id, lambda data: data)
    return resource._replace(data = decoder(resource.data))
Ejemplo n.º 5
0
def parse_image_resource(resource):
    """
    Replaces ``data`` of image resource block with a parsed data structure.
    """
    if not ImageResourceID.is_known(resource.resource_id):
        warnings.warn("Unknown resource_id (%s)" % resource.resource_id)

    decoder = _image_resource_decoders.get(resource.resource_id,
                                           lambda data: data)
    return resource._replace(data=decoder(resource.data))
Ejemplo n.º 6
0
 def _repr_pretty_(self, p, cycle):
     if cycle:
         p.text(repr(self))
     else:
         with p.group(0, 'ImageResource(', ')'):
             p.text("%r %s, %r, " % (
                 self.resource_id, ImageResourceID.name_of(self.resource_id),
                 self.name
             ))
             if isinstance(self.data, bytes):
                 p.text(trimmed_repr(self.data))
             else:
                 p.pretty(self.data)
Ejemplo n.º 7
0
    def image_resource_blocks(self):
        """
        Returns dict of the underlying image resource blocks. See
        :py:mod:`psd_tools.decoder.image_resources`

        :rtype: `dict`
        """
        if not self._image_resource_blocks:
            self._image_resource_blocks = {
                ImageResourceID.name_of(block.resource_id).lower(): block.data
                for block in self.decoded_data.image_resource_blocks
            }
        return self._image_resource_blocks
Ejemplo n.º 8
0
def parse_image_resource(resource):
    """
    Replaces ``data`` of image resource block with a parsed data structure.
    """
    if not ImageResourceID.is_known(resource.resource_id):
        warnings.warn("Unknown resource_id (%s)" % resource.resource_id)

    if (ImageResourceID.PATH_INFO_0 <= resource.resource_id and
            ImageResourceID.PATH_INFO_LAST >= resource.resource_id):
        decoder = decode_path_resource
    else:
        decoder = _image_resource_decoders.get(resource.resource_id,
                                               lambda data: data)
    return resource._replace(data=decoder(resource.data))
Ejemplo n.º 9
0
def _read_block(fp, encoding):
    """
    Reads single image resource block. Such blocks contain non-pixel data
    for the images (e.g. pen tool paths).
    """
    sig = fp.read(4)
    if not sig in (b'8BIM', b'MeSa'):
        raise Error("Invalid resource signature (%r)" % sig)

    resource_id = read_fmt("H", fp)[0]
    name = read_pascal_string(fp, encoding, 2)

    data_size = pad(read_fmt("I", fp)[0], 2)
    if not data_size:
        logger.debug(
            "Found image resource with no data (%d %s). Dropping..." % (
            resource_id, ImageResourceID.name_of(resource_id)
        ))
        return None

    data = fp.read(data_size)
    return ImageResource(resource_id, name, data)
Ejemplo n.º 10
0
 def __repr__(self):
     return "ImageResource(%r %s, %r, %s)" % (
         self.resource_id, ImageResourceID.name_of(
             self.resource_id), self.name, trimmed_repr(self.data))