Remove commented out code.
[geeqie.git] / src / format_olympus.c
1 /*
2  * Geeqie
3  * (C) 2005 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * This software is released under the GNU General Public License (GNU GPL).
7  * Please read the included file COPYING for more information.
8  * This software comes with no warranty of any kind, use at your own risk!
9  */
10
11 #ifdef HAVE_CONFIG_H
12 #  include "config.h"
13 #endif
14
15 #ifndef HAVE_EXIV2
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20
21 #include <glib.h>
22
23 #include "intl.h"
24
25 #include "main.h"
26 #include "format_olympus.h"
27 #include "format_raw.h"
28
29 #include "exif.h"
30
31
32 /*
33  *-----------------------------------------------------------------------------
34  * Raw ORF embedded jpeg extraction for Olympus
35  *-----------------------------------------------------------------------------
36  */
37
38 static guint olympus_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
39                                 gint level,
40                                 guint *image_offset, guint *exif_offset);
41
42
43 static void olympus_tiff_entry(guchar *data, const guint len, guint offset, ExifByteOrder bo,
44                                gint level,
45                                guint *image_offset, guint *exif_offset)
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 == 0x201)
72                 {
73                 /* start of embedded jpeg, not all olympus cameras embed a jpeg */
74                 *image_offset = exif_byte_get_int32(data + segment, bo);
75                 }
76
77         if (tag == 0x8769)
78                 {
79                 /* This is the Exif info */
80                 *exif_offset = exif_byte_get_int32(data + segment, bo);
81                 }
82 }
83
84 static guint olympus_tiff_table(guchar *data, const guint len, guint offset, ExifByteOrder bo,
85                                 gint level,
86                                 guint *image_offset, guint *exif_offset)
87 {
88         guint count;
89         guint i;
90
91         if (level > EXIF_TIFF_MAX_LEVELS) return 0;
92
93         if (len < offset + 2) return FALSE;
94
95         count = exif_byte_get_int16(data + offset, bo);
96         offset += 2;
97         if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
98
99         for (i = 0; i < count; i++)
100                 {
101                 olympus_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level,
102                                    image_offset, exif_offset);
103                 }
104
105         return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
106 }
107
108 gboolean format_olympus_raw(guchar *data, const guint len,
109                             guint *image_offset, guint *exif_offset)
110 {
111         guint i_off = 0;
112         guint e_off = 0;
113         guint offset;
114         gint level;
115
116         if (len < 8) return FALSE;
117
118         /* these are in tiff file format with a different magick header */
119         if (memcmp(data, "IIR", 3) != 0) return FALSE;
120
121         offset = exif_byte_get_int32(data + 4, EXIF_BYTE_ORDER_INTEL);
122
123         level = 0;
124         while (offset && level < EXIF_TIFF_MAX_LEVELS)
125                 {
126                 offset = olympus_tiff_table(data, len, offset, EXIF_BYTE_ORDER_INTEL, 0, &i_off, &e_off);
127                 level++;
128                 }
129
130         if (i_off != 0 || e_off != 0)
131                 {
132                 if (image_offset) *image_offset = i_off;
133                 if (exif_offset) *exif_offset = e_off;
134                 return TRUE;
135                 }
136
137         return FALSE;
138 }
139
140
141 /*
142  *-----------------------------------------------------------------------------
143  * EXIF Makernote for Olympus
144  *-----------------------------------------------------------------------------
145  */
146
147 static ExifTextList KonMinTagColorMode[]= {
148         { 0,    "natural" },
149         { 1,    "black and white" },
150         { 2,    "vivid" },
151         { 3,    "solarization" },
152         { 4,    "Adobe RGB" },
153         EXIF_TEXT_LIST_END
154 };
155
156 static ExifTextList KonMinTagQuality[]= {
157         { 0,    "raw" },
158         { 1,    "super fine" },
159         { 2,    "find" },
160         { 3,    "standard" },
161         { 4,    "extra fine" },
162         EXIF_TEXT_LIST_END
163 };
164
165 static ExifTextList OlympusTagJpegQuality[]= {
166         { 1,    "standard" },
167         { 2,    "high" },
168         { 3,    "super high" },
169         EXIF_TEXT_LIST_END
170 };
171
172 static ExifTextList OlympusTagMacro[]= {
173         { 0,    "off" },
174         { 1,    "on" },
175         { 2,    "view" },
176         { 3,    "manual" },
177         EXIF_TEXT_LIST_END
178 };
179
180 static ExifTextList OlympusTagFlashMode[]= {
181         { 0,    "auto" },
182         { 1,    "red-eye reduction" },
183         { 2,    "fill" },
184         { 3,    "off" },
185         EXIF_TEXT_LIST_END
186 };
187
188 static ExifTextList OlympusTagFocusMode[]= {
189         { 0,    "auto" },
190         { 1,    "manual" },
191         EXIF_TEXT_LIST_END
192 };
193
194 static ExifTextList OlympusTagSharpness[]= {
195         { 0,    "normal" },
196         { 1,    "hard" },
197         { 2,    "soft" },
198         EXIF_TEXT_LIST_END
199 };
200
201 static ExifTextList OlympusTagContrast[]= {
202         { 0,    "hard" },
203         { 1,    "normal" },
204         { 2,    "soft" },
205         EXIF_TEXT_LIST_END
206 };
207
208
209 static ExifMarker OlympusExifMarkersList[] = {
210 { 0x0001, EXIF_FORMAT_LONG_UNSIGNED, -1, "Konica/MinoltaSettings", "Konica / Minolta settings", NULL },
211 { 0x0003, EXIF_FORMAT_LONG_UNSIGNED, -1, "Konica/MinoltaSettings", "Konica / Minolta settings", NULL },
212 { 0x0040, EXIF_FORMAT_LONG_UNSIGNED, -1, "CompressedImageSize", "Compressed image size", NULL },
213 { 0x0081, EXIF_FORMAT_LONG_UNSIGNED, 1,  "ThumbnailOffset",     "Thumbnail offset",     NULL },
214 { 0x0088, EXIF_FORMAT_LONG_UNSIGNED, 1,  "ThumbnailOffset",     "Thumbnail offset",     NULL },
215 { 0x0089, EXIF_FORMAT_LONG_UNSIGNED, 1,  "ThumbnailLength",     "Thumbnail length",     NULL },
216 { 0x0101, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Konica/Minolta.ColorMode", "Color mode",      KonMinTagColorMode },
217 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Konica/Minolta.Quality", "Quality",           KonMinTagQuality },
218 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Konica/Minolta.Quality", "Quality",           KonMinTagQuality },
219 { 0x0200, EXIF_FORMAT_LONG_UNSIGNED, 3,  "Olympus.SpecialMode", "Special mode",         NULL },
220 { 0x0201, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.JpegQuality", "Jpeg quality",         OlympusTagJpegQuality },
221 { 0x0202, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.Macro",       "Macro",                OlympusTagMacro },
222 { 0x0204, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Olympus.DigitalZoom", "Digital zoom",      NULL },
223 { 0x0207, EXIF_FORMAT_STRING, -1,        "Olympus.Firmware",    "Firmware version",     NULL },
224 { 0x0208, EXIF_FORMAT_STRING, -1,        "Olympus.PictureInfo", "Picture info",         NULL },
225 { 0x0209, EXIF_FORMAT_UNDEFINED, -1,     "Olympus.CameraID",    "Camera ID",            NULL },
226 { 0x020b, EXIF_FORMAT_LONG_UNSIGNED, 1,  "Epson.ImageWidth",    "Image width",          NULL },
227 { 0x020c, EXIF_FORMAT_LONG_UNSIGNED, 1,  "Epson.ImageHeight",   "Image height",         NULL },
228 { 0x020d, EXIF_FORMAT_STRING, -1,        "Epson.Manufacturer",  "Manufacturer",         NULL },
229 { 0x0e00, EXIF_FORMAT_BYTE, -1,          "Olympus.PrintImageMatching", "Print image matching", NULL },
230 { 0x1004, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.FlashMode",   "Flash mode",           OlympusTagFlashMode },
231 { 0x1006, EXIF_FORMAT_RATIONAL, 1,       "Olympus.Bracket",     "Bracket",              NULL },
232 { 0x100b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.FocusMode",   "Focus mode",           OlympusTagFocusMode },
233 { 0x100c, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Olympus.FocusDistance", "Focus distance",  NULL },
234 { 0x100d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.Zoom",        "Zoom",                 NULL },
235 { 0x1006, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.MacroFocus",  "Macro focus",          NULL },
236 { 0x100f, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.Sharpness",   "Sharpness",            OlympusTagSharpness },
237 { 0x1011, EXIF_FORMAT_SHORT_UNSIGNED, 9, "Olympus.ColorMatrix", "Color matrix",         NULL },
238 { 0x1012, EXIF_FORMAT_SHORT_UNSIGNED, 4, "Olympus.BlackLevel",  "Black level",          NULL },
239 { 0x1015, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Olympus.WhiteBalance", "White balance",       NULL },
240 { 0x1017, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Olympus.RedBias",     "Red bias",             NULL },
241 { 0x1018, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Olympus.BlueBias",    "Blue bias",            NULL },
242 { 0x101a, EXIF_FORMAT_STRING, -1,        "Olympus.SerialNumber", "Serial number",       NULL },
243 { 0x1023, EXIF_FORMAT_RATIONAL, 1,       "Olympus.FlashBias",   "Flash bias",           NULL },
244 { 0x1029, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.Contrast",    "Contrast",             OlympusTagContrast },
245 { 0x102a, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.SharpnessFactor", "Sharpness factor", NULL },
246 { 0x102b, EXIF_FORMAT_SHORT_UNSIGNED, 6, "Olympus.ColorControl", "Color control",       NULL },
247 { 0x102c, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Olympus.ValidBits",   "Valid bits",           NULL },
248 { 0x102d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.CoringFilter", "Coring filter",       NULL },
249 { 0x102e, EXIF_FORMAT_LONG_UNSIGNED, 1,  "Olympus.FinalWidth",  "Final width",          NULL },
250 { 0x102f, EXIF_FORMAT_LONG_UNSIGNED, 1,  "Olympus.FinalHeight", "Final height",         NULL },
251 { 0x1034, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Olympus.CompressionRatio", "Compression ratio", NULL },
252 EXIF_MARKER_LIST_END
253 };
254
255 static ExifTextList OlympusShootingMode[]= {
256         { 0,    "normal" },
257         { 1,    "unknown" },
258         { 2,    "fast" },
259         { 3,    "panorama" },
260         EXIF_TEXT_LIST_END
261 };
262
263 static ExifTextList OlympusPanoramaDirection[]= {
264         { 1,    "left to right" },
265         { 2,    "right to left" },
266         { 3,    "bottom to top" },
267         { 4,    "top to bottom" },
268         EXIF_TEXT_LIST_END
269 };
270
271 static ExifTextList OlympusWB[]= {
272         { 1,    "auto" },
273         { 2,    "manual" },
274         { 3,    "one-touch" },
275         EXIF_TEXT_LIST_END
276 };
277
278 static ExifTextList OlympusWBColorTemp[]= {
279         { 2,    "3000" },
280         { 3,    "3700" },
281         { 4,    "4000" },
282         { 5,    "4500" },
283         { 6,    "5500" },
284         { 7,    "6500" },
285         { 8,    "7500" },
286         EXIF_TEXT_LIST_END
287 };
288
289 gboolean format_olympus_makernote(ExifData *exif, guchar *tiff, guint offset,
290                                   guint size, ExifByteOrder bo)
291 {
292         guchar *data;
293         ExifItem *item;
294
295         if (offset + 8 + 4 >= size) return FALSE;
296
297         data = tiff + offset;
298
299         /* Olympus tag format starts with "OLYMP\x00\x01" or "OLYMP\x00\x02",
300          * plus an unknown byte,
301          * followed by IFD data using Olympus tags.
302          */
303         if (memcmp(data, "OLYMP\x00\x01", 7) != 0 &&
304             memcmp(data, "OLYMP\x00\x02", 7) != 0) return FALSE;
305
306         if (exif_parse_IFD_table(exif, tiff, offset + 8, size,
307                                  bo, 0, OlympusExifMarkersList) != 0)
308                 {
309                 return FALSE;
310                 }
311
312         item = exif_get_item(exif, "Olympus.SpecialMode");
313         if (item && item->data_len == 3 * sizeof(guint32))
314                 {
315                 static ExifMarker marker = { 0x0200, EXIF_FORMAT_STRING, -1,
316                                              "Olympus.ShootingMode", "Shooting mode", NULL };
317                 guint32 *array = item->data;
318                 gchar *mode;
319                 gchar *pdir = NULL;
320                 gchar *text;
321                 gint l;
322
323                 mode = exif_text_list_find_value(OlympusShootingMode, array[0]);
324                 if (array[0] == 3)
325                         {
326                         pdir = exif_text_list_find_value(OlympusPanoramaDirection, array[2]);
327                         }
328
329                 text = g_strdup_printf("%s%s%s, seq %d", mode,
330                                        (pdir) ? " " : "", (pdir) ? pdir : "",
331                                        array[1] + 1);
332                 l = strlen(text) + 1;
333                 item = exif_item_new(marker.format, marker.tag, l, &marker);
334                 memcpy(item->data, text, l);
335
336                 g_free(text);
337                 g_free(pdir);
338                 g_free(mode);
339
340                 exif->items = g_list_prepend(exif->items, item);
341                 }
342
343         item = exif_get_item(exif, "Olympus.WhiteBalance");
344         if (item && item->data_len == 2 * sizeof(guint16))
345                 {
346                 static ExifMarker marker = { 0x1015, EXIF_FORMAT_STRING, -1,
347                                              "Olympus.WhiteBalance", "White balance", NULL };
348                 guint16 *array = item->data;
349                 gchar *mode;
350                 gchar *color = NULL;
351                 gchar *text;
352                 gint l;
353
354                 mode = exif_text_list_find_value(OlympusWB, array[0]);
355                 if (array[0] == 2)
356                         {
357                         color = exif_text_list_find_value(OlympusWBColorTemp, array[1]);
358                         }
359
360                 text = g_strdup_printf("%s%s%s", mode,
361                                        (color) ? " " : "", (color) ? color : "");
362                 l = strlen(text) + 1;
363                 item = exif_item_new(marker.format, marker.tag, l, &marker);
364                 memcpy(item->data, text, l);
365
366                 g_free(text);
367                 g_free(color);
368                 g_free(mode);
369
370                 exif->items = g_list_prepend(exif->items, item);
371                 }
372
373         return TRUE;
374 }
375
376
377 #endif
378 /* not HAVE_EXIV2 */
379 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */