5 * This software is released under the GNU General Public License (GNU GPL).
6 * Please read the included file COPYING for more information.
7 * This software comes with no warranty of any kind, use at your own risk!
10 * Code to add support for Canon CR2 and CRW files, version 0.2
12 * Developed by Daniel M. German, dmgerman at uvic.ca
14 * you can find the sources for this patch at http://turingmachine.org/~dmg/libdcraw/gqview/
33 #include "format_canon.h"
34 #include "format_raw.h"
40 *-----------------------------------------------------------------------------
41 * Raw (CR2, CRW) embedded jpeg extraction for Canon
42 *-----------------------------------------------------------------------------
45 static gint canon_cr2_tiff_entry(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
46 guint *image_offset, gint *jpeg_encoding)
53 /* the two (tiff compliant) tags we want are:
54 * 0x0103 image compression type (must be type 6 for jpeg)
55 * 0x0111 jpeg start offset
56 * only use the first segment that contains an actual jpeg - as there
57 * is a another that contains the raw data.
59 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
60 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
61 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
63 /* tag 0x0103 contains the compression type for this segment's image data */
66 if (ExifFormatList[type].size * count == 2 &&
67 exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_DATA, bo) == 6)
69 *jpeg_encoding = TRUE;
74 /* find and verify jpeg offset */
76 !jpeg_encoding) return FALSE;
78 /* make sure data segment contains 4 bytes */
79 if (ExifFormatList[type].size * count != 4) return FALSE;
81 jpeg_start = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
83 /* verify this is jpeg data */
84 if (len < jpeg_start + 4 ||
85 memcmp(data + jpeg_start, "\xff\xd8", 2) != 0)
90 *image_offset = jpeg_start;
94 static gint canon_cr2_tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
97 gint jpeg_encoding = FALSE;
101 if (len < offset + 2) return 0;
103 count = exif_byte_get_int16(data + offset, bo);
105 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
107 for (i = 0; i < count; i++)
109 if (canon_cr2_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo,
110 image_offset, &jpeg_encoding))
116 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
119 gint format_canon_raw_cr2(unsigned char *data, const guint len,
120 guint *image_offset, guint *exif_offset)
122 guint jpeg_offset = 0;
127 /* cr2 files are tiff files with a few canon specific directory tags
128 * they are (always ?) in little endian format
130 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
133 while (offset && level < EXIF_TIFF_MAX_LEVELS)
135 offset = canon_cr2_tiff_table(data, len, offset, bo, &jpeg_offset);
138 if (jpeg_offset != 0)
140 if (image_offset) *image_offset = jpeg_offset;
148 #define CRW_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
149 #define CRW_HEADER_SIZE 26
150 #define CRW_DIR_ENTRY_SIZE 10
152 gint format_canon_raw_crw(unsigned char *data, const guint len,
153 guint *image_offset, guint *exif_offset)
161 /* CRW header starts with 2 bytes for byte order (always "II", little endian),
162 * 4 bytes for start of root block,
163 * and 8 bytes of magic for file type and format "HEAPCCDR"
164 * (also 4 bytes for file version, and 8 bytes reserved)
166 * CIFF specification in pdf format is available on some websites,
167 * search for "CIFFspecV1R03.pdf" or "CIFFspecV1R04.pdf"
169 if (len < CRW_HEADER_SIZE ||
170 memcmp(data, "II", 2) != 0 ||
171 memcmp(data + 6, "HEAPCCDR", 8) != 0)
176 block_offset = exif_byte_get_int32(data + 2, CRW_BYTE_ORDER);
178 /* the end of the root block equals end of file,
179 * the last 4 bytes of the root block contain the block's data size
182 data_length = exif_byte_get_int32(data + offset, CRW_BYTE_ORDER);
184 offset = block_offset + data_length;
185 if (len < offset + 2) return FALSE;
187 /* number of directory entries for this block is in
188 * the next two bytes after the data for this block.
190 count = exif_byte_get_int16(data + offset, CRW_BYTE_ORDER);
192 if (len < offset + count * CRW_DIR_ENTRY_SIZE + 4) return FALSE;
194 /* walk the directory entries looking for type jpeg (tag 0x2007),
195 * for reference, other tags are 0x2005 for raw and 0x300a for photo info:
197 for (i = 0; i < count ; i++)
204 entry_offset = offset + i * CRW_DIR_ENTRY_SIZE;
206 /* entry is 10 bytes (in order):
208 * 4 for length of data
209 * 4 for offset into data segment of this block
211 record_type = exif_byte_get_int16(data + entry_offset, CRW_BYTE_ORDER);
212 record_length = exif_byte_get_int32(data + entry_offset + 2, CRW_BYTE_ORDER);
213 record_offset = exif_byte_get_int32(data + entry_offset + 6, CRW_BYTE_ORDER);
215 /* tag we want for jpeg data */
216 if (record_type == 0x2007)
220 jpeg_offset = block_offset + record_offset;
221 if (len < jpeg_offset + record_length ||
223 memcmp(data + jpeg_offset, "\xff\xd8\xff\xdb", 4) != 0)
228 /* we now know offset and verified jpeg */
229 *image_offset = jpeg_offset;
239 *-----------------------------------------------------------------------------
240 * EXIF Makernote for Canon
241 *-----------------------------------------------------------------------------
244 static ExifTextList CanonSet1MacroMode[] = {
250 static ExifTextList CanonSet1Quality[] = {
258 static ExifTextList CanonSet1FlashMode[] = {
259 { 0, "flash not fired" },
262 { 3, "red-eye reduction" },
264 { 5, "auto + red-eye reduction" },
265 { 6, "on + red-eye reduction" },
266 { 16, "external flash" },
270 static ExifTextList CanonSet1DriveMode[] = {
271 { 0, "single or timer" },
276 static ExifTextList CanonSet1FocusMode[] = {
277 { 0, "one-shot AF" },
278 { 1, "AI servo AF" },
279 { 2, "AI focus AF" },
287 static ExifTextList CanonSet1ImageSize[] = {
291 /* where (or) does Medium 1/2 fit in here ? */
295 static ExifTextList CanonSet1ShootingMode[] = {
299 { 3, "fast shutter" },
300 { 4, "slow shutter" },
302 { 6, "black and white" },
311 /* Don't think this is interpreted correctly/completely, A60 at 2.5x Digital sets value of 3 */
312 static ExifTextList CanonSet1DigitalZoom[] = {
320 static ExifTextList CanonSet1ConSatSharp[] = {
327 static ExifTextList CanonSet1ISOSpeed[] = {
328 /* { 0, "not set/see EXIF tag" }, */
337 static ExifTextList CanonSet1MeteringMode[] = {
342 { 5, "center-weighted" },
346 static ExifTextList CanonSet1FocusType[] = {
356 static ExifTextList CanonSet1AutoFocusPoint[] = {
357 { 0x2005, "manual AF point selection" },
358 { 0x3000, "manual focus" },
361 { 0x3003, "center" },
363 { 0x4001, "auto AF point selection" },
367 static ExifTextList CanonSet1ExposureMode[] = {
370 { 2, "Tv priority" },
371 { 3, "Av priority" },
377 static ExifTextList CanonSet1FlashFired[] = {
383 static ExifTextList CanonSet1FocusCont[] = {
384 { 0, "no (single)" },
389 static ExifMarker CanonSet1[] = {
390 /* 0 is length of array in bytes (2 x array size) */
391 { 1, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MacroMode", "Macro mode", CanonSet1MacroMode },
392 { 2, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SelfTimer", "Self timer (10ths of second)", NULL },
393 { 3, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Quality", "Quality", CanonSet1Quality },
394 { 4, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashMode", "Flash mode", CanonSet1FlashMode },
395 { 5, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DriveMode", "Drive mode", CanonSet1DriveMode },
396 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusMode", "Focus mode", CanonSet1FocusMode },
397 { 10, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ImageSize", "Image size", CanonSet1ImageSize },
398 { 11, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ShootingMode","Shooting mode", CanonSet1ShootingMode },
399 { 11, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureProgram", "ExposureProgram", CanonSet1ShootingMode },
400 { 12, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DigitalZoom", "Digital zoom", CanonSet1DigitalZoom },
401 { 13, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Contrast", "Contrast", CanonSet1ConSatSharp },
402 { 14, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Saturation", "Saturation", CanonSet1ConSatSharp },
403 { 15, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Sharpness", "Sharpness", CanonSet1ConSatSharp },
404 { 16, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ISOSpeed", "ISO speed", CanonSet1ISOSpeed },
405 { 16, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ISOSpeedRatings", "ISO speed", CanonSet1ISOSpeed },
406 { 17, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MeteringMode","Metering mode", CanonSet1MeteringMode },
407 { 18, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusType", "Focus type", CanonSet1FocusType },
408 { 19, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AutoFocus", "AutoFocus point", CanonSet1AutoFocusPoint },
409 { 20, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ExposureMode","Exposure mode", CanonSet1ExposureMode },
410 { 20, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureMode", "Exposure mode", CanonSet1ExposureMode },
411 { 23, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthLong","Long focal length", NULL },
412 { 24, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthShort","Short focal length", NULL },
413 { 25, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthUnits","Focal units per mm", NULL },
414 { 28, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashFired", "Flash fired", CanonSet1FlashFired },
415 { 29, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashDetails","Flash details", NULL },
416 { 32, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ContinuousFocus","Continuous focus", CanonSet1FocusCont },
420 static ExifTextList CanonSet2WhiteBalance[] = {
425 { 4, "fluorescent" },
428 { 7, "black and white" },
431 { 14, "daylight fluorescent" },
432 { 17, "underwater" },
436 static ExifTextList CanonSet2FlashBias[] = {
457 static ExifMarker CanonSet2[] = {
458 /* 0 is length of array in bytes (2 x array size) */
459 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.WhiteBalance","White balance", CanonSet2WhiteBalance },
460 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "LightSource", "White balance", CanonSet2WhiteBalance },
461 { 9, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SequenceNumber","Sequence number", NULL },
462 { 15, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashBias", "Flash bias", CanonSet2FlashBias },
463 /* distance needs more than just this (metric) value */
464 { 19, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SubjectDistance", "Subject Distance", NULL },
470 static ExifTextList CanonCustomEnable[] = {
476 static ExifTextList CanonCustomEnableInvert[] = {
482 static ExifTextList CanonCustomExposureLevel[] = {
488 static ExifTextList CanonCustomAVShutterSpeed[] = {
490 { 1, "1/200 (fixed)" },
494 static ExifTextList CanonCustomShutterCurtainSync[] = {
500 static ExifMarker CanonCustom[] = {
501 { 1, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.NoiseReduction", "Noise reduction", CanonCustomEnable },
502 /*{ 2, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.BtnFuncShutter",
503 "Shutter/Auto exposure button function",CanonCustomBTNShutter }, */
504 { 3, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MirrorLockup", "Mirror lockup", CanonCustomEnable },
505 { 4, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.TvAvExposureLevel",
506 "Tv/Av and exposure level", CanonCustomExposureLevel },
507 { 5, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AFAssistLight", "AF assist light", CanonCustomEnableInvert },
508 { 6, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AvShutterSpeed",
509 "Shutter speed in Av mode", CanonCustomAVShutterSpeed },
510 /*{ 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AutoBracket",
511 "Auto-Exposure bracketting sequence/auto cancellation", CanonCustom }, */
512 { 8, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ShutterSync", "Shutter sync", CanonCustomShutterCurtainSync },
513 /* { 9, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.BtnFuncAF", "AF button function", CanonCustom }, */
514 { 10, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FillFlashReduction",
515 "Fill flash auto reduction", CanonCustomEnableInvert },
516 /*{ 11, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.BtnFuncMenu",
517 "Menu button function", CanonCustom }, */
518 /*{ 12, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.BtnFuncSet", "Set button function", CanonCustom }, */
519 { 13, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SensorCleaning", "Sensor cleaning", CanonCustomEnable },
525 static ExifMarker CanonExifMarkersList[] = {
526 { 1, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings1", NULL, NULL },
527 { 4, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings2", NULL, NULL },
528 { 6, EXIF_FORMAT_STRING, -1, "MkN.Canon.ImageType", "Image type", NULL },
529 { 7, EXIF_FORMAT_STRING, -1, "MkN.Canon.FirmwareVersion", "Firmware version", NULL },
530 { 8, EXIF_FORMAT_LONG_UNSIGNED, 1, "MkN.Canon.ImageNumber", "Image number", NULL },
531 { 9, EXIF_FORMAT_STRING, -1, "MkN.Canon.OwnerName", "Owner name", NULL },
532 { 12, EXIF_FORMAT_LONG_UNSIGNED, -1, "MkN.Canon.SerialNumber", "Camera serial number", NULL },
533 { 15, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.CustomFunctions", NULL, NULL },
537 static void canon_mknote_parse_settings(ExifData *exif,
538 guint16 *data, guint32 len, ExifByteOrder bo,
544 while (list[i].tag != 0)
546 if (list[i].tag < len)
550 item = exif_item_new(EXIF_FORMAT_SHORT_UNSIGNED, list[i].tag, 1, &list[i]);
551 exif_item_copy_data(item, &data[list[i].tag], 2, EXIF_FORMAT_SHORT_UNSIGNED, bo);
552 exif->items = g_list_prepend(exif->items, item);
560 static void canon_mknote_parse_convert(ExifData *exif)
565 /* seems we need more than only this value for distance */
566 if (exif_get_integer(exif, "MkN.Canon.SubjectDistance", &value))
568 static ExifMarker marker= { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,
569 "SubjectDistance", "Subject distance", NULL };
571 ExifRational *rational;
573 item = exif_item_new(marker.format, marker.tag, 1, &marker);
574 rational = item->data;
575 rational->num = value;
578 exif->items = g_list_prepend(exif->items, item);
581 result = exif_get_item(exif, "MkN.Canon.SerialNumber");
582 if (result && result->format == EXIF_FORMAT_LONG_UNSIGNED && result->data_len == 4)
584 static ExifMarker marker= { 12, EXIF_FORMAT_STRING, -1,
585 "SerialNumber", "Camera serial number", NULL };
591 n = (guint32)((guint32 *)(result->data))[0];
592 text = g_strdup_printf("%04X%05d", n & 0xffff0000 >> 8, n & 0x0000ffff);
593 l = strlen(text) + 1;
594 item = exif_item_new(marker.format, marker.tag, l, &marker);
595 memcpy(item->data, text, l);
598 exif->items = g_list_prepend(exif->items, item);
603 gint format_canon_makernote(ExifData *exif, unsigned char *tiff, guint offset,
604 guint size, ExifByteOrder bo)
608 if (exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, CanonExifMarkersList) != 0)
613 item = exif_get_item(exif, "MkN.Canon.Settings1");
616 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet1);
619 item = exif_get_item(exif, "MkN.Canon.Settings2");
622 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet2);
626 canon_mknote_parse_convert(exif);