forked from hfiguiere/exifprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jp2000.c
2433 lines (2276 loc) · 79.2 KB
/
jp2000.c
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
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* EXIFPROBE - TIFF/JPEG/EXIF image file probe */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* Copyright (C) 2005 by Duane H. Hesser. All rights reserved. */
/* */
/* See the file LICENSE.EXIFPROBE for terms of use. */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
#ifndef lint
static char *ModuleId = "@(#) $Id: jp2000.c,v 1.10 2005/07/24 19:54:04 alex Exp $";
#endif
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* JP2/Jpeg2000 routines */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* The information coded here is derived from the public version of */
/* the Jpeg2000 specification (badly outdated) and from the jasper */
/* code at */
/* http://www.ece.uvic.ca/~mdadams/jasper/ */
/* Some items, such as the ipr, xml, uuid, and uinf boxes, are not */
/* exposed in the jasper implementation, and are implemented from the */
/* public version of the spec. A few test images containing xml and */
/* uuid boxes have been found; the rest are thoroughly untested. */
/* 64-bit addressing is not yet supported */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include "defs.h"
#include "datadefs.h"
#include "summary.h"
#include "misc.h"
#include "tags.h"
#include "jp2.h"
#include "jp2_extern.h"
#include "jpegtags.h"
#include "jp2tags.h"
#include "extern.h"
struct jp2box *
read_jp2box(FILE *inptr,unsigned long offset)
{
static struct jp2box box;
struct jp2box *newbox = NULL;
if(inptr)
{
box.boxoffset = offset;
box.lbox = read_ulong(inptr,TIFF_MOTOROLA,offset);
if(!feof(inptr) && !(ferror(inptr)))
{
box.tbox = read_ulong(inptr,TIFF_MOTOROLA,HERE);
if(!feof(inptr) && !(ferror(inptr)))
{
if(box.lbox == 1)
{
#if 0
box.boxlength = read_ulong64(inptr,TIFF_MOTOROLA,HERE);
box.dataoffset = 16;
#else
PUSHCOLOR(RED);
printf("%s: cannot read 64 bit sizes/offsets yet",Progname);
POPCOLOR();
newbox = NULL;
#endif
}
else
{
box.boxlength = box.lbox;
box.dataoffset = 8;
}
newbox = &box;
}
else
newbox = NULL;
}
else
newbox = NULL;
}
return(newbox);
}
/* Print the content of the JP2 file signature box iff allowed by */
/* print options. */
int
print_jp2_header(struct fileheader *fileheader,unsigned long section_id)
{
struct jp2_header jp2header;
int status = -1;
int chpr = 0;
if(Print_options & section_id)
{
if(fileheader && (fileheader->probe_magic == PROBE_JP2MAGIC))
{
jp2header = fileheader->jp2_header;
if(jp2header.magic == PROBE_JP2MAGIC)
{
/* Ok, I'm convinced... */
print_jp2type(jp2header.type,0);
chpr += printf(" <%#lx> magic %#lx, length %lu",jp2header.type,
jp2header.magic,jp2header.length);
chpr = newline(chpr);
status = 0;
}
else
chpr += printf(" NOT A JP2 HEADER");
}
else
chpr += printf(" NOT A JP2 HEADER");
}
chpr = newline(chpr);
return(status);
}
/* Print a jp2 box identifier, with its length and offset to the box */
/* data, if SECTIONS are enabled in print options. */
void
print_jp2box(FILE *inptr,struct jp2box *box,int indent)
{
unsigned long boxlength = 0UL;
int chpr = 0;
if(box)
{
if(box->boxlength == 0UL)
box->boxlength = get_filesize(inptr) - box->boxoffset;
else
boxlength = box->boxlength;
if(PRINT_SECTION)
{
print_tag_address(SECTION,box->boxoffset,indent,"@");
print_jp2type(box->tbox,0);
chpr += printf(" <%#lx>",box->tbox);
chpr += printf(" length %lu",boxlength);
/* Indicate that this is lastbox */
if(box->boxlength == 0UL)
chpr += printf(" (0)");
chpr += printf(" data offset %lu",box->dataoffset);
}
}
chpr = newline(chpr);
}
int
list_jp2box(FILE *inptr,struct jp2box *box,char *parent_name,int indent,int donl)
{
unsigned long boxlength = 0UL;
int chpr = 0;
if(box)
{
if(box->boxlength == 0UL)
boxlength = get_filesize(inptr) - box->boxoffset;
else
boxlength = box->boxlength;
if((LIST_MODE))
{
print_tag_address(ENTRY,box->boxoffset,indent,"@");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
printf("%s",parent_name);
print_jp2type(box->tbox,0);
}
if((PRINT_VALUE))
{
chpr += printf(" = @%lu:%-4lu",box->boxoffset,boxlength);
/* Indicate that this is lastbox */
if(box->boxlength == 0UL)
chpr += printf(":0");
}
}
}
if(donl)
chpr = newline(chpr);
return(chpr);
}
/* Report the type of a JP2 box. Types are normally printable ascii */
/* (stored in 4 byte unsigned integers), but this routine is prepared */
/* to print garbled 'types', which may indicate that the program has */
/* gone astray, or that the file is garbled. Unprintable bytes, */
/* including newlines and such, are printed in escaped octal */
/* notation. */
/* Box type names are enclosed in []. */
void
print_jp2type(unsigned long type,int atend)
{
int i;
char *p;
int chpr = 0;
p = (char *)&type + 3;
if(!(LIST_MODE))
{
putchar('[');
++chpr;
}
/* 'atend' declares that the output marks the last byte of the */
/* box; this is indicated by prepending a '/' to the box name */
if(atend)
{
putchar('/');
++chpr;
}
for(i = 0; i < 4; ++i,--p)
{
if(isascii(*p) && isprint(*p))
{
putchar(*p);
++chpr;
}
else if(*p)
chpr += printf("\\%03u",*p & 0xff);
else
chpr += printf("\\0");
}
if(!(LIST_MODE))
{
putchar(']');
++chpr;
}
setcharsprinted(chpr);
}
/* The top level JP2 processor. */
unsigned long
process_jp2(FILE *inptr,unsigned long offset,struct image_summary *summary_entry,
char *parent_name,int indent)
{
struct jp2box *box;
unsigned long max_offset = 0L;
unsigned long lastbox = 0L;
unsigned long dumplength;
while(!feof(inptr) && !lastbox)
{
box = read_jp2box(inptr,offset);
if(box)
{
/* This routine is currently called only from main(), */
/* where the summary_entry will already have been created */
/* nonetheless... */
if((summary_entry == NULL) || summary_entry->entry_lock)
summary_entry = new_summary_entry(summary_entry,0,IMGFMT_JPEG2000);
switch(box->tbox)
{
case JP2_FTYP:
max_offset = process_jp2_ftyp(inptr,box,summary_entry,indent);
break;
case JP2_XML:
max_offset = process_jp2_xml(inptr,box,indent);
break;
case JP2_JP2I:
max_offset = process_jp2_jp2i(inptr,box,indent);
break;
case JP2_JP2H:
max_offset = process_jp2_jp2h(inptr,box,summary_entry,indent);
break;
case JP2_UUID:
max_offset = process_jp2_uuid(inptr,box,summary_entry,indent);
break;
case JP2_JP2C:
if(box->boxlength == 0)
++lastbox;
max_offset = process_jp2_jp2c(inptr,box,summary_entry,indent);
break;
case JP2_UINF:
max_offset = process_jp2_uinf(inptr,box,summary_entry,indent);
break;
case JP2_PRFL: /* apparently not in the final spec */
default:
if((box->boxlength == 0) || ateof(inptr))
++lastbox;
if((LIST_MODE))
setcharsprinted(list_jp2box(inptr,box,"JP2.",indent,1));
else
print_jp2box(inptr,box,indent);
max_offset = box->boxoffset + box->boxlength;
if((PRINT_SECTION))
{
/* Dump unrecognized boxes. */
if(Max_undefined > 0)
{
if(Max_undefined == DUMPALL)
dumplength = box->boxlength;
else if(Max_undefined > box->boxlength)
dumplength = box->boxlength;
else
dumplength = Max_undefined;
}
else
{
/* Always dump a little of undefined */
/* boxes */
if(box->boxlength < 48)
dumplength = box->boxlength;
else
dumplength = 48;
}
(void)newline(0);
hexdump(inptr,box->boxoffset,dumplength,dumplength,
16,indent,SUBINDENT);
(void)newline(1);
}
if((PRINT_SECTION))
{
(void)newline(0);
print_tag_address(SECTION,max_offset - 1,indent,"@");
print_jp2type(box->tbox,1);
}
break;
}
(void)newline(0);
offset = max_offset;
if(ateof(inptr))
++lastbox;
}
else
break;
}
return(max_offset);
}
unsigned long
process_jp2_ftyp(FILE *inptr,struct jp2box *box,struct image_summary *summary_entry,
int indent)
{
unsigned long boxoffset,max_offset,dataoffset;
unsigned long boxlength;
unsigned long mjv,mnv,cl,ncl;
int compat = 0;
int chpr = 0;
int i;
if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.",indent,1);
else
print_jp2box(inptr,box,indent);
boxoffset = box->boxoffset;
boxlength = box->boxlength;
dataoffset = boxoffset + box->dataoffset;
max_offset = boxoffset + boxlength;
mjv = read_ulong(inptr,TIFF_MOTOROLA,dataoffset);
dataoffset += 4;
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.ftyp.");
chpr += printf("MajorVersion");
}
if((PRINT_VALUE))
{
chpr += printf(" = %#-10lx = ",mjv);
print_jp2type(mjv,0);
if(mjv != JP2_BR)
printred( " (INVALID MAJOR VERSION");
}
chpr = newline(chpr);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
mnv = read_ulong(inptr,TIFF_MOTOROLA,dataoffset);
dataoffset += 4;
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.ftyp.");
chpr += printf("MinorVersion");
}
if((PRINT_VALUE))
{
chpr += printf(" = %#-10lx = ",mnv);
print_jp2type(mnv,0);
}
chpr = newline(chpr);
ncl = box->boxlength - box->dataoffset - 8;
ncl /= 4;
for(i = 0; i < ncl; ++i)
{
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
cl = read_ulong(inptr,TIFF_MOTOROLA,dataoffset);
dataoffset += 4;
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.ftyp.");
chpr += printf("Compat");
}
if((PRINT_VALUE))
{
chpr += printf(" = %#-10lx = ",cl);
print_jp2type(cl,0);
}
chpr = newline(chpr);
if(cl == JP2_BR)
compat++;
}
if((PRINT_SECTION))
{
chpr = newline(chpr);
print_tag_address(SECTION,dataoffset - 1,indent,"@");
print_jp2type(box->tbox,1);
}
chpr = newline(chpr);
return(max_offset);
}
/* The xml box. The xml is printed if Print_options includes SECTION, */
/* but the text is not examined for information which might go in the */
/* image summary. Even though image information may be recorded in */
/* the xml, the information need not be correct or pertain to the */
/* current image (the original image may have been modified without */
/* updating the xml. */
unsigned long
process_jp2_xml(FILE *inptr,struct jp2box *box,int indent)
{
unsigned long boxoffset,max_offset,dataoffset;
unsigned long readsize;
unsigned long boxlength,size;
int chpr = 0;
print_jp2box(inptr,box,indent);
boxoffset = box->boxoffset;
boxlength = box->boxlength;
dataoffset = boxoffset + box->dataoffset;
max_offset = box->boxoffset + box->boxlength;
size = boxlength - 8;
if((PRINT_SECTION))
{
print_tag_address(VALUE,dataoffset,indent + 10,"=");
if((PRINT_VALUE))
{
if((inptr && (fseek(inptr,dataoffset,SEEK_SET)) != -1))
{
while(size > 0ULL)
{
if(size > READSIZE)
readsize = READSIZE;
else
readsize = size;
/* Print as ascii */
setcharsprinted(chpr);
print_ascii(inptr,readsize,dataoffset);
dataoffset += readsize;
size -= readsize;
}
}
}
chpr = newline(chpr);
print_tag_address(SECTION,dataoffset - 1,indent,"=");
print_jp2type(box->tbox,1);
}
else if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.",indent,1);
chpr = newline(chpr);
return(max_offset);
}
/* The "Intellectual Property Rights" box, which records information */
/* which may have nothing to do with intellect, property, or any */
/* logical concept of rights. */
unsigned long
process_jp2_jp2i(FILE *inptr,struct jp2box *box,int indent)
{
unsigned long boxoffset,max_offset,dataoffset;
unsigned long readsize;
unsigned long boxlength,size;
int chpr = 0;
print_jp2box(inptr,box,indent);
boxoffset = box->boxoffset;
boxlength = box->boxlength;
dataoffset = boxoffset + box->dataoffset;
max_offset = boxoffset + boxlength;
if((PRINT_SECTION))
{
print_tag_address(SECTION,dataoffset,indent + 8,"=");
size = boxlength - 8;
if((inptr && (fseek(inptr,dataoffset,SEEK_SET)) != -1))
{
while(size > 0ULL)
{
if(size > READSIZE)
readsize = READSIZE;
else
readsize = size;
/* Print as ascii... */
setcharsprinted(chpr);
print_ascii(inptr,readsize,dataoffset);
dataoffset += readsize;
size -= readsize;
}
}
chpr = newline(chpr);
print_tag_address(SECTION,dataoffset - 1,indent,"=");
print_jp2type(box->tbox,1);
}
else if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.",indent,1);
chpr = newline(chpr);
return(max_offset);
}
/* The header superbox; this box has lots of human-interest stuff */
unsigned long
process_jp2_jp2h(FILE *inptr,struct jp2box *box,struct image_summary *summary_entry,
int indent)
{
unsigned long lastbox = 0L;
unsigned long boxoffset,max_offset,dataoffset,tbox;
unsigned long boxlength;
int chpr = 0;
if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.",indent,1);
else
print_jp2box(inptr,box,indent);
boxoffset = box->boxoffset;
boxlength = box->boxlength;
tbox = box->tbox;
dataoffset = boxoffset + box->dataoffset;
max_offset = boxoffset + boxlength;
while(!feof(inptr) && !lastbox && (dataoffset < max_offset))
{
box = read_jp2box(inptr,dataoffset);
if(box)
{
switch(box->tbox)
{
case JP2_ihdr:
dataoffset = process_jp2_ihdr(inptr,box,summary_entry,indent + SUBINDENT);
break;
case JP2_colr:
dataoffset = process_jp2_colr(inptr,box,indent + SUBINDENT);
break;
case JP2_res:
dataoffset = process_jp2_res(inptr,box,indent + SUBINDENT);
break;
case JP2_bpcc:
case JP2_pclr:
case JP2_cdef:
case JP2_resc: /* handled in process_jp2_res(). */
case JP2_resd: /* handled in process_jp2_res(). */
/* These fields are reported but not expanded */
print_jp2box(inptr,box,indent + SUBINDENT);
if(boxlength)
dataoffset += boxlength;
else
++lastbox;
chpr = newline(chpr);
break;
default:
/* report the unknown box and try to continue. If */
/* the box is properly constructed, that may be */
/* possible; most likely this is garbled data. */
print_jp2box(inptr,box,indent + SUBINDENT);
if(ferror(inptr) || feof(inptr))
{
clearerr(inptr);
++lastbox;
}
if(boxlength)
dataoffset += boxlength;
else
++lastbox;
chpr = newline(chpr);
break;
}
}
}
if((PRINT_SECTION))
{
chpr = newline(chpr);
print_tag_address(SECTION,dataoffset - 1,indent,"@");
print_jp2type(tbox,1);
}
chpr = newline(chpr);
return(max_offset);
}
/* The ihdr box, within the jp2h box, wherein we find the image size. */
unsigned long
process_jp2_ihdr(FILE *inptr,struct jp2box *box,struct image_summary *summary_entry,
int indent)
{
unsigned long boxoffset,max_offset,dataoffset;
unsigned long boxlength;
unsigned long imgheight = 0UL;
unsigned long imgwidth = 0UL;
unsigned short nc = 0;
unsigned short bpc = 0;
unsigned short comp = 0;
unsigned short hasUNK = 0;
unsigned short hasIPR = 0;
int chpr = 0;
if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.jp2h.",indent,1);
else
print_jp2box(inptr,box,indent);
boxoffset = box->boxoffset;
boxlength = box->boxlength;
dataoffset = boxoffset + box->dataoffset;
max_offset = boxoffset + boxlength;
imgheight = read_ulong(inptr,TIFF_MOTOROLA,dataoffset);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.jp2h.ihdr.");
chpr += printf("ImageHeight");
}
if((PRINT_VALUE))
chpr += printf(" = %lu",imgheight);
chpr = newline(chpr);
dataoffset += 4;
imgwidth = read_ulong(inptr,TIFF_MOTOROLA,dataoffset);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.jp2h.ihdr.");
chpr += printf("ImageWidth");
}
if((PRINT_VALUE))
chpr += printf(" = %lu",imgwidth);
chpr = newline(chpr);
dataoffset += 4;
if(summary_entry && (imgheight > 0) &&
(summary_entry->pixel_height < imgheight))
{
summary_entry->pixel_height = imgheight;
}
if(summary_entry && (imgwidth > 0) &&
(summary_entry->pixel_width < imgwidth))
{
summary_entry->pixel_width = imgwidth;
}
nc = read_ushort(inptr,TIFF_MOTOROLA,dataoffset);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.jp2h.ihdr.");
chpr += printf("NumberOfComponents");
}
if((PRINT_VALUE))
chpr += printf(" = %u",nc);
chpr = newline(chpr);
dataoffset += 2;
bpc = read_ubyte(inptr,dataoffset);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.jp2h.ihdr.");
chpr += printf("BitsPerComponent");
}
if((PRINT_VALUE))
chpr += printf(" = %u",bpc);
chpr = newline(chpr);
dataoffset++;
comp = read_ubyte(inptr,dataoffset);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.jp2h.ihdr.");
chpr += printf("Compression");
}
if((PRINT_VALUE))
chpr += printf(" = %u",comp);
chpr = newline(chpr);
/* This is always 7 according to spec, and tells us nothing; */
/* it will be over-written with the transform type from a COD */
/* segement, if one is found. I imagine that "extensions" to */
/* the spec will quickly overwhelm this. */
if((summary_entry) && (summary_entry->compression <= 0))
summary_entry->compression = comp;
dataoffset++;
hasUNK = read_ubyte(inptr,dataoffset);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.jp2h.ihdr.");
chpr += printf("Colorspace");
}
if((PRINT_VALUE))
chpr += printf(" = %#x = %sknown",hasUNK,hasUNK ? "un" : "");
chpr = newline(chpr);
dataoffset++;
hasIPR = read_ubyte(inptr,dataoffset);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.jp2h.ihdr.");
chpr += printf("IPRbox");
}
if((PRINT_VALUE))
chpr += printf(" = %#x = %s",hasIPR,hasIPR ? "yes" : "no");
chpr = newline(chpr);
dataoffset++;
if((PRINT_SECTION))
{
print_tag_address(SECTION,dataoffset - 1,indent,"@");
print_jp2type(box->tbox,1);
chpr = newline(chpr);
}
return(max_offset);
}
unsigned long
process_jp2_uuid(FILE *inptr,struct jp2box *box,struct image_summary *summary_entry,
int indent)
{
unsigned long boxoffset,max_offset,dataoffset;
unsigned long boxlength,dumplength;
unsigned long ifd_offset = 0UL;
unsigned short idbyte,byte;
struct fileheader *header = NULL;
int chpr = 0;
int i;
if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.",indent,0);
else
print_jp2box(inptr,box,indent);
boxoffset = box->boxoffset;
boxlength = box->boxlength;
dataoffset = boxoffset + box->dataoffset;
max_offset = boxoffset + boxlength;
idbyte = byte = read_ubyte(inptr,dataoffset);
print_tag_address(SECTION,dataoffset,indent + 4,"*");
if((LIST_MODE))
{
if((PRINT_VALUE))
chpr += printf(" # ID: ");
}
else
chpr += printf("ID: ");
if(!(LIST_MODE) || (PRINT_VALUE))
{
if(!feof(inptr) && !ferror(inptr))
{
chpr += printf("%02x",(unsigned int)byte & 0xff);
for(i = 1; i < 16; ++i)
{
byte = read_ubyte(inptr,HERE);
if(feof(inptr) || ferror(inptr))
break;
chpr += printf(",%02x",(unsigned int)byte & 0xff);
}
chpr = newline(chpr);
}
}
dataoffset += 16;
/* No idea what the 16 "ID" bytes mean, or how to track */
/* registered types through the OID register. For now, a leading */
/* byte of 0x5 seems to signal a TIFF IFD. Dump a little bit of */
/* anything else. Probably need to check more of the ID... */
/* ...ok, now I've seen *two* ids that introduce TIFF sections... */
if((idbyte != 5) && (idbyte != 0xb1))
{
if((PRINT_SECTION))
{
dumplength = max_offset - dataoffset;
if(dumplength > 48)
dumplength = 48;
chpr = newline(chpr);
hexdump(inptr,dataoffset,dumplength,dumplength,16,indent,SUBINDENT);
chpr = newline(1);
}
}
/* So far I've seen these with recognizable "magic" */
/* 96,a9,f1,f1,... MSIG - ??? what is this? */
/* 2c,4c,01,00,... 8BIM Adobe PhotoShop - should handle this... */
/* 05,37,cd,ab,... TIFF - this we can do... */
/* b1,4b,f8,bd.... also TIFF */
header = read_imageheader(inptr,dataoffset);
if(header && (header->probe_magic == PROBE_TIFFMAGIC))
{
if((PRINT_SECTION))
{
chpr = newline(chpr);
print_tag_address(SECTION,dataoffset,indent + 4,"@");
print_header(header,SECTION);
ifd_offset = read_ulong(inptr,header->file_marker,HERE);
chpr += printf(" ifd offset = %#lx/%lu",ifd_offset,ifd_offset);
chpr += printf(" (+ %lu = %#lx/%lu)",dataoffset,
dataoffset + ifd_offset,
dataoffset + ifd_offset);
chpr = newline(0);
}
dataoffset = process_tiff_ifd(inptr,header->file_marker,
8,dataoffset,max_offset,summary_entry,"JP2.uuid",
TIFF_IFD,0,-1,indent + 4);
}
dataoffset = boxoffset + boxlength;
if((PRINT_SECTION))
{
chpr = newline(0);
print_tag_address(SECTION,dataoffset - 1,indent,"@");
print_jp2type(box->tbox,1);
chpr = newline(chpr);
}
return(max_offset);
}
/* The uinf superbox, presumably containing a list of uuids and urls */
/* pointing to data to describe them. */
/* This is from the outdated public Jpeg2000 spec; jasper doesn't */
/* appear to handle it. Haven't seen one in the wild yet. */
unsigned long
process_jp2_uinf(FILE *inptr,struct jp2box *box,struct image_summary *summary_entry,
int indent)
{
unsigned long boxoffset,max_offset,dataoffset,tbox;
unsigned long boxlength;
int chpr = 0;
if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.",indent,1);
else
print_jp2box(inptr,box,indent);
boxoffset = box->boxoffset;
boxlength = box->boxlength;
tbox = box->tbox;
dataoffset = boxoffset + box->dataoffset;
max_offset = boxoffset + boxlength;
box = read_jp2box(inptr,dataoffset);
if(box)
{
if((box->tbox) == JP2_ulst)
dataoffset = process_jp2_ulst(inptr,box,indent);
else
{
print_jp2box(inptr,box,indent);
chpr += printf(" NOT a ULST box!");
dataoffset += box->boxlength;
}
box = read_jp2box(inptr,dataoffset);
if(box)
dataoffset = process_jp2_de(inptr,box,indent);
else
{
print_jp2box(inptr,box,indent);
chpr += printf(" NOT a DE box!");
dataoffset += box->boxlength;
}
}
if((PRINT_SECTION))
{
chpr = newline(chpr);
print_tag_address(SECTION,dataoffset - 1,indent,"@");
print_jp2type(box->tbox,1);
}
chpr = newline(chpr);
return(max_offset);
}
/* The uuid list (ulst) box of the uinf superbox. */
unsigned long
process_jp2_ulst(FILE *inptr,struct jp2box *box,int indent)
{
unsigned long boxoffset,max_offset,dataoffset,tbox;
unsigned long boxlength;
unsigned short nuuid,byte;
int chpr = 0;
int i,j;
if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.uinf.",indent,1);
else
print_jp2box(inptr,box,indent);
boxoffset = box->boxoffset;
boxlength = box->boxlength;
tbox = box->tbox;
dataoffset = boxoffset + box->dataoffset;
max_offset = boxoffset + boxlength;
nuuid = read_ushort(inptr,TIFF_MOTOROLA,dataoffset);
print_tag_address(ENTRY,dataoffset,indent + 8,"=");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.uinf.ulst.");
chpr += printf("NumberOfUuids");
}
if((PRINT_VALUE))
chpr += printf(" = %u",nuuid);
chpr = newline(chpr);
dataoffset += 2;
/* Print the uuid list, in hex */
for(i = 0; i < nuuid; ++i)
{
print_tag_address(SECTION|ENTRY,dataoffset,indent + 4,"*");
if((PRINT_TAGINFO))
{
if((PRINT_LONGNAMES))
chpr += printf("JP2.uinf.ulst.");
chpr += printf("ID%d = ",i);
}
if((PRINT_VALUE))
chpr += printf(" = ");
if(!feof(inptr) && !ferror(inptr))
{
for(j = 0; j < 16; ++i)
{
byte = read_ubyte(inptr,dataoffset++);
if(feof(inptr) || ferror(inptr))
break;
if((PRINT_VALUE))
chpr += printf(",%02x",(unsigned int)byte & 0xff);
}
}
else
break;
chpr = newline(chpr);
}
if((PRINT_SECTION))
{
chpr = newline(chpr);
print_tag_address(SECTION,dataoffset - 1,indent,"@");
print_jp2type(box->tbox,1);
}
chpr = newline(chpr);
return(max_offset);
}
/* The data entry URL box of the uinf superbox. */
unsigned long
process_jp2_de(FILE *inptr,struct jp2box *box,int indent)
{
unsigned long boxoffset,max_offset,dataoffset,tbox;
unsigned long boxlength;
unsigned short vers;
unsigned long flag;
int chpr = 0;
if((LIST_MODE))
chpr = list_jp2box(inptr,box,"JP2.uinf.",indent,1);
else