6 * Support for Exif file format, originally written by Eric Swalens.
7 * Modified by Quy Tonthat
9 * Reimplemented with generic data storage by John Ellis (Nov 2003)
11 * The tags were added with information from the FREE document:
12 * http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
14 * For the official Exif Format, please refer to:
16 * http://www.exif.org/specifications.html (PDF spec sheets)
19 * Additional tag formats should be added to the proper
20 * location in ExifKnownMarkersList[].
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'.
27 * Unsupported at this time:
33 * Convert data to useable form in the ??_as_text function for:
34 * ComponentsConfiguration
35 * UserComment (convert this to UTF-8?)
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.
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.
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.
63 #include <sys/types.h>
75 #include "format_raw.h"
76 #include "ui_fileops.h"
80 *-----------------------------------------------------------------------------
82 *-----------------------------------------------------------------------------
85 ExifFormatAttrib ExifFormatList[] = {
86 { EXIF_FORMAT_UNKNOWN, 1, "unknown", "unknown" },
87 { EXIF_FORMAT_BYTE_UNSIGNED, 1, "ubyte", "unsigned byte" },
88 { EXIF_FORMAT_STRING, 1, "string", "string" },
89 { EXIF_FORMAT_SHORT_UNSIGNED, 2, "ushort", "unsigned short" },
90 { EXIF_FORMAT_LONG_UNSIGNED, 4, "ulong", "unsigned long" },
91 { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
92 { EXIF_FORMAT_BYTE, 1, "byte", "byte" },
93 { EXIF_FORMAT_UNDEFINED, 1, "undefined", "undefined" },
94 { EXIF_FORMAT_SHORT, 2, "sshort", "signed short" },
95 { EXIF_FORMAT_LONG, 4, "slong", "signed long" },
96 { EXIF_FORMAT_RATIONAL, 8, "srational", "signed rational" },
97 { EXIF_FORMAT_FLOAT, 4, "float", "float" },
98 { EXIF_FORMAT_DOUBLE, 8, "double", "double" },
102 /* tags that are special, or need special treatment */
103 #define TAG_EXIFOFFSET 0x8769
104 #define TAG_EXIFMAKERNOTE 0x927c
108 *-----------------------------------------------------------------------------
110 *-----------------------------------------------------------------------------
113 static ExifTextList ExifOrientationList[] = {
114 { EXIF_ORIENTATION_UNKNOWN, N_("unknown") },
115 { EXIF_ORIENTATION_TOP_LEFT, N_("top left") },
116 { EXIF_ORIENTATION_TOP_RIGHT, N_("top right") },
117 { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
118 { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
119 { EXIF_ORIENTATION_LEFT_TOP, N_("left top") },
120 { EXIF_ORIENTATION_RIGHT_TOP, N_("right top") },
121 { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
122 { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
126 static ExifTextList ExifUnitList[] = {
127 { EXIF_UNIT_UNKNOWN, N_("unknown") },
128 { EXIF_UNIT_NOUNIT, "" },
129 { EXIF_UNIT_INCH, N_("inch") },
130 { EXIF_UNIT_CENTIMETER, N_("centimeter") },
134 static ExifTextList ExifYCbCrPosList[] = {
140 static ExifTextList ExifMeteringModeList[] = {
141 { 0, N_("unknown") },
142 { 1, N_("average") },
143 { 2, N_("center weighted") },
145 { 4, N_("multi-spot") },
146 { 5, N_("multi-segment") },
147 { 6, N_("partial") },
148 { 255, N_("other") },
152 static ExifTextList ExifExposureProgramList[] = {
153 { 0, N_("not defined") },
156 { 3, N_("aperture") },
157 { 4, N_("shutter") },
158 { 5, N_("creative") },
160 { 7, N_("portrait") },
161 { 8, N_("landscape") },
165 static ExifTextList ExifLightSourceList[] = {
166 { 0, N_("unknown") },
167 { 1, N_("daylight") },
168 { 2, N_("fluorescent") },
169 { 3, N_("tungsten (incandescent)") },
171 { 9, "fine weather" },
172 { 10, "cloudy weather" },
174 { 12, "daylight fluorescent" },
175 { 13, "day white fluorescent" },
176 { 14, "cool white fluorescent" },
177 { 15, "while fluorescent" },
178 { 17, "standard light A" },
179 { 18, "standard light B" },
180 { 19, "standard light C" },
185 { 24, "ISO studio tungsten" },
186 { 255, N_("other") },
190 static ExifTextList ExifFlashList[] = {
193 { 5, N_("yes, not detected by strobe") },
194 { 7, N_("yes, detected by strobe") },
198 static ExifTextList ExifColorSpaceList[] = {
200 { 65535,"uncalibrated" },
204 static ExifTextList ExifSensorList[] = {
205 { 1, "not defined" },
206 { 2, "1 chip color area" },
207 { 2, "2 chip color area" },
208 { 4, "3 chip color area" },
209 { 5, "color sequential area" },
211 { 8, "color sequential linear" },
215 static ExifTextList ExifSourceList[] = {
216 { 3, "digital still camera" },
220 static ExifTextList ExifSceneList[] = {
221 { 1, "direct photo" },
225 static ExifTextList ExifCustRenderList[] = {
231 static ExifTextList ExifExposureModeList[] = {
234 { 2, "auto bracket" },
238 static ExifTextList ExifWhiteBalanceList[] = {
244 static ExifTextList ExifSceneCaptureList[] = {
248 { 3, "night scene" },
252 static ExifTextList ExifGainControlList[] = {
254 { 1, "low gain up" },
255 { 2, "high gain up" },
256 { 3, "low gain down" },
257 { 4, "high gain down" },
261 static ExifTextList ExifContrastList[] = {
268 static ExifTextList ExifSaturationList[] = {
275 static ExifTextList ExifSharpnessList[] = {
282 static ExifTextList ExifSubjectRangeList[] = {
290 ExifMarker ExifKnownMarkersList[] = {
291 { 0x010e, EXIF_FORMAT_STRING, -1, "Exif.Image.ImageDescription", N_("Image description"), NULL },
292 { 0x010f, EXIF_FORMAT_STRING, -1, "Exif.Image.Make", "Camera make", NULL },
293 { 0x0110, EXIF_FORMAT_STRING, -1, "Exif.Image.Model", "Camera model", NULL },
294 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Orientation", N_("Orientation"), ExifOrientationList },
295 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.XResolution", "X resolution", NULL },
296 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.YResolution", "Y Resolution", NULL },
297 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.ResolutionUnit", "Resolution units", ExifUnitList },
298 { 0x0131, EXIF_FORMAT_STRING, -1, "Exif.Image.Software", "Firmware", NULL },
299 { 0x0132, EXIF_FORMAT_STRING, 20, "Exif.Image.DateTime", N_("Date"), NULL },
300 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2, "Exif.Image.WhitePoint", "White point", NULL },
301 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.PrimaryChromaticities","Primary chromaticities", NULL },
302 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.Image.YCbCrCoefficients", "YCbCy coefficients", NULL },
303 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.YCbCrPositioning", "YCbCr positioning", ExifYCbCrPosList },
304 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.ReferenceBlackWhite", "Black white reference", NULL },
305 { 0x8298, EXIF_FORMAT_STRING, -1, "Exif.Image.Copyright", N_("Copyright"), NULL },
306 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifOffset", "SubIFD Exif offset", NULL },
308 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureTime", "Exposure time (seconds)", NULL },
309 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FNumber", "FNumber", NULL },
310 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureProgram", N_("Exposure program"), ExifExposureProgramList },
311 { 0x8824, EXIF_FORMAT_STRING, -1, "Exif.Photo.SpectralSensitivity", "Spectral Sensitivity", NULL },
312 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.ISOSpeedRatings", N_("ISO sensitivity"), NULL },
313 { 0x8828, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.OECF", "Optoelectric conversion factor", NULL },
314 { 0x9000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.ExifVersion", "Exif version", NULL },
315 { 0x9003, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeOriginal", N_("Date original"), NULL },
316 { 0x9004, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeDigitized", N_("Date digitized"), NULL },
317 { 0x9101, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.ComponentsConfiguration","Pixel format", NULL },
318 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1, "Exif.Photo.CompressedBitsPerPixel","Compression ratio", NULL },
319 { 0x9201, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), NULL },
320 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ApertureValue", N_("Aperture"), NULL },
321 { 0x9203, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.BrightnessValue", "Brightness", NULL },
322 { 0x9204, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), NULL },
323 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.MaxApertureValue", "Maximum aperture", NULL },
324 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.SubjectDistance", N_("Subject distance"), NULL },
325 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.MeteringMode", N_("Metering mode"), ExifMeteringModeList },
326 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.LightSource", N_("Light source"), ExifLightSourceList },
327 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Flash", N_("Flash"), ExifFlashList },
328 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalLength", N_("Focal length"), NULL },
329 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SubjectArea", "Subject area", NULL },
330 { 0x927c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.MakerNote", "MakerNote", NULL },
331 { 0x9286, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.UserComment", "UserComment", NULL },
332 { 0x9290, EXIF_FORMAT_STRING, -1, "SubsecTime", "Subsecond time", NULL },
333 { 0x9291, EXIF_FORMAT_STRING, -1, "SubsecTimeOriginal", "Subsecond time original", NULL },
334 { 0x9292, EXIF_FORMAT_STRING, -1, "SubsecTimeDigitized", "Subsecond time digitized", NULL },
335 { 0xa000, EXIF_FORMAT_UNDEFINED, 4, "FlashPixVersion", "FlashPix version", NULL },
336 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ColorSpace", "Colorspace", ExifColorSpaceList },
337 /* ExifImageWidth, ExifImageHeight can also be unsigned short */
338 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifImageWidth", N_("Width"), NULL },
339 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifImageHeight", N_("Height"), NULL },
340 { 0xa004, EXIF_FORMAT_STRING, -1, "Exif.Photo.RelatedSoundFile", "Audio data", NULL },
341 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifInteroperabilityOffset", "ExifR98 extension", NULL },
342 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", "Flash strength", NULL },
343 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SpatialFrequencyResponse","Spatial frequency response", NULL },
344 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneXResolution", "X Pixel density", NULL },
345 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneYResolution", "Y Pixel density", NULL },
346 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalPlaneResolutionUnit", "Pixel density units", ExifUnitList },
347 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Photo.SubjectLocation", "Subject location", NULL },
348 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", N_("ISO sensitivity"), NULL },
349 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SensingMethod", "Sensor type", ExifSensorList },
350 { 0xa300, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.FileSource", "Source type", ExifSourceList },
351 { 0xa301, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.SceneType", "Scene type", ExifSceneList },
352 { 0xa302, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.CFAPattern", "Color filter array pattern", NULL },
353 /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
354 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.CustomRendered", "Render process", ExifCustRenderList },
355 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureMode", "Exposure mode", ExifExposureModeList },
356 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.WhiteBalance", "White balance", ExifWhiteBalanceList },
357 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.DigitalZoomRatio", "Digital zoom ratio", NULL },
358 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1, "FocalLength35mmFilm", "Focal length (35mm)", NULL },
359 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SceneCapturetype", "Scene capture type", ExifSceneCaptureList },
360 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.GainControl", "Gain control", ExifGainControlList },
361 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Contrast", "Contrast", ExifContrastList },
362 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Saturation", "Saturation", ExifSaturationList },
363 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Sharpness", "Sharpness", ExifSharpnessList },
364 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.DeviceSettingDescription","Device setting", NULL },
365 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SubjectDistanceRange", "Subject range", ExifSubjectRangeList },
366 { 0xa420, EXIF_FORMAT_STRING, -1, "Exif.Photo.ImageUniqueID", "Image serial number", NULL },
367 /* place known, but undocumented or lesser used tags here */
368 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.NewSubfileType", NULL, NULL },
369 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubfileType", NULL, NULL },
370 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3, "Exif.Image.TransferFunction", NULL, NULL },
371 { 0x013b, EXIF_FORMAT_STRING, -1, "Exif.Image.Artist", "Artist", NULL },
372 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Predictor", NULL, NULL },
373 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileWidth", NULL, NULL },
374 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileLength", NULL, NULL },
375 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1, "TileOffsets", NULL, NULL },
376 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1, "TileByteCounts", NULL, NULL },
377 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1, "Exif.Image.SubIFDs", NULL, NULL },
378 { 0x015b, EXIF_FORMAT_UNDEFINED, -1, "JPEGTables", NULL, NULL },
379 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Image.CFARepeatPatternDim", NULL, NULL },
380 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Exif.Image.CFAPattern", NULL, NULL },
381 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.BatteryLevel", NULL, NULL },
382 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1, "IPTC/NAA", NULL, NULL },
383 { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "ColorProfile", NULL, NULL },
384 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1, "GPSInfo", "SubIFD GPS offset", NULL },
385 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Interlace", NULL, NULL },
386 { 0x882a, EXIF_FORMAT_SHORT, 1, "TimeZoneOffset", NULL, NULL },
387 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SelfTimerMode", NULL, NULL },
388 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", NULL, NULL },
389 { 0x920c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.SpatialFrequencyResponse", NULL, NULL },
390 { 0x920d, EXIF_FORMAT_UNDEFINED, -1, "Noise", NULL, NULL },
391 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1, "ImageNumber", NULL, NULL },
392 { 0x9212, EXIF_FORMAT_STRING, 1, "SecurityClassification", NULL, NULL },
393 { 0x9213, EXIF_FORMAT_STRING, -1, "ImageHistory", NULL, NULL },
394 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", NULL, NULL },
395 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4, "TIFF/EPStandardID", NULL, NULL },
400 ExifMarker ExifUnknownMarkersList[] = {
401 { 0x0000, EXIF_FORMAT_UNKNOWN, 0, "unknown", NULL, NULL },
402 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1, "unknown", NULL, NULL },
403 { 0x0000, EXIF_FORMAT_STRING, -1, "unknown", NULL, NULL },
404 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1, "unknown", NULL, NULL },
405 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1, "unknown", NULL, NULL },
406 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "unknown", NULL, NULL },
407 { 0x0000, EXIF_FORMAT_BYTE, -1, "unknown", NULL, NULL },
408 { 0x0000, EXIF_FORMAT_UNDEFINED, -1, "unknown", NULL, NULL },
409 { 0x0000, EXIF_FORMAT_SHORT, -1, "unknown", NULL, NULL },
410 { 0x0000, EXIF_FORMAT_LONG, -1, "unknown", NULL, NULL },
411 { 0x0000, EXIF_FORMAT_RATIONAL, -1, "unknown", NULL, NULL },
412 { 0x0000, EXIF_FORMAT_FLOAT, -1, "unknown", NULL, NULL },
413 { 0x0000, EXIF_FORMAT_DOUBLE, -1, "unknown", NULL, NULL },
416 /* human readable key list */
418 ExifFormattedText ExifFormattedList[] = {
419 { "fCamera", N_("Camera") },
420 { "fDateTime", N_("Date") },
421 { "fShutterSpeed", N_("Shutter speed") },
422 { "fAperture", N_("Aperture") },
423 { "fExposureBias", N_("Exposure bias") },
424 { "fISOSpeedRating", N_("ISO sensitivity") },
425 { "fFocalLength", N_("Focal length") },
426 { "fSubjectDistance", N_("Subject distance") },
427 { "fFlash", N_("Exif.Photo.Flash") },
428 { "fResolution", N_("Resolution") },
433 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
436 *-----------------------------------------------------------------------------
438 *-----------------------------------------------------------------------------
441 ExifItem *exif_item_new(ExifFormatType format, guint tag,
442 guint elements, const ExifMarker *marker)
446 item = g_new0(ExifItem, 1);
447 item->format = format;
449 item->marker = marker;
450 item->elements = elements;
456 case EXIF_FORMAT_UNKNOWN:
457 /* unknown, data is NULL */
460 case EXIF_FORMAT_BYTE_UNSIGNED:
461 item->data_len = sizeof(char) * elements;
463 case EXIF_FORMAT_STRING:
464 item->data_len = sizeof(char) * elements;
466 case EXIF_FORMAT_SHORT_UNSIGNED:
467 item->data_len = sizeof(guint16) * elements;
469 case EXIF_FORMAT_LONG_UNSIGNED:
470 item->data_len = sizeof(guint32) * elements;
472 case EXIF_FORMAT_RATIONAL_UNSIGNED:
473 item->data_len = sizeof(ExifRational) * elements;
475 case EXIF_FORMAT_BYTE:
476 item->data_len = sizeof(char) * elements;
478 case EXIF_FORMAT_UNDEFINED:
479 item->data_len = sizeof(char) * elements;
481 case EXIF_FORMAT_SHORT:
482 item->data_len = sizeof(gint16) * elements;
484 case EXIF_FORMAT_LONG:
485 item->data_len = sizeof(gint32) * elements;
487 case EXIF_FORMAT_RATIONAL:
488 item->data_len = sizeof(ExifRational) * elements;
490 case EXIF_FORMAT_FLOAT:
491 item->data_len = sizeof(float) * elements;
493 case EXIF_FORMAT_DOUBLE:
494 item->data_len = sizeof(double) * elements;
498 item->data = g_malloc0(item->data_len);
503 static void exif_item_free(ExifItem *item)
511 const char *exif_item_get_tag_name(ExifItem *item)
513 if (!item || !item->marker) return NULL;
514 return item->marker->key;
517 guint exif_item_get_tag_id(ExifItem *item)
522 guint exif_item_get_elements(ExifItem *item)
524 return item->elements;
527 char *exif_item_get_data(ExifItem *item, guint *data_len)
530 *data_len = item->data_len;
534 guint exif_item_get_format_id(ExifItem *item)
540 const char *exif_item_get_description(ExifItem *item)
542 if (!item || !item->marker) return NULL;
543 return _(item->marker->description);
546 const char *exif_item_get_format_name(ExifItem *item, gint brief)
548 if (!item || !item->marker) return NULL;
549 return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
552 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
556 for (i = 0 ; i < ne; i++)
558 unsigned char c = ((char *)data)[i];
559 if (c < 32 || c > 127) c = '.';
560 g_string_append_printf(string, "%c", c);
562 string = g_string_append(string, " : ");
563 for (i = 0 ; i < ne; i++)
581 g_string_append_printf(string, "%s%02x", spacer, ((char *)data)[i]);
587 gchar *exif_text_list_find_value(ExifTextList *list, guint value)
589 gchar *result = NULL;
593 while (!result && list[i].value >= 0)
595 if (value == list[i].value) result = g_strdup(_(list[i].description));
598 if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
605 *-------------------------------------------------------------------
607 *-------------------------------------------------------------------
610 /* note: the align_buf is used to avoid alignment issues (on sparc) */
612 guint16 exif_byte_get_int16(unsigned char *f, ExifByteOrder bo)
616 memcpy(&align_buf, f, sizeof(guint16));
618 if (bo == EXIF_BYTE_ORDER_INTEL)
619 return GUINT16_FROM_LE(align_buf);
621 return GUINT16_FROM_BE(align_buf);
624 guint32 exif_byte_get_int32(unsigned char *f, ExifByteOrder bo)
628 memcpy(&align_buf, f, sizeof(guint32));
630 if (bo == EXIF_BYTE_ORDER_INTEL)
631 return GUINT32_FROM_LE(align_buf);
633 return GUINT32_FROM_BE(align_buf);
636 void exif_byte_put_int16(unsigned char *f, guint16 n, ExifByteOrder bo)
640 if (bo == EXIF_BYTE_ORDER_INTEL)
642 align_buf = GUINT16_TO_LE(n);
646 align_buf = GUINT16_TO_BE(n);
649 memcpy(f, &align_buf, sizeof(guint16));
652 void exif_byte_put_int32(unsigned char *f, guint32 n, ExifByteOrder bo)
656 if (bo == EXIF_BYTE_ORDER_INTEL)
658 align_buf = GUINT32_TO_LE(n);
662 align_buf = GUINT32_TO_BE(n);
665 memcpy(f, &align_buf, sizeof(guint32));
670 *-------------------------------------------------------------------
672 *-------------------------------------------------------------------
675 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list)
679 if (!list) return NULL;
681 while (list[i].tag != 0 && list[i].tag != tag)
686 return (list[i].tag == 0 ? NULL : &list[i]);
689 static void rational_from_data(ExifRational *r, void *src, ExifByteOrder bo)
691 r->num = exif_byte_get_int32(src, bo);
692 r->den = exif_byte_get_int32(src + sizeof(guint32), bo);
695 /* src_format and item->format must be compatible
696 * and not overrun src or item->data.
698 void exif_item_copy_data(ExifItem *item, void *src, guint len,
699 ExifFormatType src_format, ExifByteOrder bo)
706 bs = ExifFormatList[item->format].size;
711 ExifFormatList[src_format].size * ne > len)
713 printf("exif tag %s data size mismatch\n", exif_item_get_tag_name(item));
717 switch (item->format)
719 case EXIF_FORMAT_UNKNOWN:
721 case EXIF_FORMAT_BYTE_UNSIGNED:
722 case EXIF_FORMAT_BYTE:
723 case EXIF_FORMAT_UNDEFINED:
724 memcpy(dest, src, len);
726 case EXIF_FORMAT_STRING:
727 memcpy(dest, src, len);
728 /* string is NULL terminated, make sure this is true */
729 if (((char *)dest)[len - 1] != '\0') ((char *)dest)[len - 1] = '\0';
731 case EXIF_FORMAT_SHORT_UNSIGNED:
732 case EXIF_FORMAT_SHORT:
733 for (i = 0; i < ne; i++)
735 ((guint16 *)dest)[i] = exif_byte_get_int16(src + i * bs, bo);
738 case EXIF_FORMAT_LONG_UNSIGNED:
739 case EXIF_FORMAT_LONG:
740 if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
741 src_format == EXIF_FORMAT_SHORT)
743 /* a short fits into a long, so allow it */
746 ss = ExifFormatList[src_format].size;
747 for (i = 0; i < ne; i++)
749 ((gint32 *)dest)[i] =
750 (gint32)exif_byte_get_int16(src + i * ss, bo);
755 for (i = 0; i < ne; i++)
757 ((gint32 *)dest)[i] =
758 exif_byte_get_int32(src + i * bs, bo);
762 case EXIF_FORMAT_RATIONAL_UNSIGNED:
763 case EXIF_FORMAT_RATIONAL:
764 for (i = 0; i < ne; i++)
766 rational_from_data(&((ExifRational *)dest)[i], src + i * bs, bo);
769 case EXIF_FORMAT_FLOAT:
770 for (i = 0; i < ne; i++)
772 ((float *)dest)[i] = exif_byte_get_int32(src + i * bs, bo);
775 case EXIF_FORMAT_DOUBLE:
776 for (i = 0; i < ne; i++)
780 rational_from_data(&r, src + i * bs, bo);
781 if (r.den) ((double *)dest)[i] = (double)r.num / r.den;
787 static gint exif_parse_IFD_entry(ExifData *exif, unsigned char *tiff, guint offset,
788 guint size, ExifByteOrder bo,
790 const ExifMarker *list)
798 const ExifMarker *marker;
801 tag = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_TAG, bo);
802 format = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
803 count = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_COUNT, bo);
804 data_val = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_DATA, bo);
806 /* Check tag type. If it does not match, either the format is wrong,
807 * either it is a unknown tag; so it is not really an error.
809 marker = exif_marker_from_tag(tag, list);
812 if (format >= EXIF_FORMAT_COUNT)
814 printf("warning: exif tag 0x%4x has invalid format %d\n", tag, format);
817 /* allow non recognized tags to be displayed */
818 marker = &ExifUnknownMarkersList[format];
820 if (marker->format != format)
822 /* Some cameras got mixed up signed/unsigned_rational
823 * eg KODAK DC4800 on object_distance tag
825 * FIXME: what exactly is this test trying to do?
826 * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
828 if (!(marker->format == EXIF_FORMAT_RATIONAL_UNSIGNED && format == EXIF_FORMAT_RATIONAL) &&
829 !(marker->format == EXIF_FORMAT_RATIONAL && format == EXIF_FORMAT_RATIONAL_UNSIGNED) &&
830 /* short fits into a long so allow this mismatch
831 * as well (some tags allowed to be unsigned short _or_ unsigned long)
833 !(marker->format == EXIF_FORMAT_LONG_UNSIGNED && format == EXIF_FORMAT_SHORT_UNSIGNED) )
835 if (format < EXIF_FORMAT_COUNT)
837 printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
838 marker->key, ExifFormatList[format].short_name,
839 ExifFormatList[marker->format].short_name);
843 printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
844 marker->key, format, marker->format,
845 ExifFormatList[marker->format].short_name);
851 /* Where is the data, is it available?
853 if (marker->components > 0 && marker->components != count)
855 printf("warning: exif tag %s has %d elements, exif spec requests %d\n",
856 marker->key, count, marker->components);
859 data_length = ExifFormatList[marker->format].size * count;
862 data_offset = data_val;
863 if (size < data_offset + data_length)
865 printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
871 data_offset = offset + EXIF_TIFD_OFFSET_DATA;
874 item = exif_item_new(marker->format, tag, count, marker);
875 exif_item_copy_data(item, tiff + data_offset, data_length, format, bo);
876 exif->items = g_list_prepend(exif->items, item);
878 if (list == ExifKnownMarkersList)
883 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, list);
885 case TAG_EXIFMAKERNOTE:
886 format_exif_makernote_parse(exif, tiff, data_val, size, bo);
894 gint exif_parse_IFD_table(ExifData *exif,
895 unsigned char *tiff, guint offset,
896 guint size, ExifByteOrder bo,
898 const ExifMarker *list)
903 /* limit damage from infinite loops */
904 if (level > EXIF_TIFF_MAX_LEVELS) return -1;
906 /* We should be able to read number of entries in IFD0) */
907 if (size < offset + 2) return -1;
909 count = exif_byte_get_int16(tiff + offset, bo);
912 /* Entries and next IFD offset must be readable */
913 if (size < offset + count * EXIF_TIFD_SIZE + 4) return -1;
915 for (i = 0; i < count; i++)
917 exif_parse_IFD_entry(exif, tiff, offset + i * EXIF_TIFD_SIZE, size, bo, level, list);
924 *-------------------------------------------------------------------
926 *-------------------------------------------------------------------
929 gint exif_tiff_directory_offset(unsigned char *data, const guint len,
930 guint *offset, ExifByteOrder *bo)
932 if (len < 8) return FALSE;
934 if (memcmp(data, "II", 2) == 0)
936 *bo = EXIF_BYTE_ORDER_INTEL;
938 else if (memcmp(data, "MM", 2) == 0)
940 *bo = EXIF_BYTE_ORDER_MOTOROLA;
947 if (exif_byte_get_int16(data + 2, *bo) != 0x002A)
952 *offset = exif_byte_get_int32(data + 4, *bo);
954 return (*offset < len);
957 gint exif_tiff_parse(ExifData *exif, unsigned char *tiff, guint size, ExifMarker *list)
962 if (!exif_tiff_directory_offset(tiff, size, &offset, &bo)) return -1;
964 return exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, list);
968 *-------------------------------------------------------------------
970 *-------------------------------------------------------------------
973 #define JPEG_MARKER 0xFF
974 #define JPEG_MARKER_SOI 0xD8
975 #define JPEG_MARKER_EOI 0xD9
976 #define JPEG_MARKER_APP1 0xE1
977 #define JPEG_MARKER_APP2 0xE2
979 /* jpeg container format:
980 all data markers start with 0XFF
981 2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
982 4 byte long data segment markers in format: 0xFFTTSSSSNNN...
983 FF: 1 byte standard marker identifier
985 SSSS: 2 bytes in Motorola byte alignment for length of the data.
986 This value includes these 2 bytes in the count, making actual
987 length of NN... == SSSS - 2.
988 NNN.: the data in this segment
991 static gint exif_jpeg_segment_find(unsigned char *data, guint size,
992 guchar app_marker, const gchar *magic, guint magic_len,
993 guint *seg_offset, guint *seg_length)
999 while (marker != app_marker &&
1000 marker != JPEG_MARKER_EOI)
1005 if (offset + 2 >= size ||
1006 data[offset] != JPEG_MARKER) return FALSE;
1008 marker = data[offset + 1];
1009 if (marker != JPEG_MARKER_SOI &&
1010 marker != JPEG_MARKER_EOI)
1012 if (offset + 4 >= size) return FALSE;
1013 length += exif_byte_get_int16(data + offset + 2, EXIF_BYTE_ORDER_MOTOROLA);
1017 if (marker == app_marker &&
1018 offset + length < size &&
1019 length >= 4 + magic_len &&
1020 memcmp(data + offset + 4, magic, magic_len) == 0)
1022 *seg_offset = offset + 4;
1023 *seg_length = length - 4;
1030 static ExifMarker jpeg_color_marker = { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "ColorProfile", NULL, NULL };
1032 static gint exif_jpeg_parse_color(ExifData *exif, unsigned char *data, guint size)
1034 guint seg_offset = 0;
1035 guint seg_length = 0;
1036 guint chunk_offset[255];
1037 guint chunk_length[255];
1038 guint chunk_count = 0;
1040 /* For jpeg/jfif, ICC color profile data can be in more than one segment.
1041 the data is in APP2 data segments that start with "ICC_PROFILE\x00\xNN\xTT"
1042 NN = segment number for data
1043 TT = total number of ICC segments (TT in each ICC segment should match)
1046 while (exif_jpeg_segment_find(data + seg_offset + seg_length,
1047 size - seg_offset - seg_length,
1049 "ICC_PROFILE\x00", 12,
1050 &seg_offset, &seg_length))
1055 if (seg_length < 14) return FALSE;
1057 chunk_num = data[seg_offset + 12];
1058 chunk_tot = data[seg_offset + 13];
1060 if (chunk_num == 0 || chunk_tot == 0) return FALSE;
1062 if (chunk_count == 0)
1066 chunk_count = (guint)chunk_tot;
1067 for (i = 0; i < chunk_count; i++) chunk_offset[i] = 0;
1068 for (i = 0; i < chunk_count; i++) chunk_length[i] = 0;
1071 if (chunk_tot != chunk_count ||
1072 chunk_num > chunk_count) return FALSE;
1075 chunk_offset[chunk_num] = seg_offset + 14;
1076 chunk_length[chunk_num] = seg_length - 14;
1079 if (chunk_count > 0)
1082 unsigned char *cp_data;
1083 guint cp_length = 0;
1086 for (i = 0; i < chunk_count; i++) cp_length += chunk_length[i];
1087 cp_data = g_malloc(cp_length);
1089 for (i = 0; i < chunk_count; i++)
1091 if (chunk_offset[i] == 0)
1093 /* error, we never saw this chunk */
1097 memcpy(cp_data, data + chunk_offset[i], chunk_length[i]);
1100 item = exif_item_new(jpeg_color_marker.format, jpeg_color_marker.tag, 1,
1101 &jpeg_color_marker);
1103 item->data = cp_data;
1104 item->elements = cp_length;
1105 item->data_len = cp_length;
1106 exif->items = g_list_prepend(exif->items, item);
1114 static gint exif_jpeg_parse(ExifData *exif,
1115 unsigned char *data, guint size,
1116 ExifMarker *list, gint parse_color)
1118 guint seg_offset = 0;
1119 guint seg_length = 0;
1123 memcmp(data, "\xFF\xD8", 2) != 0)
1128 if (exif_jpeg_segment_find(data, size, JPEG_MARKER_APP1,
1130 &seg_offset, &seg_length))
1132 res = exif_tiff_parse(exif, data + seg_offset + 6, seg_length - 6, list);
1136 exif_jpeg_parse_color(exif, data, size))
1146 *-------------------------------------------------------------------
1148 *-------------------------------------------------------------------
1152 ExifItem *exif_get_first_item(ExifData *exif)
1156 ExifItem *ret = (ExifItem *)exif->items->data;
1157 exif->current = exif->items->next;
1160 exif->current = NULL;
1164 ExifItem *exif_get_next_item(ExifData *exif)
1168 ExifItem *ret = (ExifItem *)exif->current->data;
1169 exif->current = exif->current->next;
1177 static gint map_file(const gchar *path, void **mapping, int *size)
1182 if ((fd = open(path, O_RDONLY)) == -1)
1188 if (fstat(fd, &fs) == -1)
1197 if ((*mapping = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
1208 static gint unmap_file(void *mapping, int size)
1210 if (munmap(mapping, size) == -1)
1219 void exif_free(ExifData *exif)
1228 ExifItem *item = work->data;
1230 exif_item_free(item);
1233 g_list_free(exif->items);
1237 ExifData *exif_read(gchar *path, gint parse_color_profile)
1244 if (!fd) return NULL;
1246 pathl = path_from_utf8(path);
1247 if (map_file(pathl, &f, &size) == -1)
1254 exif = g_new0(ExifData, 1);
1256 exif->current = NULL;
1258 if ((res = exif_jpeg_parse(exif, (unsigned char *)f, size,
1259 ExifKnownMarkersList,
1260 parse_color_profile)) == -2)
1262 res = exif_tiff_parse(exif, (unsigned char *)f, size, ExifKnownMarkersList);
1267 FormatRawExifType exif_type;
1268 FormatRawExifParseFunc exif_parse_func;
1271 exif_type = format_raw_exif_offset(f, size, &offset, &exif_parse_func);
1274 case FORMAT_RAW_EXIF_NONE:
1277 case FORMAT_RAW_EXIF_TIFF:
1278 res = exif_tiff_parse(exif, (unsigned char*)f + offset, size - offset,
1279 ExifKnownMarkersList);
1281 case FORMAT_RAW_EXIF_JPEG:
1282 res = exif_jpeg_parse(exif, (unsigned char*)f + offset, size - offset,
1283 ExifKnownMarkersList, FALSE);
1285 case FORMAT_RAW_EXIF_IFD_II:
1286 case FORMAT_RAW_EXIF_IFD_MM:
1287 res = exif_parse_IFD_table(exif, (unsigned char*)f, offset, size - offset,
1288 (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1289 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1290 0, ExifKnownMarkersList);
1292 case FORMAT_RAW_EXIF_PROPRIETARY:
1293 if (exif_parse_func)
1295 res = exif_parse_func((unsigned char*)f + offset, size - offset, exif);
1307 unmap_file(f, size);
1309 if (exif) exif->items = g_list_reverse(exif->items);
1312 exif_write_data_list(exif, stdout, TRUE);
1313 exif_write_data_list(exif, stdout, FALSE);
1319 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1323 if (!key) return NULL;
1332 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1337 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1339 gchar *exif_item_get_data_as_text(ExifItem *item)
1341 const ExifMarker *marker;
1348 if (!item) return NULL;
1350 marker = item->marker;
1351 if (!marker) return NULL;
1354 ne = item->elements;
1355 if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1356 string = g_string_new("");
1357 switch (item->format)
1359 case EXIF_FORMAT_UNKNOWN:
1361 case EXIF_FORMAT_BYTE_UNSIGNED:
1362 case EXIF_FORMAT_BYTE:
1363 case EXIF_FORMAT_UNDEFINED:
1364 if (ne == 1 && marker->list)
1369 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1370 item->format == EXIF_FORMAT_UNDEFINED)
1372 val = ((unsigned char *)data)[0];
1376 val = (unsigned char)(((signed char *)data)[0]);
1379 result = exif_text_list_find_value(marker->list, (guint)val);
1380 string = g_string_append(string, result);
1385 string = string_append_raw_bytes(string, data, ne);
1388 case EXIF_FORMAT_STRING:
1389 string = g_string_append(string, (gchar *)(item->data));
1391 case EXIF_FORMAT_SHORT_UNSIGNED:
1392 if (ne == 1 && marker->list)
1396 result = exif_text_list_find_value(marker->list, ((guint16 *)data)[0]);
1397 string = g_string_append(string, result);
1400 else for (i = 0; i < ne; i++)
1402 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1403 ((guint16 *)data)[i]);
1406 case EXIF_FORMAT_LONG_UNSIGNED:
1407 for (i = 0; i < ne; i++)
1409 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1410 (unsigned long int)((guint32 *)data)[i]);
1413 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1414 for (i = 0; i < ne; i++)
1418 r = &((ExifRational *)data)[i];
1419 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1420 (unsigned long)r->num, (unsigned long)r->den);
1423 case EXIF_FORMAT_SHORT:
1424 for (i = 0; i < ne; i++)
1426 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1427 ((gint16 *)data)[i]);
1430 case EXIF_FORMAT_LONG:
1431 for (i = 0; i < ne; i++)
1433 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1434 (long int)((gint32 *)data)[i]);
1437 case EXIF_FORMAT_RATIONAL:
1438 for (i = 0; i < ne; i++)
1442 r = &((ExifRational *)data)[i];
1443 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1444 (long)r->num, (long)r->den);
1447 case EXIF_FORMAT_FLOAT:
1448 for (i = 0; i < ne; i++)
1450 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1451 ((float *)data)[i]);
1454 case EXIF_FORMAT_DOUBLE:
1455 for (i = 0; i < ne; i++)
1457 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1458 ((double *)data)[i]);
1463 if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1464 item->format != EXIF_FORMAT_STRING)
1466 g_string_append(string, " ...");
1470 g_string_free(string, FALSE);
1475 gint exif_item_get_integer(ExifItem *item, gint *value)
1477 if (!item) return FALSE;
1479 switch (item->format)
1481 case EXIF_FORMAT_SHORT:
1482 *value = (gint)(((gint16 *)(item->data))[0]);
1485 case EXIF_FORMAT_SHORT_UNSIGNED:
1486 *value = (gint)(((guint16 *)(item->data))[0]);
1489 case EXIF_FORMAT_LONG:
1490 *value = (gint)(((gint32 *)(item->data))[0]);
1493 case EXIF_FORMAT_LONG_UNSIGNED:
1494 /* FIXME: overflow possible */
1495 *value = (gint)(((guint32 *)(item->data))[0]);
1498 /* all other type return FALSE */
1504 gint exif_get_integer(ExifData *exif, const gchar *key, gint *value)
1508 item = exif_get_item(exif, key);
1509 return exif_item_get_integer(item, value);
1512 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
1514 if (!item) return NULL;
1516 if (item->format == EXIF_FORMAT_RATIONAL ||
1517 item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1519 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1520 return &((ExifRational *)(item->data))[0];
1526 ExifRational *exif_get_rational(ExifData *exif, const gchar *key, gint *sign)
1530 item = exif_get_item(exif, key);
1531 return exif_item_get_rational(item, sign);
1534 double exif_rational_to_double(ExifRational *r, gint sign)
1536 if (!r || r->den == 0.0) return 0.0;
1538 if (sign) return (double)((int)r->num) / (double)((int)r->den);
1539 return (double)r->num / r->den;
1542 static double exif_get_rational_as_double(ExifData *exif, const gchar *key)
1547 r = exif_get_rational(exif, key, &sign);
1548 return exif_rational_to_double(r, sign);
1551 static GString *append_comma_text(GString *string, const gchar *text)
1553 string = g_string_append(string, ", ");
1554 string = g_string_append(string, text);
1559 static gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_valid)
1561 /* must begin with f, else not formatted */
1564 if (key_valid) *key_valid = FALSE;
1568 if (key_valid) *key_valid = TRUE;
1570 if (strcmp(key, "fCamera") == 0)
1573 gchar *make = exif_get_data_as_text(exif, "Exif.Image.Make");
1574 gchar *model = exif_get_data_as_text(exif, "Exif.Image.Model");
1575 gchar *software = exif_get_data_as_text(exif, "Exif.Image.Software");
1577 text = g_strdup_printf("%s%s%s%s%s%s", (make) ? make : "", ((make) && (model)) ? " " : "",
1578 (model) ? model : "",
1579 (software) ? " (" : "",
1580 (software) ? software : "",
1581 (software) ? ")" : "");
1588 if (strcmp(key, "fDateTime") == 0)
1590 gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
1591 gchar *subsec = NULL;
1592 if (text) subsec = exif_get_data_as_text(exif, "SubsecTimeOriginal");
1595 text = exif_get_data_as_text(exif, "Exif.Image.DateTime");
1596 if (text) subsec = exif_get_data_as_text(exif, "SubsecTime");
1601 text = g_strconcat(tmp, ".", subsec, NULL);
1607 if (strcmp(key, "fShutterSpeed") == 0)
1611 r = exif_get_rational(exif, "Exif.Photo.ExposureTime", NULL);
1612 if (r && r->num && r->den)
1614 double n = (double)r->den / (double)r->num;
1615 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
1616 n > 1.0 ? n : 1.0 / n);
1618 r = exif_get_rational(exif, "Exif.Photo.ShutterSpeedValue", NULL);
1619 if (r && r->num && r->den)
1621 double n = pow(2.0, exif_rational_to_double(r, TRUE));
1623 /* Correct exposure time to avoid values like 1/91s (seen on Minolta DImage 7) */
1624 if (n > 1.0 && (int)n - ((int)(n/10))*10 == 1) n--;
1626 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
1627 n > 1.0 ? floor(n) : 1.0 / n);
1631 if (strcmp(key, "fAperture") == 0)
1635 n = exif_get_rational_as_double(exif, "Exif.Photo.FNumber");
1636 if (n == 0.0) n = exif_get_rational_as_double(exif, "Exif.Photo.ApertureValue");
1637 if (n == 0.0) return NULL;
1639 return g_strdup_printf("f/%.1f", n);
1641 if (strcmp(key, "fExposureBias") == 0)
1647 r = exif_get_rational(exif, "Exif.Photo.ExposureBiasValue", &sign);
1648 if (!r) return NULL;
1650 n = exif_rational_to_double(r, sign);
1651 return g_strdup_printf("%+.1f", n);
1653 if (strcmp(key, "fFocalLength") == 0)
1657 n = exif_get_rational_as_double(exif, "Exif.Photo.FocalLength");
1658 if (n == 0.0) return NULL;
1659 return g_strdup_printf("%.2f mm", n);
1661 if (strcmp(key, "fISOSpeedRating") == 0)
1665 text = exif_get_data_as_text(exif, "Exif.Photo.ISOSpeedRatings");
1666 /* kodak may set this instead */
1667 if (!text) text = exif_get_data_as_text(exif, "Exif.Photo.ExposureIndex");
1670 if (strcmp(key, "fSubjectDistance") == 0)
1676 r = exif_get_rational(exif, "Exif.Photo.SubjectDistance", &sign);
1677 if (!r) return NULL;
1679 if ((long)r->num == 0xffffffff) return g_strdup(_("infinity"));
1680 if ((long)r->num == 0) return g_strdup(_("unknown"));
1682 n = exif_rational_to_double(r, sign);
1683 if (n == 0.0) return _("unknown");
1684 return g_strdup_printf("%.3f m", n);
1686 if (strcmp(key, "fFlash") == 0)
1688 /* grr, flash is a bitmask... */
1694 if (!exif_get_integer(exif, "Exif.Photo.Flash", &n)) return NULL;
1696 /* Exif 2.1 only defines first 3 bits */
1697 if (n <= 0x07) return exif_text_list_find_value(ExifFlashList, n);
1699 /* must be Exif 2.2 */
1700 string = g_string_new("");
1702 /* flash fired (bit 0) */
1703 string = g_string_append(string, (n & 0x01) ? _("yes") : _("no"));
1705 /* flash mode (bits 3, 4) */
1706 v = (n >> 3) & 0x03;
1707 if (v) string = append_comma_text(string, _("mode:"));
1711 string = g_string_append(string, _("on"));
1714 string = g_string_append(string, _("off"));
1717 string = g_string_append(string, _("auto"));
1721 /* return light (bits 1, 2) */
1722 v = (n >> 1) & 0x03;
1723 if (v == 2) string = append_comma_text(string, _("not detected by strobe"));
1724 if (v == 3) string = append_comma_text(string, _("detected by strobe"));
1726 /* we ignore flash function (bit 5) */
1728 /* red-eye (bit 6) */
1729 if ((n >> 5) & 0x01) string = append_comma_text(string, _("red-eye reduction"));
1732 g_string_free(string, FALSE);
1735 if (strcmp(key, "fResolution") == 0)
1737 ExifRational *rx, *ry;
1741 rx = exif_get_rational(exif, "Exif.Image.XResolution", NULL);
1742 ry = exif_get_rational(exif, "Exif.Image.YResolution", NULL);
1743 if (!rx || !ry) return NULL;
1745 units = exif_get_data_as_text(exif, "Exif.Image.ResolutionUnit");
1746 text = g_strdup_printf("%0.f x %0.f (%s/%s)", rx->den ? (double)rx->num / rx->den : 1.0,
1747 ry->den ? (double)ry->num / ry->den : 1.0,
1748 _("dot"), (units) ? units : _("unknown"));
1754 if (key_valid) *key_valid = FALSE;
1758 gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
1764 if (!key) return NULL;
1766 text = exif_get_formatted_by_key(exif, key, &key_valid);
1767 if (key_valid) return text;
1769 item = exif_get_item(exif, key);
1770 if (item) return exif_item_get_data_as_text(item);
1775 const gchar *exif_get_description_by_key(const gchar *key)
1779 if (!key) return NULL;
1782 while (ExifFormattedList[i].key != NULL)
1784 if (strcmp(key, ExifFormattedList[i].key) == 0) return _(ExifFormattedList[i].description);
1789 while (ExifKnownMarkersList[i].tag > 0)
1791 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return _(ExifKnownMarkersList[i].description);
1798 static void exif_write_item(FILE *f, ExifItem *item)
1802 text = exif_item_get_data_as_text(item);
1805 fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1806 exif_item_get_tag_name(item), text);
1811 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1813 if (!f || !exif) return;
1815 fprintf(f, " tag format key value\n");
1816 fprintf(f, "----------------------------------------------------\n");
1818 if (human_readable_list)
1823 while (ExifFormattedList[i].key)
1827 text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1830 fprintf(f, " %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1847 exif_write_item(f, item);
1850 fprintf(f, "----------------------------------------------------\n");
1854 /* not HAVE_EXIV2 */