- decode the EXIF GPS IFD as well
[imager.git] / imexif.c
1 #include "imexif.h"
2 #include <stdlib.h>
3 #include <float.h>
4
5 /*
6 =head1 NAME
7
8 imexif.c - EXIF support for Imager
9
10 =head1 SYNOPSIS
11
12   if (i_int_decode_exif(im, app1data, app1datasize)) {
13     // exif block seen
14   }
15
16 =head1 DESCRIPTION
17
18 This code provides a basic EXIF data decoder.  It is intended to be
19 called from the JPEG reader code when an APP1 data block is found, and
20 will set tags in the supplied image.
21
22 =cut
23 */
24
25 typedef enum tiff_type_tag {
26   tt_intel = 'I',
27   tt_motorola = 'M'
28 } tiff_type;
29
30 typedef enum {
31   ift_byte = 1,
32   ift_ascii = 2,
33   ift_short = 3,
34   ift_long = 4,
35   ift_rational = 5,
36   ift_sbyte = 6,
37   ift_undefined = 7,
38   ift_sshort = 8,
39   ift_slong = 9,
40   ift_srational = 10,
41   ift_float = 11,
42   ift_double = 12,
43   ift_last = 12 /* keep the same as the highest type code */
44 } ifd_entry_type;
45
46 static int type_sizes[] =
47   {
48     0, /* not used */
49     1, /* byte */
50     1, /* ascii */
51     2, /* short */
52     4, /* long */
53     8, /* rational */
54     1, /* sbyte */
55     1, /* undefined */
56     2, /* sshort */
57     4, /* slong */
58     8, /* srational */
59     4, /* float */
60     8, /* double */
61   };
62
63 typedef struct {
64   int tag;
65   int type;
66   int count;
67   int item_size;
68   int size;
69   int offset;
70 } ifd_entry;
71
72 typedef struct {
73   int tag;
74   char const *name;
75 } tag_map;
76
77 typedef struct {
78   int tag;
79   char const *name;
80   tag_map const *map;
81   int map_count;
82 } tag_value_map;
83
84 #define PASTE(left, right) PASTE_(left, right)
85 #define PASTE_(left, right) left##right
86 #define QUOTE(value) #value
87
88 #define VALUE_MAP_ENTRY(name) \
89   { \
90     PASTE(tag_, name), \
91     "exif_" QUOTE(name) "_name", \
92     PASTE(name, _values), \
93     ARRAY_COUNT(PASTE(name, _values)) \
94   }
95
96 /* we don't process every tag */
97 #define tag_make 271
98 #define tag_model 272
99 #define tag_orientation 274
100 #define tag_x_resolution 282
101 #define tag_y_resolution 283
102 #define tag_resolution_unit 296
103 #define tag_copyright 33432
104 #define tag_software 305
105 #define tag_artist 315
106 #define tag_date_time 306
107 #define tag_image_description 270
108
109 #define tag_exif_ifd 34665
110 #define tag_gps_ifd 34853
111
112 #define resunit_none 1
113 #define resunit_inch 2
114 #define resunit_centimeter 3
115
116 /* tags from the EXIF ifd */
117 #define tag_exif_version 0x9000
118 #define tag_flashpix_version 0xA000
119 #define tag_color_space 0xA001
120 #define tag_component_configuration 0x9101
121 #define tag_component_bits_per_pixel 0x9102
122 #define tag_pixel_x_dimension 0xA002
123 #define tag_pixel_y_dimension 0xA003
124 #define tag_maker_note 0x927C
125 #define tag_user_comment 0x9286
126 #define tag_related_sound_file 0xA004
127 #define tag_date_time_original 0x9003
128 #define tag_date_time_digitized 0x9004
129 #define tag_sub_sec_time 0x9290
130 #define tag_sub_sec_time_original 0x9291
131 #define tag_sub_sec_time_digitized 0x9292
132 #define tag_image_unique_id 0xA420
133 #define tag_exposure_time 0x829a
134 #define tag_f_number 0x829D
135 #define tag_exposure_program 0x8822
136 #define tag_spectral_sensitivity 0x8824
137 #define tag_iso_speed_ratings 0x8827
138 #define tag_oecf 0x8828
139 #define tag_shutter_speed 0x9201
140 #define tag_aperture 0x9202
141 #define tag_brightness 0x9203
142 #define tag_exposure_bias 0x9204
143 #define tag_max_aperture 0x9205
144 #define tag_subject_distance 0x9206
145 #define tag_metering_mode 0x9207
146 #define tag_light_source 0x9208
147 #define tag_flash 0x9209
148 #define tag_focal_length 0x920a
149 #define tag_subject_area 0x9214
150 #define tag_flash_energy 0xA20B
151 #define tag_spatial_frequency_response 0xA20C
152 #define tag_focal_plane_x_resolution 0xA20e
153 #define tag_focal_plane_y_resolution 0xA20F
154 #define tag_focal_plane_resolution_unit 0xA210
155 #define tag_subject_location 0xA214
156 #define tag_exposure_index 0xA215
157 #define tag_sensing_method 0xA217
158 #define tag_file_source 0xA300
159 #define tag_scene_type 0xA301
160 #define tag_cfa_pattern 0xA302
161 #define tag_custom_rendered 0xA401
162 #define tag_exposure_mode 0xA402
163 #define tag_white_balance 0xA403
164 #define tag_digital_zoom_ratio 0xA404
165 #define tag_focal_length_in_35mm_film 0xA405
166 #define tag_scene_capture_type 0xA406
167 #define tag_gain_control 0xA407
168 #define tag_contrast 0xA408
169 #define tag_saturation 0xA409
170 #define tag_sharpness 0xA40A
171 #define tag_device_setting_description 0xA40B
172 #define tag_subject_distance_range 0xA40C
173
174 /* GPS tags */
175 #define tag_gps_version_id 0
176 #define tag_gps_latitude_ref 1
177 #define tag_gps_latitude 2
178 #define tag_gps_longitude_ref 3
179 #define tag_gps_longitude 4
180 #define tag_gps_altitude_ref 5
181 #define tag_gps_altitude 6
182 #define tag_gps_time_stamp 7
183 #define tag_gps_satellites 8
184 #define tag_gps_status 9
185 #define tag_gps_measure_mode 10
186 #define tag_gps_dop 11
187 #define tag_gps_speed_ref 12
188 #define tag_gps_speed 13
189 #define tag_gps_track_ref 14
190 #define tag_gps_track 15
191 #define tag_gps_img_direction_ref 16
192 #define tag_gps_img_direction 17
193 #define tag_gps_map_datum 18
194 #define tag_gps_dest_latitude_ref 19
195 #define tag_gps_dest_latitude 20
196 #define tag_gps_dest_longitude_ref 21
197 #define tag_gps_dest_longitude 22
198 #define tag_gps_dest_bearing_ref 23
199 #define tag_gps_dest_bearing 24
200 #define tag_gps_dest_distance_ref 25
201 #define tag_gps_dest_distance 26
202 #define tag_gps_processing_method 27
203 #define tag_gps_area_information 28
204 #define tag_gps_date_stamp 29
205 #define tag_gps_differential 30
206
207 /* don't use this on pointers */
208 #define ARRAY_COUNT(array) (sizeof(array)/sizeof(*array))
209
210 /* in memory tiff structure */
211 typedef struct {
212   /* the data we use as a tiff */
213   unsigned char *base;
214   size_t size;
215
216   /* intel or motorola byte order */
217   tiff_type type;
218
219   /* initial ifd offset */
220   unsigned long first_ifd_offset;
221   
222   /* size (in entries) and data */
223   int ifd_size; 
224   ifd_entry *ifd;
225   unsigned long next_ifd;
226 } imtiff;
227
228 static int tiff_init(imtiff *tiff, unsigned char *base, size_t length);
229 static int tiff_load_ifd(imtiff *tiff, unsigned long offset);
230 static void tiff_final(imtiff *tiff);
231 static void tiff_clear_ifd(imtiff *tiff);
232 static int tiff_get_bytes(imtiff *tiff, unsigned char *to, size_t offset, 
233                           size_t count);
234 static int tiff_get_tag_double(imtiff *, int index, double *result);
235 static int tiff_get_tag_int(imtiff *, int index, int *result);
236 static unsigned tiff_get16(imtiff *, unsigned long offset);
237 static unsigned tiff_get32(imtiff *, unsigned long offset);
238 static int tiff_get16s(imtiff *, unsigned long offset);
239 static int tiff_get32s(imtiff *, unsigned long offset);
240 static double tiff_get_rat(imtiff *, unsigned long offset);
241 static double tiff_get_rats(imtiff *, unsigned long offset);
242 static void save_ifd0_tags(i_img *im, imtiff *tiff, unsigned long *exif_ifd_offset, unsigned long *gps_ifd_offset);
243 static void save_exif_ifd_tags(i_img *im, imtiff *tiff);
244 static void save_gps_ifd_tags(i_img *im, imtiff *tiff);
245 static void
246 copy_string_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
247 static void
248 copy_int_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
249 static void
250 copy_rat_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
251 static void
252 copy_num_array_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
253 static void
254 copy_name_tags(i_img *im, imtiff *tiff, tag_value_map *map, int map_count);
255 static void process_maker_note(i_img *im, imtiff *tiff, unsigned long offset, size_t size);
256
257 /*
258 =head1 PUBLIC FUNCTIONS
259
260 These functions are available to other parts of Imager.  They aren't
261 intended to be called from outside of Imager.
262
263 =over
264
265 =item i_int_decode_exit
266
267 i_int_decode_exif(im, data_base, data_size);
268
269 The data from data_base for data_size bytes will be scanned for EXIF
270 data.
271
272 Any data found will be used to set tags in the supplied image.
273
274 The intent is that invalid EXIF data will simply fail to set tags, and
275 write to the log.  In no case should this code exit when supplied
276 invalid data.
277
278 Returns true if an Exif header was seen.
279
280 */
281
282 int
283 i_int_decode_exif(i_img *im, unsigned char *data, size_t length) {
284   imtiff tiff;
285   unsigned long exif_ifd_offset = 0;
286   unsigned long gps_ifd_offset = 0;
287   /* basic checks - must start with "Exif\0\0" */
288
289   if (length < 6 || memcmp(data, "Exif\0\0", 6) != 0) {
290     return 0;
291   }
292
293   data += 6;
294   length -= 6;
295
296   if (!tiff_init(&tiff, data, length)) {
297     mm_log((2, "Exif header found, but no valid TIFF header\n"));
298     return 1;
299   }
300   if (!tiff_load_ifd(&tiff, tiff.first_ifd_offset)) {
301     mm_log((2, "Exif header found, but could not load IFD 0\n"));
302     tiff_final(&tiff);
303     return 1;
304   }
305
306   save_ifd0_tags(im, &tiff, &exif_ifd_offset, &gps_ifd_offset);
307
308   if (exif_ifd_offset) {
309     if (tiff_load_ifd(&tiff, exif_ifd_offset)) {
310       save_exif_ifd_tags(im, &tiff);
311     }
312     else {
313       mm_log((2, "Could not load Exif IFD\n"));
314     }
315   }
316
317   if (gps_ifd_offset) {
318     if (tiff_load_ifd(&tiff, gps_ifd_offset)) {
319       save_gps_ifd_tags(im, &tiff);
320     }
321     else {
322       mm_log((2, "Could not load GPS IFD\n"));
323     }
324   }
325
326   tiff_final(&tiff);
327
328   return 1;
329 }
330
331 /*
332
333 =back
334
335 =head1 INTERNAL FUNCTIONS
336
337 =head2 EXIF Processing 
338
339 =over
340
341 =item save_ifd0_tags
342
343 save_ifd0_tags(im, tiff, &exif_ifd_offset, &gps_ifd_offset)
344
345 Scans the currently loaded IFD for tags expected in IFD0 and sets them
346 in the image.
347
348 Sets *exif_ifd_offset to the offset of the EXIF IFD if found.
349
350 =cut
351
352 */
353
354 static tag_map ifd0_string_tags[] =
355   {
356     tag_make, "exif_make",
357     tag_model, "exif_model",
358     tag_copyright, "exif_copyright",
359     tag_software, "exif_software",
360     tag_artist, "exif_artist",
361     tag_date_time, "exif_date_time",
362     tag_image_description, "exif_image_description",
363   };
364
365 static const int ifd0_string_tag_count = ARRAY_COUNT(ifd0_string_tags);
366
367 static tag_map ifd0_int_tags[] =
368   {
369     { tag_orientation, "exif_orientation", },
370     { tag_resolution_unit, "exif_resolution_unit" },
371   };
372
373 static const int ifd0_int_tag_count = ARRAY_COUNT(ifd0_int_tags);
374
375 static tag_map ifd0_rat_tags[] =
376   {
377     { tag_x_resolution, "exif_x_resolution" },
378     { tag_y_resolution, "exif_y_resolution" },
379   };
380
381 static tag_map resolution_unit_values[] =
382   {
383     { 1, "none" },
384     { 2, "inches" },
385     { 3, "centimeters" },
386   };
387
388 static tag_value_map ifd0_values[] =
389   {
390     VALUE_MAP_ENTRY(resolution_unit),
391   };
392
393 static void
394 save_ifd0_tags(i_img *im, imtiff *tiff, unsigned long *exif_ifd_offset,
395                unsigned long *gps_ifd_offset) {
396   int i, tag_index;
397   int work;
398   ifd_entry *entry;
399
400   for (tag_index = 0, entry = tiff->ifd; 
401        tag_index < tiff->ifd_size; ++tag_index, ++entry) {
402     switch (entry->tag) {
403     case tag_exif_ifd:
404       if (tiff_get_tag_int(tiff, tag_index, &work))
405         *exif_ifd_offset = work;
406       break;
407
408     case tag_gps_ifd:
409       if (tiff_get_tag_int(tiff, tag_index, &work))
410         *gps_ifd_offset = work;
411       break;
412     }
413   }
414
415   copy_string_tags(im, tiff, ifd0_string_tags, ifd0_string_tag_count);
416   copy_int_tags(im, tiff, ifd0_int_tags, ifd0_int_tag_count);
417   copy_rat_tags(im, tiff, ifd0_rat_tags, ARRAY_COUNT(ifd0_rat_tags));
418   copy_name_tags(im, tiff, ifd0_values, ARRAY_COUNT(ifd0_values));
419   /* copy_num_array_tags(im, tiff, ifd0_num_arrays, ARRAY_COUNT(ifd0_num_arrays)); */
420 }
421
422 /*
423 =item save_exif_ifd_tags
424
425 save_exif_ifd_tags(im, tiff)
426
427 Scans the currently loaded IFD for the tags expected in the EXIF IFD
428 and sets them as tags in the image.
429
430 =cut
431
432 */
433
434 static tag_map exif_ifd_string_tags[] =
435   {
436     { tag_exif_version, "exif_version", },
437     { tag_flashpix_version, "exif_flashpix_version", },
438     { tag_related_sound_file, "exif_related_sound_file", },
439     { tag_date_time_original, "exif_date_time_original", },
440     { tag_date_time_digitized, "exif_date_time_digitized", },
441     { tag_sub_sec_time, "exif_sub_sec_time" },
442     { tag_sub_sec_time_original, "exif_sub_sec_time_original" },
443     { tag_sub_sec_time_digitized, "exif_sub_sec_time_digitized" },
444     { tag_image_unique_id, "exif_image_unique_id" },
445     { tag_spectral_sensitivity, "exif_spectral_sensitivity" },
446   };
447
448 static const int exif_ifd_string_tag_count = ARRAY_COUNT(exif_ifd_string_tags);
449
450 static tag_map exif_ifd_int_tags[] =
451   {
452     { tag_color_space, "exif_color_space" },
453     { tag_exposure_program, "exif_exposure_program" },
454     { tag_metering_mode, "exif_metering_mode" },
455     { tag_light_source, "exif_light_source" },
456     { tag_flash, "exif_flash" },
457     { tag_focal_plane_resolution_unit, "exif_focal_plane_resolution_unit" },
458     { tag_subject_location, "exif_subject_location" },
459     { tag_sensing_method, "exif_sensing_method" },
460     { tag_custom_rendered, "exif_custom_rendered" },
461     { tag_exposure_mode, "exif_exposure_mode" },
462     { tag_white_balance, "exif_white_balance" },
463     { tag_focal_length_in_35mm_film, "exif_focal_length_in_35mm_film" },
464     { tag_scene_capture_type, "exif_scene_capture_type" },
465     { tag_contrast, "exif_contrast" },
466     { tag_saturation, "exif_saturation" },
467     { tag_sharpness, "exif_sharpness" },
468     { tag_subject_distance_range, "exif_subject_distance_range" },
469   };
470
471
472 static const int exif_ifd_int_tag_count = ARRAY_COUNT(exif_ifd_int_tags);
473
474 static tag_map exif_ifd_rat_tags[] =
475   {
476     { tag_exposure_time, "exif_exposure_time" },
477     { tag_f_number, "exif_f_number" },
478     { tag_shutter_speed, "exif_shutter_speed" },
479     { tag_aperture, "exif_aperture" },
480     { tag_brightness, "exif_brightness" },
481     { tag_exposure_bias, "exif_exposure_bias" },
482     { tag_max_aperture, "exif_max_aperture" },
483     { tag_subject_distance, "exif_subject_distance" },
484     { tag_focal_length, "exif_focal_length" },
485     { tag_flash_energy, "exif_flash_energy" },
486     { tag_focal_plane_x_resolution, "exif_focal_plane_x_resolution" },
487     { tag_focal_plane_y_resolution, "exif_focal_plane_y_resolution" },
488     { tag_exposure_index, "exif_exposure_index" },
489     { tag_digital_zoom_ratio, "exif_digital_zoom_ratio" },
490     { tag_gain_control, "exif_gain_control" },
491   };
492
493 static const int exif_ifd_rat_tag_count = ARRAY_COUNT(exif_ifd_rat_tags);
494
495 static tag_map exposure_mode_values[] =
496   {
497     { 0, "Auto exposure" },
498     { 1, "Manual exposure" },
499     { 2, "Auto bracket" },
500   };
501 static tag_map color_space_values[] =
502   {
503     { 1, "sRGB" },
504     { 0xFFFF, "Uncalibrated" },
505   };
506
507 static tag_map exposure_program_values[] =
508   {
509     { 0, "Not defined" },
510     { 1, "Manual" },
511     { 2, "Normal program" },
512     { 3, "Aperture priority" },
513     { 4, "Shutter priority" },
514     { 5, "Creative program" },
515     { 6, "Action program" },
516     { 7, "Portrait mode" },
517     { 8, "Landscape mode" },
518   };
519
520 static tag_map metering_mode_values[] =
521   {
522     { 0, "unknown" },
523     { 1, "Average" },
524     { 2, "CenterWeightedAverage" },
525     { 3, "Spot" },
526     { 4, "MultiSpot" },
527     { 5, "Pattern" },
528     { 6, "Partial" },
529     { 255, "other" },
530   };
531
532 static tag_map light_source_values[] =
533   {
534     { 0, "unknown" },
535     { 1, "Daylight" },
536     { 2, "Fluorescent" },
537     { 3, "Tungsten (incandescent light)" },
538     { 4, "Flash" },
539     { 9, "Fine weather" },
540     { 10, "Cloudy weather" },
541     { 11, "Shade" },
542     { 12, "Daylight fluorescent (D 5700 Ã 7100K)" },
543     { 13, "Day white fluorescent (N 4600 Ã 5400K)" },
544     { 14, "Cool white fluorescent (W 3900 Ã 4500K)" },
545     { 15, "White fluorescent (WW 3200 Ã 3700K)" },
546     { 17, "Standard light A" },
547     { 18, "Standard light B" },
548     { 19, "Standard light C" },
549     { 20, "D55" },
550     { 21, "D65" },
551     { 22, "D75" },
552     { 23, "D50" },
553     { 24, "ISO studio tungsten" },
554     { 255, "other light source" },
555   };
556
557 static tag_map flash_values[] =
558   {
559     { 0x0000, "Flash did not fire." },
560     { 0x0001, "Flash fired." },
561     { 0x0005, "Strobe return light not detected." },
562     { 0x0007, "Strobe return light detected." },
563     { 0x0009, "Flash fired, compulsory flash mode" },
564     { 0x000D, "Flash fired, compulsory flash mode, return light not detected" },
565     { 0x000F, "Flash fired, compulsory flash mode, return light detected" },
566     { 0x0010, "Flash did not fire, compulsory flash mode" },
567     { 0x0018, "Flash did not fire, auto mode" },
568     { 0x0019, "Flash fired, auto mode" },
569     { 0x001D, "Flash fired, auto mode, return light not detected" },
570     { 0x001F, "Flash fired, auto mode, return light detected" },
571     { 0x0020, "No flash function" },
572     { 0x0041, "Flash fired, red-eye reduction mode" },
573     { 0x0045, "Flash fired, red-eye reduction mode, return light not detected" },
574     { 0x0047, "Flash fired, red-eye reduction mode, return light detected" },
575     { 0x0049, "Flash fired, compulsory flash mode, red-eye reduction mode" },
576     { 0x004D, "Flash fired, compulsory flash mode, red-eye reduction mode, return light not detected" },
577     { 0x004F, "Flash fired, compulsory flash mode, red-eye reduction mode, return light detected" },
578     { 0x0059, "Flash fired, auto mode, red-eye reduction mode" },
579     { 0x005D, "Flash fired, auto mode, return light not detected, red-eye reduction mode" },
580     { 0x005F, "Flash fired, auto mode, return light detected, red-eye reduction mode" },
581   };
582
583 static tag_map sensing_method_values[] =
584   {
585     { 1, "Not defined" },
586     { 2, "One-chip color area sensor" },
587     { 3, "Two-chip color area sensor" },
588     { 4, "Three-chip color area sensor" },
589     { 5, "Color sequential area sensor" },
590     { 7, "Trilinear sensor" },
591     { 8, "Color sequential linear sensor" },
592   };
593
594 static tag_map custom_rendered_values[] =
595   {
596     { 0, "Normal process" },
597     { 1, "Custom process" },
598   };
599
600 static tag_map white_balance_values[] =
601   {
602     { 0, "Auto white balance" },
603     { 1, "Manual white balance" },
604   };
605
606 static tag_map scene_capture_type_values[] =
607   {
608     { 0, "Standard" },
609     { 1, "Landscape" },
610     { 2, "Portrait" },
611     { 3, "Night scene" },
612   };
613
614 static tag_map gain_control_values[] =
615   {
616     { 0, "None" },
617     { 1, "Low gain up" },
618     { 2, "High gain up" },
619     { 3, "Low gain down" },
620     { 4, "High gain down" },
621   };
622
623 static tag_map contrast_values[] =
624   {
625     { 0, "Normal" },
626     { 1, "Soft" },
627     { 2, "Hard" },
628   };
629
630 static tag_map saturation_values[] =
631   {
632     { 0, "Normal" },
633     { 1, "Low saturation" },
634     { 2, "High saturation" },
635   };
636
637 static tag_map sharpness_values[] =
638   {
639     { 0, "Normal" },
640     { 1, "Soft" },
641     { 2, "Hard" },
642   };
643
644 static tag_map subject_distance_range_values[] =
645   {
646     { 0, "unknown" },
647     { 1, "Macro" },
648     { 2, "Close view" },
649     { 3, "Distant view" },
650   };
651
652 #define focal_plane_resolution_unit_values resolution_unit_values
653
654 static tag_value_map exif_ifd_values[] =
655   {
656     VALUE_MAP_ENTRY(exposure_mode),
657     VALUE_MAP_ENTRY(color_space),
658     VALUE_MAP_ENTRY(exposure_program),
659     VALUE_MAP_ENTRY(metering_mode),
660     VALUE_MAP_ENTRY(light_source),
661     VALUE_MAP_ENTRY(flash),
662     VALUE_MAP_ENTRY(sensing_method),
663     VALUE_MAP_ENTRY(custom_rendered),
664     VALUE_MAP_ENTRY(white_balance),
665     VALUE_MAP_ENTRY(scene_capture_type),
666     VALUE_MAP_ENTRY(gain_control),
667     VALUE_MAP_ENTRY(contrast),
668     VALUE_MAP_ENTRY(saturation),
669     VALUE_MAP_ENTRY(sharpness),
670     VALUE_MAP_ENTRY(subject_distance_range),
671     VALUE_MAP_ENTRY(focal_plane_resolution_unit),
672   };
673
674 static tag_map exif_num_arrays[] =
675   {
676     { tag_iso_speed_ratings, "exif_iso_speed_ratings" },
677     { tag_subject_area, "exif_subject_area" },
678     { tag_subject_location, "exif_subject_location" },
679   };
680
681 static void
682 save_exif_ifd_tags(i_img *im, imtiff *tiff) {
683   int i, tag_index;
684   int work;
685   ifd_entry *entry;
686   char *user_comment;
687   unsigned long maker_note_offset = 0;
688   size_t maker_note_size = 0;
689
690   for (tag_index = 0, entry = tiff->ifd; 
691        tag_index < tiff->ifd_size; ++tag_index, ++entry) {
692     switch (entry->tag) {
693     case tag_user_comment:
694       /* I don't want to trash the source, so work on a copy */
695       user_comment = mymalloc(entry->size);
696       memcpy(user_comment, tiff->base + entry->offset, entry->size);
697       /* the first 8 bytes indicate the encoding, make them into spaces
698          for better presentation */
699       for (i = 0; i < 8; ++i) {
700         if (user_comment[i] == '\0')
701           user_comment[i] = ' ';
702       }
703       /* find the actual end of the string */
704       while (i < entry->size && user_comment[i])
705         ++i;
706       i_tags_add(&im->tags, "exif_user_comment", 0, user_comment, i, 0);
707       myfree(user_comment);
708       break;
709
710     case tag_maker_note:
711       maker_note_offset = entry->offset;
712       maker_note_size = entry->size;
713       break;
714
715       /* the following aren't processed yet */
716     case tag_oecf:
717     case tag_spatial_frequency_response:
718     case tag_file_source:
719     case tag_scene_type:
720     case tag_cfa_pattern:
721     case tag_device_setting_description:
722     case tag_subject_area:
723       break;
724     }
725   }
726
727   copy_string_tags(im, tiff, exif_ifd_string_tags, exif_ifd_string_tag_count);
728   copy_int_tags(im, tiff, exif_ifd_int_tags, exif_ifd_int_tag_count);
729   copy_rat_tags(im, tiff, exif_ifd_rat_tags, exif_ifd_rat_tag_count);
730   copy_name_tags(im, tiff, exif_ifd_values, ARRAY_COUNT(exif_ifd_values));
731   copy_num_array_tags(im, tiff, exif_num_arrays, ARRAY_COUNT(exif_num_arrays));
732
733   /* This trashes the IFD - make sure it's done last */
734   if (maker_note_offset) {
735     process_maker_note(im, tiff, maker_note_offset, maker_note_size);
736   }
737 }
738
739 static tag_map gps_ifd_string_tags[] =
740   {
741     { tag_gps_version_id, "exif_gps_version_id" },
742     { tag_gps_latitude_ref, "exif_gps_latitude_ref" },
743     { tag_gps_longitude_ref, "exif_gps_longitude_ref" },
744     { tag_gps_altitude_ref, "exif_gps_altitude_ref" },
745     { tag_gps_satellites, "exif_gps_satellites" },
746     { tag_gps_status, "exif_gps_status" },
747     { tag_gps_measure_mode, "exif_gps_measure_mode" },
748     { tag_gps_speed_ref, "exif_gps_speed_ref" },
749     { tag_gps_track_ref, "exif_gps_track_ref" },
750   };
751
752 static tag_map gps_ifd_int_tags[] =
753   {
754     { tag_gps_differential, "exif_gps_differential" },
755   };
756
757 static tag_map gps_ifd_rat_tags[] =
758   {
759     { tag_gps_altitude, "exif_gps_altitude" },
760     { tag_gps_time_stamp, "exif_gps_time_stamp" },
761     { tag_gps_dop, "exif_gps_dop" },
762     { tag_gps_speed, "exif_gps_speed" },
763     { tag_gps_track, "exif_track" }
764   };
765
766 static tag_map gps_differential_values [] =
767   {
768     { 0, "without differential correction" },
769     { 1, "Differential correction applied" },
770   };
771
772 static tag_value_map gps_ifd_values[] =
773   {
774     VALUE_MAP_ENTRY(gps_differential),
775   };
776
777 static tag_map gps_num_arrays[] =
778   {
779     { tag_gps_latitude, "exif_gps_latitude" },
780     { tag_gps_longitude, "exif_gps_longitude" },
781   };
782
783 static void
784 save_gps_ifd_tags(i_img *im, imtiff *tiff) {
785   int i, tag_index;
786   int work;
787   ifd_entry *entry;
788
789   /* for (tag_index = 0, entry = tiff->ifd; 
790        tag_index < tiff->ifd_size; ++tag_index, ++entry) {
791     switch (entry->tag) {
792       break;
793     }
794     }*/
795
796   copy_string_tags(im, tiff, gps_ifd_string_tags, 
797          ARRAY_COUNT(gps_ifd_string_tags));
798   copy_int_tags(im, tiff, gps_ifd_int_tags, ARRAY_COUNT(gps_ifd_int_tags));
799   copy_rat_tags(im, tiff, gps_ifd_rat_tags, ARRAY_COUNT(gps_ifd_rat_tags));
800   copy_name_tags(im, tiff, gps_ifd_values, ARRAY_COUNT(gps_ifd_values));
801   copy_num_array_tags(im, tiff, gps_num_arrays, ARRAY_COUNT(gps_num_arrays));
802 }
803
804 /*
805 =item process_maker_note
806
807 This is a stub for processing the maker note tag.
808
809 Maker notes aren't covered by EXIF itself and in general aren't
810 documented by the manufacturers.
811
812 =cut
813 */
814
815 static void
816 process_maker_note(i_img *im, imtiff *tiff, unsigned long offset, size_t size) {
817   /* this will be added in a future release */
818 }
819
820 /*
821 =back
822
823 =head2 High level TIFF functions
824
825 To avoid relying upon tifflib when we're not processing an image we
826 have some simple in-memory TIFF file management.
827
828 =over
829
830 =item tiff_init
831
832 imtiff tiff;
833 if (tiff_init(tiff, data_base, data_size)) {
834   // success
835 }
836
837 Initialize the tiff data structure.
838
839 Scans for the byte order and version markers, and stores the offset to
840 the first IFD (IFD0 in EXIF) in first_ifd_offset.
841
842 =cut
843 */
844
845 static int
846 tiff_init(imtiff *tiff, unsigned char *data, size_t length) {
847   int version;
848
849   tiff->base = data;
850   tiff->size = length;
851   if (length < 8) /* well... would have to be much bigger to be useful */
852     return 0;
853   if (data[0] == 'M' && data[1] == 'M')
854     tiff->type = tt_motorola;
855   else if (data[0] == 'I' && data[1] == 'I') 
856     tiff->type = tt_intel;
857   else
858     return 0; /* invalid header */
859
860   version = tiff_get16(tiff, 2);
861   if (version != 42)
862     return 0;
863
864   tiff->first_ifd_offset = tiff_get32(tiff, 4);
865   if (tiff->first_ifd_offset > length || tiff->first_ifd_offset < 8)
866     return 0;
867
868   tiff->ifd_size = 0;
869   tiff->ifd = NULL;
870   tiff->next_ifd = 0;
871
872   return 1;
873 }
874
875 /*
876 =item tiff_final
877
878 tiff_final(&tiff)
879
880 Clean up the tiff structure initialized by tiff_init()
881
882 =cut
883 */
884
885 static void
886 tiff_final(imtiff *tiff) {
887   tiff_clear_ifd(tiff);
888 }
889
890 /*
891 =item tiff_load_ifd
892
893 if (tiff_load_ifd(tiff, offset)) {
894   // process the ifd
895 }
896
897 Loads the IFD from the given offset into the tiff objects ifd.
898
899 This can fail if the IFD extends beyond end of file, or if any data
900 offsets combined with their sizes, extends beyond end of file.
901
902 Returns true on success.
903
904 =cut
905 */
906
907 static int
908 tiff_load_ifd(imtiff *tiff, unsigned long offset) {
909   unsigned count;
910   int ifd_size;
911   ifd_entry *entries = NULL;
912   int i;
913   unsigned long base;
914
915   tiff_clear_ifd(tiff);
916
917   /* rough check count + 1 entry + next offset */
918   if (offset + (2+12+4) > tiff->size) {
919     mm_log((2, "offset %uld beyond end off Exif block"));
920     return 0;
921   }
922
923   count = tiff_get16(tiff, offset);
924   
925   /* check we can fit the whole thing */
926   ifd_size = 2 + count * 12 + 4; /* count + count entries + next offset */
927   if (offset + ifd_size > tiff->size) {
928     mm_log((2, "offset %uld beyond end off Exif block"));
929     return 0;
930   }
931
932   entries = mymalloc(count * sizeof(ifd_entry));
933   memset(entries, 0, count * sizeof(ifd_entry));
934   base = offset + 2;
935   for (i = 0; i < count; ++i) {
936     ifd_entry *entry = entries + i;
937     entry->tag = tiff_get16(tiff, base);
938     entry->type = tiff_get16(tiff, base+2);
939     entry->count = tiff_get32(tiff, base+4);
940     if (entry->type >= 1 || entry->type <= ift_last) {
941       entry->item_size = type_sizes[entry->type];
942       entry->size = entry->item_size * entry->count;
943       if (entry->size / entry->item_size != entry->count) {
944         mm_log((1, "Integer overflow calculating tag data size processing EXIF block\n"));
945         return 0;
946       }
947       else if (entry->size <= 4) {
948         entry->offset = base + 8;
949       }
950       else {
951         entry->offset = tiff_get32(tiff, base+8);
952         if (entry->offset + entry->size > tiff->size) {
953           mm_log((2, "Invalid data offset processing IFD\n"));
954           myfree(entries);
955           return 0;
956         }
957       }
958     }
959     else {
960       entry->size = 0;
961       entry->offset = 0;
962     }
963     base += 12;
964   }
965
966   tiff->ifd_size = count;
967   tiff->ifd = entries;
968   tiff->next_ifd = tiff_get32(tiff, base);
969
970   return 1;
971 }
972
973 /*
974 =item tiff_clear_ifd
975
976 tiff_clear_ifd(tiff)
977
978 Releases any memory associated with the stored IFD and resets the IFD
979 pointers.
980
981 This is called by tiff_load_ifd() and tiff_final().
982
983 =cut
984 */
985
986 static void
987 tiff_clear_ifd(imtiff *tiff) {
988   int i;
989  
990   if (tiff->ifd_size && tiff->ifd) {
991     myfree(tiff->ifd);
992     tiff->ifd_size = 0;
993     tiff->ifd = NULL;
994   }
995 }
996
997 /*
998 =item tiff_get_tag_double
999
1000   double value;
1001   if (tiff_get_tag(tiff, index, &value)) {
1002     // process value
1003   }
1004
1005 Attempts to retrieve a double value from the given index in the
1006 current IFD.
1007
1008 The value must have a count of 1.
1009
1010 =cut
1011 */
1012
1013 static int
1014 tiff_get_tag_double_array(imtiff *tiff, int index, double *result, 
1015                           int array_index) {
1016   ifd_entry *entry;
1017   unsigned long offset;
1018   if (index < 0 || index >= tiff->ifd_size) {
1019     m_fatal(3, "tiff_get_tag_double_array() tag index out of range");
1020   }
1021   
1022   entry = tiff->ifd + index;
1023   if (array_index < 0 || array_index >= entry->count) {
1024     mm_log((3, "tiff_get_tag_double_array() array index out of range"));
1025     return 0;
1026   }
1027
1028   offset = entry->offset + array_index * entry->item_size;
1029
1030   switch (entry->type) {
1031   case ift_short:
1032     *result = tiff_get16(tiff, offset);
1033     return 1;
1034    
1035   case ift_long:
1036     *result = tiff_get32(tiff, offset);
1037     return 1;
1038
1039   case ift_rational:
1040     *result = tiff_get_rat(tiff, offset);
1041     return 1;
1042
1043   case ift_sshort:
1044     *result = tiff_get16s(tiff, offset);
1045     return 1;
1046
1047   case ift_slong:
1048     *result = tiff_get32s(tiff, offset);
1049     return 1;
1050
1051   case ift_srational:
1052     *result = tiff_get_rats(tiff, offset);
1053     return 1;
1054
1055   case ift_byte:
1056     *result = *(tiff->base + offset);
1057     return 1;
1058   }
1059
1060   return 0;
1061 }
1062
1063 /*
1064 =item tiff_get_tag_double
1065
1066   double value;
1067   if (tiff_get_tag(tiff, index, &value)) {
1068     // process value
1069   }
1070
1071 Attempts to retrieve a double value from the given index in the
1072 current IFD.
1073
1074 The value must have a count of 1.
1075
1076 =cut
1077 */
1078
1079 static int
1080 tiff_get_tag_double(imtiff *tiff, int index, double *result) {
1081   ifd_entry *entry;
1082   if (index < 0 || index >= tiff->ifd_size) {
1083     m_fatal(3, "tiff_get_tag_double() index out of range");
1084   }
1085   
1086   entry = tiff->ifd + index;
1087   if (entry->count != 1) {
1088     mm_log((3, "tiff_get_tag_double() called on tag with multiple values"));
1089     return 0;
1090   }
1091
1092   return tiff_get_tag_double_array(tiff, index, result, 0);
1093 }
1094
1095 /*
1096 =item tiff_get_tag_int_array
1097
1098   int value;
1099   if (tiff_get_tag_int_array(tiff, index, &value, array_index)) {
1100     // process value
1101   }
1102
1103 Attempts to retrieve an integer value from the given index in the
1104 current IFD.
1105
1106 =cut
1107 */
1108
1109 static int
1110 tiff_get_tag_int_array(imtiff *tiff, int index, int *result, int array_index) {
1111   ifd_entry *entry;
1112   unsigned long offset;
1113   if (index < 0 || index >= tiff->ifd_size) {
1114     m_fatal(3, "tiff_get_tag_int_array() tag index out of range");
1115   }
1116   
1117   entry = tiff->ifd + index;
1118   if (array_index < 0 || array_index >= entry->count) {
1119     m_fatal(3, "tiff_get_tag_int_array() array index out of range");
1120   }
1121
1122   offset = entry->offset + array_index * entry->item_size;
1123
1124   switch (entry->type) {
1125   case ift_short:
1126     *result = tiff_get16(tiff, offset);
1127     return 1;
1128    
1129   case ift_long:
1130     *result = tiff_get32(tiff, offset);
1131     return 1;
1132
1133   case ift_sshort:
1134     *result = tiff_get16s(tiff, offset);
1135     return 1;
1136
1137   case ift_slong:
1138     *result = tiff_get32s(tiff, offset);
1139     return 1;
1140
1141   case ift_byte:
1142     *result = *(tiff->base + offset);
1143     return 1;
1144   }
1145
1146   return 0;
1147 }
1148
1149 /*
1150 =item tiff_get_tag_int
1151
1152   int value;
1153   if (tiff_get_tag_int(tiff, index, &value)) {
1154     // process value
1155   }
1156
1157 Attempts to retrieve an integer value from the given index in the
1158 current IFD.
1159
1160 The value must have a count of 1.
1161
1162 =cut
1163 */
1164
1165 static int
1166 tiff_get_tag_int(imtiff *tiff, int index, int *result) {
1167   ifd_entry *entry;
1168   if (index < 0 || index >= tiff->ifd_size) {
1169     m_fatal(3, "tiff_get_tag_int() index out of range");
1170   }
1171
1172   entry = tiff->ifd + index;
1173   if (entry->count != 1) {
1174     mm_log((3, "tiff_get_tag_int() called on tag with multiple values"));
1175     return 0;
1176   }
1177
1178   return tiff_get_tag_int_array(tiff, index, result, 0);
1179 }
1180
1181 /*
1182 =back
1183
1184 =head2 Table-based tag setters
1185
1186 This set of functions checks for matches between the current IFD and
1187 tags supplied in an array, when there's a match it sets the
1188 appropriate tag in the image.
1189
1190 =over
1191
1192 =item copy_int_tags
1193
1194 Scans the IFD for integer tags and sets them in the image,
1195
1196 =cut
1197 */
1198
1199 static void
1200 copy_int_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count) {
1201   int i, tag_index;
1202   ifd_entry *entry;
1203
1204   for (tag_index = 0, entry = tiff->ifd; 
1205        tag_index < tiff->ifd_size; ++tag_index, ++entry) {
1206     for (i = 0; i < map_count; ++i) {
1207       int value;
1208       if (map[i].tag == entry->tag
1209           && tiff_get_tag_int(tiff, tag_index, &value)) {
1210         i_tags_addn(&im->tags, map[i].name, 0, value);
1211         break;
1212       }
1213     }
1214   }
1215 }
1216
1217 /*
1218 =item copy_rat_tags
1219
1220 Scans the IFD for rational tags and sets them in the image.
1221
1222 =cut
1223 */
1224
1225 static void
1226 copy_rat_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count) {
1227   int i, tag_index;
1228   ifd_entry *entry;
1229
1230   for (tag_index = 0, entry = tiff->ifd; 
1231        tag_index < tiff->ifd_size; ++tag_index, ++entry) {
1232     for (i = 0; i < map_count; ++i) {
1233       double value;
1234       if (map[i].tag == entry->tag
1235           && tiff_get_tag_double(tiff, tag_index, &value)) {
1236         i_tags_set_float2(&im->tags, map[i].name, 0, value, 6);
1237         break;
1238       }
1239     }
1240   }
1241 }
1242
1243 /*
1244 =item copy_string_tags
1245
1246 Scans the IFD for string tags and sets them in the image.
1247
1248 =cut
1249 */
1250
1251 static void
1252 copy_string_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count) {
1253   int i, tag_index;
1254   ifd_entry *entry;
1255
1256   for (tag_index = 0, entry = tiff->ifd; 
1257        tag_index < tiff->ifd_size; ++tag_index, ++entry) {
1258     for (i = 0; i < map_count; ++i) {
1259       if (map[i].tag == entry->tag) {
1260         int len = entry->type == ift_ascii ? entry->size - 1 : entry->size;
1261         i_tags_add(&im->tags, map[i].name, 0,
1262                    (char const *)(tiff->base + entry->offset), len, 0);
1263         break;
1264       }
1265     }
1266   }
1267 }
1268
1269 /*
1270 =item copy_num_array_tags
1271
1272 Scans the IFD for arrays of numbers and sets them in the image.
1273
1274 =cut
1275 */
1276
1277 /* a more general solution would be better in some ways, but we don't need it */
1278 #define MAX_ARRAY_VALUES 10
1279 #define MAX_ARRAY_STRING (MAX_ARRAY_VALUES * 20)
1280
1281 static void
1282 copy_num_array_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count) {
1283   int i, j, tag_index;
1284   ifd_entry *entry;
1285
1286   for (tag_index = 0, entry = tiff->ifd; 
1287        tag_index < tiff->ifd_size; ++tag_index, ++entry) {
1288     for (i = 0; i < map_count; ++i) {
1289       if (map[i].tag == entry->tag && entry->count <= MAX_ARRAY_VALUES) {
1290         if (entry->type == ift_rational || entry->type == ift_srational) {
1291           double value;
1292           char workstr[MAX_ARRAY_STRING];
1293           *workstr = '\0';
1294           for (j = 0; j < entry->count; ++j) {
1295             if (!tiff_get_tag_double_array(tiff, tag_index, &value, j)) {
1296               m_fatal(3, "unexpected failure from tiff_get_tag_double_array(..., %d, ..., %d)\n", tag_index, j);
1297             }
1298             if (j) 
1299               strcat(workstr, " ");
1300             sprintf(workstr + strlen(workstr), "%.6g", value);
1301           }
1302           i_tags_add(&im->tags, map[i].name, 0, workstr, -1, 0);
1303         }
1304         else if (entry->type == ift_short || entry->type == ift_long
1305                  || entry->type == ift_sshort || entry->type == ift_slong
1306                  || entry->type == ift_byte) {
1307           int value;
1308           char workstr[MAX_ARRAY_STRING];
1309           *workstr = '\0';
1310           for (j = 0; j < entry->count; ++j) {
1311             if (!tiff_get_tag_int_array(tiff, tag_index, &value, j)) {
1312               m_fatal(3, "unexpected failure from tiff_get_tag_int_array(..., %d, ..., %d)\n", tag_index, j);
1313             }
1314             if (j) 
1315               strcat(workstr, " ");
1316             sprintf(workstr + strlen(workstr), "%d", value);
1317           }
1318           i_tags_add(&im->tags, map[i].name, 0, workstr, -1, 0);
1319         }
1320         break;
1321       }
1322     }
1323   }
1324 }
1325
1326 /*
1327 =item copy_name_tags
1328
1329 This function maps integer values to descriptions for those values.
1330
1331 In general we handle the integer value through copy_int_tags() and
1332 then the same tage with a "_name" suffix here.
1333
1334 =cut
1335 */
1336
1337 static void
1338 copy_name_tags(i_img *im, imtiff *tiff, tag_value_map *map, int map_count) {
1339   int i, j, tag_index;
1340   ifd_entry *entry;
1341
1342   for (tag_index = 0, entry = tiff->ifd; 
1343        tag_index < tiff->ifd_size; ++tag_index, ++entry) {
1344     for (i = 0; i < map_count; ++i) {
1345       int value;
1346       if (map[i].tag == entry->tag
1347           && tiff_get_tag_int(tiff, tag_index, &value)) {
1348         tag_map const *found = NULL;
1349         for (j = 0; j < map[i].map_count; ++j) {
1350           if (value == map[i].map[j].tag) {
1351             found = map[i].map + j;
1352             break;
1353           }
1354         }
1355         if (found) {
1356           i_tags_add(&im->tags, map[i].name, 0, found->name, -1, 0);
1357         }
1358         break;
1359       }
1360     }
1361   }
1362 }
1363
1364
1365 /*
1366 =back
1367
1368 =head2 Low level data access functions
1369
1370 These functions use the byte order in the tiff object to extract
1371 various types of data from the tiff data.
1372
1373 These functions will abort if called with an out of range offset.
1374
1375 The intent is that any offset checks should have been done by the caller.
1376
1377 =over
1378
1379 =item tiff_get16
1380
1381 Retrieve a 16 bit unsigned integer from offset.
1382
1383 =cut
1384 */
1385
1386 static unsigned
1387 tiff_get16(imtiff *tiff, unsigned long offset) {
1388   if (offset + 2 > tiff->size)
1389     m_fatal(3, "attempt to get16 at %uld in %uld image", offset, tiff->size);
1390
1391   if (tiff->type == tt_intel) 
1392     return tiff->base[offset] + 0x100 * tiff->base[offset+1];
1393   else
1394     return tiff->base[offset+1] + 0x100 * tiff->base[offset];
1395 }
1396
1397 /*
1398 =item tiff_get32
1399
1400 Retrieve a 32-bit unsigned integer from offset.
1401
1402 =cut
1403 */
1404
1405 static unsigned
1406 tiff_get32(imtiff *tiff, unsigned long offset) {
1407   if (offset + 4 > tiff->size)
1408     m_fatal(3, "attempt to get16 at %uld in %uld image", offset, tiff->size);
1409
1410   if (tiff->type == tt_intel) 
1411     return tiff->base[offset] + 0x100 * tiff->base[offset+1] 
1412       + 0x10000 * tiff->base[offset+2] + 0x1000000 * tiff->base[offset+3];
1413   else
1414     return tiff->base[offset+3] + 0x100 * tiff->base[offset+2]
1415       + 0x10000 * tiff->base[offset+1] + 0x1000000 * tiff->base[offset];
1416 }
1417
1418 /*
1419 =item tiff_get_bytes
1420
1421 Retrieve a byte string from offset.
1422
1423 This isn't used much, you can usually deal with the data in-situ.
1424 This is intended for use when you need to modify the data in some way.
1425
1426 =cut
1427 */
1428
1429 static int
1430 tiff_get_bytes(imtiff *tiff, unsigned char *data, size_t offset, 
1431                size_t size) {
1432   if (offset + size > tiff->size)
1433     return 0;
1434
1435   memcpy(data, tiff->base+offset, size);
1436
1437   return 1;
1438 }
1439
1440 /*
1441 =item tiff_get16s
1442
1443 Retrieve a 16-bit signed integer from offset.
1444
1445 =cut
1446 */
1447
1448 static int
1449 tiff_get16s(imtiff *tiff, unsigned long offset) {
1450   int result;
1451
1452   if (offset + 2 > tiff->size)
1453     m_fatal(3, "attempt to get16 at %uld in %uld image", offset, tiff->size);
1454
1455   if (tiff->type == tt_intel) 
1456     result = tiff->base[offset] + 0x100 * tiff->base[offset+1];
1457   else
1458     result = tiff->base[offset+1] + 0x100 * tiff->base[offset];
1459
1460   if (result > 0x7FFF)
1461     result -= 0x10000;
1462
1463   return result;
1464 }
1465
1466 /*
1467 =item tiff_get32s
1468
1469 Retrieve a 32-bit signed integer from offset.
1470
1471 =cut
1472 */
1473
1474 static int
1475 tiff_get32s(imtiff *tiff, unsigned long offset) {
1476   unsigned work;
1477
1478   if (offset + 4 > tiff->size)
1479     m_fatal(3, "attempt to get16 at %uld in %uld image", offset, tiff->size);
1480
1481   if (tiff->type == tt_intel) 
1482     work = tiff->base[offset] + 0x100 * tiff->base[offset+1] 
1483       + 0x10000 * tiff->base[offset+2] + 0x1000000 * tiff->base[offset+3];
1484   else
1485     work = tiff->base[offset+3] + 0x100 * tiff->base[offset+2]
1486       + 0x10000 * tiff->base[offset+1] + 0x1000000 * tiff->base[offset];
1487
1488   /* not really needed on 32-bit int machines */
1489   if (work > 0x7FFFFFFFUL)
1490     return work - 0x80000000UL;
1491   else
1492     return work;
1493 }
1494
1495 /*
1496 =item tiff_get_rat
1497
1498 Retrieve an unsigned rational from offset.
1499
1500 =cut
1501 */
1502
1503 static double
1504 tiff_get_rat(imtiff *tiff, unsigned long offset) {
1505   unsigned long numer, denom;
1506   if (offset + 8 > tiff->size)
1507     m_fatal(3, "attempt to get_rat at %lu in %lu image", offset, tiff->size);
1508
1509   numer = tiff_get32(tiff, offset);
1510   denom = tiff_get32(tiff, offset+4);
1511
1512   if (denom == 0) {
1513     return -DBL_MAX;
1514   }
1515
1516   return (double)numer / denom;
1517 }
1518
1519 /*
1520 =item tiff_get_rats
1521
1522 Retrieve an signed rational from offset.
1523
1524 =cut
1525 */
1526
1527 static double
1528 tiff_get_rats(imtiff *tiff, unsigned long offset) {
1529   long numer, denom;
1530   if (offset + 8 > tiff->size)
1531     m_fatal(3, "attempt to get_rat at %lu in %lu image", offset, tiff->size);
1532
1533   numer = tiff_get32s(tiff, offset);
1534   denom = tiff_get32s(tiff, offset+4);
1535
1536   if (denom == 0) {
1537     return -DBL_MAX;
1538   }
1539
1540   return (double)numer / denom;
1541 }
1542
1543 /*
1544 =back
1545
1546 =head1 SEE ALSO
1547
1548 L<Imager>, jpeg.c
1549
1550 http://www.exif.org/
1551
1552 =head1 AUTHOR
1553
1554 Tony Cook <tony@imager.perl.org>
1555
1556 =head1 REVISION
1557
1558 $Revision$
1559
1560 =cut
1561 */