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/
31 #include "format_canon.h"
32 #include "format_raw.h"
38 *-----------------------------------------------------------------------------
39 * Raw (CR2, CRW) embedded jpeg extraction for Canon
40 *-----------------------------------------------------------------------------
43 static gint canon_cr2_tiff_entry(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
44 guint *image_offset, gint *jpeg_encoding)
51 /* the two (tiff compliant) tags we want are:
52 * 0x0103 image compression type (must be type 6 for jpeg)
53 * 0x0111 jpeg start offset
54 * only use the first segment that contains an actual jpeg - as there
55 * is a another that contains the raw data.
57 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
58 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
59 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
61 /* tag 0x0103 contains the compression type for this segment's image data */
64 if (ExifFormatList[type].size * count == 2 &&
65 exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_DATA, bo) == 6)
67 *jpeg_encoding = TRUE;
72 /* find and verify jpeg offset */
74 !jpeg_encoding) return FALSE;
76 /* make sure data segment contains 4 bytes */
77 if (ExifFormatList[type].size * count != 4) return FALSE;
79 jpeg_start = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
81 /* verify this is jpeg data */
82 if (len < jpeg_start + 4 ||
83 memcmp(data + jpeg_start, "\xff\xd8", 2) != 0)
88 *image_offset = jpeg_start;
92 static gint canon_cr2_tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
95 gint jpeg_encoding = FALSE;
99 if (len < offset + 2) return 0;
101 count = exif_byte_get_int16(data + offset, bo);
103 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
105 for (i = 0; i < count; i++)
107 if (canon_cr2_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo,
108 image_offset, &jpeg_encoding))
114 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
117 gint format_canon_raw_cr2(unsigned char *data, const guint len,
118 guint *image_offset, guint *exif_offset)
120 guint jpeg_offset = 0;
125 /* cr2 files are tiff files with a few canon specific directory tags
126 * they are (always ?) in little endian format
128 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
131 while (offset && level < EXIF_TIFF_MAX_LEVELS)
133 offset = canon_cr2_tiff_table(data, len, offset, bo, &jpeg_offset);
136 if (jpeg_offset != 0)
138 if (image_offset) *image_offset = jpeg_offset;
146 #define CRW_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
147 #define CRW_HEADER_SIZE 26
148 #define CRW_DIR_ENTRY_SIZE 10
150 gint format_canon_raw_crw(unsigned char *data, const guint len,
151 guint *image_offset, guint *exif_offset)
159 /* CRW header starts with 2 bytes for byte order (always "II", little endian),
160 * 4 bytes for start of root block,
161 * and 8 bytes of magic for file type and format "HEAPCCDR"
162 * (also 4 bytes for file version, and 8 bytes reserved)
164 * CIFF specification in pdf format is available on some websites,
165 * search for "CIFFspecV1R03.pdf" or "CIFFspecV1R04.pdf"
167 if (len < CRW_HEADER_SIZE ||
168 memcmp(data, "II", 2) != 0 ||
169 memcmp(data + 6, "HEAPCCDR", 8) != 0)
174 block_offset = exif_byte_get_int32(data + 2, CRW_BYTE_ORDER);
176 /* the end of the root block equals end of file,
177 * the last 4 bytes of the root block contain the block's data size
180 data_length = exif_byte_get_int32(data + offset, CRW_BYTE_ORDER);
182 offset = block_offset + data_length;
183 if (len < offset + 2) return FALSE;
185 /* number of directory entries for this block is in
186 * the next two bytes after the data for this block.
188 count = exif_byte_get_int16(data + offset, CRW_BYTE_ORDER);
190 if (len < offset + count * CRW_DIR_ENTRY_SIZE + 4) return FALSE;
192 /* walk the directory entries looking for type jpeg (tag 0x2007),
193 * for reference, other tags are 0x2005 for raw and 0x300a for photo info:
195 for (i = 0; i < count ; i++)
202 entry_offset = offset + i * CRW_DIR_ENTRY_SIZE;
204 /* entry is 10 bytes (in order):
206 * 4 for length of data
207 * 4 for offset into data segment of this block
209 record_type = exif_byte_get_int16(data + entry_offset, CRW_BYTE_ORDER);
210 record_length = exif_byte_get_int32(data + entry_offset + 2, CRW_BYTE_ORDER);
211 record_offset = exif_byte_get_int32(data + entry_offset + 6, CRW_BYTE_ORDER);
213 /* tag we want for jpeg data */
214 if (record_type == 0x2007)
218 jpeg_offset = block_offset + record_offset;
219 if (len < jpeg_offset + record_length ||
221 memcmp(data + jpeg_offset, "\xff\xd8\xff\xdb", 4) != 0)
226 /* we now know offset and verified jpeg */
227 *image_offset = jpeg_offset;
237 *-----------------------------------------------------------------------------
238 * EXIF Makernote for Canon
239 *-----------------------------------------------------------------------------
242 static ExifTextList CanonSet1MacroMode[] = {
248 static ExifTextList CanonSet1Quality[] = {
256 static ExifTextList CanonSet1FlashMode[] = {
257 { 0, "flash not fired" },
260 { 3, "red-eye reduction" },
262 { 5, "auto + red-eye reduction" },
263 { 6, "on + red-eye reduction" },
264 { 16, "external flash" },
268 static ExifTextList CanonSet1DriveMode[] = {
269 { 0, "single or timer" },
274 static ExifTextList CanonSet1FocusMode[] = {
275 { 0, "one-shot AF" },
276 { 1, "AI servo AF" },
277 { 2, "AI focus AF" },
285 static ExifTextList CanonSet1ImageSize[] = {
289 /* where (or) does Medium 1/2 fit in here ? */
293 static ExifTextList CanonSet1ShootingMode[] = {
297 { 3, "fast shutter" },
298 { 4, "slow shutter" },
300 { 6, "black and white" },
309 /* Don't think this is interpreted correctly/completely, A60 at 2.5x Digital sets value of 3 */
310 static ExifTextList CanonSet1DigitalZoom[] = {
318 static ExifTextList CanonSet1ConSatSharp[] = {
325 static ExifTextList CanonSet1ISOSpeed[] = {
326 /* { 0, "not set/see EXIF tag" }, */
335 static ExifTextList CanonSet1MeteringMode[] = {
340 { 5, "center-weighted" },
344 static ExifTextList CanonSet1FocusType[] = {
354 static ExifTextList CanonSet1AutoFocusPoint[] = {
355 { 0x2005, "manual AF point selection" },
356 { 0x3000, "manual focus" },
359 { 0x3003, "center" },
361 { 0x4001, "auto AF point selection" },
365 static ExifTextList CanonSet1ExposureMode[] = {
368 { 2, "Tv priority" },
369 { 3, "Av priority" },
375 static ExifTextList CanonSet1FlashFired[] = {
381 static ExifTextList CanonSet1FocusCont[] = {
382 { 0, "no (single)" },
387 static ExifMarker CanonSet1[] = {
388 /* 0 is length of array in bytes (2 x array size) */
389 { 1, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MacroMode", "Macro mode", CanonSet1MacroMode },
390 { 2, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SelfTimer", "Self timer (10ths of second)", NULL },
391 { 3, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Quality", "Quality", CanonSet1Quality },
392 { 4, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashMode", "Flash mode", CanonSet1FlashMode },
393 { 5, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DriveMode", "Drive mode", CanonSet1DriveMode },
394 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusMode", "Focus mode", CanonSet1FocusMode },
395 { 10, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ImageSize", "Image size", CanonSet1ImageSize },
396 { 11, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ShootingMode","Shooting mode", CanonSet1ShootingMode },
397 { 11, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureProgram", "ExposureProgram", CanonSet1ShootingMode },
398 { 12, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.DigitalZoom", "Digital zoom", CanonSet1DigitalZoom },
399 { 13, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Contrast", "Contrast", CanonSet1ConSatSharp },
400 { 14, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Saturation", "Saturation", CanonSet1ConSatSharp },
401 { 15, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.Sharpness", "Sharpness", CanonSet1ConSatSharp },
402 { 16, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ISOSpeed", "ISO speed", CanonSet1ISOSpeed },
403 { 16, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ISOSpeedRatings", "ISO speed", CanonSet1ISOSpeed },
404 { 17, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MeteringMode","Metering mode", CanonSet1MeteringMode },
405 { 18, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocusType", "Focus type", CanonSet1FocusType },
406 { 19, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AutoFocus", "AutoFocus point", CanonSet1AutoFocusPoint },
407 { 20, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ExposureMode","Exposure mode", CanonSet1ExposureMode },
408 { 20, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureMode", "Exposure mode", CanonSet1ExposureMode },
409 { 23, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthLong","Long focal length", NULL },
410 { 24, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthShort","Short focal length", NULL },
411 { 25, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FocalLengthUnits","Focal units per mm", NULL },
412 { 28, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashFired", "Flash fired", CanonSet1FlashFired },
413 { 29, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashDetails","Flash details", NULL },
414 { 32, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ContinuousFocus","Continuous focus", CanonSet1FocusCont },
418 static ExifTextList CanonSet2WhiteBalance[] = {
423 { 4, "fluorescent" },
426 { 7, "black and white" },
429 { 14, "daylight fluorescent" },
430 { 17, "underwater" },
434 static ExifTextList CanonSet2FlashBias[] = {
455 static ExifMarker CanonSet2[] = {
456 /* 0 is length of array in bytes (2 x array size) */
457 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.WhiteBalance","White balance", CanonSet2WhiteBalance },
458 { 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "LightSource", "White balance", CanonSet2WhiteBalance },
459 { 9, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SequenceNumber","Sequence number", NULL },
460 { 15, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FlashBias", "Flash bias", CanonSet2FlashBias },
461 /* distance needs more than just this (metric) value */
462 { 19, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SubjectDistance", "Subject Distance", NULL },
468 static ExifTextList CanonCustomEnable[] = {
474 static ExifTextList CanonCustomEnableInvert[] = {
480 static ExifTextList CanonCustomExposureLevel[] = {
486 static ExifTextList CanonCustomAVShutterSpeed[] = {
488 { 1, "1/200 (fixed)" },
492 static ExifTextList CanonCustomShutterCurtainSync[] = {
498 static ExifMarker CanonCustom[] = {
499 { 1, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.NoiseReduction", "Noise reduction", CanonCustomEnable },
500 /*{ 2, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.BtnFuncShutter",
501 "Shutter/Auto exposure button function",CanonCustomBTNShutter }, */
502 { 3, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.MirrorLockup", "Mirror lockup", CanonCustomEnable },
503 { 4, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.TvAvExposureLevel",
504 "Tv/Av and exposure level", CanonCustomExposureLevel },
505 { 5, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AFAssistLight", "AF assist light", CanonCustomEnableInvert },
506 { 6, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AvShutterSpeed",
507 "Shutter speed in Av mode", CanonCustomAVShutterSpeed },
508 /*{ 7, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.AutoBracket",
509 "Auto-Exposure bracketting sequence/auto cancellation", CanonCustom }, */
510 { 8, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.ShutterSync", "Shutter sync", CanonCustomShutterCurtainSync },
511 /* { 9, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.BtnFuncAF", "AF button function", CanonCustom }, */
512 { 10, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.FillFlashReduction",
513 "Fill flash auto reduction", CanonCustomEnableInvert },
514 /*{ 11, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.BtnFuncMenu",
515 "Menu button function", CanonCustom }, */
516 /*{ 12, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.BtnFuncSet", "Set button function", CanonCustom }, */
517 { 13, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MkN.Canon.SensorCleaning", "Sensor cleaning", CanonCustomEnable },
523 static ExifMarker CanonExifMarkersList[] = {
524 { 1, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings1", NULL, NULL },
525 { 4, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.Settings2", NULL, NULL },
526 { 6, EXIF_FORMAT_STRING, -1, "MkN.Canon.ImageType", "Image type", NULL },
527 { 7, EXIF_FORMAT_STRING, -1, "MkN.Canon.FirmwareVersion", "Firmware version", NULL },
528 { 8, EXIF_FORMAT_LONG_UNSIGNED, 1, "MkN.Canon.ImageNumber", "Image number", NULL },
529 { 9, EXIF_FORMAT_STRING, -1, "MkN.Canon.OwnerName", "Owner name", NULL },
530 { 12, EXIF_FORMAT_LONG_UNSIGNED, -1, "MkN.Canon.SerialNumber", "Camera serial number", NULL },
531 { 15, EXIF_FORMAT_SHORT_UNSIGNED, -1, "MkN.Canon.CustomFunctions", NULL, NULL },
535 static void canon_mknote_parse_settings(ExifData *exif,
536 guint16 *data, guint32 len, ExifByteOrder bo,
542 while (list[i].tag != 0)
544 if (list[i].tag < len)
548 item = exif_item_new(EXIF_FORMAT_SHORT_UNSIGNED, list[i].tag, 1, &list[i]);
549 exif_item_copy_data(item, &data[list[i].tag], 2, EXIF_FORMAT_SHORT_UNSIGNED, bo);
550 exif->items = g_list_prepend(exif->items, item);
558 static void canon_mknote_parse_convert(ExifData *exif)
563 /* seems we need more than only this value for distance */
564 if (exif_get_integer(exif, "MkN.Canon.SubjectDistance", &value))
566 static ExifMarker marker= { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,
567 "SubjectDistance", "Subject distance", NULL };
569 ExifRational *rational;
571 item = exif_item_new(marker.format, marker.tag, 1, &marker);
572 rational = item->data;
573 rational->num = value;
576 exif->items = g_list_prepend(exif->items, item);
579 result = exif_get_item(exif, "MkN.Canon.SerialNumber");
580 if (result && result->format == EXIF_FORMAT_LONG_UNSIGNED && result->data_len == 4)
582 static ExifMarker marker= { 12, EXIF_FORMAT_STRING, -1,
583 "SerialNumber", "Camera serial number", NULL };
589 n = (guint32)((guint32 *)(result->data))[0];
590 text = g_strdup_printf("%04X%05d", n & 0xffff0000 >> 8, n & 0x0000ffff);
591 l = strlen(text) + 1;
592 item = exif_item_new(marker.format, marker.tag, l, &marker);
593 memcpy(item->data, text, l);
596 exif->items = g_list_prepend(exif->items, item);
601 gint format_canon_makernote(ExifData *exif, unsigned char *tiff, guint offset,
602 guint size, ExifByteOrder bo)
606 if (exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, CanonExifMarkersList) != 0)
611 item = exif_get_item(exif, "MkN.Canon.Settings1");
614 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet1);
617 item = exif_get_item(exif, "MkN.Canon.Settings2");
620 canon_mknote_parse_settings(exif, item->data, item->data_len, bo, CanonSet2);
624 canon_mknote_parse_convert(exif);