Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
gif.py358 linesDownload Raw Back to kaitaistruct
1# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild2 3import kaitaistruct4from kaitaistruct import KaitaiStruct, KaitaiStream, BytesIO5from enum import Enum6 7 8if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 9):9    raise Exception("Incompatible Kaitai Struct Python API: 0.9 or later is required, but you have %s" % (kaitaistruct.__version__))10 11class Gif(KaitaiStruct):12    """GIF (Graphics Interchange Format) is an image file format, developed13    in 1987. It became popular in 1990s as one of the main image formats14    used in World Wide Web.15    16    GIF format allows encoding of palette-based images up to 256 colors17    (each of the colors can be chosen from a 24-bit RGB18    colorspace). Image data stream uses LZW (Lempel-Ziv-Welch) lossless19    compression.20    21    Over the years, several version of the format were published and22    several extensions to it were made, namely, a popular Netscape23    extension that allows to store several images in one file, switching24    between them, which produces crude form of animation.25    26    Structurally, format consists of several mandatory headers and then27    a stream of blocks follows. Blocks can carry additional28    metainformation or image data.29    """30 31    class BlockType(Enum):32        extension = 3333        local_image_descriptor = 4434        end_of_file = 5935 36    class ExtensionLabel(Enum):37        graphic_control = 24938        comment = 25439        application = 25540    def __init__(self, _io, _parent=None, _root=None):41        self._io = _io42        self._parent = _parent43        self._root = _root if _root else self44        self._read()45 46    def _read(self):47        self.hdr = Gif.Header(self._io, self, self._root)48        self.logical_screen_descriptor = Gif.LogicalScreenDescriptorStruct(self._io, self, self._root)49        if self.logical_screen_descriptor.has_color_table:50            self._raw_global_color_table = self._io.read_bytes((self.logical_screen_descriptor.color_table_size * 3))51            _io__raw_global_color_table = KaitaiStream(BytesIO(self._raw_global_color_table))52            self.global_color_table = Gif.ColorTable(_io__raw_global_color_table, self, self._root)53 54        self.blocks = []55        i = 056        while True:57            _ = Gif.Block(self._io, self, self._root)58            self.blocks.append(_)59            if  ((self._io.is_eof()) or (_.block_type == Gif.BlockType.end_of_file)) :60                break61            i += 162 63    class ImageData(KaitaiStruct):64        """65        .. seealso::66           - section 22 - https://www.w3.org/Graphics/GIF/spec-gif89a.txt67        """68        def __init__(self, _io, _parent=None, _root=None):69            self._io = _io70            self._parent = _parent71            self._root = _root if _root else self72            self._read()73 74        def _read(self):75            self.lzw_min_code_size = self._io.read_u1()76            self.subblocks = Gif.Subblocks(self._io, self, self._root)77 78 79    class ColorTableEntry(KaitaiStruct):80        def __init__(self, _io, _parent=None, _root=None):81            self._io = _io82            self._parent = _parent83            self._root = _root if _root else self84            self._read()85 86        def _read(self):87            self.red = self._io.read_u1()88            self.green = self._io.read_u1()89            self.blue = self._io.read_u1()90 91 92    class LogicalScreenDescriptorStruct(KaitaiStruct):93        """94        .. seealso::95           - section 18 - https://www.w3.org/Graphics/GIF/spec-gif89a.txt96        """97        def __init__(self, _io, _parent=None, _root=None):98            self._io = _io99            self._parent = _parent100            self._root = _root if _root else self101            self._read()102 103        def _read(self):104            self.screen_width = self._io.read_u2le()105            self.screen_height = self._io.read_u2le()106            self.flags = self._io.read_u1()107            self.bg_color_index = self._io.read_u1()108            self.pixel_aspect_ratio = self._io.read_u1()109 110        @property111        def has_color_table(self):112            if hasattr(self, '_m_has_color_table'):113                return self._m_has_color_table114 115            self._m_has_color_table = (self.flags & 128) != 0116            return getattr(self, '_m_has_color_table', None)117 118        @property119        def color_table_size(self):120            if hasattr(self, '_m_color_table_size'):121                return self._m_color_table_size122 123            self._m_color_table_size = (2 << (self.flags & 7))124            return getattr(self, '_m_color_table_size', None)125 126 127    class LocalImageDescriptor(KaitaiStruct):128        def __init__(self, _io, _parent=None, _root=None):129            self._io = _io130            self._parent = _parent131            self._root = _root if _root else self132            self._read()133 134        def _read(self):135            self.left = self._io.read_u2le()136            self.top = self._io.read_u2le()137            self.width = self._io.read_u2le()138            self.height = self._io.read_u2le()139            self.flags = self._io.read_u1()140            if self.has_color_table:141                self._raw_local_color_table = self._io.read_bytes((self.color_table_size * 3))142                _io__raw_local_color_table = KaitaiStream(BytesIO(self._raw_local_color_table))143                self.local_color_table = Gif.ColorTable(_io__raw_local_color_table, self, self._root)144 145            self.image_data = Gif.ImageData(self._io, self, self._root)146 147        @property148        def has_color_table(self):149            if hasattr(self, '_m_has_color_table'):150                return self._m_has_color_table151 152            self._m_has_color_table = (self.flags & 128) != 0153            return getattr(self, '_m_has_color_table', None)154 155        @property156        def has_interlace(self):157            if hasattr(self, '_m_has_interlace'):158                return self._m_has_interlace159 160            self._m_has_interlace = (self.flags & 64) != 0161            return getattr(self, '_m_has_interlace', None)162 163        @property164        def has_sorted_color_table(self):165            if hasattr(self, '_m_has_sorted_color_table'):166                return self._m_has_sorted_color_table167 168            self._m_has_sorted_color_table = (self.flags & 32) != 0169            return getattr(self, '_m_has_sorted_color_table', None)170 171        @property172        def color_table_size(self):173            if hasattr(self, '_m_color_table_size'):174                return self._m_color_table_size175 176            self._m_color_table_size = (2 << (self.flags & 7))177            return getattr(self, '_m_color_table_size', None)178 179 180    class Block(KaitaiStruct):181        def __init__(self, _io, _parent=None, _root=None):182            self._io = _io183            self._parent = _parent184            self._root = _root if _root else self185            self._read()186 187        def _read(self):188            self.block_type = KaitaiStream.resolve_enum(Gif.BlockType, self._io.read_u1())189            _on = self.block_type190            if _on == Gif.BlockType.extension:191                self.body = Gif.Extension(self._io, self, self._root)192            elif _on == Gif.BlockType.local_image_descriptor:193                self.body = Gif.LocalImageDescriptor(self._io, self, self._root)194 195 196    class ColorTable(KaitaiStruct):197        """198        .. seealso::199           - section 19 - https://www.w3.org/Graphics/GIF/spec-gif89a.txt200        """201        def __init__(self, _io, _parent=None, _root=None):202            self._io = _io203            self._parent = _parent204            self._root = _root if _root else self205            self._read()206 207        def _read(self):208            self.entries = []209            i = 0210            while not self._io.is_eof():211                self.entries.append(Gif.ColorTableEntry(self._io, self, self._root))212                i += 1213 214 215 216    class Header(KaitaiStruct):217        """218        .. seealso::219           - section 17 - https://www.w3.org/Graphics/GIF/spec-gif89a.txt220        """221        def __init__(self, _io, _parent=None, _root=None):222            self._io = _io223            self._parent = _parent224            self._root = _root if _root else self225            self._read()226 227        def _read(self):228            self.magic = self._io.read_bytes(3)229            if not self.magic == b"\x47\x49\x46":230                raise kaitaistruct.ValidationNotEqualError(b"\x47\x49\x46", self.magic, self._io, u"/types/header/seq/0")231            self.version = (self._io.read_bytes(3)).decode(u"ASCII")232 233 234    class ExtGraphicControl(KaitaiStruct):235        """236        .. seealso::237           - section 23 - https://www.w3.org/Graphics/GIF/spec-gif89a.txt238        """239        def __init__(self, _io, _parent=None, _root=None):240            self._io = _io241            self._parent = _parent242            self._root = _root if _root else self243            self._read()244 245        def _read(self):246            self.block_size = self._io.read_bytes(1)247            if not self.block_size == b"\x04":248                raise kaitaistruct.ValidationNotEqualError(b"\x04", self.block_size, self._io, u"/types/ext_graphic_control/seq/0")249            self.flags = self._io.read_u1()250            self.delay_time = self._io.read_u2le()251            self.transparent_idx = self._io.read_u1()252            self.terminator = self._io.read_bytes(1)253            if not self.terminator == b"\x00":254                raise kaitaistruct.ValidationNotEqualError(b"\x00", self.terminator, self._io, u"/types/ext_graphic_control/seq/4")255 256        @property257        def transparent_color_flag(self):258            if hasattr(self, '_m_transparent_color_flag'):259                return self._m_transparent_color_flag260 261            self._m_transparent_color_flag = (self.flags & 1) != 0262            return getattr(self, '_m_transparent_color_flag', None)263 264        @property265        def user_input_flag(self):266            if hasattr(self, '_m_user_input_flag'):267                return self._m_user_input_flag268 269            self._m_user_input_flag = (self.flags & 2) != 0270            return getattr(self, '_m_user_input_flag', None)271 272 273    class Subblock(KaitaiStruct):274        def __init__(self, _io, _parent=None, _root=None):275            self._io = _io276            self._parent = _parent277            self._root = _root if _root else self278            self._read()279 280        def _read(self):281            self.len_bytes = self._io.read_u1()282            self.bytes = self._io.read_bytes(self.len_bytes)283 284 285    class ApplicationId(KaitaiStruct):286        def __init__(self, _io, _parent=None, _root=None):287            self._io = _io288            self._parent = _parent289            self._root = _root if _root else self290            self._read()291 292        def _read(self):293            self.len_bytes = self._io.read_u1()294            if not self.len_bytes == 11:295                raise kaitaistruct.ValidationNotEqualError(11, self.len_bytes, self._io, u"/types/application_id/seq/0")296            self.application_identifier = (self._io.read_bytes(8)).decode(u"ASCII")297            self.application_auth_code = self._io.read_bytes(3)298 299 300    class ExtApplication(KaitaiStruct):301        def __init__(self, _io, _parent=None, _root=None):302            self._io = _io303            self._parent = _parent304            self._root = _root if _root else self305            self._read()306 307        def _read(self):308            self.application_id = Gif.ApplicationId(self._io, self, self._root)309            self.subblocks = []310            i = 0311            while True:312                _ = Gif.Subblock(self._io, self, self._root)313                self.subblocks.append(_)314                if _.len_bytes == 0:315                    break316                i += 1317 318 319    class Subblocks(KaitaiStruct):320        def __init__(self, _io, _parent=None, _root=None):321            self._io = _io322            self._parent = _parent323            self._root = _root if _root else self324            self._read()325 326        def _read(self):327            self.entries = []328            i = 0329            while True:330                _ = Gif.Subblock(self._io, self, self._root)331                self.entries.append(_)332                if _.len_bytes == 0:333                    break334                i += 1335 336 337    class Extension(KaitaiStruct):338        def __init__(self, _io, _parent=None, _root=None):339            self._io = _io340            self._parent = _parent341            self._root = _root if _root else self342            self._read()343 344        def _read(self):345            self.label = KaitaiStream.resolve_enum(Gif.ExtensionLabel, self._io.read_u1())346            _on = self.label347            if _on == Gif.ExtensionLabel.application:348                self.body = Gif.ExtApplication(self._io, self, self._root)349            elif _on == Gif.ExtensionLabel.comment:350                self.body = Gif.Subblocks(self._io, self, self._root)351            elif _on == Gif.ExtensionLabel.graphic_control:352                self.body = Gif.ExtGraphicControl(self._io, self, self._root)353            else:354                self.body = Gif.Subblocks(self._io, self, self._root)355 356 357 358