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