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.
37 #include "format_canon.h"
38 #include "format_raw.h"
44 *-----------------------------------------------------------------------------
45 * Raw (CR2, CRW) embedded jpeg extraction for Canon
46 *-----------------------------------------------------------------------------
49 static gboolean canon_cr2_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
50 guint *image_offset, gint *jpeg_encoding)
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.
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);
67 /* tag 0x0103 contains the compression type for this segment's image data */
70 if (ExifFormatList[type].size * count == 2 &&
71 exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_DATA, bo) == 6)
73 *jpeg_encoding = TRUE;
78 /* find and verify jpeg offset */
80 !jpeg_encoding) return FALSE;
82 /* make sure data segment contains 4 bytes */
83 if (ExifFormatList[type].size * count != 4) return FALSE;
85 jpeg_start = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
87 /* verify this is jpeg data */
88 if (len < jpeg_start + 4 ||
89 memcmp(data + jpeg_start, "\xff\xd8", 2) != 0)
94 *image_offset = jpeg_start;
98 static gint canon_cr2_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
101 gboolean jpeg_encoding = FALSE;
105 if (len < offset + 2) return 0;
107 count = exif_byte_get_int16(data + offset, bo);
109 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
111 for (i = 0; i < count; i++)
113 if (canon_cr2_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo,
114 image_offset, &jpeg_encoding))
120 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
123 gboolean format_canon_raw_cr2(guchar *data, const guint len,
124 guint *image_offset, guint *exif_offset)
126 guint jpeg_offset = 0;
131 /* cr2 files are tiff files with a few canon specific directory tags
132 * they are (always ?) in little endian format
134 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
137 while (offset && level < EXIF_TIFF_MAX_LEVELS)
139 offset = canon_cr2_tiff_table(data, len, offset, bo, &jpeg_offset);
142 if (jpeg_offset != 0)
144 if (image_offset) *image_offset = jpeg_offset;
152 #define CRW_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
153 #define CRW_HEADER_SIZE 26
154 #define CRW_DIR_ENTRY_SIZE 10
156 gboolean format_canon_raw_crw(guchar *data, const guint len,
157 guint *image_offset, guint *exif_offset)
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)
170 * CIFF specification in pdf format is available on some websites,
171 * search for "CIFFspecV1R03.pdf" or "CIFFspecV1R04.pdf"
173 if (len < CRW_HEADER_SIZE ||
174 memcmp(data, "II", 2) != 0 ||
175 memcmp(data + 6, "HEAPCCDR", 8) != 0)
180 block_offset = exif_byte_get_int32(data + 2, CRW_BYTE_ORDER);
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
186 data_length = exif_byte_get_int32(data + offset, CRW_BYTE_ORDER);
188 offset = block_offset + data_length;
189 if (len < offset + 2) return FALSE;
191 /* number of directory entries for this block is in
192 * the next two bytes after the data for this block.
194 count = exif_byte_get_int16(data + offset, CRW_BYTE_ORDER);
196 if (len < offset + count * CRW_DIR_ENTRY_SIZE + 4) return FALSE;
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:
201 for (i = 0; i < count ; i++)
208 entry_offset = offset + i * CRW_DIR_ENTRY_SIZE;
210 /* entry is 10 bytes (in order):
212 * 4 for length of data
213 * 4 for offset into data segment of this block
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);
219 /* tag we want for jpeg data */
220 if (record_type == 0x2007)
224 jpeg_offset = block_offset + record_offset;
225 if (len < jpeg_offset + record_length ||
227 memcmp(data + jpeg_offset, "\xff\xd8\xff\xdb", 4) != 0)
232 /* we now know offset and verified jpeg */
233 *image_offset = jpeg_offset;
243 *-----------------------------------------------------------------------------
244 * EXIF Makernote for Canon
245 *-----------------------------------------------------------------------------
248 static ExifTextList CanonSet1MacroMode[] = {
254 static ExifTextList CanonSet1Quality[] = {
262 static ExifTextList CanonSet1FlashMode[] = {
263 { 0, "flash not fired" },
266 { 3, "red-eye reduction" },
268 { 5, "auto + red-eye reduction" },
269 { 6, "on + red-eye reduction" },
270 { 16, "external flash" },
274 static ExifTextList CanonSet1DriveMode[] = {
275 { 0, "single or timer" },
280 static ExifTextList CanonSet1FocusMode[] = {
281 { 0, "one-shot AF" },
282 { 1, "AI servo AF" },
283 { 2, "AI focus AF" },
291 static ExifTextList CanonSet1ImageSize[] = {
295 /* where (or) does Medium 1/2 fit in here ? */
299 static ExifTextList CanonSet1ShootingMode[] = {
303 { 3, "fast shutter" },
304 { 4, "slow shutter" },
306 { 6, "black and white" },
315 /* Don't think this is interpreted correctly/completely, A60 at 2.5x Digital sets value of 3 */
316 static ExifTextList CanonSet1DigitalZoom[] = {
324 static ExifTextList CanonSet1ConSatSharp[] = {
331 static ExifTextList CanonSet1ISOSpeed[] = {
332 /* { 0, "not set/see EXIF tag" }, */
341 static ExifTextList CanonSet1MeteringMode[] = {
346 { 5, "center-weighted" },
350 static ExifTextList CanonSet1FocusType[] = {
360 static ExifTextList CanonSet1AutoFocusPoint[] = {
361 { 0x2005, "manual AF point selection" },
362 { 0x3000, "manual focus" },
365 { 0x3003, "center" },
367 { 0x4001, "auto AF point selection" },
371 static ExifTextList CanonSet1ExposureMode[] = {
374 { 2, "Tv priority" },
375 { 3, "Av priority" },
381 static ExifTextList CanonSet1FlashFired[] = {
387 static ExifTextList CanonSet1FocusCont[] = {
388 { 0, "no (single)" },
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 },
424 static ExifTextList CanonSet2WhiteBalance[] = {
429 { 4, "fluorescent" },
432 { 7, "black and white" },
435 { 14, "daylight fluorescent" },
436 { 17, "underwater" },
440 static ExifTextList CanonSet2FlashBias[] = {
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 },
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 },
485 static void canon_mknote_parse_settings(ExifData *exif,
486 guint16 *data, guint32 len, ExifByteOrder bo,
492 while (list[i].tag != 0)
494 if (list[i].tag < len)
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);
508 gboolean format_canon_makernote(ExifData *exif, guchar *tiff, guint offset,
509 guint size, ExifByteOrder bo)
513 if (exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, CanonExifMarkersList) != 0)
518 item = exif_get_item(exif, "MkN.Canon.Settings1");
521 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet1);
524 item = exif_get_item(exif, "MkN.Canon.Settings2");
527 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet2);
536 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */