2 * Copyright (C) 2005 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #include "format_nikon.h"
43 *-----------------------------------------------------------------------------
44 * Raw NEF embedded jpeg extraction for Nikon
45 *-----------------------------------------------------------------------------
48 static guint nikon_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
50 guint *image_offset, guint *jpeg_len);
53 static void nikon_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
55 guint *image_offset, guint *image_length, guint *jpeg_start, guint *jpeg_len)
63 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
64 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
65 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
67 /* so far, we only care about tags with type long */
68 if (type != EXIF_FORMAT_LONG_UNSIGNED && type != EXIF_FORMAT_LONG) return;
70 seg_len = ExifFormatList[type].size * count;
73 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
74 if (segment + seg_len > len) return;
78 segment = offset + EXIF_TIFD_OFFSET_DATA;
86 for (i = 0; i < count; i++)
90 subset = exif_byte_get_int32(data + segment + i * 4, bo);
91 nikon_tiff_table(data, len, subset, bo, level + 1, image_offset, image_length);
95 else if (tag == 0x201)
97 /* jpeg data start offset */
98 *jpeg_start = exif_byte_get_int32(data + segment, bo);
100 else if (tag == 0x202)
102 /* jpeg data length */
103 *jpeg_len = exif_byte_get_int32(data + segment, bo);
107 static guint nikon_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
109 guint *image_offset, guint *image_length)
113 guint jpeg_start = 0;
116 /* limit damage from infinite loops */
117 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
119 if (len < offset + 2) return FALSE;
121 count = exif_byte_get_int16(data + offset, bo);
123 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
125 for (i = 0; i < count; i++)
127 nikon_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level,
128 image_offset, image_length, &jpeg_start, &jpeg_len);
131 if (jpeg_start > 0 &&
132 jpeg_len > *image_length)
134 *image_offset = jpeg_start;
135 *image_length = jpeg_len;
138 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
141 gboolean format_nikon_raw(guchar *data, const guint len,
142 guint *image_offset, guint *exif_offset)
150 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
153 while (offset && level < EXIF_TIFF_MAX_LEVELS)
155 offset = nikon_tiff_table(data, len, offset, bo, 0, &i_off, &i_len);
161 if (image_offset) *image_offset = i_off;
170 *-----------------------------------------------------------------------------
171 * EXIF Makernote for Nikon
172 *-----------------------------------------------------------------------------
175 static ExifTextList NikonTagQuality[]= {
180 { 5, "SXGA normal" },
182 { 7, "XGA basic (?)" },
183 { 8, "XGA normal (?)" },
184 { 9, "XGA fine (?)" },
185 { 10, "UXGA basic" },
186 { 11, "UXGA normal" },
191 static ExifTextList NikonTagColorMode[]= {
197 static ExifTextList NikonTagImgAdjust[]= {
206 static ExifTextList NikonTagISOSensitivity[]= {
214 static ExifTextList NikonTagWhiteBalance[]= {
218 { 3, "incandescent" },
219 { 4, "fluorescence" },
225 static ExifTextList NikonTagConverter[]= {
231 static ExifMarker NikonExifMarkersList1[] = {
232 { 0x0002, EXIF_FORMAT_STRING, 6, "Nikon.unknown", NULL, NULL },
233 { 0x0003, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Quality", "Quality", NikonTagQuality },
234 { 0x0004, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ColorMode", "Color mode", NikonTagColorMode },
235 { 0x0005, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ImageAdjustment",
236 "Image adjustment", NikonTagImgAdjust },
237 { 0x0006, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ISOSensitivity",
238 "ISO sensitivity", NikonTagISOSensitivity },
239 { 0x0007, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.WhiteBalance", "White balance",NikonTagWhiteBalance },
240 { 0x0008, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Nikon.Focus", "Focus", NULL },
241 { 0x000a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Nikon.DigitalZoom", "Digital zoom", NULL },
242 { 0x000b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Converter", "Converter", NikonTagConverter },
246 static ExifTextList NikonTag2FlashComp[]= {
267 static ExifTextList NikonTag2LensType[]= {
270 { 2, "AF-D or AF-s" },
276 static ExifTextList NikonTag2FlashUsed[]= {
278 { 4, "unit unknown" },
285 static ExifMarker NikonExifMarkersList2[] = {
286 { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Nikon.ISOSpeed", "ISO speed", NULL },
287 { 0x0003, EXIF_FORMAT_STRING, -1, "Nikon.ColorMode", "Color mode", NULL },
288 { 0x0004, EXIF_FORMAT_STRING, -1, "Nikon.Quality", "Quality", NULL },
289 { 0x0005, EXIF_FORMAT_STRING, -1, "Nikon.WhiteBalance", "White balance",NULL },
290 { 0x0006, EXIF_FORMAT_STRING, -1, "Nikon.Sharpening", "Sharpening", NULL },
291 { 0x0007, EXIF_FORMAT_STRING, -1, "Nikon.FocusMode", "Focus mode", NULL },
292 { 0x0008, EXIF_FORMAT_STRING, -1, "Nikon.FlashSetting", "Flash setting",NULL },
293 { 0x0009, EXIF_FORMAT_STRING, -1, "Nikon.AutoFlashMode","Auto flash mode",NULL },
294 { 0x000b, EXIF_FORMAT_SHORT, 1, "Nikon.WhiteBalanceBias",
295 "White balance bias value", NULL },
296 /* { 0x000c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.WhiteBalanceRB",
297 "White balance red/blue coefficients", NULL }, */
298 /* { 0x000f, EXIF_FORMAT_STRING, -1, "Nikon.ISOSelect", "ISO selection",NULL }, */
299 { 0x0012, EXIF_FORMAT_UNDEFINED, 4, "Nikon.FlashCompensation",
300 "Flash compensation", NikonTag2FlashComp },
301 { 0x0013, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Nikon.ISOSpeedRequest",
302 "ISO speed requested", NULL },
303 { 0x0016, EXIF_FORMAT_SHORT_UNSIGNED, 4, "Nikon.CornerCoord",
304 "Corner coordinates", NULL },
305 { 0x0018, EXIF_FORMAT_UNDEFINED, 4, "Nikon.FlashBracketCompensation",
306 "Flash bracket compensation", NikonTag2FlashComp },
307 { 0x0019, EXIF_FORMAT_RATIONAL, 1, "Nikon.AEBracketCompensation",
308 "AE bracket compensation", NULL },
309 { 0x0080, EXIF_FORMAT_STRING, -1, "Nikon.ImageAdjustment",
310 "Image adjustment", NULL },
311 { 0x0081, EXIF_FORMAT_STRING, -1, "Nikon.Contrast", "Contrast", NULL },
312 { 0x0082, EXIF_FORMAT_STRING, -1, "Nikon.AuxLens", "Aux lens adapter", NULL },
313 { 0x0083, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Nikon.LensType", "Lens type", NikonTag2LensType },
314 { 0x0084, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Nikon.LensFocalLength",
315 "Lens min/max focal length and aperture", NULL },
316 { 0x0085, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ManualFocusDistance",
317 "Manual focus distance", NULL },
318 { 0x0086, EXIF_FORMAT_RATIONAL, 1, "Nikon.DigitalZoomFactor",
319 "Digital zoom factor", NULL },
320 { 0x0087, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Nikon.FlashUsed", "Flash used", NikonTag2FlashUsed },
321 { 0x0088, EXIF_FORMAT_UNDEFINED, 4, "Nikon.AutoFocusArea","Auto focus area",NULL },
322 /* { 0x0089, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Nikon.Bracket/ShootingMode", NULL, NULL }, */
323 { 0x008d, EXIF_FORMAT_STRING, -1, "Nikon.ColorMode", "Color mode", NULL },
324 { 0x008f, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.SceneMode", "Scene mode", NULL },
325 { 0x0090, EXIF_FORMAT_STRING, -1, "Nikon.LightingType", "Lighting type",NULL },
326 { 0x0092, EXIF_FORMAT_SHORT, 1, "Nikon.HueAdjust", "Hue adjustment",NULL },
327 /* { 0x0094, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Saturation", "Saturation", NikonTag2Saturation }, */
328 { 0x0095, EXIF_FORMAT_STRING, -1, "Nikon.NoiseReduction", "Noise reduction", NULL },
329 { 0x00a7, EXIF_FORMAT_LONG_UNSIGNED, 1, "Nikon.ShutterCount", "Shutter release count", NULL },
330 { 0x00a9, EXIF_FORMAT_STRING, -1, "Nikon.ImageOptimization", "Image optimization", NULL },
331 { 0x00aa, EXIF_FORMAT_STRING, -1, "Nikon.Saturation", "Saturation", NULL },
332 { 0x00ab, EXIF_FORMAT_STRING, -1, "Nikon.DigitalVariProg", "Digital Vari-program", NULL },
336 static ExifTextList NikonAFPoint[]= {
346 gboolean format_nikon_makernote(ExifData *exif, guchar *tiff, guint offset,
347 guint size, ExifByteOrder bo)
352 if (offset + 8 + 4 >= size) return FALSE;
354 data = tiff + offset;
356 /* Nikon tag format 1 */
357 if (memcmp(data, "Nikon\x00\x01\x00", 8) == 0)
359 if (exif_parse_IFD_table(exif, tiff, offset + 8, size,
360 bo, 0, NikonExifMarkersList1) != 0)
367 /* Nikon tag format 2 uses Embedded tiff header */
368 if (memcmp(data, "Nikon\x00\x02\x00\x00\x00", 10) == 0 ||
369 memcmp(data, "Nikon\x00\x02\x10\x00\x00", 10) == 0)
373 tiff_header = offset + 10;
374 if (exif_tiff_parse(exif, tiff + tiff_header, size - tiff_header,
375 NikonExifMarkersList2) != 0)
380 /* Nikon tag format 3 uses format 2 tags without "Nikon" and tiff header */
381 else if (exif_parse_IFD_table(exif, tiff, offset, size,
382 bo, 0, NikonExifMarkersList2) != 0)
387 item = exif_get_item(exif, "Nikon.AutoFocusArea");
388 if (item && item->data_len == 4 * sizeof(guchar))
390 static ExifMarker marker = { 0x0088, EXIF_FORMAT_STRING, -1,
391 "Nikon.AutoFocusPoint", "Auto focus point", NULL };
392 guchar *array = item->data;
396 text = exif_text_list_find_value(NikonAFPoint, (gint)array[1]);
397 l = strlen(text) + 1;
399 item = exif_item_new(marker.format, marker.tag, l, &marker);
400 memcpy(item->data, text, l);
404 exif->items = g_list_prepend(exif->items, item);
407 item = exif_get_item(exif, "Nikon.ISOSpeed");
408 if (item && item->data_len == 2 * 2)
410 static ExifMarker marker = { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 1,
411 "ISOSpeedRatings", "ISO speed", NULL };
414 shadow = exif_item_new(marker.format, marker.tag, 1, &marker);
415 memcpy(shadow->data, item->data + 2, 2);
417 exif->items = g_list_prepend(exif->items, shadow);
420 item = exif_get_item(exif, "Nikon.WhiteBalance");
421 if (item && item->format == EXIF_FORMAT_STRING)
423 static ExifMarker marker = { 0x0005, EXIF_FORMAT_STRING, -1,
424 "LightSource", "Light source", NULL };
427 shadow = exif_item_new(marker.format, marker.tag, item->data_len, &marker);
428 memcpy(shadow->data, item->data, item->data_len);
430 exif->items = g_list_prepend(exif->items, shadow);
438 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */