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.
58 #include <inttypes.h> /* stdint.h is not available on all systems... */
62 #include <sys/types.h>
73 #include "format_raw.h"
74 #include "ui_fileops.h"
78 *-----------------------------------------------------------------------------
80 *-----------------------------------------------------------------------------
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" },
100 /* tags that are special, or need special treatment */
101 #define TAG_EXIFOFFSET 0x8769
105 *-----------------------------------------------------------------------------
107 *-----------------------------------------------------------------------------
110 #define EXIF_TEXT_LIST_END { -1, NULL }
112 static ExifTextList ExifOrientationList[] = {
113 { EXIF_ORIENTATION_UNKNOWN, N_("unknown") },
114 { EXIF_ORIENTATION_TOP_LEFT, N_("top left") },
115 { EXIF_ORIENTATION_TOP_RIGHT, N_("top right") },
116 { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
117 { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
118 { EXIF_ORIENTATION_LEFT_TOP, N_("left top") },
119 { EXIF_ORIENTATION_RIGHT_TOP, N_("right top") },
120 { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
121 { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
125 static ExifTextList ExifUnitList[] = {
126 { EXIF_UNIT_UNKNOWN, N_("unknown") },
127 { EXIF_UNIT_NOUNIT, "" },
128 { EXIF_UNIT_INCH, N_("inch") },
129 { EXIF_UNIT_CENTIMETER, N_("centimeter") },
133 static ExifTextList ExifYCbCrPosList[] = {
139 static ExifTextList ExifMeteringModeList[] = {
140 { 0, N_("unknown") },
141 { 1, N_("average") },
142 { 2, N_("center weighted") },
144 { 4, N_("multi-spot") },
145 { 5, N_("multi-segment") },
146 { 6, N_("partial") },
147 { 255, N_("other") },
151 static ExifTextList ExifExposureProgramList[] = {
152 { 0, N_("not defined") },
155 { 3, N_("aperture") },
156 { 4, N_("shutter") },
157 { 5, N_("creative") },
159 { 7, N_("portrait") },
160 { 8, N_("landscape") },
164 static ExifTextList ExifLightSourceList[] = {
165 { 0, N_("unknown") },
166 { 1, N_("daylight") },
167 { 2, N_("fluorescent") },
168 { 3, N_("tungsten (incandescent)") },
170 { 9, "fine weather" },
171 { 10, "cloudy weather" },
173 { 12, "daylight fluorescent" },
174 { 13, "day white fluorescent" },
175 { 14, "cool white fluorescent" },
176 { 15, "while fluorescent" },
177 { 17, "standard light A" },
178 { 18, "standard light B" },
179 { 19, "standard light C" },
184 { 24, "ISO studio tungsten" },
185 { 255, N_("other") },
189 static ExifTextList ExifFlashList[] = {
192 { 5, N_("yes, not detected by strobe") },
193 { 7, N_("yes, detected by strobe") },
197 static ExifTextList ExifColorSpaceList[] = {
199 { 65535,"uncalibrated" },
203 static ExifTextList ExifSensorList[] = {
204 { 1, "not defined" },
205 { 2, "1 chip color area" },
206 { 2, "2 chip color area" },
207 { 4, "3 chip color area" },
208 { 5, "color sequential area" },
210 { 8, "color sequential linear" },
214 static ExifTextList ExifSourceList[] = {
215 { 3, "digital still camera" },
219 static ExifTextList ExifSceneList[] = {
220 { 1, "direct photo" },
224 static ExifTextList ExifCustRenderList[] = {
230 static ExifTextList ExifExposureModeList[] = {
233 { 2, "auto bracket" },
237 static ExifTextList ExifWhiteBalanceList[] = {
243 static ExifTextList ExifSceneCaptureList[] = {
247 { 3, "night scene" },
251 static ExifTextList ExifGainControlList[] = {
253 { 1, "low gain up" },
254 { 2, "high gain up" },
255 { 3, "low gain down" },
256 { 4, "high gain down" },
260 static ExifTextList ExifContrastList[] = {
267 static ExifTextList ExifSaturationList[] = {
274 static ExifTextList ExifSharpnessList[] = {
281 static ExifTextList ExifSubjectRangeList[] = {
289 ExifMarker ExifKnownMarkersList[] = {
290 { 0x010e, EXIF_FORMAT_STRING, -1, "ImageDescription", N_("Image description"), NULL },
291 { 0x010f, EXIF_FORMAT_STRING, -1, "Make", "Camera make", NULL },
292 { 0x0110, EXIF_FORMAT_STRING, -1, "Model", "Camera model", NULL },
293 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Orientation", N_("Orientation"), ExifOrientationList },
294 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "XResolution", "X resolution", NULL },
295 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "YResolution", "Y Resolution", NULL },
296 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ResolutionUnit", "Resolution units", ExifUnitList },
297 { 0x0131, EXIF_FORMAT_STRING, -1, "Software", "Firmware", NULL },
298 { 0x0132, EXIF_FORMAT_STRING, 20, "DateTime", N_("Date"), NULL },
299 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2, "WhitePoint", "White point", NULL },
300 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "PrimaryChromaticities","Primary chromaticities", NULL },
301 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "YCbCrCoefficients", "YCbCy coefficients", NULL },
302 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1, "YCbCrPositioning", "YCbCr positioning", ExifYCbCrPosList },
303 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "ReferenceBlackWhite", "Black white reference", NULL },
304 { 0x8298, EXIF_FORMAT_STRING, -1, "Copyright", N_("Copyright"), NULL },
305 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifOffset", "SubIFD Exif offset", NULL },
307 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "ExposureTime", "Exposure time (seconds)", NULL },
308 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "FNumber", "FNumber", NULL },
309 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureProgram", N_("Exposure program"), ExifExposureProgramList },
310 { 0x8824, EXIF_FORMAT_STRING, -1, "SpectralSensitivity", "Spectral Sensitivity", NULL },
311 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1, "ISOSpeedRatings", N_("ISO sensitivity"), NULL },
312 { 0x8828, EXIF_FORMAT_UNDEFINED, -1, "OECF", "Optoelectric conversion factor", NULL },
313 { 0x9000, EXIF_FORMAT_UNDEFINED, 4, "ExifVersion", "Exif version", NULL },
314 { 0x9003, EXIF_FORMAT_STRING, 20, "DateTimeOriginal", N_("Date original"), NULL },
315 { 0x9004, EXIF_FORMAT_STRING, 20, "DateTimeDigitized", N_("Date digitized"), NULL },
316 { 0x9101, EXIF_FORMAT_UNDEFINED, -1, "ComponentsConfiguration","Pixel format", NULL },
317 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1, "CompressedBitsPerPixel","Compression ratio", NULL },
318 { 0x9201, EXIF_FORMAT_RATIONAL, 1, "ShutterSpeedValue", N_("Shutter speed"), NULL },
319 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "ApertureValue", N_("Aperture"), NULL },
320 { 0x9203, EXIF_FORMAT_RATIONAL, 1, "BrightnessValue", "Brightness", NULL },
321 { 0x9204, EXIF_FORMAT_RATIONAL, 1, "ExposureBiasValue", N_("Exposure bias"), NULL },
322 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "MaxApertureValue", "Maximum aperture", NULL },
323 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "SubjectDistance", N_("Subject distance"), NULL },
324 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1, "MeteringMode", N_("Metering mode"), ExifMeteringModeList },
325 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1, "LightSource", N_("Light source"), ExifLightSourceList },
326 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Flash", N_("Flash"), ExifFlashList },
327 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "FocalLength", N_("Focal length"), NULL },
328 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1, "SubjectArea", "Subject area", NULL },
329 { 0x927c, EXIF_FORMAT_UNDEFINED, -1, "MakerNote", "MakerNote", NULL },
330 { 0x9286, EXIF_FORMAT_UNDEFINED, -1, "UserComment", "UserComment", NULL },
331 { 0x9290, EXIF_FORMAT_STRING, -1, "SubsecTime", "Subsecond time", NULL },
332 { 0x9291, EXIF_FORMAT_STRING, -1, "SubsecTimeOriginal", "Subsecond time original", NULL },
333 { 0x9292, EXIF_FORMAT_STRING, -1, "SubsecTimeDigitized", "Subsecond time digitized", NULL },
334 { 0xa000, EXIF_FORMAT_UNDEFINED, 4, "FlashPixVersion", "FlashPix version", NULL },
335 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ColorSpace", "Colorspace", ExifColorSpaceList },
336 /* ExifImageWidth, ExifImageHeight can also be unsigned short */
337 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifImageWidth", N_("Width"), NULL },
338 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifImageHeight", N_("Height"), NULL },
339 { 0xa004, EXIF_FORMAT_STRING, -1, "RelatedSoundFile", "Audio data", NULL },
340 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifInteroperabilityOffset", "ExifR98 extension", NULL },
341 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "FlashEnergy", "Flash strength", NULL },
342 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SpatialFrequencyResponse","Spatial frequency response", NULL },
343 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "FocalPlaneXResolution", "X Pixel density", NULL },
344 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "FocalPlaneYResolution", "Y Pixel density", NULL },
345 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1, "FocalPlaneResolutionUnit", "Pixel density units", ExifUnitList },
346 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2, "SubjectLocation", "Subject location", NULL },
347 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "ExposureIndex", N_("ISO sensitivity"), NULL },
348 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1, "SensingMethod", "Sensor type", ExifSensorList },
349 { 0xa300, EXIF_FORMAT_UNDEFINED, 1, "FileSource", "Source type", ExifSourceList },
350 { 0xa301, EXIF_FORMAT_UNDEFINED, 1, "SceneType", "Scene type", ExifSceneList },
351 { 0xa302, EXIF_FORMAT_UNDEFINED, -1, "CFAPattern", "Color filter array pattern", NULL },
352 /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
353 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1, "CustomRendered", "Render process", ExifCustRenderList },
354 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1, "ExposureMode", "Exposure mode", ExifExposureModeList },
355 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1, "WhiteBalance", "White balance", ExifWhiteBalanceList },
356 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "DigitalZoomRatio", "Digital zoom ratio", NULL },
357 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1, "FocalLength35mmFilm", "Focal length (35mm)", NULL },
358 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SceneCapturetype", "Scene capture type", ExifSceneCaptureList },
359 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1, "GainControl", "Gain control", ExifGainControlList },
360 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Contrast", "Contrast", ExifContrastList },
361 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Saturation", "Saturation", ExifSaturationList },
362 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Sharpness", "Sharpness", ExifSharpnessList },
363 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1, "DeviceSettingDescription","Device setting", NULL },
364 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubjectDistanceRange", "Subject range", ExifSubjectRangeList },
365 { 0xa420, EXIF_FORMAT_STRING, -1, "ImageUniqueID", "Image serial number", NULL },
366 /* place known, but undocumented or lesser used tags here */
367 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1, "NewSubfileType", NULL, NULL },
368 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubfileType", NULL, NULL },
369 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3, "TransferFunction", NULL, NULL },
370 { 0x013b, EXIF_FORMAT_STRING, -1, "Artist", "Artist", NULL },
371 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Predictor", NULL, NULL },
372 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileWidth", NULL, NULL },
373 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileLength", NULL, NULL },
374 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1, "TileOffsets", NULL, NULL },
375 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1, "TileByteCounts", NULL, NULL },
376 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1, "SubIFDs", NULL, NULL },
377 { 0x015b, EXIF_FORMAT_UNDEFINED, -1, "JPEGTables", NULL, NULL },
378 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2, "CFARepeatPatternDim", NULL, NULL },
379 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1, "CFAPattern", NULL, NULL },
380 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "BatteryLevel", NULL, NULL },
381 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1, "IPTC/NAA", NULL, NULL },
382 { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "InterColorProfile", NULL, NULL },
383 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1, "GPSInfo", "SubIFD GPS offset", NULL },
384 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Interlace", NULL, NULL },
385 { 0x882a, EXIF_FORMAT_SHORT, 1, "TimeZoneOffset", NULL, NULL },
386 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SelfTimerMode", NULL, NULL },
387 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "FlashEnergy", NULL, NULL },
388 { 0x920c, EXIF_FORMAT_UNDEFINED, -1, "SpatialFrequencyResponse", NULL, NULL },
389 { 0x920d, EXIF_FORMAT_UNDEFINED, -1, "Noise", NULL, NULL },
390 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1, "ImageNumber", NULL, NULL },
391 { 0x9212, EXIF_FORMAT_STRING, 1, "SecurityClassification", NULL, NULL },
392 { 0x9213, EXIF_FORMAT_STRING, -1, "ImageHistory", NULL, NULL },
393 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "ExposureIndex", NULL, NULL },
394 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4, "TIFF/EPStandardID", NULL, NULL },
396 /* end is marked by 0 tag */
397 { 0x0000, EXIF_FORMAT_UNKNOWN, 0, NULL, 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_("Flash") },
428 { "fResolution", N_("Resolution") },
434 *-----------------------------------------------------------------------------
436 *-----------------------------------------------------------------------------
439 #define BYTE_ORDER_INTEL 1
440 #define BYTE_ORDER_MOTOROLA 2
443 #define MARKER_UNKNOWN 0x00
444 #define MARKER_SOI 0xD8
445 #define MARKER_APP1 0xE1
447 /* These data structs are packed to make sure the
448 * byte alignment matches the on-disk data format.
450 typedef struct __attribute__((packed)) {
456 typedef struct __attribute__((packed)) {
464 static ExifMarker *exif_marker_from_tag(uint16_t tag);
465 static int parse_IFD_table(ExifData *exif, unsigned char *tiff, int offset,
466 int size, int byte_order);
469 *-----------------------------------------------------------------------------
471 *-----------------------------------------------------------------------------
474 static ExifItem *exif_item_new(ExifFormatType format, unsigned int tag, unsigned int elements, ExifMarker *marker)
478 item = g_new0(ExifItem, 1);
479 item->format = format;
481 item->marker = marker;
482 item->elements = elements;
488 case EXIF_FORMAT_UNKNOWN:
489 /* unknown, data is NULL */
492 case EXIF_FORMAT_BYTE_UNSIGNED:
493 item->data_len = sizeof(char) * elements;
495 case EXIF_FORMAT_STRING:
496 item->data_len = sizeof(char) * elements;
498 case EXIF_FORMAT_SHORT_UNSIGNED:
499 item->data_len = sizeof(unsigned short int) * elements;
501 case EXIF_FORMAT_LONG_UNSIGNED:
502 item->data_len = sizeof(unsigned long int) * elements;
504 case EXIF_FORMAT_RATIONAL_UNSIGNED:
505 item->data_len = sizeof(ExifRational) * elements;
507 case EXIF_FORMAT_BYTE:
508 item->data_len = sizeof(char) * elements;
510 case EXIF_FORMAT_UNDEFINED:
511 item->data_len = sizeof(char) * elements;
513 case EXIF_FORMAT_SHORT:
514 item->data_len = sizeof(short int) * elements;
516 case EXIF_FORMAT_LONG:
517 item->data_len = sizeof(long int) * elements;
519 case EXIF_FORMAT_RATIONAL:
520 item->data_len = sizeof(ExifRational) * elements;
522 case EXIF_FORMAT_FLOAT:
523 item->data_len = sizeof(float) * elements;
525 case EXIF_FORMAT_DOUBLE:
526 item->data_len = sizeof(double) * elements;
530 item->data = g_malloc0(item->data_len);
535 static void exif_item_free(ExifItem *item)
543 const char *exif_item_get_tag_name(ExifItem *item)
545 if (!item || !item->marker) return NULL;
546 return item->marker->key;
549 const char *exif_item_get_description(ExifItem *item)
551 if (!item || !item->marker) return NULL;
552 return _(item->marker->description);
555 const char *exif_item_get_format_name(ExifItem *item, gint brief)
557 if (!item || !item->marker) return NULL;
558 return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
562 #define UNDEFINED_TEXT_BYTE_COUNT 16
564 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
568 for (i = 0 ; i < ne && i < UNDEFINED_TEXT_BYTE_COUNT; i++)
570 unsigned char c = ((char *)data)[i];
571 if (c < 32 || c > 127) c = '.';
572 g_string_append_printf(string, "%c", c);
574 string = g_string_append(string, " : ");
575 for (i = 0 ; i < ne && i < UNDEFINED_TEXT_BYTE_COUNT; i++)
593 g_string_append_printf(string, "%s%02x", spacer, ((char *)data)[i]);
595 if (i >= UNDEFINED_TEXT_BYTE_COUNT) g_string_append_printf(string, " (%d bytes)", ne);
600 static gchar *text_list_find_value(ExifTextList *list, gint value)
602 gchar *result = NULL;
606 while (!result && list[i].value >= 0)
608 if (value == list[i].value) result = g_strdup(_(list[i].description));
611 if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
617 *-------------------------------------------------------------------
619 *-------------------------------------------------------------------
622 static uint16_t get_int16(unsigned char *f, int bo)
624 if (bo == BYTE_ORDER_INTEL)
625 return *f + (*(f+1)<<8);
627 return ((*f)<<8) + *(f+1);
632 static uint32_t get_int32(unsigned char *f, int bo)
634 if (bo == BYTE_ORDER_INTEL)
635 return get_int16(f, BYTE_ORDER_INTEL) + (get_int16(f+2, BYTE_ORDER_INTEL)<<16);
637 return (get_int16(f, BYTE_ORDER_MOTOROLA)<<16) + get_int16(f+2, BYTE_ORDER_MOTOROLA);
641 static uint16_t swab_int16(uint16_t n, int bo)
643 #if BYTE_ORDER == LITTLE_ENDIAN
644 if (bo == BYTE_ORDER_MOTOROLA)
646 if (bo == BYTE_ORDER_INTEL)
653 static uint32_t swab_int32(uint32_t n, int bo)
655 #if BYTE_ORDER == LITTLE_ENDIAN
656 if (bo == BYTE_ORDER_MOTOROLA)
658 if (bo == BYTE_ORDER_INTEL)
660 return n<<24 | n>>24 | (n & 0xFF0000)>>8 | (n & 0xFF00)<<8;
666 *-------------------------------------------------------------------
668 *-------------------------------------------------------------------
671 static int get_marker_size(unsigned char *f)
673 /* Size is always in Motorola byte order */
674 return get_int16(f+2, BYTE_ORDER_MOTOROLA);
677 static int goto_next_marker(unsigned char **f, int *size, int *marker)
681 *marker = MARKER_UNKNOWN;
683 /* It is safe to access the marker and its size since we have checked
684 * the SOI and this function guaranties the whole next marker is
687 if (*(*f+1) != MARKER_SOI)
689 marker_size += get_marker_size(*f);
692 *size -= marker_size;
694 /* size should be at least 4, so we can read the marker and its size
695 * and check data are actually available
697 if (*size < 4) return -1;
699 /* Jump to the next marker and be sure it begins with 0xFF
702 if (**f != 0xFF) return -1;
704 if (get_marker_size(*f)+2 > *size) return -1;
712 *-------------------------------------------------------------------
714 *-------------------------------------------------------------------
717 static ExifMarker *exif_marker_from_tag(uint16_t tag)
719 static int len = sizeof(ExifKnownMarkersList)/sizeof(ExifMarker) - 1;
722 while (i < len && ExifKnownMarkersList[i].tag != tag)
727 return (i >= len ? NULL : &ExifKnownMarkersList[i]);
730 static void rational_from_data(ExifRational *r, void *src, int byte_order)
732 r->num = swab_int32(*(uint32_t*)src, byte_order);
733 r->den = swab_int32(*(uint32_t*)(src + sizeof(uint32_t)), byte_order);
736 static void exif_item_copy_data(ExifItem *item, void *src, int len, ExifFormatType src_format, int byte_order)
743 bs = ExifFormatList[item->format].size;
747 if (!dest || len > item->data_len)
749 printf("exif tag %s data size mismatch\n", exif_item_get_tag_name(item));
753 switch (item->format)
755 case EXIF_FORMAT_UNKNOWN:
757 case EXIF_FORMAT_BYTE_UNSIGNED:
758 case EXIF_FORMAT_BYTE:
759 case EXIF_FORMAT_UNDEFINED:
760 memcpy(dest, src, len);
762 case EXIF_FORMAT_STRING:
763 memcpy(dest, src, len);
764 /* string is NULL terminated, make sure this is true */
765 if (((char *)dest)[len - 1] != '\0') ((char *)dest)[len - 1] = '\0';
767 case EXIF_FORMAT_SHORT_UNSIGNED:
768 case EXIF_FORMAT_SHORT:
769 for (i = 0; i < ne; i++)
771 ((short *)dest)[i] = swab_int16(*(uint16_t*)(src + i * bs), byte_order);
774 case EXIF_FORMAT_LONG_UNSIGNED:
775 case EXIF_FORMAT_LONG:
776 if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
777 src_format == EXIF_FORMAT_SHORT)
779 /* a short fits into a long, so allow it */
782 ss = ExifFormatList[src_format].size;
783 for (i = 0; i < ne; i++)
785 ((long *)dest)[i] = (long)swab_int16(*(uint16_t*)(src + i * ss), byte_order);
790 for (i = 0; i < ne; i++)
792 ((long *)dest)[i] = swab_int32(*(uint32_t*)(src + i * bs), byte_order);
796 case EXIF_FORMAT_RATIONAL_UNSIGNED:
797 case EXIF_FORMAT_RATIONAL:
798 for (i = 0; i < ne; i++)
800 rational_from_data(&((ExifRational *)dest)[i], src + i * bs, byte_order);
803 case EXIF_FORMAT_FLOAT:
804 for (i = 0; i < ne; i++)
806 ((float *)dest)[i] = swab_int32(*(uint32_t*)(src + i * bs), byte_order);
809 case EXIF_FORMAT_DOUBLE:
810 for (i = 0; i < ne; i++)
814 rational_from_data(&r, src + i * bs, byte_order);
815 if (r.den) ((double *)dest)[i] = (double)r.num / r.den;
821 static int parse_IFD_entry(ExifData *exif, unsigned char *tiff, int offset,
822 int size, int byte_order)
824 IFDEntry *ent = (IFDEntry*)(tiff+offset);
825 uint32_t swabed_data;
831 ent->tag = swab_int16(ent->tag, byte_order);
832 ent->format = swab_int16(ent->format, byte_order);
833 ent->nb = swab_int32(ent->nb, byte_order);
834 swabed_data = swab_int32(ent->data, byte_order);
836 /* Check tag type. If it does not match, either the format is wrong,
837 * either it is a unknown tag; so it is not really an error.
839 marker = exif_marker_from_tag(ent->tag);
842 if (ent->format > EXIF_FORMAT_DOUBLE)
844 printf("warning: exif tag 0x%4x has invalid format %d\n", ent->tag, ent->format);
847 /* allow non recognized tags to be displayed */
848 marker = &ExifUnknownMarkersList[ent->format];
850 if (marker->format != ent->format)
852 /* Some cameras got mixed up signed/unsigned_rational
853 * eg KODAK DC4800 on object_distance tag
855 * FIXME: what exactly is this test trying to do?
856 * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
858 if ( !(marker->format == EXIF_FORMAT_RATIONAL_UNSIGNED && ent->format == EXIF_FORMAT_RATIONAL) &&
859 !(marker->format == EXIF_FORMAT_RATIONAL && ent->format == EXIF_FORMAT_RATIONAL_UNSIGNED) &&
860 /* short fits into a long so allow this mismatch
861 * as well (some tags allowed to be unsigned short _or_ unsigned long)
863 !(marker->format == EXIF_FORMAT_LONG_UNSIGNED && ent->format == EXIF_FORMAT_SHORT_UNSIGNED) )
865 if (ent->format <= EXIF_FORMAT_DOUBLE)
867 printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
868 marker->key, ExifFormatList[ent->format].short_name, ExifFormatList[marker->format].short_name);
872 printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
873 marker->key, ent->format, marker->format, ExifFormatList[marker->format].short_name);
879 /* Where is the data, is it available?
881 if (marker->components > 0 && marker->components != ent->nb)
883 printf("warning: exif tag %s has %d elements, exif spec requests %d\n", marker->key, ent->nb, marker->components);
885 data_len = ExifFormatList[marker->format].size * ent->nb;
886 if (data_len > sizeof(ent->data))
888 if (size < swabed_data+data_len)
890 printf("warning: exif tag %s will overrun IFD segment, ignored.\n", marker->key);
893 data = (void*)tiff + swabed_data;
897 data = (void*)(&(ent->data));
900 item = exif_item_new(marker->format, ent->tag, ent->nb, marker);
901 exif_item_copy_data(item, data, data_len, ent->format, byte_order);
902 exif->items = g_list_prepend(exif->items, item);
904 if (item->tag == TAG_EXIFOFFSET)
906 parse_IFD_table(exif, tiff, swabed_data, size, byte_order);
912 static int parse_IFD_table(ExifData *exif, unsigned char *tiff, int offset,
913 int size, int byte_order)
917 /* We should be able to read number of entries in IFD0) */
918 if (size < offset+2) return -1;
920 nb_entries = get_int16(tiff+offset, byte_order);
922 /* Entries and next IFD offset must be readable */
923 if (size < offset+nb_entries*12+4) return -1;
925 for (i=0; i<nb_entries; ++i)
927 parse_IFD_entry(exif, tiff, offset+2+i*sizeof(IFDEntry), size, byte_order);
934 *-------------------------------------------------------------------
936 *-------------------------------------------------------------------
939 static int parse_TIFF(ExifData *exif, unsigned char *tiff, int size)
941 int byte_order, offset=0;
943 if (size < sizeof(TIFFHeader))
948 if (strncmp(((TIFFHeader*)tiff)->byte_order, "II", 2) == 0)
950 byte_order = BYTE_ORDER_INTEL;
952 else if (strncmp(((TIFFHeader*)tiff)->byte_order, "MM", 2) == 0)
954 byte_order = BYTE_ORDER_MOTOROLA;
961 if (swab_int16(((TIFFHeader*)tiff)->magic, byte_order) != 0x002A)
966 offset = swab_int32(((TIFFHeader*)tiff)->IFD_offset, byte_order);
968 return parse_IFD_table(exif, tiff, offset, size, byte_order);
971 static int parse_JPEG(ExifData *exif, unsigned char *f, int size)
973 int marker, marker_size;
975 if (size<2 || *f!=0xFF || *(f+1)!=MARKER_SOI)
981 if (goto_next_marker(&f, &size, &marker) == -1)
985 } while (marker != MARKER_APP1);
987 if (marker != MARKER_APP1)
992 marker_size = get_marker_size(f)-2;
994 if (marker_size<6 || strncmp((char*)f+4, "Exif\0\0", 6)!=0)
999 return parse_TIFF(exif, f+10, marker_size-6);
1002 static gint map_file(const gchar *path, void **mapping, int *size)
1007 if ((fd = open(path, O_RDONLY)) == -1)
1013 if (fstat(fd, &fs) == -1)
1022 if ((*mapping = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
1033 static gint unmap_file(void *mapping, int size)
1035 if (munmap(mapping, size) == -1)
1044 void exif_free(ExifData *exif)
1053 ExifItem *item = work->data;
1055 exif_item_free(item);
1058 g_list_free(exif->items);
1062 ExifData *exif_read(const gchar *path)
1069 if (!path) return NULL;
1071 pathl = path_from_utf8(path);
1072 if (map_file(pathl, &f, &size) == -1)
1079 exif = g_new0(ExifData, 1);
1082 if ((res = parse_JPEG(exif, (unsigned char *)f, size)) == -2)
1084 res = parse_TIFF(exif, (unsigned char *)f, size);
1091 if (format_raw_img_exif_offsets(-1, f, size, NULL, &offset))
1093 res = parse_TIFF(exif, (unsigned char*)f + offset, size - offset);
1103 unmap_file(f, size);
1105 if (exif) exif->items = g_list_reverse(exif->items);
1108 exif_write_data_list(exif, stdout, TRUE);
1109 exif_write_data_list(exif, stdout, FALSE);
1115 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1119 if (!key) return NULL;
1128 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1133 gchar *exif_item_get_data_as_text(ExifItem *item)
1142 if (!item) return NULL;
1144 marker = item->marker;
1145 if (!marker) return NULL;
1148 ne = item->elements;
1149 string = g_string_new("");
1150 switch (item->format)
1152 case EXIF_FORMAT_UNKNOWN:
1154 case EXIF_FORMAT_BYTE_UNSIGNED:
1155 case EXIF_FORMAT_BYTE:
1156 case EXIF_FORMAT_UNDEFINED:
1157 if (ne == 1 && marker->list)
1162 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1163 item->format == EXIF_FORMAT_UNDEFINED)
1165 val = ((unsigned char *)data)[0];
1169 val = (unsigned char)(((signed char *)data)[0]);
1172 result = text_list_find_value(marker->list, (unsigned short)val);
1173 string = g_string_append(string, result);
1178 string = string_append_raw_bytes(string, data, ne);
1181 case EXIF_FORMAT_STRING:
1182 string = g_string_append(string, (gchar *)(item->data));
1184 case EXIF_FORMAT_SHORT_UNSIGNED:
1185 if (ne == 1 && marker->list)
1189 result = text_list_find_value(marker->list, ((unsigned short *)data)[0]);
1190 string = g_string_append(string, result);
1193 else for (i = 0; i < ne; i++)
1195 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1196 ((unsigned short *)data)[i]);
1199 case EXIF_FORMAT_LONG_UNSIGNED:
1200 for (i = 0; i < ne; i++)
1202 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1203 ((unsigned long *)data)[i]);
1206 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1207 for (i = 0; i < ne; i++)
1211 r = &((ExifRational *)data)[i];
1212 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1213 (unsigned long)r->num, (unsigned long)r->den);
1216 case EXIF_FORMAT_SHORT:
1217 for (i = 0; i < ne; i++)
1219 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1220 ((short *)data)[i]);
1223 case EXIF_FORMAT_LONG:
1224 for (i = 0; i < ne; i++)
1226 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1230 case EXIF_FORMAT_RATIONAL:
1231 for (i = 0; i < ne; i++)
1235 r = &((ExifRational *)data)[i];
1236 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1237 (long)r->num, (long)r->den);
1240 case EXIF_FORMAT_FLOAT:
1241 for (i = 0; i < ne; i++)
1243 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1244 ((float *)data)[i]);
1247 case EXIF_FORMAT_DOUBLE:
1248 for (i = 0; i < ne; i++)
1250 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1251 ((double *)data)[i]);
1256 text = g_strdup(string->str);
1257 g_string_free(string, TRUE);
1262 gint exif_item_get_integer(ExifItem *item, gint *value)
1264 if (!item) return FALSE;
1266 switch (item->format)
1268 case EXIF_FORMAT_SHORT:
1269 *value = (gint)(((short *)(item->data))[0]);
1272 case EXIF_FORMAT_SHORT_UNSIGNED:
1273 *value = (gint)(((unsigned short *)(item->data))[0]);
1276 case EXIF_FORMAT_LONG:
1277 *value = (gint)(((long *)(item->data))[0]);
1280 case EXIF_FORMAT_LONG_UNSIGNED:
1281 /* FIXME: overflow possible */
1282 *value = (gint)(((unsigned long *)(item->data))[0]);
1285 /* all other type return FALSE */
1291 gint exif_get_integer(ExifData *exif, const gchar *key, gint *value)
1295 item = exif_get_item(exif, key);
1296 return exif_item_get_integer(item, value);
1299 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign)
1301 if (!item) return NULL;
1303 if (item->format == EXIF_FORMAT_RATIONAL ||
1304 item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1306 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1307 return &((ExifRational *)(item->data))[0];
1313 ExifRational *exif_get_rational(ExifData *exif, const gchar *key, gint *sign)
1317 item = exif_get_item(exif, key);
1318 return exif_item_get_rational(item, sign);
1321 double exif_rational_to_double(ExifRational *r, gint sign)
1323 if (!r || r->den == 0.0) return 0.0;
1325 if (sign) return (double)((int)r->num) / (double)((int)r->den);
1326 return (double)r->num / r->den;
1329 static double exif_get_rational_as_double(ExifData *exif, const gchar *key)
1334 r = exif_get_rational(exif, key, &sign);
1335 return exif_rational_to_double(r, sign);
1338 static GString *append_comma_text(GString *string, const gchar *text)
1340 string = g_string_append(string, ", ");
1341 string = g_string_append(string, text);
1346 static gchar *exif_get_formatted_by_key(ExifData *exif, const gchar *key, gint *key_valid)
1348 /* must begin with f, else not formatted */
1351 if (key_valid) *key_valid = FALSE;
1355 if (key_valid) *key_valid = TRUE;
1357 if (strcmp(key, "fCamera") == 0)
1360 gchar *make = exif_get_data_as_text(exif, "Make");
1361 gchar *model = exif_get_data_as_text(exif, "Model");
1362 gchar *software = exif_get_data_as_text(exif, "Software");
1364 text = g_strdup_printf("%s%s%s%s%s%s", (make) ? make : "", ((make) && (model)) ? " " : "",
1365 (model) ? model : "",
1366 (software) ? " (" : "",
1367 (software) ? software : "",
1368 (software) ? ")" : "");
1375 if (strcmp(key, "fDateTime") == 0)
1377 gchar *text = exif_get_data_as_text(exif, "DateTimeOriginal");
1378 gchar *subsec = NULL;
1379 if (text) subsec = exif_get_data_as_text(exif, "SubsecTimeOriginal");
1382 text = exif_get_data_as_text(exif, "DateTime");
1383 if (text) subsec = exif_get_data_as_text(exif, "SubsecTime");
1388 text = g_strconcat(tmp, ".", subsec, NULL);
1394 if (strcmp(key, "fShutterSpeed") == 0)
1398 r = exif_get_rational(exif, "ExposureTime", NULL);
1399 if (r && r->num && r->den)
1401 double n = (double)r->den / (double)r->num;
1402 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
1403 n > 1.0 ? n : 1.0 / n);
1405 r = exif_get_rational(exif, "ShutterSpeedValue", NULL);
1406 if (r && r->num && r->den)
1408 double n = pow(2.0, exif_rational_to_double(r, TRUE));
1410 /* Correct exposure time to avoid values like 1/91s (seen on Minolta DImage 7) */
1411 if (n > 1.0 && (int)n - ((int)(n/10))*10 == 1) n--;
1413 return g_strdup_printf("%s%.0fs", n > 1.0 ? "1/" : "",
1414 n > 1.0 ? floor(n) : 1.0 / n);
1418 if (strcmp(key, "fAperture") == 0)
1422 n = exif_get_rational_as_double(exif, "FNumber");
1423 if (n == 0.0) n = exif_get_rational_as_double(exif, "ApertureValue");
1424 if (n == 0.0) return NULL;
1426 return g_strdup_printf("f/%.1f", n);
1428 if (strcmp(key, "fExposureBias") == 0)
1434 r = exif_get_rational(exif, "ExposureBiasValue", &sign);
1435 if (!r) return NULL;
1437 n = exif_rational_to_double(r, sign);
1438 return g_strdup_printf("%+.1f", n);
1440 if (strcmp(key, "fFocalLength") == 0)
1444 n = exif_get_rational_as_double(exif, "FocalLength");
1445 if (n == 0.0) return NULL;
1446 return g_strdup_printf("%.2f mm", n);
1448 if (strcmp(key, "fISOSpeedRating") == 0)
1452 text = exif_get_data_as_text(exif, "ISOSpeedRatings");
1453 /* kodak may set this instead */
1454 if (!text) text = exif_get_data_as_text(exif, "ExposureIndex");
1457 if (strcmp(key, "fSubjectDistance") == 0)
1463 r = exif_get_rational(exif, "SubjectDistance", &sign);
1464 if (!r) return NULL;
1466 if ((long)r->num == 0xffffffff) return g_strdup(_("infinity"));
1467 if ((long)r->num == 0) return g_strdup(_("unknown"));
1469 n = exif_rational_to_double(r, sign);
1470 if (n == 0.0) return _("unknown");
1471 return g_strdup_printf("%.3f m", n);
1473 if (strcmp(key, "fFlash") == 0)
1475 /* grr, flash is a bitmask... */
1481 if (!exif_get_integer(exif, "Flash", &n)) return NULL;
1483 /* Exif 2.1 only defines first 3 bits */
1484 if (n <= 0x07) return g_strdup(text_list_find_value(ExifFlashList, n));
1486 /* must be Exif 2.2 */
1487 string = g_string_new("");
1489 /* flash fired (bit 0) */
1490 string = g_string_append(string, (n & 0x01) ? _("yes") : _("no"));
1492 /* flash mode (bits 3, 4) */
1493 v = (n >> 3) & 0x03;
1494 if (v) string = append_comma_text(string, _("mode:"));
1498 string = g_string_append(string, _("on"));
1501 string = g_string_append(string, _("off"));
1504 string = g_string_append(string, _("auto"));
1508 /* return light (bits 1, 2) */
1509 v = (n >> 1) & 0x03;
1510 if (v == 2) string = append_comma_text(string, _("not detected by strobe"));
1511 if (v == 3) string = append_comma_text(string, _("detected by strobe"));
1513 /* we ignore flash function (bit 5) */
1515 /* red-eye (bit 6) */
1516 if ((n >> 5) & 0x01) string = append_comma_text(string, _("red-eye reduction"));
1519 g_string_free(string, FALSE);
1522 if (strcmp(key, "fResolution") == 0)
1524 ExifRational *rx, *ry;
1528 rx = exif_get_rational(exif, "XResolution", NULL);
1529 ry = exif_get_rational(exif, "YResolution", NULL);
1530 if (!rx || !ry) return NULL;
1532 units = exif_get_data_as_text(exif, "ResolutionUnit");
1533 text = g_strdup_printf("%0.f x %0.f (%s/%s)", rx->den ? (double)rx->num / rx->den : 1.0,
1534 ry->den ? (double)ry->num / ry->den : 1.0,
1535 _("dot"), (units) ? units : _("unknown"));
1541 if (key_valid) *key_valid = FALSE;
1545 gchar *exif_get_data_as_text(ExifData *exif, const gchar *key)
1551 if (!key) return NULL;
1553 text = exif_get_formatted_by_key(exif, key, &key_valid);
1554 if (key_valid) return text;
1556 item = exif_get_item(exif, key);
1557 if (item) return exif_item_get_data_as_text(item);
1562 const gchar *exif_get_description_by_key(const gchar *key)
1566 if (!key) return NULL;
1569 while (ExifFormattedList[i].key != NULL)
1571 if (strcmp(key, ExifFormattedList[i].key) == 0) return _(ExifFormattedList[i].description);
1576 while (ExifKnownMarkersList[i].tag > 0)
1578 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return _(ExifKnownMarkersList[i].description);
1585 static void exif_write_item(FILE *f, ExifItem *item)
1589 text = exif_item_get_data_as_text(item);
1592 fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1593 exif_item_get_tag_name(item), text);
1598 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1600 if (!f || !exif) return;
1602 fprintf(f, " tag format key value\n");
1603 fprintf(f, "----------------------------------------------------\n");
1605 if (human_readable_list)
1610 while (ExifFormattedList[i].key)
1614 text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1617 fprintf(f, " %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1634 exif_write_item(f, item);
1637 fprintf(f, "----------------------------------------------------\n");