7e467044b27b7a7e7dd4147baa3c5f27d983ebf3
[geeqie.git] / src / format_canon.c
1 /*
2  *  GQView
3  *  (C) 2005 John Ellis
4  *
5  * This software is released under the GNU General Public License (GNU GPL).
6  * Please read the included file COPYING for more information.
7  * This software comes with no warranty of any kind, use at your own risk!
8  *
9  *
10  * Code to add support for Canon CR2 and CRW files, version 0.2
11  *
12  * Developed by Daniel M. German, dmgerman at uvic.ca
13  *
14  * you can find the sources for this patch at http://turingmachine.org/~dmg/libdcraw/gqview/
15  *
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #  include "config.h"
20 #endif
21
22 #ifndef HAVE_EXIV2
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include <glib.h>
29
30 #include "intl.h"
31
32 #include "main.h"
33 #include "format_canon.h"
34 #include "format_raw.h"
35
36 #include "exif.h"
37
38
39 /*
40  *-----------------------------------------------------------------------------
41  * Raw (CR2, CRW) embedded jpeg extraction for Canon
42  *-----------------------------------------------------------------------------
43  */
44
45 static gint canon_cr2_tiff_entry(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
46                                  guint *image_offset, gint *jpeg_encoding)
47 {
48         guint tag;
49         guint type;
50         guint count;
51         guint jpeg_start;
52
53         /* the two (tiff compliant) tags we want are:
54          *  0x0103 image compression type (must be type 6 for jpeg)
55          *  0x0111 jpeg start offset
56          * only use the first segment that contains an actual jpeg - as there
57          * is a another that contains the raw data.
58          */
59         tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
60         type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
61         count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
62
63         /* tag 0x0103 contains the compression type for this segment's image data */
64         if (tag == 0x0103)
65                 {
66                 if (ExifFormatList[type].size * count == 2 &&
67                     exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_DATA, bo) == 6)
68                         {
69                         *jpeg_encoding = TRUE;
70                         }
71                 return FALSE;
72                 }
73
74         /* find and verify jpeg offset */
75         if (tag != 0x0111 ||
76             !jpeg_encoding) return FALSE;
77
78         /* make sure data segment contains 4 bytes */
79         if (ExifFormatList[type].size * count != 4) return FALSE;
80
81         jpeg_start = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
82
83         /* verify this is jpeg data */
84         if (len < jpeg_start + 4 ||
85             memcmp(data + jpeg_start, "\xff\xd8", 2) != 0)
86                 {
87                 return FALSE;
88                 }
89
90         *image_offset = jpeg_start;
91         return TRUE;
92 }
93
94 static gint canon_cr2_tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
95                                  guint *image_offset)
96 {
97         gint jpeg_encoding = FALSE;
98         guint count;
99         guint i;
100
101         if (len < offset + 2) return 0;
102
103         count = exif_byte_get_int16(data + offset, bo);
104         offset += 2;
105         if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
106
107         for (i = 0; i < count; i++)
108                 {
109                 if (canon_cr2_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo,
110                                          image_offset, &jpeg_encoding))
111                         {
112                         return 0;
113                         }
114                 }
115
116         return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
117 }
118
119 gint format_canon_raw_cr2(unsigned char *data, const guint len,
120                           guint *image_offset, guint *exif_offset)
121 {
122         guint jpeg_offset = 0;
123         ExifByteOrder bo;
124         guint offset;
125         gint level;
126
127         /* cr2 files are tiff files with a few canon specific directory tags
128          * they are (always ?) in little endian format
129          */
130         if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
131
132         level = 0;
133         while (offset && level < EXIF_TIFF_MAX_LEVELS)
134                 {
135                 offset = canon_cr2_tiff_table(data, len, offset, bo, &jpeg_offset);
136                 level++;
137
138                 if (jpeg_offset != 0)
139                         {
140                         if (image_offset) *image_offset = jpeg_offset;
141                         return TRUE;
142                         }
143                 }
144
145         return FALSE;
146 }
147
148 #define CRW_BYTE_ORDER          EXIF_BYTE_ORDER_INTEL
149 #define CRW_HEADER_SIZE         26
150 #define CRW_DIR_ENTRY_SIZE      10
151
152 gint format_canon_raw_crw(unsigned char *data, const guint len,
153                           guint *image_offset, guint *exif_offset)
154 {
155         guint block_offset;
156         guint data_length;
157         guint offset;
158         guint count;
159         guint i;
160
161         /* CRW header starts with 2 bytes for byte order (always "II", little endian),
162          * 4 bytes for start of root block,
163          * and 8 bytes of magic for file type and format "HEAPCCDR"
164          * (also 4 bytes for file version, and 8 bytes reserved)
165          *
166          * CIFF specification in pdf format is available on some websites,
167          * search for "CIFFspecV1R03.pdf" or "CIFFspecV1R04.pdf"
168          */
169         if (len < CRW_HEADER_SIZE ||
170             memcmp(data, "II", 2) != 0 ||
171             memcmp(data + 6, "HEAPCCDR", 8) != 0)
172                 {
173                 return FALSE;
174                 }
175
176         block_offset = exif_byte_get_int32(data + 2, CRW_BYTE_ORDER);
177
178         /* the end of the root block equals end of file,
179          * the last 4 bytes of the root block contain the block's data size
180          */
181         offset = len - 4;
182         data_length = exif_byte_get_int32(data + offset, CRW_BYTE_ORDER);
183
184         offset = block_offset + data_length;
185         if (len < offset + 2) return FALSE;
186
187         /* number of directory entries for this block is in
188          * the next two bytes after the data for this block.
189          */
190         count = exif_byte_get_int16(data + offset, CRW_BYTE_ORDER);
191         offset += 2;
192         if (len < offset + count * CRW_DIR_ENTRY_SIZE + 4) return FALSE;
193
194         /* walk the directory entries looking for type jpeg (tag 0x2007),
195          * for reference, other tags are 0x2005 for raw and 0x300a for photo info:
196          */
197         for (i = 0; i < count ; i++)
198                 {
199                 guint entry_offset;
200                 guint record_type;
201                 guint record_offset;
202                 guint record_length;
203
204                 entry_offset = offset + i * CRW_DIR_ENTRY_SIZE;
205
206                 /* entry is 10 bytes (in order):
207                  *  2 for type
208                  *  4 for length of data
209                  *  4 for offset into data segment of this block
210                  */
211                 record_type = exif_byte_get_int16(data + entry_offset, CRW_BYTE_ORDER);
212                 record_length = exif_byte_get_int32(data + entry_offset + 2, CRW_BYTE_ORDER);
213                 record_offset = exif_byte_get_int32(data + entry_offset + 6, CRW_BYTE_ORDER);
214
215                 /* tag we want for jpeg data */
216                 if (record_type == 0x2007)
217                         {
218                         guint jpeg_offset;
219
220                         jpeg_offset = block_offset + record_offset;
221                         if (len < jpeg_offset + record_length ||
222                             record_length < 4 ||
223                             memcmp(data + jpeg_offset, "\xff\xd8\xff\xdb", 4) != 0)
224                                 {
225                                 return FALSE;
226                                 }
227
228                         /* we now know offset and verified jpeg */
229                         *image_offset = jpeg_offset;
230                         return TRUE;
231                         }
232                 }
233
234         return FALSE;
235 }
236
237
238 /*
239  *-----------------------------------------------------------------------------
240  * EXIF Makernote for Canon
241  *-----------------------------------------------------------------------------
242  */
243
244 static ExifTextList CanonSet1MacroMode[] = {
245         { 1,    "macro" },
246         { 2,    "normal" },
247         EXIF_TEXT_LIST_END
248 };
249
250 static ExifTextList CanonSet1Quality[] = {
251         { 2,    "normal" },
252         { 3,    "fine" },
253         { 4,    "raw" },
254         { 5,    "superfine" },
255         EXIF_TEXT_LIST_END
256 };
257
258 static ExifTextList CanonSet1FlashMode[] = {
259         { 0,    "flash not fired" },
260         { 1,    "auto" },
261         { 2,    "on" },
262         { 3,    "red-eye reduction" },
263         { 4,    "slow sync" },
264         { 5,    "auto + red-eye reduction" },
265         { 6,    "on + red-eye reduction" },
266         { 16,   "external flash" },
267         EXIF_TEXT_LIST_END
268 };
269
270 static ExifTextList CanonSet1DriveMode[] = {
271         { 0,    "single or timer" },
272         { 1,    "continuous" },
273         EXIF_TEXT_LIST_END
274 };
275
276 static ExifTextList CanonSet1FocusMode[] = {
277         { 0,    "one-shot AF" },
278         { 1,    "AI servo AF" },
279         { 2,    "AI focus AF" },
280         { 3,    "manual" },
281         { 4,    "single" },
282         { 5,    "continuous" },
283         { 6,    "manual" },
284         EXIF_TEXT_LIST_END
285 };
286
287 static ExifTextList CanonSet1ImageSize[] = {
288         { 0,    "large" },
289         { 1,    "medium" },
290         { 2,    "small" },
291         /* where (or) does Medium 1/2 fit in here ? */
292         EXIF_TEXT_LIST_END
293 };
294
295 static ExifTextList CanonSet1ShootingMode[] = {
296         { 0,    "auto" },
297         { 1,    "manual" },
298         { 2,    "landscape" },
299         { 3,    "fast shutter" },
300         { 4,    "slow shutter" },
301         { 5,    "night" },
302         { 6,    "black and white" },
303         { 7,    "sepia" },
304         { 8,    "portrait" },
305         { 9,    "sports" },
306         { 10,   "macro" },
307         { 11,   "pan focus" },
308         EXIF_TEXT_LIST_END
309 };
310
311 /* Don't think this is interpreted correctly/completely, A60 at 2.5x Digital sets value of 3 */
312 static ExifTextList CanonSet1DigitalZoom[] = {
313         { 0,    "none" },
314         { 1,    "2x" },
315         { 2,    "4x" },
316         { 3,    "other" },
317         EXIF_TEXT_LIST_END
318 };
319
320 static ExifTextList CanonSet1ConSatSharp[] = {
321         { 0,    "normal" },
322         { 1,    "high" },
323         { 65535,"low" },
324         EXIF_TEXT_LIST_END
325 };
326
327 static ExifTextList CanonSet1ISOSpeed[] = {
328 /*      { 0,    "not set/see EXIF tag" }, */
329         { 15,   "auto" },
330         { 16,   "50" },
331         { 17,   "100" },
332         { 18,   "200" },
333         { 19,   "400" },
334         EXIF_TEXT_LIST_END
335 };
336
337 static ExifTextList CanonSet1MeteringMode[] = {
338         { 0,    "default" },
339         { 1,    "spot" },
340         { 3,    "evaluative" },
341         { 4,    "partial" },
342         { 5,    "center-weighted" },
343         EXIF_TEXT_LIST_END
344 };
345
346 static ExifTextList CanonSet1FocusType[] = {
347         { 0,    "manual" },
348         { 1,    "auto" },
349         { 2,    "auto" },
350         { 3,    "macro" },
351         { 7,    "infinity" },
352         { 8,    "locked" },
353         EXIF_TEXT_LIST_END
354 };
355
356 static ExifTextList CanonSet1AutoFocusPoint[] = {
357         { 0x2005,       "manual AF point selection" },
358         { 0x3000,       "manual focus" },
359         { 0x3001,       "auto" },
360         { 0x3002,       "right" },
361         { 0x3003,       "center" },
362         { 0x3004,       "left" },
363         { 0x4001,       "auto AF point selection" },
364         EXIF_TEXT_LIST_END
365 };
366
367 static ExifTextList CanonSet1ExposureMode[] = {
368         { 0,    "auto" },
369         { 1,    "program" },
370         { 2,    "Tv priority" },
371         { 3,    "Av priority" },
372         { 4,    "manual" },
373         { 5,    "A-DEP" },
374         EXIF_TEXT_LIST_END
375 };
376
377 static ExifTextList CanonSet1FlashFired[] = {
378         { 0,    "no" },
379         { 1,    "yes" },
380         EXIF_TEXT_LIST_END
381 };
382
383 static ExifTextList CanonSet1FocusCont[] = {
384         { 0,    "no (single)" },
385         { 1,    "yes" },
386         EXIF_TEXT_LIST_END
387 };
388
389 static ExifMarker CanonSet1[] = {
390 /* 0 is length of array in bytes (2 x array size) */
391 { 1,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MacroMode",   "Macro mode",           CanonSet1MacroMode },
392 { 2,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SelfTimer",   "Self timer (10ths of second)", NULL },
393 { 3,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Quality",     "Quality",              CanonSet1Quality },
394 { 4,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashMode",   "Flash mode",           CanonSet1FlashMode },
395 { 5,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DriveMode",   "Drive mode",           CanonSet1DriveMode },
396 { 7,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusMode",   "Focus mode",           CanonSet1FocusMode },
397 { 10,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ImageSize",   "Image size",           CanonSet1ImageSize },
398 { 11,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ShootingMode","Shooting mode",        CanonSet1ShootingMode },
399  { 11,  EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureProgram",       "ExposureProgram",      CanonSet1ShootingMode },
400 { 12,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DigitalZoom", "Digital zoom",         CanonSet1DigitalZoom },
401 { 13,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Contrast",    "Contrast",             CanonSet1ConSatSharp },
402 { 14,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Saturation",  "Saturation",           CanonSet1ConSatSharp },
403 { 15,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Sharpness",   "Sharpness",            CanonSet1ConSatSharp },
404 { 16,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ISOSpeed",    "ISO speed",            CanonSet1ISOSpeed },
405  { 16,  EXIF_FORMAT_SHORT_UNSIGNED, 1, "ISOSpeedRatings",       "ISO speed",            CanonSet1ISOSpeed },
406 { 17,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MeteringMode","Metering mode",        CanonSet1MeteringMode },
407 { 18,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusType",   "Focus type",           CanonSet1FocusType },
408 { 19,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AutoFocus",   "AutoFocus point",      CanonSet1AutoFocusPoint },
409 { 20,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ExposureMode","Exposure mode",        CanonSet1ExposureMode },
410  { 20,  EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureMode",          "Exposure mode",        CanonSet1ExposureMode },
411 { 23,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthLong","Long focal length", NULL },
412 { 24,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthShort","Short focal length", NULL },
413 { 25,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthUnits","Focal units per mm", NULL },
414 { 28,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashFired",  "Flash fired",          CanonSet1FlashFired },
415 { 29,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashDetails","Flash details",        NULL },
416 { 32,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ContinuousFocus","Continuous focus",  CanonSet1FocusCont },
417 EXIF_MARKER_LIST_END
418 };
419
420 static ExifTextList CanonSet2WhiteBalance[] = {
421         { 0,    "auto" },
422         { 1,    "sunny" },
423         { 2,    "cloudy" },
424         { 3,    "tungsten" },
425         { 4,    "fluorescent" },
426         { 5,    "flash" },
427         { 6,    "custom" },
428         { 7,    "black and white" },
429         { 8,    "shade" },
430         { 9,    "manual" },
431         { 14,   "daylight fluorescent" },
432         { 17,   "underwater" },
433         EXIF_TEXT_LIST_END
434 };
435
436 static ExifTextList CanonSet2FlashBias[] = {
437         { 0x0000,       "0" },
438         { 0x000c,       "0.33" },
439         { 0x0010,       "0.5" },
440         { 0x0014,       "0.67" },
441         { 0x0020,       "1" },
442         { 0x002c,       "1.33" },
443         { 0x0030,       "1.5" },
444         { 0x0034,       "1.67" },
445         { 0x0040,       "2" },
446         { 0xffc0,       "-2" },
447         { 0xffcc,       "-1.67" },
448         { 0xffd0,       "-1.5" },
449         { 0xffd4,       "-1.33" },
450         { 0xffe0,       "-1" },
451         { 0xffec,       "-0.67" },
452         { 0xfff0,       "-0.5" },
453         { 0xfff4,       "-0.33" },
454         EXIF_TEXT_LIST_END
455 };
456
457 static ExifMarker CanonSet2[] = {
458 /* 0 is length of array in bytes (2 x array size) */
459 { 7,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.WhiteBalance","White balance",        CanonSet2WhiteBalance },
460  { 7,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "LightSource",           "White balance",        CanonSet2WhiteBalance },
461 { 9,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SequenceNumber","Sequence number",    NULL },
462 { 15,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashBias",   "Flash bias",           CanonSet2FlashBias },
463 /* distance needs more than just this (metric) value */
464 { 19,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SubjectDistance",     "Subject Distance", NULL },
465 EXIF_MARKER_LIST_END
466 };
467
468 #if 0
469
470 static ExifTextList CanonCustomEnable[] = {
471         { 0,    "off" },
472         { 1,    "on" },
473         EXIF_TEXT_LIST_END
474 };
475
476 static ExifTextList CanonCustomEnableInvert[] = {
477         { 0,    "on" },
478         { 1,    "off" },
479         EXIF_TEXT_LIST_END
480 };
481
482 static ExifTextList CanonCustomExposureLevel[] = {
483         { 0,    "1/2 stop" },
484         { 1,    "1/3 stop" },
485         EXIF_TEXT_LIST_END
486 };
487
488 static ExifTextList CanonCustomAVShutterSpeed[] = {
489         { 0,    "auto" },
490         { 1,    "1/200 (fixed)" },
491         EXIF_TEXT_LIST_END
492 };
493
494 static ExifTextList CanonCustomShutterCurtainSync[] = {
495         { 0,    "1st" },
496         { 1,    "2nd" },
497         EXIF_TEXT_LIST_END
498 };
499
500 static ExifMarker CanonCustom[] = {
501 { 1,    EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.NoiseReduction", "Noise reduction",  CanonCustomEnable },
502 /*{ 2,  EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.BtnFuncShutter",
503                                                 "Shutter/Auto exposure button function",CanonCustomBTNShutter }, */
504 { 3,    EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.MirrorLockup", "Mirror lockup",      CanonCustomEnable },
505 { 4,    EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.TvAvExposureLevel",
506                                                         "Tv/Av and exposure level",     CanonCustomExposureLevel },
507 { 5,    EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.AFAssistLight", "AF assist light",   CanonCustomEnableInvert },
508 { 6,    EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.AvShutterSpeed",
509                                                         "Shutter speed in Av mode",     CanonCustomAVShutterSpeed },
510 /*{ 7,  EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.AutoBracket",
511                                 "Auto-Exposure bracketting sequence/auto cancellation", CanonCustom }, */
512 { 8,    EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.ShutterSync", "Shutter sync",        CanonCustomShutterCurtainSync },
513 /* { 9, EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.BtnFuncAF",  "AF button function",   CanonCustom }, */
514 { 10,   EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.FillFlashReduction",
515                                                         "Fill flash auto reduction",    CanonCustomEnableInvert },
516 /*{ 11, EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.BtnFuncMenu",
517                                                         "Menu button function",         CanonCustom }, */
518 /*{ 12, EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.BtnFuncSet", "Set button function",  CanonCustom }, */
519 { 13,   EXIF_FORMAT_SHORT_UNSIGNED, 1,  "MkN.Canon.SensorCleaning", "Sensor cleaning",  CanonCustomEnable },
520 EXIF_MARKER_LIST_END
521 };
522
523 #endif
524
525 static ExifMarker CanonExifMarkersList[] = {
526         { 1,    EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings1",          NULL, NULL },
527         { 4,    EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings2",          NULL, NULL },
528         { 6,    EXIF_FORMAT_STRING, -1,         "MkN.Canon.ImageType",          "Image type", NULL },
529         { 7,    EXIF_FORMAT_STRING, -1,         "MkN.Canon.FirmwareVersion",    "Firmware version", NULL },
530         { 8,    EXIF_FORMAT_LONG_UNSIGNED, 1,   "MkN.Canon.ImageNumber",        "Image number", NULL },
531         { 9,    EXIF_FORMAT_STRING, -1,         "MkN.Canon.OwnerName",          "Owner name", NULL },
532         { 12,   EXIF_FORMAT_LONG_UNSIGNED, -1,  "MkN.Canon.SerialNumber",       "Camera serial number", NULL },
533         { 15,   EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.CustomFunctions",    NULL, NULL },
534         EXIF_MARKER_LIST_END
535 };
536
537 static void canon_mknote_parse_settings(ExifData *exif,
538                                         guint16 *data, guint32 len, ExifByteOrder bo,
539                                         ExifMarker *list)
540 {
541         gint i;
542
543         i = 0;
544         while (list[i].tag != 0)
545                 {
546                 if (list[i].tag < len)
547                         {
548                         ExifItem *item;
549
550                         item = exif_item_new(EXIF_FORMAT_SHORT_UNSIGNED, list[i].tag, 1, &list[i]);
551                         exif_item_copy_data(item, &data[list[i].tag], 2, EXIF_FORMAT_SHORT_UNSIGNED, bo);
552                         exif->items = g_list_prepend(exif->items, item);
553                         }
554
555                 i++;
556                 }
557 }
558
559 #if 0
560 static void canon_mknote_parse_convert(ExifData *exif)
561 {
562         gint value;
563         ExifItem *result;
564
565         /* seems we need more than only this value for distance */
566         if (exif_get_integer(exif, "MkN.Canon.SubjectDistance", &value))
567                 {
568                 static ExifMarker marker= { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,
569                                             "SubjectDistance", "Subject distance", NULL };
570                 ExifItem *item;
571                 ExifRational *rational;
572
573                 item = exif_item_new(marker.format, marker.tag, 1, &marker);
574                 rational = item->data;
575                 rational->num = value;
576                 rational->den = 100;
577
578                 exif->items = g_list_prepend(exif->items, item);
579                 }
580
581         result = exif_get_item(exif, "MkN.Canon.SerialNumber");
582         if (result && result->format == EXIF_FORMAT_LONG_UNSIGNED && result->data_len == 4)
583                 {
584                 static ExifMarker marker= { 12, EXIF_FORMAT_STRING, -1,
585                                             "SerialNumber", "Camera serial number", NULL };
586                 ExifItem *item;
587                 gchar *text;
588                 gint l;
589                 guint32 n;
590
591                 n = (guint32)((guint32 *)(result->data))[0];
592                 text = g_strdup_printf("%04X%05d", n & 0xffff0000 >> 8, n & 0x0000ffff);
593                 l = strlen(text) + 1;
594                 item = exif_item_new(marker.format, marker.tag, l, &marker);
595                 memcpy(item->data, text, l);
596                 g_free(text);
597
598                 exif->items = g_list_prepend(exif->items, item);
599                 }
600 }
601 #endif
602
603 gint format_canon_makernote(ExifData *exif, unsigned char *tiff, guint offset,
604                             guint size, ExifByteOrder bo)
605 {
606         ExifItem *item;
607
608         if (exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, CanonExifMarkersList) != 0)
609                 {
610                 return FALSE;
611                 }
612
613         item = exif_get_item(exif, "MkN.Canon.Settings1");
614         if (item)
615                 {
616                 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet1);
617                 }
618
619         item = exif_get_item(exif, "MkN.Canon.Settings2");
620         if (item)
621                 {
622                 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet2);
623                 }
624
625 #if 0
626         canon_mknote_parse_convert(exif);
627 #endif
628
629         return TRUE;
630 }
631
632
633 #endif
634 /* not HAVE_EXIV2 */