Fix #381: Feature-Request: Make JPEG comment available for overlays
[geeqie.git] / src / exif.c
1 /*
2  * Copyright (C) 2003, 2006 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Authors: Eric Swalens, Quy Tonthat
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *  The tags were added with information from the FREE document:
22  *     http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
23  *
24  *  For the official Exif Format, please refer to:
25  *     http://www.exif.org
26  *     http://www.exif.org/specifications.html (PDF spec sheets)
27  *
28  *  Notes:
29  *     Additional tag formats should be added to the proper
30  *     location in ExifKnownMarkersList[].
31  *
32  *     Human readable ouput (that needs additional processing of data to
33  *     be useable) can be defined by adding a key to ExifFormattedList[],
34  *     then handling that tag in the function exif_get_formatted_by_key().
35  *     The human readable formatted keys must begin with the character 'f'.
36  *
37  *  Unsupported at this time:
38  *     IFD1 (thumbnail)
39  *     MakerNote
40  *
41  *  TODO:
42  *     Convert data to useable form in the ??_as_text function for:
43  *        ComponentsConfiguration
44  *        UserComment (convert this to UTF-8?)
45  *     Add support for marker tag 0x0000
46  */
47
48 #ifdef HAVE_CONFIG_H
49 #  include "config.h"
50 #endif
51
52 #ifndef HAVE_EXIV2
53
54 #include <stdio.h>
55 #include <string.h>
56 #include <fcntl.h>
57 #include <unistd.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <sys/mman.h>
61 #include <math.h>
62
63 #include <glib.h>
64 #include <glib/gprintf.h>
65
66 #include "intl.h"
67
68 #include "main.h"
69 #include "exif-int.h"
70 #include "jpeg_parser.h"
71
72 #include "format_raw.h"
73 #include "ui_fileops.h"
74
75
76 /*
77  *-----------------------------------------------------------------------------
78  * Tag formats
79  *-----------------------------------------------------------------------------
80  */
81
82 ExifFormatAttrib ExifFormatList[] = {
83         { EXIF_FORMAT_UNKNOWN,          1, "unknown",   "unknown" },
84         { EXIF_FORMAT_BYTE_UNSIGNED,    1, "ubyte",     "unsigned byte" },
85         { EXIF_FORMAT_STRING,           1, "string",    "string" },
86         { EXIF_FORMAT_SHORT_UNSIGNED,   2, "ushort",    "unsigned short" },
87         { EXIF_FORMAT_LONG_UNSIGNED,    4, "ulong",     "unsigned long" },
88         { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
89         { EXIF_FORMAT_BYTE,             1, "byte",      "byte" },
90         { EXIF_FORMAT_UNDEFINED,        1, "undefined", "undefined" },
91         { EXIF_FORMAT_SHORT,            2, "sshort",    "signed short" },
92         { EXIF_FORMAT_LONG,             4, "slong",     "signed long" },
93         { EXIF_FORMAT_RATIONAL,         8, "srational", "signed rational" },
94         { EXIF_FORMAT_FLOAT,            4, "float",     "float" },
95         { EXIF_FORMAT_DOUBLE,           8, "double",    "double" },
96         { -1, 0, NULL, NULL }
97 };
98
99 /* tags that are special, or need special treatment */
100 #define TAG_EXIFOFFSET          0x8769
101 #define TAG_EXIFMAKERNOTE       0x927c
102 #define TAG_GPSOFFSET           0x8825
103
104
105 /*
106  *-----------------------------------------------------------------------------
107  * Data
108  *-----------------------------------------------------------------------------
109  */
110 static ExifTextList ExifCompressionList[] = {
111         { 1, "Uncompressed" },
112         { 2, "CCITT 1D" },
113         { 3, "T4/Group 3 Fax" },
114         { 4, "T6/Group 4 Fax" },
115         { 5, "LZW" },
116         { 6, "JPEG (old style)" },
117         { 7, "JPEG" },
118         { 8, "Adobe Deflate" },
119         { 9, "JBIG B&W" },
120         { 10, "JBIG Color" },
121         { 32766, "Next" },
122         { 32771, "CCIRLEW" },
123         { 32773, "PackBits" },
124         { 32809, "ThunderScan" },
125         { 32895, "IT8CTPAD" },
126         { 32896, "IT8LW" },
127         { 32897, "IT8MP" },
128         { 32898, "IT8BL" },
129         { 32908, "PixasFilm" },
130         { 32909, "PixasLog" },
131         { 32946, "Deflate" },
132         { 32947, "DCS" },
133         { 34661, "JBIG" },
134         { 34676, "SGILog" },
135         { 34677, "SGILog24" },
136         { 34712, "JPEF 2000" },
137         { 34713, "Nikon NEF Compressed" },
138         EXIF_TEXT_LIST_END
139 };
140
141 static ExifTextList ExifOrientationList[] = {
142         { EXIF_ORIENTATION_UNKNOWN,     N_("unknown") },
143         { EXIF_ORIENTATION_TOP_LEFT,    N_("top left") },
144         { EXIF_ORIENTATION_TOP_RIGHT,   N_("top right") },
145         { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
146         { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
147         { EXIF_ORIENTATION_LEFT_TOP,    N_("left top") },
148         { EXIF_ORIENTATION_RIGHT_TOP,   N_("right top") },
149         { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
150         { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
151         EXIF_TEXT_LIST_END
152 };
153
154 static ExifTextList ExifUnitList[] = {
155         { EXIF_UNIT_UNKNOWN,    N_("unknown") },
156         { EXIF_UNIT_NOUNIT,     "" },
157         { EXIF_UNIT_INCH,       N_("inch") },
158         { EXIF_UNIT_CENTIMETER, N_("centimeter") },
159         EXIF_TEXT_LIST_END
160 };
161
162 static ExifTextList ExifYCbCrPosList[] = {
163         { 1,    "center" },
164         { 2,    "datum" },
165         EXIF_TEXT_LIST_END
166 };
167
168 static ExifTextList ExifMeteringModeList[] = {
169         { 0,    N_("unknown") },
170         { 1,    N_("average") },
171         { 2,    N_("center weighted") },
172         { 3,    N_("spot") },
173         { 4,    N_("multi-spot") },
174         { 5,    N_("multi-segment") },
175         { 6,    N_("partial") },
176         { 255,  N_("other") },
177         EXIF_TEXT_LIST_END
178 };
179
180 static ExifTextList ExifExposureProgramList[] = {
181         { 0,    N_("not defined") },
182         { 1,    N_("manual") },
183         { 2,    N_("normal") },
184         { 3,    N_("aperture") },
185         { 4,    N_("shutter") },
186         { 5,    N_("creative") },
187         { 6,    N_("action") },
188         { 7,    N_("portrait") },
189         { 8,    N_("landscape") },
190         EXIF_TEXT_LIST_END
191 };
192
193 static ExifTextList ExifLightSourceList[] = {
194         { 0,    N_("unknown") },
195         { 1,    N_("daylight") },
196         { 2,    N_("fluorescent") },
197         { 3,    N_("tungsten (incandescent)") },
198         { 4,    N_("flash") },
199         { 9,    N_("fine weather") },
200         { 10,   N_("cloudy weather") },
201         { 11,   N_("shade") },
202         { 12,   N_("daylight fluorescent") },
203         { 13,   N_("day white fluorescent") },
204         { 14,   N_("cool white fluorescent") },
205         { 15,   N_("white fluorescent") },
206         { 17,   N_("standard light A") },
207         { 18,   N_("standard light B") },
208         { 19,   N_("standard light C") },
209         { 20,   N_("D55") },
210         { 21,   N_("D65") },
211         { 22,   N_("D75") },
212         { 23,   N_("D50") },
213         { 24,   N_("ISO studio tungsten") },
214         { 255,  N_("other") },
215         EXIF_TEXT_LIST_END
216 };
217
218 static ExifTextList ExifFlashList[] = {
219         { 0,    N_("no") },
220         { 1,    N_("yes") },
221         { 5,    N_("yes, not detected by strobe") },
222         { 7,    N_("yes, detected by strobe") },
223         EXIF_TEXT_LIST_END
224 };
225
226 static ExifTextList ExifColorSpaceList[] = {
227         { 1,    N_("sRGB") },
228         { 65535,N_("uncalibrated") },
229         EXIF_TEXT_LIST_END
230 };
231
232 static ExifTextList ExifSensorList[] = {
233         { 1,    N_("not defined") },
234         { 2,    N_("1 chip color area") },
235         { 2,    N_("2 chip color area") },
236         { 4,    N_("3 chip color area") },
237         { 5,    N_("color sequential area") },
238         { 7,    N_("trilinear") },
239         { 8,    N_("color sequential linear") },
240         EXIF_TEXT_LIST_END
241 };
242
243 static ExifTextList ExifSourceList[] = {
244         { 3,    N_("digital still camera") },
245         EXIF_TEXT_LIST_END
246 };
247
248 static ExifTextList ExifSceneList[] = {
249         { 1,    N_("direct photo") },
250         EXIF_TEXT_LIST_END
251 };
252
253 static ExifTextList ExifCustRenderList[] = {
254         { 0,    N_("normal") },
255         { 1,    N_("custom") },
256         EXIF_TEXT_LIST_END
257 };
258
259 static ExifTextList ExifExposureModeList[] = {
260         { 0,    N_("auto") },
261         { 1,    N_("manual") },
262         { 2,    N_("auto bracket") },
263         EXIF_TEXT_LIST_END
264 };
265
266 static ExifTextList ExifWhiteBalanceList[] = {
267         { 0,    N_("auto") },
268         { 1,    N_("manual") },
269         EXIF_TEXT_LIST_END
270 };
271
272 static ExifTextList ExifSceneCaptureList[] = {
273         { 0,    N_("standard") },
274         { 1,    N_("landscape") },
275         { 2,    N_("portrait") },
276         { 3,    N_("night scene") },
277         EXIF_TEXT_LIST_END
278 };
279
280 static ExifTextList ExifGainControlList[] = {
281         { 0,    N_("none") },
282         { 1,    N_("low gain up") },
283         { 2,    N_("high gain up") },
284         { 3,    N_("low gain down") },
285         { 4,    N_("high gain down") },
286         EXIF_TEXT_LIST_END
287 };
288
289 static ExifTextList ExifContrastList[] = {
290         { 0,    N_("normal") },
291         { 1,    N_("soft") },
292         { 2,    N_("hard") },
293         EXIF_TEXT_LIST_END
294 };
295
296 static ExifTextList ExifSaturationList[] = {
297         { 0,    N_("normal") },
298         { 1,    N_("low") },
299         { 2,    N_("high") },
300         EXIF_TEXT_LIST_END
301 };
302
303 static ExifTextList ExifSharpnessList[] = {
304         { 0,    N_("normal") },
305         { 1,    N_("soft") },
306         { 2,    N_("hard") },
307         EXIF_TEXT_LIST_END
308 };
309
310 static ExifTextList ExifSubjectRangeList[] = {
311         { 0,    N_("unknown") },
312         { 1,    N_("macro") },
313         { 2,    N_("close") },
314         { 3,    N_("distant") },
315         EXIF_TEXT_LIST_END
316 };
317
318 /*
319 Tag names should match to exiv2 keys, http://www.exiv2.org/metadata.html
320 Tags that don't match are not supported by exiv2 and should not be used anywhere in the code
321 */
322
323 ExifMarker ExifKnownMarkersList[] = {
324 { 0x0100, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ImageWidth",        N_("Image Width"), NULL },
325 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ImageLength",       N_("Image Height"), NULL },
326 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.BitsPerSample",     N_("Bits per Sample/Pixel"), NULL },
327 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.Compression",       N_("Compression"), ExifCompressionList },
328 { 0x010e, EXIF_FORMAT_STRING, -1,               "Exif.Image.ImageDescription",  N_("Image description"), NULL },
329 { 0x010f, EXIF_FORMAT_STRING, -1,               "Exif.Image.Make",              N_("Camera make"), NULL },
330 { 0x0110, EXIF_FORMAT_STRING, -1,               "Exif.Image.Model",             N_("Camera model"), NULL },
331 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.Orientation",       N_("Orientation"), ExifOrientationList },
332 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.XResolution",       N_("X resolution"), NULL },
333 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.YResolution",       N_("Y Resolution"), NULL },
334 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.ResolutionUnit",    N_("Resolution units"), ExifUnitList },
335 { 0x0131, EXIF_FORMAT_STRING, -1,               "Exif.Image.Software",          N_("Firmware"), NULL },
336 { 0x0132, EXIF_FORMAT_STRING, 20,               "Exif.Image.DateTime",          N_("Date"), NULL },
337 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2,     "Exif.Image.WhitePoint",        N_("White point"), NULL },
338 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6,     "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), NULL },
339 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3,     "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), NULL },
340 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.YCbCrPositioning",  N_("YCbCr positioning"), ExifYCbCrPosList },
341 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6,     "Exif.Image.ReferenceBlackWhite",N_("Black white reference"), NULL },
342 { 0x8298, EXIF_FORMAT_STRING, -1,               "Exif.Image.Copyright",         N_("Copyright"), NULL },
343 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ExifTag",           N_("SubIFD Exif offset"), NULL },
344         /* subIFD follows */
345 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureTime",      N_("Exposure time (seconds)"), NULL },
346 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FNumber",           N_("FNumber"), NULL },
347 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ExposureProgram",   N_("Exposure program"), ExifExposureProgramList },
348 { 0x8824, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SpectralSensitivity",N_("Spectral Sensitivity"), NULL },
349 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.ISOSpeedRatings",   N_("ISO sensitivity"), NULL },
350 { 0x8828, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.OECF",              N_("Optoelectric conversion factor"), NULL },
351 { 0x9000, EXIF_FORMAT_UNDEFINED, 4,             "Exif.Photo.ExifVersion",       N_("Exif version"), NULL },
352 { 0x9003, EXIF_FORMAT_STRING, 20,               "Exif.Photo.DateTimeOriginal",  N_("Date original"), NULL },
353 { 0x9004, EXIF_FORMAT_STRING, 20,               "Exif.Photo.DateTimeDigitized", N_("Date digitized"), NULL },
354 { 0x9101, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), NULL },
355 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1,      "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), NULL },
356 { 0x9201, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), NULL },
357 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ApertureValue",     N_("Aperture"), NULL },
358 { 0x9203, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.BrightnessValue",   N_("Brightness"), NULL },
359 { 0x9204, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), NULL },
360 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.MaxApertureValue",  N_("Maximum aperture"), NULL },
361 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.SubjectDistance",   N_("Subject distance"), NULL },
362 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.MeteringMode",      N_("Metering mode"), ExifMeteringModeList },
363 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.LightSource",       N_("Light source"), ExifLightSourceList },
364 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Flash",             N_("Flash"), ExifFlashList },
365 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalLength",       N_("Focal length"), NULL },
366 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.SubjectArea",       N_("Subject area"), NULL },
367 { 0x927c, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.MakerNote",         N_("MakerNote"), NULL },
368 { 0x9286, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.UserComment",       N_("UserComment"), NULL },
369 { 0x9290, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTime",        N_("Subsecond time"), NULL },
370 { 0x9291, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), NULL },
371 { 0x9292, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), NULL },
372 { 0xa000, EXIF_FORMAT_UNDEFINED, 4,             "Exif.Photo.FlashpixVersion",   N_("FlashPix version"), NULL },
373 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ColorSpace",        N_("Colorspace"), ExifColorSpaceList },
374         /* ExifImageWidth, ExifImageHeight can also be unsigned short */
375 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Photo.PixelXDimension",   N_("Width"), NULL },
376 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Photo.PixelYDimension",   N_("Height"), NULL },
377 { 0xa004, EXIF_FORMAT_STRING, -1,               "Exif.Photo.RelatedSoundFile",  N_("Audio data"), NULL },
378 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1,         "ExifInteroperabilityOffset",   N_("ExifR98 extension"), NULL },
379 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FlashEnergy",       N_("Flash strength"), NULL },
380 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), NULL },
381 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), NULL },
382 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), NULL },
383 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.FocalPlaneResolutionUnit", N_("Pixel density units"), ExifUnitList },
384 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2,        "Exif.Photo.SubjectLocation",   N_("Subject location"), NULL },
385 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureIndex",     N_("ISO sensitivity"), NULL },
386 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.SensingMethod",     N_("Sensor type"), ExifSensorList },
387 { 0xa300, EXIF_FORMAT_UNDEFINED, 1,             "Exif.Photo.FileSource",        N_("Source type"), ExifSourceList },
388 { 0xa301, EXIF_FORMAT_UNDEFINED, 1,             "Exif.Photo.SceneType",         N_("Scene type"), ExifSceneList },
389 { 0xa302, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Image.CFAPattern",        N_("Color filter array pattern"), NULL },
390         /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
391 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.CustomRendered",    N_("Render process"), ExifCustRenderList },
392 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ExposureMode",      N_("Exposure mode"), ExifExposureModeList },
393 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.WhiteBalance",      N_("White balance"), ExifWhiteBalanceList },
394 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.DigitalZoomRatio",  N_("Digital zoom ratio"), NULL },
395 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), NULL },
396 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SceneCaptureType",  N_("Scene capture type"), ExifSceneCaptureList },
397 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.GainControl",       N_("Gain control"), ExifGainControlList },
398 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Contrast",          N_("Contrast"), ExifContrastList },
399 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Saturation",        N_("Saturation"), ExifSaturationList },
400 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Sharpness",         N_("Sharpness"), ExifSharpnessList },
401 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.DeviceSettingDescription",N_("Device setting"), NULL },
402 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SubjectDistanceRange",N_("Subject range"), ExifSubjectRangeList },
403 { 0xa420, EXIF_FORMAT_STRING, -1,               "Exif.Photo.ImageUniqueID",     N_("Image serial number"), NULL },
404         /* place known, but undocumented or lesser used tags here */
405 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.NewSubfileType",    NULL, NULL },
406 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "SubfileType",                  NULL, NULL },
407 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3,        "Exif.Image.TransferFunction",  NULL, NULL },
408 { 0x013b, EXIF_FORMAT_STRING, -1,               "Exif.Image.Artist",            "Artist", NULL },
409 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Predictor",            NULL, NULL },
410 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "TileWidth",            NULL, NULL },
411 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "TileLength",           NULL, NULL },
412 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1,        "TileOffsets",          NULL, NULL },
413 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "TileByteCounts",       NULL, NULL },
414 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1,        "Exif.Image.SubIFDs",           NULL, NULL },
415 { 0x015b, EXIF_FORMAT_UNDEFINED, -1,            "JPEGTables",           NULL, NULL },
416 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2,        "Exif.Image.CFARepeatPatternDim",       NULL, NULL },
417 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1,        "Exif.Image.CFAPattern",                NULL, NULL },
418 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.BatteryLevel",              NULL, NULL },
419 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1,        "IPTC/NAA",             NULL, NULL },
420 { 0x8773, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Image.InterColorProfile",         NULL, NULL },
421 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1,         "GPSInfo",              "SubIFD GPS offset", NULL },
422 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Interlace",            NULL, NULL },
423 { 0x882a, EXIF_FORMAT_SHORT, 1,                 "TimeZoneOffset",       NULL, NULL },
424 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "SelfTimerMode",        NULL, NULL },
425 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FlashEnergy",               NULL, NULL },
426 { 0x920c, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.SpatialFrequencyResponse", NULL, NULL },
427 { 0x920d, EXIF_FORMAT_UNDEFINED, -1,            "Noise",                NULL, NULL },
428 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1,         "ImageNumber",          NULL, NULL },
429 { 0x9212, EXIF_FORMAT_STRING, 1,                "SecurityClassification", NULL, NULL },
430 { 0x9213, EXIF_FORMAT_STRING, -1,               "ImageHistory",         NULL, NULL },
431 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureIndex",     NULL, NULL },
432 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4,         "TIFF/EPStandardID",    NULL, NULL },
433
434 EXIF_MARKER_LIST_END
435 };
436
437 ExifMarker ExifKnownGPSInfoMarkersList[] = {
438         /* The following do not work at the moment as the tag value 0x0000 has a
439          * special meaning. */
440         /* { 0x0000, EXIF_FORMAT_BYTE, -1, "Exif.GPSInfo.GPSVersionID", NULL, NULL }, */
441         { 0x0001, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLatitudeRef", NULL, NULL },
442         { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", NULL, NULL },
443         { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", NULL, NULL },
444         { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", NULL, NULL },
445         { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", NULL, NULL },
446         { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", NULL, NULL },
447         { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", NULL, NULL },
448         { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", NULL, NULL },
449         { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", NULL, NULL },
450         { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", NULL, NULL },
451         { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", NULL, NULL },
452         { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", NULL, NULL },
453         { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", NULL, NULL },
454         { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", NULL, NULL },
455         { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", NULL, NULL },
456         { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", NULL, NULL },
457         { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", NULL, NULL },
458         { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", NULL, NULL },
459         { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", NULL, NULL },
460         { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", NULL, NULL },
461         { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", NULL, NULL },
462         { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", NULL, NULL },
463         { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", NULL, NULL },
464         { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", NULL, NULL },
465         { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", NULL, NULL },
466         { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", NULL, NULL },
467         { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", NULL, NULL },
468         { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", NULL, NULL },
469         { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", NULL, NULL },
470         { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", NULL, NULL },
471
472         EXIF_MARKER_LIST_END
473 };
474
475 ExifMarker ExifUnknownMarkersList[] = {
476 { 0x0000, EXIF_FORMAT_UNKNOWN, 0,               "unknown",      NULL, NULL },
477 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1,        "unknown",      NULL, NULL },
478 { 0x0000, EXIF_FORMAT_STRING, -1,               "unknown",      NULL, NULL },
479 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "unknown",      NULL, NULL },
480 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1,        "unknown",      NULL, NULL },
481 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1,    "unknown",      NULL, NULL },
482 { 0x0000, EXIF_FORMAT_BYTE, -1,                 "unknown",      NULL, NULL },
483 { 0x0000, EXIF_FORMAT_UNDEFINED, -1,            "unknown",      NULL, NULL },
484 { 0x0000, EXIF_FORMAT_SHORT, -1,                "unknown",      NULL, NULL },
485 { 0x0000, EXIF_FORMAT_LONG, -1,                 "unknown",      NULL, NULL },
486 { 0x0000, EXIF_FORMAT_RATIONAL, -1,             "unknown",      NULL, NULL },
487 { 0x0000, EXIF_FORMAT_FLOAT, -1,                "unknown",      NULL, NULL },
488 { 0x0000, EXIF_FORMAT_DOUBLE, -1,               "unknown",      NULL, NULL },
489 };
490
491 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
492
493 /*
494  *-----------------------------------------------------------------------------
495  * ExifItem
496  *-----------------------------------------------------------------------------
497  */
498
499 ExifItem *exif_item_new(ExifFormatType format, guint tag,
500                         guint elements, const ExifMarker *marker)
501 {
502         ExifItem *item;
503
504         item = g_new0(ExifItem, 1);
505         item->format = format;
506         item->tag = tag;
507         item->marker = marker;
508         item->elements = elements;
509
510         switch (format)
511                 {
512                 case EXIF_FORMAT_UNKNOWN:
513                         /* unknown, data is NULL */
514                         return item;
515                         break;
516                 case EXIF_FORMAT_BYTE_UNSIGNED:
517                         item->data_len = sizeof(gchar) * elements;
518                         break;
519                 case EXIF_FORMAT_STRING:
520                         item->data_len = sizeof(gchar) * elements;
521                         break;
522                 case EXIF_FORMAT_SHORT_UNSIGNED:
523                         item->data_len = sizeof(guint16) * elements;
524                         break;
525                 case EXIF_FORMAT_LONG_UNSIGNED:
526                         item->data_len = sizeof(guint32) * elements;
527                         break;
528                 case EXIF_FORMAT_RATIONAL_UNSIGNED:
529                         item->data_len = sizeof(ExifRational) * elements;
530                         break;
531                 case EXIF_FORMAT_BYTE:
532                         item->data_len = sizeof(gchar) * elements;
533                         break;
534                 case EXIF_FORMAT_UNDEFINED:
535                         item->data_len = sizeof(gchar) * elements;
536                         break;
537                 case EXIF_FORMAT_SHORT:
538                         item->data_len = sizeof(gint16) * elements;
539                         break;
540                 case EXIF_FORMAT_LONG:
541                         item->data_len = sizeof(gint32) * elements;
542                         break;
543                 case EXIF_FORMAT_RATIONAL:
544                         item->data_len = sizeof(ExifRational) * elements;
545                         break;
546                 case EXIF_FORMAT_FLOAT:
547                         item->data_len = sizeof(float) * elements;
548                         break;
549                 case EXIF_FORMAT_DOUBLE:
550                         item->data_len = sizeof(gdouble) * elements;
551                         break;
552                 }
553
554         item->data = g_malloc0(item->data_len);
555
556         return item;
557 }
558
559 static void exif_item_free(ExifItem *item)
560 {
561         if (!item) return;
562
563         g_free(item->data);
564         g_free(item);
565 }
566
567 gchar *exif_item_get_tag_name(ExifItem *item)
568 {
569         if (!item || !item->marker) return NULL;
570         return g_strdup(item->marker->key);
571 }
572
573 guint exif_item_get_tag_id(ExifItem *item)
574 {
575         if (!item) return 0;
576         return item->tag;
577 }
578
579 guint exif_item_get_elements(ExifItem *item)
580 {
581         if (!item) return 0;
582         return item->elements;
583 }
584
585 gchar *exif_item_get_data(ExifItem *item, guint *data_len)
586 {
587         if (data_len)
588                 *data_len = item->data_len;
589         return g_memdup(item->data, item->data_len);
590 }
591
592 guint exif_item_get_format_id(ExifItem *item)
593 {
594         if (!item) return EXIF_FORMAT_UNKNOWN;
595         return item->format;
596 }
597
598
599 gchar *exif_item_get_description(ExifItem *item)
600 {
601         if (!item || !item->marker) return NULL;
602         return g_strdup(_(item->marker->description));
603 }
604
605 const gchar *exif_item_get_format_name(ExifItem *item, gboolean brief)
606 {
607         if (!item || !item->marker) return NULL;
608         return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
609 }
610
611 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
612 {
613         gint i;
614
615         for (i = 0 ; i < ne; i++)
616                 {
617                 guchar c = ((gchar *)data)[i];
618                 if (c < 32 || c > 127) c = '.';
619                 g_string_append_printf(string, "%c", c);
620                 }
621         string = g_string_append(string, " : ");
622         for (i = 0 ; i < ne; i++)
623                 {
624                 const gchar *spacer;
625                 if (i > 0)
626                         {
627                         if (i%8 == 0)
628                                 {
629                                 spacer = " - ";
630                                 }
631                         else
632                                 {
633                                 spacer = " ";
634                                 }
635                         }
636                 else
637                         {
638                         spacer = "";
639                         }
640                 g_string_append_printf(string, "%s%02x", spacer, ((gchar *)data)[i]);
641                 }
642
643         return string;
644 }
645
646
647 gchar *exif_text_list_find_value(ExifTextList *list, guint value)
648 {
649         gchar *result = NULL;
650         gint i;
651
652         i = 0;
653         while (!result && list[i].value >= 0)
654                 {
655                 if (value == (guint) list[i].value) result = g_strdup(_(list[i].description));
656                 i++;
657                 }
658         if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
659
660         return result;
661 }
662
663
664 /*
665  *-------------------------------------------------------------------
666  * byte order utils
667  *-------------------------------------------------------------------
668  */
669
670 /* note: the align_buf is used to avoid alignment issues (on sparc) */
671
672 guint16 exif_byte_get_int16(guchar *f, ExifByteOrder bo)
673 {
674         guint16 align_buf;
675
676         memcpy(&align_buf, f, sizeof(guint16));
677
678         if (bo == EXIF_BYTE_ORDER_INTEL)
679                 return GUINT16_FROM_LE(align_buf);
680         else
681                 return GUINT16_FROM_BE(align_buf);
682 }
683
684 guint32 exif_byte_get_int32(guchar *f, ExifByteOrder bo)
685 {
686         guint32 align_buf;
687
688         memcpy(&align_buf, f, sizeof(guint32));
689
690         if (bo == EXIF_BYTE_ORDER_INTEL)
691                 return GUINT32_FROM_LE(align_buf);
692         else
693                 return GUINT32_FROM_BE(align_buf);
694 }
695
696 void exif_byte_put_int16(guchar *f, guint16 n, ExifByteOrder bo)
697 {
698         guint16 align_buf;
699
700         if (bo == EXIF_BYTE_ORDER_INTEL)
701                 {
702                 align_buf = GUINT16_TO_LE(n);
703                 }
704         else
705                 {
706                 align_buf = GUINT16_TO_BE(n);
707                 }
708
709         memcpy(f, &align_buf, sizeof(guint16));
710 }
711
712 void exif_byte_put_int32(guchar *f, guint32 n, ExifByteOrder bo)
713 {
714         guint32 align_buf;
715
716         if (bo == EXIF_BYTE_ORDER_INTEL)
717                 {
718                 align_buf = GUINT32_TO_LE(n);
719                 }
720         else
721                 {
722                 align_buf = GUINT32_TO_BE(n);
723                 }
724
725         memcpy(f, &align_buf, sizeof(guint32));
726 }
727
728
729 /*
730  *-------------------------------------------------------------------
731  * IFD utils
732  *-------------------------------------------------------------------
733  */
734
735 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list)
736 {
737         gint i = 0;
738
739         if (!list) return NULL;
740
741         while (list[i].tag != 0 && list[i].tag != tag)
742                 {
743                 i++;
744                 }
745
746         return (list[i].tag == 0 ? NULL : &list[i]);
747 }
748
749 static void rational_from_data(ExifRational *r, gpointer src, ExifByteOrder bo)
750 {
751         r->num = exif_byte_get_int32(src, bo);
752         r->den = exif_byte_get_int32(src + sizeof(guint32), bo);
753 }
754
755 /* src_format and item->format must be compatible
756  * and not overrun src or item->data.
757  */
758 void exif_item_copy_data(ExifItem *item, gpointer src, guint len,
759                          ExifFormatType src_format, ExifByteOrder bo)
760 {
761         gint bs;
762         gint ne;
763         gpointer dest;
764         gint i;
765
766         bs = ExifFormatList[item->format].size;
767         ne = item->elements;
768         dest = item->data;
769
770         if (!dest ||
771             ExifFormatList[src_format].size * ne > len)
772                 {
773                 gchar *tag = exif_item_get_tag_name(item);
774                 log_printf("exif tag %s data size mismatch\n", tag);
775                 g_free(tag);
776                 return;
777                 }
778
779         switch (item->format)
780                 {
781                 case EXIF_FORMAT_UNKNOWN:
782                         break;
783                 case EXIF_FORMAT_BYTE_UNSIGNED:
784                 case EXIF_FORMAT_BYTE:
785                 case EXIF_FORMAT_UNDEFINED:
786                         memcpy(dest, src, len);
787                         break;
788                 case EXIF_FORMAT_STRING:
789                         memcpy(dest, src, len);
790                         /* string is NULL terminated, make sure this is true */
791                         if (((gchar *)dest)[len - 1] != '\0') ((gchar *)dest)[len - 1] = '\0';
792                         break;
793                 case EXIF_FORMAT_SHORT_UNSIGNED:
794                 case EXIF_FORMAT_SHORT:
795                         for (i = 0; i < ne; i++)
796                                 {
797                                 ((guint16 *)dest)[i] = exif_byte_get_int16(src + i * bs, bo);
798                                 }
799                         break;
800                 case EXIF_FORMAT_LONG_UNSIGNED:
801                 case EXIF_FORMAT_LONG:
802                         if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
803                             src_format == EXIF_FORMAT_SHORT)
804                                 {
805                                 /* a short fits into a long, so allow it */
806                                 gint ss;
807
808                                 ss = ExifFormatList[src_format].size;
809                                 for (i = 0; i < ne; i++)
810                                         {
811                                         ((gint32 *)dest)[i] =
812                                                 (gint32)exif_byte_get_int16(src + i * ss, bo);
813                                         }
814                                 }
815                         else
816                                 {
817                                 for (i = 0; i < ne; i++)
818                                         {
819                                         ((gint32 *)dest)[i] =
820                                                 exif_byte_get_int32(src + i * bs, bo);
821                                         }
822                                 }
823                         break;
824                 case EXIF_FORMAT_RATIONAL_UNSIGNED:
825                 case EXIF_FORMAT_RATIONAL:
826                         for (i = 0; i < ne; i++)
827                                 {
828                                 rational_from_data(&((ExifRational *)dest)[i], src + i * bs, bo);
829                                 }
830                         break;
831                 case EXIF_FORMAT_FLOAT:
832                         for (i = 0; i < ne; i++)
833                                 {
834                                 ((float *)dest)[i] = exif_byte_get_int32(src + i * bs, bo);
835                                 }
836                         break;
837                 case EXIF_FORMAT_DOUBLE:
838                         for (i = 0; i < ne; i++)
839                                 {
840                                 ExifRational r;
841
842                                 rational_from_data(&r, src + i * bs, bo);
843                                 if (r.den) ((gdouble *)dest)[i] = (gdouble)r.num / r.den;
844                                 }
845                         break;
846                 }
847 }
848
849 static gint exif_parse_IFD_entry(ExifData *exif, guchar *tiff, guint offset,
850                                  guint size, ExifByteOrder bo,
851                                  gint level,
852                                  const ExifMarker *list)
853 {
854         guint tag;
855         guint format;
856         guint count;
857         guint data_val;
858         guint data_offset;
859         guint data_length;
860         const ExifMarker *marker;
861         ExifItem *item;
862
863         tag = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_TAG, bo);
864         format = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
865         count = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_COUNT, bo);
866         data_val = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_DATA, bo);
867
868         /* Check tag type. If it does not match, either the format is wrong,
869          * either it is a unknown tag; so it is not really an error.
870          */
871         marker = exif_marker_from_tag(tag, list);
872         if (!marker)
873                 {
874                 if (format >= EXIF_FORMAT_COUNT)
875                         {
876                         log_printf("warning: exif tag 0x%4x has invalid format %d\n", tag, format);
877                         return 0;
878                         }
879                 /* allow non recognized tags to be displayed */
880                 marker = &ExifUnknownMarkersList[format];
881                 }
882         if (marker->format != format)
883                 {
884                 /* Some cameras got mixed up signed/unsigned_rational
885                  * eg KODAK DC4800 on object_distance tag
886                  *
887                  * FIXME: what exactly is this test trying to do?
888                  * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
889                  */
890                 if (!(marker->format == EXIF_FORMAT_RATIONAL_UNSIGNED && format == EXIF_FORMAT_RATIONAL) &&
891                     !(marker->format == EXIF_FORMAT_RATIONAL && format == EXIF_FORMAT_RATIONAL_UNSIGNED) &&
892                         /* short fits into a long so allow this mismatch
893                          * as well (some tags allowed to be unsigned short _or_ unsigned long)
894                          */
895                     !(marker->format == EXIF_FORMAT_LONG_UNSIGNED && format == EXIF_FORMAT_SHORT_UNSIGNED) )
896                         {
897                         if (format < EXIF_FORMAT_COUNT)
898                                 {
899                                 log_printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
900                                         marker->key, ExifFormatList[format].short_name,
901                                         ExifFormatList[marker->format].short_name);
902                                 }
903                         else
904                                 {
905                                 log_printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
906                                         marker->key, format, marker->format,
907                                         ExifFormatList[marker->format].short_name);
908                                 }
909                         return 0;
910                         }
911                 }
912
913         /* Where is the data, is it available?
914          */
915         if (marker->components > 0 && (guint) marker->components != count)
916                 {
917                 log_printf("warning: exif tag %s has %d elements, exif spec requests %d\n",
918                         marker->key, count, marker->components);
919                 }
920
921         data_length = ExifFormatList[marker->format].size * count;
922         if (data_length > 4)
923                 {
924                 data_offset = data_val;
925                 if (size < data_offset || size < data_offset + data_length)
926                         {
927                         log_printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
928                         return -1;
929                         }
930                 }
931         else
932                 {
933                 data_offset = offset + EXIF_TIFD_OFFSET_DATA;
934                 }
935
936         item = exif_item_new(marker->format, tag, count, marker);
937         exif_item_copy_data(item, tiff + data_offset, data_length, format, bo);
938         exif->items = g_list_prepend(exif->items, item);
939
940         if (list == ExifKnownMarkersList)
941                 {
942                 switch (item->tag)
943                         {
944                         case TAG_EXIFOFFSET:
945                                 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, list);
946                                 break;
947                         case TAG_GPSOFFSET:
948                                 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, ExifKnownGPSInfoMarkersList);
949                                 break;
950                         case TAG_EXIFMAKERNOTE:
951                                 format_exif_makernote_parse(exif, tiff, data_val, size, bo);
952                                 break;
953                         }
954                 }
955
956         return 0;
957 }
958
959 gint exif_parse_IFD_table(ExifData *exif,
960                           guchar *tiff, guint offset,
961                           guint size, ExifByteOrder bo,
962                           gint level,
963                           const ExifMarker *list)
964 {
965         guint count;
966         guint i;
967
968         /* limit damage from infinite loops */
969         if (level > EXIF_TIFF_MAX_LEVELS) return -1;
970
971         /* We should be able to read number of entries in IFD0) */
972         if (size < offset + 2) return -1;
973
974         count = exif_byte_get_int16(tiff + offset, bo);
975         offset += 2;
976
977         /* Entries and next IFD offset must be readable */
978         if (size < offset + count * EXIF_TIFD_SIZE + 4) return -1;
979
980         for (i = 0; i < count; i++)
981                 {
982                 exif_parse_IFD_entry(exif, tiff, offset + i * EXIF_TIFD_SIZE, size, bo, level, list);
983                 }
984
985         return 0;
986 }
987
988 /*
989  *-------------------------------------------------------------------
990  * file formats
991  *-------------------------------------------------------------------
992  */
993
994 gint exif_tiff_directory_offset(guchar *data, const guint len,
995                                 guint *offset, ExifByteOrder *bo)
996 {
997         if (len < 8) return FALSE;
998
999         if (memcmp(data, "II", 2) == 0)
1000                 {
1001                 *bo = EXIF_BYTE_ORDER_INTEL;
1002                 }
1003         else if (memcmp(data, "MM", 2) == 0)
1004                 {
1005                 *bo = EXIF_BYTE_ORDER_MOTOROLA;
1006                 }
1007         else
1008                 {
1009                 return FALSE;
1010                 }
1011
1012         if (exif_byte_get_int16(data + 2, *bo) != 0x002A)
1013                 {
1014                 return FALSE;
1015                 }
1016
1017         *offset = exif_byte_get_int32(data + 4, *bo);
1018
1019         return (*offset < len);
1020 }
1021
1022 gint exif_tiff_parse(ExifData *exif, guchar *tiff, guint size, ExifMarker *list)
1023 {
1024         ExifByteOrder bo;
1025         guint offset;
1026
1027         if (!exif_tiff_directory_offset(tiff, size, &offset, &bo)) return -1;
1028
1029         return exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, list);
1030 }
1031
1032
1033 /*
1034  *-------------------------------------------------------------------
1035  * jpeg marker utils
1036  *-------------------------------------------------------------------
1037  */
1038
1039 #define JPEG_MARKER             0xFF
1040 #define JPEG_MARKER_SOI         0xD8
1041 #define JPEG_MARKER_EOI         0xD9
1042 #define JPEG_MARKER_APP1        0xE1
1043 #define JPEG_MARKER_APP2        0xE2
1044
1045 /* jpeg container format:
1046      all data markers start with 0XFF
1047      2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
1048      4 byte long data segment markers in format: 0xFFTTSSSSNNN...
1049        FF:   1 byte standard marker identifier
1050        TT:   1 byte data type
1051        SSSS: 2 bytes in Motorola byte alignment for length of the data.
1052              This value includes these 2 bytes in the count, making actual
1053              length of NN... == SSSS - 2.
1054        NNN.: the data in this segment
1055  */
1056 static ExifMarker jpeg_color_marker = { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL };
1057
1058 void exif_add_jpeg_color_profile(ExifData *exif, guchar *cp_data, guint cp_length)
1059 {
1060         ExifItem *item = exif_item_new(jpeg_color_marker.format, jpeg_color_marker.tag, 1,
1061                                      &jpeg_color_marker);
1062         g_free(item->data);
1063         item->data = cp_data;
1064         item->elements = cp_length;
1065         item->data_len = cp_length;
1066         exif->items = g_list_prepend(exif->items, item);
1067
1068 }
1069
1070 static gint exif_jpeg_parse(ExifData *exif,
1071                             guchar *data, guint size,
1072                             ExifMarker *list)
1073 {
1074         guint seg_offset = 0;
1075         guint seg_length = 0;
1076         gint res = -1;
1077
1078         if (size < 4 ||
1079             memcmp(data, "\xFF\xD8", 2) != 0)
1080                 {
1081                 return -2;
1082                 }
1083
1084         if (jpeg_segment_find(data, size, JPEG_MARKER_APP1,
1085                                    "Exif\x00\x00", 6,
1086                                    &seg_offset, &seg_length))
1087                 {
1088                 res = exif_tiff_parse(exif, data + seg_offset + 6, seg_length - 6, list);
1089                 }
1090
1091         if (exif_jpeg_parse_color(exif, data, size))
1092                 {
1093                 res = 0;
1094                 }
1095
1096         return res;
1097 }
1098
1099 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1100 {
1101         ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1102         if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1103                 return (guchar *) exif_item_get_data(prof_item, data_len);
1104         return NULL;
1105 }
1106
1107
1108 gchar* exif_get_image_comment(FileData* fd)
1109 {
1110         log_printf("%s", _("Can't get image comment: not compiled with Exiv2.\n"));
1111         return g_strdup("");
1112 }
1113
1114 void exif_set_image_comment(FileData* fd, const gchar* comment)
1115 {
1116         log_printf("%s", _("Can't set image comment: not compiled with Exiv2.\n"));
1117 }
1118
1119
1120 /*
1121  *-------------------------------------------------------------------
1122  * misc
1123  *-------------------------------------------------------------------
1124  */
1125
1126
1127 ExifItem *exif_get_first_item(ExifData *exif)
1128 {
1129         if (exif->items)
1130                 {
1131                 ExifItem *ret = (ExifItem *)exif->items->data;
1132                 exif->current = exif->items->next;
1133                 return ret;
1134                 }
1135         exif->current = NULL;
1136         return NULL;
1137 }
1138
1139 ExifItem *exif_get_next_item(ExifData *exif)
1140 {
1141         if (exif->current)
1142                 {
1143                 ExifItem *ret = (ExifItem *)exif->current->data;
1144                 exif->current = exif->current->next;
1145                 return ret;
1146                 }
1147         return NULL;
1148 }
1149
1150 static gint map_file(const gchar *path, void **mapping, gint *size)
1151 {
1152         gint fd;
1153         struct stat fs;
1154
1155         fd = open(path, O_RDONLY);
1156         if (fd == -1)
1157                 {
1158                 perror(path);
1159                 return -1;
1160                 }
1161
1162         if (fstat(fd, &fs) == -1)
1163                 {
1164                 perror(path);
1165                 close(fd);
1166                 return -1;
1167                 }
1168
1169         *size = fs.st_size;
1170
1171         *mapping = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1172         if (*mapping == MAP_FAILED)
1173                 {
1174                 perror(path);
1175                 close(fd);
1176                 return -1;
1177                 }
1178
1179         close(fd);
1180         return 0;
1181 }
1182
1183 static gint unmap_file(gpointer mapping, gint size)
1184 {
1185         if (munmap(mapping, size) == -1)
1186                 {
1187                 perror("munmap");
1188                 return -1;
1189                 }
1190
1191         return 0;
1192 }
1193
1194 ExifData *exif_get_original(ExifData *processed)
1195 {
1196         return processed;
1197 }
1198
1199 void exif_free(ExifData *exif)
1200 {
1201         GList *work;
1202
1203         if (!exif) return;
1204
1205         work = exif->items;
1206         while (work)
1207                 {
1208                 ExifItem *item = work->data;
1209                 work = work->next;
1210                 exif_item_free(item);
1211                 }
1212
1213         g_list_free(exif->items);
1214         g_free(exif->path);
1215         g_free(exif);
1216 }
1217
1218 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
1219 {
1220         ExifData *exif;
1221         gpointer f;
1222         gint size, res;
1223         gchar *pathl;
1224
1225         if (!path) return NULL;
1226
1227         pathl = path_from_utf8(path);
1228         if (map_file(pathl, &f, &size) == -1)
1229                 {
1230                 g_free(pathl);
1231                 return NULL;
1232                 }
1233         g_free(pathl);
1234
1235         exif = g_new0(ExifData, 1);
1236         exif->path = g_strdup(path);
1237
1238         res = exif_jpeg_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1239         if (res == -2)
1240                 {
1241                 res = exif_tiff_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1242                 }
1243
1244         if (res != 0)
1245                 {
1246                 FormatRawExifType exif_type;
1247                 FormatRawExifParseFunc exif_parse_func;
1248                 guint32 offset = 0;
1249
1250                 exif_type = format_raw_exif_offset(f, size, &offset, &exif_parse_func);
1251                 switch (exif_type)
1252                         {
1253                         case FORMAT_RAW_EXIF_NONE:
1254                         default:
1255                                 break;
1256                         case FORMAT_RAW_EXIF_TIFF:
1257                                 res = exif_tiff_parse(exif, (guchar *)f + offset, size - offset,
1258                                                       ExifKnownMarkersList);
1259                                 break;
1260                         case FORMAT_RAW_EXIF_JPEG:
1261                                 res = exif_jpeg_parse(exif, (guchar *)f + offset, size - offset,
1262                                                       ExifKnownMarkersList);
1263                                 break;
1264                         case FORMAT_RAW_EXIF_IFD_II:
1265                         case FORMAT_RAW_EXIF_IFD_MM:
1266                                 res = exif_parse_IFD_table(exif, (guchar *)f, offset, size - offset,
1267                                                            (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1268                                                                 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1269                                                            0, ExifKnownMarkersList);
1270                                 break;
1271                         case FORMAT_RAW_EXIF_PROPRIETARY:
1272                                 if (exif_parse_func)
1273                                         {
1274                                         res = exif_parse_func((guchar *)f + offset, size - offset, exif);
1275                                         }
1276                                 break;
1277                         }
1278                 }
1279
1280         if (res != 0)
1281                 {
1282                 exif_free(exif);
1283                 exif = NULL;
1284                 }
1285
1286         unmap_file(f, size);
1287
1288         if (exif) exif->items = g_list_reverse(exif->items);
1289
1290         return exif;
1291 }
1292
1293 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1294 {
1295         GList *work;
1296
1297         if (!key) return NULL;
1298
1299         work = exif->items;
1300         while (work)
1301                 {
1302                 ExifItem *item;
1303
1304                 item = work->data;
1305                 work = work->next;
1306                 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1307                 }
1308         return NULL;
1309 }
1310
1311 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1312
1313
1314 static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
1315 {
1316         const ExifMarker *marker;
1317         gpointer data;
1318         GString *string;
1319         gchar *text;
1320         gint ne;
1321         gint i;
1322
1323         if (!item) return NULL;
1324
1325         marker = item->marker;
1326         if (!marker) return NULL;
1327
1328         data = item->data;
1329         ne = item->elements;
1330         if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1331         string = g_string_new("");
1332         switch (item->format)
1333                 {
1334                 case EXIF_FORMAT_UNKNOWN:
1335                         break;
1336                 case EXIF_FORMAT_BYTE_UNSIGNED:
1337                 case EXIF_FORMAT_BYTE:
1338                 case EXIF_FORMAT_UNDEFINED:
1339                         if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1340                                 {
1341                                 gchar *result;
1342                                 guchar val;
1343
1344                                 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1345                                     item->format == EXIF_FORMAT_UNDEFINED)
1346                                         {
1347                                         val = ((guchar *)data)[0];
1348                                         }
1349                                 else
1350                                         {
1351                                         val = (guchar)(((gchar *)data)[0]);
1352                                         }
1353
1354                                 result = exif_text_list_find_value(marker->list, (guint)val);
1355                                 string = g_string_append(string, result);
1356                                 g_free(result);
1357                                 }
1358                         else
1359                                 {
1360                                 string = string_append_raw_bytes(string, data, ne);
1361                                 }
1362                         break;
1363                 case EXIF_FORMAT_STRING:
1364                         if (item->data) string = g_string_append(string, (gchar *)(item->data));
1365                         break;
1366                 case EXIF_FORMAT_SHORT_UNSIGNED:
1367                         if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1368                                 {
1369                                 gchar *result;
1370
1371                                 result = exif_text_list_find_value(marker->list, ((guint16 *)data)[0]);
1372                                 string = g_string_append(string, result);
1373                                 g_free(result);
1374                                 }
1375                         else for (i = 0; i < ne; i++)
1376                                 {
1377                                 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1378                                                         ((guint16 *)data)[i]);
1379                                 }
1380                         break;
1381                 case EXIF_FORMAT_LONG_UNSIGNED:
1382                         for (i = 0; i < ne; i++)
1383                                 {
1384                                 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1385                                                         (gulong)((guint32 *)data)[i]);
1386                                 }
1387                         break;
1388                 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1389                         for (i = 0; i < ne; i++)
1390                                 {
1391                                 ExifRational *r;
1392
1393                                 r = &((ExifRational *)data)[i];
1394                                 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1395                                                         (gulong)r->num, (gulong)r->den);
1396                                 }
1397                         break;
1398                 case EXIF_FORMAT_SHORT:
1399                         for (i = 0; i < ne; i++)
1400                                 {
1401                                 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1402                                                         ((gint16 *)data)[i]);
1403                                 }
1404                         break;
1405                 case EXIF_FORMAT_LONG:
1406                         for (i = 0; i < ne; i++)
1407                                 {
1408                                 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1409                                                         (glong)((gint32 *)data)[i]);
1410                                 }
1411                         break;
1412                 case EXIF_FORMAT_RATIONAL:
1413                         for (i = 0; i < ne; i++)
1414                                 {
1415                                 ExifRational *r;
1416
1417                                 r = &((ExifRational *)data)[i];
1418                                 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1419                                                         (glong)r->num, (glong)r->den);
1420                                 }
1421                         break;
1422                 case EXIF_FORMAT_FLOAT:
1423                         for (i = 0; i < ne; i++)
1424                                 {
1425                                 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1426                                                         ((float *)data)[i]);
1427                                 }
1428                         break;
1429                 case EXIF_FORMAT_DOUBLE:
1430                         for (i = 0; i < ne; i++)
1431                                 {
1432                                 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1433                                                         ((gdouble *)data)[i]);
1434                                 }
1435                         break;
1436                 }
1437
1438         if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1439             item->format != EXIF_FORMAT_STRING)
1440                 {
1441                 g_string_append(string, " ...");
1442                 }
1443
1444         text = string->str;
1445         g_string_free(string, FALSE);
1446
1447         return text;
1448 }
1449
1450 gchar *exif_item_get_string(ExifItem *item, gint idx)
1451 {
1452         return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
1453 }
1454
1455 gchar *exif_item_get_data_as_text(ExifItem *item)
1456 {
1457         return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
1458 }
1459
1460 gint exif_item_get_integer(ExifItem *item, gint *value)
1461 {
1462         if (!item) return FALSE;
1463         if (!item->elements) return FALSE;
1464
1465         switch (item->format)
1466                 {
1467                 case EXIF_FORMAT_SHORT:
1468                         *value = (gint)(((gint16 *)(item->data))[0]);
1469                         return TRUE;
1470                         break;
1471                 case EXIF_FORMAT_SHORT_UNSIGNED:
1472                         *value = (gint)(((guint16 *)(item->data))[0]);
1473                         return TRUE;
1474                         break;
1475                 case EXIF_FORMAT_LONG:
1476                         *value = (gint)(((gint32 *)(item->data))[0]);
1477                         return TRUE;
1478                         break;
1479                 case EXIF_FORMAT_LONG_UNSIGNED:
1480                         /* FIXME: overflow possible */
1481                         *value = (gint)(((guint32 *)(item->data))[0]);
1482                         return TRUE;
1483                 default:
1484                         /* all other type return FALSE */
1485                         break;
1486                 }
1487         return FALSE;
1488 }
1489
1490
1491 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1492 {
1493         if (!item) return NULL;
1494         if (n >= item->elements) return NULL;
1495
1496         if (item->format == EXIF_FORMAT_RATIONAL ||
1497             item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1498                 {
1499                 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1500                 return &((ExifRational *)(item->data))[n];
1501                 }
1502
1503         return NULL;
1504 }
1505
1506 gchar *exif_get_tag_description_by_key(const gchar *key)
1507 {
1508         gint i;
1509
1510         if (!key) return NULL;
1511
1512         i = 0;
1513         while (ExifKnownMarkersList[i].tag > 0)
1514                 {
1515                 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1516                 i++;
1517                 }
1518
1519         i = 0;
1520         while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1521         {
1522            if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
1523            i++;
1524         }
1525
1526         return NULL;
1527 }
1528
1529 static void exif_write_item(FILE *f, ExifItem *item)
1530 {
1531         gchar *text;
1532
1533         text = exif_item_get_data_as_text(item);
1534         if (text)
1535                 {
1536                 gchar *tag = exif_item_get_tag_name(item);
1537                 g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1538                         tag, text);
1539                 g_free(tag);
1540                 }
1541         g_free(text);
1542 }
1543
1544 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1545 {
1546         if (!f || !exif) return;
1547
1548         g_fprintf(f, " tag   format                             key value\n");
1549         g_fprintf(f, "----------------------------------------------------\n");
1550
1551         if (human_readable_list)
1552                 {
1553                 gint i;
1554
1555                 i = 0;
1556                 while (ExifFormattedList[i].key)
1557                         {
1558                         gchar *text;
1559
1560                         text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1561                         if (text)
1562                                 {
1563                                 g_fprintf(f, "     %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1564                                 }
1565                         i++;
1566                         }
1567                 }
1568         else
1569                 {
1570                 GList *work;
1571
1572                 work = exif->items;
1573                 while (work)
1574                         {
1575                         ExifItem *item;
1576
1577                         item = work->data;
1578                         work = work->next;
1579
1580                         exif_write_item(f, item);
1581                         }
1582                 }
1583         g_fprintf(f, "----------------------------------------------------\n");
1584 }
1585
1586 gboolean exif_write(ExifData *exif)
1587 {
1588         log_printf("Not compiled with EXIF write support\n");
1589         return FALSE;
1590 }
1591
1592 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
1593 {
1594         log_printf("Not compiled with EXIF write support\n");
1595         return FALSE;
1596 }
1597
1598
1599 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
1600 {
1601         return 0;
1602 }
1603
1604 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1605 {
1606         gchar *str;
1607         ExifItem *item;
1608
1609         if (!key) return NULL;
1610
1611         /* convert xmp key to exif key */
1612         if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
1613
1614         if (format == METADATA_FORMATTED)
1615                 {
1616                 gchar *text;
1617                 gint key_valid;
1618                 text = exif_get_formatted_by_key(exif, key, &key_valid);
1619                 if (key_valid) return g_list_append(NULL, text);
1620                 }
1621
1622         item = exif_get_item(exif, key);
1623         if (!item) return NULL;
1624
1625         str = exif_item_get_data_as_text_full(item, format);
1626
1627         if (!str) return NULL;
1628
1629         return g_list_append(NULL, str);
1630 }
1631
1632 typedef struct _UnmapData UnmapData;
1633 struct _UnmapData
1634 {
1635         guchar *ptr;
1636         guchar *map_data;
1637         size_t map_len;
1638 };
1639
1640 static GList *exif_unmap_list = 0;
1641
1642 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1643 {
1644         guint offset;
1645         const gchar* path;
1646         struct stat st;
1647         guchar *map_data;
1648         size_t map_len;
1649         int fd;
1650
1651         if (!exif) return NULL;
1652         path = exif->path;
1653
1654         fd = open(path, O_RDONLY);
1655
1656
1657         if (fd == -1)
1658                 {
1659                 return 0;
1660                 }
1661
1662         if (fstat(fd, &st) == -1)
1663                 {
1664                 close(fd);
1665                 return 0;
1666                 }
1667         map_len = st.st_size;
1668         map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1669         close(fd);
1670
1671         if (map_data == MAP_FAILED)
1672                 {
1673                 return 0;
1674                 }
1675
1676         if (format_raw_img_exif_offsets(map_data, map_len, &offset, NULL) && offset)
1677                 {
1678                 UnmapData *ud;
1679
1680                 DEBUG_1("%s: offset %u", path, offset);
1681
1682                 *data_len = map_len - offset;
1683                 ud = g_new(UnmapData, 1);
1684                 ud->ptr = map_data + offset;
1685                 ud->map_data = map_data;
1686                 ud->map_len = map_len;
1687
1688                 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1689                 return ud->ptr;
1690                 }
1691
1692         munmap(map_data, map_len);
1693         return NULL;
1694
1695 }
1696
1697 void exif_free_preview(guchar *buf)
1698 {
1699         GList *work = exif_unmap_list;
1700
1701         while (work)
1702                 {
1703                 UnmapData *ud = (UnmapData *)work->data;
1704                 if (ud->ptr == buf)
1705                         {
1706                         exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1707                         g_free(ud);
1708                         return;
1709                         }
1710                 work = work->next;
1711                 }
1712         g_assert_not_reached();
1713 }
1714
1715 void exif_init(void)
1716 {
1717 }
1718
1719 #endif
1720 /* not HAVE_EXIV2 */
1721 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */