forked from thorfdbg/codestream-parser
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjp2codestream.py
executable file
·1403 lines (1295 loc) · 51.1 KB
/
jp2codestream.py
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# $Id: jp2codestream.py,v 1.47 2019/07/26 07:08:26 thor Exp $
import sys
from jp2utils import *
#
# Some Exceptions
#
class InvalidMarker(JP2Error):
def __init__(self, marker):
JP2Error.__init__(self, 'marker 0xff%s can\'t appear here' % (marker))
self.marker = marker
class InvalidSizedMarker(JP2Error):
def __init__(self, marker):
JP2Error.__init__(self, 'invalid sized marker %s' % (marker))
self.marker = marker
class InvalidMarkerField(JP2Error):
def __init__(self, marker, field):
JP2Error.__init__(self, 'invalid field %s in marker %s' % (field, marker))
self.marker = marker
self.field = field
class RequiredMarkerMissing(JP2Error):
def __init__(self, marker):
JP2Error.__init__(self, 'required marker 0xff%s missing' % (marker))
self.marker = marker
class UnexpectedEOC(JP2Error):
def __init__(self):
JP2Error.__init__(self, 'unexpected end of codestream')
class MisplacedData(JP2Error):
def __init__(self):
JP2Error.__init__(self, 'marker expected')
#
# The Codestream Class
#
class JP2Codestream:
def __init__(self, indent = 0):
self.indent = indent
self.datacount = 0
self.bytecount = 0
self.offset = 0
def print_indent(self, buffer, nl = 1):
print_indent(buffer, self.indent, nl)
def print_header(self, header, content):
self.headers.append((header, content))
def print_data(self, count):
if count > 0:
self.datacount = self.datacount + count
self.bytecount = self.bytecount + count
self.print_indent("Data : %d bytes" % (count))
print()
def new_marker(self, name, description):
self.print_indent("%-8s: New marker: %s (%s)" % \
(str(self.pos-2 + self.offset),name, description))
print()
self.indent = self.indent + 1
self.headers = []
def end_marker(self):
self.flush_marker()
self.indent = self.indent - 1
def flush_marker(self):
if len(self.headers) > 0:
maxlen = 0
for header in self.headers:
maxlen = max(maxlen, len(header[0]))
for header in self.headers:
s = ""
for i in range(maxlen - len(header[0])):
s = s + " "
self.print_indent("%s%s : %s" % (header[0], s, header[1]))
print()
self.headers = []
def parse(self, buffer, startpos):
self.buffer = buffer
self.pos = 0
self.datacount = 0
self.offset = startpos
# Read SOC Marker
if len(self.buffer) - self.pos < 2:
raise RequiredMarkerMissing("SOC")
if self.buffer[self.pos + 0] != 0xff or \
self.buffer[self.pos + 1] != 0x4f:
raise RequiredMarkerMissing("SOC")
self.pos = self.pos + 2
self.read_SOC()
# Read SIZ Marker
if len(self.buffer) - self.pos < 2:
raise RequiredMarkerMissing("SIZ")
if self.buffer[self.pos + 0] != 0xff or \
self.buffer[self.pos + 1] != 0x51:
raise RequiredMarkerMissing("SIZ")
self.pos = self.pos + 2
self.read_SIZ()
# Read other header markers
while len(self.buffer) - self.pos >= 2 and \
self.buffer[self.pos + 1] != 0x90:
if self.buffer[self.pos + 0] != 0xff:
raise MisplacedData()
if len(self.buffer) - self.pos < 4:
raise UnexpectedEOC()
self.read_header_marker()
# Read Tile Parts
while len(self.buffer) - self.pos >= 2 and \
self.buffer[self.pos + 0] == 0xff and \
self.buffer[self.pos + 1] == 0x90:
self.pos = self.pos + 2
self.read_SOT()
# Read Next Marker
while len(self.buffer) - self.pos >= 2 and \
self.buffer[self.pos + 1] != 0x93: # SOD
if self.buffer[self.pos + 0] != 0xff:
raise MisplacedData()
if len(self.buffer) - self.pos < 4:
raise UnexpectedEOC()
self.read_header_marker()
if len(self.buffer) - self.pos < 2:
raise UnexpectedEOC()
self.pos = self.pos + 2
self.new_marker("SOD", "Start of data")
self.end_marker()
self.parse_data()
if len(self.buffer) - self.pos > 0:
raise MisplacedData()
l = len(self.buffer)
oh = l - self.datacount
self.print_indent("Size : %d bytes" % (l))
self.print_indent("Data Size : %d bytes" % (self.datacount))
self.print_indent("Overhead : %d bytes (%d%%)" % (oh, 100 * oh / l))
def load_marker(self, file, marker):
mrk = ((marker[0] << 8) +
(marker[1] << 0))
if (mrk >= 0xff30 and mrk <= 0xff3f):
self.buffer = marker
elif (mrk == 0xff93 or mrk == 0xff4f or mrk == 0xffd9 or mrk == 0xff92):
self.buffer = marker
elif (mrk >= 0xff4f and mrk <= 0xff93):
size = file.read(2)
ln = ((size[0] << 8) +
(size[1] << 0))
if (ln < 2):
raise InvalidSizedMarker("Marker too short")
self.buffer = marker + size + file.read(ln-2)
if len(self.buffer) != ln + 2:
raise UnexpectedEOC()
else:
raise MisplacedData()
self.bytecount = self.bytecount + len(self.buffer)
self.pos = 0
def load_buffer(self, file):
marker = file.read(2)
if len(marker) == 0:
self.buffer = []
else:
if len(marker) < 2:
raise UnexpectedEOC()
self.load_marker(file,marker)
def stream_parse(self, file, startpos):
self.pos = 0
self.datacount = 0
self.bytecount = 0
self.offset = startpos
# Read SOC Marker
self.load_buffer(file)
if self.buffer[self.pos + 0] != 0xff or \
self.buffer[self.pos + 1] != 0x4f:
raise RequiredMarkerMissing("SOC")
self.pos = self.pos + 2
self.read_SOC()
self.offset = self.offset + len(self.buffer)
# Read SIZ Marker
self.load_buffer(file)
if self.buffer[self.pos + 0] != 0xff or \
self.buffer[self.pos + 1] != 0x51:
raise RequiredMarkerMissing("SIZ")
self.pos = self.pos + 2
self.read_SIZ()
self.offset = self.offset + len(self.buffer)
# Read other header markers
self.load_buffer(file)
while len(self.buffer) - self.pos >= 2 and \
self.buffer[self.pos + 1] != 0x90:
if self.buffer[self.pos + 0] != 0xff:
raise MisplacedData()
if len(self.buffer) - self.pos < 4:
raise UnexpectedEOC()
self.read_header_marker()
self.offset = self.offset + len(self.buffer)
self.load_buffer(file)
# Read Tile Parts
while len(self.buffer) >= 2 and \
self.buffer[0] == 0xff and \
self.buffer[1] == 0x90:
self.pos = self.pos + 2
self.read_SOT()
self.offset = self.offset + len(self.buffer)
self.load_buffer(file)
# Read Next Marker
while len(self.buffer) >= 2 and \
self.buffer[self.pos + 1] != 0x93: # SOD
if self.buffer[self.pos + 0] != 0xff:
raise MisplacedData()
if len(self.buffer) - self.pos < 4:
raise UnexpectedEOC()
self.read_header_marker()
self.offset = self.offset + len(self.buffer)
self.load_buffer(file)
self.offset = self.offset + len(self.buffer)
self.new_marker("SOD", "Start of data")
self.end_marker()
self.stream_data(file)
if len(self.buffer) - self.pos > 0:
raise MisplacedData()
oh = self.bytecount - self.datacount
self.print_indent("Size : %d bytes" % (self.bytecount))
self.print_indent("Data Size : %d bytes" % (self.datacount))
self.print_indent("Overhead : %d bytes (%d%%)" % (oh, 100 * oh / self.bytecount))
def stream_data(self,file):
count = 0
while 1:
byte = file.read(1)
if len(byte) != 1:
raise UnexpectedEOC()
count = count + 1
if (byte[0] == 0xff):
marker = file.read(1)
if len(marker) == 1:
count = count + 1
if marker[0] >= 0x90:
self.offset = self.offset + count-2
self.print_data(count-2)
self.load_marker(file,byte+marker)
if self.read_data_marker():
break
self.offset = self.offset + len(self.buffer)
count = 0
def parse_data(self, buffer = None):
if buffer:
self.buffer = buffer
self.pos = 0
while 1:
count = 0
while len(self.buffer) - self.pos >= 2 and \
(self.buffer[self.pos + 0] != 0xff or \
self.buffer[self.pos + 1] < 0x90):
self.pos = self.pos + 1
count = count + 1
if len(self.buffer) - self.pos == 1:
self.pos = self.pos + 1
count = count + 1
self.print_data(count)
if len(self.buffer) - self.pos == 0:
return
if len(self.buffer) - self.pos < 2:
raise UnexpectedEOC()
# Read Marker
if self.read_data_marker():
break
def read_SGco(self):
if len(self.buffer) - self.pos < 4:
raise InvalidSizedMarker("SGco")
self.print_header("Progression Order",
self.progression_order(self.buffer[self.pos + 0]))
self.print_header("Layers",str(ordw(self.buffer[self.pos + 1:self.pos + 3])))
trafo = self.buffer[self.pos + 3]
if trafo == 0:
s = "none"
elif trafo == 1:
s = "components 0,1,2"
elif trafo == 2:
s = "generic array based transform"
elif trafo == 4:
s = "wavelet based transform"
elif trafo == 6:
s = "array and wavelet based transform"
else:
s = str(self.buffer[self.pos + 3])
self.print_header("Multiple Component Transformation", s)
self.pos = self.pos + 4
def read_SPco(self, precincts):
if len(self.buffer) - self.pos < 5 + precincts:
raise InvalidSizedMarker("SPco")
levels = self.buffer[self.pos + 0]
if levels <= 32:
self.print_header("Decomposition Levels", str(levels))
else:
self.print_header("Downsampling factor style", str(levels))
self.print_header("Code-block size", "%dx%d" % \
(1 << (self.buffer[self.pos + 1] + 2),
1 << (self.buffer[self.pos + 2] + 2)))
x = self.buffer[self.pos + 3]
if x & 0x01:
s = "yes"
else:
s = "no"
self.print_header("Selective Arithmetic Coding Bypass", s)
if x & 0x02:
s = "yes"
else:
s = "no"
self.print_header("Reset Context Probabilities", s)
if x & 0x04:
s = "yes"
else:
s = "no"
self.print_header("Termination on Each Coding Pass", s)
if x & 0x08:
s = "yes"
else:
s = "no"
self.print_header("Vertically Causal Context", s)
if x & 0x10:
s = "yes"
else:
s = "no"
self.print_header("Predictable Termination", s)
if x & 0x20:
s = "yes"
else:
s = "no"
self.print_header("Segmentation Symbols", s)
if x & 0x40:
s = "FBCOT (Part 15)"
else:
s = "EBCOT"
self.print_header("Entropy Coding", s)
if x & 0x40:
if x & 0x80:
s = "yes"
else:
s = "no"
self.print_header("Mixing of FBCOT and EBCOT",s)
if self.buffer[self.pos + 4] == 0x00:
s = "9-7 irreversible"
elif self.buffer[self.pos + 4] == 0x01:
s = "5-3 reversible"
else:
s = "abritrary ATK specified transform"
self.print_header("Wavelet Transformation", s)
for i in range(precincts):
x = self.buffer[self.pos + i + 5]
self.print_header("Precinct #%d Size Exponents" % (i),
"%dx%d" % (x & 0x0f, x >> 4))
self.pos = self.pos + 5 + precincts
def read_SOC(self):
self.new_marker("SOC", "Start of codestream")
self.end_marker()
def read_NSI(self):
self.new_marker("NSI", "Additional Dimansion Image and Tile Size")
size = ordw(self.buffer[self.pos + 0:self.pos + 2])
if size < 20 or size > 16403:
raise InvalidSizedMarker("NSI")
ndim = self.buffer[self.pos + 2]
zsiz = ordl(self.buffer[self.pos + 3:self.pos + 7])
osiz = ordl(self.buffer[self.pos + 7:self.pos + 11])
tsiz = ordl(self.buffer[self.pos + 11:self.pos + 15])
tosz = ordl(self.buffer[self.pos + 15:self.pos + 19])
self.print_header("Dimensionality","%d" % ndim)
self.print_header("Image Depth","%d" % zsiz)
self.print_header("Image Depth Offset","%d" % osiz)
self.print_header("Tile Depth","%d" % tsiz)
self.print_header("Tile Depth Offset","%d" % tosz)
for i in range(size - 19):
self.print_header("Z Sample Separation for component %d" % i,"%d" % self.buffer[self.pos + 19 + i])
self.pos = self.pos + size
self.end_marker()
def read_SIZ(self):
self.new_marker("SIZ", "Image and tile size")
size = ordw(self.buffer[self.pos + 0:self.pos + 2])
if size < 41:
raise InvalidSizedMarker("SIZ")
if (size - 38) % 3 != 0:
raise InvalidSizedMarker("SIZ")
# Read Csiz
components = (size - 38) / 3
self.csiz = ordw(self.buffer[self.pos + 36:self.pos + 38])
if self.csiz != components:
raise InvalidSizedMarker("SIZ")
# Read Rsiz
rsiz = ordw(self.buffer[self.pos + 2:self.pos + 4])
if rsiz == 0:
s = "JPEG2000 full standard"
elif rsiz == 1:
s = "JPEG2000 profile 0"
elif rsiz == 2:
s = "JPEG2000 profile 1"
elif rsiz == 3:
s = "DCI 2K profile"
elif rsiz == 4:
s = "DCI 4K profile"
elif rsiz == 5:
s = "DCI long term storage profile"
elif rsiz == 6:
s = "DCI 2K scalable profile"
elif rsiz & (1 << 14):
s = "JPEG2000 part 15"
elif rsiz & (1 << 15):
s = "JPEG2000 part 2"
if rsiz & (1 << 11):
s += " Precinct dependent QNT"
if rsiz & (1 << 10):
s += " Arbitrary ROIs"
if rsiz & (1 << 9):
s += " NLT transform"
if rsiz & (1 << 8):
s += " Multi-component transform"
if rsiz & (1 << 7):
s += " WSS transformation kernel"
if rsiz & (1 << 6):
s += " Arbitrary kernel"
if rsiz & (1 << 5):
s += " Arbitrary decomposition"
if rsiz & (1 << 4):
s += " Single sample overlap"
if rsiz & (1 << 3):
s += " Visual masking"
if rsiz & (1 << 2):
s += " Trellis quantization"
if rsiz & (1 << 1):
s += " Variable scalar quantization"
if rsiz & (1 << 0):
s += " Variable DC offset"
else:
s = "unknown"
self.print_header("Required Capabilities", s)
# Read Xsiz and Ysiz
xsiz = ordl(self.buffer[self.pos + 4:self.pos + 8])
ysiz = ordl(self.buffer[self.pos + 8:self.pos + 12])
self.print_header("Reference Grid Size", "%dx%d" % (xsiz, ysiz))
# Read XOsiz and YOsiz
xosiz = ordl(self.buffer[self.pos + 12:self.pos + 16])
yosiz = ordl(self.buffer[self.pos + 16:self.pos + 20])
self.print_header("Image Offset", "%dx%d" % (xosiz, yosiz))
# Read XTsiz and YTsiz
xtsiz = ordl(self.buffer[self.pos + 20:self.pos + 24])
ytsiz = ordl(self.buffer[self.pos + 24:self.pos + 28])
self.print_header("Reference Tile Size","%dx%d" % (xtsiz, ytsiz))
# Read XTOsiz and YTOsiz
xtosiz = ordl(self.buffer[self.pos + 28:self.pos + 32])
ytosiz = ordl(self.buffer[self.pos + 32:self.pos + 36])
self.print_header("Reference Tile Offset", "%dx%d" % (xtosiz, ytosiz))
# Csiz (already read)
self.print_header("Components", str(components))
# Read Components
for i in range(0, int(components)):
ssiz = self.buffer[self.pos + 38 + i * 3]
xrsiz = self.buffer[self.pos + 39 + i * 3]
yrsiz = self.buffer[self.pos + 40 + i * 3]
self.print_header("Component #%d Depth" % (i), "%d" % ((ssiz & 0x7f) + 1))
if ssiz & 0x80:
s = "yes"
else:
s = "no"
self.print_header("Component #%d Signed" % (i), s)
self.print_header("Component #%d Sample Separation" % (i),
"%dx%d" % (xrsiz, yrsiz))
self.end_marker()
self.pos = self.pos + size
def read_SOT(self):
self.new_marker("SOT", "Start of tile-part")
if len(self.buffer) - self.pos < 10:
raise InvalidSizedMarker("SOT")
size = ordw(self.buffer[self.pos + 0:self.pos + 2])
if size != 10:
raise InvalidSizedMarker("SOT")
self.print_header("Tile",str(ordw(self.buffer[self.pos + 2:self.pos + 4])))
length = ordl(self.buffer[self.pos + 4:self.pos + 8])
self.print_header("Length", str(length))
self.print_header("Index", str(self.buffer[self.pos + 8]))
if self.buffer[self.pos + 9] == 0:
s = "unknown"
else:
s = str(self.buffer[self.pos + 9])
self.print_header("Tile-Parts", s)
self.end_marker()
self.pos = self.pos + 10
def read_COD(self):
self.new_marker("COD", "Coding style default")
if self.size < 3:
raise InvalidSizedMarker("COD")
cod = self.buffer[self.pos + 2]
if cod & 0x01:
s = "no"
else:
s = "yes"
self.print_header("Default Precincts of 2^15x2^15", s)
if cod & 0x02:
s = "yes"
else:
s = "no"
self.print_header("SOP Marker Segments", s)
if cod & 0x04:
s = "yes"
else:
s = "no"
self.print_header("EPH Marker Segments", s)
if cod & 0x08:
s = "1";
else:
s = "0";
self.print_header("Codeblock X offset", s);
if cod & 0x10:
s = "1";
else:
s = "0";
self.print_header("Codeblock Y offset", s);
self.print_header("All Flags", "%08x" % (cod))
self.pos = self.pos + 3
self.read_SGco()
self.read_SPco(self.size - 12)
self.end_marker()
def read_COC(self):
self.new_marker("COC", "Coding style component")
if self.csiz <= 256 and self.size < 9 or \
self.csiz > 256 and self.size < 10:
raise InvalidSizedMarker("COC")
self.pos = self.pos + 2
# Print Ccoc
if self.csiz <= 256:
component = self.buffer[self.pos + 0]
self.pos = self.pos + 1
else:
component = ordw(self.buffer[self.pos + 0:self.pos + 2])
self.pos = self.pos + 2
self.print_header("Component", str(component))
# Print Scoc
prec = self.buffer[self.pos + 0]
self.pos = self.pos + 1
if prec == 0:
s = "default"
elif prec == 1:
s = "custom"
else:
s = "unknown"
self.print_header("Precincts", s)
# Print SPcoc
if prec == 0:
if self.csiz <= 256 and self.size != 9 or \
self.csiz > 256 and self.size != 10:
raise InvalidSizedMarker("COC")
precincts = self.size - 9
if self.csiz > 256:
precincts = precincts - 1
self.read_SPco(precincts)
self.end_marker()
def read_QCD(self):
self.new_marker("QCD", "Quantization default")
if self.size < 4:
raise InvalidSizedMarker("QCD")
sqcd = self.buffer[self.pos + 2]
if sqcd & 0x1f == 0:
s = "none"
elif sqcd & 0x1f == 1:
s = "scalar derived"
elif sqcd & 0x1f == 2:
s = "scalar expounded"
elif sqcd & 0x1f == 3:
s = "variable deadzone scalar derived"
elif sqcd & 0x1f == 4:
s = "variable deadzone scalar expounded"
elif sqcd & 0x1f == 5:
s = "variable deadzone scalar expounded"
elif sqcd & 0x1f == 9:
s = "trellis quantization derived"
elif sqcd & 0x1f == 10:
s = "trellis quantization expounded"
else:
s = "unknown"
self.print_header("Quantization Type", s)
self.print_header("Guard Bits", str(sqcd >> 5))
subbands = self.size - 3
if sqcd & 0x1f == 1 or sqcd & 0x1f == 2:
if subbands % 2 != 0:
raise InvalidSizedMarker("QCD")
subbands = subbands / 2
for i in range(subbands):
mantissa = 1.0
if sqcd & 0x1f == 1 or sqcd & 0x1f == 2:
spqcd = ordw(self.buffer[self.pos + i * 2 + 3:self.pos + i * 2 + 5])
mantissa = 1.0 + ((spqcd & 0x7ff) / 2048.0)
self.print_header("Mantissa #%d" % (i), str(spqcd & 0x7ff))
exponent = spqcd >> 11
else:
spqcd = self.buffer[self.pos + i + 3]
exponent = spqcd >> 3
self.print_header("Exponent #%d" % (i), str(exponent))
self.print_header("Delta #%d" % (i), str(mantissa * pow(2.0,-exponent)))
self.end_marker()
self.pos = self.pos + self.size
def read_QCC(self):
self.new_marker("QCC", "Quantization component")
if self.size < 4:
raise InvalidSizedMarker("QCC")
if self.csiz <= 256:
index = self.buffer[self.pos + 2]
self.pos = self.pos + 3
else:
index = ordw(self.buffer[self.pos + 2:self.pos + 4])
self.pos = self.pos + 4
self.print_header("Index", str(index))
sqcc = self.buffer[self.pos + 0]
self.pos = self.pos + 1
if sqcc & 0x1f == 0:
s = "none"
elif sqcc & 0x1f == 1:
s = "scalar derived"
elif sqcc & 0x1f == 2:
s = "scalar expounded"
else:
s = "unknown"
self.print_header("Quantization Type", s)
self.print_header("Guard Bits", str(sqcc >> 5))
if self.csiz <= 256:
subbands = self.size - 4
else:
subbands = self.size - 5
if sqcc & 0x1f == 1 or sqcc & 0x1f == 2:
if subbands % 2 != 0:
raise InvalidSizedMarker("QCC")
subbands = subbands / 2
for i in range(subbands):
mantissa = 1.0
if sqcc & 0x1f == 1 or sqcc & 0x1f == 2:
spqcd = ordw(self.buffer[self.pos + 0:self.pos + 2])
self.pos = self.pos + 2
mantissa = 1.0 + ((spqcd & 0x7ff) / 2048.0)
self.print_header("Mantissa #%d" % (i), str(spqcd & 0x7ff))
exponent = spqcd >> 11
else:
spqcd = self.buffer[self.pos + 0]
self.pos = self.pos + 1
exponent = spqcd >> 3
self.print_header("Exponent #%d" % (i), str(exponent))
self.print_header("Delta #%d" % (i), mantissa * pow(2.0,-exponent))
self.end_marker()
def read_RGN(self):
self.new_marker("RGN", "Region-of-interest")
# Print Crgn
if self.csiz <= 256:
cmp = self.buffer[self.pos + 2]
self.pos = self.pos + 3
else:
cmp = ordw(self.buffer[self.pos + 2:self.pos + 4])
self.pos = self.pos + 4
self.print_header("Component", str(cmp))
method = self.buffer[self.pos + 0]
if method == 0:
s = "implicit"
elif method == 1:
s = "rectangle"
elif method == 2:
s = "ellipse"
else:
s = str(method)
self.pos = self.pos + 1
self.print_header("Style", s)
self.print_header("Implicit ROI Shift",
str(self.buffer[self.pos + 0]))
self.pos = self.pos + 1
self.end_marker()
def read_POC(self):
self.new_marker("POC", "Progression order change")
if self.size < 9:
raise InvalidSizedMarker("POC")
if self.csiz <= 256:
if (self.size - 2) % 7 != 0:
raise InvalidSizedMarker("POC")
num = (self.size - 2) / 7
else:
if (self.size - 2) % 9 != 0:
raise InvalidSizedMarker("POC")
num = (self.size - 2) / 9
self.pos = self.pos + 2
for i in range(num):
self.print_header("Resolution Level Index #%d (Start)" % (i),
str(self.buffer[self.pos]))
if self.csiz <= 256:
rspoc = self.buffer[self.pos + 1]
self.pos = self.pos + 2
else:
rspoc = ordw(self.buffer[self.pos + 1:self.pos + 3])
self.pos = self.pos + 3
self.print_header("Component Index #%d (Start)" % (i), str(rspoc))
lyepoc = ordw(self.buffer[self.pos + 0:self.pos + 2])
self.print_header("Layer Index #%d (End)" % (i), str(lyepoc))
self.print_header("Resolution Level Index #%d (End)" % (i),
str(self.buffer[self.pos + 2]))
if self.csiz <= 256:
cepoc = self.buffer[self.pos + 3]
if cepoc == 0:
cepoc = 256
self.pos = self.pos + 4
else:
cepoc = ordw(self.buffer[self.pos + 3:self.pos + 5])
if cepoc == 0:
cepoc = 16384
self.pos = self.pos + 5
self.print_header("Component Index #%d (End)" % (i), str(cepoc))
po = self.progression_order(self.buffer[self.pos])
self.print_header("Progression Order #%d" % (i), po)
self.pos = self.pos + 1
self.end_marker()
def read_PPM(self):
self.new_marker("PPM", "Packed packet headers, main header")
if self.size < 3:
raise InvalidSizedMarker("PPM")
self.print_header("Index Zppm", str(self.buffer[self.pos + 2]))
self.print_header("Marker Length Lppm", str(self.size))
self.end_marker()
self.pos = self.pos + self.size
def read_PPT(self):
self.new_marker("PPT", "Packed packet headers, tile-part header")
if self.size < 3:
raise InvalidSizedMarker("PPT")
self.print_header("Index", str(self.buffer[self.pos + 2]))
self.print_header("Contents", "")
self.flush_marker()
restlen = self.size - 3
self.pos = self.pos + 3
cs = JP2Codestream(self.indent + 1)
cs.parse_data(self.buffer[self.pos:self.pos + restlen])
self.datacount = self.datacount + cs.datacount
self.end_marker()
self.pos = self.pos + restlen
def read_SOP(self):
self.new_marker("SOP", "Start of packet")
if self.size != 4:
raise InvalidSizedMarker("SOP")
self.print_header("Sequence" ,
str(ordw(self.buffer[self.pos + 2:self.pos + 4])))
self.end_marker()
self.pos = self.pos + self.size
def read_EPH(self):
self.new_marker("EPH", "End of packet header")
self.end_marker()
def read_TLM(self):
self.new_marker("TLM", "Tile-part length")
if self.size < 4:
raise InvalidSizedMarker("TLM")
self.print_header("Index", str(self.buffer[self.pos + 2]))
stlm = self.buffer[self.pos + 3] >> 4
st = stlm & 0x03
sp = (stlm >> 2) & 0x1
if st == 3:
raise InvalidMarkerField("TLM", "Stlm")
if st == 0:
if sp == 0:
if (self.size - 4) % 2 != 0:
raise InvalidSizedMarker("TLM")
tileparts = (self.size - 4) / 2
else:
if (self.size - 4) % 4 != 0:
raise InvalidSizedMarker("TLM")
tileparts = (self.size - 4) / 4
elif st == 1:
if sp == 0:
if (self.size - 4) % 3 != 0:
raise InvalidSizedMarker("TLM")
tileparts = (self.size - 4) / 3
else:
if (self.size - 4) % 5 != 0:
raise InvalidSizedMarker("TLM")
tileparts = (self.size - 4) / 5
else:
if sp == 0:
if (self.size - 4) % 4 != 0:
raise InvalidSizedMarker("TLM")
tileparts = (self.size - 4) / 4
else:
if (self.size - 4) % 6 != 0:
raise InvalidSizedMarker("TLM")
tileparts = (self.size - 4) / 6
self.pos = self.pos + 4
for i in range(tileparts):
if st == 0:
ttlm = "in order"
if st == 1:
ttlm = str(self.buffer[self.pos + 0])
self.pos = self.pos + 1
elif st == 2:
ttlm = str(ordw(self.buffer[self.pos + 0:self.pos + 2]))
self.pos = self.pos + 2
self.print_header("Tile index #%d" % (i), ttlm)
if sp == 0:
length = ordw(self.buffer[self.pos + 0:self.pos + 2])
self.pos = self.pos + 2
else:
length = ordl(self.buffer[self.pos + 0:self.pos + 4])
self.pos = self.pos + 4
self.print_header("Length #%d" % (i), str(length))
self.end_marker()
def read_PLM(self):
self.new_marker("PLM", "Packet length, main header")
if self.size < 3:
raise InvalidSizedMarker("PLM")
self.print_header("Index", str(self.buffer[self.pos + 2]))
self.pos = self.pos + 3
self.size = self.size - 3
self.print_header("Length", str(self.size))
self.end_marker()
self.pos = self.pos + self.size
def read_PLT(self):
self.new_marker("PLT", "Packet length, tile-part header")
if self.size < 3:
raise InvalidSizedMarker("PLT")
self.print_header("Index Zplt", str(self.buffer[self.pos + 2]))
self.print_header("Marker size Lplt", "%d bytes" % (self.size))
self.end_marker()
self.pos = self.pos + self.size
def read_CRG(self):
self.new_marker("CRG", "Component registration")
if self.size != self.csiz * 4 + 2:
raise InvalidSizedMarker("CRG")
self.pos = self.pos + 2
for i in range(self.csiz):
x = ordw(self.buffer[self.pos + 0:self.pos + 2])
y = ordw(self.buffer[self.pos + 2:self.pos + 4])
self.print_header("Offset #%d" % (i), "%dx%d" % (x, y))
self.pos = self.pos + 4
self.end_marker()
def read_CBD(self):
self.new_marker("CBD", "Component bit depth definition")
if self.size < 5:
raise InvalidSizedMarker("CBD")
nbcd = ordw(self.buffer[self.pos + 2:self.pos + 4])
if nbcd & (1 << 15):
nbcd -= 1 << 15
self.print_header("Definition style","Identical depth and signs")
count = 1
else:
self.print_header("Definition style","Individual depths and signs")
count = nbcd
self.print_header("Number of generated components",str(nbcd))
self.pos = self.pos + 4
for i in range(count):
if self.buffer[self.pos] & (1 << 7):
self.print_header("Component %d sign" % i,"signed")
else:
self.print_header("Component %d sign" % i,"unsigned")
self.print_header("Component %d Bit Depth" % i,str(1 + (self.buffer[self.pos] & 0x7f)))
self.pos += 1
self.end_marker()
def read_MCO(self):
self.new_marker("MCO", "Multiple component transform ordering")
if self.size < 3:
raise InvalidSizedMarker("MCO")
nmco = self.buffer[self.pos + 2]
if self.size != nmco + 3:
raise InvalidSizedMarker("MCO")
self.print_header("Number of component collections","%d" % nmco)
self.pos += 3
for i in range(nmco):
self.print_header("MCC collection %d" % i,str(self.buffer[self.pos]))
self.pos += 1
self.end_marker()
def read_MCC(self):
self.new_marker("MCC", "Multiple component collection")
if self.size < 5:
raise InvalidSizedMarker("MCC")
zmcc = ordw(self.buffer[self.pos + 2:self.pos + 4])
self.print_header("Concatenation index",str(zmcc))
imcc = self.buffer[self.pos + 4]
self.print_header("Reference index",str(imcc))
self.pos += 5
if zmcc == 0:
ymcc = ordw(self.buffer[self.pos + 0:self.pos + 2])
self.print_header("Last concatenation index",str(ymcc))
qmcc = ordw(self.buffer[self.pos + 2:self.pos + 4])
self.print_header("Number of collections",str(qmcc))
self.pos += 4
for i in range(qmcc):
ctp = self.buffer[self.pos]
if ctp & 3 == 0:
s = "array based dependency transformation"
elif ctp & 3 == 1:
s = "array based decorrelation transformation"
elif ctp & 3 == 3:
s = "wavelet based transformation"
else:
s = "invalid type"
self.print_header("Transformation type",s)
self.pos += 1
nmcc = ordw(self.buffer[self.pos + 0:self.pos + 2])
if nmcc & (1 << 15):
self.print_header("Collection %d input index size" % i,"16 bit")
intype = 2
nmcc -= 1 << 15
else:
self.print_header("Collection %d input index size" % i,"8 bit")
intype = 1
self.print_header("Collection %d # of input components" % i,nmcc)
self.pos += 2
for j in range(nmcc):
if intype == 2:
incom = ordw(self.buffer[self.pos + 0:self.pos + 2])
self.pos += 2
else:
incom = self.buffer[self.pos]
self.pos += 1
self.print_header("Collection %d input component %d" % (i,j),str(incom))
mmcc = ordw(self.buffer[self.pos + 0:self.pos + 2])
if mmcc & (1 << 15):
self.print_header("Collection %d output index size" % i,"16 bit")
outtype = 2
mmcc -= 1 << 15
else:
self.print_header("Collection %d output index size" % i,"8 bit")
outtype = 1
self.print_header("Collection %d # of output components" % i,mmcc)
self.pos += 2
for j in range(mmcc):
if outtype == 2:
outcom = ordw(self.buffer[self.pos + 0:self.pos + 2])
self.pos += 2
else: