Remove commented out code.
[geeqie.git] / src / exif.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  *  Copyright (C) 2008 - 2012 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 #include <glib/gprintf.h>
71
72 #include "intl.h"
73
74 #include "main.h"
75 #include "exif-int.h"
76 #include "jpeg_parser.h"
77
78 #include "format_raw.h"
79 #include "ui_fileops.h"
80
81
82 /*
83  *-----------------------------------------------------------------------------
84  * Tag formats
85  *-----------------------------------------------------------------------------
86  */
87
88 ExifFormatAttrib ExifFormatList[] = {
89         { EXIF_FORMAT_UNKNOWN,          1, "unknown",   "unknown" },
90         { EXIF_FORMAT_BYTE_UNSIGNED,    1, "ubyte",     "unsigned byte" },
91         { EXIF_FORMAT_STRING,           1, "string",    "string" },
92         { EXIF_FORMAT_SHORT_UNSIGNED,   2, "ushort",    "unsigned short" },
93         { EXIF_FORMAT_LONG_UNSIGNED,    4, "ulong",     "unsigned long" },
94         { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
95         { EXIF_FORMAT_BYTE,             1, "byte",      "byte" },
96         { EXIF_FORMAT_UNDEFINED,        1, "undefined", "undefined" },
97         { EXIF_FORMAT_SHORT,            2, "sshort",    "signed short" },
98         { EXIF_FORMAT_LONG,             4, "slong",     "signed long" },
99         { EXIF_FORMAT_RATIONAL,         8, "srational", "signed rational" },
100         { EXIF_FORMAT_FLOAT,            4, "float",     "float" },
101         { EXIF_FORMAT_DOUBLE,           8, "double",    "double" },
102         { -1, 0, NULL, NULL }
103 };
104
105 /* tags that are special, or need special treatment */
106 #define TAG_EXIFOFFSET          0x8769
107 #define TAG_EXIFMAKERNOTE       0x927c
108 #define TAG_GPSOFFSET           0x8825
109
110
111 /*
112  *-----------------------------------------------------------------------------
113  * Data
114  *-----------------------------------------------------------------------------
115  */
116 static ExifTextList ExifCompressionList[] = {
117         { 1, "Uncompressed" },
118         { 2, "CCITT 1D" },
119         { 3, "T4/Group 3 Fax" },
120         { 4, "T6/Group 4 Fax" },
121         { 5, "LZW" },
122         { 6, "JPEG (old style)" },
123         { 7, "JPEG" },
124         { 8, "Adobe Deflate" },
125         { 9, "JBIG B&W" },
126         { 10, "JBIG Color" },
127         { 32766, "Next" },
128         { 32771, "CCIRLEW" },
129         { 32773, "PackBits" },
130         { 32809, "ThunderScan" },
131         { 32895, "IT8CTPAD" },
132         { 32896, "IT8LW" },
133         { 32897, "IT8MP" },
134         { 32898, "IT8BL" },
135         { 32908, "PixasFilm" },
136         { 32909, "PixasLog" },
137         { 32946, "Deflate" },
138         { 32947, "DCS" },
139         { 34661, "JBIG" },
140         { 34676, "SGILog" },
141         { 34677, "SGILog24" },
142         { 34712, "JPEF 2000" },
143         { 34713, "Nikon NEF Compressed" },
144         EXIF_TEXT_LIST_END
145 };
146
147 static ExifTextList ExifOrientationList[] = {
148         { EXIF_ORIENTATION_UNKNOWN,     N_("unknown") },
149         { EXIF_ORIENTATION_TOP_LEFT,    N_("top left") },
150         { EXIF_ORIENTATION_TOP_RIGHT,   N_("top right") },
151         { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
152         { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
153         { EXIF_ORIENTATION_LEFT_TOP,    N_("left top") },
154         { EXIF_ORIENTATION_RIGHT_TOP,   N_("right top") },
155         { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
156         { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
157         EXIF_TEXT_LIST_END
158 };
159
160 static ExifTextList ExifUnitList[] = {
161         { EXIF_UNIT_UNKNOWN,    N_("unknown") },
162         { EXIF_UNIT_NOUNIT,     "" },
163         { EXIF_UNIT_INCH,       N_("inch") },
164         { EXIF_UNIT_CENTIMETER, N_("centimeter") },
165         EXIF_TEXT_LIST_END
166 };
167
168 static ExifTextList ExifYCbCrPosList[] = {
169         { 1,    "center" },
170         { 2,    "datum" },
171         EXIF_TEXT_LIST_END
172 };
173
174 static ExifTextList ExifMeteringModeList[] = {
175         { 0,    N_("unknown") },
176         { 1,    N_("average") },
177         { 2,    N_("center weighted") },
178         { 3,    N_("spot") },
179         { 4,    N_("multi-spot") },
180         { 5,    N_("multi-segment") },
181         { 6,    N_("partial") },
182         { 255,  N_("other") },
183         EXIF_TEXT_LIST_END
184 };
185
186 static ExifTextList ExifExposureProgramList[] = {
187         { 0,    N_("not defined") },
188         { 1,    N_("manual") },
189         { 2,    N_("normal") },
190         { 3,    N_("aperture") },
191         { 4,    N_("shutter") },
192         { 5,    N_("creative") },
193         { 6,    N_("action") },
194         { 7,    N_("portrait") },
195         { 8,    N_("landscape") },
196         EXIF_TEXT_LIST_END
197 };
198
199 static ExifTextList ExifLightSourceList[] = {
200         { 0,    N_("unknown") },
201         { 1,    N_("daylight") },
202         { 2,    N_("fluorescent") },
203         { 3,    N_("tungsten (incandescent)") },
204         { 4,    N_("flash") },
205         { 9,    N_("fine weather") },
206         { 10,   N_("cloudy weather") },
207         { 11,   N_("shade") },
208         { 12,   N_("daylight fluorescent") },
209         { 13,   N_("day white fluorescent") },
210         { 14,   N_("cool white fluorescent") },
211         { 15,   N_("white fluorescent") },
212         { 17,   N_("standard light A") },
213         { 18,   N_("standard light B") },
214         { 19,   N_("standard light C") },
215         { 20,   N_("D55") },
216         { 21,   N_("D65") },
217         { 22,   N_("D75") },
218         { 23,   N_("D50") },
219         { 24,   N_("ISO studio tungsten") },
220         { 255,  N_("other") },
221         EXIF_TEXT_LIST_END
222 };
223
224 static ExifTextList ExifFlashList[] = {
225         { 0,    N_("no") },
226         { 1,    N_("yes") },
227         { 5,    N_("yes, not detected by strobe") },
228         { 7,    N_("yes, detected by strobe") },
229         EXIF_TEXT_LIST_END
230 };
231
232 static ExifTextList ExifColorSpaceList[] = {
233         { 1,    N_("sRGB") },
234         { 65535,N_("uncalibrated") },
235         EXIF_TEXT_LIST_END
236 };
237
238 static ExifTextList ExifSensorList[] = {
239         { 1,    N_("not defined") },
240         { 2,    N_("1 chip color area") },
241         { 2,    N_("2 chip color area") },
242         { 4,    N_("3 chip color area") },
243         { 5,    N_("color sequential area") },
244         { 7,    N_("trilinear") },
245         { 8,    N_("color sequential linear") },
246         EXIF_TEXT_LIST_END
247 };
248
249 static ExifTextList ExifSourceList[] = {
250         { 3,    N_("digital still camera") },
251         EXIF_TEXT_LIST_END
252 };
253
254 static ExifTextList ExifSceneList[] = {
255         { 1,    N_("direct photo") },
256         EXIF_TEXT_LIST_END
257 };
258
259 static ExifTextList ExifCustRenderList[] = {
260         { 0,    N_("normal") },
261         { 1,    N_("custom") },
262         EXIF_TEXT_LIST_END
263 };
264
265 static ExifTextList ExifExposureModeList[] = {
266         { 0,    N_("auto") },
267         { 1,    N_("manual") },
268         { 2,    N_("auto bracket") },
269         EXIF_TEXT_LIST_END
270 };
271
272 static ExifTextList ExifWhiteBalanceList[] = {
273         { 0,    N_("auto") },
274         { 1,    N_("manual") },
275         EXIF_TEXT_LIST_END
276 };
277
278 static ExifTextList ExifSceneCaptureList[] = {
279         { 0,    N_("standard") },
280         { 1,    N_("landscape") },
281         { 2,    N_("portrait") },
282         { 3,    N_("night scene") },
283         EXIF_TEXT_LIST_END
284 };
285
286 static ExifTextList ExifGainControlList[] = {
287         { 0,    N_("none") },
288         { 1,    N_("low gain up") },
289         { 2,    N_("high gain up") },
290         { 3,    N_("low gain down") },
291         { 4,    N_("high gain down") },
292         EXIF_TEXT_LIST_END
293 };
294
295 static ExifTextList ExifContrastList[] = {
296         { 0,    N_("normal") },
297         { 1,    N_("soft") },
298         { 2,    N_("hard") },
299         EXIF_TEXT_LIST_END
300 };
301
302 static ExifTextList ExifSaturationList[] = {
303         { 0,    N_("normal") },
304         { 1,    N_("low") },
305         { 2,    N_("high") },
306         EXIF_TEXT_LIST_END
307 };
308
309 static ExifTextList ExifSharpnessList[] = {
310         { 0,    N_("normal") },
311         { 1,    N_("soft") },
312         { 2,    N_("hard") },
313         EXIF_TEXT_LIST_END
314 };
315
316 static ExifTextList ExifSubjectRangeList[] = {
317         { 0,    N_("unknown") },
318         { 1,    N_("macro") },
319         { 2,    N_("close") },
320         { 3,    N_("distant") },
321         EXIF_TEXT_LIST_END
322 };
323
324 /*
325 Tag names should match to exiv2 keys, http://www.exiv2.org/metadata.html
326 Tags that don't match are not supported by exiv2 and should not be used anywhere in the code
327 */
328
329 ExifMarker ExifKnownMarkersList[] = {
330 { 0x0100, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ImageWidth",        N_("Image Width"), NULL },
331 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ImageLength",       N_("Image Height"), NULL },
332 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.BitsPerSample",     N_("Bits per Sample/Pixel"), NULL },
333 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.Compression",       N_("Compression"), ExifCompressionList },
334 { 0x010e, EXIF_FORMAT_STRING, -1,               "Exif.Image.ImageDescription",  N_("Image description"), NULL },
335 { 0x010f, EXIF_FORMAT_STRING, -1,               "Exif.Image.Make",              N_("Camera make"), NULL },
336 { 0x0110, EXIF_FORMAT_STRING, -1,               "Exif.Image.Model",             N_("Camera model"), NULL },
337 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.Orientation",       N_("Orientation"), ExifOrientationList },
338 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.XResolution",       N_("X resolution"), NULL },
339 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.YResolution",       N_("Y Resolution"), NULL },
340 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.ResolutionUnit",    N_("Resolution units"), ExifUnitList },
341 { 0x0131, EXIF_FORMAT_STRING, -1,               "Exif.Image.Software",          N_("Firmware"), NULL },
342 { 0x0132, EXIF_FORMAT_STRING, 20,               "Exif.Image.DateTime",          N_("Date"), NULL },
343 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2,     "Exif.Image.WhitePoint",        N_("White point"), NULL },
344 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6,     "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), NULL },
345 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3,     "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), NULL },
346 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Image.YCbCrPositioning",  N_("YCbCr positioning"), ExifYCbCrPosList },
347 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6,     "Exif.Image.ReferenceBlackWhite",N_("Black white reference"), NULL },
348 { 0x8298, EXIF_FORMAT_STRING, -1,               "Exif.Image.Copyright",         N_("Copyright"), NULL },
349 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.ExifTag",           N_("SubIFD Exif offset"), NULL },
350         /* subIFD follows */
351 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureTime",      N_("Exposure time (seconds)"), NULL },
352 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FNumber",           N_("FNumber"), NULL },
353 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ExposureProgram",   N_("Exposure program"), ExifExposureProgramList },
354 { 0x8824, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SpectralSensitivity",N_("Spectral Sensitivity"), NULL },
355 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.ISOSpeedRatings",   N_("ISO sensitivity"), NULL },
356 { 0x8828, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.OECF",              N_("Optoelectric conversion factor"), NULL },
357 { 0x9000, EXIF_FORMAT_UNDEFINED, 4,             "Exif.Photo.ExifVersion",       N_("Exif version"), NULL },
358 { 0x9003, EXIF_FORMAT_STRING, 20,               "Exif.Photo.DateTimeOriginal",  N_("Date original"), NULL },
359 { 0x9004, EXIF_FORMAT_STRING, 20,               "Exif.Photo.DateTimeDigitized", N_("Date digitized"), NULL },
360 { 0x9101, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), NULL },
361 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1,      "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), NULL },
362 { 0x9201, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), NULL },
363 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ApertureValue",     N_("Aperture"), NULL },
364 { 0x9203, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.BrightnessValue",   N_("Brightness"), NULL },
365 { 0x9204, EXIF_FORMAT_RATIONAL, 1,              "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), NULL },
366 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.MaxApertureValue",  N_("Maximum aperture"), NULL },
367 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.SubjectDistance",   N_("Subject distance"), NULL },
368 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.MeteringMode",      N_("Metering mode"), ExifMeteringModeList },
369 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.LightSource",       N_("Light source"), ExifLightSourceList },
370 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Flash",             N_("Flash"), ExifFlashList },
371 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalLength",       N_("Focal length"), NULL },
372 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.SubjectArea",       N_("Subject area"), NULL },
373 { 0x927c, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.MakerNote",         N_("MakerNote"), NULL },
374 { 0x9286, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.UserComment",       N_("UserComment"), NULL },
375 { 0x9290, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTime",        N_("Subsecond time"), NULL },
376 { 0x9291, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), NULL },
377 { 0x9292, EXIF_FORMAT_STRING, -1,               "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), NULL },
378 { 0xa000, EXIF_FORMAT_UNDEFINED, 4,             "Exif.Photo.FlashpixVersion",   N_("FlashPix version"), NULL },
379 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ColorSpace",        N_("Colorspace"), ExifColorSpaceList },
380         /* ExifImageWidth, ExifImageHeight can also be unsigned short */
381 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Photo.PixelXDimension",   N_("Width"), NULL },
382 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Photo.PixelYDimension",   N_("Height"), NULL },
383 { 0xa004, EXIF_FORMAT_STRING, -1,               "Exif.Photo.RelatedSoundFile",  N_("Audio data"), NULL },
384 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1,         "ExifInteroperabilityOffset",   N_("ExifR98 extension"), NULL },
385 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FlashEnergy",       N_("Flash strength"), NULL },
386 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), NULL },
387 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), NULL },
388 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), NULL },
389 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.FocalPlaneResolutionUnit", N_("Pixel density units"), ExifUnitList },
390 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2,        "Exif.Photo.SubjectLocation",   N_("Subject location"), NULL },
391 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureIndex",     N_("ISO sensitivity"), NULL },
392 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "Exif.Photo.SensingMethod",     N_("Sensor type"), ExifSensorList },
393 { 0xa300, EXIF_FORMAT_UNDEFINED, 1,             "Exif.Photo.FileSource",        N_("Source type"), ExifSourceList },
394 { 0xa301, EXIF_FORMAT_UNDEFINED, 1,             "Exif.Photo.SceneType",         N_("Scene type"), ExifSceneList },
395 { 0xa302, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Image.CFAPattern",        N_("Color filter array pattern"), NULL },
396         /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
397 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.CustomRendered",    N_("Render process"), ExifCustRenderList },
398 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.ExposureMode",      N_("Exposure mode"), ExifExposureModeList },
399 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.WhiteBalance",      N_("White balance"), ExifWhiteBalanceList },
400 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.DigitalZoomRatio",  N_("Digital zoom ratio"), NULL },
401 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), NULL },
402 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SceneCaptureType",  N_("Scene capture type"), ExifSceneCaptureList },
403 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.GainControl",       N_("Gain control"), ExifGainControlList },
404 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Contrast",          N_("Contrast"), ExifContrastList },
405 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Saturation",        N_("Saturation"), ExifSaturationList },
406 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.Sharpness",         N_("Sharpness"), ExifSharpnessList },
407 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.DeviceSettingDescription",N_("Device setting"), NULL },
408 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Exif.Photo.SubjectDistanceRange",N_("Subject range"), ExifSubjectRangeList },
409 { 0xa420, EXIF_FORMAT_STRING, -1,               "Exif.Photo.ImageUniqueID",     N_("Image serial number"), NULL },
410         /* place known, but undocumented or lesser used tags here */
411 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1,         "Exif.Image.NewSubfileType",    NULL, NULL },
412 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "SubfileType",                  NULL, NULL },
413 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3,        "Exif.Image.TransferFunction",  NULL, NULL },
414 { 0x013b, EXIF_FORMAT_STRING, -1,               "Exif.Image.Artist",            "Artist", NULL },
415 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Predictor",            NULL, NULL },
416 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "TileWidth",            NULL, NULL },
417 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "TileLength",           NULL, NULL },
418 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1,        "TileOffsets",          NULL, NULL },
419 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "TileByteCounts",       NULL, NULL },
420 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1,        "Exif.Image.SubIFDs",           NULL, NULL },
421 { 0x015b, EXIF_FORMAT_UNDEFINED, -1,            "JPEGTables",           NULL, NULL },
422 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2,        "Exif.Image.CFARepeatPatternDim",       NULL, NULL },
423 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1,        "Exif.Image.CFAPattern",                NULL, NULL },
424 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Image.BatteryLevel",              NULL, NULL },
425 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1,        "IPTC/NAA",             NULL, NULL },
426 { 0x8773, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Image.InterColorProfile",         NULL, NULL },
427 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1,         "GPSInfo",              "SubIFD GPS offset", NULL },
428 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "Interlace",            NULL, NULL },
429 { 0x882a, EXIF_FORMAT_SHORT, 1,                 "TimeZoneOffset",       NULL, NULL },
430 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1,        "SelfTimerMode",        NULL, NULL },
431 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.FlashEnergy",               NULL, NULL },
432 { 0x920c, EXIF_FORMAT_UNDEFINED, -1,            "Exif.Photo.SpatialFrequencyResponse", NULL, NULL },
433 { 0x920d, EXIF_FORMAT_UNDEFINED, -1,            "Noise",                NULL, NULL },
434 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1,         "ImageNumber",          NULL, NULL },
435 { 0x9212, EXIF_FORMAT_STRING, 1,                "SecurityClassification", NULL, NULL },
436 { 0x9213, EXIF_FORMAT_STRING, -1,               "ImageHistory",         NULL, NULL },
437 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1,     "Exif.Photo.ExposureIndex",     NULL, NULL },
438 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4,         "TIFF/EPStandardID",    NULL, NULL },
439
440 EXIF_MARKER_LIST_END
441 };
442
443 ExifMarker ExifKnownGPSInfoMarkersList[] = {
444         /* The following do not work at the moment as the tag value 0x0000 has a
445          * special meaning. */
446         /* { 0x0000, EXIF_FORMAT_BYTE, -1, "Exif.GPSInfo.GPSVersionID", NULL, NULL }, */
447         { 0x0001, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLatitudeRef", NULL, NULL },
448         { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", NULL, NULL },
449         { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", NULL, NULL },
450         { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", NULL, NULL },
451         { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", NULL, NULL },
452         { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", NULL, NULL },
453         { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", NULL, NULL },
454         { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", NULL, NULL },
455         { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", NULL, NULL },
456         { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", NULL, NULL },
457         { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", NULL, NULL },
458         { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", NULL, NULL },
459         { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", NULL, NULL },
460         { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", NULL, NULL },
461         { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", NULL, NULL },
462         { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", NULL, NULL },
463         { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", NULL, NULL },
464         { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", NULL, NULL },
465         { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", NULL, NULL },
466         { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", NULL, NULL },
467         { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", NULL, NULL },
468         { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", NULL, NULL },
469         { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", NULL, NULL },
470         { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", NULL, NULL },
471         { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", NULL, NULL },
472         { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", NULL, NULL },
473         { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", NULL, NULL },
474         { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", NULL, NULL },
475         { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", NULL, NULL },
476         { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", NULL, NULL },
477
478         EXIF_MARKER_LIST_END
479 };
480
481 ExifMarker ExifUnknownMarkersList[] = {
482 { 0x0000, EXIF_FORMAT_UNKNOWN, 0,               "unknown",      NULL, NULL },
483 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1,        "unknown",      NULL, NULL },
484 { 0x0000, EXIF_FORMAT_STRING, -1,               "unknown",      NULL, NULL },
485 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1,       "unknown",      NULL, NULL },
486 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1,        "unknown",      NULL, NULL },
487 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1,    "unknown",      NULL, NULL },
488 { 0x0000, EXIF_FORMAT_BYTE, -1,                 "unknown",      NULL, NULL },
489 { 0x0000, EXIF_FORMAT_UNDEFINED, -1,            "unknown",      NULL, NULL },
490 { 0x0000, EXIF_FORMAT_SHORT, -1,                "unknown",      NULL, NULL },
491 { 0x0000, EXIF_FORMAT_LONG, -1,                 "unknown",      NULL, NULL },
492 { 0x0000, EXIF_FORMAT_RATIONAL, -1,             "unknown",      NULL, NULL },
493 { 0x0000, EXIF_FORMAT_FLOAT, -1,                "unknown",      NULL, NULL },
494 { 0x0000, EXIF_FORMAT_DOUBLE, -1,               "unknown",      NULL, NULL },
495 };
496
497 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
498
499 /*
500  *-----------------------------------------------------------------------------
501  * ExifItem
502  *-----------------------------------------------------------------------------
503  */
504
505 ExifItem *exif_item_new(ExifFormatType format, guint tag,
506                         guint elements, const ExifMarker *marker)
507 {
508         ExifItem *item;
509
510         item = g_new0(ExifItem, 1);
511         item->format = format;
512         item->tag = tag;
513         item->marker = marker;
514         item->elements = elements;
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, gboolean 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 || 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 (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->path = g_strdup(path);
1231
1232         res = exif_jpeg_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1233         if (res == -2)
1234                 {
1235                 res = exif_tiff_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1236                 }
1237
1238         if (res != 0)
1239                 {
1240                 FormatRawExifType exif_type;
1241                 FormatRawExifParseFunc exif_parse_func;
1242                 guint32 offset = 0;
1243
1244                 exif_type = format_raw_exif_offset(f, size, &offset, &exif_parse_func);
1245                 switch (exif_type)
1246                         {
1247                         case FORMAT_RAW_EXIF_NONE:
1248                         default:
1249                                 break;
1250                         case FORMAT_RAW_EXIF_TIFF:
1251                                 res = exif_tiff_parse(exif, (guchar *)f + offset, size - offset,
1252                                                       ExifKnownMarkersList);
1253                                 break;
1254                         case FORMAT_RAW_EXIF_JPEG:
1255                                 res = exif_jpeg_parse(exif, (guchar *)f + offset, size - offset,
1256                                                       ExifKnownMarkersList);
1257                                 break;
1258                         case FORMAT_RAW_EXIF_IFD_II:
1259                         case FORMAT_RAW_EXIF_IFD_MM:
1260                                 res = exif_parse_IFD_table(exif, (guchar *)f, offset, size - offset,
1261                                                            (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1262                                                                 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1263                                                            0, ExifKnownMarkersList);
1264                                 break;
1265                         case FORMAT_RAW_EXIF_PROPRIETARY:
1266                                 if (exif_parse_func)
1267                                         {
1268                                         res = exif_parse_func((guchar *)f + offset, size - offset, exif);
1269                                         }
1270                                 break;
1271                         }
1272                 }
1273
1274         if (res != 0)
1275                 {
1276                 exif_free(exif);
1277                 exif = NULL;
1278                 }
1279
1280         unmap_file(f, size);
1281
1282         if (exif) exif->items = g_list_reverse(exif->items);
1283
1284         return exif;
1285 }
1286
1287 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1288 {
1289         GList *work;
1290
1291         if (!key) return NULL;
1292
1293         work = exif->items;
1294         while (work)
1295                 {
1296                 ExifItem *item;
1297
1298                 item = work->data;
1299                 work = work->next;
1300                 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1301                 }
1302         return NULL;
1303 }
1304
1305 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1306
1307
1308 static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
1309 {
1310         const ExifMarker *marker;
1311         gpointer data;
1312         GString *string;
1313         gchar *text;
1314         gint ne;
1315         gint i;
1316
1317         if (!item) return NULL;
1318
1319         marker = item->marker;
1320         if (!marker) return NULL;
1321
1322         data = item->data;
1323         ne = item->elements;
1324         if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1325         string = g_string_new("");
1326         switch (item->format)
1327                 {
1328                 case EXIF_FORMAT_UNKNOWN:
1329                         break;
1330                 case EXIF_FORMAT_BYTE_UNSIGNED:
1331                 case EXIF_FORMAT_BYTE:
1332                 case EXIF_FORMAT_UNDEFINED:
1333                         if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1334                                 {
1335                                 gchar *result;
1336                                 guchar val;
1337
1338                                 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1339                                     item->format == EXIF_FORMAT_UNDEFINED)
1340                                         {
1341                                         val = ((guchar *)data)[0];
1342                                         }
1343                                 else
1344                                         {
1345                                         val = (guchar)(((gchar *)data)[0]);
1346                                         }
1347
1348                                 result = exif_text_list_find_value(marker->list, (guint)val);
1349                                 string = g_string_append(string, result);
1350                                 g_free(result);
1351                                 }
1352                         else
1353                                 {
1354                                 string = string_append_raw_bytes(string, data, ne);
1355                                 }
1356                         break;
1357                 case EXIF_FORMAT_STRING:
1358                         if (item->data) string = g_string_append(string, (gchar *)(item->data));
1359                         break;
1360                 case EXIF_FORMAT_SHORT_UNSIGNED:
1361                         if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1362                                 {
1363                                 gchar *result;
1364
1365                                 result = exif_text_list_find_value(marker->list, ((guint16 *)data)[0]);
1366                                 string = g_string_append(string, result);
1367                                 g_free(result);
1368                                 }
1369                         else for (i = 0; i < ne; i++)
1370                                 {
1371                                 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1372                                                         ((guint16 *)data)[i]);
1373                                 }
1374                         break;
1375                 case EXIF_FORMAT_LONG_UNSIGNED:
1376                         for (i = 0; i < ne; i++)
1377                                 {
1378                                 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1379                                                         (gulong)((guint32 *)data)[i]);
1380                                 }
1381                         break;
1382                 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1383                         for (i = 0; i < ne; i++)
1384                                 {
1385                                 ExifRational *r;
1386
1387                                 r = &((ExifRational *)data)[i];
1388                                 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1389                                                         (gulong)r->num, (gulong)r->den);
1390                                 }
1391                         break;
1392                 case EXIF_FORMAT_SHORT:
1393                         for (i = 0; i < ne; i++)
1394                                 {
1395                                 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1396                                                         ((gint16 *)data)[i]);
1397                                 }
1398                         break;
1399                 case EXIF_FORMAT_LONG:
1400                         for (i = 0; i < ne; i++)
1401                                 {
1402                                 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1403                                                         (glong)((gint32 *)data)[i]);
1404                                 }
1405                         break;
1406                 case EXIF_FORMAT_RATIONAL:
1407                         for (i = 0; i < ne; i++)
1408                                 {
1409                                 ExifRational *r;
1410
1411                                 r = &((ExifRational *)data)[i];
1412                                 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1413                                                         (glong)r->num, (glong)r->den);
1414                                 }
1415                         break;
1416                 case EXIF_FORMAT_FLOAT:
1417                         for (i = 0; i < ne; i++)
1418                                 {
1419                                 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1420                                                         ((float *)data)[i]);
1421                                 }
1422                         break;
1423                 case EXIF_FORMAT_DOUBLE:
1424                         for (i = 0; i < ne; i++)
1425                                 {
1426                                 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1427                                                         ((gdouble *)data)[i]);
1428                                 }
1429                         break;
1430                 }
1431
1432         if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1433             item->format != EXIF_FORMAT_STRING)
1434                 {
1435                 g_string_append(string, " ...");
1436                 }
1437
1438         text = string->str;
1439         g_string_free(string, FALSE);
1440
1441         return text;
1442 }
1443
1444 gchar *exif_item_get_string(ExifItem *item, gint idx)
1445 {
1446         return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
1447 }
1448
1449 gchar *exif_item_get_data_as_text(ExifItem *item)
1450 {
1451         return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
1452 }
1453
1454 gint exif_item_get_integer(ExifItem *item, gint *value)
1455 {
1456         if (!item) return FALSE;
1457         if (!item->elements) return FALSE;
1458
1459         switch (item->format)
1460                 {
1461                 case EXIF_FORMAT_SHORT:
1462                         *value = (gint)(((gint16 *)(item->data))[0]);
1463                         return TRUE;
1464                         break;
1465                 case EXIF_FORMAT_SHORT_UNSIGNED:
1466                         *value = (gint)(((guint16 *)(item->data))[0]);
1467                         return TRUE;
1468                         break;
1469                 case EXIF_FORMAT_LONG:
1470                         *value = (gint)(((gint32 *)(item->data))[0]);
1471                         return TRUE;
1472                         break;
1473                 case EXIF_FORMAT_LONG_UNSIGNED:
1474                         /* FIXME: overflow possible */
1475                         *value = (gint)(((guint32 *)(item->data))[0]);
1476                         return TRUE;
1477                 default:
1478                         /* all other type return FALSE */
1479                         break;
1480                 }
1481         return FALSE;
1482 }
1483
1484
1485 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1486 {
1487         if (!item) return NULL;
1488         if (n >= item->elements) return NULL;
1489
1490         if (item->format == EXIF_FORMAT_RATIONAL ||
1491             item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1492                 {
1493                 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1494                 return &((ExifRational *)(item->data))[n];
1495                 }
1496
1497         return NULL;
1498 }
1499
1500 gchar *exif_get_tag_description_by_key(const gchar *key)
1501 {
1502         gint i;
1503
1504         if (!key) return NULL;
1505
1506         i = 0;
1507         while (ExifKnownMarkersList[i].tag > 0)
1508                 {
1509                 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1510                 i++;
1511                 }
1512
1513         i = 0;
1514         while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1515         {
1516            if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
1517            i++;
1518         }
1519
1520         return NULL;
1521 }
1522
1523 static void exif_write_item(FILE *f, ExifItem *item)
1524 {
1525         gchar *text;
1526
1527         text = exif_item_get_data_as_text(item);
1528         if (text)
1529                 {
1530                 gchar *tag = exif_item_get_tag_name(item);
1531                 g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1532                         tag, text);
1533                 g_free(tag);
1534                 }
1535         g_free(text);
1536 }
1537
1538 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1539 {
1540         if (!f || !exif) return;
1541
1542         g_fprintf(f, " tag   format                             key value\n");
1543         g_fprintf(f, "----------------------------------------------------\n");
1544
1545         if (human_readable_list)
1546                 {
1547                 gint i;
1548
1549                 i = 0;
1550                 while (ExifFormattedList[i].key)
1551                         {
1552                         gchar *text;
1553
1554                         text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1555                         if (text)
1556                                 {
1557                                 g_fprintf(f, "     %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1558                                 }
1559                         i++;
1560                         }
1561                 }
1562         else
1563                 {
1564                 GList *work;
1565
1566                 work = exif->items;
1567                 while (work)
1568                         {
1569                         ExifItem *item;
1570
1571                         item = work->data;
1572                         work = work->next;
1573
1574                         exif_write_item(f, item);
1575                         }
1576                 }
1577         g_fprintf(f, "----------------------------------------------------\n");
1578 }
1579
1580 gboolean exif_write(ExifData *exif)
1581 {
1582         log_printf("Not compiled with EXIF write support\n");
1583         return FALSE;
1584 }
1585
1586 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
1587 {
1588         log_printf("Not compiled with EXIF write support\n");
1589         return FALSE;
1590 }
1591
1592
1593 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
1594 {
1595         return 0;
1596 }
1597
1598 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1599 {
1600         gchar *str;
1601         ExifItem *item;
1602         
1603         if (!key) return NULL;
1604         
1605         /* convert xmp key to exif key */
1606         if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
1607         
1608         if (format == METADATA_FORMATTED)
1609                 {
1610                 gchar *text;
1611                 gint key_valid;
1612                 text = exif_get_formatted_by_key(exif, key, &key_valid);
1613                 if (key_valid) return g_list_append(NULL, text);
1614                 }
1615
1616         item = exif_get_item(exif, key);
1617         if (!item) return NULL;
1618         
1619         str = exif_item_get_data_as_text_full(item, format);
1620         
1621         if (!str) return NULL;
1622         
1623         return g_list_append(NULL, str);
1624 }
1625
1626 typedef struct _UnmapData UnmapData;
1627 struct _UnmapData
1628 {
1629         guchar *ptr;
1630         guchar *map_data;
1631         size_t map_len;
1632 };
1633
1634 static GList *exif_unmap_list = 0;
1635
1636 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1637 {
1638         guint offset;
1639         const gchar* path;
1640         struct stat st;
1641         guchar *map_data;
1642         size_t map_len;
1643         int fd;
1644         
1645         if (!exif) return NULL;
1646         path = exif->path;
1647
1648         fd = open(path, O_RDONLY);
1649                 
1650                 
1651         if (fd == -1)
1652                 {
1653                 return 0;
1654                 }
1655
1656         if (fstat(fd, &st) == -1)
1657                 {
1658                 close(fd);
1659                 return 0;
1660                 }
1661         map_len = st.st_size;
1662         map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1663         close(fd);
1664
1665         if (map_data == MAP_FAILED)
1666                 {
1667                 return 0;
1668                 }
1669
1670         if (format_raw_img_exif_offsets(map_data, map_len, &offset, NULL) && offset)
1671                 {
1672                 UnmapData *ud;
1673
1674                 DEBUG_1("%s: offset %u", path, offset);
1675
1676                 *data_len = map_len - offset;
1677                 ud = g_new(UnmapData, 1);
1678                 ud->ptr = map_data + offset;
1679                 ud->map_data = map_data;
1680                 ud->map_len = map_len;
1681                 
1682                 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1683                 return ud->ptr;
1684                 }
1685
1686         munmap(map_data, map_len);
1687         return NULL;
1688
1689 }
1690
1691 void exif_free_preview(guchar *buf)
1692 {
1693         GList *work = exif_unmap_list;
1694         
1695         while (work)
1696                 {
1697                 UnmapData *ud = (UnmapData *)work->data;
1698                 if (ud->ptr == buf)
1699                         {
1700                         exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1701                         g_free(ud);
1702                         return;
1703                         }
1704                 work = work->next;
1705                 }
1706         g_assert_not_reached();
1707 }
1708
1709 void exif_init(void)
1710 {
1711 }
1712
1713 #endif
1714 /* not HAVE_EXIV2 */
1715 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */