long delayed renaming of m_fatal() to i_fatal() to match Imager's
[imager.git] / imexif.c
CommitLineData
f7450478
TC
1#include "imexif.h"
2#include <stdlib.h>
3#include <float.h>
4
5/*
6=head1 NAME
7
8imexif.c - EXIF support for Imager
9
10=head1 SYNOPSIS
11
12 if (i_int_decode_exif(im, app1data, app1datasize)) {
13 // exif block seen
14 }
15
16=head1 DESCRIPTION
17
18This code provides a basic EXIF data decoder. It is intended to be
19called from the JPEG reader code when an APP1 data block is found, and
20will set tags in the supplied image.
21
22=cut
23*/
24
25typedef enum tiff_type_tag {
26 tt_intel = 'I',
27 tt_motorola = 'M'
28} tiff_type;
29
30typedef enum {
31 ift_byte = 1,
32 ift_ascii = 2,
33 ift_short = 3,
34 ift_long = 4,
35 ift_rational = 5,
36 ift_sbyte = 6,
37 ift_undefined = 7,
38 ift_sshort = 8,
39 ift_slong = 9,
40 ift_srational = 10,
41 ift_float = 11,
42 ift_double = 12,
43 ift_last = 12 /* keep the same as the highest type code */
44} ifd_entry_type;
45
46static int type_sizes[] =
47 {
48 0, /* not used */
49 1, /* byte */
50 1, /* ascii */
51 2, /* short */
52 4, /* long */
53 8, /* rational */
54 1, /* sbyte */
55 1, /* undefined */
56 2, /* sshort */
57 4, /* slong */
58 8, /* srational */
59 4, /* float */
60 8, /* double */
61 };
62
63typedef struct {
64 int tag;
65 int type;
66 int count;
59957854 67 int item_size;
f7450478
TC
68 int size;
69 int offset;
70} ifd_entry;
71
72typedef struct {
73 int tag;
74 char const *name;
75} tag_map;
76
77typedef struct {
78 int tag;
79 char const *name;
80 tag_map const *map;
81 int map_count;
82} tag_value_map;
83
84#define PASTE(left, right) PASTE_(left, right)
85#define PASTE_(left, right) left##right
86#define QUOTE(value) #value
87
88#define VALUE_MAP_ENTRY(name) \
89 { \
90 PASTE(tag_, name), \
91 "exif_" QUOTE(name) "_name", \
92 PASTE(name, _values), \
93 ARRAY_COUNT(PASTE(name, _values)) \
94 }
95
96/* we don't process every tag */
97#define tag_make 271
98#define tag_model 272
99#define tag_orientation 274
100#define tag_x_resolution 282
101#define tag_y_resolution 283
102#define tag_resolution_unit 296
103#define tag_copyright 33432
104#define tag_software 305
105#define tag_artist 315
106#define tag_date_time 306
107#define tag_image_description 270
108
109#define tag_exif_ifd 34665
110#define tag_gps_ifd 34853
111
112#define resunit_none 1
113#define resunit_inch 2
114#define resunit_centimeter 3
115
116/* tags from the EXIF ifd */
117#define tag_exif_version 0x9000
118#define tag_flashpix_version 0xA000
119#define tag_color_space 0xA001
120#define tag_component_configuration 0x9101
121#define tag_component_bits_per_pixel 0x9102
122#define tag_pixel_x_dimension 0xA002
123#define tag_pixel_y_dimension 0xA003
124#define tag_maker_note 0x927C
125#define tag_user_comment 0x9286
126#define tag_related_sound_file 0xA004
127#define tag_date_time_original 0x9003
128#define tag_date_time_digitized 0x9004
129#define tag_sub_sec_time 0x9290
130#define tag_sub_sec_time_original 0x9291
131#define tag_sub_sec_time_digitized 0x9292
132#define tag_image_unique_id 0xA420
133#define tag_exposure_time 0x829a
134#define tag_f_number 0x829D
135#define tag_exposure_program 0x8822
136#define tag_spectral_sensitivity 0x8824
59957854 137#define tag_iso_speed_ratings 0x8827
f7450478
TC
138#define tag_oecf 0x8828
139#define tag_shutter_speed 0x9201
140#define tag_aperture 0x9202
141#define tag_brightness 0x9203
142#define tag_exposure_bias 0x9204
143#define tag_max_aperture 0x9205
144#define tag_subject_distance 0x9206
145#define tag_metering_mode 0x9207
146#define tag_light_source 0x9208
147#define tag_flash 0x9209
148#define tag_focal_length 0x920a
149#define tag_subject_area 0x9214
150#define tag_flash_energy 0xA20B
151#define tag_spatial_frequency_response 0xA20C
152#define tag_focal_plane_x_resolution 0xA20e
153#define tag_focal_plane_y_resolution 0xA20F
154#define tag_focal_plane_resolution_unit 0xA210
155#define tag_subject_location 0xA214
156#define tag_exposure_index 0xA215
157#define tag_sensing_method 0xA217
158#define tag_file_source 0xA300
159#define tag_scene_type 0xA301
160#define tag_cfa_pattern 0xA302
161#define tag_custom_rendered 0xA401
162#define tag_exposure_mode 0xA402
163#define tag_white_balance 0xA403
164#define tag_digital_zoom_ratio 0xA404
165#define tag_focal_length_in_35mm_film 0xA405
166#define tag_scene_capture_type 0xA406
167#define tag_gain_control 0xA407
168#define tag_contrast 0xA408
169#define tag_saturation 0xA409
170#define tag_sharpness 0xA40A
171#define tag_device_setting_description 0xA40B
172#define tag_subject_distance_range 0xA40C
173
59957854
TC
174/* GPS tags */
175#define tag_gps_version_id 0
176#define tag_gps_latitude_ref 1
177#define tag_gps_latitude 2
178#define tag_gps_longitude_ref 3
179#define tag_gps_longitude 4
180#define tag_gps_altitude_ref 5
181#define tag_gps_altitude 6
182#define tag_gps_time_stamp 7
183#define tag_gps_satellites 8
184#define tag_gps_status 9
185#define tag_gps_measure_mode 10
186#define tag_gps_dop 11
187#define tag_gps_speed_ref 12
188#define tag_gps_speed 13
189#define tag_gps_track_ref 14
190#define tag_gps_track 15
191#define tag_gps_img_direction_ref 16
192#define tag_gps_img_direction 17
193#define tag_gps_map_datum 18
194#define tag_gps_dest_latitude_ref 19
195#define tag_gps_dest_latitude 20
196#define tag_gps_dest_longitude_ref 21
197#define tag_gps_dest_longitude 22
198#define tag_gps_dest_bearing_ref 23
199#define tag_gps_dest_bearing 24
200#define tag_gps_dest_distance_ref 25
201#define tag_gps_dest_distance 26
202#define tag_gps_processing_method 27
203#define tag_gps_area_information 28
204#define tag_gps_date_stamp 29
205#define tag_gps_differential 30
206
f7450478
TC
207/* don't use this on pointers */
208#define ARRAY_COUNT(array) (sizeof(array)/sizeof(*array))
209
210/* in memory tiff structure */
211typedef struct {
212 /* the data we use as a tiff */
213 unsigned char *base;
214 size_t size;
215
216 /* intel or motorola byte order */
217 tiff_type type;
218
219 /* initial ifd offset */
220 unsigned long first_ifd_offset;
221
222 /* size (in entries) and data */
223 int ifd_size;
224 ifd_entry *ifd;
225 unsigned long next_ifd;
226} imtiff;
227
228static int tiff_init(imtiff *tiff, unsigned char *base, size_t length);
229static int tiff_load_ifd(imtiff *tiff, unsigned long offset);
230static void tiff_final(imtiff *tiff);
231static void tiff_clear_ifd(imtiff *tiff);
232static int tiff_get_bytes(imtiff *tiff, unsigned char *to, size_t offset,
233 size_t count);
234static int tiff_get_tag_double(imtiff *, int index, double *result);
235static int tiff_get_tag_int(imtiff *, int index, int *result);
236static unsigned tiff_get16(imtiff *, unsigned long offset);
237static unsigned tiff_get32(imtiff *, unsigned long offset);
238static int tiff_get16s(imtiff *, unsigned long offset);
239static int tiff_get32s(imtiff *, unsigned long offset);
240static double tiff_get_rat(imtiff *, unsigned long offset);
241static double tiff_get_rats(imtiff *, unsigned long offset);
59957854 242static void save_ifd0_tags(i_img *im, imtiff *tiff, unsigned long *exif_ifd_offset, unsigned long *gps_ifd_offset);
f7450478 243static void save_exif_ifd_tags(i_img *im, imtiff *tiff);
59957854 244static void save_gps_ifd_tags(i_img *im, imtiff *tiff);
f7450478
TC
245static void
246copy_string_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
247static void
248copy_int_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
249static void
250copy_rat_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
251static void
59957854
TC
252copy_num_array_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
253static void
f7450478
TC
254copy_name_tags(i_img *im, imtiff *tiff, tag_value_map *map, int map_count);
255static void process_maker_note(i_img *im, imtiff *tiff, unsigned long offset, size_t size);
256
257/*
258=head1 PUBLIC FUNCTIONS
259
260These functions are available to other parts of Imager. They aren't
261intended to be called from outside of Imager.
262
263=over
264
265=item i_int_decode_exit
266
267i_int_decode_exif(im, data_base, data_size);
268
269The data from data_base for data_size bytes will be scanned for EXIF
270data.
271
272Any data found will be used to set tags in the supplied image.
273
274The intent is that invalid EXIF data will simply fail to set tags, and
275write to the log. In no case should this code exit when supplied
276invalid data.
277
278Returns true if an Exif header was seen.
279
280*/
281
282int
283i_int_decode_exif(i_img *im, unsigned char *data, size_t length) {
284 imtiff tiff;
285 unsigned long exif_ifd_offset = 0;
59957854 286 unsigned long gps_ifd_offset = 0;
f7450478
TC
287 /* basic checks - must start with "Exif\0\0" */
288
289 if (length < 6 || memcmp(data, "Exif\0\0", 6) != 0) {
290 return 0;
291 }
292
293 data += 6;
294 length -= 6;
295
296 if (!tiff_init(&tiff, data, length)) {
297 mm_log((2, "Exif header found, but no valid TIFF header\n"));
298 return 1;
299 }
300 if (!tiff_load_ifd(&tiff, tiff.first_ifd_offset)) {
301 mm_log((2, "Exif header found, but could not load IFD 0\n"));
302 tiff_final(&tiff);
303 return 1;
304 }
305
59957854 306 save_ifd0_tags(im, &tiff, &exif_ifd_offset, &gps_ifd_offset);
f7450478
TC
307
308 if (exif_ifd_offset) {
309 if (tiff_load_ifd(&tiff, exif_ifd_offset)) {
310 save_exif_ifd_tags(im, &tiff);
311 }
312 else {
313 mm_log((2, "Could not load Exif IFD\n"));
314 }
315 }
316
59957854
TC
317 if (gps_ifd_offset) {
318 if (tiff_load_ifd(&tiff, gps_ifd_offset)) {
319 save_gps_ifd_tags(im, &tiff);
320 }
321 else {
322 mm_log((2, "Could not load GPS IFD\n"));
323 }
324 }
325
f7450478
TC
326 tiff_final(&tiff);
327
328 return 1;
329}
330
331/*
332
333=back
334
335=head1 INTERNAL FUNCTIONS
336
337=head2 EXIF Processing
338
339=over
340
341=item save_ifd0_tags
342
59957854 343save_ifd0_tags(im, tiff, &exif_ifd_offset, &gps_ifd_offset)
f7450478
TC
344
345Scans the currently loaded IFD for tags expected in IFD0 and sets them
346in the image.
347
348Sets *exif_ifd_offset to the offset of the EXIF IFD if found.
349
350=cut
351
352*/
353
354static tag_map ifd0_string_tags[] =
355 {
af070d99
TC
356 { tag_make, "exif_make" },
357 { tag_model, "exif_model" },
358 { tag_copyright, "exif_copyright" },
359 { tag_software, "exif_software" },
360 { tag_artist, "exif_artist" },
361 { tag_date_time, "exif_date_time" },
362 { tag_image_description, "exif_image_description" },
f7450478
TC
363 };
364
365static const int ifd0_string_tag_count = ARRAY_COUNT(ifd0_string_tags);
366
367static tag_map ifd0_int_tags[] =
368 {
369 { tag_orientation, "exif_orientation", },
370 { tag_resolution_unit, "exif_resolution_unit" },
371 };
372
373static const int ifd0_int_tag_count = ARRAY_COUNT(ifd0_int_tags);
374
375static tag_map ifd0_rat_tags[] =
376 {
377 { tag_x_resolution, "exif_x_resolution" },
378 { tag_y_resolution, "exif_y_resolution" },
379 };
380
381static tag_map resolution_unit_values[] =
382 {
383 { 1, "none" },
384 { 2, "inches" },
385 { 3, "centimeters" },
386 };
387
388static tag_value_map ifd0_values[] =
389 {
390 VALUE_MAP_ENTRY(resolution_unit),
391 };
392
393static void
59957854
TC
394save_ifd0_tags(i_img *im, imtiff *tiff, unsigned long *exif_ifd_offset,
395 unsigned long *gps_ifd_offset) {
af070d99 396 int tag_index;
f7450478
TC
397 int work;
398 ifd_entry *entry;
399
400 for (tag_index = 0, entry = tiff->ifd;
401 tag_index < tiff->ifd_size; ++tag_index, ++entry) {
402 switch (entry->tag) {
403 case tag_exif_ifd:
404 if (tiff_get_tag_int(tiff, tag_index, &work))
405 *exif_ifd_offset = work;
406 break;
59957854
TC
407
408 case tag_gps_ifd:
409 if (tiff_get_tag_int(tiff, tag_index, &work))
410 *gps_ifd_offset = work;
411 break;
f7450478
TC
412 }
413 }
414
415 copy_string_tags(im, tiff, ifd0_string_tags, ifd0_string_tag_count);
416 copy_int_tags(im, tiff, ifd0_int_tags, ifd0_int_tag_count);
417 copy_rat_tags(im, tiff, ifd0_rat_tags, ARRAY_COUNT(ifd0_rat_tags));
418 copy_name_tags(im, tiff, ifd0_values, ARRAY_COUNT(ifd0_values));
59957854 419 /* copy_num_array_tags(im, tiff, ifd0_num_arrays, ARRAY_COUNT(ifd0_num_arrays)); */
f7450478
TC
420}
421
422/*
423=item save_exif_ifd_tags
424
425save_exif_ifd_tags(im, tiff)
426
427Scans the currently loaded IFD for the tags expected in the EXIF IFD
428and sets them as tags in the image.
429
430=cut
431
432*/
433
434static tag_map exif_ifd_string_tags[] =
435 {
436 { tag_exif_version, "exif_version", },
437 { tag_flashpix_version, "exif_flashpix_version", },
438 { tag_related_sound_file, "exif_related_sound_file", },
439 { tag_date_time_original, "exif_date_time_original", },
440 { tag_date_time_digitized, "exif_date_time_digitized", },
441 { tag_sub_sec_time, "exif_sub_sec_time" },
442 { tag_sub_sec_time_original, "exif_sub_sec_time_original" },
443 { tag_sub_sec_time_digitized, "exif_sub_sec_time_digitized" },
444 { tag_image_unique_id, "exif_image_unique_id" },
445 { tag_spectral_sensitivity, "exif_spectral_sensitivity" },
446 };
447
448static const int exif_ifd_string_tag_count = ARRAY_COUNT(exif_ifd_string_tags);
449
450static tag_map exif_ifd_int_tags[] =
451 {
452 { tag_color_space, "exif_color_space" },
453 { tag_exposure_program, "exif_exposure_program" },
f7450478
TC
454 { tag_metering_mode, "exif_metering_mode" },
455 { tag_light_source, "exif_light_source" },
456 { tag_flash, "exif_flash" },
457 { tag_focal_plane_resolution_unit, "exif_focal_plane_resolution_unit" },
458 { tag_subject_location, "exif_subject_location" },
459 { tag_sensing_method, "exif_sensing_method" },
460 { tag_custom_rendered, "exif_custom_rendered" },
461 { tag_exposure_mode, "exif_exposure_mode" },
462 { tag_white_balance, "exif_white_balance" },
463 { tag_focal_length_in_35mm_film, "exif_focal_length_in_35mm_film" },
464 { tag_scene_capture_type, "exif_scene_capture_type" },
465 { tag_contrast, "exif_contrast" },
466 { tag_saturation, "exif_saturation" },
467 { tag_sharpness, "exif_sharpness" },
468 { tag_subject_distance_range, "exif_subject_distance_range" },
469 };
470
471
472static const int exif_ifd_int_tag_count = ARRAY_COUNT(exif_ifd_int_tags);
473
474static tag_map exif_ifd_rat_tags[] =
475 {
476 { tag_exposure_time, "exif_exposure_time" },
477 { tag_f_number, "exif_f_number" },
478 { tag_shutter_speed, "exif_shutter_speed" },
479 { tag_aperture, "exif_aperture" },
480 { tag_brightness, "exif_brightness" },
481 { tag_exposure_bias, "exif_exposure_bias" },
482 { tag_max_aperture, "exif_max_aperture" },
483 { tag_subject_distance, "exif_subject_distance" },
484 { tag_focal_length, "exif_focal_length" },
485 { tag_flash_energy, "exif_flash_energy" },
486 { tag_focal_plane_x_resolution, "exif_focal_plane_x_resolution" },
487 { tag_focal_plane_y_resolution, "exif_focal_plane_y_resolution" },
488 { tag_exposure_index, "exif_exposure_index" },
489 { tag_digital_zoom_ratio, "exif_digital_zoom_ratio" },
490 { tag_gain_control, "exif_gain_control" },
491 };
492
493static const int exif_ifd_rat_tag_count = ARRAY_COUNT(exif_ifd_rat_tags);
494
495static tag_map exposure_mode_values[] =
496 {
497 { 0, "Auto exposure" },
498 { 1, "Manual exposure" },
499 { 2, "Auto bracket" },
500 };
501static tag_map color_space_values[] =
502 {
503 { 1, "sRGB" },
504 { 0xFFFF, "Uncalibrated" },
505 };
506
507static tag_map exposure_program_values[] =
508 {
509 { 0, "Not defined" },
510 { 1, "Manual" },
511 { 2, "Normal program" },
512 { 3, "Aperture priority" },
513 { 4, "Shutter priority" },
514 { 5, "Creative program" },
515 { 6, "Action program" },
516 { 7, "Portrait mode" },
517 { 8, "Landscape mode" },
518 };
519
520static tag_map metering_mode_values[] =
521 {
522 { 0, "unknown" },
523 { 1, "Average" },
524 { 2, "CenterWeightedAverage" },
525 { 3, "Spot" },
526 { 4, "MultiSpot" },
527 { 5, "Pattern" },
528 { 6, "Partial" },
529 { 255, "other" },
530 };
531
532static tag_map light_source_values[] =
533 {
534 { 0, "unknown" },
535 { 1, "Daylight" },
536 { 2, "Fluorescent" },
537 { 3, "Tungsten (incandescent light)" },
538 { 4, "Flash" },
539 { 9, "Fine weather" },
540 { 10, "Cloudy weather" },
541 { 11, "Shade" },
542