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