2 * Copyright (C) 2005 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
5 * Author: Daniel M. German <dmgerman@uvic.ca>
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.
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.
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.
22 #include "format-canon.h"
32 *-----------------------------------------------------------------------------
33 * Raw (CR2, CRW) embedded jpeg extraction for Canon
34 *-----------------------------------------------------------------------------
37 static gboolean canon_cr2_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
38 guint *image_offset, gint *jpeg_encoding)
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.
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);
55 /* tag 0x0103 contains the compression type for this segment's image data */
58 if (ExifFormatList[type].size * count == 2 &&
59 exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_DATA, bo) == 6)
61 *jpeg_encoding = TRUE;
66 /* find and verify jpeg offset */
68 !jpeg_encoding) return FALSE;
70 /* make sure data segment contains 4 bytes */
71 if (ExifFormatList[type].size * count != 4) return FALSE;
73 jpeg_start = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
75 /* verify this is jpeg data */
76 if (len < jpeg_start + 4 ||
77 memcmp(data + jpeg_start, "\xff\xd8", 2) != 0)
82 *image_offset = jpeg_start;
86 static gint canon_cr2_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
89 gboolean jpeg_encoding = FALSE;
93 if (len < offset + 2) return 0;
95 count = exif_byte_get_int16(data + offset, bo);
97 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
99 for (i = 0; i < count; i++)
101 if (canon_cr2_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo,
102 image_offset, &jpeg_encoding))
108 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
111 gboolean format_canon_raw_cr2(guchar *data, const guint len,
112 guint *image_offset, guint *)
114 guint jpeg_offset = 0;
119 /* cr2 files are tiff files with a few canon specific directory tags
120 * they are (always ?) in little endian format
122 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
125 while (offset && level < EXIF_TIFF_MAX_LEVELS)
127 offset = canon_cr2_tiff_table(data, len, offset, bo, &jpeg_offset);
130 if (jpeg_offset != 0)
132 if (image_offset) *image_offset = jpeg_offset;
140 #define CRW_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
141 #define CRW_HEADER_SIZE 26
142 #define CRW_DIR_ENTRY_SIZE 10
144 gboolean format_canon_raw_crw(guchar *data, const guint len,
145 guint *image_offset, guint *)
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)
158 * CIFF specification in pdf format is available on some websites,
159 * search for 'CIFFspecV1R03.pdf' or 'CIFFspecV1R04.pdf'
161 if (len < CRW_HEADER_SIZE ||
162 memcmp(data, "II", 2) != 0 ||
163 memcmp(data + 6, "HEAPCCDR", 8) != 0)
168 block_offset = exif_byte_get_int32(data + 2, CRW_BYTE_ORDER);
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
174 data_length = exif_byte_get_int32(data + offset, CRW_BYTE_ORDER);
176 offset = block_offset + data_length;
177 if (len < offset + 2) return FALSE;
179 /* number of directory entries for this block is in
180 * the next two bytes after the data for this block.
182 count = exif_byte_get_int16(data + offset, CRW_BYTE_ORDER);
184 if (len < offset + count * CRW_DIR_ENTRY_SIZE + 4) return FALSE;
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:
189 for (i = 0; i < count ; i++)
196 entry_offset = offset + i * CRW_DIR_ENTRY_SIZE;
198 /* entry is 10 bytes (in order):
200 * 4 for length of data
201 * 4 for offset into data segment of this block
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);
207 /* tag we want for jpeg data */
208 if (record_type == 0x2007)
212 jpeg_offset = block_offset + record_offset;
213 if (len < jpeg_offset + record_length ||
215 memcmp(data + jpeg_offset, "\xff\xd8\xff\xdb", 4) != 0)
220 /* we now know offset and verified jpeg */
221 *image_offset = jpeg_offset;
231 *-----------------------------------------------------------------------------
232 * EXIF Makernote for Canon
233 *-----------------------------------------------------------------------------
236 static ExifTextList CanonSet1MacroMode[] = {
242 static ExifTextList CanonSet1Quality[] = {
250 static ExifTextList CanonSet1FlashMode[] = {
251 { 0, "flash not fired" },
254 { 3, "red-eye reduction" },
256 { 5, "auto + red-eye reduction" },
257 { 6, "on + red-eye reduction" },
258 { 16, "external flash" },
262 static ExifTextList CanonSet1DriveMode[] = {
263 { 0, "single or timer" },
268 static ExifTextList CanonSet1FocusMode[] = {
269 { 0, "one-shot AF" },
270 { 1, "AI servo AF" },
271 { 2, "AI focus AF" },
279 static ExifTextList CanonSet1ImageSize[] = {
283 /* where (or) does Medium 1/2 fit in here ? */
287 static ExifTextList CanonSet1ShootingMode[] = {
291 { 3, "fast shutter" },
292 { 4, "slow shutter" },
294 { 6, "black and white" },
303 /* Don't think this is interpreted correctly/completely, A60 at 2.5x Digital sets value of 3 */
304 static ExifTextList CanonSet1DigitalZoom[] = {
312 static ExifTextList CanonSet1ConSatSharp[] = {
319 static ExifTextList CanonSet1ISOSpeed[] = {
320 /* { 0, "not set/see EXIF tag" }, */
329 static ExifTextList CanonSet1MeteringMode[] = {
334 { 5, "center-weighted" },
338 static ExifTextList CanonSet1FocusType[] = {
348 static ExifTextList CanonSet1AutoFocusPoint[] = {
349 { 0x2005, "manual AF point selection" },
350 { 0x3000, "manual focus" },
353 { 0x3003, "center" },
355 { 0x4001, "auto AF point selection" },
359 static ExifTextList CanonSet1ExposureMode[] = {
362 { 2, "Tv priority" },
363 { 3, "Av priority" },
369 static ExifTextList CanonSet1FlashFired[] = {
375 static ExifTextList CanonSet1FocusCont[] = {
376 { 0, "no (single)" },
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 },
412 static ExifTextList CanonSet2WhiteBalance[] = {
417 { 4, "fluorescent" },
420 { 7, "black and white" },
423 { 14, "daylight fluorescent" },
424 { 17, "underwater" },
428 static ExifTextList CanonSet2FlashBias[] = {
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 },
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 },
473 static void canon_mknote_parse_settings(ExifData *exif,
474 guint16 *data, guint32 len, ExifByteOrder bo,
480 while (list[i].tag != 0)
482 if (list[i].tag < len)
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);
496 gboolean format_canon_makernote(ExifData *exif, guchar *tiff, guint offset,
497 guint size, ExifByteOrder bo)
501 if (exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, CanonExifMarkersList) != 0)
506 item = exif_get_item(exif, "MkN.Canon.Settings1");
509 canon_mknote_parse_settings(exif, static_cast<guint16*>(item->data), item->data_len, bo, CanonSet1);
512 item = exif_get_item(exif, "MkN.Canon.Settings2");
515 canon_mknote_parse_settings(exif, static_cast<guint16*>(item->data), item->data_len, bo, CanonSet2);
521 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */