Silence GTK deprecation warning in unused function
[geeqie.git] / src / format-nikon.cc
1 /*
2  * Copyright (C) 2005 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: Joseph Heled
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "format-nikon.h"
23
24 #include <cstring>
25
26 #include <glib.h>
27
28 #include "exif.h"
29
30
31 /*
32  *-----------------------------------------------------------------------------
33  * Raw NEF embedded jpeg extraction for Nikon
34  *-----------------------------------------------------------------------------
35  */
36
37 static guint nikon_tiff_table(guchar *data, guint len, guint offset, ExifByteOrder bo,
38                               gint level,
39                               guint *image_offset, guint *jpeg_len);
40
41
42 static void nikon_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
43                              gint level,
44                              guint *image_offset, guint *image_length, guint *jpeg_start, guint *jpeg_len)
45 {
46         guint tag;
47         guint type;
48         guint count;
49         guint segment;
50         guint seg_len;
51
52         tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
53         type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
54         count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
55
56         /* so far, we only care about tags with type long */
57         if (type != EXIF_FORMAT_LONG_UNSIGNED && type != EXIF_FORMAT_LONG) return;
58
59         seg_len = ExifFormatList[type].size * count;
60         if (seg_len > 4)
61                 {
62                 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
63                 if (segment + seg_len > len) return;
64                 }
65         else
66                 {
67                 segment = offset + EXIF_TIFD_OFFSET_DATA;
68                 }
69
70         if (tag == 0x14a)
71                 {
72                 /* sub IFD table */
73                 guint i;
74
75                 for (i = 0; i < count; i++)
76                         {
77                         guint subset;
78
79                         subset = exif_byte_get_int32(data + segment + i * 4, bo);
80                         nikon_tiff_table(data, len, subset, bo, level + 1, image_offset, image_length);
81                         }
82
83                 }
84         else if (tag == 0x201)
85                 {
86                 /* jpeg data start offset */
87                 *jpeg_start = exif_byte_get_int32(data + segment, bo);
88                 }
89         else if (tag == 0x202)
90                 {
91                 /* jpeg data length */
92                 *jpeg_len = exif_byte_get_int32(data + segment, bo);
93                 }
94 }
95
96 static guint nikon_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
97                               gint level,
98                               guint *image_offset, guint *image_length)
99 {
100         guint count;
101         guint i;
102         guint jpeg_start = 0;
103         guint jpeg_len = 0;
104
105         /* limit damage from infinite loops */
106         if (level > EXIF_TIFF_MAX_LEVELS) return 0;
107
108         if (len < offset + 2) return FALSE;
109
110         count = exif_byte_get_int16(data + offset, bo);
111         offset += 2;
112         if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
113
114         for (i = 0; i < count; i++)
115                 {
116                 nikon_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level,
117                                  image_offset, image_length, &jpeg_start, &jpeg_len);
118                 }
119
120         if (jpeg_start > 0 &&
121             jpeg_len > *image_length)
122                 {
123                 *image_offset = jpeg_start;
124                 *image_length = jpeg_len;
125                 }
126
127         return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
128 }
129
130 gboolean format_nikon_raw(guchar *data, const guint len,
131                           guint *image_offset, guint *)
132 {
133         guint i_off = 0;
134         guint i_len = 0;
135         ExifByteOrder bo;
136         guint offset;
137         gint level;
138
139         if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
140
141         level = 0;
142         while (offset && level < EXIF_TIFF_MAX_LEVELS)
143                 {
144                 offset = nikon_tiff_table(data, len, offset, bo, 0, &i_off, &i_len);
145                 level++;
146                 }
147
148         if (i_off != 0)
149                 {
150                 if (image_offset) *image_offset = i_off;
151                 return TRUE;
152                 }
153
154         return FALSE;
155 }
156
157
158 /*
159  *-----------------------------------------------------------------------------
160  * EXIF Makernote for Nikon
161  *-----------------------------------------------------------------------------
162  */
163
164 static ExifTextList NikonTagQuality[]= {
165         { 1,    "VGA basic" },
166         { 2,    "VGA normal" },
167         { 3,    "VGA fine" },
168         { 4,    "SXGA basic" },
169         { 5,    "SXGA normal" },
170         { 6,    "SXGA fine" },
171         { 7,    "XGA basic (?)" },
172         { 8,    "XGA normal (?)" },
173         { 9,    "XGA fine (?)" },
174         { 10,   "UXGA basic" },
175         { 11,   "UXGA normal" },
176         { 12,   "UXGA fine" },
177         EXIF_TEXT_LIST_END
178 };
179
180 static ExifTextList NikonTagColorMode[]= {
181         { 1,    "color" },
182         { 2,    "monochrome" },
183         EXIF_TEXT_LIST_END
184 };
185
186 static ExifTextList NikonTagImgAdjust[]= {
187         { 0,    "normal" },
188         { 1,    "bright+" },
189         { 2,    "bright-" },
190         { 3,    "contrast+" },
191         { 4,    "contrast-" },
192         EXIF_TEXT_LIST_END
193 };
194
195 static ExifTextList NikonTagISOSensitivity[]= {
196         { 0,    "80" },
197         { 2,    "160" },
198         { 4,    "320" },
199         { 5,    "100" },
200         EXIF_TEXT_LIST_END
201 };
202
203 static ExifTextList NikonTagWhiteBalance[]= {
204         { 0,    "auto" },
205         { 1,    "preset" },
206         { 2,    "daylight" },
207         { 3,    "incandescent" },
208         { 4,    "fluorescence" },
209         { 5,    "cloudy" },
210         { 6,    "speedlight" },
211         EXIF_TEXT_LIST_END
212 };
213
214 static ExifTextList NikonTagConverter[]= {
215         { 0,    "none" },
216         { 1,    "Fisheye" },
217         EXIF_TEXT_LIST_END
218 };
219
220 static ExifMarker NikonExifMarkersList1[] = {
221 { 0x0002, EXIF_FORMAT_STRING, 6,                "Nikon.unknown",        nullptr,                nullptr },
222 { 0x0003, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Nikon.Quality",        "Quality",      NikonTagQuality },
223 { 0x0004, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Nikon.ColorMode",      "Color mode",   NikonTagColorMode },
224 { 0x0005, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Nikon.ImageAdjustment",
225                                                                 "Image adjustment",     NikonTagImgAdjust },
226 { 0x0006, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Nikon.ISOSensitivity",
227                                                                 "ISO sensitivity",      NikonTagISOSensitivity },
228 { 0x0007, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Nikon.WhiteBalance",   "White balance",NikonTagWhiteBalance },
229 { 0x0008, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Nikon.Focus",          "Focus",        nullptr },
230 { 0x000a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Nikon.DigitalZoom",    "Digital zoom", nullptr },
231 { 0x000b, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Nikon.Converter",      "Converter",    NikonTagConverter },
232 EXIF_MARKER_LIST_END
233 };
234
235 static ExifTextList NikonTag2FlashComp[]= {
236         { 0x06, "+1.0 EV" },
237         { 0x04, "+0.7 EV" },
238         { 0x03, "+0.5 EV" },
239         { 0x02, "+0.3 EV" },
240         { 0x00, "0.0 EV" },
241         { 0xfe, "-0.3 EV" },
242         { 0xfd, "-0.5 EV" },
243         { 0xfc, "-0.7 EV" },
244         { 0xfa, "-1.0 EV" },
245         { 0xf8, "-1.3 EV" },
246         { 0xf7, "-1.5 EV" },
247         { 0xf6, "-1.7 EV" },
248         { 0xf4, "-2.0 EV" },
249         { 0xf2, "-2.3 EV" },
250         { 0xf1, "-2.5 EV" },
251         { 0xf0, "-2.7 EV" },
252         { 0xee, "-3.0 EV" },
253         EXIF_TEXT_LIST_END
254 };
255
256 static ExifTextList NikonTag2LensType[]= {
257         { 0,    "AF non D" },
258         { 1,    "manual" },
259         { 2,    "AF-D or AF-s" },
260         { 6,    "AF-D G" },
261         { 10,   "AF-D VR" },
262         EXIF_TEXT_LIST_END
263 };
264
265 static ExifTextList NikonTag2FlashUsed[]= {
266         { 0,    "no" },
267         { 4,    "unit unknown" },
268         { 7,    "external" },
269         { 9,    "yes" },
270         EXIF_TEXT_LIST_END
271 };
272
273
274 static ExifMarker NikonExifMarkersList2[] = {
275 { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 2,        "Nikon.ISOSpeed",       "ISO speed",    nullptr },
276 { 0x0003, EXIF_FORMAT_STRING, -1,               "Nikon.ColorMode",      "Color mode",   nullptr },
277 { 0x0004, EXIF_FORMAT_STRING, -1,               "Nikon.Quality",        "Quality",      nullptr },
278 { 0x0005, EXIF_FORMAT_STRING, -1,               "Nikon.WhiteBalance",   "White balance",nullptr },
279 { 0x0006, EXIF_FORMAT_STRING, -1,               "Nikon.Sharpening",     "Sharpening",   nullptr },
280 { 0x0007, EXIF_FORMAT_STRING, -1,               "Nikon.FocusMode",      "Focus mode",   nullptr },
281 { 0x0008, EXIF_FORMAT_STRING, -1,               "Nikon.FlashSetting",   "Flash setting",nullptr },
282 { 0x0009, EXIF_FORMAT_STRING, -1,               "Nikon.AutoFlashMode","Auto flash mode",nullptr },
283 { 0x000b, EXIF_FORMAT_SHORT, 1,                 "Nikon.WhiteBalanceBias",
284                                                         "White balance bias value",     nullptr },
285 /* { 0x000c, EXIF_FORMAT_SHORT_UNSIGNED, 1,     "Nikon.WhiteBalanceRB",
286                                                 "White balance red/blue coefficients",  NULL }, */
287 /* { 0x000f, EXIF_FORMAT_STRING, -1,            "Nikon.ISOSelect",      "ISO selection",NULL }, */
288 { 0x0012, EXIF_FORMAT_UNDEFINED, 4,             "Nikon.FlashCompensation",
289                                                                 "Flash compensation",   NikonTag2FlashComp },
290 { 0x0013, EXIF_FORMAT_SHORT_UNSIGNED, 2,        "Nikon.ISOSpeedRequest",
291                                                                 "ISO speed requested",  nullptr },
292 { 0x0016, EXIF_FORMAT_SHORT_UNSIGNED, 4,        "Nikon.CornerCoord",
293                                                                 "Corner coordinates",   nullptr },
294 { 0x0018, EXIF_FORMAT_UNDEFINED, 4,             "Nikon.FlashBracketCompensation",
295                                                         "Flash bracket compensation",   NikonTag2FlashComp },
296 { 0x0019, EXIF_FORMAT_RATIONAL, 1,              "Nikon.AEBracketCompensation",
297                                                         "AE bracket compensation",      nullptr },
298 { 0x0080, EXIF_FORMAT_STRING, -1,               "Nikon.ImageAdjustment",
299                                                                 "Image adjustment",     nullptr },
300 { 0x0081, EXIF_FORMAT_STRING, -1,               "Nikon.Contrast",       "Contrast",     nullptr },
301 { 0x0082, EXIF_FORMAT_STRING, -1,               "Nikon.AuxLens", "Aux lens adapter",    nullptr },
302 { 0x0083, EXIF_FORMAT_BYTE_UNSIGNED, -1,        "Nikon.LensType",       "Lens type",    NikonTag2LensType },
303 { 0x0084, EXIF_FORMAT_RATIONAL_UNSIGNED, -1,    "Nikon.LensFocalLength",
304                                                         "Lens min/max focal length and aperture", nullptr },
305 { 0x0085, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Nikon.ManualFocusDistance",
306                                                         "Manual focus distance",        nullptr },
307 { 0x0086, EXIF_FORMAT_RATIONAL, 1,              "Nikon.DigitalZoomFactor",
308                                                         "Digital zoom factor",          nullptr },
309 { 0x0087, EXIF_FORMAT_BYTE_UNSIGNED, 1,         "Nikon.FlashUsed",      "Flash used",   NikonTag2FlashUsed },
310 { 0x0088, EXIF_FORMAT_UNDEFINED, 4,             "Nikon.AutoFocusArea","Auto focus area",nullptr },
311 /* { 0x0089, EXIF_FORMAT_SHORT_UNSIGNED, -1,    "Nikon.Bracket/ShootingMode", NULL,     NULL }, */
312 { 0x008d, EXIF_FORMAT_STRING, -1,               "Nikon.ColorMode",      "Color mode",   nullptr },
313 { 0x008f, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Nikon.SceneMode",      "Scene mode",   nullptr },
314 { 0x0090, EXIF_FORMAT_STRING, -1,               "Nikon.LightingType",   "Lighting type",nullptr },
315 { 0x0092, EXIF_FORMAT_SHORT, 1,                 "Nikon.HueAdjust",      "Hue adjustment",nullptr },
316 /* { 0x0094, EXIF_FORMAT_SHORT_UNSIGNED, 1,     "Nikon.Saturation",     "Saturation",   NikonTag2Saturation }, */
317 { 0x0095, EXIF_FORMAT_STRING, -1,               "Nikon.NoiseReduction", "Noise reduction", nullptr },
318 { 0x00a7, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Nikon.ShutterCount", "Shutter release count", nullptr },
319 { 0x00a9, EXIF_FORMAT_STRING, -1,               "Nikon.ImageOptimization", "Image optimization", nullptr },
320 { 0x00aa, EXIF_FORMAT_STRING, -1,               "Nikon.Saturation", "Saturation",       nullptr },
321 { 0x00ab, EXIF_FORMAT_STRING, -1,               "Nikon.DigitalVariProg", "Digital Vari-program", nullptr },
322 EXIF_MARKER_LIST_END
323 };
324
325 static ExifTextList NikonAFPoint[]= {
326         { 0,    "center" },
327         { 1,    "top" },
328         { 2,    "bottom" },
329         { 3,    "left" },
330         { 4,    "right" },
331         EXIF_TEXT_LIST_END
332 };
333
334
335 gboolean format_nikon_makernote(ExifData *exif, guchar *tiff, guint offset,
336                                 guint size, ExifByteOrder bo)
337 {
338         guchar *data;
339         ExifItem *item;
340
341         if (offset + 8 + 4 >= size) return FALSE;
342
343         data = tiff + offset;
344
345         /* Nikon tag format 1 */
346         if (memcmp(data, "Nikon\x00\x01\x00", 8) == 0)
347                 {
348                 if (exif_parse_IFD_table(exif, tiff, offset + 8, size,
349                                          bo, 0, NikonExifMarkersList1) != 0)
350                         {
351                         return FALSE;
352                         }
353                 return TRUE;
354                 }
355
356         /* Nikon tag format 2 uses Embedded tiff header */
357         if (memcmp(data, "Nikon\x00\x02\x00\x00\x00", 10) == 0 ||
358             memcmp(data, "Nikon\x00\x02\x10\x00\x00", 10) == 0)
359                 {
360                 guint tiff_header;
361
362                 tiff_header = offset + 10;
363                 if (exif_tiff_parse(exif, tiff + tiff_header, size - tiff_header,
364                     NikonExifMarkersList2) != 0)
365                         {
366                         return FALSE;
367                         }
368                 }
369         /* Nikon tag format 3 uses format 2 tags without "Nikon" and tiff header */
370         else if (exif_parse_IFD_table(exif, tiff, offset, size,
371                                       bo, 0, NikonExifMarkersList2) != 0)
372                 {
373                 return FALSE;
374                 }
375
376         item = exif_get_item(exif, "Nikon.AutoFocusArea");
377         if (item && item->data_len == 4 * sizeof(guchar))
378                 {
379                 static ExifMarker marker = { 0x0088, EXIF_FORMAT_STRING, -1,
380                                              "Nikon.AutoFocusPoint", "Auto focus point", nullptr };
381                 auto array = static_cast<guchar*>(item->data);
382                 gchar *text;
383                 gint l;
384
385                 text = exif_text_list_find_value(NikonAFPoint, static_cast<gint>(array[1]));
386                 l = strlen(text) + 1;
387
388                 item = exif_item_new(marker.format, marker.tag, l, &marker);
389                 memcpy(item->data, text, l);
390
391                 g_free(text);
392
393                 exif->items = g_list_prepend(exif->items, item);
394                 }
395
396         item = exif_get_item(exif, "Nikon.ISOSpeed");
397         if (item && item->data_len == 2 * 2)
398                 {
399                 static ExifMarker marker = { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 1,
400                                              "ISOSpeedRatings", "ISO speed", nullptr };
401                 ExifItem *shadow;
402
403                 shadow = exif_item_new(marker.format, marker.tag, 1, &marker);
404                 memcpy(shadow->data, static_cast<char *>(item->data) + 2, 2);
405
406                 exif->items = g_list_prepend(exif->items, shadow);
407                 }
408
409         item = exif_get_item(exif, "Nikon.WhiteBalance");
410         if (item && item->format == EXIF_FORMAT_STRING)
411                 {
412                 static ExifMarker marker = { 0x0005, EXIF_FORMAT_STRING, -1,
413                                              "LightSource", "Light source", nullptr };
414                 ExifItem *shadow;
415
416                 shadow = exif_item_new(marker.format, marker.tag, item->data_len, &marker);
417                 memcpy(shadow->data, item->data, item->data_len);
418
419                 exif->items = g_list_prepend(exif->items, shadow);
420                 }
421
422         return TRUE;
423 }
424
425 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */