summaryrefslogtreecommitdiff
path: root/python/examples/kaitai/jpeg.py
blob: 54c28bb6325ef148dacf4ec9914158f5f9a8c6c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

from pkg_resources import parse_version
from .kaitaistruct import __version__ as ks_version, KaitaiStruct, KaitaiStream, BytesIO
from enum import Enum
import collections


if parse_version(ks_version) < parse_version('0.7'):
    raise Exception("Incompatible Kaitai Struct Python API: 0.7 or later is required, but you have %s" % (ks_version))

from .exif import Exif
class Jpeg(KaitaiStruct):

    class ComponentId(Enum):
        y = 1
        cb = 2
        cr = 3
        i = 4
        q = 5
    SEQ_FIELDS = ["segments"]
    def __init__(self, _io, _parent=None, _root=None):
        self._io = _io
        self._parent = _parent
        self._root = _root if _root else self
        self._debug = collections.defaultdict(dict)

    def _read(self):
        self._debug['segments']['start'] = self._io.pos()
        self.segments = []
        i = 0
        while not self._io.is_eof():
            if not 'arr' in self._debug['segments']:
                self._debug['segments']['arr'] = []
            self._debug['segments']['arr'].append({'start': self._io.pos()})
            _t_segments = self._root.Segment(self._io, self, self._root)
            _t_segments._read()
            self.segments.append(_t_segments)
            self._debug['segments']['arr'][len(self.segments) - 1]['end'] = self._io.pos()
            i += 1

        self._debug['segments']['end'] = self._io.pos()

    class Segment(KaitaiStruct):

        class MarkerEnum(Enum):
            tem = 1
            sof0 = 192
            sof1 = 193
            sof2 = 194
            sof3 = 195
            dht = 196
            sof5 = 197
            sof6 = 198
            sof7 = 199
            soi = 216
            eoi = 217
            sos = 218
            dqt = 219
            dnl = 220
            dri = 221
            dhp = 222
            app0 = 224
            app1 = 225
            app2 = 226
            app3 = 227
            app4 = 228
            app5 = 229
            app6 = 230
            app7 = 231
            app8 = 232
            app9 = 233
            app10 = 234
            app11 = 235
            app12 = 236
            app13 = 237
            app14 = 238
            app15 = 239
            com = 254
        SEQ_FIELDS = ["magic", "marker", "length", "data", "image_data"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['magic']['start'] = self._io.pos()
            self.magic = self._io.ensure_fixed_contents(b"\xFF")
            self._debug['magic']['end'] = self._io.pos()
            self._debug['marker']['start'] = self._io.pos()
            self.marker = KaitaiStream.resolve_enum(self._root.Segment.MarkerEnum, self._io.read_u1())
            self._debug['marker']['end'] = self._io.pos()
            if  ((self.marker != self._root.Segment.MarkerEnum.soi) and (self.marker != self._root.Segment.MarkerEnum.eoi)) :
                self._debug['length']['start'] = self._io.pos()
                self.length = self._io.read_u2be()
                self._debug['length']['end'] = self._io.pos()

            if  ((self.marker != self._root.Segment.MarkerEnum.soi) and (self.marker != self._root.Segment.MarkerEnum.eoi)) :
                self._debug['data']['start'] = self._io.pos()
                _on = self.marker
                if _on == self._root.Segment.MarkerEnum.app1:
                    self._raw_data = self._io.read_bytes((self.length - 2))
                    io = KaitaiStream(BytesIO(self._raw_data))
                    self.data = self._root.SegmentApp1(io, self, self._root)
                    self.data._read()
                elif _on == self._root.Segment.MarkerEnum.app0:
                    self._raw_data = self._io.read_bytes((self.length - 2))
                    io = KaitaiStream(BytesIO(self._raw_data))
                    self.data = self._root.SegmentApp0(io, self, self._root)
                    self.data._read()
                elif _on == self._root.Segment.MarkerEnum.sof0:
                    self._raw_data = self._io.read_bytes((self.length - 2))
                    io = KaitaiStream(BytesIO(self._raw_data))
                    self.data = self._root.SegmentSof0(io, self, self._root)
                    self.data._read()
                elif _on == self._root.Segment.MarkerEnum.sos:
                    self._raw_data = self._io.read_bytes((self.length - 2))
                    io = KaitaiStream(BytesIO(self._raw_data))
                    self.data = self._root.SegmentSos(io, self, self._root)
                    self.data._read()
                else:
                    self.data = self._io.read_bytes((self.length - 2))
                self._debug['data']['end'] = self._io.pos()

            if self.marker == self._root.Segment.MarkerEnum.sos:
                self._debug['image_data']['start'] = self._io.pos()
                self.image_data = self._io.read_bytes_full()
                self._debug['image_data']['end'] = self._io.pos()



    class SegmentSos(KaitaiStruct):
        SEQ_FIELDS = ["num_components", "components", "start_spectral_selection", "end_spectral", "appr_bit_pos"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['num_components']['start'] = self._io.pos()
            self.num_components = self._io.read_u1()
            self._debug['num_components']['end'] = self._io.pos()
            self._debug['components']['start'] = self._io.pos()
            self.components = [None] * (self.num_components)
            for i in range(self.num_components):
                if not 'arr' in self._debug['components']:
                    self._debug['components']['arr'] = []
                self._debug['components']['arr'].append({'start': self._io.pos()})
                _t_components = self._root.SegmentSos.Component(self._io, self, self._root)
                _t_components._read()
                self.components[i] = _t_components
                self._debug['components']['arr'][i]['end'] = self._io.pos()

            self._debug['components']['end'] = self._io.pos()
            self._debug['start_spectral_selection']['start'] = self._io.pos()
            self.start_spectral_selection = self._io.read_u1()
            self._debug['start_spectral_selection']['end'] = self._io.pos()
            self._debug['end_spectral']['start'] = self._io.pos()
            self.end_spectral = self._io.read_u1()
            self._debug['end_spectral']['end'] = self._io.pos()
            self._debug['appr_bit_pos']['start'] = self._io.pos()
            self.appr_bit_pos = self._io.read_u1()
            self._debug['appr_bit_pos']['end'] = self._io.pos()

        class Component(KaitaiStruct):
            SEQ_FIELDS = ["id", "huffman_table"]
            def __init__(self, _io, _parent=None, _root=None):
                self._io = _io
                self._parent = _parent
                self._root = _root if _root else self
                self._debug = collections.defaultdict(dict)

            def _read(self):
                self._debug['id']['start'] = self._io.pos()
                self.id = KaitaiStream.resolve_enum(self._root.ComponentId, self._io.read_u1())
                self._debug['id']['end'] = self._io.pos()
                self._debug['huffman_table']['start'] = self._io.pos()
                self.huffman_table = self._io.read_u1()
                self._debug['huffman_table']['end'] = self._io.pos()



    class SegmentApp1(KaitaiStruct):
        SEQ_FIELDS = ["magic", "body"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['magic']['start'] = self._io.pos()
            self.magic = (self._io.read_bytes_term(0, False, True, True)).decode(u"ASCII")
            self._debug['magic']['end'] = self._io.pos()
            self._debug['body']['start'] = self._io.pos()
            _on = self.magic
            if _on == u"Exif":
                self.body = self._root.ExifInJpeg(self._io, self, self._root)
                self.body._read()
            self._debug['body']['end'] = self._io.pos()


    class SegmentSof0(KaitaiStruct):
        SEQ_FIELDS = ["bits_per_sample", "image_height", "image_width", "num_components", "components"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['bits_per_sample']['start'] = self._io.pos()
            self.bits_per_sample = self._io.read_u1()
            self._debug['bits_per_sample']['end'] = self._io.pos()
            self._debug['image_height']['start'] = self._io.pos()
            self.image_height = self._io.read_u2be()
            self._debug['image_height']['end'] = self._io.pos()
            self._debug['image_width']['start'] = self._io.pos()
            self.image_width = self._io.read_u2be()
            self._debug['image_width']['end'] = self._io.pos()
            self._debug['num_components']['start'] = self._io.pos()
            self.num_components = self._io.read_u1()
            self._debug['num_components']['end'] = self._io.pos()
            self._debug['components']['start'] = self._io.pos()
            self.components = [None] * (self.num_components)
            for i in range(self.num_components):
                if not 'arr' in self._debug['components']:
                    self._debug['components']['arr'] = []
                self._debug['components']['arr'].append({'start': self._io.pos()})
                _t_components = self._root.SegmentSof0.Component(self._io, self, self._root)
                _t_components._read()
                self.components[i] = _t_components
                self._debug['components']['arr'][i]['end'] = self._io.pos()

            self._debug['components']['end'] = self._io.pos()

        class Component(KaitaiStruct):
            SEQ_FIELDS = ["id", "sampling_factors", "quantization_table_id"]
            def __init__(self, _io, _parent=None, _root=None):
                self._io = _io
                self._parent = _parent
                self._root = _root if _root else self
                self._debug = collections.defaultdict(dict)

            def _read(self):
                self._debug['id']['start'] = self._io.pos()
                self.id = KaitaiStream.resolve_enum(self._root.ComponentId, self._io.read_u1())
                self._debug['id']['end'] = self._io.pos()
                self._debug['sampling_factors']['start'] = self._io.pos()
                self.sampling_factors = self._io.read_u1()
                self._debug['sampling_factors']['end'] = self._io.pos()
                self._debug['quantization_table_id']['start'] = self._io.pos()
                self.quantization_table_id = self._io.read_u1()
                self._debug['quantization_table_id']['end'] = self._io.pos()

            @property
            def sampling_x(self):
                if hasattr(self, '_m_sampling_x'):
                    return self._m_sampling_x if hasattr(self, '_m_sampling_x') else None

                self._m_sampling_x = ((self.sampling_factors & 240) >> 4)
                return self._m_sampling_x if hasattr(self, '_m_sampling_x') else None

            @property
            def sampling_y(self):
                if hasattr(self, '_m_sampling_y'):
                    return self._m_sampling_y if hasattr(self, '_m_sampling_y') else None

                self._m_sampling_y = (self.sampling_factors & 15)
                return self._m_sampling_y if hasattr(self, '_m_sampling_y') else None



    class ExifInJpeg(KaitaiStruct):
        SEQ_FIELDS = ["extra_zero", "data"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['extra_zero']['start'] = self._io.pos()
            self.extra_zero = self._io.ensure_fixed_contents(b"\x00")
            self._debug['extra_zero']['end'] = self._io.pos()
            self._debug['data']['start'] = self._io.pos()
            self._raw_data = self._io.read_bytes_full()
            io = KaitaiStream(BytesIO(self._raw_data))
            self.data = Exif(io)
            self.data._read()
            self._debug['data']['end'] = self._io.pos()


    class SegmentApp0(KaitaiStruct):

        class DensityUnit(Enum):
            no_units = 0
            pixels_per_inch = 1
            pixels_per_cm = 2
        SEQ_FIELDS = ["magic", "version_major", "version_minor", "density_units", "density_x", "density_y", "thumbnail_x", "thumbnail_y", "thumbnail"]
        def __init__(self, _io, _parent=None, _root=None):
            self._io = _io
            self._parent = _parent
            self._root = _root if _root else self
            self._debug = collections.defaultdict(dict)

        def _read(self):
            self._debug['magic']['start'] = self._io.pos()
            self.magic = (self._io.read_bytes(5)).decode(u"ASCII")
            self._debug['magic']['end'] = self._io.pos()
            self._debug['version_major']['start'] = self._io.pos()
            self.version_major = self._io.read_u1()
            self._debug['version_major']['end'] = self._io.pos()
            self._debug['version_minor']['start'] = self._io.pos()
            self.version_minor = self._io.read_u1()
            self._debug['version_minor']['end'] = self._io.pos()
            self._debug['density_units']['start'] = self._io.pos()
            self.density_units = KaitaiStream.resolve_enum(self._root.SegmentApp0.DensityUnit, self._io.read_u1())
            self._debug['density_units']['end'] = self._io.pos()
            self._debug['density_x']['start'] = self._io.pos()
            self.density_x = self._io.read_u2be()
            self._debug['density_x']['end'] = self._io.pos()
            self._debug['density_y']['start'] = self._io.pos()
            self.density_y = self._io.read_u2be()
            self._debug['density_y']['end'] = self._io.pos()
            self._debug['thumbnail_x']['start'] = self._io.pos()
            self.thumbnail_x = self._io.read_u1()
            self._debug['thumbnail_x']['end'] = self._io.pos()
            self._debug['thumbnail_y']['start'] = self._io.pos()
            self.thumbnail_y = self._io.read_u1()
            self._debug['thumbnail_y']['end'] = self._io.pos()
            self._debug['thumbnail']['start'] = self._io.pos()
            self.thumbnail = self._io.read_bytes(((self.thumbnail_x * self.thumbnail_y) * 3))
            self._debug['thumbnail']['end'] = self._io.pos()