SickGear/lib/hachoir/parser/image/ico.py
JackDandy 980e05cc99 Change Hachoir can't support PY2 so backport their PY3 to prevent a need for system dependant external binaries like mediainfo.
Backported 400 revisions from rev 1de4961-8897c5b (2018-2014).
Move core/benchmark, core/cmd_line, core/memory, core/profiler and core/timeout to core/optional/*
Remove metadata/qt*

PORT: Version 2.0a3 (inline with 3.0a3 @ f80c7d5).
Basic Support for XMP Packets.
tga: improvements to adhere more closely to the spec.
pdf: slightly improved parsing.
rar: fix TypeError on unknown block types.
Add MacRoman win32 codepage.
tiff/exif: support SubIFDs and tiled images.
Add method to export metadata in dictionary.
mpeg_video: don't attempt to parse Stream past length.
mpeg_video: parse ESCR correctly, add SCR value.
Change centralise CustomFragments.
field: don't set parser class if class is None, to enable autodetect.
field: add value/display for CustomFragment.
parser: inline warning to enable tracebacks in debug mode.
Fix empty bytestrings in makePrintable.
Fix contentSize in jpeg.py to account for image_data blocks.
Fix the ELF parser.
Enhance the AR archive parser.
elf parser: fix wrong wrong fields order in parsing little endian section flags.
elf parser: add s390 as a machine type.
Flesh out mp4 parser.

PORT: Version 2.0a1 (inline with 3.0a1).
Major refactoring and PEP8.
Fix ResourceWarning warnings on files. Add a close() method and support for the context manager protocol ("with obj: ...") to parsers, input and output streams.
metadata: get comment from ZIP.
Support for InputIOStream.read(0).
Fix sizeGe when size is None.
Remove unused new_seekable_field_set file.
Remove parser Mapsforge .map.
Remove parser Parallel Realities Starfighter .pak files.
sevenzip: fix for newer archives.
java: update access flags and modifiers for Java 1.7 and update description text for most recent Java.
Support ustar prefix field in tar archives.
Remove file_system* parsers.
Remove misc parsers 3d0, 3ds, gnome_keyring, msoffice*, mstask, ole*, word*.
Remove program parsers macho, nds, prc.
Support non-8bit Character subclasses.
Python parser supports Python 3.7.
Enhance mpeg_ts parser to support MTS/M2TS.
Support for creation date in tiff.
Change don't hardcode errno constant.

PORT: 1.9.1
Internal Only: The following are legacy reference to upstream commit messages.
Relevant changes up to b0a115f8.
Use integer division.
Replace HACHOIR_ERRORS with Exception.
Fix metadata.Data: make it sortable.
Import fixes from e7de492.
PORT: Version 2.0a1 (inline with 3.0a1 @ e9f8fad).
Replace hachoir.core.field with hachoir.field
Replace hachoir.core.stream with hachoir.stream
Remove the compatibility module for PY1.5 to PY2.5.
metadata: support TIFF picture.
metadata: fix string normalization.
metadata: fix datetime regex Fix hachoir bug #57.
FileFromInputStream: fix comparison between None and an int.
InputIOStream: open the file in binary mode.
2018-03-28 00:43:11 +01:00

141 lines
4.9 KiB
Python

"""
Microsoft Windows icon and cursor file format parser.
Author: Victor Stinner
"""
from hachoir.parser import Parser
from hachoir.field import (FieldSet, ParserError,
UInt8, UInt16, UInt32, Enum, RawBytes)
from hachoir.parser.image.common import PaletteRGBA
from hachoir.core.endian import LITTLE_ENDIAN
from hachoir.parser.common.win32 import BitmapInfoHeader
class IconHeader(FieldSet):
def createFields(self):
yield UInt8(self, "width", "Width")
yield UInt8(self, "height", "Height")
yield UInt8(self, "nb_color", "Number of colors")
yield UInt8(self, "reserved", "(reserved)")
yield UInt16(self, "planes", "Color planes (=1)")
yield UInt16(self, "bpp", "Bits per pixel")
yield UInt32(self, "size", "Content size in bytes")
yield UInt32(self, "offset", "Data offset")
def createDescription(self):
return "Icon: %ux%u pixels, %u bits/pixel" % \
(self["width"].value, self["height"].value, self["bpp"].value)
def isValid(self):
if self["nb_color"].value == 0:
if self["bpp"].value in (8, 24, 32) and self["planes"].value == 1:
return True
if self["planes"].value == 4 and self["bpp"].value == 0:
return True
elif self["nb_color"].value == 16:
if self["bpp"].value in (4, 16) and self["planes"].value == 1:
return True
else:
return False
if self["bpp"].value == 0 and self["planes"].value == 0:
return True
return False
class IconData(FieldSet):
def __init__(self, parent, name, header):
FieldSet.__init__(self, parent, name, "Icon data")
self.header = header
def createFields(self):
yield BitmapInfoHeader(self, "header")
# Read palette if needed
nb_color = self.header["nb_color"].value
if self.header["bpp"].value == 8:
nb_color = 256
if nb_color != 0:
yield PaletteRGBA(self, "palette", nb_color)
# Read pixels
size = self.header["size"].value - self.current_size // 8
yield RawBytes(self, "pixels", size, "Image pixels")
class IcoFile(Parser):
endian = LITTLE_ENDIAN
PARSER_TAGS = {
"id": "ico",
"category": "image",
"file_ext": ("ico", "cur"),
"mime": (u"image/x-ico",),
"min_size": (22 + 40) * 8,
# "magic": (
# ("\0\0\1\0", 0), # Icon
# ("\0\0\2\0", 0), # Cursor
# ),
"magic_regex": ((
# signature=0, type=(1|2), count in 1..20,
"\0\0[\1\2]\0[\x01-\x14]."
# size=(16x16|32x32|48x48|64x64),
"(\x10\x10|\x20\x20|\x30\x30|\x40\x40)"
# nb_color=0 or 16; nb_plane=(0|1|4), bpp=(0|8|24|32)
"[\x00\x10]\0[\0\1\4][\0\x08\x18\x20]\0",
0),),
"description": "Microsoft Windows icon or cursor",
}
TYPE_NAME = {
1: "icon",
2: "cursor"
}
def validate(self):
# Check signature and type
if self["signature"].value != 0:
return "Wrong file signature"
if self["type"].value not in self.TYPE_NAME:
return "Unknown picture type"
# Check all icon headers
index = -1
for field in self:
if field.name.startswith("icon_header"):
index += 1
if not field.isValid():
return "Invalid header #%u" % index
elif 0 <= index:
break
return True
def createFields(self):
yield UInt16(self, "signature", "Signature (0x0000)")
yield Enum(UInt16(self, "type", "Resource type"), self.TYPE_NAME)
yield UInt16(self, "nb_items", "Number of items")
items = []
for index in xrange(self["nb_items"].value):
item = IconHeader(self, "icon_header[]")
yield item
items.append(item)
for header in items:
if header["offset"].value * 8 != self.current_size:
raise ParserError("Icon: Problem with icon data offset.")
yield IconData(self, "icon_data[]", header)
def createDescription(self):
desc = "Microsoft Windows %s" % self["type"].display
size = []
for header in self.array("icon_header"):
size.append("%ux%ux%u" % (header["width"].value,
header["height"].value, header["bpp"].value))
if size:
return "%s: %s" % (desc, ", ".join(size))
else:
return desc
def createContentSize(self):
count = self["nb_items"].value
if not count:
return None
field = self["icon_data[%u]" % (count - 1)]
return field.absolute_address + field.size