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