File: imexif.c

package info (click to toggle)
libimager-perl 1.027%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,820 kB
  • sloc: perl: 32,971; ansic: 28,092; makefile: 52; cpp: 4
file content (1610 lines) | stat: -rw-r--r-- 39,940 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
#include "imager.h"
#include "imexif.h"
#include <stdlib.h>
#include <float.h>
#include <string.h>
#include <stdio.h>

/*
=head1 NAME

imexif.c - EXIF support for Imager

=head1 SYNOPSIS

  if (im_decode_exif(im, app1data, app1datasize)) {
    // exif block seen
  }

=head1 DESCRIPTION

This code provides a basic EXIF data decoder.  It is intended to be
called from the JPEG reader code when an APP1 data block is found, and
will set tags in the supplied image.

=cut
*/

typedef enum tiff_type_tag {
  tt_intel = 'I',
  tt_motorola = 'M'
} tiff_type;

typedef enum {
  ift_byte = 1,
  ift_ascii = 2,
  ift_short = 3,
  ift_long = 4,
  ift_rational = 5,
  ift_sbyte = 6,
  ift_undefined = 7,
  ift_sshort = 8,
  ift_slong = 9,
  ift_srational = 10,
  ift_float = 11,
  ift_double = 12,
  ift_last = 12 /* keep the same as the highest type code */
} ifd_entry_type;

static int type_sizes[] =
  {
    0, /* not used */
    1, /* byte */
    1, /* ascii */
    2, /* short */
    4, /* long */
    8, /* rational */
    1, /* sbyte */
    1, /* undefined */
    2, /* sshort */
    4, /* slong */
    8, /* srational */
    4, /* float */
    8, /* double */
  };

typedef struct {
  int tag;
  int type;
  int count;
  int item_size;
  int size;
  int offset;
} ifd_entry;

typedef struct {
  int tag;
  char const *name;
} tag_map;

typedef struct {
  int tag;
  char const *name;
  tag_map const *map;
  int map_count;
} tag_value_map;

#define PASTE(left, right) PASTE_(left, right)
#define PASTE_(left, right) left##right
#define QUOTE(value) #value

#define VALUE_MAP_ENTRY(name) \
  { \
    PASTE(tag_, name), \
    "exif_" QUOTE(name) "_name", \
    PASTE(name, _values), \
    ARRAY_COUNT(PASTE(name, _values)) \
  }

/* we don't process every tag */
#define tag_make 271
#define tag_model 272
#define tag_orientation 274
#define tag_x_resolution 282
#define tag_y_resolution 283
#define tag_resolution_unit 296
#define tag_copyright 33432
#define tag_software 305
#define tag_artist 315
#define tag_date_time 306
#define tag_image_description 270

#define tag_exif_ifd 34665
#define tag_gps_ifd 34853

#define resunit_none 1
#define resunit_inch 2
#define resunit_centimeter 3

/* tags from the EXIF ifd */
#define tag_exif_version 0x9000
#define tag_flashpix_version 0xA000
#define tag_color_space 0xA001
#define tag_component_configuration 0x9101
#define tag_component_bits_per_pixel 0x9102
#define tag_pixel_x_dimension 0xA002
#define tag_pixel_y_dimension 0xA003
#define tag_maker_note 0x927C
#define tag_user_comment 0x9286
#define tag_related_sound_file 0xA004
#define tag_date_time_original 0x9003
#define tag_date_time_digitized 0x9004
#define tag_sub_sec_time 0x9290
#define tag_sub_sec_time_original 0x9291
#define tag_sub_sec_time_digitized 0x9292
#define tag_image_unique_id 0xA420
#define tag_exposure_time 0x829a
#define tag_f_number 0x829D
#define tag_exposure_program 0x8822
#define tag_spectral_sensitivity 0x8824
#define tag_iso_speed_ratings 0x8827
#define tag_oecf 0x8828
#define tag_shutter_speed 0x9201
#define tag_aperture 0x9202
#define tag_brightness 0x9203
#define tag_exposure_bias 0x9204
#define tag_max_aperture 0x9205
#define tag_subject_distance 0x9206
#define tag_metering_mode 0x9207
#define tag_light_source 0x9208
#define tag_flash 0x9209
#define tag_focal_length 0x920a
#define tag_subject_area 0x9214
#define tag_flash_energy 0xA20B
#define tag_spatial_frequency_response 0xA20C
#define tag_focal_plane_x_resolution 0xA20e
#define tag_focal_plane_y_resolution 0xA20F
#define tag_focal_plane_resolution_unit 0xA210
#define tag_subject_location 0xA214
#define tag_exposure_index 0xA215
#define tag_sensing_method 0xA217
#define tag_file_source 0xA300
#define tag_scene_type 0xA301
#define tag_cfa_pattern 0xA302
#define tag_custom_rendered 0xA401
#define tag_exposure_mode 0xA402
#define tag_white_balance 0xA403
#define tag_digital_zoom_ratio 0xA404
#define tag_focal_length_in_35mm_film 0xA405
#define tag_scene_capture_type 0xA406
#define tag_gain_control 0xA407
#define tag_contrast 0xA408
#define tag_saturation 0xA409
#define tag_sharpness 0xA40A
#define tag_device_setting_description 0xA40B
#define tag_subject_distance_range 0xA40C

/* GPS tags */
#define tag_gps_version_id 0
#define tag_gps_latitude_ref 1
#define tag_gps_latitude 2
#define tag_gps_longitude_ref 3
#define tag_gps_longitude 4
#define tag_gps_altitude_ref 5
#define tag_gps_altitude 6
#define tag_gps_time_stamp 7
#define tag_gps_satellites 8
#define tag_gps_status 9
#define tag_gps_measure_mode 10
#define tag_gps_dop 11
#define tag_gps_speed_ref 12
#define tag_gps_speed 13
#define tag_gps_track_ref 14
#define tag_gps_track 15
#define tag_gps_img_direction_ref 16
#define tag_gps_img_direction 17
#define tag_gps_map_datum 18
#define tag_gps_dest_latitude_ref 19
#define tag_gps_dest_latitude 20
#define tag_gps_dest_longitude_ref 21
#define tag_gps_dest_longitude 22
#define tag_gps_dest_bearing_ref 23
#define tag_gps_dest_bearing 24
#define tag_gps_dest_distance_ref 25
#define tag_gps_dest_distance 26
#define tag_gps_processing_method 27
#define tag_gps_area_information 28
#define tag_gps_date_stamp 29
#define tag_gps_differential 30

/* don't use this on pointers */
#define ARRAY_COUNT(array) (sizeof(array)/sizeof(*array))

/* in memory tiff structure */
typedef struct {
  /* the data we use as a tiff */
  const unsigned char *base;
  size_t size;

  /* intel or motorola byte order */
  tiff_type type;

  /* initial ifd offset */
  unsigned long first_ifd_offset;
  
  /* size (in entries) and data */
  int ifd_size; 
  ifd_entry *ifd;
  unsigned long next_ifd;
} imtiff;

static int tiff_init(imtiff *tiff, const unsigned char *base, size_t length);
static int tiff_load_ifd(imtiff *tiff, unsigned long offset);
static void tiff_final(imtiff *tiff);
static void tiff_clear_ifd(imtiff *tiff);
#if 0 /* currently unused, but that may change */
static int tiff_get_bytes(imtiff *tiff, unsigned char *to, size_t offset, 
			  size_t count);
#endif
static int tiff_get_tag_double(imtiff *, int index, double *result);
static int tiff_get_tag_int(imtiff *, int index, int *result);
static unsigned tiff_get16(imtiff *, unsigned long offset);
static unsigned tiff_get32(imtiff *, unsigned long offset);
static int tiff_get16s(imtiff *, unsigned long offset);
static int tiff_get32s(imtiff *, unsigned long offset);
static double tiff_get_rat(imtiff *, unsigned long offset);
static double tiff_get_rats(imtiff *, unsigned long offset);
static void save_ifd0_tags(i_img *im, imtiff *tiff, unsigned long *exif_ifd_offset, unsigned long *gps_ifd_offset);
static void save_exif_ifd_tags(i_img *im, imtiff *tiff);
static void save_gps_ifd_tags(i_img *im, imtiff *tiff);
static void
copy_string_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
static void
copy_int_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
static void
copy_rat_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
static void
copy_num_array_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count);
static void
copy_name_tags(i_img *im, imtiff *tiff, tag_value_map *map, int map_count);
static void process_maker_note(i_img *im, imtiff *tiff, unsigned long offset, size_t size);

/*
=head1 PUBLIC FUNCTIONS

These functions are available to other parts of Imager.  They aren't
intended to be called from outside of Imager.

=over

=item im_decode_exif

im_decode_exif(im, data_base, data_size);

The data from C<data_base> for C<data_size> bytes will be scanned for
EXIF data.

Any data found will be used to set tags in the supplied image.

The intent is that invalid EXIF data will simply fail to set tags, and
write to the log.  In no case should this code exit when supplied
invalid data.

Returns true if an EXIF header was seen.

=cut
*/

int
im_decode_exif(i_img *im, const unsigned char *data, size_t length) {
  imtiff tiff;
  unsigned long exif_ifd_offset = 0;
  unsigned long gps_ifd_offset = 0;

  if (!tiff_init(&tiff, data, length)) {
    mm_log((2, "Exif header found, but no valid TIFF header\n"));
    return 1;
  }
  if (!tiff_load_ifd(&tiff, tiff.first_ifd_offset)) {
    mm_log((2, "Exif header found, but could not load IFD 0\n"));
    tiff_final(&tiff);
    return 1;
  }

  save_ifd0_tags(im, &tiff, &exif_ifd_offset, &gps_ifd_offset);

  if (exif_ifd_offset) {
    if (tiff_load_ifd(&tiff, exif_ifd_offset)) {
      save_exif_ifd_tags(im, &tiff);
    }
    else {
      mm_log((2, "Could not load Exif IFD\n"));
    }
  }

  if (gps_ifd_offset) {
    if (tiff_load_ifd(&tiff, gps_ifd_offset)) {
      save_gps_ifd_tags(im, &tiff);
    }
    else {
      mm_log((2, "Could not load GPS IFD\n"));
    }
  }

  tiff_final(&tiff);

  return 1;
}

/*

=back

=head1 INTERNAL FUNCTIONS

=head2 EXIF Processing 

=over

=item save_ifd0_tags

save_ifd0_tags(im, tiff, &exif_ifd_offset, &gps_ifd_offset)

Scans the currently loaded IFD for tags expected in IFD0 and sets them
in the image.

Sets *exif_ifd_offset to the offset of the EXIF IFD if found.

=cut

*/

static tag_map ifd0_string_tags[] =
  {
    { tag_make, "exif_make" },
    { tag_model, "exif_model" },
    { tag_copyright, "exif_copyright" },
    { tag_software, "exif_software" },
    { tag_artist, "exif_artist" },
    { tag_date_time, "exif_date_time" },
    { tag_image_description, "exif_image_description" },
  };

static const int ifd0_string_tag_count = ARRAY_COUNT(ifd0_string_tags);

static tag_map ifd0_int_tags[] =
  {
    { tag_orientation, "exif_orientation", },
    { tag_resolution_unit, "exif_resolution_unit" },
  };

static const int ifd0_int_tag_count = ARRAY_COUNT(ifd0_int_tags);

static tag_map ifd0_rat_tags[] =
  {
    { tag_x_resolution, "exif_x_resolution" },
    { tag_y_resolution, "exif_y_resolution" },
  };

static tag_map resolution_unit_values[] =
  {
    { 1, "none" },
    { 2, "inches" },
    { 3, "centimeters" },
  };

static tag_value_map ifd0_values[] =
  {
    VALUE_MAP_ENTRY(resolution_unit),
  };

static void
save_ifd0_tags(i_img *im, imtiff *tiff, unsigned long *exif_ifd_offset,
	       unsigned long *gps_ifd_offset) {
  int tag_index;
  int work;
  ifd_entry *entry;

  for (tag_index = 0, entry = tiff->ifd; 
       tag_index < tiff->ifd_size; ++tag_index, ++entry) {
    switch (entry->tag) {
    case tag_exif_ifd:
      if (tiff_get_tag_int(tiff, tag_index, &work))
	*exif_ifd_offset = work;
      break;

    case tag_gps_ifd:
      if (tiff_get_tag_int(tiff, tag_index, &work))
	*gps_ifd_offset = work;
      break;
    }
  }

  copy_string_tags(im, tiff, ifd0_string_tags, ifd0_string_tag_count);
  copy_int_tags(im, tiff, ifd0_int_tags, ifd0_int_tag_count);
  copy_rat_tags(im, tiff, ifd0_rat_tags, ARRAY_COUNT(ifd0_rat_tags));
  copy_name_tags(im, tiff, ifd0_values, ARRAY_COUNT(ifd0_values));
  /* copy_num_array_tags(im, tiff, ifd0_num_arrays, ARRAY_COUNT(ifd0_num_arrays)); */
}

/*
=item save_exif_ifd_tags

save_exif_ifd_tags(im, tiff)

Scans the currently loaded IFD for the tags expected in the EXIF IFD
and sets them as tags in the image.

=cut

*/

static tag_map exif_ifd_string_tags[] =
  {
    { tag_exif_version, "exif_version", },
    { tag_flashpix_version, "exif_flashpix_version", },
    { tag_related_sound_file, "exif_related_sound_file", },
    { tag_date_time_original, "exif_date_time_original", },
    { tag_date_time_digitized, "exif_date_time_digitized", },
    { tag_sub_sec_time, "exif_sub_sec_time" },
    { tag_sub_sec_time_original, "exif_sub_sec_time_original" },
    { tag_sub_sec_time_digitized, "exif_sub_sec_time_digitized" },
    { tag_image_unique_id, "exif_image_unique_id" },
    { tag_spectral_sensitivity, "exif_spectral_sensitivity" },
  };

static const int exif_ifd_string_tag_count = ARRAY_COUNT(exif_ifd_string_tags);

static tag_map exif_ifd_int_tags[] =
  {
    { tag_color_space, "exif_color_space" },
    { tag_exposure_program, "exif_exposure_program" },
    { tag_metering_mode, "exif_metering_mode" },
    { tag_light_source, "exif_light_source" },
    { tag_flash, "exif_flash" },
    { tag_focal_plane_resolution_unit, "exif_focal_plane_resolution_unit" },
    { tag_subject_location, "exif_subject_location" },
    { tag_sensing_method, "exif_sensing_method" },
    { tag_custom_rendered, "exif_custom_rendered" },
    { tag_exposure_mode, "exif_exposure_mode" },
    { tag_white_balance, "exif_white_balance" },
    { tag_focal_length_in_35mm_film, "exif_focal_length_in_35mm_film" },
    { tag_scene_capture_type, "exif_scene_capture_type" },
    { tag_contrast, "exif_contrast" },
    { tag_saturation, "exif_saturation" },
    { tag_sharpness, "exif_sharpness" },
    { tag_subject_distance_range, "exif_subject_distance_range" },
  };


static const int exif_ifd_int_tag_count = ARRAY_COUNT(exif_ifd_int_tags);

static tag_map exif_ifd_rat_tags[] =
  {
    { tag_exposure_time, "exif_exposure_time" },
    { tag_f_number, "exif_f_number" },
    { tag_shutter_speed, "exif_shutter_speed" },
    { tag_aperture, "exif_aperture" },
    { tag_brightness, "exif_brightness" },
    { tag_exposure_bias, "exif_exposure_bias" },
    { tag_max_aperture, "exif_max_aperture" },
    { tag_subject_distance, "exif_subject_distance" },
    { tag_focal_length, "exif_focal_length" },
    { tag_flash_energy, "exif_flash_energy" },
    { tag_focal_plane_x_resolution, "exif_focal_plane_x_resolution" },
    { tag_focal_plane_y_resolution, "exif_focal_plane_y_resolution" },
    { tag_exposure_index, "exif_exposure_index" },
    { tag_digital_zoom_ratio, "exif_digital_zoom_ratio" },
    { tag_gain_control, "exif_gain_control" },
  };

static const int exif_ifd_rat_tag_count = ARRAY_COUNT(exif_ifd_rat_tags);

static tag_map exposure_mode_values[] =
  {
    { 0, "Auto exposure" },
    { 1, "Manual exposure" },
    { 2, "Auto bracket" },
  };
static tag_map color_space_values[] =
  {
    { 1, "sRGB" },
    { 0xFFFF, "Uncalibrated" },
  };

static tag_map exposure_program_values[] =
  {
    { 0, "Not defined" },
    { 1, "Manual" },
    { 2, "Normal program" },
    { 3, "Aperture priority" },
    { 4, "Shutter priority" },
    { 5, "Creative program" },
    { 6, "Action program" },
    { 7, "Portrait mode" },
    { 8, "Landscape mode" },
  };

static tag_map metering_mode_values[] =
  {
    { 0, "unknown" },
    { 1, "Average" },
    { 2, "CenterWeightedAverage" },
    { 3, "Spot" },
    { 4, "MultiSpot" },
    { 5, "Pattern" },
    { 6, "Partial" },
    { 255, "other" },
  };

static tag_map light_source_values[] =
  {
    { 0, "unknown" },
    { 1, "Daylight" },
    { 2, "Fluorescent" },
    { 3, "Tungsten (incandescent light)" },
    { 4, "Flash" },
    { 9, "Fine weather" },
    { 10, "Cloudy weather" },
    { 11, "Shade" },
    { 12, "Daylight fluorescent (D 5700 \xD0 7100K)" },
    { 13, "Day white fluorescent (N 4600 \xD0 5400K)" },
    { 14, "Cool white fluorescent (W 3900 \xD0 4500K)" },
    { 15, "White fluorescent (WW 3200 \xD0 3700K)" },
    { 17, "Standard light A" },
    { 18, "Standard light B" },
    { 19, "Standard light C" },
    { 20, "D55" },
    { 21, "D65" },
    { 22, "D75" },
    { 23, "D50" },
    { 24, "ISO studio tungsten" },
    { 255, "other light source" },
  };

static tag_map flash_values[] =
  {
    { 0x0000, "Flash did not fire." },
    { 0x0001, "Flash fired." },
    { 0x0005, "Strobe return light not detected." },
    { 0x0007, "Strobe return light detected." },
    { 0x0009, "Flash fired, compulsory flash mode" },
    { 0x000D, "Flash fired, compulsory flash mode, return light not detected" },
    { 0x000F, "Flash fired, compulsory flash mode, return light detected" },
    { 0x0010, "Flash did not fire, compulsory flash mode" },
    { 0x0018, "Flash did not fire, auto mode" },
    { 0x0019, "Flash fired, auto mode" },
    { 0x001D, "Flash fired, auto mode, return light not detected" },
    { 0x001F, "Flash fired, auto mode, return light detected" },
    { 0x0020, "No flash function" },
    { 0x0041, "Flash fired, red-eye reduction mode" },
    { 0x0045, "Flash fired, red-eye reduction mode, return light not detected" },
    { 0x0047, "Flash fired, red-eye reduction mode, return light detected" },
    { 0x0049, "Flash fired, compulsory flash mode, red-eye reduction mode" },
    { 0x004D, "Flash fired, compulsory flash mode, red-eye reduction mode, return light not detected" },
    { 0x004F, "Flash fired, compulsory flash mode, red-eye reduction mode, return light detected" },
    { 0x0059, "Flash fired, auto mode, red-eye reduction mode" },
    { 0x005D, "Flash fired, auto mode, return light not detected, red-eye reduction mode" },
    { 0x005F, "Flash fired, auto mode, return light detected, red-eye reduction mode" },
  };

static tag_map sensing_method_values[] =
  {
    { 1, "Not defined" },
    { 2, "One-chip color area sensor" },
    { 3, "Two-chip color area sensor" },
    { 4, "Three-chip color area sensor" },
    { 5, "Color sequential area sensor" },
    { 7, "Trilinear sensor" },
    { 8, "Color sequential linear sensor" },
  };

static tag_map custom_rendered_values[] =
  {
    { 0, "Normal process" },
    { 1, "Custom process" },
  };

static tag_map white_balance_values[] =
  {
    { 0, "Auto white balance" },
    { 1, "Manual white balance" },
  };

static tag_map scene_capture_type_values[] =
  {
    { 0, "Standard" },
    { 1, "Landscape" },
    { 2, "Portrait" },
    { 3, "Night scene" },
  };

static tag_map gain_control_values[] =
  {
    { 0, "None" },
    { 1, "Low gain up" },
    { 2, "High gain up" },
    { 3, "Low gain down" },
    { 4, "High gain down" },
  };

static tag_map contrast_values[] =
  {
    { 0, "Normal" },
    { 1, "Soft" },
    { 2, "Hard" },
  };

static tag_map saturation_values[] =
  {
    { 0, "Normal" },
    { 1, "Low saturation" },
    { 2, "High saturation" },
  };

static tag_map sharpness_values[] =
  {
    { 0, "Normal" },
    { 1, "Soft" },
    { 2, "Hard" },
  };

static tag_map subject_distance_range_values[] =
  {
    { 0, "unknown" },
    { 1, "Macro" },
    { 2, "Close view" },
    { 3, "Distant view" },
  };

#define focal_plane_resolution_unit_values resolution_unit_values

static tag_value_map exif_ifd_values[] =
  {
    VALUE_MAP_ENTRY(exposure_mode),
    VALUE_MAP_ENTRY(color_space),
    VALUE_MAP_ENTRY(exposure_program),
    VALUE_MAP_ENTRY(metering_mode),
    VALUE_MAP_ENTRY(light_source),
    VALUE_MAP_ENTRY(flash),
    VALUE_MAP_ENTRY(sensing_method),
    VALUE_MAP_ENTRY(custom_rendered),
    VALUE_MAP_ENTRY(white_balance),
    VALUE_MAP_ENTRY(scene_capture_type),
    VALUE_MAP_ENTRY(gain_control),
    VALUE_MAP_ENTRY(contrast),
    VALUE_MAP_ENTRY(saturation),
    VALUE_MAP_ENTRY(sharpness),
    VALUE_MAP_ENTRY(subject_distance_range),
    VALUE_MAP_ENTRY(focal_plane_resolution_unit),
  };

static tag_map exif_num_arrays[] =
  {
    { tag_iso_speed_ratings, "exif_iso_speed_ratings" },
    { tag_subject_area, "exif_subject_area" },
    { tag_subject_location, "exif_subject_location" },
  };

static void
save_exif_ifd_tags(i_img *im, imtiff *tiff) {
  int i, tag_index;
  ifd_entry *entry;
  char *user_comment;
  unsigned long maker_note_offset = 0;
  size_t maker_note_size = 0;

  for (tag_index = 0, entry = tiff->ifd; 
       tag_index < tiff->ifd_size; ++tag_index, ++entry) {
    switch (entry->tag) {
    case tag_user_comment:
      /* I don't want to trash the source, so work on a copy */
      user_comment = mymalloc(entry->size);
      memcpy(user_comment, tiff->base + entry->offset, entry->size);
      /* the first 8 bytes indicate the encoding, make them into spaces
	 for better presentation */
      for (i = 0; i < entry->size && i < 8; ++i) {
	if (user_comment[i] == '\0')
	  user_comment[i] = ' ';
      }
      /* find the actual end of the string */
      while (i < entry->size && user_comment[i])
	++i;
      i_tags_set(&im->tags, "exif_user_comment", user_comment, i);
      myfree(user_comment);
      break;

    case tag_maker_note:
      maker_note_offset = entry->offset;
      maker_note_size = entry->size;
      break;

      /* the following aren't processed yet */
    case tag_oecf:
    case tag_spatial_frequency_response:
    case tag_file_source:
    case tag_scene_type:
    case tag_cfa_pattern:
    case tag_device_setting_description:
    case tag_subject_area:
      break;
    }
  }

  copy_string_tags(im, tiff, exif_ifd_string_tags, exif_ifd_string_tag_count);
  copy_int_tags(im, tiff, exif_ifd_int_tags, exif_ifd_int_tag_count);
  copy_rat_tags(im, tiff, exif_ifd_rat_tags, exif_ifd_rat_tag_count);
  copy_name_tags(im, tiff, exif_ifd_values, ARRAY_COUNT(exif_ifd_values));
  copy_num_array_tags(im, tiff, exif_num_arrays, ARRAY_COUNT(exif_num_arrays));

  /* This trashes the IFD - make sure it's done last */
  if (maker_note_offset) {
    process_maker_note(im, tiff, maker_note_offset, maker_note_size);
  }
}

static tag_map gps_ifd_string_tags[] =
  {
    { tag_gps_version_id, "exif_gps_version_id" },
    { tag_gps_latitude_ref, "exif_gps_latitude_ref" },
    { tag_gps_longitude_ref, "exif_gps_longitude_ref" },
    { tag_gps_altitude_ref, "exif_gps_altitude_ref" },
    { tag_gps_satellites, "exif_gps_satellites" },
    { tag_gps_status, "exif_gps_status" },
    { tag_gps_measure_mode, "exif_gps_measure_mode" },
    { tag_gps_speed_ref, "exif_gps_speed_ref" },
    { tag_gps_track_ref, "exif_gps_track_ref" },
  };

static tag_map gps_ifd_int_tags[] =
  {
    { tag_gps_differential, "exif_gps_differential" },
  };

static tag_map gps_ifd_rat_tags[] =
  {
    { tag_gps_altitude, "exif_gps_altitude" },
    { tag_gps_time_stamp, "exif_gps_time_stamp" },
    { tag_gps_dop, "exif_gps_dop" },
    { tag_gps_speed, "exif_gps_speed" },
    { tag_gps_track, "exif_track" }
  };

static tag_map gps_differential_values [] =
  {
    { 0, "without differential correction" },
    { 1, "Differential correction applied" },
  };

static tag_value_map gps_ifd_values[] =
  {
    VALUE_MAP_ENTRY(gps_differential),
  };

static tag_map gps_num_arrays[] =
  {
    { tag_gps_latitude, "exif_gps_latitude" },
    { tag_gps_longitude, "exif_gps_longitude" },
  };

static void
save_gps_ifd_tags(i_img *im, imtiff *tiff) {
  /* int i, tag_index; 
  int work;
  ifd_entry *entry; */

  /* for (tag_index = 0, entry = tiff->ifd; 
       tag_index < tiff->ifd_size; ++tag_index, ++entry) {
    switch (entry->tag) {
      break;
    }
    }*/

  copy_string_tags(im, tiff, gps_ifd_string_tags, 
	 ARRAY_COUNT(gps_ifd_string_tags));
  copy_int_tags(im, tiff, gps_ifd_int_tags, ARRAY_COUNT(gps_ifd_int_tags));
  copy_rat_tags(im, tiff, gps_ifd_rat_tags, ARRAY_COUNT(gps_ifd_rat_tags));
  copy_name_tags(im, tiff, gps_ifd_values, ARRAY_COUNT(gps_ifd_values));
  copy_num_array_tags(im, tiff, gps_num_arrays, ARRAY_COUNT(gps_num_arrays));
}

/*
=item process_maker_note

This is a stub for processing the maker note tag.

Maker notes aren't covered by EXIF itself and in general aren't
documented by the manufacturers.

=cut
*/

static void
process_maker_note(i_img *im, imtiff *tiff, unsigned long offset, size_t size) {
  /* this will be added in a future release */
}

/*
=back

=head2 High level TIFF functions

To avoid relying upon tifflib when we're not processing an image we
have some simple in-memory TIFF file management.

=over

=item tiff_init

imtiff tiff;
if (tiff_init(tiff, data_base, data_size)) {
  // success
}

Initialize the tiff data structure.

Scans for the byte order and version markers, and stores the offset to
the first IFD (IFD0 in EXIF) in first_ifd_offset.

=cut
*/

static int
tiff_init(imtiff *tiff, const unsigned char *data, size_t length) {
  int version;

  tiff->base = data;
  tiff->size = length;
  if (length < 8) /* well... would have to be much bigger to be useful */
    return 0;
  if (data[0] == 'M' && data[1] == 'M')
    tiff->type = tt_motorola;
  else if (data[0] == 'I' && data[1] == 'I') 
    tiff->type = tt_intel;
  else
    return 0; /* invalid header */

  version = tiff_get16(tiff, 2);
  if (version != 42)
    return 0;

  tiff->first_ifd_offset = tiff_get32(tiff, 4);
  if (tiff->first_ifd_offset > length || tiff->first_ifd_offset < 8)
    return 0;

  tiff->ifd_size = 0;
  tiff->ifd = NULL;
  tiff->next_ifd = 0;

  return 1;
}

/*
=item tiff_final

tiff_final(&tiff)

Clean up the tiff structure initialized by tiff_init()

=cut
*/

static void
tiff_final(imtiff *tiff) {
  tiff_clear_ifd(tiff);
}

/*
=item tiff_load_ifd

if (tiff_load_ifd(tiff, offset)) {
  // process the ifd
}

Loads the IFD from the given offset into the tiff objects ifd.

This can fail if the IFD extends beyond end of file, or if any data
offsets combined with their sizes, extends beyond end of file.

Returns true on success.

=cut
*/

static int
tiff_load_ifd(imtiff *tiff, unsigned long offset) {
  unsigned count;
  int ifd_size;
  ifd_entry *entries = NULL;
  int i;
  unsigned long base;

  tiff_clear_ifd(tiff);

  /* rough check count + 1 entry + next offset */
  if (offset + (2+12+4) > tiff->size) {
    mm_log((2, "offset %lu beyond end off Exif block", offset));
    return 0;
  }

  count = tiff_get16(tiff, offset);
  
  /* check we can fit the whole thing */
  ifd_size = 2 + count * 12 + 4; /* count + count entries + next offset */
  if (offset + ifd_size > tiff->size) {
    mm_log((2, "offset %lu beyond end off Exif block", offset));
    return 0;
  }

  entries = mymalloc(count * sizeof(ifd_entry));
  memset(entries, 0, count * sizeof(ifd_entry));
  base = offset + 2;
  for (i = 0; i < count; ++i) {
    ifd_entry *entry = entries + i;
    entry->tag = tiff_get16(tiff, base);
    entry->type = tiff_get16(tiff, base+2);
    entry->count = tiff_get32(tiff, base+4);
    if (entry->type >= 1 && entry->type <= ift_last) {
      entry->item_size = type_sizes[entry->type];
      entry->size = entry->item_size * entry->count;
      if (entry->size / entry->item_size != entry->count) {
	myfree(entries);
	mm_log((1, "Integer overflow calculating tag data size processing EXIF block\n"));
	return 0;
      }
      else if (entry->size <= 4) {
	entry->offset = base + 8;
      }
      else {
	entry->offset = tiff_get32(tiff, base+8);
	if (entry->offset + entry->size > tiff->size) {
	  mm_log((2, "Invalid data offset processing IFD\n"));
	  myfree(entries);
	  return 0;
	}
      }
    }
    else {
      entry->size = 0;
      entry->offset = 0;
    }
    base += 12;
  }

  tiff->ifd_size = count;
  tiff->ifd = entries;
  tiff->next_ifd = tiff_get32(tiff, base);

  return 1;
}

/*
=item tiff_clear_ifd

tiff_clear_ifd(tiff)

Releases any memory associated with the stored IFD and resets the IFD
pointers.

This is called by tiff_load_ifd() and tiff_final().

=cut
*/

static void
tiff_clear_ifd(imtiff *tiff) {
  if (tiff->ifd_size && tiff->ifd) {
    myfree(tiff->ifd);
    tiff->ifd_size = 0;
    tiff->ifd = NULL;
  }
}

/*
=item tiff_get_tag_double

  double value;
  if (tiff_get_tag(tiff, index, &value)) {
    // process value
  }

Attempts to retrieve a double value from the given index in the
current IFD.

The value must have a count of 1.

=cut
*/

static int
tiff_get_tag_double_array(imtiff *tiff, int index, double *result, 
			  int array_index) {
  ifd_entry *entry;
  unsigned long offset;
  if (index < 0 || index >= tiff->ifd_size) {
    mm_log((3, "tiff_get_tag_double_array() tag index out of range"));
    return 0;
  }
  
  entry = tiff->ifd + index;
  if (array_index < 0 || array_index >= entry->count) {
    mm_log((3, "tiff_get_tag_double_array() array index out of range"));
    return 0;
  }

  offset = entry->offset + array_index * entry->item_size;

  switch (entry->type) {
  case ift_short:
    *result = tiff_get16(tiff, offset);
    return 1;
   
  case ift_long:
    *result = tiff_get32(tiff, offset);
    return 1;

  case ift_rational:
    *result = tiff_get_rat(tiff, offset);
    return 1;

  case ift_sshort:
    *result = tiff_get16s(tiff, offset);
    return 1;

  case ift_slong:
    *result = tiff_get32s(tiff, offset);
    return 1;

  case ift_srational:
    *result = tiff_get_rats(tiff, offset);
    return 1;

  case ift_byte:
    *result = *(tiff->base + offset);
    return 1;
  }

  return 0;
}

/*
=item tiff_get_tag_double

  double value;
  if (tiff_get_tag(tiff, index, &value)) {
    // process value
  }

Attempts to retrieve a double value from the given index in the
current IFD.

The value must have a count of 1.

=cut
*/

static int
tiff_get_tag_double(imtiff *tiff, int index, double *result) {
  ifd_entry *entry;
  if (index < 0 || index >= tiff->ifd_size) {
    mm_log((3, "tiff_get_tag_double() index out of range"));
    return 0;
  }
  
  entry = tiff->ifd + index;
  if (entry->count != 1) {
    mm_log((3, "tiff_get_tag_double() called on tag with multiple values"));
    return 0;
  }

  return tiff_get_tag_double_array(tiff, index, result, 0);
}

/*
=item tiff_get_tag_int_array

  int value;
  if (tiff_get_tag_int_array(tiff, index, &value, array_index)) {
    // process value
  }

Attempts to retrieve an integer value from the given index in the
current IFD.

=cut
*/

static int
tiff_get_tag_int_array(imtiff *tiff, int index, int *result, int array_index) {
  ifd_entry *entry;
  unsigned long offset;
  if (index < 0 || index >= tiff->ifd_size) {
    mm_log((3, "tiff_get_tag_int_array() tag index out of range"));
    return 0;
  }
  
  entry = tiff->ifd + index;
  if (array_index < 0 || array_index >= entry->count) {
    mm_log((3, "tiff_get_tag_int_array() array index out of range"));
    return 0;
  }

  offset = entry->offset + array_index * entry->item_size;

  switch (entry->type) {
  case ift_short:
    *result = tiff_get16(tiff, offset);
    return 1;
   
  case ift_long:
    *result = tiff_get32(tiff, offset);
    return 1;

  case ift_sshort:
    *result = tiff_get16s(tiff, offset);
    return 1;

  case ift_slong:
    *result = tiff_get32s(tiff, offset);
    return 1;

  case ift_byte:
    *result = *(tiff->base + offset);
    return 1;
  }

  return 0;
}

/*
=item tiff_get_tag_int

  int value;
  if (tiff_get_tag_int(tiff, index, &value)) {
    // process value
  }

Attempts to retrieve an integer value from the given index in the
current IFD.

The value must have a count of 1.

=cut
*/

static int
tiff_get_tag_int(imtiff *tiff, int index, int *result) {
  ifd_entry *entry;
  if (index < 0 || index >= tiff->ifd_size) {
    mm_log((3, "tiff_get_tag_int() index out of range"));
    return 0;
  }

  entry = tiff->ifd + index;
  if (entry->count != 1) {
    mm_log((3, "tiff_get_tag_int() called on tag with multiple values"));
    return 0;
  }

  return tiff_get_tag_int_array(tiff, index, result, 0);
}

/*
=back

=head2 Table-based tag setters

This set of functions checks for matches between the current IFD and
tags supplied in an array, when there's a match it sets the
appropriate tag in the image.

=over

=item copy_int_tags

Scans the IFD for integer tags and sets them in the image,

=cut
*/

static void
copy_int_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count) {
  int i, tag_index;
  ifd_entry *entry;

  for (tag_index = 0, entry = tiff->ifd; 
       tag_index < tiff->ifd_size; ++tag_index, ++entry) {
    for (i = 0; i < map_count; ++i) {
      int value;
      if (map[i].tag == entry->tag
	  && tiff_get_tag_int(tiff, tag_index, &value)) {
	i_tags_setn(&im->tags, map[i].name, value);
	break;
      }
    }
  }
}

/*
=item copy_rat_tags

Scans the IFD for rational tags and sets them in the image.

=cut
*/

static void
copy_rat_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count) {
  int i, tag_index;
  ifd_entry *entry;

  for (tag_index = 0, entry = tiff->ifd; 
       tag_index < tiff->ifd_size; ++tag_index, ++entry) {
    for (i = 0; i < map_count; ++i) {
      double value;
      if (map[i].tag == entry->tag
	  && tiff_get_tag_double(tiff, tag_index, &value)) {
	i_tags_set_float2(&im->tags, map[i].name, 0, value, 6);
	break;
      }
    }
  }
}

/*
=item copy_string_tags

Scans the IFD for string tags and sets them in the image.

=cut
*/

static void
copy_string_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count) {
  int i, tag_index;
  ifd_entry *entry;

  for (tag_index = 0, entry = tiff->ifd; 
       tag_index < tiff->ifd_size; ++tag_index, ++entry) {
    for (i = 0; i < map_count; ++i) {
      if (map[i].tag == entry->tag) {
	int len = entry->type == ift_ascii ? entry->size - 1 : entry->size;
	i_tags_set(&im->tags, map[i].name,
		   (char const *)(tiff->base + entry->offset), len);
	break;
      }
    }
  }
}

/*
=item copy_num_array_tags

Scans the IFD for arrays of numbers and sets them in the image.

=cut
*/

/* a more general solution would be better in some ways, but we don't need it */
#define MAX_ARRAY_VALUES 10
#define MAX_ARRAY_STRING (MAX_ARRAY_VALUES * 20)

static void
copy_num_array_tags(i_img *im, imtiff *tiff, tag_map *map, int map_count) {
  int i, j, tag_index;
  ifd_entry *entry;

  for (tag_index = 0, entry = tiff->ifd; 
       tag_index < tiff->ifd_size; ++tag_index, ++entry) {
    for (i = 0; i < map_count; ++i) {
      if (map[i].tag == entry->tag && entry->count <= MAX_ARRAY_VALUES) {
	if (entry->type == ift_rational || entry->type == ift_srational) {
	  double value;
	  char workstr[MAX_ARRAY_STRING];
	  size_t len = 0, item_len;
	  *workstr = '\0';
	  for (j = 0; j < entry->count; ++j) {
	    if (!tiff_get_tag_double_array(tiff, tag_index, &value, j)) {
	      mm_log((3, "unexpected failure from tiff_get_tag_double_array(..., %d, ..., %d)\n", tag_index, j));
	      return;
	    }
	    if (len >= sizeof(workstr) - 1) {
	      mm_log((3, "Buffer would overflow reading tag %#x\n", entry->tag));
	      return;
	    }
	    if (j) {
	      strcat(workstr, " ");
	      ++len;
	    }
#ifdef IMAGER_SNPRINTF
	    item_len = snprintf(workstr + len, sizeof(workstr)-len, "%.6g", value);
#else
	    item_len = sprintf(workstr + len, "%.6g", value);
#endif
	    len += item_len;
	  }
	  i_tags_set(&im->tags, map[i].name, workstr, -1);
	}
	else if (entry->type == ift_short || entry->type == ift_long
		 || entry->type == ift_sshort || entry->type == ift_slong
		 || entry->type == ift_byte) {
	  int value;
	  char workstr[MAX_ARRAY_STRING];
	  size_t len = 0, item_len;
	  *workstr = '\0';
	  for (j = 0; j < entry->count; ++j) {
	    if (!tiff_get_tag_int_array(tiff, tag_index, &value, j)) {
	      mm_log((3, "unexpected failure from tiff_get_tag_int_array(..., %d, ..., %d)\n", tag_index, j));
	      return;
	    }
	    if (len >= sizeof(workstr) - 1) {
	      mm_log((3, "Buffer would overflow reading tag %#x\n", entry->tag));
	      return;
	    }
	    if (j) {
	      strcat(workstr, " ");
	      ++len;
	    }
#ifdef IMAGER_SNPRINTF
	    item_len = snprintf(workstr + len, sizeof(workstr) - len, "%d", value);
#else
	    item_len = sprintf(workstr + len, "%d", value);
#endif
	    len += item_len;
	  }
	  i_tags_set(&im->tags, map[i].name, workstr, -1);
	}
	break;
      }
    }
  }
}

/*
=item copy_name_tags

This function maps integer values to descriptions for those values.

In general we handle the integer value through copy_int_tags() and
then the same tage with a "_name" suffix here.

=cut
*/

static void
copy_name_tags(i_img *im, imtiff *tiff, tag_value_map *map, int map_count) {
  int i, j, tag_index;
  ifd_entry *entry;

  for (tag_index = 0, entry = tiff->ifd; 
       tag_index < tiff->ifd_size; ++tag_index, ++entry) {
    for (i = 0; i < map_count; ++i) {
      int value;
      if (map[i].tag == entry->tag
	  && tiff_get_tag_int(tiff, tag_index, &value)) {
	tag_map const *found = NULL;
	for (j = 0; j < map[i].map_count; ++j) {
	  if (value == map[i].map[j].tag) {
	    found = map[i].map + j;
	    break;
	  }
	}
	if (found) {
	  i_tags_set(&im->tags, map[i].name, found->name, -1);
	}
	break;
      }
    }
  }
}


/*
=back

=head2 Low level data access functions

These functions use the byte order in the tiff object to extract
various types of data from the tiff data.

These functions will abort if called with an out of range offset.

The intent is that any offset checks should have been done by the caller.

=over

=item tiff_get16

Retrieve a 16 bit unsigned integer from offset.

=cut
*/

static unsigned
tiff_get16(imtiff *tiff, unsigned long offset) {
  if (offset + 2 > tiff->size) {
    mm_log((3, "attempt to get16 at %lu in %lu image", offset,
	    (unsigned long)tiff->size));
    return 0;
  }

  if (tiff->type == tt_intel) 
    return tiff->base[offset] + 0x100 * tiff->base[offset+1];
  else
    return tiff->base[offset+1] + 0x100 * tiff->base[offset];
}

/*
=item tiff_get32

Retrieve a 32-bit unsigned integer from offset.

=cut
*/

static unsigned
tiff_get32(imtiff *tiff, unsigned long offset) {
  if (offset + 4 > tiff->size) {
    mm_log((3, "attempt to get16 at %lu in %lu image", offset,
	    (unsigned long)tiff->size));
    return 0;
  }

  if (tiff->type == tt_intel) 
    return tiff->base[offset] + 0x100 * tiff->base[offset+1] 
      + 0x10000 * tiff->base[offset+2] + 0x1000000 * tiff->base[offset+3];
  else
    return tiff->base[offset+3] + 0x100 * tiff->base[offset+2]
      + 0x10000 * tiff->base[offset+1] + 0x1000000 * tiff->base[offset];
}

#if 0 /* currently unused, but that may change */

/*
=item tiff_get_bytes

Retrieve a byte string from offset.

This isn't used much, you can usually deal with the data in-situ.
This is intended for use when you need to modify the data in some way.

=cut
*/

static int
tiff_get_bytes(imtiff *tiff, unsigned char *data, size_t offset, 
	       size_t size) {
  if (offset + size > tiff->size)
    return 0;

  memcpy(data, tiff->base+offset, size);

  return 1;
}

#endif

/*
=item tiff_get16s

Retrieve a 16-bit signed integer from offset.

=cut
*/

static int
tiff_get16s(imtiff *tiff, unsigned long offset) {
  int result;

  if (offset + 2 > tiff->size) {
    mm_log((3, "attempt to get16 at %lu in %lu image", offset,
	    (unsigned long)tiff->size));
    return 0;
  }

  if (tiff->type == tt_intel) 
    result = tiff->base[offset] + 0x100 * tiff->base[offset+1];
  else
    result = tiff->base[offset+1] + 0x100 * tiff->base[offset];

  if (result > 0x7FFF)
    result -= 0x10000;

  return result;
}

/*
=item tiff_get32s

Retrieve a 32-bit signed integer from offset.

=cut
*/

static int
tiff_get32s(imtiff *tiff, unsigned long offset) {
  unsigned work;

  if (offset + 4 > tiff->size) {
    mm_log((3, "attempt to get16 at %lu in %lu image", offset,
	    (unsigned long)tiff->size));
    return 0;
  }

  if (tiff->type == tt_intel) 
    work = tiff->base[offset] + 0x100 * tiff->base[offset+1] 
      + 0x10000 * tiff->base[offset+2] + 0x1000000 * tiff->base[offset+3];
  else
    work = tiff->base[offset+3] + 0x100 * tiff->base[offset+2]
      + 0x10000 * tiff->base[offset+1] + 0x1000000 * tiff->base[offset];

  /* not really needed on 32-bit int machines */
  if (work > 0x7FFFFFFFUL)
    return work - 0x80000000UL;
  else
    return work;
}

/*
=item tiff_get_rat

Retrieve an unsigned rational from offset.

=cut
*/

static double
tiff_get_rat(imtiff *tiff, unsigned long offset) {
  unsigned long numer, denom;
  if (offset + 8 > tiff->size) {
    mm_log((3, "attempt to get_rat at %lu in %lu image", offset,
	    (unsigned long)tiff->size));
    return 0;
  }

  numer = tiff_get32(tiff, offset);
  denom = tiff_get32(tiff, offset+4);

  if (denom == 0) {
    return -DBL_MAX;
  }

  return (double)numer / denom;
}

/*
=item tiff_get_rats

Retrieve an signed rational from offset.

=cut
*/

static double
tiff_get_rats(imtiff *tiff, unsigned long offset) {
  long numer, denom;
  if (offset + 8 > tiff->size) {
    mm_log((3, "attempt to get_rat at %lu in %lu image", offset,
	    (unsigned long)tiff->size));
    return 0;
  }

  numer = tiff_get32s(tiff, offset);
  denom = tiff_get32s(tiff, offset+4);

  if (denom == 0) {
    return -DBL_MAX;
  }

  return (double)numer / denom;
}

/*
=back

=head1 SEE ALSO

L<Imager>, jpeg.c

http://www.exif.org/

=head1 AUTHOR

Tony Cook <tonyc@cpan.org>

=head1 REVISION

$Revision$

=cut
*/