forked from hfiguiere/exifprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.c
1374 lines (1257 loc) · 48 KB
/
misc.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) 2002,2005 by Duane H. Hesser. All rights reserved. */
/* */
/* See the file LICENSE.EXIFPROBE for terms of use. */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
#ifndef lint
static char *ModuleId = "@(#) $Id: misc.c,v 1.22 2005/07/24 16:01:17 alex Exp $";
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include "defs.h"
#include "datadefs.h"
#include "summary.h"
#include "maker.h"
#include "misc.h"
#include "tags.h"
#include "extern.h"
#include "ciff.h"
/* Return the size in bytes of a TIFF type */
int
value_type_size(unsigned short value_type)
{
int size = 0;
switch(value_type)
{
case BYTE:
case ASCII:
case SBYTE:
case UNDEFINED:
size = 1;
break;
case SHORT:
case SSHORT:
size = 2;
break;
case LONG:
case SLONG:
case FLOAT:
size = 4;
break;
case RATIONAL:
case SRATIONAL:
case DOUBLE:
size = 8;
break;
default:
break;
}
return(size);
}
/* Return a string naming the TIFF type of a TIFF entry */
char *
value_type_name(unsigned short value_type)
{
char *name;
switch(value_type)
{
case BYTE: name = "BYTE"; break;
case ASCII: name = "ASCII"; break;
case SBYTE: name = "SBYTE"; break;
case UNDEFINED: name = "UNDEFINED"; break;
case SHORT: name = "SHORT"; break;
case SSHORT: name = "SSHORT"; break;
case LONG: name = "LONG"; break;
case SLONG: name = "SLONG"; break;
case FLOAT: name = "FLOAT"; break;
case RATIONAL: name = "RATIONAL"; break;
case SRATIONAL: name = "SRATIONAL"; break;
case DOUBLE: name = "DOUBLE"; break;
default: name = "INVALID"; break;
}
return(name);
}
/* caller must call free() */
char *
strdup_value(struct ifd_entry *entry, FILE *inptr,
unsigned long fileoffset_base)
{
char *val;
if (is_offset(entry))
{
char *buf = (char *)read_bytes(inptr,
entry->count,
fileoffset_base +
entry->value);
val = strndup(buf, entry->count);
return val;
}
else
{
char *buf = (char *)&(entry->value);
val = (char*)calloc(entry->count + 1, sizeof(char));
for (int i = 0; i < entry->count; i++) {
val[i] = buf[i];
}
return val;
}
}
/* Print a summary of all of the images found in the file. This will */
/* include thumbnail images stored by the unpleasant but ubiquitous */
/* JPEFThumbnailFormat method as well as images described by TIFF */
/* IFDs and SubIFDS, APP0 thumbnails, CIFF subimages, etc. */
/* Even formats such as MRW or JP2 which might be considered */
/* single-image formats can harbor TIFF IFDs (in a TTW block or uuid */
/* box) which can in turn describe thumbnails or reduced resolution */
/* images, so almost any image file can harbor this kind of overhead. */
/* The summary attempts to report the image format, location, size, */
/* and compression type of each block of image data found. For JPEG */
/* subimages claimed by the file, missing or corrupted data may be */
/* reported. */
/* A count of apparently legitimate images found is given at the end, */
/* as well as a count of missing images. */
void
print_summary(struct image_summary *summary_entry)
{
int number_of_images = 0;
int images_found = 0;
int chpr = 0;
char *plural;
/* Pre-process the summary list; marks primary, reduced-res image */
/* types and attempts to avoid kruft from imcomplete summary */
/* entries. */
number_of_images = scan_summary(summary_entry);
for( ; summary_entry; summary_entry = summary_entry->next_entry)
{
PUSHCOLOR(SUMMARY_COLOR);
images_found += print_image_summary_entry(summary_entry,"@");
POPCOLOR();
}
chpr = newline(chpr);
plural = NULLSTRING;
print_filename();
if(LIST_MODE)
chpr += printf("NumberOfImages = %d",number_of_images);
else
chpr += printf("Number of images = %d",number_of_images);
if(number_of_images - images_found)
{
chpr = newline(chpr);
print_filename();
if(LIST_MODE)
chpr += printf("ImagesNotFound = %d",number_of_images - images_found);
else
chpr += printf("Images not found = %d",number_of_images - images_found);
}
chpr = newline(chpr);
}
/* Print one entry of the image summary. */
int
print_image_summary_entry(struct image_summary *entry,char *prefix)
{
int chpr = 0;
int image_found = 0;
if(entry && (entry->entry_lock > 0) && (entry->imageformat != IMGFMT_NOIMAGE))
{
/* Make shell comments out of these if addresses are not */
/* printed */
if(!(PRINT_ADDRESS))
chpr += printf("# ");
else
print_tag_address(SECTION|ENTRY|SEGMENT,entry->offset,0,"@");
if(Debug & 0x4)
chpr += printf("#%d: ",entry->entry_lock);
chpr += printf("Start of");
print_imageformat(entry);
if((entry->imagesubformat & ~IMGSUBFMT_ERRORMASK) != IMGSUBFMT_JPEGTABLES)
{
print_imagesubformat(entry);
print_imagecompression(entry);
print_imagesubtype(entry);
print_imagesize(entry);
print_location(entry);
++image_found;
}
else
chpr += printf(" length %lu",entry->length);
if(entry->imagesubformat & IMGSUBFMT_NO_JPEG)
{
printred(" (NO SOI)");
image_found = 0;
}
else if(entry->imagesubformat & IMGSUBFMT_JPEGFAILED)
printred(" (CORRUPTED)");
else if(entry->imagesubformat & IMGSUBFMT_TRUNCATED)
printred(" (TRUNCATED)");
if(entry->length > 0)
{
if((PRINT_ADDRESS) == 0) /* print_tag_address didn't do it */
chpr += printf(" at offset %#lx/%lu",entry->offset,entry->offset);
chpr = newline(chpr);
if((PRINT_ADDRESS) == 0)
chpr += printf("# ");
else
print_tag_address(SECTION|ENTRY|SEGMENT,
entry->offset + entry->length - 1,
0,"-");
chpr += printf(" End of");
print_imageformat(entry);
if(entry->imagesubformat != IMGSUBFMT_JPEGTABLES)
{
print_imagesubtype(entry);
if((PRINT_ADDRESS) == 0) /* print_tag_address didn't do it */
chpr += printf(" image data at offset %#lx/%lu",
entry->offset + entry->length - 1,
entry->offset + entry->length - 1);
else
chpr += printf(" image data");
}
if(entry->imageformat & IMGFMT_MISMARKED)
{
PUSHCOLOR(RED);
chpr += printf(" *uncompressed data marked as JPEG");
POPCOLOR();
}
chpr = newline(chpr);
}
else
{
printred(" (no image)");
image_found = 0;
}
}
chpr = newline(chpr);
return(image_found);
}
/* Scan the summary entry chain in an attempt to identify primary and */
/* reduced-resolution images. TIFF/EP marks types in NewSubfileType, */
/* but bare TIFF does not. Exif sometimes writes the primary image */
/* size (which is recorded in the summary entry passed to it), but */
/* many cameras write abbreviated Exif sections. */
/* This routine scans the image summary chain forward and back again, */
/* attempting to write any found primary image size to all entries */
/* and/or deduce which entry represents the primary image if none */
/* have been marked by TIFF/EP. This is easy when there are only one */
/* or two images, but a bit more trouble when there are 4 or 5. */
/* During the scan, the maximum image size encountered is recorded, */
/* and may be used to deduce the primary. It is assumed that the */
/* primary will be maximum size (in pixels); when there are ties, the */
/* image type and/or compression may be checked. Uncompressed, "raw" */
/* or "lossless" compression at maximum image size is likely the */
/* primary. */
/* ###%%% This routine is a work in progress; it will likely change */
/* and improve as more is learned about the ways in which */
/* manufacturers are mangling the specifications. The routine */
/* currently performs a useful and necessary task, and some largely */
/* useless work. It is heavily dependent upon the code 'out there' */
/* gathering and recording the proper information. */
/* This is called by the print_summary() routine just before printing */
/* the summary. It returns the number of images which are represented */
/* by summary entries. */
int
scan_summary(struct image_summary *summary_entry)
{
struct image_summary *next_entry = NULL;
struct image_summary *prev_entry = NULL;
unsigned long long image_size = 0ULL;
unsigned long long max_image_size = 0ULL;
unsigned long marked_primary_image_length = 0ULL;
unsigned long long marked_primary_image_size = 0ULL;
unsigned long long primary_image_size = 0ULL;
unsigned long long next_primary_size = 0ULL;
unsigned long max_image_width = 0UL;
unsigned long max_image_height = 0UL;
unsigned long max_image_length = 0UL;
unsigned long image_length = 0UL;
int primary_found = 0;
int number_of_images = 0;
if(Debug & 0x4)
(void)newline(1);
/* The passed entry should be the head; scan forward merging data */
while(summary_entry/* && next_entry */)
{
if(summary_entry->entry_lock <= 0)
{
summary_entry = summary_entry->next_entry;
continue;
}
if(summary_entry)
next_entry = summary_entry->next_entry;
if(Debug & 0x4)
{
printred("DEBUG:");
printf(" %d: ffm=%#x, fsf=%#x",summary_entry->entry_lock,
summary_entry->fileformat,summary_entry->filesubformat);
printf(", spp=%d, bps[0]=%d",summary_entry->spp,summary_entry->bps[0]);
}
/* 'image_size' in pixels */
image_size = summary_entry->pixel_width * summary_entry->pixel_height;
/* image_length vs summary length: compressed? how much? */
/* ###%%% this is "experimental" and shows up only in Debug */
if(summary_entry->bps[0] && summary_entry->spp)
{
int i;
int spp = summary_entry->spp;
float total_bits,bytes_per_pixel;
total_bits = 0;
if (spp > MAXSAMPLE)
{
spp = MAXSAMPLE;
if(Debug & 0x4)
{
printf("CLAMPING spp\n");
}
}
for(i = 0; i < spp; ++i)
total_bits += summary_entry->bps[i];
bytes_per_pixel = total_bits / 8.0;
if(Debug & 0x4)
printf(", bytesperpixel=%.1f",bytes_per_pixel);
image_length = image_size * bytes_per_pixel;
summary_entry->compress_percent = (float)summary_entry->length / (float)image_length;
summary_entry->compress_percent *= 100.0;
}
if(Debug & 0x4)
printf(" c=%lu/%lu=%.0f%%\n",summary_entry->length,image_length,
summary_entry->compress_percent);
if(summary_entry->subfiletype == PRIMARY_TYPE)
{
++primary_found; /* marked in a TIFF/EP IFD */
marked_primary_image_size = image_size;
marked_primary_image_length = summary_entry->length;
if(summary_entry->primary_height == 0UL)
summary_entry->primary_height = summary_entry->pixel_height;
if(summary_entry->primary_width == 0UL)
summary_entry->primary_width = summary_entry->pixel_width;
}
/* maintain max values independently of primary */
if(image_size > max_image_size)
{
max_image_height = summary_entry->pixel_height;
max_image_width = summary_entry->pixel_width;
max_image_size = image_size;
}
/* The largest chunk of image data; could be very important */
if(summary_entry->length > max_image_length)
max_image_length = summary_entry->length;
/* If a primary size was found, e.g. in an exif ifd, carry */
/* it forward. If more than one was found, keep the largest. */
/* Then carry it back down to the head in the backward loop. */
/* Should wind up with all entries the same (and possibly */
/* zero). */
if(summary_entry->primary_height || summary_entry->primary_width)
{
primary_image_size = summary_entry->primary_width * summary_entry->primary_height;
if(next_entry && (next_entry->primary_height || next_entry->primary_width))
{
next_primary_size = next_entry->primary_width * next_entry->primary_height;
if(next_primary_size > primary_image_size)
{
summary_entry->primary_width = next_entry->primary_width;
summary_entry->primary_height = next_entry->primary_height;
primary_image_size = next_primary_size;
}
else
{
next_entry->primary_width = summary_entry->primary_width;
next_entry->primary_height = summary_entry->primary_height;
}
}
else if(next_entry)
{
next_entry->primary_width = summary_entry->primary_width;
next_entry->primary_height = summary_entry->primary_height;
}
}
/* In JPEG format files, it's almost certain that the primary */
/* begins at the beginning of the file; "almost" only because */
/* I've seen some strange things... */
if((summary_entry->fileformat == FILEFMT_JPEG) &&
(summary_entry->subfiletype == UNKNOWN_TYPE))
{
if(summary_entry->offset == 0)
summary_entry->subfiletype = POSSIBLE_PRIMARY_TYPE;
}
prev_entry = summary_entry;
summary_entry = next_entry;
}
summary_entry = prev_entry;
if((Debug & 0x4) && summary_entry)
{
printred("DEBUG:");
printf(" Exif Primary=%lux%lu = %llu\n",summary_entry->primary_width,summary_entry->primary_height,primary_image_size);
printred("DEBUG:");
printf(" Maxsize=%lux%lu = %llu\n",max_image_width,max_image_height,max_image_size);
printred("DEBUG:");
printf(" Marked Primary size = %llu",marked_primary_image_size);
printf(" Marked Primary length=%lu, max length = %lu\n",marked_primary_image_length,max_image_length);
}
/* now walk back to the head */
/* If a marked primary has been found, the primary job is to mark */
/* reduced resolution images which haven't been defined as */
/* thumbnails in the main code. If no marked primary, it is also */
/* necessary to deduce the identity of the primary image, using */
/* size, length and/or image and compression type. */
/* ###%%% at this point, after all the futzing around with */
/* primary_width, etc., it never gets used. max_* are used */
/* (and primary_image_size may not have been set if the last */
/* entry contained the values) */
if(summary_entry)
prev_entry = summary_entry->prev_entry;
while(summary_entry)
{
if(Debug & 0x4)
{
printred("DEBUG:");
printf(" #%d se=%#-9x, pe=%#-9x",summary_entry->entry_lock,
(unsigned int)summary_entry,(unsigned int)prev_entry);
}
image_size = summary_entry->pixel_width * summary_entry->pixel_height;
image_length = summary_entry->length;
if(summary_entry->entry_lock <= 0)
{
summary_entry = prev_entry;
if(summary_entry)
prev_entry = summary_entry->prev_entry;
continue;
}
if(Debug & 0x4)
{
printf(" ffm=%#x, fsf=%#-3x",summary_entry->fileformat,summary_entry->filesubformat);
printf(", ifm=%#lx, isf=%#lx",summary_entry->imageformat,summary_entry->imagesubformat);
printf(", offs=%lu, len=%lu",summary_entry->offset,summary_entry->length);
printf(", pw=%lu, ph=%lu",summary_entry->pixel_width,summary_entry->pixel_height);
}
if(prev_entry && (prev_entry->subfiletype != PRIMARY_TYPE))
{
/* The primary size was carried up to the last entry in */
/* the forward scan; now copy it back down. */
/* ###%%% is this used? ...no, never */
prev_entry->primary_width = summary_entry->primary_width;
prev_entry->primary_height = summary_entry->primary_height;
}
if(Debug & 0x4)
printf(" sft=%d\n",summary_entry->subfiletype);
if(primary_found >= 1) /* should never be greater than 1 */
{
if(summary_entry->subfiletype == POSSIBLE_PRIMARY_TYPE)
summary_entry->subfiletype = REDUCED_RES_TYPE;
}
else if(primary_found == 0)
{
if(summary_entry->subfiletype == POSSIBLE_PRIMARY_TYPE)
{
if((summary_entry->fileformat == FILEFMT_TIFF) ||
(summary_entry->fileformat == FILEFMT_ORF1) ||
(summary_entry->fileformat == FILEFMT_ORF2))
{
switch(summary_entry->compression)
{
case 1:
case 34712: /* JPEG 2000 */
case 34713: /* NEF compressed */
case JPEG_SOF_3:
case JPEG_SOF_7:
case JPEG_SOF_11:
case JPEG_SOF_15:
if(image_size >= max_image_size)
{
summary_entry->subfiletype = PRIMARY_TYPE;
++primary_found;
}
break;
default:
if((image_size < max_image_size) || (image_length < max_image_length))
summary_entry->subfiletype = REDUCED_RES_TYPE;
break;
}
}
else if(summary_entry->fileformat == FILEFMT_JP2)
{
summary_entry->subfiletype = PRIMARY_TYPE;
++primary_found;
}
else if(summary_entry->fileformat == FILEFMT_JPEG)
{
if(summary_entry->offset == 0)
{
summary_entry->subfiletype = PRIMARY_TYPE;
++primary_found;
}
else if(summary_entry->filesubformat & (FILESUBFMT_TIFF|FILESUBFMT_TIFFEP))
{
switch(summary_entry->compression)
{
case JPEG_SOF_3:
case JPEG_SOF_7:
case JPEG_SOF_11:
case JPEG_SOF_15:
/* lossless compression */
if(image_size >= max_image_size)
{
summary_entry->subfiletype = PRIMARY_TYPE;
++primary_found;
}
break;
default:
if((image_size < max_image_size) ||
(image_length < max_image_length))
summary_entry->subfiletype = REDUCED_RES_TYPE;
break;
}
}
}
else if(summary_entry->imagesubformat == IMGSUBFMT_CFA)
{
/* fileformat may be MRW; other fileformats? */
if(image_size >= max_image_size)
{
summary_entry->subfiletype = PRIMARY_TYPE;
++primary_found;
}
else
summary_entry->subfiletype = REDUCED_RES_TYPE;
}
else
summary_entry->subfiletype = REDUCED_RES_TYPE;
}
}
if(primary_found)
{
if(summary_entry->subfiletype <= POSSIBLE_PRIMARY_TYPE)
summary_entry->subfiletype = REDUCED_RES_TYPE;
}
else if(image_size >= max_image_size)
{
if(summary_entry->subfiletype == POSSIBLE_PRIMARY_TYPE)
{
summary_entry->subfiletype = PRIMARY_TYPE;
++primary_found;
}
}
else if(summary_entry->subfiletype <= POSSIBLE_PRIMARY_TYPE)
summary_entry->subfiletype = REDUCED_RES_TYPE;
if(((summary_entry->imageformat) != IMGFMT_NOIMAGE) &&
((summary_entry->imagesubformat & ~IMGSUBFMT_ERRORMASK) != IMGSUBFMT_JPEGTABLES))
++number_of_images;
if(Debug & 0x4)
{
printred("DEBUG:");
printf(" #%d se=%#-9x, pe=%#-9x",summary_entry->entry_lock,
(unsigned int)summary_entry,(unsigned int)prev_entry);
printf(" ffm=%#x, fsf=%#-3x",summary_entry->fileformat,summary_entry->filesubformat);
printf(", ifm=%#lx, isf=%#lx",summary_entry->imageformat,summary_entry->imagesubformat);
printf(", offs=%lu, len=%lu",summary_entry->offset,summary_entry->length);
printf(", pw=%lu, ph=%lu",summary_entry->pixel_width,summary_entry->pixel_height);
printf(" sft=%d\n",summary_entry->subfiletype);
}
summary_entry = prev_entry;
if(summary_entry)
prev_entry = summary_entry->prev_entry;
else
prev_entry = NULL;
}
if(Debug & 0x4)
(void)newline(1);
return(number_of_images);
}
#include "maker_datadefs.h"
extern struct maker_scheme *retrieve_makerscheme();
/* Print a "file format" as a listing of major sections used in the */
/* file. It would be nice (perhaps) to print a single file format for */
/* TIFF-derived formats such as CR2, NEF, K25, DNG, etc. but it is */
/* difficult to say, in the absence of specifications, what makes */
/* e.g. a CR2 file a CR2 file. */
/* Filename extensions and vendor names do not characterize the */
/* internal format of the file, and may be applied (erroneously) to */
/* any file. */
/* At present, the program keeps watch for certain tag numbers in */
/* TIFF Ifds which seem to be unique to a particular format, and */
/* writes a qualifier (e.g. [NEF]) for TIFF-derived types. */
/* ORF files can be marked reliably because Olympus was clever enough */
/* to change the magic number in the header, a trivial but important */
/* change which probably should have been done for DNG (and CR and */
/* NEF). There are even separate magic numbers for the two variants */
/* of ORF. */
/* File formats with specific magic numbers in the file header, such */
/* as MRW, CIFF, and ORF (and TIFF) will show up at the beginning of */
/* output as that file 'type'. This routine will characterize the */
/* "file format" by the major sections found in the summary entries. */
/* Identifiable derived formats (e.g. a NEF compression tag) will be */
/* indicated. The presence of MakerNotes and subifds in MakerNotes */
/* will be shown. */
void
print_fileformat(struct image_summary *entry)
{
struct maker_scheme *scheme;
char *sep,*camera_name;
int chpr = 0;
int filesubformat_shown = 0;
int with_filesubformat = 0;
int vendor_filesubformat = 0;
int has_appn[16];
int i;
print_filename();
if((PRINT_LONGNAMES))
chpr += printf("FileFormat = ");
else
chpr += printf("File Format = ");
if(entry)
{
memset(has_appn,0,sizeof(has_appn));
print_filetype(entry->fileformat,0);
if(entry->filesubformat & FILESUBFMT_TIFFEP)
chpr += printf("EP");
if(entry->fileformat == FILEFMT_TIFF)
filesubformat_shown |= FILESUBFMT_TIFF;
if(entry->filesubformat & FILESUBFMT_TIFFEP)
filesubformat_shown |= FILESUBFMT_TIFFEP;
sep = "/";
for( ; entry; entry = entry->next_entry)
{
if((entry->filesubformat & (FILESUBFMT_TIFF|FILESUBFMT_TIFFOLD)) &&
!(filesubformat_shown & (FILESUBFMT_TIFF|FILESUBFMT_TIFFOLD)))
chpr += printf("%sTIFF",sep);
if((entry->filesubformat & FILESUBFMT_JPEG) &&
!(filesubformat_shown & FILESUBFMT_JPEG))
chpr += printf("%sJPEG",sep);
if((entry->filesubformat & FILESUBFMT_GEOTIFF) &&
!(filesubformat_shown & FILESUBFMT_GEOTIFF))
chpr += printf("%sGEOTIFF",sep);
if((entry->filesubformat & FILESUBFMT_TIFFEP) &&
!(filesubformat_shown & FILESUBFMT_TIFFEP))
chpr += printf("%sEP",sep);
if((entry->filesubformat & FILESUBFMT_APPN) &&
!(filesubformat_shown & FILESUBFMT_APPN))
{
if(entry->filesubformatAPPN[0])
{
chpr += printf("%sAPP0",sep);
if(entry->filesubformat & FILESUBFMT_JFIF)
chpr += printf("%sJFIF",sep);
if(entry->filesubformat & FILESUBFMT_JFXX)
chpr += printf("%sJFXX",sep);
}
if(entry->filesubformatAPPN[1])
{
chpr += printf("%sAPP1",sep);
if(entry->filesubformat & (FILESUBFMT_TIFF|FILESUBFMT_TIFFOLD))
{
entry->filesubformat &= ~(FILESUBFMT_TIFF|FILESUBFMT_TIFFOLD);
chpr += printf("%sTIFF",sep);
}
if(entry->filesubformat & FILESUBFMT_EXIF)
{
entry->filesubformat &= ~FILESUBFMT_EXIF;
chpr += printf("%sEXIF",sep);
}
}
if(entry->filesubformatAPPN[2])
{
chpr += printf("%sAPP2",sep);
if(entry->filesubformat & FILESUBFMT_FPIX)
{
entry->filesubformat &= ~FILESUBFMT_FPIX;
chpr += printf("%sFPIX",sep);
}
}
for(i = 3; i < 16; ++i)
{
if(entry->filesubformatAPPN[i])
has_appn[i] = entry->filesubformatAPPN[i];
}
}
if((entry->filesubformat & FILESUBFMT_CIFF) &&
!(filesubformat_shown & FILESUBFMT_CIFF))
chpr += printf("%sCIFF",sep);
if((entry->filesubformat & FILESUBFMT_EXIF) &&
!(filesubformat_shown & FILESUBFMT_EXIF))
chpr += printf("%sEXIF",sep);
if((entry->filesubformat & FILESUBFMT_FPIX) &&
!(filesubformat_shown & FILESUBFMT_FPIX))
chpr += printf("%sFPIX",sep);
filesubformat_shown |= entry->filesubformat;
if(entry->filesubformat & FILESUBFMT_NEF)
vendor_filesubformat |= FILESUBFMT_NEF;
if(entry->filesubformat & FILESUBFMT_CR2)
vendor_filesubformat |= FILESUBFMT_CR2;
if(entry->filesubformat & FILESUBFMT_DNG)
vendor_filesubformat |= FILESUBFMT_DNG;
if(entry->filesubformat & FILESUBFMT_MNOTE)
with_filesubformat |= FILESUBFMT_MNOTE;
if(entry->filesubformat & FILESUBFMT_MNSUBIFD)
with_filesubformat |= FILESUBFMT_MNSUBIFD;
}
for(i = 3; i < 16; ++i)
{
if(has_appn[i])
chpr += printf("%sAPP%d",sep,i);
}
if(vendor_filesubformat & FILESUBFMT_NEF)
chpr += printf(" [NEF]");
if(vendor_filesubformat & FILESUBFMT_CR2)
chpr += printf(" [CR2]");
if(vendor_filesubformat & FILESUBFMT_DNG)
chpr += printf(" [DNG]");
if(with_filesubformat)
chpr += printf(" #");
if(with_filesubformat & FILESUBFMT_MNOTE)
{
chpr += printf(" with MakerNote");
scheme = retrieve_makerscheme();
if(scheme->make)
{
camera_name = find_camera_name(scheme->make);
if(scheme->scheme_type != UNKNOWN_SCHEME)
printf(" (%s [%d])",camera_name,scheme->note_version);
else
printf(" (%s - unknown makernote format)",camera_name);
}
if(with_filesubformat & FILESUBFMT_MNSUBIFD)
chpr += printf(" and MakerNote-SubIFD");
}
}
else
chpr += printf("unknown");
chpr = newline(chpr);
}
/* Find the name of a device (usually a camera) identified by the */
/* internal id number 'make'. */
extern struct camera_name Camera_make[];
char *
find_camera_name(int make)
{
struct camera_name *maker_id;
char *camera_name = UNKNOWN_CAMERA_STRING;
/* 'Camera_make' is a global parameter */
for(maker_id = Camera_make; maker_id && maker_id->name; ++maker_id)
{
if(make == maker_id->id)
{
camera_name = maker_id->name;
break;
}
}
return(camera_name);
}
void
print_imageformat(struct image_summary *entry)
{
int chpr = 0;
if(entry)
{
switch(entry->imageformat & ~IMGFMT_MISMARKED)
{
case IMGFMT_TIFF:
if(entry->fileformat == FILEFMT_ORF1)
{
chpr += printf(" ORF1 16bps");
/* ###%%% this is the wrong place to do this */
entry->imagesubformat = IMGSUBFMT_CFA;
}
else if(entry->fileformat == FILEFMT_ORF2)
{
chpr += printf(" ORF2 12bps");
entry->imagesubformat = IMGSUBFMT_CFA;
}
else if(entry->fileformat == FILEFMT_RW2)
{
chpr += printf(" RW2");
entry->imagesubformat = IMGSUBFMT_CFA;
}
else
chpr += printf(" TIFF");
break;
case IMGFMT_JPEG:
if(entry->imagesubformat == IMGSUBFMT_JPEGTABLES)
chpr += printf("JPEGTables");
else
chpr += printf(" JPEG");
if(entry->imageformat & IMGFMT_MISMARKED)
chpr += printf("*");
break;
case IMGFMT_JPEG2000:
chpr += printf(" JPEG2000");
break;
case IMGFMT_CRW:
chpr += printf(" CRW");
break;
case IMGFMT_MRW:
chpr += printf(" MRW");
chpr += printf(" %d/%d",entry->bps[0],entry->sample_size);
break;
case IMGFMT_RAF:
chpr += printf(" RAF");
break;
case IMGFMT_X3F:
chpr += printf(" X3F");
break;
default:
chpr += printf(" UNKNOWN format");
break;
}
}
setcharsprinted(chpr);
}
void
print_tiff_compression(struct image_summary *entry)
{
char *compression;
int chpr = 0;
compression = tiff_compression_string(entry->compression);
chpr += printf(" %s",compression);
setcharsprinted(chpr);
}
char *
tiff_compression_string(unsigned long compvalue)
{
char *compression;
compression = NULLSTRING;
switch(compvalue)
{
/* will be 0 only if compression tag does not appear */
case 0: compression = "unknown format"; break;
case 1: compression = "uncompressed"; break; /* TIFF6/TIFF_EP */
case 2: compression = "Modifed Huffman RLE compressed"; break; /* TIFF6 */
case 3: compression = "T4 fax encoded"; break; /* TIFF6 */
case 4: compression = "T6 fax encoded"; break; /* TIFF6 */
case 5: compression = "LZW compressed"; break; /* TIFF6 */
case 6: compression = "Exif/old JPEG"; break; /* TIFF6 pre TechNote 2 */
/* required by Exif! */
case 7: compression = "JPEG"; break; /* TIFF6 current/TIFF_EP/DNG */
case 32773: compression = "Packbits RLE compressed"; break; /* TIFF6 */
/* These are all from "libtiff -- tiff.h" */
case 8: compression="Adobe Deflate compressed"; break;
case 32771: compression="NeXT 2-bit RLE compressed"; break;
case 32809: compression="ThunderScan RLE compressed"; break;
case 32895: compression="IT8 CT compressed with padding"; break;
case 32896: compression="IT8 Linework compressed"; break;
case 32897: compression="IT8 Monochromecompressed"; break;
case 32898: compression="IT8 Binary line art compressed"; break;
case 32908: compression="Pixar companded 10bit LZW compressed"; break;
case 32909: compression="Pixar companded 11bit ZIP compressed"; break;
case 32946: compression="Deflate compressed"; break;
case 32947: compression="Kodak DCS encoded"; break;
case 34661: compression="ISO JBIG compressed"; break;
case 34676: compression="SGI Log Luminance RLE compressed"; break;
case 34677: compression="SGI Log 24-bit compressed"; break;
/* Noted in images */
case 32867: compression="Kodak KDC compressed"; break;
case 34712: compression="Jpeg2000 compressed"; break;
case 34713: compression="NEF compressed"; break;
case 65000: compression="Kodak DCR compressed"; break;
default: compression = "(UNKNOWN TIFF compression)"; break;
}
return(compression);
}
void
print_jpeg_compression(struct image_summary *entry)
{
char *compression;
int chpr = 0;
compression = NULLSTRING;
if(entry)
{
switch(entry->compression)
{
case JPEG_SOF_0: compression="baseline DCT compressed"; break;
case JPEG_SOF_1: compression="extended seq DCT Huffman"; break;
case JPEG_SOF_2: compression="progressive DCT Huffman"; break;
case JPEG_SOF_3: compression="lossless seq Huffman"; break;
case JPEG_SOF_5: compression="differential seq DCT Huffman"; break;
case JPEG_SOF_6: compression="differential progressive DCT Huffman"; break;
case JPEG_SOF_7: compression="differential lossless Huffman"; break;
case JPEG_SOF_9: compression="extended seq DCT arithmetic"; break;
case JPEG_SOF_10: compression="progressive seq arithmetic"; break;
case JPEG_SOF_11: compression="lossless seq arithmetic"; break;
case JPEG_SOF_13: compression="differential seq DCT arithmetic"; break;
case JPEG_SOF_14: compression="differential progressive DCT arithmetic"; break;
case JPEG_SOF_15: compression="differential lossless arithmetic"; break;
default: compression = "(UNKNOWN JPEG compression)"; break;
}
}
chpr += printf(" %s",compression);
setcharsprinted(chpr);
}
void
print_jp2c_compression(struct image_summary *entry)
{
char *transform;
char *quantization;
int chpr = 0;
transform = quantization = NULLSTRING;
if(entry)
{
if((entry->compression & 0xff) == 1)
transform = "5/3 reversible";
else
transform = "9/7 irreversible";
switch((entry->compression & 0xff00) >> 8)
{
case 0:
quantization = "no quantization";
break;
case 1:
quantization = "implicit quantization";
break;
case 2:
quantization = "explicit quantization";
break;
default:
quantization = "unknown quantization";
break;
}
}
chpr += printf(" (%s compression with %s)",transform,quantization);
setcharsprinted(chpr);
}
void
print_crw_compression(struct image_summary *entry)
{
char *compression;
int chpr = 0;
compression = NULLSTRING;
if(entry)
{
switch(entry->compression)
{
case 0: compression = "uncompressed"; break;
case 1: compression = "compressed"; break;