4 * Copyright (C) 2008 - 2012 The Geeqie Team
6 * This software is released under the GNU General Public License (GNU GPL).
7 * Please read the included file COPYING for more information.
8 * This software comes with no warranty of any kind, use at your own risk!
11 * Code to add support for Canon CR2 and CRW files, version 0.2
13 * Developed by Daniel M. German, dmgerman at uvic.ca
15 * you can find the sources for this patch at http://turingmachine.org/~dmg/libdcraw/gqview/
34 #include "format_canon.h"
35 #include "format_raw.h"
41 *-----------------------------------------------------------------------------
42 * Raw (CR2, CRW) embedded jpeg extraction for Canon
43 *-----------------------------------------------------------------------------
46 static gboolean canon_cr2_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
47 guint *image_offset, gint *jpeg_encoding)
54 /* the two (tiff compliant) tags we want are:
55 * 0x0103 image compression type (must be type 6 for jpeg)
56 * 0x0111 jpeg start offset
57 * only use the first segment that contains an actual jpeg - as there
58 * is a another that contains the raw data.
60 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
61 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
62 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
64 /* tag 0x0103 contains the compression type for this segment's image data */
67 if (ExifFormatList[type].size * count == 2 &&
68 exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_DATA, bo) == 6)
70 *jpeg_encoding = TRUE;
75 /* find and verify jpeg offset */
77 !jpeg_encoding) return FALSE;
79 /* make sure data segment contains 4 bytes */
80 if (ExifFormatList[type].size * count != 4) return FALSE;
82 jpeg_start = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
84 /* verify this is jpeg data */
85 if (len < jpeg_start + 4 ||
86 memcmp(data + jpeg_start, "\xff\xd8", 2) != 0)
91 *image_offset = jpeg_start;
95 static gint canon_cr2_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
98 gboolean jpeg_encoding = FALSE;
102 if (len < offset + 2) return 0;
104 count = exif_byte_get_int16(data + offset, bo);
106 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
108 for (i = 0; i < count; i++)
110 if (canon_cr2_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo,
111 image_offset, &jpeg_encoding))
117 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
120 gboolean format_canon_raw_cr2(guchar *data, const guint len,
121 guint *image_offset, guint *exif_offset)
123 guint jpeg_offset = 0;
128 /* cr2 files are tiff files with a few canon specific directory tags
129 * they are (always ?) in little endian format
131 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
134 while (offset && level < EXIF_TIFF_MAX_LEVELS)
136 offset = canon_cr2_tiff_table(data, len, offset, bo, &jpeg_offset);
139 if (jpeg_offset != 0)
141 if (image_offset) *image_offset = jpeg_offset;
149 #define CRW_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
150 #define CRW_HEADER_SIZE 26
151 #define CRW_DIR_ENTRY_SIZE 10
153 gboolean format_canon_raw_crw(guchar *data, const guint len,
154 guint *image_offset, guint *exif_offset)
162 /* CRW header starts with 2 bytes for byte order (always "II", little endian),
163 * 4 bytes for start of root block,
164 * and 8 bytes of magic for file type and format "HEAPCCDR"
165 * (also 4 bytes for file version, and 8 bytes reserved)
167 * CIFF specification in pdf format is available on some websites,
168 * search for "CIFFspecV1R03.pdf" or "CIFFspecV1R04.pdf"
170 if (len < CRW_HEADER_SIZE ||
171 memcmp(data, "II", 2) != 0 ||
172 memcmp(data + 6, "HEAPCCDR", 8) != 0)
177 block_offset = exif_byte_get_int32(data + 2, CRW_BYTE_ORDER);
179 /* the end of the root block equals end of file,
180 * the last 4 bytes of the root block contain the block's data size
183 data_length = exif_byte_get_int32(data + offset, CRW_BYTE_ORDER);
185 offset = block_offset + data_length;
186 if (len < offset + 2) return FALSE;
188 /* number of directory entries for this block is in
189 * the next two bytes after the data for this block.
191 count = exif_byte_get_int16(data + offset, CRW_BYTE_ORDER);
193 if (len < offset + count * CRW_DIR_ENTRY_SIZE + 4) return FALSE;
195 /* walk the directory entries looking for type jpeg (tag 0x2007),
196 * for reference, other tags are 0x2005 for raw and 0x300a for photo info:
198 for (i = 0; i < count ; i++)
205 entry_offset = offset + i * CRW_DIR_ENTRY_SIZE;
207 /* entry is 10 bytes (in order):
209 * 4 for length of data
210 * 4 for offset into data segment of this block
212 record_type = exif_byte_get_int16(data + entry_offset, CRW_BYTE_ORDER);
213 record_length = exif_byte_get_int32(data + entry_offset + 2, CRW_BYTE_ORDER);
214 record_offset = exif_byte_get_int32(data + entry_offset + 6, CRW_BYTE_ORDER);
216 /* tag we want for jpeg data */
217 if (record_type == 0x2007)
221 jpeg_offset = block_offset + record_offset;
222 if (len < jpeg_offset + record_length ||
224 memcmp(data + jpeg_offset, "\xff\xd8\xff\xdb", 4) != 0)
229 /* we now know offset and verified jpeg */
230 *image_offset = jpeg_offset;
240 *-----------------------------------------------------------------------------
241 * EXIF Makernote for Canon
242 *-----------------------------------------------------------------------------
245 static ExifTextList CanonSet1MacroMode[] = {
251 static ExifTextList CanonSet1Quality[] = {
259 static ExifTextList CanonSet1FlashMode[] = {
260 { 0, "flash not fired" },
263 { 3, "red-eye reduction" },
265 { 5, "auto + red-eye reduction" },
266 { 6, "on + red-eye reduction" },
267 { 16, "external flash" },
271 static ExifTextList CanonSet1DriveMode[] = {
272 { 0, "single or timer" },
277 static ExifTextList CanonSet1FocusMode[] = {
278 { 0, "one-shot AF" },
279 { 1, "AI servo AF" },
280 { 2, "AI focus AF" },
288 static ExifTextList CanonSet1ImageSize[] = {
292 /* where (or) does Medium 1/2 fit in here ? */
296 static ExifTextList CanonSet1ShootingMode[] = {
300 { 3, "fast shutter" },
301 { 4, "slow shutter" },
303 { 6, "black and white" },
312 /* Don't think this is interpreted correctly/completely, A60 at 2.5x Digital sets value of 3 */
313 static ExifTextList CanonSet1DigitalZoom[] = {
321 static ExifTextList CanonSet1ConSatSharp[] = {
328 static ExifTextList CanonSet1ISOSpeed[] = {
329 /* { 0, "not set/see EXIF tag" }, */
338 static ExifTextList CanonSet1MeteringMode[] = {
343 { 5, "center-weighted" },
347 static ExifTextList CanonSet1FocusType[] = {
357 static ExifTextList CanonSet1AutoFocusPoint[] = {
358 { 0x2005, "manual AF point selection" },
359 { 0x3000, "manual focus" },
362 { 0x3003, "center" },
364 { 0x4001, "auto AF point selection" },
368 static ExifTextList CanonSet1ExposureMode[] = {
371 { 2, "Tv priority" },
372 { 3, "Av priority" },
378 static ExifTextList CanonSet1FlashFired[] = {
384 static ExifTextList CanonSet1FocusCont[] = {
385 { 0, "no (single)" },
390 static ExifMarker CanonSet1[] = {
391 /* 0 is length of array in bytes (2 x array size) */
392 { 1, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MacroMode", "Macro mode", CanonSet1MacroMode },
393 { 2, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SelfTimer", "Self timer (10ths of second)", NULL },
394 { 3, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Quality", "Quality", CanonSet1Quality },
395 { 4, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashMode", "Flash mode", CanonSet1FlashMode },
396 { 5, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DriveMode", "Drive mode", CanonSet1DriveMode },
397 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusMode", "Focus mode", CanonSet1FocusMode },
398 { 10, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ImageSize", "Image size", CanonSet1ImageSize },
399 { 11, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ShootingMode","Shooting mode", CanonSet1ShootingMode },
400 { 11, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureProgram", "ExposureProgram", CanonSet1ShootingMode },
401 { 12, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DigitalZoom", "Digital zoom", CanonSet1DigitalZoom },
402 { 13, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Contrast", "Contrast", CanonSet1ConSatSharp },
403 { 14, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Saturation", "Saturation", CanonSet1ConSatSharp },
404 { 15, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Sharpness", "Sharpness", CanonSet1ConSatSharp },
405 { 16, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ISOSpeed", "ISO speed", CanonSet1ISOSpeed },
406 { 16, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ISOSpeedRatings", "ISO speed", CanonSet1ISOSpeed },
407 { 17, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MeteringMode","Metering mode", CanonSet1MeteringMode },
408 { 18, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusType", "Focus type", CanonSet1FocusType },
409 { 19, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AutoFocus", "AutoFocus point", CanonSet1AutoFocusPoint },
410 { 20, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ExposureMode","Exposure mode", CanonSet1ExposureMode },
411 { 20, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureMode", "Exposure mode", CanonSet1ExposureMode },
412 { 23, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthLong","Long focal length", NULL },
413 { 24, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthShort","Short focal length", NULL },
414 { 25, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthUnits","Focal units per mm", NULL },
415 { 28, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashFired", "Flash fired", CanonSet1FlashFired },
416 { 29, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashDetails","Flash details", NULL },
417 { 32, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ContinuousFocus","Continuous focus", CanonSet1FocusCont },
421 static ExifTextList CanonSet2WhiteBalance[] = {
426 { 4, "fluorescent" },
429 { 7, "black and white" },
432 { 14, "daylight fluorescent" },
433 { 17, "underwater" },
437 static ExifTextList CanonSet2FlashBias[] = {
458 static ExifMarker CanonSet2[] = {
459 /* 0 is length of array in bytes (2 x array size) */
460 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.WhiteBalance","White balance", CanonSet2WhiteBalance },
461 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "LightSource", "White balance", CanonSet2WhiteBalance },
462 { 9, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SequenceNumber","Sequence number", NULL },
463 { 15, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashBias", "Flash bias", CanonSet2FlashBias },
464 /* distance needs more than just this (metric) value */
465 { 19, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SubjectDistance", "Subject Distance", NULL },
470 static ExifMarker CanonExifMarkersList[] = {
471 { 1, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings1", NULL, NULL },
472 { 4, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings2", NULL, NULL },
473 { 6, EXIF_FORMAT_STRING, -1, "MkN.Canon.ImageType", "Image type", NULL },
474 { 7, EXIF_FORMAT_STRING, -1, "MkN.Canon.FirmwareVersion", "Firmware version", NULL },
475 { 8, EXIF_FORMAT_LONG_UNSIGNED, 1, "MkN.Canon.ImageNumber", "Image number", NULL },
476 { 9, EXIF_FORMAT_STRING, -1, "MkN.Canon.OwnerName", "Owner name", NULL },
477 { 12, EXIF_FORMAT_LONG_UNSIGNED, -1, "MkN.Canon.SerialNumber", "Camera serial number", NULL },
478 { 15, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.CustomFunctions", NULL, NULL },
482 static void canon_mknote_parse_settings(ExifData *exif,
483 guint16 *data, guint32 len, ExifByteOrder bo,
489 while (list[i].tag != 0)
491 if (list[i].tag < len)
495 item = exif_item_new(EXIF_FORMAT_SHORT_UNSIGNED, list[i].tag, 1, &list[i]);
496 exif_item_copy_data(item, &data[list[i].tag], 2, EXIF_FORMAT_SHORT_UNSIGNED, bo);
497 exif->items = g_list_prepend(exif->items, item);
505 gboolean format_canon_makernote(ExifData *exif, guchar *tiff, guint offset,
506 guint size, ExifByteOrder bo)
510 if (exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, CanonExifMarkersList) != 0)
515 item = exif_get_item(exif, "MkN.Canon.Settings1");
518 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet1);
521 item = exif_get_item(exif, "MkN.Canon.Settings2");
524 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet2);
533 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */