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