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