Compile modules based on HAVE_* features
[geeqie.git] / src / format-canon.cc
1 /*
2  * Copyright (C) 2005 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: Daniel M. German <dmgerman@uvic.ca>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "format-canon.h"
23
24 #include <cstring>
25
26 #include <glib.h>
27
28 #include "exif.h"
29
30
31 /*
32  *-----------------------------------------------------------------------------
33  * Raw (CR2, CRW) embedded jpeg extraction for Canon
34  *-----------------------------------------------------------------------------
35  */
36
37 static gboolean canon_cr2_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
38                                      guint *image_offset, gint *jpeg_encoding)
39 {
40         guint tag;
41         guint type;
42         guint count;
43         guint jpeg_start;
44
45         /* the two (tiff compliant) tags we want are:
46          *  0x0103 image compression type (must be type 6 for jpeg)
47          *  0x0111 jpeg start offset
48          * only use the first segment that contains an actual jpeg - as there
49          * is a another that contains the raw data.
50          */
51         tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
52         type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
53         count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
54
55         /* tag 0x0103 contains the compression type for this segment's image data */
56         if (tag == 0x0103)
57                 {
58                 if (ExifFormatList[type].size * count == 2 &&
59                     exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_DATA, bo) == 6)
60                         {
61                         *jpeg_encoding = TRUE;
62                         }
63                 return FALSE;
64                 }
65
66         /* find and verify jpeg offset */
67         if (tag != 0x0111 ||
68             !jpeg_encoding) return FALSE;
69
70         /* make sure data segment contains 4 bytes */
71         if (ExifFormatList[type].size * count != 4) return FALSE;
72
73         jpeg_start = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
74
75         /* verify this is jpeg data */
76         if (len < jpeg_start + 4 ||
77             memcmp(data + jpeg_start, "\xff\xd8", 2) != 0)
78                 {
79                 return FALSE;
80                 }
81
82         *image_offset = jpeg_start;
83         return TRUE;
84 }
85
86 static gint canon_cr2_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
87                                  guint *image_offset)
88 {
89         gboolean jpeg_encoding = FALSE;
90         guint count;
91         guint i;
92
93         if (len < offset + 2) return 0;
94
95         count = exif_byte_get_int16(data + offset, bo);
96         offset += 2;
97         if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
98
99         for (i = 0; i < count; i++)
100                 {
101                 if (canon_cr2_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo,
102                                          image_offset, &jpeg_encoding))
103                         {
104                         return 0;
105                         }
106                 }
107
108         return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
109 }
110
111 gboolean format_canon_raw_cr2(guchar *data, const guint len,
112                               guint *image_offset, guint *)
113 {
114         guint jpeg_offset = 0;
115         ExifByteOrder bo;
116         guint offset;
117         gint level;
118
119         /* cr2 files are tiff files with a few canon specific directory tags
120          * they are (always ?) in little endian format
121          */
122         if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
123
124         level = 0;
125         while (offset && level < EXIF_TIFF_MAX_LEVELS)
126                 {
127                 offset = canon_cr2_tiff_table(data, len, offset, bo, &jpeg_offset);
128                 level++;
129
130                 if (jpeg_offset != 0)
131                         {
132                         if (image_offset) *image_offset = jpeg_offset;
133                         return TRUE;
134                         }
135                 }
136
137         return FALSE;
138 }
139
140 #define CRW_BYTE_ORDER          EXIF_BYTE_ORDER_INTEL
141 #define CRW_HEADER_SIZE         26
142 #define CRW_DIR_ENTRY_SIZE      10
143
144 gboolean format_canon_raw_crw(guchar *data, const guint len,
145                               guint *image_offset, guint *)
146 {
147         guint block_offset;
148         guint data_length;
149         guint offset;
150         guint count;
151         guint i;
152
153         /* CRW header starts with 2 bytes for byte order (always "II", little endian),
154          * 4 bytes for start of root block,
155          * and 8 bytes of magic for file type and format 'HEAPCCDR'
156          * (also 4 bytes for file version, and 8 bytes reserved)
157          *
158          * CIFF specification in pdf format is available on some websites,
159          * search for 'CIFFspecV1R03.pdf' or 'CIFFspecV1R04.pdf'
160          */
161         if (len < CRW_HEADER_SIZE ||
162             memcmp(data, "II", 2) != 0 ||
163             memcmp(data + 6, "HEAPCCDR", 8) != 0)
164                 {
165                 return FALSE;
166                 }
167
168         block_offset = exif_byte_get_int32(data + 2, CRW_BYTE_ORDER);
169
170         /* the end of the root block equals end of file,
171          * the last 4 bytes of the root block contain the block's data size
172          */
173         offset = len - 4;
174         data_length = exif_byte_get_int32(data + offset, CRW_BYTE_ORDER);
175
176         offset = block_offset + data_length;
177         if (len < offset + 2) return FALSE;
178
179         /* number of directory entries for this block is in
180          * the next two bytes after the data for this block.
181          */
182         count = exif_byte_get_int16(data + offset, CRW_BYTE_ORDER);
183         offset += 2;
184         if (len < offset + count * CRW_DIR_ENTRY_SIZE + 4) return FALSE;
185
186         /* walk the directory entries looking for type jpeg (tag 0x2007),
187          * for reference, other tags are 0x2005 for raw and 0x300a for photo info:
188          */
189         for (i = 0; i < count ; i++)
190                 {
191                 guint entry_offset;
192                 guint record_type;
193                 guint record_offset;
194                 guint record_length;
195
196                 entry_offset = offset + i * CRW_DIR_ENTRY_SIZE;
197
198                 /* entry is 10 bytes (in order):
199                  *  2 for type
200                  *  4 for length of data
201                  *  4 for offset into data segment of this block
202                  */
203                 record_type = exif_byte_get_int16(data + entry_offset, CRW_BYTE_ORDER);
204                 record_length = exif_byte_get_int32(data + entry_offset + 2, CRW_BYTE_ORDER);
205                 record_offset = exif_byte_get_int32(data + entry_offset + 6, CRW_BYTE_ORDER);
206
207                 /* tag we want for jpeg data */
208                 if (record_type == 0x2007)
209                         {
210                         guint jpeg_offset;
211
212                         jpeg_offset = block_offset + record_offset;
213                         if (len < jpeg_offset + record_length ||
214                             record_length < 4 ||
215                             memcmp(data + jpeg_offset, "\xff\xd8\xff\xdb", 4) != 0)
216                                 {
217                                 return FALSE;
218                                 }
219
220                         /* we now know offset and verified jpeg */
221                         *image_offset = jpeg_offset;
222                         return TRUE;
223                         }
224                 }
225
226         return FALSE;
227 }
228
229
230 /*
231  *-----------------------------------------------------------------------------
232  * EXIF Makernote for Canon
233  *-----------------------------------------------------------------------------
234  */
235
236 static ExifTextList CanonSet1MacroMode[] = {
237         { 1,    "macro" },
238         { 2,    "normal" },
239         EXIF_TEXT_LIST_END
240 };
241
242 static ExifTextList CanonSet1Quality[] = {
243         { 2,    "normal" },
244         { 3,    "fine" },
245         { 4,    "raw" },
246         { 5,    "superfine" },
247         EXIF_TEXT_LIST_END
248 };
249
250 static ExifTextList CanonSet1FlashMode[] = {
251         { 0,    "flash not fired" },
252         { 1,    "auto" },
253         { 2,    "on" },
254         { 3,    "red-eye reduction" },
255         { 4,    "slow sync" },
256         { 5,    "auto + red-eye reduction" },
257         { 6,    "on + red-eye reduction" },
258         { 16,   "external flash" },
259         EXIF_TEXT_LIST_END
260 };
261
262 static ExifTextList CanonSet1DriveMode[] = {
263         { 0,    "single or timer" },
264         { 1,    "continuous" },
265         EXIF_TEXT_LIST_END
266 };
267
268 static ExifTextList CanonSet1FocusMode[] = {
269         { 0,    "one-shot AF" },
270         { 1,    "AI servo AF" },
271         { 2,    "AI focus AF" },
272         { 3,    "manual" },
273         { 4,    "single" },
274         { 5,    "continuous" },
275         { 6,    "manual" },
276         EXIF_TEXT_LIST_END
277 };
278
279 static ExifTextList CanonSet1ImageSize[] = {
280         { 0,    "large" },
281         { 1,    "medium" },
282         { 2,    "small" },
283         /* where (or) does Medium 1/2 fit in here ? */
284         EXIF_TEXT_LIST_END
285 };
286
287 static ExifTextList CanonSet1ShootingMode[] = {
288         { 0,    "auto" },
289         { 1,    "manual" },
290         { 2,    "landscape" },
291         { 3,    "fast shutter" },
292         { 4,    "slow shutter" },
293         { 5,    "night" },
294         { 6,    "black and white" },
295         { 7,    "sepia" },
296         { 8,    "portrait" },
297         { 9,    "sports" },
298         { 10,   "macro" },
299         { 11,   "pan focus" },
300         EXIF_TEXT_LIST_END
301 };
302
303 /* Don't think this is interpreted correctly/completely, A60 at 2.5x Digital sets value of 3 */
304 static ExifTextList CanonSet1DigitalZoom[] = {
305         { 0,    "none" },
306         { 1,    "2x" },
307         { 2,    "4x" },
308         { 3,    "other" },
309         EXIF_TEXT_LIST_END
310 };
311
312 static ExifTextList CanonSet1ConSatSharp[] = {
313         { 0,    "normal" },
314         { 1,    "high" },
315         { 65535,"low" },
316         EXIF_TEXT_LIST_END
317 };
318
319 static ExifTextList CanonSet1ISOSpeed[] = {
320 /*      { 0,    "not set/see EXIF tag" }, */
321         { 15,   "auto" },
322         { 16,   "50" },
323         { 17,   "100" },
324         { 18,   "200" },
325         { 19,   "400" },
326         EXIF_TEXT_LIST_END
327 };
328
329 static ExifTextList CanonSet1MeteringMode[] = {
330         { 0,    "default" },
331         { 1,    "spot" },
332         { 3,    "evaluative" },
333         { 4,    "partial" },
334         { 5,    "center-weighted" },
335         EXIF_TEXT_LIST_END
336 };
337
338 static ExifTextList CanonSet1FocusType[] = {
339         { 0,    "manual" },
340         { 1,    "auto" },
341         { 2,    "auto" },
342         { 3,    "macro" },
343         { 7,    "infinity" },
344         { 8,    "locked" },
345         EXIF_TEXT_LIST_END
346 };
347
348 static ExifTextList CanonSet1AutoFocusPoint[] = {
349         { 0x2005,       "manual AF point selection" },
350         { 0x3000,       "manual focus" },
351         { 0x3001,       "auto" },
352         { 0x3002,       "right" },
353         { 0x3003,       "center" },
354         { 0x3004,       "left" },
355         { 0x4001,       "auto AF point selection" },
356         EXIF_TEXT_LIST_END
357 };
358
359 static ExifTextList CanonSet1ExposureMode[] = {
360         { 0,    "auto" },
361         { 1,    "program" },
362         { 2,    "Tv priority" },
363         { 3,    "Av priority" },
364         { 4,    "manual" },
365         { 5,    "A-DEP" },
366         EXIF_TEXT_LIST_END
367 };
368
369 static ExifTextList CanonSet1FlashFired[] = {
370         { 0,    "no" },
371         { 1,    "yes" },
372         EXIF_TEXT_LIST_END
373 };
374
375 static ExifTextList CanonSet1FocusCont[] = {
376         { 0,    "no (single)" },
377         { 1,    "yes" },
378         EXIF_TEXT_LIST_END
379 };
380
381 static ExifMarker CanonSet1[] = {
382 /* 0 is length of array in bytes (2 x array size) */
383 { 1,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MacroMode",   "Macro mode",           CanonSet1MacroMode },
384 { 2,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SelfTimer",   "Self timer (10ths of second)", nullptr },
385 { 3,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Quality",     "Quality",              CanonSet1Quality },
386 { 4,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashMode",   "Flash mode",           CanonSet1FlashMode },
387 { 5,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DriveMode",   "Drive mode",           CanonSet1DriveMode },
388 { 7,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusMode",   "Focus mode",           CanonSet1FocusMode },
389 { 10,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ImageSize",   "Image size",           CanonSet1ImageSize },
390 { 11,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ShootingMode","Shooting mode",        CanonSet1ShootingMode },
391  { 11,  EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureProgram",       "ExposureProgram",      CanonSet1ShootingMode },
392 { 12,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DigitalZoom", "Digital zoom",         CanonSet1DigitalZoom },
393 { 13,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Contrast",    "Contrast",             CanonSet1ConSatSharp },
394 { 14,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Saturation",  "Saturation",           CanonSet1ConSatSharp },
395 { 15,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Sharpness",   "Sharpness",            CanonSet1ConSatSharp },
396 { 16,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ISOSpeed",    "ISO speed",            CanonSet1ISOSpeed },
397  { 16,  EXIF_FORMAT_SHORT_UNSIGNED, 1, "ISOSpeedRatings",       "ISO speed",            CanonSet1ISOSpeed },
398 { 17,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MeteringMode","Metering mode",        CanonSet1MeteringMode },
399 { 18,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusType",   "Focus type",           CanonSet1FocusType },
400 { 19,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AutoFocus",   "AutoFocus point",      CanonSet1AutoFocusPoint },
401 { 20,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ExposureMode","Exposure mode",        CanonSet1ExposureMode },
402  { 20,  EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureMode",          "Exposure mode",        CanonSet1ExposureMode },
403 { 23,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthLong","Long focal length", nullptr },
404 { 24,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthShort","Short focal length", nullptr },
405 { 25,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthUnits","Focal units per mm", nullptr },
406 { 28,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashFired",  "Flash fired",          CanonSet1FlashFired },
407 { 29,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashDetails","Flash details",        nullptr },
408 { 32,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ContinuousFocus","Continuous focus",  CanonSet1FocusCont },
409 EXIF_MARKER_LIST_END
410 };
411
412 static ExifTextList CanonSet2WhiteBalance[] = {
413         { 0,    "auto" },
414         { 1,    "sunny" },
415         { 2,    "cloudy" },
416         { 3,    "tungsten" },
417         { 4,    "fluorescent" },
418         { 5,    "flash" },
419         { 6,    "custom" },
420         { 7,    "black and white" },
421         { 8,    "shade" },
422         { 9,    "manual" },
423         { 14,   "daylight fluorescent" },
424         { 17,   "underwater" },
425         EXIF_TEXT_LIST_END
426 };
427
428 static ExifTextList CanonSet2FlashBias[] = {
429         { 0x0000,       "0" },
430         { 0x000c,       "0.33" },
431         { 0x0010,       "0.5" },
432         { 0x0014,       "0.67" },
433         { 0x0020,       "1" },
434         { 0x002c,       "1.33" },
435         { 0x0030,       "1.5" },
436         { 0x0034,       "1.67" },
437         { 0x0040,       "2" },
438         { 0xffc0,       "-2" },
439         { 0xffcc,       "-1.67" },
440         { 0xffd0,       "-1.5" },
441         { 0xffd4,       "-1.33" },
442         { 0xffe0,       "-1" },
443         { 0xffec,       "-0.67" },
444         { 0xfff0,       "-0.5" },
445         { 0xfff4,       "-0.33" },
446         EXIF_TEXT_LIST_END
447 };
448
449 static ExifMarker CanonSet2[] = {
450 /* 0 is length of array in bytes (2 x array size) */
451 { 7,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.WhiteBalance","White balance",        CanonSet2WhiteBalance },
452  { 7,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "LightSource",           "White balance",        CanonSet2WhiteBalance },
453 { 9,    EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SequenceNumber","Sequence number",    nullptr },
454 { 15,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashBias",   "Flash bias",           CanonSet2FlashBias },
455 /* distance needs more than just this (metric) value */
456 { 19,   EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SubjectDistance",     "Subject Distance", nullptr },
457 EXIF_MARKER_LIST_END
458 };
459
460
461 static ExifMarker CanonExifMarkersList[] = {
462         { 1,    EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings1",          nullptr, nullptr },
463         { 4,    EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings2",          nullptr, nullptr },
464         { 6,    EXIF_FORMAT_STRING, -1,         "MkN.Canon.ImageType",          "Image type", nullptr },
465         { 7,    EXIF_FORMAT_STRING, -1,         "MkN.Canon.FirmwareVersion",    "Firmware version", nullptr },
466         { 8,    EXIF_FORMAT_LONG_UNSIGNED, 1,   "MkN.Canon.ImageNumber",        "Image number", nullptr },
467         { 9,    EXIF_FORMAT_STRING, -1,         "MkN.Canon.OwnerName",          "Owner name", nullptr },
468         { 12,   EXIF_FORMAT_LONG_UNSIGNED, -1,  "MkN.Canon.SerialNumber",       "Camera serial number", nullptr },
469         { 15,   EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.CustomFunctions",    nullptr, nullptr },
470         EXIF_MARKER_LIST_END
471 };
472
473 static void canon_mknote_parse_settings(ExifData *exif,
474                                         guint16 *data, guint32 len, ExifByteOrder bo,
475                                         ExifMarker *list)
476 {
477         gint i;
478
479         i = 0;
480         while (list[i].tag != 0)
481                 {
482                 if (list[i].tag < len)
483                         {
484                         ExifItem *item;
485
486                         item = exif_item_new(EXIF_FORMAT_SHORT_UNSIGNED, list[i].tag, 1, &list[i]);
487                         exif_item_copy_data(item, &data[list[i].tag], 2, EXIF_FORMAT_SHORT_UNSIGNED, bo);
488                         exif->items = g_list_prepend(exif->items, item);
489                         }
490
491                 i++;
492                 }
493 }
494
495
496 gboolean format_canon_makernote(ExifData *exif, guchar *tiff, guint offset,
497                                 guint size, ExifByteOrder bo)
498 {
499         ExifItem *item;
500
501         if (exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, CanonExifMarkersList) != 0)
502                 {
503                 return FALSE;
504                 }
505
506         item = exif_get_item(exif, "MkN.Canon.Settings1");
507         if (item)
508                 {
509                 canon_mknote_parse_settings(exif, static_cast<guint16*>(item->data), item->data_len, bo, CanonSet1);
510                 }
511
512         item = exif_get_item(exif, "MkN.Canon.Settings2");
513         if (item)
514                 {
515                 canon_mknote_parse_settings(exif, static_cast<guint16*>(item->data), item->data_len, bo, CanonSet2);
516                 }
517
518         return TRUE;
519 }
520
521 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */