4 * Copyright (C) 2008 The Geeqie Team
7 * Support for Exif file format, originally written by Eric Swalens.
8 * Modified by Quy Tonthat
10 * Reimplemented with generic data storage by John Ellis (Nov 2003)
12 * The tags were added with information from the FREE document:
13 * http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
15 * For the official Exif Format, please refer to:
17 * http://www.exif.org/specifications.html (PDF spec sheets)
20 * Additional tag formats should be added to the proper
21 * location in ExifKnownMarkersList[].
23 * Human readable ouput (that needs additional processing of data to
24 * be useable) can be defined by adding a key to ExifFormattedList[],
25 * then handling that tag in the function exif_get_formatted_by_key().
26 * The human readable formatted keys must begin with the character 'f'.
28 * 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?)
36 * Add support for marker tag 0x0000
39 This program is free software; you can redistribute it and/or modify
40 it under the terms of the GNU General Public License as published by
41 the Free Software Foundation; either version 2 of the License, or
42 (at your option) any later version.
44 This program is distributed in the hope that it will be useful,
45 but WITHOUT ANY WARRANTY; without even the implied warranty of
46 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 GNU General Public License for more details.
49 You should have received a copy of the GNU General Public License
50 along with this program; if not, write to the Free Software
51 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
64 #include <sys/types.h>
76 #include "format_raw.h"
77 #include "ui_fileops.h"
81 *-----------------------------------------------------------------------------
83 *-----------------------------------------------------------------------------
86 ExifFormatAttrib ExifFormatList[] = {
87 { EXIF_FORMAT_UNKNOWN, 1, "unknown", "unknown" },
88 { EXIF_FORMAT_BYTE_UNSIGNED, 1, "ubyte", "unsigned byte" },
89 { EXIF_FORMAT_STRING, 1, "string", "string" },
90 { EXIF_FORMAT_SHORT_UNSIGNED, 2, "ushort", "unsigned short" },
91 { EXIF_FORMAT_LONG_UNSIGNED, 4, "ulong", "unsigned long" },
92 { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
93 { EXIF_FORMAT_BYTE, 1, "byte", "byte" },
94 { EXIF_FORMAT_UNDEFINED, 1, "undefined", "undefined" },
95 { EXIF_FORMAT_SHORT, 2, "sshort", "signed short" },
96 { EXIF_FORMAT_LONG, 4, "slong", "signed long" },
97 { EXIF_FORMAT_RATIONAL, 8, "srational", "signed rational" },
98 { EXIF_FORMAT_FLOAT, 4, "float", "float" },
99 { EXIF_FORMAT_DOUBLE, 8, "double", "double" },
100 { -1, 0, NULL, NULL }
103 /* tags that are special, or need special treatment */
104 #define TAG_EXIFOFFSET 0x8769
105 #define TAG_EXIFMAKERNOTE 0x927c
106 #define TAG_GPSOFFSET 0x8825
110 *-----------------------------------------------------------------------------
112 *-----------------------------------------------------------------------------
114 static ExifTextList ExifCompressionList[] = {
115 { 1, "Uncompressed" },
117 { 3, "T4/Group 3 Fax" },
118 { 4, "T6/Group 4 Fax" },
120 { 6, "JPEG (old style)" },
122 { 8, "Adobe Deflate" },
124 { 10, "JBIG Color" },
126 { 32771, "CCIRLEW" },
127 { 32773, "PackBits" },
128 { 32809, "ThunderScan" },
129 { 32895, "IT8CTPAD" },
133 { 32908, "PixasFilm" },
134 { 32909, "PixasLog" },
135 { 32946, "Deflate" },
139 { 34677, "SGILog24" },
140 { 34712, "JPEF 2000" },
141 { 34713, "Nikon NEF Compressed" },
145 static ExifTextList ExifOrientationList[] = {
146 { EXIF_ORIENTATION_UNKNOWN, N_("unknown") },
147 { EXIF_ORIENTATION_TOP_LEFT, N_("top left") },
148 { EXIF_ORIENTATION_TOP_RIGHT, N_("top right") },
149 { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
150 { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
151 { EXIF_ORIENTATION_LEFT_TOP, N_("left top") },
152 { EXIF_ORIENTATION_RIGHT_TOP, N_("right top") },
153 { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
154 { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
158 static ExifTextList ExifUnitList[] = {
159 { EXIF_UNIT_UNKNOWN, N_("unknown") },
160 { EXIF_UNIT_NOUNIT, "" },
161 { EXIF_UNIT_INCH, N_("inch") },
162 { EXIF_UNIT_CENTIMETER, N_("centimeter") },
166 static ExifTextList ExifYCbCrPosList[] = {
172 static ExifTextList ExifMeteringModeList[] = {
173 { 0, N_("unknown") },
174 { 1, N_("average") },
175 { 2, N_("center weighted") },
177 { 4, N_("multi-spot") },
178 { 5, N_("multi-segment") },
179 { 6, N_("partial") },
180 { 255, N_("other") },
184 static ExifTextList ExifExposureProgramList[] = {
185 { 0, N_("not defined") },
188 { 3, N_("aperture") },
189 { 4, N_("shutter") },
190 { 5, N_("creative") },
192 { 7, N_("portrait") },
193 { 8, N_("landscape") },
197 static ExifTextList ExifLightSourceList[] = {
198 { 0, N_("unknown") },
199 { 1, N_("daylight") },
200 { 2, N_("fluorescent") },
201 { 3, N_("tungsten (incandescent)") },
203 { 9, N_("fine weather") },
204 { 10, N_("cloudy weather") },
206 { 12, N_("daylight fluorescent") },
207 { 13, N_("day white fluorescent") },
208 { 14, N_("cool white fluorescent") },
209 { 15, N_("white fluorescent") },
210 { 17, N_("standard light A") },
211 { 18, N_("standard light B") },
212 { 19, N_("standard light C") },
217 { 24, N_("ISO studio tungsten") },
218 { 255, N_("other") },
222 static ExifTextList ExifFlashList[] = {
225 { 5, N_("yes, not detected by strobe") },
226 { 7, N_("yes, detected by strobe") },
230 static ExifTextList ExifColorSpaceList[] = {
232 { 65535,N_("uncalibrated") },
236 static ExifTextList ExifSensorList[] = {
237 { 1, N_("not defined") },
238 { 2, N_("1 chip color area") },
239 { 2, N_("2 chip color area") },
240 { 4, N_("3 chip color area") },
241 { 5, N_("color sequential area") },
242 { 7, N_("trilinear") },
243 { 8, N_("color sequential linear") },
247 static ExifTextList ExifSourceList[] = {
248 { 3, N_("digital still camera") },
252 static ExifTextList ExifSceneList[] = {
253 { 1, N_("direct photo") },
257 static ExifTextList ExifCustRenderList[] = {
263 static ExifTextList ExifExposureModeList[] = {
266 { 2, N_("auto bracket") },
270 static ExifTextList ExifWhiteBalanceList[] = {
276 static ExifTextList ExifSceneCaptureList[] = {
277 { 0, N_("standard") },
278 { 1, N_("landscape") },
279 { 2, N_("portrait") },
280 { 3, N_("night scene") },
284 static ExifTextList ExifGainControlList[] = {
286 { 1, N_("low gain up") },
287 { 2, N_("high gain up") },
288 { 3, N_("low gain down") },
289 { 4, N_("high gain down") },
293 static ExifTextList ExifContrastList[] = {
300 static ExifTextList ExifSaturationList[] = {
307 static ExifTextList ExifSharpnessList[] = {
314 static ExifTextList ExifSubjectRangeList[] = {
315 { 0, N_("unknown") },
318 { 3, N_("distant") },
323 Tag names should match to exiv2 keys, http://www.exiv2.org/metadata.html
324 Tags that don't match are not supported by exiv2 and should not be used anywhere in the code
327 ExifMarker ExifKnownMarkersList[] = {
328 { 0x0100, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageWidth", N_("Image Width"), NULL },
329 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageLength", N_("Image Height"), NULL },
330 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.BitsPerSample", N_("Bits per Sample/Pixel"), NULL },
331 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Compression", N_("Compression"), ExifCompressionList },
332 { 0x010e, EXIF_FORMAT_STRING, -1, "Exif.Image.ImageDescription", N_("Image description"), NULL },
333 { 0x010f, EXIF_FORMAT_STRING, -1, "Exif.Image.Make", N_("Camera make"), NULL },
334 { 0x0110, EXIF_FORMAT_STRING, -1, "Exif.Image.Model", N_("Camera model"), NULL },
335 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Orientation", N_("Orientation"), ExifOrientationList },
336 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.XResolution", N_("X resolution"), NULL },
337 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.YResolution", N_("Y Resolution"), NULL },
338 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.ResolutionUnit", N_("Resolution units"), ExifUnitList },
339 { 0x0131, EXIF_FORMAT_STRING, -1, "Exif.Image.Software", N_("Firmware"), NULL },
340 { 0x0132, EXIF_FORMAT_STRING, 20, "Exif.Image.DateTime", N_("Date"), NULL },
341 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2, "Exif.Image.WhitePoint", N_("White point"), NULL },
342 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), NULL },
343 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), NULL },
344 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.YCbCrPositioning", N_("YCbCr positioning"), ExifYCbCrPosList },
345 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.ReferenceBlackWhite",N_("Black white reference"), NULL },
346 { 0x8298, EXIF_FORMAT_STRING, -1, "Exif.Image.Copyright", N_("Copyright"), NULL },
347 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ExifTag", N_("SubIFD Exif offset"), NULL },
349 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureTime", N_("Exposure time (seconds)"), NULL },
350 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FNumber", N_("FNumber"), NULL },
351 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureProgram", N_("Exposure program"), ExifExposureProgramList },
352 { 0x8824, EXIF_FORMAT_STRING, -1, "Exif.Photo.SpectralSensitivity",N_("Spectral Sensitivity"), NULL },
353 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.ISOSpeedRatings", N_("ISO sensitivity"), NULL },
354 { 0x8828, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.OECF", N_("Optoelectric conversion factor"), NULL },
355 { 0x9000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.ExifVersion", N_("Exif version"), NULL },
356 { 0x9003, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeOriginal", N_("Date original"), NULL },
357 { 0x9004, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeDigitized", N_("Date digitized"), NULL },
358 { 0x9101, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), NULL },
359 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1, "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), NULL },
360 { 0x9201, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), NULL },
361 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ApertureValue", N_("Aperture"), NULL },
362 { 0x9203, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.BrightnessValue", N_("Brightness"), NULL },
363 { 0x9204, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), NULL },
364 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.MaxApertureValue", N_("Maximum aperture"), NULL },
365 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.SubjectDistance", N_("Subject distance"), NULL },
366 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.MeteringMode", N_("Metering mode"), ExifMeteringModeList },
367 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.LightSource", N_("Light source"), ExifLightSourceList },
368 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Flash", N_("Flash"), ExifFlashList },
369 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalLength", N_("Focal length"), NULL },
370 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SubjectArea", N_("Subject area"), NULL },
371 { 0x927c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.MakerNote", N_("MakerNote"), NULL },
372 { 0x9286, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.UserComment", N_("UserComment"), NULL },
373 { 0x9290, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTime", N_("Subsecond time"), NULL },
374 { 0x9291, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), NULL },
375 { 0x9292, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), NULL },
376 { 0xa000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.FlashpixVersion", N_("FlashPix version"), NULL },
377 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ColorSpace", N_("Colorspace"), ExifColorSpaceList },
378 /* ExifImageWidth, ExifImageHeight can also be unsigned short */
379 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelXDimension", N_("Width"), NULL },
380 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelYDimension", N_("Height"), NULL },
381 { 0xa004, EXIF_FORMAT_STRING, -1, "Exif.Photo.RelatedSoundFile", N_("Audio data"), NULL },
382 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifInteroperabilityOffset", N_("ExifR98 extension"), NULL },
383 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", N_("Flash strength"), NULL },
384 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), NULL },
385 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), NULL },
386 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), NULL },
387 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalPlaneResolutionUnit", N_("Pixel density units"), ExifUnitList },
388 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Photo.SubjectLocation", N_("Subject location"), NULL },
389 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", N_("ISO sensitivity"), NULL },
390 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SensingMethod", N_("Sensor type"), ExifSensorList },
391 { 0xa300, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.FileSource", N_("Source type"), ExifSourceList },
392 { 0xa301, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.SceneType", N_("Scene type"), ExifSceneList },
393 { 0xa302, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.CFAPattern", N_("Color filter array pattern"), NULL },
394 /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
395 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.CustomRendered", N_("Render process"), ExifCustRenderList },
396 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureMode", N_("Exposure mode"), ExifExposureModeList },
397 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.WhiteBalance", N_("White balance"), ExifWhiteBalanceList },
398 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.DigitalZoomRatio", N_("Digital zoom ratio"), NULL },
399 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), NULL },
400 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SceneCaptureType", N_("Scene capture type"), ExifSceneCaptureList },
401 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.GainControl", N_("Gain control"), ExifGainControlList },
402 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Contrast", N_("Contrast"), ExifContrastList },
403 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Saturation", N_("Saturation"), ExifSaturationList },
404 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Sharpness", N_("Sharpness"), ExifSharpnessList },
405 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.DeviceSettingDescription",N_("Device setting"), NULL },
406 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SubjectDistanceRange",N_("Subject range"), ExifSubjectRangeList },
407 { 0xa420, EXIF_FORMAT_STRING, -1, "Exif.Photo.ImageUniqueID", N_("Image serial number"), NULL },
408 /* place known, but undocumented or lesser used tags here */
409 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.NewSubfileType", NULL, NULL },
410 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubfileType", NULL, NULL },
411 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3, "Exif.Image.TransferFunction", NULL, NULL },
412 { 0x013b, EXIF_FORMAT_STRING, -1, "Exif.Image.Artist", "Artist", NULL },
413 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Predictor", NULL, NULL },
414 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileWidth", NULL, NULL },
415 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileLength", NULL, NULL },
416 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1, "TileOffsets", NULL, NULL },
417 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1, "TileByteCounts", NULL, NULL },
418 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1, "Exif.Image.SubIFDs", NULL, NULL },
419 { 0x015b, EXIF_FORMAT_UNDEFINED, -1, "JPEGTables", NULL, NULL },
420 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Image.CFARepeatPatternDim", NULL, NULL },
421 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Exif.Image.CFAPattern", NULL, NULL },
422 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.BatteryLevel", NULL, NULL },
423 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1, "IPTC/NAA", NULL, NULL },
424 { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL },
425 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1, "GPSInfo", "SubIFD GPS offset", NULL },
426 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Interlace", NULL, NULL },
427 { 0x882a, EXIF_FORMAT_SHORT, 1, "TimeZoneOffset", NULL, NULL },
428 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SelfTimerMode", NULL, NULL },
429 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", NULL, NULL },
430 { 0x920c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.SpatialFrequencyResponse", NULL, NULL },
431 { 0x920d, EXIF_FORMAT_UNDEFINED, -1, "Noise", NULL, NULL },
432 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1, "ImageNumber", NULL, NULL },
433 { 0x9212, EXIF_FORMAT_STRING, 1, "SecurityClassification", NULL, NULL },
434 { 0x9213, EXIF_FORMAT_STRING, -1, "ImageHistory", NULL, NULL },
435 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", NULL, NULL },
436 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4, "TIFF/EPStandardID", NULL, NULL },
441 ExifMarker ExifKnownGPSInfoMarkersList[] = {
442 /* The following do not work at the moment as the tag value 0x0000 has a
443 * special meaning. */
444 /* { 0x0000, EXIF_FORMAT_BYTE, -1, "Exif.GPSInfo.GPSVersionID", NULL, NULL }, */
445 { 0x0001, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLatitudeRef", NULL, NULL },
446 { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", NULL, NULL },
447 { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", NULL, NULL },
448 { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", NULL, NULL },
449 { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", NULL, NULL },
450 { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", NULL, NULL },
451 { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", NULL, NULL },
452 { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", NULL, NULL },
453 { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", NULL, NULL },
454 { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", NULL, NULL },
455 { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", NULL, NULL },
456 { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", NULL, NULL },
457 { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", NULL, NULL },
458 { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", NULL, NULL },
459 { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", NULL, NULL },
460 { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", NULL, NULL },
461 { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", NULL, NULL },
462 { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", NULL, NULL },
463 { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", NULL, NULL },
464 { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", NULL, NULL },
465 { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", NULL, NULL },
466 { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", NULL, NULL },
467 { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", NULL, NULL },
468 { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", NULL, NULL },
469 { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", NULL, NULL },
470 { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", NULL, NULL },
471 { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", NULL, NULL },
472 { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", NULL, NULL },
473 { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", NULL, NULL },
474 { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", NULL, NULL },
479 ExifMarker ExifUnknownMarkersList[] = {
480 { 0x0000, EXIF_FORMAT_UNKNOWN, 0, "unknown", NULL, NULL },
481 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1, "unknown", NULL, NULL },
482 { 0x0000, EXIF_FORMAT_STRING, -1, "unknown", NULL, NULL },
483 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1, "unknown", NULL, NULL },
484 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1, "unknown", NULL, NULL },
485 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "unknown", NULL, NULL },
486 { 0x0000, EXIF_FORMAT_BYTE, -1, "unknown", NULL, NULL },
487 { 0x0000, EXIF_FORMAT_UNDEFINED, -1, "unknown", NULL, NULL },
488 { 0x0000, EXIF_FORMAT_SHORT, -1, "unknown", NULL, NULL },
489 { 0x0000, EXIF_FORMAT_LONG, -1, "unknown", NULL, NULL },
490 { 0x0000, EXIF_FORMAT_RATIONAL, -1, "unknown", NULL, NULL },
491 { 0x0000, EXIF_FORMAT_FLOAT, -1, "unknown", NULL, NULL },
492 { 0x0000, EXIF_FORMAT_DOUBLE, -1, "unknown", NULL, NULL },
495 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
498 *-----------------------------------------------------------------------------
500 *-----------------------------------------------------------------------------
503 ExifItem *exif_item_new(ExifFormatType format, guint tag,
504 guint elements, const ExifMarker *marker)
508 item = g_new0(ExifItem, 1);
509 item->format = format;
511 item->marker = marker;
512 item->elements = elements;
518 case EXIF_FORMAT_UNKNOWN:
519 /* unknown, data is NULL */
522 case EXIF_FORMAT_BYTE_UNSIGNED:
523 item->data_len = sizeof(gchar) * elements;
525 case EXIF_FORMAT_STRING:
526 item->data_len = sizeof(gchar) * elements;
528 case EXIF_FORMAT_SHORT_UNSIGNED:
529 item->data_len = sizeof(guint16) * elements;
531 case EXIF_FORMAT_LONG_UNSIGNED:
532 item->data_len = sizeof(guint32) * elements;
534 case EXIF_FORMAT_RATIONAL_UNSIGNED:
535 item->data_len = sizeof(ExifRational) * elements;
537 case EXIF_FORMAT_BYTE:
538 item->data_len = sizeof(gchar) * elements;
540 case EXIF_FORMAT_UNDEFINED:
541 item->data_len = sizeof(gchar) * elements;
543 case EXIF_FORMAT_SHORT:
544 item->data_len = sizeof(gint16) * elements;
546 case EXIF_FORMAT_LONG:
547 item->data_len = sizeof(gint32) * elements;
549 case EXIF_FORMAT_RATIONAL:
550 item->data_len = sizeof(ExifRational) * elements;
552 case EXIF_FORMAT_FLOAT:
553 item->data_len = sizeof(float) * elements;
555 case EXIF_FORMAT_DOUBLE:
556 item->data_len = sizeof(gdouble) * elements;
560 item->data = g_malloc0(item->data_len);
565 static void exif_item_free(ExifItem *item)
573 gchar *exif_item_get_tag_name(ExifItem *item)
575 if (!item || !item->marker) return NULL;
576 return g_strdup(item->marker->key);
579 guint exif_item_get_tag_id(ExifItem *item)
585 guint exif_item_get_elements(ExifItem *item)
588 return item->elements;
591 gchar *exif_item_get_data(ExifItem *item, guint *data_len)
594 *data_len = item->data_len;
595 return g_memdup(item->data, item->data_len);
598 guint exif_item_get_format_id(ExifItem *item)
600 if (!item) return EXIF_FORMAT_UNKNOWN;
605 gchar *exif_item_get_description(ExifItem *item)
607 if (!item || !item->marker) return NULL;
608 return g_strdup(_(item->marker->description));
611 const gchar *exif_item_get_format_name(ExifItem *item, gint brief)
613 if (!item || !item->marker) return NULL;
614 return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
617 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
621 for (i = 0 ; i < ne; i++)
623 guchar c = ((gchar *)data)[i];
624 if (c < 32 || c > 127) c = '.';
625 g_string_append_printf(string, "%c", c);
627 string = g_string_append(string, " : ");
628 for (i = 0 ; i < ne; i++)
646 g_string_append_printf(string, "%s%02x", spacer, ((gchar *)data)[i]);
653 gchar *exif_text_list_find_value(ExifTextList *list, guint value)
655 gchar *result = NULL;
659 while (!result && list[i].value >= 0)
661 if (value == (guint) list[i].value) result = g_strdup(_(list[i].description));
664 if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
671 *-------------------------------------------------------------------
673 *-------------------------------------------------------------------
676 /* note: the align_buf is used to avoid alignment issues (on sparc) */
678 guint16 exif_byte_get_int16(guchar *f, ExifByteOrder bo)
682 memcpy(&align_buf, f, sizeof(guint16));
684 if (bo == EXIF_BYTE_ORDER_INTEL)
685 return GUINT16_FROM_LE(align_buf);
687 return GUINT16_FROM_BE(align_buf);
690 guint32 exif_byte_get_int32(guchar *f, ExifByteOrder bo)
694 memcpy(&align_buf, f, sizeof(guint32));
696 if (bo == EXIF_BYTE_ORDER_INTEL)
697 return GUINT32_FROM_LE(align_buf);
699 return GUINT32_FROM_BE(align_buf);
702 void exif_byte_put_int16(guchar *f, guint16 n, ExifByteOrder bo)
706 if (bo == EXIF_BYTE_ORDER_INTEL)
708 align_buf = GUINT16_TO_LE(n);
712 align_buf = GUINT16_TO_BE(n);
715 memcpy(f, &align_buf, sizeof(guint16));
718 void exif_byte_put_int32(guchar *f, guint32 n, ExifByteOrder bo)
722 if (bo == EXIF_BYTE_ORDER_INTEL)
724 align_buf = GUINT32_TO_LE(n);
728 align_buf = GUINT32_TO_BE(n);
731 memcpy(f, &align_buf, sizeof(guint32));
736 *-------------------------------------------------------------------
738 *-------------------------------------------------------------------
741 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list)
745 if (!list) return NULL;
747 while (list[i].tag != 0 && list[i].tag != tag)
752 return (list[i].tag == 0 ? NULL : &list[i]);
755 static void rational_from_data(ExifRational *r, gpointer src, ExifByteOrder bo)
757 r->num = exif_byte_get_int32(src, bo);
758 r->den = exif_byte_get_int32(src + sizeof(guint32), bo);
761 /* src_format and item->format must be compatible
762 * and not overrun src or item->data.
764 void exif_item_copy_data(ExifItem *item, gpointer src, guint len,
765 ExifFormatType src_format, ExifByteOrder bo)
772 bs = ExifFormatList[item->format].size;
777 ExifFormatList[src_format].size * ne > len)
779 gchar *tag = exif_item_get_tag_name(item);
780 log_printf("exif tag %s data size mismatch\n", tag);
785 switch (item->format)
787 case EXIF_FORMAT_UNKNOWN:
789 case EXIF_FORMAT_BYTE_UNSIGNED:
790 case EXIF_FORMAT_BYTE:
791 case EXIF_FORMAT_UNDEFINED:
792 memcpy(dest, src, len);
794 case EXIF_FORMAT_STRING:
795 memcpy(dest, src, len);
796 /* string is NULL terminated, make sure this is true */
797 if (((gchar *)dest)[len - 1] != '\0') ((gchar *)dest)[len - 1] = '\0';
799 case EXIF_FORMAT_SHORT_UNSIGNED:
800 case EXIF_FORMAT_SHORT:
801 for (i = 0; i < ne; i++)
803 ((guint16 *)dest)[i] = exif_byte_get_int16(src + i * bs, bo);
806 case EXIF_FORMAT_LONG_UNSIGNED:
807 case EXIF_FORMAT_LONG:
808 if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
809 src_format == EXIF_FORMAT_SHORT)
811 /* a short fits into a long, so allow it */
814 ss = ExifFormatList[src_format].size;
815 for (i = 0; i < ne; i++)
817 ((gint32 *)dest)[i] =
818 (gint32)exif_byte_get_int16(src + i * ss, bo);
823 for (i = 0; i < ne; i++)
825 ((gint32 *)dest)[i] =
826 exif_byte_get_int32(src + i * bs, bo);
830 case EXIF_FORMAT_RATIONAL_UNSIGNED:
831 case EXIF_FORMAT_RATIONAL:
832 for (i = 0; i < ne; i++)
834 rational_from_data(&((ExifRational *)dest)[i], src + i * bs, bo);
837 case EXIF_FORMAT_FLOAT:
838 for (i = 0; i < ne; i++)
840 ((float *)dest)[i] = exif_byte_get_int32(src + i * bs, bo);
843 case EXIF_FORMAT_DOUBLE:
844 for (i = 0; i < ne; i++)
848 rational_from_data(&r, src + i * bs, bo);
849 if (r.den) ((gdouble *)dest)[i] = (gdouble)r.num / r.den;
855 static gint exif_parse_IFD_entry(ExifData *exif, guchar *tiff, guint offset,
856 guint size, ExifByteOrder bo,
858 const ExifMarker *list)
866 const ExifMarker *marker;
869 tag = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_TAG, bo);
870 format = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
871 count = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_COUNT, bo);
872 data_val = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_DATA, bo);
874 /* Check tag type. If it does not match, either the format is wrong,
875 * either it is a unknown tag; so it is not really an error.
877 marker = exif_marker_from_tag(tag, list);
880 if (format >= EXIF_FORMAT_COUNT)
882 log_printf("warning: exif tag 0x%4x has invalid format %d\n", tag, format);
885 /* allow non recognized tags to be displayed */
886 marker = &ExifUnknownMarkersList[format];
888 if (marker->format != format)
890 /* Some cameras got mixed up signed/unsigned_rational
891 * eg KODAK DC4800 on object_distance tag
893 * FIXME: what exactly is this test trying to do?
894 * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
896 if (!(marker->format == EXIF_FORMAT_RATIONAL_UNSIGNED && format == EXIF_FORMAT_RATIONAL) &&
897 !(marker->format == EXIF_FORMAT_RATIONAL && format == EXIF_FORMAT_RATIONAL_UNSIGNED) &&
898 /* short fits into a long so allow this mismatch
899 * as well (some tags allowed to be unsigned short _or_ unsigned long)
901 !(marker->format == EXIF_FORMAT_LONG_UNSIGNED && format == EXIF_FORMAT_SHORT_UNSIGNED) )
903 if (format < EXIF_FORMAT_COUNT)
905 log_printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
906 marker->key, ExifFormatList[format].short_name,
907 ExifFormatList[marker->format].short_name);
911 log_printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
912 marker->key, format, marker->format,
913 ExifFormatList[marker->format].short_name);
919 /* Where is the data, is it available?
921 if (marker->components > 0 && (guint) marker->components != count)
923 log_printf("warning: exif tag %s has %d elements, exif spec requests %d\n",
924 marker->key, count, marker->components);
927 data_length = ExifFormatList[marker->format].size * count;
930 data_offset = data_val;
931 if (size < data_offset + data_length)
933 log_printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
939 data_offset = offset + EXIF_TIFD_OFFSET_DATA;
942 item = exif_item_new(marker->format, tag, count, marker);
943 exif_item_copy_data(item, tiff + data_offset, data_length, format, bo);
944 exif->items = g_list_prepend(exif->items, item);
946 if (list == ExifKnownMarkersList)
951 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, list);
954 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, ExifKnownGPSInfoMarkersList);
956 case TAG_EXIFMAKERNOTE:
957 format_exif_makernote_parse(exif, tiff, data_val, size, bo);
965 gint exif_parse_IFD_table(ExifData *exif,
966 guchar *tiff, guint offset,
967 guint size, ExifByteOrder bo,
969 const ExifMarker *list)
974 /* limit damage from infinite loops */
975 if (level > EXIF_TIFF_MAX_LEVELS) return -1;
977 /* We should be able to read number of entries in IFD0) */
978 if (size < offset + 2) return -1;
980 count = exif_byte_get_int16(tiff + offset, bo);
983 /* Entries and next IFD offset must be readable */
984 if (size < offset + count * EXIF_TIFD_SIZE + 4) return -1;
986 for (i = 0; i < count; i++)
988 exif_parse_IFD_entry(exif, tiff, offset + i * EXIF_TIFD_SIZE, size, bo, level, list);
995 *-------------------------------------------------------------------
997 *-------------------------------------------------------------------
1000 gint exif_tiff_directory_offset(guchar *data, const guint len,
1001 guint *offset, ExifByteOrder *bo)
1003 if (len < 8) return FALSE;
1005 if (memcmp(data, "II", 2) == 0)
1007 *bo = EXIF_BYTE_ORDER_INTEL;
1009 else if (memcmp(data, "MM", 2) == 0)
1011 *bo = EXIF_BYTE_ORDER_MOTOROLA;
1018 if (exif_byte_get_int16(data + 2, *bo) != 0x002A)
1023 *offset = exif_byte_get_int32(data + 4, *bo);
1025 return (*offset < len);
1028 gint exif_tiff_parse(ExifData *exif, guchar *tiff, guint size, ExifMarker *list)
1033 if (!exif_tiff_directory_offset(tiff, size, &offset, &bo)) return -1;
1035 return exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, list);
1040 *-------------------------------------------------------------------
1042 *-------------------------------------------------------------------
1045 #define JPEG_MARKER 0xFF
1046 #define JPEG_MARKER_SOI 0xD8
1047 #define JPEG_MARKER_EOI 0xD9
1048 #define JPEG_MARKER_APP1 0xE1
1049 #define JPEG_MARKER_APP2 0xE2
1051 /* jpeg container format:
1052 all data markers start with 0XFF
1053 2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
1054 4 byte long data segment markers in format: 0xFFTTSSSSNNN...
1055 FF: 1 byte standard marker identifier
1056 TT: 1 byte data type
1057 SSSS: 2 bytes in Motorola byte alignment for length of the data.
1058 This value includes these 2 bytes in the count, making actual
1059 length of NN... == SSSS - 2.
1060 NNN.: the data in this segment
1062 static ExifMarker jpeg_color_marker = { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL };
1064 void exif_add_jpeg_color_profile(ExifData *exif, guchar *cp_data, guint cp_length)
1066 ExifItem *item = exif_item_new(jpeg_color_marker.format, jpeg_color_marker.tag, 1,
1067 &jpeg_color_marker);
1069 item->data = cp_data;
1070 item->elements = cp_length;
1071 item->data_len = cp_length;
1072 exif->items = g_list_prepend(exif->items, item);
1076 static gint exif_jpeg_parse(ExifData *exif,
1077 guchar *data, guint size,
1080 guint seg_offset = 0;
1081 guint seg_length = 0;
1085 memcmp(data, "\xFF\xD8", 2) != 0)
1090 if (exif_jpeg_segment_find(data, size, JPEG_MARKER_APP1,
1092 &seg_offset, &seg_length))
1094 res = exif_tiff_parse(exif, data + seg_offset + 6, seg_length - 6, list);
1097 if (exif_jpeg_parse_color(exif, data, size))
1105 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1107 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1108 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1109 return (guchar *) exif_item_get_data(prof_item, data_len);
1115 *-------------------------------------------------------------------
1117 *-------------------------------------------------------------------
1121 ExifItem *exif_get_first_item(ExifData *exif)
1125 ExifItem *ret = (ExifItem *)exif->items->data;
1126 exif->current = exif->items->next;
1129 exif->current = NULL;
1133 ExifItem *exif_get_next_item(ExifData *exif)
1137 ExifItem *ret = (ExifItem *)exif->current->data;
1138 exif->current = exif->current->next;
1144 static gint map_file(const gchar *path, void **mapping, gint *size)
1149 fd = open(path, O_RDONLY);
1156 if (fstat(fd, &fs) == -1)
1165 *mapping = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1166 if (*mapping == MAP_FAILED)
1177 static gint unmap_file(gpointer mapping, gint size)
1179 if (munmap(mapping, size) == -1)
1188 void exif_free(ExifData *exif)
1197 ExifItem *item = work->data;
1199 exif_item_free(item);
1202 g_list_free(exif->items);
1207 ExifData *exif_read(gchar *path, gchar *sidecar_path)
1214 if (!path) return NULL;
1216 pathl = path_from_utf8(path);
1217 if (map_file(pathl, &f, &size) == -1)
1224 exif = g_new0(ExifData, 1);
1226 exif->current = NULL;
1227 exif->path = g_strdup(path);
1229 res = exif_jpeg_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1232 res = exif_tiff_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1237 FormatRawExifType exif_type;
1238 FormatRawExifParseFunc exif_parse_func;
1241 exif_type = format_raw_exif_offset(f, size, &offset, &exif_parse_func);
1244 case FORMAT_RAW_EXIF_NONE:
1247 case FORMAT_RAW_EXIF_TIFF:
1248 res = exif_tiff_parse(exif, (guchar *)f + offset, size - offset,
1249 ExifKnownMarkersList);
1251 case FORMAT_RAW_EXIF_JPEG:
1252 res = exif_jpeg_parse(exif, (guchar *)f + offset, size - offset,
1253 ExifKnownMarkersList);
1255 case FORMAT_RAW_EXIF_IFD_II:
1256 case FORMAT_RAW_EXIF_IFD_MM:
1257 res = exif_parse_IFD_table(exif, (guchar *)f, offset, size - offset,
1258 (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1259 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1260 0, ExifKnownMarkersList);
1262 case FORMAT_RAW_EXIF_PROPRIETARY:
1263 if (exif_parse_func)
1265 res = exif_parse_func((guchar *)f + offset, size - offset, exif);
1277 unmap_file(f, size);
1279 if (exif) exif->items = g_list_reverse(exif->items);
1282 exif_write_data_list(exif, stdout, TRUE);
1283 exif_write_data_list(exif, stdout, FALSE);
1289 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1293 if (!key) return NULL;
1302 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1307 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1309 gchar *exif_item_get_string(ExifItem *item, gint idx)
1311 return exif_item_get_data_as_text(item);
1315 gchar *exif_item_get_data_as_text(ExifItem *item)
1317 const ExifMarker *marker;
1324 if (!item) return NULL;
1326 marker = item->marker;
1327 if (!marker) return NULL;
1330 ne = item->elements;
1331 if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1332 string = g_string_new("");
1333 switch (item->format)
1335 case EXIF_FORMAT_UNKNOWN:
1337 case EXIF_FORMAT_BYTE_UNSIGNED:
1338 case EXIF_FORMAT_BYTE:
1339 case EXIF_FORMAT_UNDEFINED:
1340 if (ne == 1 && marker->list)
1345 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1346 item->format == EXIF_FORMAT_UNDEFINED)
1348 val = ((guchar *)data)[0];
1352 val = (guchar)(((gchar *)data)[0]);
1355 result = exif_text_list_find_value(marker->list, (guint)val);
1356 string = g_string_append(string, result);
1361 string = string_append_raw_bytes(string, data, ne);
1364 case EXIF_FORMAT_STRING:
1365 string = g_string_append(string, (gchar *)(item->data));
1367 case EXIF_FORMAT_SHORT_UNSIGNED:
1368 if (ne == 1 && marker->list)
1372 result = exif_text_list_find_value(marker->list, ((guint16 *)data)[0]);
1373 string = g_string_append(string, result);
1376 else for (i = 0; i < ne; i++)
1378 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1379 ((guint16 *)data)[i]);
1382 case EXIF_FORMAT_LONG_UNSIGNED:
1383 for (i = 0; i < ne; i++)
1385 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1386 (gulong)((guint32 *)data)[i]);
1389 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1390 for (i = 0; i < ne; i++)
1394 r = &((ExifRational *)data)[i];
1395 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1396 (gulong)r->num, (gulong)r->den);
1399 case EXIF_FORMAT_SHORT:
1400 for (i = 0; i < ne; i++)
1402 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1403 ((gint16 *)data)[i]);
1406 case EXIF_FORMAT_LONG:
1407 for (i = 0; i < ne; i++)
1409 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1410 (glong)((gint32 *)data)[i]);
1413 case EXIF_FORMAT_RATIONAL:
1414 for (i = 0; i < ne; i++)
1418 r = &((ExifRational *)data)[i];
1419 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1420 (glong)r->num, (glong)r->den);
1423 case EXIF_FORMAT_FLOAT:
1424 for (i = 0; i < ne; i++)
1426 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1427 ((float *)data)[i]);
1430 case EXIF_FORMAT_DOUBLE:
1431 for (i = 0; i < ne; i++)
1433 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1434 ((gdouble *)data)[i]);
1439 if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1440 item->format != EXIF_FORMAT_STRING)
1442 g_string_append(string, " ...");
1446 g_string_free(string, FALSE);
1451 gint exif_item_get_integer(ExifItem *item, gint *value)
1453 if (!item) return FALSE;
1455 switch (item->format)
1457 case EXIF_FORMAT_SHORT:
1458 *value = (gint)(((gint16 *)(item->data))[0]);
1461 case EXIF_FORMAT_SHORT_UNSIGNED:
1462 *value = (gint)(((guint16 *)(item->data))[0]);
1465 case EXIF_FORMAT_LONG:
1466 *value = (gint)(((gint32 *)(item->data))[0]);
1469 case EXIF_FORMAT_LONG_UNSIGNED:
1470 /* FIXME: overflow possible */
1471 *value = (gint)(((guint32 *)(item->data))[0]);
1474 /* all other type return FALSE */
1481 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1483 if (!item) return NULL;
1484 if (n >= item->elements) return NULL;
1486 if (item->format == EXIF_FORMAT_RATIONAL ||
1487 item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1489 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1490 return &((ExifRational *)(item->data))[n];
1496 gchar *exif_get_tag_description_by_key(const gchar *key)
1500 if (!key) return NULL;
1503 while (ExifKnownMarkersList[i].tag > 0)
1505 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1510 while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1512 if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return _(ExifKnownGPSInfoMarkersList[i].description);
1519 static void exif_write_item(FILE *f, ExifItem *item)
1523 text = exif_item_get_data_as_text(item);
1526 gchar *tag = exif_item_get_tag_name(item);
1527 fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1534 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1536 if (!f || !exif) return;
1538 fprintf(f, " tag format key value\n");
1539 fprintf(f, "----------------------------------------------------\n");
1541 if (human_readable_list)
1546 while (ExifFormattedList[i].key)
1550 text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1553 fprintf(f, " %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1570 exif_write_item(f, item);
1573 fprintf(f, "----------------------------------------------------\n");
1576 gint exif_write(ExifData *exif)
1578 log_printf("Not compiled with EXIF write support");
1582 ExifItem *exif_add_item(ExifData *exif, const gchar *key)
1587 gint exif_item_delete(ExifData *exif, ExifItem *item)
1592 gint exif_item_set_string(ExifItem *item, const gchar *str)
1598 typedef struct _UnmapData UnmapData;
1606 static GList *exif_unmap_list = 0;
1608 guchar *exif_get_preview(ExifData *exif, guint *data_len)
1618 if (!exif) return NULL;
1621 fd = open(path, O_RDONLY);
1629 if (fstat(fd, &st) == -1)
1634 map_len = st.st_size;
1635 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1638 if (map_data == MAP_FAILED)
1643 if (format_raw_img_exif_offsets(map_data, map_len, &offset, NULL) && offset)
1647 DEBUG_1("%s: offset %lu", path, offset);
1649 *data_len = map_len - offset;
1650 ud = g_new(UnmapData, 1);
1651 ud->ptr = map_data + offset;
1652 ud->map_data = map_data;
1653 ud->map_len = map_len;
1655 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1659 munmap(map_data, map_len);
1664 void exif_free_preview(guchar *buf)
1666 GList *work = exif_unmap_list;
1670 UnmapData *ud = (UnmapData *)work->data;
1673 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1679 g_assert_not_reached();
1684 /* not HAVE_EXIV2 */
1685 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */