Simplify vflist_get_formatted()
[geeqie.git] / src / format_nikon.c
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 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #ifndef HAVE_EXIV2
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <glib.h>
33
34 #include "intl.h"
35
36 #include "main.h"
37 #include "format_nikon.h"
38
39 #include "exif.h"
40
41
42 /*
43  *-----------------------------------------------------------------------------
44  * Raw NEF embedded jpeg extraction for Nikon
45  *-----------------------------------------------------------------------------
46  */
47
48 static guint nikon_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
49                               gint level,
50                               guint *image_offset, guint *jpeg_len);
51
52
53 static void nikon_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
54                              gint level,
55                              guint *image_offset, guint *image_length, guint *jpeg_start, guint *jpeg_len)
56 {
57         guint tag;
58         guint type;
59         guint count;
60         guint segment;
61         guint seg_len;
62
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);
66
67         /* so far, we only care about tags with type long */
68         if (type != EXIF_FORMAT_LONG_UNSIGNED && type != EXIF_FORMAT_LONG) return;
69
70         seg_len = ExifFormatList[type].size * count;
71         if (seg_len > 4)
72                 {
73                 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
74                 if (segment + seg_len > len) return;
75                 }
76         else
77                 {
78                 segment = offset + EXIF_TIFD_OFFSET_DATA;
79                 }
80
81         if (tag == 0x14a)
82                 {
83                 /* sub IFD table */
84                 guint i;
85
86                 for (i = 0; i < count; i++)
87                         {
88                         guint subset;
89
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);
92                         }
93
94                 }
95         else if (tag == 0x201)
96                 {
97                 /* jpeg data start offset */
98                 *jpeg_start = exif_byte_get_int32(data + segment, bo);
99                 }
100         else if (tag == 0x202)
101                 {
102                 /* jpeg data length */
103                 *jpeg_len = exif_byte_get_int32(data + segment, bo);
104                 }
105 }
106
107 static guint nikon_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
108                               gint level,
109                               guint *image_offset, guint *image_length)
110 {
111         guint count;
112         guint i;
113         guint jpeg_start = 0;
114         guint jpeg_len = 0;
115
116         /* limit damage from infinite loops */
117         if (level > EXIF_TIFF_MAX_LEVELS) return 0;
118
119         if (len < offset + 2) return FALSE;
120
121         count = exif_byte_get_int16(data + offset, bo);
122         offset += 2;
123         if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
124
125         for (i = 0; i < count; i++)
126                 {
127                 nikon_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level,
128                                  image_offset, image_length, &jpeg_start, &jpeg_len);
129                 }
130
131         if (jpeg_start > 0 &&
132             jpeg_len > *image_length)
133                 {
134                 *image_offset = jpeg_start;
135                 *image_length = jpeg_len;
136                 }
137
138         return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
139 }
140
141 gboolean format_nikon_raw(guchar *data, const guint len,
142                           guint *image_offset, guint *exif_offset)
143 {
144         guint i_off = 0;
145         guint i_len = 0;
146         ExifByteOrder bo;
147         guint offset;
148         gint level;
149
150         if (!exif_tiff_directory_offset(data, len, &offset, &bo)) return FALSE;
151
152         level = 0;
153         while (offset && level < EXIF_TIFF_MAX_LEVELS)
154                 {
155                 offset = nikon_tiff_table(data, len, offset, bo, 0, &i_off, &i_len);
156                 level++;
157                 }
158
159         if (i_off != 0)
160                 {
161                 if (image_offset) *image_offset = i_off;
162                 return TRUE;
163                 }
164
165         return FALSE;
166 }
167
168
169 /*
170  *-----------------------------------------------------------------------------
171  * EXIF Makernote for Nikon
172  *-----------------------------------------------------------------------------
173  */
174
175 static ExifTextList NikonTagQuality[]= {
176         { 1,    "VGA basic" },
177         { 2,    "VGA normal" },
178         { 3,    "VGA fine" },
179         { 4,    "SXGA basic" },
180         { 5,    "SXGA normal" },
181         { 6,    "SXGA fine" },
182         { 7,    "XGA basic (?)" },
183         { 8,    "XGA normal (?)" },
184         { 9,    "XGA fine (?)" },
185         { 10,   "UXGA basic" },
186         { 11,   "UXGA normal" },
187         { 12,   "UXGA fine" },
188         EXIF_TEXT_LIST_END
189 };
190
191 static ExifTextList NikonTagColorMode[]= {
192         { 1,    "color" },
193         { 2,    "monochrome" },
194         EXIF_TEXT_LIST_END
195 };
196
197 static ExifTextList NikonTagImgAdjust[]= {
198         { 0,    "normal" },
199         { 1,    "bright+" },
200         { 2,    "bright-" },
201         { 3,    "contrast+" },
202         { 4,    "contrast-" },
203         EXIF_TEXT_LIST_END
204 };
205
206 static ExifTextList NikonTagISOSensitivity[]= {
207         { 0,    "80" },
208         { 2,    "160" },
209         { 4,    "320" },
210         { 5,    "100" },
211         EXIF_TEXT_LIST_END
212 };
213
214 static ExifTextList NikonTagWhiteBalance[]= {
215         { 0,    "auto" },
216         { 1,    "preset" },
217         { 2,    "daylight" },
218         { 3,    "incandescent" },
219         { 4,    "fluorescence" },
220         { 5,    "cloudy" },
221         { 6,    "speedlight" },
222         EXIF_TEXT_LIST_END
223 };
224
225 static ExifTextList NikonTagConverter[]= {
226         { 0,    "none" },
227         { 1,    "Fisheye" },
228         EXIF_TEXT_LIST_END
229 };
230
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 },
243 EXIF_MARKER_LIST_END
244 };
245
246 static ExifTextList NikonTag2FlashComp[]= {
247         { 0x06, "+1.0 EV" },
248         { 0x04, "+0.7 EV" },
249         { 0x03, "+0.5 EV" },
250         { 0x02, "+0.3 EV" },
251         { 0x00, "0.0 EV" },
252         { 0xfe, "-0.3 EV" },
253         { 0xfd, "-0.5 EV" },
254         { 0xfc, "-0.7 EV" },
255         { 0xfa, "-1.0 EV" },
256         { 0xf8, "-1.3 EV" },
257         { 0xf7, "-1.5 EV" },
258         { 0xf6, "-1.7 EV" },
259         { 0xf4, "-2.0 EV" },
260         { 0xf2, "-2.3 EV" },
261         { 0xf1, "-2.5 EV" },
262         { 0xf0, "-2.7 EV" },
263         { 0xee, "-3.0 EV" },
264         EXIF_TEXT_LIST_END
265 };
266
267 static ExifTextList NikonTag2LensType[]= {
268         { 0,    "AF non D" },
269         { 1,    "manual" },
270         { 2,    "AF-D or AF-s" },
271         { 6,    "AF-D G" },
272         { 10,   "AF-D VR" },
273         EXIF_TEXT_LIST_END
274 };
275
276 static ExifTextList NikonTag2FlashUsed[]= {
277         { 0,    "no" },
278         { 4,    "unit unknown" },
279         { 7,    "external" },
280         { 9,    "yes" },
281         EXIF_TEXT_LIST_END
282 };
283
284
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 },
333 EXIF_MARKER_LIST_END
334 };
335
336 static ExifTextList NikonAFPoint[]= {
337         { 0,    "center" },
338         { 1,    "top" },
339         { 2,    "bottom" },
340         { 3,    "left" },
341         { 4,    "right" },
342         EXIF_TEXT_LIST_END
343 };
344
345
346 gboolean format_nikon_makernote(ExifData *exif, guchar *tiff, guint offset,
347                                 guint size, ExifByteOrder bo)
348 {
349         guchar *data;
350         ExifItem *item;
351
352         if (offset + 8 + 4 >= size) return FALSE;
353
354         data = tiff + offset;
355
356         /* Nikon tag format 1 */
357         if (memcmp(data, "Nikon\x00\x01\x00", 8) == 0)
358                 {
359                 if (exif_parse_IFD_table(exif, tiff, offset + 8, size,
360                                          bo, 0, NikonExifMarkersList1) != 0)
361                         {
362                         return FALSE;
363                         }
364                 return TRUE;
365                 }
366
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)
370                 {
371                 guint tiff_header;
372
373                 tiff_header = offset + 10;
374                 if (exif_tiff_parse(exif, tiff + tiff_header, size - tiff_header,
375                     NikonExifMarkersList2) != 0)
376                         {
377                         return FALSE;
378                         }
379                 }
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)
383                 {
384                 return FALSE;
385                 }
386
387         item = exif_get_item(exif, "Nikon.AutoFocusArea");
388         if (item && item->data_len == 4 * sizeof(guchar))
389                 {
390                 static ExifMarker marker = { 0x0088, EXIF_FORMAT_STRING, -1,
391                                              "Nikon.AutoFocusPoint", "Auto focus point", NULL };
392                 guchar *array = item->data;
393                 gchar *text;
394                 gint l;
395
396                 text = exif_text_list_find_value(NikonAFPoint, (gint)array[1]);
397                 l = strlen(text) + 1;
398
399                 item = exif_item_new(marker.format, marker.tag, l, &marker);
400                 memcpy(item->data, text, l);
401
402                 g_free(text);
403
404                 exif->items = g_list_prepend(exif->items, item);
405                 }
406
407         item = exif_get_item(exif, "Nikon.ISOSpeed");
408         if (item && item->data_len == 2 * 2)
409                 {
410                 static ExifMarker marker = { 0x0002, EXIF_FORMAT_SHORT_UNSIGNED, 1,
411                                              "ISOSpeedRatings", "ISO speed", NULL };
412                 ExifItem *shadow;
413
414                 shadow = exif_item_new(marker.format, marker.tag, 1, &marker);
415                 memcpy(shadow->data, item->data + 2, 2);
416
417                 exif->items = g_list_prepend(exif->items, shadow);
418                 }
419
420         item = exif_get_item(exif, "Nikon.WhiteBalance");
421         if (item && item->format == EXIF_FORMAT_STRING)
422                 {
423                 static ExifMarker marker = { 0x0005, EXIF_FORMAT_STRING, -1,
424                                              "LightSource", "Light source", NULL };
425                 ExifItem *shadow;
426
427                 shadow = exif_item_new(marker.format, marker.tag, item->data_len, &marker);
428                 memcpy(shadow->data, item->data, item->data_len);
429
430                 exif->items = g_list_prepend(exif->items, shadow);
431                 }
432
433         return TRUE;
434 }
435
436 #endif
437 /* not HAVE_EXIV2 */
438 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */