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