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