6 * Raw NEF jpeg extraction based on nefextract.c by Joseph Heled,
7 * in addition nefextract.c is based on dcraw by Dave Coffin.
9 * This software is released under the GNU General Public License (GNU GPL).
10 * Please read the included file COPYING for more information.
11 * This software comes with no warranty of any kind, use at your own risk!
27 #include "format_nikon.h"
33 *-----------------------------------------------------------------------------
34 * Raw NEF embedded jpeg extraction for Nikon
35 *-----------------------------------------------------------------------------
38 static guint nikon_tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
40 guint *image_offset, guint *jpeg_len);
43 static void nikon_tiff_entry(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
45 guint *image_offset, guint *image_length, guint *jpeg_start, guint *jpeg_len)
53 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
54 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
55 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
57 /* so far, we only care about tags with type long */
58 if (type != EXIF_FORMAT_LONG_UNSIGNED && type != EXIF_FORMAT_LONG) return;
60 seg_len = ExifFormatList[type].size * count;
63 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
64 if (segment + seg_len > len) return;
68 segment = offset + EXIF_TIFD_OFFSET_DATA;
76 for (i = 0; i < count; i++)
80 subset = exif_byte_get_int32(data + segment + i * 4, bo);
81 nikon_tiff_table(data, len, subset, bo, level + 1, image_offset, image_length);
85 else if (tag == 0x201)
87 /* jpeg data start offset */
88 *jpeg_start = exif_byte_get_int32(data + segment, bo);
90 else if (tag == 0x202)
92 /* jpeg data length */
93 *jpeg_len = exif_byte_get_int32(data + segment, bo);
97 static guint nikon_tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
99 guint *image_offset, guint *image_length)
103 guint jpeg_start = 0;
106 /* limit damage from infinite loops */
107 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
109 if (len < offset + 2) return FALSE;
111 count = exif_byte_get_int16(data + offset, bo);
113 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
115 for (i = 0; i < count; i++)
117 nikon_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level,
118 image_offset, image_length, &jpeg_start, &jpeg_len);
121 if (jpeg_start > 0 &&
122 jpeg_len > *image_length)
124 *image_offset = jpeg_start;
125 *image_length = jpeg_len;
128 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
131 gint format_nikon_raw(unsigned char *data, const guint len,
132 guint *image_offset, guint *exif_offset)
140 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
143 while (offset && level < EXIF_TIFF_MAX_LEVELS)
145 offset = nikon_tiff_table(data, len, offset, bo, 0, &i_off, &i_len);
151 if (image_offset) *image_offset = i_off;
160 *-----------------------------------------------------------------------------
161 * EXIF Makernote for Nikon
162 *-----------------------------------------------------------------------------
165 static ExifTextList NikonTagQuality[]= {
170 { 5, "SXGA normal" },
172 { 7, "XGA basic (?)" },
173 { 8, "XGA normal (?)" },
174 { 9, "XGA fine (?)" },
175 { 10, "UXGA basic" },
176 { 11, "UXGA normal" },
181 static ExifTextList NikonTagColorMode[]= {
187 static ExifTextList NikonTagImgAdjust[]= {
196 static ExifTextList NikonTagISOSensitivity[]= {
204 static ExifTextList NikonTagWhiteBalance[]= {
208 { 3, "incandescent" },
209 { 4, "fluorescence" },
215 static ExifTextList NikonTagConverter[]= {
222 static ExifTextList NikonTag[]= {
229 static ExifMarker NikonExifMarkersList1[] = {
230 { 0x0002, EXIF_FORMAT_STRING, 6, "Nikon.unknown", NULL, NULL },
231 { 0x0003, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Quality", "Quality", NikonTagQuality },
232 { 0x0004, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ColorMode", "Color mode", NikonTagColorMode },
233 { 0x0005, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ImageAdjustment",
234 "Image adjustment", NikonTagImgAdjust },
235 { 0x0006, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ISOSensitivity",
236 "ISO sensitivity", NikonTagISOSensitivity },
237 { 0x0007, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.WhiteBalance", "White balance",NikonTagWhiteBalance },
238 { 0x0008, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Nikon.Focus", "Focus", NULL },
239 { 0x000a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Nikon.DigitalZoom", "Digital zoom", NULL },
240 { 0x000b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Converter", "Converter", NikonTagConverter },
244 static ExifTextList NikonTag2FlashComp[]= {
265 static ExifTextList NikonTag2LensType[]= {
268 { 2, "AF-D or AF-s" },
274 static ExifTextList NikonTag2FlashUsed[]= {
276 { 4, "unit unknown" },
283 static ExifTextList NikonTagi2Saturation[]= {
284 { -3, "black and white" },
294 static ExifMarker NikonExifMarkersList2[] = {
295 { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Nikon.ISOSpeed", "ISO speed", NULL },
296 { 0x0003, EXIF_FORMAT_STRING, -1, "Nikon.ColorMode", "Color mode", NULL },
297 { 0x0004, EXIF_FORMAT_STRING, -1, "Nikon.Quality", "Quality", NULL },
298 { 0x0005, EXIF_FORMAT_STRING, -1, "Nikon.WhiteBalance", "White balance",NULL },
299 { 0x0006, EXIF_FORMAT_STRING, -1, "Nikon.Sharpening", "Sharpening", NULL },
300 { 0x0007, EXIF_FORMAT_STRING, -1, "Nikon.FocusMode", "Focus mode", NULL },
301 { 0x0008, EXIF_FORMAT_STRING, -1, "Nikon.FlashSetting", "Flash setting",NULL },
302 { 0x0009, EXIF_FORMAT_STRING, -1, "Nikon.AutoFlashMode","Auto flash mode",NULL },
303 { 0x000b, EXIF_FORMAT_SHORT, 1, "Nikon.WhiteBalanceBias",
304 "White balance bias value", NULL },
305 /* { 0x000c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.WhiteBalanceRB",
306 "White balance red/blue coefficients", NULL }, */
307 /* { 0x000f, EXIF_FORMAT_STRING, -1, "Nikon.ISOSelect", "ISO selection",NULL }, */
308 { 0x0012, EXIF_FORMAT_UNDEFINED, 4, "Nikon.FlashCompensation",
309 "Flash compensation", NikonTag2FlashComp },
310 { 0x0013, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Nikon.ISOSpeedRequest",
311 "ISO speed requested", NULL },
312 { 0x0016, EXIF_FORMAT_SHORT_UNSIGNED, 4, "Nikon.CornerCoord",
313 "Corner coordinates", NULL },
314 { 0x0018, EXIF_FORMAT_UNDEFINED, 4, "Nikon.FlashBracketCompensation",
315 "Flash bracket compensation", NikonTag2FlashComp },
316 { 0x0019, EXIF_FORMAT_RATIONAL, 1, "Nikon.AEBracketCompensation",
317 "AE bracket compensation", NULL },
318 { 0x0080, EXIF_FORMAT_STRING, -1, "Nikon.ImageAdjustment",
319 "Image adjustment", NULL },
320 { 0x0081, EXIF_FORMAT_STRING, -1, "Nikon.Contrast", "Contrast", NULL },
321 { 0x0082, EXIF_FORMAT_STRING, -1, "Nikon.AuxLens", "Aux lens adapter", NULL },
322 { 0x0083, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Nikon.LensType", "Lens type", NikonTag2LensType },
323 { 0x0084, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Nikon.LensFocalLength",
324 "Lens min/max focal length and aperture", NULL },
325 { 0x0085, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ManualFocusDistance",
326 "Manual focus distance", NULL },
327 { 0x0086, EXIF_FORMAT_RATIONAL, 1, "Nikon.DigitalZoomFactor",
328 "Digital zoom factor", NULL },
329 { 0x0087, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Nikon.FlashUsed", "Flash used", NikonTag2FlashUsed },
330 { 0x0088, EXIF_FORMAT_UNDEFINED, 4, "Nikon.AutoFocusArea","Auto focus area",NULL },
331 /* { 0x0089, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Nikon.Bracket/ShootingMode", NULL, NULL }, */
332 { 0x008d, EXIF_FORMAT_STRING, -1, "Nikon.ColorMode", "Color mode", NULL },
333 { 0x008f, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.SceneMode", "Scene mode", NULL },
334 { 0x0090, EXIF_FORMAT_STRING, -1, "Nikon.LightingType", "Lighting type",NULL },
335 { 0x0092, EXIF_FORMAT_SHORT, 1, "Nikon.HueAdjust", "Hue adjustment",NULL },
336 /* { 0x0094, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Saturation", "Saturation", NikonTag2Saturation }, */
337 { 0x0095, EXIF_FORMAT_STRING, -1, "Nikon.NoiseReduction", "Noise reduction", NULL },
338 { 0x00a7, EXIF_FORMAT_LONG_UNSIGNED, 1, "Nikon.ShutterCount", "Shutter release count", NULL },
339 { 0x00a9, EXIF_FORMAT_STRING, -1, "Nikon.ImageOptimization", "Image optimization", NULL },
340 { 0x00aa, EXIF_FORMAT_STRING, -1, "Nikon.Saturation", "Saturation", NULL },
341 { 0x00ab, EXIF_FORMAT_STRING, -1, "Nikon.DigitalVariProg", "Digital Vari-program", NULL },
345 static ExifTextList NikonAFPoint[]= {
355 gint format_nikon_makernote(ExifData *exif, unsigned char *tiff, guint offset,
356 guint size, ExifByteOrder bo)
361 if (offset + 8 + 4 >= size) return FALSE;
363 data = tiff + offset;
365 /* Nikon tag format 1 */
366 if (memcmp(data, "Nikon\x00\x01\x00", 8) == 0)
368 if (exif_parse_IFD_table(exif, tiff, offset + 8, size,
369 bo, 0, NikonExifMarkersList1) != 0)
376 /* Nikon tag format 2 uses Embedded tiff header */
377 if (memcmp(data, "Nikon\x00\x02\x00\x00\x00", 10) == 0 ||
378 memcmp(data, "Nikon\x00\x02\x10\x00\x00", 10) == 0)
382 tiff_header = offset + 10;
383 if (exif_tiff_parse(exif, tiff + tiff_header, size - tiff_header,
384 NikonExifMarkersList2) != 0)
389 /* Nikon tag format 3 uses format 2 tags without "Nikon" and tiff header */
390 else if (exif_parse_IFD_table(exif, tiff, offset, size,
391 bo, 0, NikonExifMarkersList2) != 0)
396 item = exif_get_item(exif, "Nikon.AutoFocusArea");
397 if (item && item->data_len == 4 * sizeof(guchar))
399 static ExifMarker marker = { 0x0088, EXIF_FORMAT_STRING, -1,
400 "Nikon.AutoFocusPoint", "Auto focus point", NULL };
401 guchar *array = item->data;
404 text = exif_text_list_find_value(NikonAFPoint, (gint)array[1]);
409 l = strlen(text) + 1;
410 item = exif_item_new(marker.format, marker.tag, l, &marker);
411 memcpy(item->data, text, l);
413 exif->items = g_list_prepend(exif->items, item);
417 item = exif_get_item(exif, "Nikon.ISOSpeed");
418 if (item && item->data_len == 2 * 2)
420 static ExifMarker marker = { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 1,
421 "ISOSpeedRatings", "ISO speed", NULL };
424 shadow = exif_item_new(marker.format, marker.tag, 1, &marker);
425 memcpy(shadow->data, item->data + 2, 2);
427 exif->items = g_list_prepend(exif->items, shadow);
430 item = exif_get_item(exif, "Nikon.WhiteBalance");
431 if (item && item->format == EXIF_FORMAT_STRING)
433 static ExifMarker marker = { 0x0005, EXIF_FORMAT_STRING, -1,
434 "LightSource", "Light source", NULL };
437 shadow = exif_item_new(marker.format, marker.tag, item->data_len, &marker);
438 memcpy(shadow->data, item->data, item->data_len);
440 exif->items = g_list_prepend(exif->items, shadow);