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