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