1 /* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
2 /*          EXIFPROBE - TIFF/JPEG/EXIF image file probe               */
3 /* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
4 /* Copyright (C) 2002,2005 by Duane H. Hesser. All rights reserved.   */
5 /*                                                                    */
6 /* See the file LICENSE.EXIFPROBE for terms of use.                   */
7 /* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
8 
9 #ifndef lint
10 static char *ModuleId = "@(#) $Id: misc.c,v 1.22 2005/07/24 16:01:17 alex Exp $";
11 #endif
12 
13 #include <stdio.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <stdlib.h>
17 
18 #include "defs.h"
19 #include "datadefs.h"
20 #include "summary.h"
21 #include "maker.h"
22 #include "misc.h"
23 #include "tags.h"
24 #include "extern.h"
25 #include "ciff.h"
26 
27 /* Return the size in bytes of a TIFF type                            */
28 
29 int
value_type_size(unsigned short value_type)30 value_type_size(unsigned short value_type)
31 {
32     int size = 0;
33 
34     switch(value_type)
35     {
36         case BYTE:
37         case ASCII:
38         case SBYTE:
39         case UNDEFINED:
40             size = 1;
41             break;
42         case SHORT:
43         case SSHORT:
44             size = 2;
45             break;
46         case LONG:
47         case SLONG:
48         case FLOAT:
49             size = 4;
50             break;
51         case RATIONAL:
52         case SRATIONAL:
53         case DOUBLE:
54             size = 8;
55             break;
56         default:
57             break;
58     }
59     return(size);
60 }
61 
62 /* Return a string naming the TIFF type of a TIFF entry               */
63 
64 char *
value_type_name(unsigned short value_type)65 value_type_name(unsigned short value_type)
66 {
67     char *name;
68 
69     switch(value_type)
70     {
71         case BYTE: name = "BYTE"; break;
72         case ASCII: name = "ASCII"; break;
73         case SBYTE: name = "SBYTE"; break;
74         case UNDEFINED: name = "UNDEFINED"; break;
75         case SHORT: name = "SHORT"; break;
76         case SSHORT: name = "SSHORT"; break;
77         case LONG: name = "LONG"; break;
78         case SLONG: name = "SLONG"; break;
79         case FLOAT: name = "FLOAT"; break;
80         case RATIONAL: name = "RATIONAL"; break;
81         case SRATIONAL: name = "SRATIONAL"; break;
82         case DOUBLE: name = "DOUBLE"; break;
83         default: name = "INVALID"; break;
84     }
85     return(name);
86 }
87 
88 
89 /* Print a summary of all of the images found in the file. This will  */
90 /* include thumbnail images stored by the unpleasant but ubiquitous   */
91 /* JPEFThumbnailFormat method as well as images described by TIFF     */
92 /* IFDs and SubIFDS, APP0 thumbnails, CIFF subimages, etc.            */
93 
94 /* Even formats such as MRW or JP2 which might be considered          */
95 /* single-image formats can harbor TIFF IFDs (in a TTW block or uuid  */
96 /* box) which can in turn describe thumbnails or reduced resolution   */
97 /* images, so almost any image file can harbor this kind of overhead. */
98 
99 /* The summary attempts to report the image format, location, size,   */
100 /* and compression type of each block of image data found. For JPEG   */
101 /* subimages claimed by the file, missing or corrupted data may be    */
102 /* reported.                                                          */
103 
104 /* A count of apparently legitimate images found is given at the end, */
105 /* as well as a count of missing images.                              */
106 
107 void
print_summary(struct image_summary * summary_entry)108 print_summary(struct image_summary *summary_entry)
109 {
110     int number_of_images = 0;
111     int images_found = 0;
112     int chpr = 0;
113     char *plural;
114 
115     /* Pre-process the summary list; marks primary, reduced-res image */
116     /* types and attempts to avoid kruft from imcomplete summary      */
117     /* entries.                                                       */
118     number_of_images = scan_summary(summary_entry);
119 
120     for( ; summary_entry; summary_entry = summary_entry->next_entry)
121     {
122         PUSHCOLOR(SUMMARY_COLOR);
123         images_found += print_image_summary_entry(summary_entry,"@");
124         POPCOLOR();
125     }
126 
127     chpr = newline(chpr);
128     plural = NULLSTRING;
129     print_filename();
130     if(LIST_MODE)
131         chpr += printf("NumberOfImages = %d",number_of_images);
132     else
133         chpr += printf("Number of images = %d",number_of_images);
134     if(number_of_images - images_found)
135     {
136         chpr = newline(chpr);
137         print_filename();
138         if(LIST_MODE)
139             chpr += printf("ImagesNotFound = %d",number_of_images - images_found);
140         else
141             chpr += printf("Images not found = %d",number_of_images - images_found);
142     }
143     chpr = newline(chpr);
144 }
145 
146 /* Print one entry of the image summary.                              */
147 
148 int
print_image_summary_entry(struct image_summary * entry,char * prefix)149 print_image_summary_entry(struct image_summary *entry,char *prefix)
150 {
151     int chpr = 0;
152     int image_found = 0;
153 
154     if(entry && (entry->entry_lock > 0) && (entry->imageformat != IMGFMT_NOIMAGE))
155     {
156         /* Make shell comments out of these if addresses are not      */
157         /* printed                                                    */
158         if(!(PRINT_ADDRESS))
159             chpr += printf("# ");
160         else
161             print_tag_address(SECTION|ENTRY|SEGMENT,entry->offset,0,"@");
162         if(Debug & 0x4)
163             chpr += printf("#%d: ",entry->entry_lock);
164         chpr += printf("Start of");
165         print_imageformat(entry);
166         if((entry->imagesubformat & ~IMGSUBFMT_ERRORMASK) != IMGSUBFMT_JPEGTABLES)
167         {
168             print_imagesubformat(entry);
169             print_imagecompression(entry);
170             print_imagesubtype(entry);
171             print_imagesize(entry);
172             print_location(entry);
173             ++image_found;
174         }
175         else
176             chpr += printf(" length %lu",entry->length);
177 
178         if(entry->imagesubformat & IMGSUBFMT_NO_JPEG)
179         {
180             printred(" (NO SOI)");
181             image_found = 0;
182         }
183         else if(entry->imagesubformat & IMGSUBFMT_JPEGFAILED)
184             printred(" (CORRUPTED)");
185         else if(entry->imagesubformat & IMGSUBFMT_TRUNCATED)
186             printred(" (TRUNCATED)");
187         if(entry->length > 0)
188         {
189             if((PRINT_ADDRESS) == 0)   /* print_tag_address didn't do it     */
190                 chpr += printf(" at offset %#lx/%lu",entry->offset,entry->offset);
191             chpr = newline(chpr);
192 
193             if((PRINT_ADDRESS) == 0)
194                 chpr += printf("# ");
195             else
196                 print_tag_address(SECTION|ENTRY|SEGMENT,
197                                             entry->offset + entry->length - 1,
198                                             0,"-");
199             chpr += printf("  End of");
200             print_imageformat(entry);
201             if(entry->imagesubformat != IMGSUBFMT_JPEGTABLES)
202             {
203                 print_imagesubtype(entry);
204                 if((PRINT_ADDRESS) == 0)   /* print_tag_address didn't do it     */
205                     chpr += printf(" image data at offset %#lx/%lu",
206                                                     entry->offset + entry->length - 1,
207                                                     entry->offset + entry->length - 1);
208                 else
209                     chpr += printf(" image data");
210             }
211             if(entry->imageformat & IMGFMT_MISMARKED)
212             {
213                 PUSHCOLOR(RED);
214                 chpr += printf(" *uncompressed data marked as JPEG");
215                 POPCOLOR();
216             }
217             chpr = newline(chpr);
218         }
219         else
220         {
221             printred(" (no image)");
222             image_found = 0;
223         }
224     }
225 
226     chpr = newline(chpr);
227     return(image_found);
228 }
229 
230 /* Scan the summary entry chain in an attempt to identify primary and */
231 /* reduced-resolution images. TIFF/EP marks types in NewSubfileType,  */
232 /* but bare TIFF does not. Exif sometimes writes the primary image    */
233 /* size (which is recorded in the summary entry passed to it), but    */
234 /* many cameras write abbreviated Exif sections.                      */
235 
236 /* This routine scans the image summary chain forward and back again, */
237 /* attempting to write any found primary image size to all entries    */
238 /* and/or deduce which entry represents the primary image if none     */
239 /* have been marked by TIFF/EP. This is easy when there are only one  */
240 /* or two images, but a bit more trouble when there are 4 or 5.       */
241 /* During the scan, the maximum image size encountered is recorded,   */
242 /* and may be used to deduce the primary. It is assumed that the      */
243 /* primary will be maximum size (in pixels); when there are ties, the */
244 /* image type and/or compression may be checked. Uncompressed, "raw"  */
245 /* or "lossless" compression at maximum image size is likely the      */
246 /* primary.                                                           */
247 
248 /* ###%%% This routine is a work in progress; it will likely change   */
249 /* and improve as more is learned about the ways in which             */
250 /* manufacturers are mangling the specifications. The routine         */
251 /* currently performs a useful and necessary task, and some largely   */
252 /* useless work. It is heavily dependent upon the code 'out there'    */
253 /* gathering and recording the proper information.                    */
254 
255 /* This is called by the print_summary() routine just before printing */
256 /* the summary. It returns the number of images which are represented */
257 /* by summary entries.                                                */
258 
259 int
scan_summary(struct image_summary * summary_entry)260 scan_summary(struct image_summary *summary_entry)
261 {
262     struct image_summary *next_entry = NULL;
263     struct image_summary *prev_entry = NULL;
264     unsigned long long image_size = 0ULL;
265     unsigned long long max_image_size = 0ULL;
266     unsigned long marked_primary_image_length = 0ULL;
267     unsigned long long marked_primary_image_size = 0ULL;
268     unsigned long long primary_image_size = 0ULL;
269     unsigned long long next_primary_size = 0ULL;
270     unsigned long max_image_width = 0UL;
271     unsigned long max_image_height = 0UL;
272     unsigned long max_image_length = 0UL;
273     unsigned long image_length = 0UL;
274     int primary_found = 0;
275     int number_of_images = 0;
276 
277     if(Debug & 0x4)
278         (void)newline(1);
279 
280     /* The passed entry should be the head; scan forward merging data */
281     while(summary_entry/*  && next_entry */)
282     {
283         if(summary_entry->entry_lock <= 0)
284         {
285             summary_entry = summary_entry->next_entry;
286             continue;
287         }
288         if(summary_entry)
289             next_entry = summary_entry->next_entry;
290         if(Debug & 0x4)
291         {
292             printred("DEBUG:");
293             printf(" %d: ffm=%#x, fsf=%#x",summary_entry->entry_lock,
294                     summary_entry->fileformat,summary_entry->filesubformat);
295             printf(", spp=%d, bps[0]=%d",summary_entry->spp,summary_entry->bps[0]);
296         }
297 
298         /* 'image_size' in pixels                                     */
299         image_size = summary_entry->pixel_width * summary_entry->pixel_height;
300 
301         /* image_length vs summary length: compressed? how much?      */
302         /* ###%%% this is "experimental" and shows up only in Debug   */
303         if(summary_entry->bps[0] && summary_entry->spp)
304         {
305             int i;
306             float total_bits,bytes_per_pixel;
307 
308             total_bits = 0;
309             for(i = 0; i < summary_entry->spp; ++i)
310                 total_bits += summary_entry->bps[i];
311             bytes_per_pixel = total_bits / 8.0;
312             if(Debug & 0x4)
313                 printf(", bytesperpixel=%.1f",bytes_per_pixel);
314             image_length = image_size * bytes_per_pixel;
315             summary_entry->compress_percent = (float)summary_entry->length / (float)image_length;
316             summary_entry->compress_percent *= 100.0;
317         }
318         if(Debug & 0x4)
319             printf(" c=%lu/%lu=%.0f%%\n",summary_entry->length,image_length,
320                                             summary_entry->compress_percent);
321 
322         if(summary_entry->subfiletype == PRIMARY_TYPE)
323         {
324             ++primary_found;    /* marked in a TIFF/EP IFD            */
325             marked_primary_image_size = image_size;
326             marked_primary_image_length = summary_entry->length;
327             if(summary_entry->primary_height == 0UL)
328                 summary_entry->primary_height = summary_entry->pixel_height;
329             if(summary_entry->primary_width == 0UL)
330                 summary_entry->primary_width = summary_entry->pixel_width;
331 
332         }
333         /* maintain max values independently of primary               */
334         if(image_size > max_image_size)
335         {
336             max_image_height = summary_entry->pixel_height;
337             max_image_width = summary_entry->pixel_width;
338             max_image_size = image_size;
339         }
340 
341         /* The largest chunk of image data; could be very important   */
342         if(summary_entry->length > max_image_length)
343             max_image_length = summary_entry->length;
344 
345         /* If a primary size was found, e.g. in an exif ifd, carry    */
346         /* it forward. If more than one was found, keep the largest.  */
347         /* Then carry it back down to the head in the backward loop.  */
348         /* Should wind up with all entries the same (and possibly     */
349         /* zero).                                                     */
350         if(summary_entry->primary_height || summary_entry->primary_width)
351         {
352             primary_image_size = summary_entry->primary_width * summary_entry->primary_height;
353             if(next_entry && (next_entry->primary_height || next_entry->primary_width))
354             {
355                 next_primary_size = next_entry->primary_width * next_entry->primary_height;
356                 if(next_primary_size > primary_image_size)
357                 {
358                     summary_entry->primary_width = next_entry->primary_width;
359                     summary_entry->primary_height = next_entry->primary_height;
360                     primary_image_size = next_primary_size;
361                 }
362                 else
363                 {
364                     next_entry->primary_width = summary_entry->primary_width;
365                     next_entry->primary_height = summary_entry->primary_height;
366                 }
367             }
368             else if(next_entry)
369             {
370                 next_entry->primary_width = summary_entry->primary_width;
371                 next_entry->primary_height = summary_entry->primary_height;
372             }
373         }
374         /* In JPEG format files, it's almost certain that the primary */
375         /* begins at the beginning of the file; "almost" only because */
376         /* I've seen some strange things...                           */
377         if((summary_entry->fileformat == FILEFMT_JPEG) &&
378                             (summary_entry->subfiletype == UNKNOWN_TYPE))
379         {
380             if(summary_entry->offset == 0)
381                 summary_entry->subfiletype = POSSIBLE_PRIMARY_TYPE;
382         }
383 
384         prev_entry = summary_entry;
385         summary_entry = next_entry;
386     }
387     summary_entry = prev_entry;
388     if((Debug & 0x4) && summary_entry)
389     {
390         printred("DEBUG:");
391         printf(" Exif   Primary=%lux%lu = %llu\n",summary_entry->primary_width,summary_entry->primary_height,primary_image_size);
392         printred("DEBUG:");
393         printf("        Maxsize=%lux%lu = %llu\n",max_image_width,max_image_height,max_image_size);
394         printred("DEBUG:");
395         printf(" Marked Primary size = %llu",marked_primary_image_size);
396         printf(" Marked Primary length=%lu, max length = %lu\n",marked_primary_image_length,max_image_length);
397     }
398 
399     /* now walk back to the head                                      */
400     /* If a marked primary has been found, the primary job is to mark */
401     /* reduced resolution images which haven't been defined as        */
402     /* thumbnails in the main code. If no marked primary, it is also  */
403     /* necessary to deduce the identity of the primary image, using   */
404     /* size, length and/or image and compression type.                */
405 
406     /* ###%%% at this point, after all the futzing around with        */
407     /* primary_width, etc., it never gets used. max_* are used        */
408     /* (and primary_image_size may not have been set if the last      */
409     /* entry contained the values)                                    */
410 
411     if(summary_entry)
412         prev_entry = summary_entry->prev_entry;
413     while(summary_entry)
414     {
415         if(Debug & 0x4)
416         {
417             printred("DEBUG:");
418             printf(" #%d se=%#-9x, pe=%#-9x",summary_entry->entry_lock,
419                     (unsigned int)summary_entry,(unsigned int)prev_entry);
420         }
421         image_size = summary_entry->pixel_width * summary_entry->pixel_height;
422         image_length = summary_entry->length;
423         if(summary_entry->entry_lock <= 0)
424         {
425             summary_entry = prev_entry;
426             if(summary_entry)
427                 prev_entry = summary_entry->prev_entry;
428             continue;
429         }
430         if(Debug & 0x4)
431         {
432             printf(" ffm=%#x, fsf=%#-3x",summary_entry->fileformat,summary_entry->filesubformat);
433             printf(", ifm=%#lx, isf=%#lx",summary_entry->imageformat,summary_entry->imagesubformat);
434             printf(", offs=%lu, len=%lu",summary_entry->offset,summary_entry->length);
435             printf(", pw=%lu, ph=%lu",summary_entry->pixel_width,summary_entry->pixel_height);
436         }
437         if(prev_entry && (prev_entry->subfiletype != PRIMARY_TYPE))
438         {
439             /* The primary size was carried up to the last entry in   */
440             /* the forward scan; now copy it back down.               */
441             /* ###%%% is this used?   ...no, never                    */
442             prev_entry->primary_width = summary_entry->primary_width;
443             prev_entry->primary_height = summary_entry->primary_height;
444         }
445         if(Debug & 0x4)
446             printf(" sft=%d\n",summary_entry->subfiletype);
447         if(primary_found >= 1)  /* should never be greater than 1     */
448         {
449             if(summary_entry->subfiletype == POSSIBLE_PRIMARY_TYPE)
450                 summary_entry->subfiletype = REDUCED_RES_TYPE;
451         }
452         else if(primary_found == 0)
453         {
454             if(summary_entry->subfiletype == POSSIBLE_PRIMARY_TYPE)
455             {
456                 if((summary_entry->fileformat == FILEFMT_TIFF) ||
457                     (summary_entry->fileformat == FILEFMT_ORF1) ||
458                         (summary_entry->fileformat == FILEFMT_ORF2))
459                 {
460                     switch(summary_entry->compression)
461                     {
462                         case 1:
463                         case 34712:     /* JPEG 2000 */
464                         case 34713:     /* NEF compressed */
465                         case JPEG_SOF_3:
466                         case JPEG_SOF_7:
467                         case JPEG_SOF_11:
468                         case JPEG_SOF_15:
469                             if(image_size >= max_image_size)
470                             {
471                                 summary_entry->subfiletype = PRIMARY_TYPE;
472                                 ++primary_found;
473                             }
474                             break;
475                         default:
476                             if((image_size < max_image_size) || (image_length < max_image_length))
477                                 summary_entry->subfiletype = REDUCED_RES_TYPE;
478                             break;
479                     }
480                 }
481                 else if(summary_entry->fileformat == FILEFMT_JP2)
482                 {
483                         summary_entry->subfiletype = PRIMARY_TYPE;
484                         ++primary_found;
485                 }
486                 else if(summary_entry->fileformat == FILEFMT_JPEG)
487                 {
488                     if(summary_entry->offset == 0)
489                     {
490                         summary_entry->subfiletype = PRIMARY_TYPE;
491                         ++primary_found;
492                     }
493                     else if(summary_entry->filesubformat & (FILESUBFMT_TIFF|FILESUBFMT_TIFFEP))
494                     {
495                         switch(summary_entry->compression)
496                         {
497                             case JPEG_SOF_3:
498                             case JPEG_SOF_7:
499                             case JPEG_SOF_11:
500                             case JPEG_SOF_15:
501                                 /* lossless compression               */
502                                 if(image_size >= max_image_size)
503                                 {
504                                     summary_entry->subfiletype = PRIMARY_TYPE;
505                                     ++primary_found;
506                                 }
507                                 break;
508                             default:
509                                 if((image_size < max_image_size) ||
510                                                             (image_length < max_image_length))
511                                     summary_entry->subfiletype = REDUCED_RES_TYPE;
512                                 break;
513                         }
514                     }
515                 }
516                 else if(summary_entry->imagesubformat == IMGSUBFMT_CFA)
517                 {
518                     /* fileformat may be MRW; other fileformats?      */
519                     if(image_size >= max_image_size)
520                     {
521                         summary_entry->subfiletype = PRIMARY_TYPE;
522                         ++primary_found;
523                     }
524                     else
525                         summary_entry->subfiletype = REDUCED_RES_TYPE;
526                 }
527                 else
528                     summary_entry->subfiletype = REDUCED_RES_TYPE;
529             }
530         }
531 
532         if(primary_found)
533         {
534             if(summary_entry->subfiletype <= POSSIBLE_PRIMARY_TYPE)
535                 summary_entry->subfiletype = REDUCED_RES_TYPE;
536         }
537         else if(image_size >= max_image_size)
538         {
539             if(summary_entry->subfiletype == POSSIBLE_PRIMARY_TYPE)
540             {
541                 summary_entry->subfiletype = PRIMARY_TYPE;
542                 ++primary_found;
543             }
544         }
545         else if(summary_entry->subfiletype <= POSSIBLE_PRIMARY_TYPE)
546             summary_entry->subfiletype = REDUCED_RES_TYPE;
547 
548         if(((summary_entry->imageformat) != IMGFMT_NOIMAGE) &&
549                 ((summary_entry->imagesubformat & ~IMGSUBFMT_ERRORMASK) != IMGSUBFMT_JPEGTABLES))
550             ++number_of_images;
551         if(Debug & 0x4)
552         {
553             printred("DEBUG:");
554             printf(" #%d se=%#-9x, pe=%#-9x",summary_entry->entry_lock,
555                     (unsigned int)summary_entry,(unsigned int)prev_entry);
556             printf(" ffm=%#x, fsf=%#-3x",summary_entry->fileformat,summary_entry->filesubformat);
557             printf(", ifm=%#lx, isf=%#lx",summary_entry->imageformat,summary_entry->imagesubformat);
558             printf(", offs=%lu, len=%lu",summary_entry->offset,summary_entry->length);
559             printf(", pw=%lu, ph=%lu",summary_entry->pixel_width,summary_entry->pixel_height);
560             printf(" sft=%d\n",summary_entry->subfiletype);
561         }
562         summary_entry = prev_entry;
563         if(summary_entry)
564             prev_entry = summary_entry->prev_entry;
565         else
566             prev_entry = NULL;
567     }
568     if(Debug & 0x4)
569         (void)newline(1);
570     return(number_of_images);
571 }
572 
573 #include "maker_datadefs.h"
574 extern struct maker_scheme *retrieve_makerscheme();
575 
576 /* Print a "file format" as a listing of major sections used in the   */
577 /* file. It would be nice (perhaps) to print a single file format for */
578 /* TIFF-derived formats such as CR2, NEF, K25, DNG, etc. but it is    */
579 /* difficult to say, in the absence of specifications, what makes     */
580 /* e.g. a CR2 file a CR2 file.                                        */
581 
582 /* Filename extensions and vendor names do not characterize the       */
583 /* internal format of the file, and may be applied (erroneously) to   */
584 /* any file.                                                          */
585 
586 /* At present, the program keeps watch for certain tag numbers in     */
587 /* TIFF Ifds which seem to be unique to a particular format, and      */
588 /* writes a qualifier (e.g. [NEF]) for TIFF-derived types.            */
589 
590 /* ORF files can be marked reliably because Olympus was clever enough */
591 /* to change the magic number in the header, a trivial but important  */
592 /* change which probably should have been done for DNG (and CR and    */
593 /* NEF). There are even separate magic numbers for the two variants   */
594 /* of ORF.                                                            */
595 
596 /* File formats with specific magic numbers in the file header, such  */
597 /* as MRW, CIFF, and ORF (and TIFF) will show up at the beginning of  */
598 /* output as that file 'type'. This routine will characterize the     */
599 /* "file format" by the major sections found in the summary entries.  */
600 /* Identifiable derived formats (e.g. a NEF compression tag) will be  */
601 /* indicated. The presence of MakerNotes and subifds in MakerNotes    */
602 /* will be shown.                                                     */
603 
604 void
print_fileformat(struct image_summary * entry)605 print_fileformat(struct image_summary *entry)
606 {
607     struct maker_scheme *scheme;
608     char *sep,*camera_name;
609     int chpr = 0;
610     int filesubformat_shown = 0;
611     int with_filesubformat = 0;
612     int vendor_filesubformat = 0;
613     int has_appn[16];
614     int i;
615 
616     print_filename();
617     if((PRINT_LONGNAMES))
618         chpr += printf("FileFormat = ");
619     else
620         chpr += printf("File Format = ");
621 
622     if(entry)
623     {
624         memset(has_appn,0,sizeof(has_appn));
625         print_filetype(entry->fileformat,0);
626         if(entry->filesubformat & FILESUBFMT_TIFFEP)
627             chpr += printf("EP");
628 
629         if(entry->fileformat == FILEFMT_TIFF)
630             filesubformat_shown |= FILESUBFMT_TIFF;
631         if(entry->filesubformat & FILESUBFMT_TIFFEP)
632             filesubformat_shown |= FILESUBFMT_TIFFEP;
633 
634         sep = "/";
635         for( ; entry; entry = entry->next_entry)
636         {
637             if((entry->filesubformat & (FILESUBFMT_TIFF|FILESUBFMT_TIFFOLD)) &&
638                         !(filesubformat_shown & (FILESUBFMT_TIFF|FILESUBFMT_TIFFOLD)))
639                 chpr += printf("%sTIFF",sep);
640             if((entry->filesubformat & FILESUBFMT_JPEG) &&
641                         !(filesubformat_shown & FILESUBFMT_JPEG))
642                 chpr += printf("%sJPEG",sep);
643             if((entry->filesubformat & FILESUBFMT_GEOTIFF) &&
644                         !(filesubformat_shown & FILESUBFMT_GEOTIFF))
645                 chpr += printf("%sGEOTIFF",sep);
646             if((entry->filesubformat & FILESUBFMT_TIFFEP) &&
647                         !(filesubformat_shown & FILESUBFMT_TIFFEP))
648                 chpr += printf("%sEP",sep);
649             if((entry->filesubformat & FILESUBFMT_APPN) &&
650                         !(filesubformat_shown & FILESUBFMT_APPN))
651             {
652                 if(entry->filesubformatAPPN[0])
653                 {
654                     chpr += printf("%sAPP0",sep);
655                     if(entry->filesubformat & FILESUBFMT_JFIF)
656                         chpr += printf("%sJFIF",sep);
657                     if(entry->filesubformat & FILESUBFMT_JFXX)
658                         chpr += printf("%sJFXX",sep);
659                 }
660                 if(entry->filesubformatAPPN[1])
661                 {
662                     chpr += printf("%sAPP1",sep);
663                     if(entry->filesubformat & (FILESUBFMT_TIFF|FILESUBFMT_TIFFOLD))
664                     {
665                         entry->filesubformat &= ~(FILESUBFMT_TIFF|FILESUBFMT_TIFFOLD);
666                         chpr += printf("%sTIFF",sep);
667                     }
668                     if(entry->filesubformat & FILESUBFMT_EXIF)
669                     {
670                         entry->filesubformat &= ~FILESUBFMT_EXIF;
671                         chpr += printf("%sEXIF",sep);
672                     }
673                 }
674                 if(entry->filesubformatAPPN[2])
675                 {
676                     chpr += printf("%sAPP2",sep);
677                     if(entry->filesubformat & FILESUBFMT_FPIX)
678                     {
679                         entry->filesubformat &= ~FILESUBFMT_FPIX;
680                         chpr += printf("%sFPIX",sep);
681                     }
682                 }
683                 for(i = 3; i < 16; ++i)
684                 {
685                     if(entry->filesubformatAPPN[i])
686                         has_appn[i] = entry->filesubformatAPPN[i];
687                 }
688             }
689             if((entry->filesubformat & FILESUBFMT_CIFF) &&
690                         !(filesubformat_shown & FILESUBFMT_CIFF))
691                 chpr += printf("%sCIFF",sep);
692             if((entry->filesubformat & FILESUBFMT_EXIF) &&
693                         !(filesubformat_shown & FILESUBFMT_EXIF))
694                 chpr += printf("%sEXIF",sep);
695             if((entry->filesubformat & FILESUBFMT_FPIX) &&
696                         !(filesubformat_shown & FILESUBFMT_FPIX))
697                 chpr += printf("%sFPIX",sep);
698 
699             filesubformat_shown |= entry->filesubformat;
700 
701             if(entry->filesubformat & FILESUBFMT_NEF)
702                 vendor_filesubformat |= FILESUBFMT_NEF;
703             if(entry->filesubformat & FILESUBFMT_CR2)
704                 vendor_filesubformat |= FILESUBFMT_CR2;
705             if(entry->filesubformat & FILESUBFMT_DNG)
706                 vendor_filesubformat |= FILESUBFMT_DNG;
707             if(entry->filesubformat & FILESUBFMT_MNOTE)
708                 with_filesubformat |= FILESUBFMT_MNOTE;
709             if(entry->filesubformat & FILESUBFMT_MNSUBIFD)
710                 with_filesubformat |= FILESUBFMT_MNSUBIFD;
711         }
712         for(i = 3; i < 16; ++i)
713         {
714             if(has_appn[i])
715                 chpr += printf("%sAPP%d",sep,i);
716         }
717         if(vendor_filesubformat & FILESUBFMT_NEF)
718                 chpr += printf(" [NEF]");
719         if(vendor_filesubformat & FILESUBFMT_CR2)
720                 chpr += printf(" [CR2]");
721         if(vendor_filesubformat & FILESUBFMT_DNG)
722                 chpr += printf(" [DNG]");
723         if(with_filesubformat)
724             chpr += printf(" #");
725         if(with_filesubformat & FILESUBFMT_MNOTE)
726         {
727             chpr += printf(" with MakerNote");
728             scheme = retrieve_makerscheme();
729             if(scheme->make)
730             {
731                 camera_name = find_camera_name(scheme->make);
732                 if(scheme->scheme_type != UNKNOWN_SCHEME)
733                     printf(" (%s [%d])",camera_name,scheme->note_version);
734                 else
735                     printf(" (%s - unknown makernote format)",camera_name);
736             }
737             if(with_filesubformat & FILESUBFMT_MNSUBIFD)
738                 chpr += printf(" and MakerNote-SubIFD");
739         }
740     }
741     else
742         chpr += printf("unknown");
743     chpr = newline(chpr);
744 }
745 
746 /* Find the name of a device (usually a camera) identified by the     */
747 /* internal id number 'make'.                                         */
748 
749 extern struct camera_name Camera_make[];
750 
751 char *
find_camera_name(int make)752 find_camera_name(int make)
753 {
754     struct camera_name *maker_id;
755     char *camera_name = UNKNOWN_CAMERA_STRING;
756 
757     /* 'Camera_make' is a global parameter                            */
758     for(maker_id = Camera_make; maker_id && maker_id->name; ++maker_id)
759     {
760         if(make == maker_id->id)
761         {
762             camera_name = maker_id->name;
763             break;
764         }
765     }
766     return(camera_name);
767 }
768 
769 void
print_imageformat(struct image_summary * entry)770 print_imageformat(struct image_summary *entry)
771 {
772     int chpr = 0;
773 
774     if(entry)
775     {
776         switch(entry->imageformat & ~IMGFMT_MISMARKED)
777         {
778         case IMGFMT_TIFF:
779             if(entry->fileformat == FILEFMT_ORF1)
780             {
781                 chpr += printf(" ORF1 16bps");
782                 /* ###%%% this is the wrong place to do this          */
783                 entry->imagesubformat = IMGSUBFMT_CFA;
784             }
785             else if(entry->fileformat == FILEFMT_ORF2)
786             {
787                 chpr += printf(" ORF2 12bps");
788                 entry->imagesubformat = IMGSUBFMT_CFA;
789             }
790             else
791                 chpr += printf(" TIFF");
792             break;
793         case IMGFMT_JPEG:
794             if(entry->imagesubformat == IMGSUBFMT_JPEGTABLES)
795                 chpr += printf("JPEGTables");
796             else
797                 chpr += printf(" JPEG");
798             if(entry->imageformat & IMGFMT_MISMARKED)
799                 chpr += printf("*");
800             break;
801         case IMGFMT_JPEG2000:
802             chpr += printf(" JPEG2000");
803             break;
804         case IMGFMT_CRW:
805             chpr += printf(" CRW");
806             break;
807         case IMGFMT_MRW:
808             chpr += printf(" MRW");
809             chpr += printf(" %d/%d",entry->bps[0],entry->sample_size);
810             break;
811         case IMGFMT_RAF:
812             chpr += printf(" RAF");
813             break;
814         case IMGFMT_X3F:
815             chpr += printf(" X3F");
816             break;
817         default:
818             chpr += printf(" UNKNOWN format");
819             break;
820         }
821     }
822     setcharsprinted(chpr);
823 }
824 
825 void
print_tiff_compression(struct image_summary * entry)826 print_tiff_compression(struct image_summary *entry)
827 {
828     char *compression;
829     int chpr = 0;
830 
831     compression = tiff_compression_string(entry->compression);
832     chpr += printf(" %s",compression);
833     setcharsprinted(chpr);
834 }
835 
836 char *
tiff_compression_string(unsigned long compvalue)837 tiff_compression_string(unsigned long compvalue)
838 {
839     char *compression;
840 
841     compression = NULLSTRING;
842     switch(compvalue)
843     {
844         /* will be 0 only if compression tag does not appear                     */
845         case 0: compression = "unknown format"; break;
846         case 1: compression = "uncompressed"; break;    /* TIFF6/TIFF_EP         */
847         case 2: compression = "Modifed Huffman RLE compressed"; break; /* TIFF6  */
848         case 3: compression = "T4 fax encoded"; break;  /* TIFF6                 */
849         case 4: compression = "T6 fax encoded"; break;  /* TIFF6                 */
850         case 5: compression = "LZW compressed"; break;  /* TIFF6                 */
851         case 6: compression = "Exif/old JPEG"; break;   /* TIFF6 pre TechNote 2  */
852                                                         /* required by Exif!     */
853         case 7: compression = "JPEG"; break;   /* TIFF6 current/TIFF_EP/DNG      */
854         case 32773: compression = "Packbits RLE compressed"; break; /* TIFF6     */
855 
856         /* These are all from "libtiff -- tiff.h"                     */
857         case 8: compression="Adobe Deflate compressed"; break;
858         case 32771: compression="NeXT 2-bit RLE compressed"; break;
859         case 32809: compression="ThunderScan RLE compressed"; break;
860         case 32895: compression="IT8 CT compressed with padding"; break;
861         case 32896: compression="IT8 Linework compressed"; break;
862         case 32897: compression="IT8 Monochromecompressed"; break;
863         case 32898: compression="IT8 Binary line art compressed"; break;
864         case 32908: compression="Pixar companded 10bit LZW compressed"; break;
865         case 32909: compression="Pixar companded 11bit ZIP compressed"; break;
866         case 32946: compression="Deflate compressed"; break;
867         case 32947: compression="Kodak DCS encoded"; break;
868         case 34661: compression="ISO JBIG compressed"; break;
869         case 34676: compression="SGI Log Luminance RLE compressed"; break;
870         case 34677: compression="SGI Log 24-bit compressed"; break;
871 
872         /* Noted in images                                            */
873         case 32867: compression="Kodak KDC compressed"; break;
874 
875         case 34712: compression="Jpeg2000 compressed"; break;
876         case 34713: compression="NEF compressed"; break;
877         case 65000: compression="Kodak DCR compressed"; break;
878         default: compression = "(UNKNOWN TIFF compression)"; break;
879     }
880     return(compression);
881 }
882 
883 void
print_jpeg_compression(struct image_summary * entry)884 print_jpeg_compression(struct image_summary *entry)
885 {
886     char *compression;
887     int chpr = 0;
888 
889     compression = NULLSTRING;
890     if(entry)
891     {
892         switch(entry->compression)
893         {
894             case JPEG_SOF_0: compression="baseline DCT compressed"; break;
895             case JPEG_SOF_1: compression="extended seq DCT Huffman"; break;
896             case JPEG_SOF_2: compression="progressive DCT Huffman"; break;
897             case JPEG_SOF_3: compression="lossless seq Huffman"; break;
898             case JPEG_SOF_5: compression="differential seq DCT Huffman"; break;
899             case JPEG_SOF_6: compression="differential progressive DCT Huffman"; break;
900             case JPEG_SOF_7: compression="differential lossless Huffman"; break;
901             case JPEG_SOF_9: compression="extended seq DCT arithmetic"; break;
902             case JPEG_SOF_10: compression="progressive seq arithmetic"; break;
903             case JPEG_SOF_11: compression="lossless seq arithmetic"; break;
904             case JPEG_SOF_13: compression="differential seq DCT arithmetic"; break;
905             case JPEG_SOF_14: compression="differential progressive DCT arithmetic"; break;
906             case JPEG_SOF_15: compression="differential lossless arithmetic"; break;
907             default: compression = "(UNKNOWN JPEG compression)"; break;
908         }
909     }
910     chpr += printf(" %s",compression);
911     setcharsprinted(chpr);
912 }
913 
914 void
print_jp2c_compression(struct image_summary * entry)915 print_jp2c_compression(struct image_summary *entry)
916 {
917     char *transform;
918     char *quantization;
919     int chpr = 0;
920 
921     transform = quantization = NULLSTRING;
922     if(entry)
923     {
924         if((entry->compression & 0xff) == 1)
925             transform = "5/3 reversible";
926         else
927             transform = "9/7 irreversible";
928         switch((entry->compression & 0xff00) >> 8)
929         {
930             case 0:
931                 quantization = "no quantization";
932                 break;
933             case 1:
934                 quantization = "implicit quantization";
935                 break;
936             case 2:
937                 quantization = "explicit quantization";
938                 break;
939             default:
940                 quantization = "unknown quantization";
941                 break;
942         }
943     }
944     chpr += printf(" (%s compression with %s)",transform,quantization);
945     setcharsprinted(chpr);
946 }
947 
948 void
print_crw_compression(struct image_summary * entry)949 print_crw_compression(struct image_summary *entry)
950 {
951     char *compression;
952     int chpr = 0;
953 
954     compression = NULLSTRING;
955     if(entry)
956     {
957         switch(entry->compression)
958         {
959             case 0: compression = "uncompressed"; break;
960             case 1: compression = "compressed"; break;
961             default: break;
962         }
963     }
964     chpr += printf(" %s",compression);
965     setcharsprinted(chpr);
966 }
967 
968 void
print_raf_compression(struct image_summary * entry)969 print_raf_compression(struct image_summary *entry)
970 {
971     char *compression;
972     int chpr = 0;
973 
974     compression = NULLSTRING;
975     if(entry)
976     {
977         switch(entry->compression)
978         {
979             case 0: compression = "uncompressed"; break;
980             case 1: compression = "uncompressed (with secondary)"; break;
981             default: break;
982         }
983     }
984     chpr += printf(" %s",compression);
985     setcharsprinted(chpr);
986 }
987 
988 void
print_x3f_compression(struct image_summary * entry)989 print_x3f_compression(struct image_summary *entry)
990 {
991     char *compression;
992     int chpr = 0;
993 
994     compression = NULLSTRING;
995     if(entry)
996     {
997         switch(entry->imagesubformat)
998         {
999             case IMGSUBFMT_HUFF_COMPRESSED_RGB: compression = "Huffman compressed"; break;
1000             case IMGSUBFMT_JPEG_COMPRESSED_RGB: compression = "Jpeg compressed"; break;
1001             case IMGSUBFMT_X3F_COMPRESSED: compression = "compressed"; break;
1002             case IMGSUBFMT_RGB: compression = "uncompressed"; break;
1003             default: break;
1004         }
1005     }
1006     chpr += printf(" %s",compression);
1007     setcharsprinted(chpr);
1008 }
1009 
1010 
1011 
1012 void
print_imagecompression(struct image_summary * entry)1013 print_imagecompression(struct image_summary *entry)
1014 {
1015     if(entry)
1016     {
1017         switch(entry->imageformat)
1018         {
1019         case IMGFMT_MRW:    /* gets it from a TIFF IFD                */
1020         case IMGFMT_TIFF:
1021             print_tiff_compression(entry);
1022             break;
1023         case IMGFMT_JPEG:
1024             print_jpeg_compression(entry);
1025             break;
1026         case IMGFMT_JPEG2000:
1027             print_jp2c_compression(entry);
1028             break;
1029         case IMGFMT_CRW:
1030             print_crw_compression(entry);
1031             break;
1032         case IMGFMT_RAF:
1033             print_raf_compression(entry);
1034             break;
1035         case IMGFMT_X3F:
1036             print_x3f_compression(entry);
1037             break;
1038         default:
1039             break;
1040         }
1041     }
1042 }
1043 
1044 
1045 void
print_imagesubformat(struct image_summary * entry)1046 print_imagesubformat(struct image_summary *entry)
1047 {
1048     char *subformat;
1049     unsigned long imagesubformat = 0;
1050     int chpr = 0;
1051 
1052     subformat = NULLSTRING;
1053     if(entry)
1054     {
1055         imagesubformat = entry->imagesubformat & ~IMGSUBFMT_ERRORMASK;
1056         switch(imagesubformat)
1057         {
1058         case IMGSUBFMT_CFA:
1059             subformat = "CFA";
1060             break;
1061         case IMGSUBFMT_RGB:
1062             subformat = "RGB";
1063             break;
1064         case IMGSUBFMT_YCBCR:
1065             subformat = "YCBCR";
1066             break;
1067         case IMGSUBFMT_PALETTE:
1068             subformat = "PALETTE";
1069             break;
1070         case IMGSUBFMT_LINEARRAW:
1071             subformat = "LinearRaw";
1072             break;
1073         case IMGSUBFMT_JPEGTABLES:
1074             subformat = "JPEGTables";
1075             break;
1076         case IMGSUBFMT_HUFF_COMPRESSED_RGB:
1077             subformat = "RGB";
1078             break;
1079         case IMGSUBFMT_JPEG_COMPRESSED_RGB:
1080             subformat = "RGB";
1081             break;
1082         case IMGSUBFMT_X3F_COMPRESSED:
1083             subformat = "RAW";
1084             break;
1085         case IMGSUBFMT_X3F_UNSPECIFIED:
1086             subformat = "Unspecified";
1087             break;
1088         default:
1089             break;
1090         }
1091     }
1092     if(*subformat)
1093         chpr += printf(" %s",subformat);
1094     setcharsprinted(chpr);
1095 }
1096 
1097 void
print_imagesubtype(struct image_summary * entry)1098 print_imagesubtype(struct image_summary *entry)
1099 {
1100     char *type_of_image = UNKNOWN_STRING;
1101     int chpr = 0;
1102 
1103     switch(entry->subfiletype)
1104     {
1105     case THUMBNAIL_TYPE:
1106         type_of_image = THUMBNAIL_STRING;
1107         break;
1108     case PRIMARY_TYPE:
1109         type_of_image = PRIMARY_STRING;
1110         break;
1111     case PAGE_TYPE:
1112         type_of_image = PAGE_STRING;
1113         break;
1114     case MASK_TYPE:
1115         type_of_image = MASK_STRING;
1116         break;
1117     case REDUCED_RES_TYPE:
1118         type_of_image = REDUCED_RES_STRING; /* if not marked THUMBNAIL */
1119         break;
1120     default:
1121         type_of_image = UNSPECIFIED_STRING;
1122         break;
1123     }
1124     if(*type_of_image)
1125         chpr += printf(" %s",type_of_image);
1126     setcharsprinted(chpr);
1127 }
1128 
1129 void
print_imagesize(struct image_summary * entry)1130 print_imagesize(struct image_summary *entry)
1131 {
1132     int chpr = 0;
1133     char *maybeshort = NULLSTRING;
1134 
1135     if(entry)
1136     {
1137         if(entry->imagesubformat & IMGSUBFMT_JPEG_EARLY_EOI)
1138             maybeshort = "<= ";
1139         chpr += printf(" image [%ldx%ld",entry->pixel_width,entry->pixel_height);
1140         if(entry->subfiletype == PRIMARY_TYPE)
1141         {
1142             if((entry->primary_width > entry->pixel_width) ||
1143                        (entry->primary_height > entry->pixel_height))
1144             {
1145                 chpr += printf("<=%ldx%ld",entry->primary_width,entry->primary_height);
1146             }
1147         }
1148         chpr += printf("] length %s%lu",maybeshort,entry->length);
1149     }
1150     setcharsprinted(chpr);
1151 }
1152 
1153 /* Indicate where the image was found; the information is contained   */
1154 /* in the filesubformat bits and/or datatype recorded for this        */
1155 /* particular entry.                                                  */
1156 
1157 void
print_location(struct image_summary * entry)1158 print_location(struct image_summary *entry)
1159 {
1160     char *space = NULL;
1161     int chpr = 0;
1162 
1163     if(entry)
1164     {
1165         space = " (";
1166         if((entry->fileformat == FILEFMT_TIFF) ||
1167                     entry->filesubformat & FILESUBFMT_TIFFUSED ||
1168                                             entry->filesubformatAPPN[1])
1169         {
1170             if(entry->ifdnum >= 0)
1171                 chpr += printf("%sIFD %d",space,entry->ifdnum),space = " ";
1172             if(entry->subifdnum >= 0)
1173                 chpr += printf("%sSubIFD %d",space,entry->subifdnum),space = " ";
1174             /* could use filesubformat here?                          */
1175             if(entry->datatype == MAKER_IFD)
1176                 chpr += printf("%sMakerNote",space),space = " ";
1177             if(entry->datatype == MAKER_SUBIFD)
1178                 chpr += printf("%sMakerNote SubIFD",space),space = " ";
1179         }
1180         else if(entry->fileformat == FILEFMT_JPEG)
1181         {
1182             if(entry->filesubformatAPPN[0])
1183             {
1184                 chpr += printf("%sAPP0",space),space = " ";
1185                 if(entry->filesubformat & FILESUBFMT_JFXX)
1186                     chpr += printf("%sJFXX",space),space = " ";
1187             }
1188         }
1189         if(chpr)
1190             chpr += printf(")");
1191     }
1192 }
1193 
1194 /* Sum the strip or type bytecounts from a TIFF header to yield the   */
1195 /* size of the image data. The bytecounts may be short or long, so    */
1196 /* the value_type must be checked.                                    */
1197 
1198 unsigned long
sum_strip_bytecounts(FILE * inptr,unsigned short byteorder,unsigned long offset,unsigned long count,unsigned short value_type)1199 sum_strip_bytecounts(FILE *inptr,unsigned short byteorder,unsigned long offset,
1200     unsigned long count,unsigned short value_type)
1201 {
1202     int i;
1203     unsigned long sum = 0L;
1204 
1205     if(count)
1206     {
1207         /* Some use SHORT, some use LONG. If anyone uses some other   */
1208         /* type, just ignore 'em.                                     */
1209         if(value_type == SHORT)
1210         {
1211             sum = read_ushort(inptr,byteorder,offset);
1212             for(i = 1; i < count; ++i)
1213                 sum += read_ushort(inptr,byteorder,HERE);
1214         }
1215         else if(value_type == LONG)
1216         {
1217             sum = read_ulong(inptr,byteorder,offset);
1218             for(i = 1; i < count; ++i)
1219                 sum += read_ulong(inptr,byteorder,HERE);
1220         }
1221     }
1222     return(sum);
1223 }
1224 
1225 
1226 void
why(FILE * outptr)1227 why(FILE *outptr)
1228 {
1229     fprintf(outptr,"\t because: %s\n",strerror(errno));
1230     errno = 0;
1231 }
1232 
1233 unsigned long
toggle(unsigned long options,unsigned long bits)1234 toggle(unsigned long options,unsigned long bits)
1235 {
1236     if(options & bits)
1237         options &= ~bits;
1238     else
1239         options |= bits;
1240     return(options);
1241 }
1242 
1243 
1244 /* Clean up memory used to save Make_name, etc.                       */
1245 void
clear_memory()1246 clear_memory()
1247 {
1248     if(Make_name)
1249         free(Make_name);
1250     if(Model_name)
1251         free(Model_name);
1252     if(Software_name)
1253         free(Software_name);
1254     Make_name = Model_name = Software_name = (char *)0;
1255 }
1256 
1257 /* splice two strings together with a separator between. The result   */
1258 /* is placed in dynamic memory. Free it when done with it.            */
1259 
1260 char *
splice(char * string1,char * sep,char * string2)1261 splice(char *string1,char *sep,char *string2)
1262 {
1263     char splicebuf[1024];
1264     char *result = NULL;
1265 
1266     if(string1 == NULL)
1267         sep = string1 = NULLSTRING;
1268     if(string2 == NULL)
1269         string2 = NULLSTRING;
1270     snprintf(splicebuf,1024,"%s%s%s",string1,sep,string2);
1271     result = strdup(splicebuf);
1272     return(result);
1273 }
1274 
1275 /* The following routines attempt to keep track of unterminated       */
1276 /* output lines and see that newlines are output as necessary, and    */
1277 /* never more than necessary.                                         */
1278 
1279 /* This requires keeping track of the count of characters printed and */
1280 /* calling either setcharsprinted(xxx) to register them, or           */
1281 /* newline(xxx) to force a newline. "newline(0)" will print a newline */
1282 /* only if Charsprinted is non-zero.                                  */
1283 
1284 /* This will work properly only if all functions which produce output */
1285 /* cooperate.                                                         */
1286 
1287 static int Charsprinted = 0;
1288 
1289 int
newline(int charsprinted)1290 newline(int charsprinted)
1291 {
1292     if((charsprinted != 0) || (Charsprinted != 0))
1293         putchar('\n');
1294     Charsprinted = 0;
1295     return(Charsprinted);
1296 }
1297 
1298 void
setcharsprinted(int charsprinted)1299 setcharsprinted(int charsprinted)
1300 {
1301     Charsprinted += charsprinted;
1302 }
1303 
1304 int
charsprinted()1305 charsprinted()
1306 {
1307     return(Charsprinted);
1308 }
1309 
1310 /* Record the filename and optionally prepend the filename to each    */
1311 /* output line. Useful e.g. when multiple files are being LISTed and  */
1312 /* grepped (exifgrep).                                                */
1313 
1314 static char *Current_filename = CNULL;
1315 
1316 void
setfilename(char * filename)1317 setfilename(char *filename)
1318 {
1319     Current_filename = filename;
1320 }
1321 
1322 void
print_filename()1323 print_filename()
1324 {
1325     int chpr = 0;
1326 
1327     if(((PRINT_FILENAMES)) && (Current_filename))
1328     {
1329         PUSHCOLOR(BLACK);
1330         chpr = printf("%s: ",Current_filename);
1331         POPCOLOR();
1332     }
1333     setcharsprinted(chpr);
1334 }
1335