Debian Codegraph @

imexif.c

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