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