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