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