4 * Copyright (C) 2008 - 2012 The Geeqie Team
7 * Raw NEF jpeg extraction based on nefextract.c by Joseph Heled,
8 * in addition nefextract.c is based on dcraw by Dave Coffin.
10 * This software is released under the GNU General Public License (GNU GPL).
11 * Please read the included file COPYING for more information.
12 * This software comes with no warranty of any kind, use at your own risk!
30 #include "format_nikon.h"
36 *-----------------------------------------------------------------------------
37 * Raw NEF embedded jpeg extraction for Nikon
38 *-----------------------------------------------------------------------------
41 static guint nikon_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
43 guint *image_offset, guint *jpeg_len);
46 static void nikon_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
48 guint *image_offset, guint *image_length, guint *jpeg_start, guint *jpeg_len)
56 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
57 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
58 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
60 /* so far, we only care about tags with type long */
61 if (type != EXIF_FORMAT_LONG_UNSIGNED && type != EXIF_FORMAT_LONG) return;
63 seg_len = ExifFormatList[type].size * count;
66 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
67 if (segment + seg_len > len) return;
71 segment = offset + EXIF_TIFD_OFFSET_DATA;
79 for (i = 0; i < count; i++)
83 subset = exif_byte_get_int32(data + segment + i * 4, bo);
84 nikon_tiff_table(data, len, subset, bo, level + 1, image_offset, image_length);
88 else if (tag == 0x201)
90 /* jpeg data start offset */
91 *jpeg_start = exif_byte_get_int32(data + segment, bo);
93 else if (tag == 0x202)
95 /* jpeg data length */
96 *jpeg_len = exif_byte_get_int32(data + segment, bo);
100 static guint nikon_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
102 guint *image_offset, guint *image_length)
106 guint jpeg_start = 0;
109 /* limit damage from infinite loops */
110 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
112 if (len < offset + 2) return FALSE;
114 count = exif_byte_get_int16(data + offset, bo);
116 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
118 for (i = 0; i < count; i++)
120 nikon_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level,
121 image_offset, image_length, &jpeg_start, &jpeg_len);
124 if (jpeg_start > 0 &&
125 jpeg_len > *image_length)
127 *image_offset = jpeg_start;
128 *image_length = jpeg_len;
131 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
134 gboolean format_nikon_raw(guchar *data, const guint len,
135 guint *image_offset, guint *exif_offset)
143 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
146 while (offset && level < EXIF_TIFF_MAX_LEVELS)
148 offset = nikon_tiff_table(data, len, offset, bo, 0, &i_off, &i_len);
154 if (image_offset) *image_offset = i_off;
163 *-----------------------------------------------------------------------------
164 * EXIF Makernote for Nikon
165 *-----------------------------------------------------------------------------
168 static ExifTextList NikonTagQuality[]= {
173 { 5, "SXGA normal" },
175 { 7, "XGA basic (?)" },
176 { 8, "XGA normal (?)" },
177 { 9, "XGA fine (?)" },
178 { 10, "UXGA basic" },
179 { 11, "UXGA normal" },
184 static ExifTextList NikonTagColorMode[]= {
190 static ExifTextList NikonTagImgAdjust[]= {
199 static ExifTextList NikonTagISOSensitivity[]= {
207 static ExifTextList NikonTagWhiteBalance[]= {
211 { 3, "incandescent" },
212 { 4, "fluorescence" },
218 static ExifTextList NikonTagConverter[]= {
225 static ExifTextList NikonTag[]= {
232 static ExifMarker NikonExifMarkersList1[] = {
233 { 0x0002, EXIF_FORMAT_STRING, 6, "Nikon.unknown", NULL, NULL },
234 { 0x0003, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Quality", "Quality", NikonTagQuality },
235 { 0x0004, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ColorMode", "Color mode", NikonTagColorMode },
236 { 0x0005, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ImageAdjustment",
237 "Image adjustment", NikonTagImgAdjust },
238 { 0x0006, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ISOSensitivity",
239 "ISO sensitivity", NikonTagISOSensitivity },
240 { 0x0007, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.WhiteBalance", "White balance",NikonTagWhiteBalance },
241 { 0x0008, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Nikon.Focus", "Focus", NULL },
242 { 0x000a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Nikon.DigitalZoom", "Digital zoom", NULL },
243 { 0x000b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Converter", "Converter", NikonTagConverter },
247 static ExifTextList NikonTag2FlashComp[]= {
268 static ExifTextList NikonTag2LensType[]= {
271 { 2, "AF-D or AF-s" },
277 static ExifTextList NikonTag2FlashUsed[]= {
279 { 4, "unit unknown" },
286 static ExifTextList NikonTagi2Saturation[]= {
287 { -3, "black and white" },
297 static ExifMarker NikonExifMarkersList2[] = {
298 { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Nikon.ISOSpeed", "ISO speed", NULL },
299 { 0x0003, EXIF_FORMAT_STRING, -1, "Nikon.ColorMode", "Color mode", NULL },
300 { 0x0004, EXIF_FORMAT_STRING, -1, "Nikon.Quality", "Quality", NULL },
301 { 0x0005, EXIF_FORMAT_STRING, -1, "Nikon.WhiteBalance", "White balance",NULL },
302 { 0x0006, EXIF_FORMAT_STRING, -1, "Nikon.Sharpening", "Sharpening", NULL },
303 { 0x0007, EXIF_FORMAT_STRING, -1, "Nikon.FocusMode", "Focus mode", NULL },
304 { 0x0008, EXIF_FORMAT_STRING, -1, "Nikon.FlashSetting", "Flash setting",NULL },
305 { 0x0009, EXIF_FORMAT_STRING, -1, "Nikon.AutoFlashMode","Auto flash mode",NULL },
306 { 0x000b, EXIF_FORMAT_SHORT, 1, "Nikon.WhiteBalanceBias",
307 "White balance bias value", NULL },
308 /* { 0x000c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.WhiteBalanceRB",
309 "White balance red/blue coefficients", NULL }, */
310 /* { 0x000f, EXIF_FORMAT_STRING, -1, "Nikon.ISOSelect", "ISO selection",NULL }, */
311 { 0x0012, EXIF_FORMAT_UNDEFINED, 4, "Nikon.FlashCompensation",
312 "Flash compensation", NikonTag2FlashComp },
313 { 0x0013, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Nikon.ISOSpeedRequest",
314 "ISO speed requested", NULL },
315 { 0x0016, EXIF_FORMAT_SHORT_UNSIGNED, 4, "Nikon.CornerCoord",
316 "Corner coordinates", NULL },
317 { 0x0018, EXIF_FORMAT_UNDEFINED, 4, "Nikon.FlashBracketCompensation",
318 "Flash bracket compensation", NikonTag2FlashComp },
319 { 0x0019, EXIF_FORMAT_RATIONAL, 1, "Nikon.AEBracketCompensation",
320 "AE bracket compensation", NULL },
321 { 0x0080, EXIF_FORMAT_STRING, -1, "Nikon.ImageAdjustment",
322 "Image adjustment", NULL },
323 { 0x0081, EXIF_FORMAT_STRING, -1, "Nikon.Contrast", "Contrast", NULL },
324 { 0x0082, EXIF_FORMAT_STRING, -1, "Nikon.AuxLens", "Aux lens adapter", NULL },
325 { 0x0083, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Nikon.LensType", "Lens type", NikonTag2LensType },
326 { 0x0084, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Nikon.LensFocalLength",
327 "Lens min/max focal length and aperture", NULL },
328 { 0x0085, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ManualFocusDistance",
329 "Manual focus distance", NULL },
330 { 0x0086, EXIF_FORMAT_RATIONAL, 1, "Nikon.DigitalZoomFactor",
331 "Digital zoom factor", NULL },
332 { 0x0087, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Nikon.FlashUsed", "Flash used", NikonTag2FlashUsed },
333 { 0x0088, EXIF_FORMAT_UNDEFINED, 4, "Nikon.AutoFocusArea","Auto focus area",NULL },
334 /* { 0x0089, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Nikon.Bracket/ShootingMode", NULL, NULL }, */
335 { 0x008d, EXIF_FORMAT_STRING, -1, "Nikon.ColorMode", "Color mode", NULL },
336 { 0x008f, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.SceneMode", "Scene mode", NULL },
337 { 0x0090, EXIF_FORMAT_STRING, -1, "Nikon.LightingType", "Lighting type",NULL },
338 { 0x0092, EXIF_FORMAT_SHORT, 1, "Nikon.HueAdjust", "Hue adjustment",NULL },
339 /* { 0x0094, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Saturation", "Saturation", NikonTag2Saturation }, */
340 { 0x0095, EXIF_FORMAT_STRING, -1, "Nikon.NoiseReduction", "Noise reduction", NULL },
341 { 0x00a7, EXIF_FORMAT_LONG_UNSIGNED, 1, "Nikon.ShutterCount", "Shutter release count", NULL },
342 { 0x00a9, EXIF_FORMAT_STRING, -1, "Nikon.ImageOptimization", "Image optimization", NULL },
343 { 0x00aa, EXIF_FORMAT_STRING, -1, "Nikon.Saturation", "Saturation", NULL },
344 { 0x00ab, EXIF_FORMAT_STRING, -1, "Nikon.DigitalVariProg", "Digital Vari-program", NULL },
348 static ExifTextList NikonAFPoint[]= {
358 gboolean format_nikon_makernote(ExifData *exif, guchar *tiff, guint offset,
359 guint size, ExifByteOrder bo)
364 if (offset + 8 + 4 >= size) return FALSE;
366 data = tiff + offset;
368 /* Nikon tag format 1 */
369 if (memcmp(data, "Nikon\x00\x01\x00", 8) == 0)
371 if (exif_parse_IFD_table(exif, tiff, offset + 8, size,
372 bo, 0, NikonExifMarkersList1) != 0)
379 /* Nikon tag format 2 uses Embedded tiff header */
380 if (memcmp(data, "Nikon\x00\x02\x00\x00\x00", 10) == 0 ||
381 memcmp(data, "Nikon\x00\x02\x10\x00\x00", 10) == 0)
385 tiff_header = offset + 10;
386 if (exif_tiff_parse(exif, tiff + tiff_header, size - tiff_header,
387 NikonExifMarkersList2) != 0)
392 /* Nikon tag format 3 uses format 2 tags without "Nikon" and tiff header */
393 else if (exif_parse_IFD_table(exif, tiff, offset, size,
394 bo, 0, NikonExifMarkersList2) != 0)
399 item = exif_get_item(exif, "Nikon.AutoFocusArea");
400 if (item && item->data_len == 4 * sizeof(guchar))
402 static ExifMarker marker = { 0x0088, EXIF_FORMAT_STRING, -1,
403 "Nikon.AutoFocusPoint", "Auto focus point", NULL };
404 guchar *array = item->data;
408 text = exif_text_list_find_value(NikonAFPoint, (gint)array[1]);
409 l = strlen(text) + 1;
411 item = exif_item_new(marker.format, marker.tag, l, &marker);
412 memcpy(item->data, text, l);
416 exif->items = g_list_prepend(exif->items, item);
419 item = exif_get_item(exif, "Nikon.ISOSpeed");
420 if (item && item->data_len == 2 * 2)
422 static ExifMarker marker = { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 1,
423 "ISOSpeedRatings", "ISO speed", NULL };
426 shadow = exif_item_new(marker.format, marker.tag, 1, &marker);
427 memcpy(shadow->data, item->data + 2, 2);
429 exif->items = g_list_prepend(exif->items, shadow);
432 item = exif_get_item(exif, "Nikon.WhiteBalance");
433 if (item && item->format == EXIF_FORMAT_STRING)
435 static ExifMarker marker = { 0x0005, EXIF_FORMAT_STRING, -1,
436 "LightSource", "Light source", NULL };
439 shadow = exif_item_new(marker.format, marker.tag, item->data_len, &marker);
440 memcpy(shadow->data, item->data, item->data_len);
442 exif->items = g_list_prepend(exif->items, shadow);
450 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */