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