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.
35 #include "format-nikon.h"
41 *-----------------------------------------------------------------------------
42 * Raw NEF embedded jpeg extraction for Nikon
43 *-----------------------------------------------------------------------------
46 static guint nikon_tiff_table(guchar *data, guint len, guint offset, ExifByteOrder bo,
48 guint *image_offset, guint *jpeg_len);
51 static void nikon_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
53 guint *image_offset, guint *image_length, guint *jpeg_start, guint *jpeg_len)
61 tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
62 type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
63 count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
65 /* so far, we only care about tags with type long */
66 if (type != EXIF_FORMAT_LONG_UNSIGNED && type != EXIF_FORMAT_LONG) return;
68 seg_len = ExifFormatList[type].size * count;
71 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
72 if (segment + seg_len > len) return;
76 segment = offset + EXIF_TIFD_OFFSET_DATA;
84 for (i = 0; i < count; i++)
88 subset = exif_byte_get_int32(data + segment + i * 4, bo);
89 nikon_tiff_table(data, len, subset, bo, level + 1, image_offset, image_length);
93 else if (tag == 0x201)
95 /* jpeg data start offset */
96 *jpeg_start = exif_byte_get_int32(data + segment, bo);
98 else if (tag == 0x202)
100 /* jpeg data length */
101 *jpeg_len = exif_byte_get_int32(data + segment, bo);
105 static guint nikon_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
107 guint *image_offset, guint *image_length)
111 guint jpeg_start = 0;
114 /* limit damage from infinite loops */
115 if (level > EXIF_TIFF_MAX_LEVELS) return 0;
117 if (len < offset + 2) return FALSE;
119 count = exif_byte_get_int16(data + offset, bo);
121 if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
123 for (i = 0; i < count; i++)
125 nikon_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level,
126 image_offset, image_length, &jpeg_start, &jpeg_len);
129 if (jpeg_start > 0 &&
130 jpeg_len > *image_length)
132 *image_offset = jpeg_start;
133 *image_length = jpeg_len;
136 return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
139 gboolean format_nikon_raw(guchar *data, const guint len,
140 guint *image_offset, guint *)
148 if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
151 while (offset && level < EXIF_TIFF_MAX_LEVELS)
153 offset = nikon_tiff_table(data, len, offset, bo, 0, &i_off, &i_len);
159 if (image_offset) *image_offset = i_off;
168 *-----------------------------------------------------------------------------
169 * EXIF Makernote for Nikon
170 *-----------------------------------------------------------------------------
173 static ExifTextList NikonTagQuality[]= {
178 { 5, "SXGA normal" },
180 { 7, "XGA basic (?)" },
181 { 8, "XGA normal (?)" },
182 { 9, "XGA fine (?)" },
183 { 10, "UXGA basic" },
184 { 11, "UXGA normal" },
189 static ExifTextList NikonTagColorMode[]= {
195 static ExifTextList NikonTagImgAdjust[]= {
204 static ExifTextList NikonTagISOSensitivity[]= {
212 static ExifTextList NikonTagWhiteBalance[]= {
216 { 3, "incandescent" },
217 { 4, "fluorescence" },
223 static ExifTextList NikonTagConverter[]= {
229 static ExifMarker NikonExifMarkersList1[] = {
230 { 0x0002, EXIF_FORMAT_STRING, 6, "Nikon.unknown", nullptr, nullptr },
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", nullptr },
239 { 0x000a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Nikon.DigitalZoom", "Digital zoom", nullptr },
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 ExifMarker NikonExifMarkersList2[] = {
284 { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Nikon.ISOSpeed", "ISO speed", nullptr },
285 { 0x0003, EXIF_FORMAT_STRING, -1, "Nikon.ColorMode", "Color mode", nullptr },
286 { 0x0004, EXIF_FORMAT_STRING, -1, "Nikon.Quality", "Quality", nullptr },
287 { 0x0005, EXIF_FORMAT_STRING, -1, "Nikon.WhiteBalance", "White balance",nullptr },
288 { 0x0006, EXIF_FORMAT_STRING, -1, "Nikon.Sharpening", "Sharpening", nullptr },
289 { 0x0007, EXIF_FORMAT_STRING, -1, "Nikon.FocusMode", "Focus mode", nullptr },
290 { 0x0008, EXIF_FORMAT_STRING, -1, "Nikon.FlashSetting", "Flash setting",nullptr },
291 { 0x0009, EXIF_FORMAT_STRING, -1, "Nikon.AutoFlashMode","Auto flash mode",nullptr },
292 { 0x000b, EXIF_FORMAT_SHORT, 1, "Nikon.WhiteBalanceBias",
293 "White balance bias value", nullptr },
294 /* { 0x000c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.WhiteBalanceRB",
295 "White balance red/blue coefficients", NULL }, */
296 /* { 0x000f, EXIF_FORMAT_STRING, -1, "Nikon.ISOSelect", "ISO selection",NULL }, */
297 { 0x0012, EXIF_FORMAT_UNDEFINED, 4, "Nikon.FlashCompensation",
298 "Flash compensation", NikonTag2FlashComp },
299 { 0x0013, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Nikon.ISOSpeedRequest",
300 "ISO speed requested", nullptr },
301 { 0x0016, EXIF_FORMAT_SHORT_UNSIGNED, 4, "Nikon.CornerCoord",
302 "Corner coordinates", nullptr },
303 { 0x0018, EXIF_FORMAT_UNDEFINED, 4, "Nikon.FlashBracketCompensation",
304 "Flash bracket compensation", NikonTag2FlashComp },
305 { 0x0019, EXIF_FORMAT_RATIONAL, 1, "Nikon.AEBracketCompensation",
306 "AE bracket compensation", nullptr },
307 { 0x0080, EXIF_FORMAT_STRING, -1, "Nikon.ImageAdjustment",
308 "Image adjustment", nullptr },
309 { 0x0081, EXIF_FORMAT_STRING, -1, "Nikon.Contrast", "Contrast", nullptr },
310 { 0x0082, EXIF_FORMAT_STRING, -1, "Nikon.AuxLens", "Aux lens adapter", nullptr },
311 { 0x0083, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Nikon.LensType", "Lens type", NikonTag2LensType },
312 { 0x0084, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Nikon.LensFocalLength",
313 "Lens min/max focal length and aperture", nullptr },
314 { 0x0085, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.ManualFocusDistance",
315 "Manual focus distance", nullptr },
316 { 0x0086, EXIF_FORMAT_RATIONAL, 1, "Nikon.DigitalZoomFactor",
317 "Digital zoom factor", nullptr },
318 { 0x0087, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Nikon.FlashUsed", "Flash used", NikonTag2FlashUsed },
319 { 0x0088, EXIF_FORMAT_UNDEFINED, 4, "Nikon.AutoFocusArea","Auto focus area",nullptr },
320 /* { 0x0089, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Nikon.Bracket/ShootingMode", NULL, NULL }, */
321 { 0x008d, EXIF_FORMAT_STRING, -1, "Nikon.ColorMode", "Color mode", nullptr },
322 { 0x008f, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.SceneMode", "Scene mode", nullptr },
323 { 0x0090, EXIF_FORMAT_STRING, -1, "Nikon.LightingType", "Lighting type",nullptr },
324 { 0x0092, EXIF_FORMAT_SHORT, 1, "Nikon.HueAdjust", "Hue adjustment",nullptr },
325 /* { 0x0094, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Nikon.Saturation", "Saturation", NikonTag2Saturation }, */
326 { 0x0095, EXIF_FORMAT_STRING, -1, "Nikon.NoiseReduction", "Noise reduction", nullptr },
327 { 0x00a7, EXIF_FORMAT_LONG_UNSIGNED, 1, "Nikon.ShutterCount", "Shutter release count", nullptr },
328 { 0x00a9, EXIF_FORMAT_STRING, -1, "Nikon.ImageOptimization", "Image optimization", nullptr },
329 { 0x00aa, EXIF_FORMAT_STRING, -1, "Nikon.Saturation", "Saturation", nullptr },
330 { 0x00ab, EXIF_FORMAT_STRING, -1, "Nikon.DigitalVariProg", "Digital Vari-program", nullptr },
334 static ExifTextList NikonAFPoint[]= {
344 gboolean format_nikon_makernote(ExifData *exif, guchar *tiff, guint offset,
345 guint size, ExifByteOrder bo)
350 if (offset + 8 + 4 >= size) return FALSE;
352 data = tiff + offset;
354 /* Nikon tag format 1 */
355 if (memcmp(data, "Nikon\x00\x01\x00", 8) == 0)
357 if (exif_parse_IFD_table(exif, tiff, offset + 8, size,
358 bo, 0, NikonExifMarkersList1) != 0)
365 /* Nikon tag format 2 uses Embedded tiff header */
366 if (memcmp(data, "Nikon\x00\x02\x00\x00\x00", 10) == 0 ||
367 memcmp(data, "Nikon\x00\x02\x10\x00\x00", 10) == 0)
371 tiff_header = offset + 10;
372 if (exif_tiff_parse(exif, tiff + tiff_header, size - tiff_header,
373 NikonExifMarkersList2) != 0)
378 /* Nikon tag format 3 uses format 2 tags without "Nikon" and tiff header */
379 else if (exif_parse_IFD_table(exif, tiff, offset, size,
380 bo, 0, NikonExifMarkersList2) != 0)
385 item = exif_get_item(exif, "Nikon.AutoFocusArea");
386 if (item && item->data_len == 4 * sizeof(guchar))
388 static ExifMarker marker = { 0x0088, EXIF_FORMAT_STRING, -1,
389 "Nikon.AutoFocusPoint", "Auto focus point", nullptr };
390 auto array = static_cast<guchar*>(item->data);
394 text = exif_text_list_find_value(NikonAFPoint, static_cast<gint>(array[1]));
395 l = strlen(text) + 1;
397 item = exif_item_new(marker.format, marker.tag, l, &marker);
398 memcpy(item->data, text, l);
402 exif->items = g_list_prepend(exif->items, item);
405 item = exif_get_item(exif, "Nikon.ISOSpeed");
406 if (item && item->data_len == 2 * 2)
408 static ExifMarker marker = { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 1,
409 "ISOSpeedRatings", "ISO speed", nullptr };
412 shadow = exif_item_new(marker.format, marker.tag, 1, &marker);
413 memcpy(shadow->data, static_cast<char *>(item->data) + 2, 2);
415 exif->items = g_list_prepend(exif->items, shadow);
418 item = exif_get_item(exif, "Nikon.WhiteBalance");
419 if (item && item->format == EXIF_FORMAT_STRING)
421 static ExifMarker marker = { 0x0005, EXIF_FORMAT_STRING, -1,
422 "LightSource", "Light source", nullptr };
425 shadow = exif_item_new(marker.format, marker.tag, item->data_len, &marker);
426 memcpy(shadow->data, item->data, item->data_len);
428 exif->items = g_list_prepend(exif->items, shadow);
435 using dummy_variable = int;
438 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */