4 * Copyright (C) 2008 - 2012 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>
70 #include <glib/gprintf.h>
76 #include "jpeg_parser.h"
78 #include "format_raw.h"
79 #include "ui_fileops.h"
83 *-----------------------------------------------------------------------------
85 *-----------------------------------------------------------------------------
88 ExifFormatAttrib ExifFormatList[] = {
89 { EXIF_FORMAT_UNKNOWN, 1, "unknown", "unknown" },
90 { EXIF_FORMAT_BYTE_UNSIGNED, 1, "ubyte", "unsigned byte" },
91 { EXIF_FORMAT_STRING, 1, "string", "string" },
92 { EXIF_FORMAT_SHORT_UNSIGNED, 2, "ushort", "unsigned short" },
93 { EXIF_FORMAT_LONG_UNSIGNED, 4, "ulong", "unsigned long" },
94 { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
95 { EXIF_FORMAT_BYTE, 1, "byte", "byte" },
96 { EXIF_FORMAT_UNDEFINED, 1, "undefined", "undefined" },
97 { EXIF_FORMAT_SHORT, 2, "sshort", "signed short" },
98 { EXIF_FORMAT_LONG, 4, "slong", "signed long" },
99 { EXIF_FORMAT_RATIONAL, 8, "srational", "signed rational" },
100 { EXIF_FORMAT_FLOAT, 4, "float", "float" },
101 { EXIF_FORMAT_DOUBLE, 8, "double", "double" },
102 { -1, 0, NULL, NULL }
105 /* tags that are special, or need special treatment */
106 #define TAG_EXIFOFFSET 0x8769
107 #define TAG_EXIFMAKERNOTE 0x927c
108 #define TAG_GPSOFFSET 0x8825
112 *-----------------------------------------------------------------------------
114 *-----------------------------------------------------------------------------
116 static ExifTextList ExifCompressionList[] = {
117 { 1, "Uncompressed" },
119 { 3, "T4/Group 3 Fax" },
120 { 4, "T6/Group 4 Fax" },
122 { 6, "JPEG (old style)" },
124 { 8, "Adobe Deflate" },
126 { 10, "JBIG Color" },
128 { 32771, "CCIRLEW" },
129 { 32773, "PackBits" },
130 { 32809, "ThunderScan" },
131 { 32895, "IT8CTPAD" },
135 { 32908, "PixasFilm" },
136 { 32909, "PixasLog" },
137 { 32946, "Deflate" },
141 { 34677, "SGILog24" },
142 { 34712, "JPEF 2000" },
143 { 34713, "Nikon NEF Compressed" },
147 static ExifTextList ExifOrientationList[] = {
148 { EXIF_ORIENTATION_UNKNOWN, N_("unknown") },
149 { EXIF_ORIENTATION_TOP_LEFT, N_("top left") },
150 { EXIF_ORIENTATION_TOP_RIGHT, N_("top right") },
151 { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
152 { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
153 { EXIF_ORIENTATION_LEFT_TOP, N_("left top") },
154 { EXIF_ORIENTATION_RIGHT_TOP, N_("right top") },
155 { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
156 { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
160 static ExifTextList ExifUnitList[] = {
161 { EXIF_UNIT_UNKNOWN, N_("unknown") },
162 { EXIF_UNIT_NOUNIT, "" },
163 { EXIF_UNIT_INCH, N_("inch") },
164 { EXIF_UNIT_CENTIMETER, N_("centimeter") },
168 static ExifTextList ExifYCbCrPosList[] = {
174 static ExifTextList ExifMeteringModeList[] = {
175 { 0, N_("unknown") },
176 { 1, N_("average") },
177 { 2, N_("center weighted") },
179 { 4, N_("multi-spot") },
180 { 5, N_("multi-segment") },
181 { 6, N_("partial") },
182 { 255, N_("other") },
186 static ExifTextList ExifExposureProgramList[] = {
187 { 0, N_("not defined") },
190 { 3, N_("aperture") },
191 { 4, N_("shutter") },
192 { 5, N_("creative") },
194 { 7, N_("portrait") },
195 { 8, N_("landscape") },
199 static ExifTextList ExifLightSourceList[] = {
200 { 0, N_("unknown") },
201 { 1, N_("daylight") },
202 { 2, N_("fluorescent") },
203 { 3, N_("tungsten (incandescent)") },
205 { 9, N_("fine weather") },
206 { 10, N_("cloudy weather") },
208 { 12, N_("daylight fluorescent") },
209 { 13, N_("day white fluorescent") },
210 { 14, N_("cool white fluorescent") },
211 { 15, N_("white fluorescent") },
212 { 17, N_("standard light A") },
213 { 18, N_("standard light B") },
214 { 19, N_("standard light C") },
219 { 24, N_("ISO studio tungsten") },
220 { 255, N_("other") },
224 static ExifTextList ExifFlashList[] = {
227 { 5, N_("yes, not detected by strobe") },
228 { 7, N_("yes, detected by strobe") },
232 static ExifTextList ExifColorSpaceList[] = {
234 { 65535,N_("uncalibrated") },
238 static ExifTextList ExifSensorList[] = {
239 { 1, N_("not defined") },
240 { 2, N_("1 chip color area") },
241 { 2, N_("2 chip color area") },
242 { 4, N_("3 chip color area") },
243 { 5, N_("color sequential area") },
244 { 7, N_("trilinear") },
245 { 8, N_("color sequential linear") },
249 static ExifTextList ExifSourceList[] = {
250 { 3, N_("digital still camera") },
254 static ExifTextList ExifSceneList[] = {
255 { 1, N_("direct photo") },
259 static ExifTextList ExifCustRenderList[] = {
265 static ExifTextList ExifExposureModeList[] = {
268 { 2, N_("auto bracket") },
272 static ExifTextList ExifWhiteBalanceList[] = {
278 static ExifTextList ExifSceneCaptureList[] = {
279 { 0, N_("standard") },
280 { 1, N_("landscape") },
281 { 2, N_("portrait") },
282 { 3, N_("night scene") },
286 static ExifTextList ExifGainControlList[] = {
288 { 1, N_("low gain up") },
289 { 2, N_("high gain up") },
290 { 3, N_("low gain down") },
291 { 4, N_("high gain down") },
295 static ExifTextList ExifContrastList[] = {
302 static ExifTextList ExifSaturationList[] = {
309 static ExifTextList ExifSharpnessList[] = {
316 static ExifTextList ExifSubjectRangeList[] = {
317 { 0, N_("unknown") },
320 { 3, N_("distant") },
325 Tag names should match to exiv2 keys, http://www.exiv2.org/metadata.html
326 Tags that don't match are not supported by exiv2 and should not be used anywhere in the code
329 ExifMarker ExifKnownMarkersList[] = {
330 { 0x0100, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageWidth", N_("Image Width"), NULL },
331 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageLength", N_("Image Height"), NULL },
332 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.BitsPerSample", N_("Bits per Sample/Pixel"), NULL },
333 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Compression", N_("Compression"), ExifCompressionList },
334 { 0x010e, EXIF_FORMAT_STRING, -1, "Exif.Image.ImageDescription", N_("Image description"), NULL },
335 { 0x010f, EXIF_FORMAT_STRING, -1, "Exif.Image.Make", N_("Camera make"), NULL },
336 { 0x0110, EXIF_FORMAT_STRING, -1, "Exif.Image.Model", N_("Camera model"), NULL },
337 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Orientation", N_("Orientation"), ExifOrientationList },
338 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.XResolution", N_("X resolution"), NULL },
339 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.YResolution", N_("Y Resolution"), NULL },
340 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.ResolutionUnit", N_("Resolution units"), ExifUnitList },
341 { 0x0131, EXIF_FORMAT_STRING, -1, "Exif.Image.Software", N_("Firmware"), NULL },
342 { 0x0132, EXIF_FORMAT_STRING, 20, "Exif.Image.DateTime", N_("Date"), NULL },
343 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2, "Exif.Image.WhitePoint", N_("White point"), NULL },
344 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), NULL },
345 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), NULL },
346 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.YCbCrPositioning", N_("YCbCr positioning"), ExifYCbCrPosList },
347 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.ReferenceBlackWhite",N_("Black white reference"), NULL },
348 { 0x8298, EXIF_FORMAT_STRING, -1, "Exif.Image.Copyright", N_("Copyright"), NULL },
349 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ExifTag", N_("SubIFD Exif offset"), NULL },
351 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureTime", N_("Exposure time (seconds)"), NULL },
352 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FNumber", N_("FNumber"), NULL },
353 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureProgram", N_("Exposure program"), ExifExposureProgramList },
354 { 0x8824, EXIF_FORMAT_STRING, -1, "Exif.Photo.SpectralSensitivity",N_("Spectral Sensitivity"), NULL },
355 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.ISOSpeedRatings", N_("ISO sensitivity"), NULL },
356 { 0x8828, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.OECF", N_("Optoelectric conversion factor"), NULL },
357 { 0x9000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.ExifVersion", N_("Exif version"), NULL },
358 { 0x9003, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeOriginal", N_("Date original"), NULL },
359 { 0x9004, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeDigitized", N_("Date digitized"), NULL },
360 { 0x9101, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), NULL },
361 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1, "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), NULL },
362 { 0x9201, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), NULL },
363 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ApertureValue", N_("Aperture"), NULL },
364 { 0x9203, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.BrightnessValue", N_("Brightness"), NULL },
365 { 0x9204, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), NULL },
366 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.MaxApertureValue", N_("Maximum aperture"), NULL },
367 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.SubjectDistance", N_("Subject distance"), NULL },
368 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.MeteringMode", N_("Metering mode"), ExifMeteringModeList },
369 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.LightSource", N_("Light source"), ExifLightSourceList },
370 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Flash", N_("Flash"), ExifFlashList },
371 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalLength", N_("Focal length"), NULL },
372 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SubjectArea", N_("Subject area"), NULL },
373 { 0x927c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.MakerNote", N_("MakerNote"), NULL },
374 { 0x9286, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.UserComment", N_("UserComment"), NULL },
375 { 0x9290, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTime", N_("Subsecond time"), NULL },
376 { 0x9291, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), NULL },
377 { 0x9292, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), NULL },
378 { 0xa000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.FlashpixVersion", N_("FlashPix version"), NULL },
379 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ColorSpace", N_("Colorspace"), ExifColorSpaceList },
380 /* ExifImageWidth, ExifImageHeight can also be unsigned short */
381 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelXDimension", N_("Width"), NULL },
382 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelYDimension", N_("Height"), NULL },
383 { 0xa004, EXIF_FORMAT_STRING, -1, "Exif.Photo.RelatedSoundFile", N_("Audio data"), NULL },
384 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifInteroperabilityOffset", N_("ExifR98 extension"), NULL },
385 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", N_("Flash strength"), NULL },
386 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), NULL },
387 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), NULL },
388 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), NULL },
389 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalPlaneResolutionUnit", N_("Pixel density units"), ExifUnitList },
390 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Photo.SubjectLocation", N_("Subject location"), NULL },
391 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", N_("ISO sensitivity"), NULL },
392 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SensingMethod", N_("Sensor type"), ExifSensorList },
393 { 0xa300, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.FileSource", N_("Source type"), ExifSourceList },
394 { 0xa301, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.SceneType", N_("Scene type"), ExifSceneList },
395 { 0xa302, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.CFAPattern", N_("Color filter array pattern"), NULL },
396 /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
397 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.CustomRendered", N_("Render process"), ExifCustRenderList },
398 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureMode", N_("Exposure mode"), ExifExposureModeList },
399 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.WhiteBalance", N_("White balance"), ExifWhiteBalanceList },
400 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.DigitalZoomRatio", N_("Digital zoom ratio"), NULL },
401 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), NULL },
402 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SceneCaptureType", N_("Scene capture type"), ExifSceneCaptureList },
403 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.GainControl", N_("Gain control"), ExifGainControlList },
404 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Contrast", N_("Contrast"), ExifContrastList },
405 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Saturation", N_("Saturation"), ExifSaturationList },
406 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Sharpness", N_("Sharpness"), ExifSharpnessList },
407 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.DeviceSettingDescription",N_("Device setting"), NULL },
408 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SubjectDistanceRange",N_("Subject range"), ExifSubjectRangeList },
409 { 0xa420, EXIF_FORMAT_STRING, -1, "Exif.Photo.ImageUniqueID", N_("Image serial number"), NULL },
410 /* place known, but undocumented or lesser used tags here */
411 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.NewSubfileType", NULL, NULL },
412 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubfileType", NULL, NULL },
413 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3, "Exif.Image.TransferFunction", NULL, NULL },
414 { 0x013b, EXIF_FORMAT_STRING, -1, "Exif.Image.Artist", "Artist", NULL },
415 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Predictor", NULL, NULL },
416 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileWidth", NULL, NULL },
417 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileLength", NULL, NULL },
418 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1, "TileOffsets", NULL, NULL },
419 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1, "TileByteCounts", NULL, NULL },
420 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1, "Exif.Image.SubIFDs", NULL, NULL },
421 { 0x015b, EXIF_FORMAT_UNDEFINED, -1, "JPEGTables", NULL, NULL },
422 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Image.CFARepeatPatternDim", NULL, NULL },
423 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Exif.Image.CFAPattern", NULL, NULL },
424 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.BatteryLevel", NULL, NULL },
425 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1, "IPTC/NAA", NULL, NULL },
426 { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL },
427 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1, "GPSInfo", "SubIFD GPS offset", NULL },
428 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Interlace", NULL, NULL },
429 { 0x882a, EXIF_FORMAT_SHORT, 1, "TimeZoneOffset", NULL, NULL },
430 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SelfTimerMode", NULL, NULL },
431 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", NULL, NULL },
432 { 0x920c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.SpatialFrequencyResponse", NULL, NULL },
433 { 0x920d, EXIF_FORMAT_UNDEFINED, -1, "Noise", NULL, NULL },
434 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1, "ImageNumber", NULL, NULL },
435 { 0x9212, EXIF_FORMAT_STRING, 1, "SecurityClassification", NULL, NULL },
436 { 0x9213, EXIF_FORMAT_STRING, -1, "ImageHistory", NULL, NULL },
437 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", NULL, NULL },
438 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4, "TIFF/EPStandardID", NULL, NULL },
443 ExifMarker ExifKnownGPSInfoMarkersList[] = {
444 /* The following do not work at the moment as the tag value 0x0000 has a
445 * special meaning. */
446 /* { 0x0000, EXIF_FORMAT_BYTE, -1, "Exif.GPSInfo.GPSVersionID", NULL, NULL }, */
447 { 0x0001, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLatitudeRef", NULL, NULL },
448 { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", NULL, NULL },
449 { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", NULL, NULL },
450 { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", NULL, NULL },
451 { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", NULL, NULL },
452 { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", NULL, NULL },
453 { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", NULL, NULL },
454 { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", NULL, NULL },
455 { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", NULL, NULL },
456 { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", NULL, NULL },
457 { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", NULL, NULL },
458 { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", NULL, NULL },
459 { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", NULL, NULL },
460 { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", NULL, NULL },
461 { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", NULL, NULL },
462 { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", NULL, NULL },
463 { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", NULL, NULL },
464 { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", NULL, NULL },
465 { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", NULL, NULL },
466 { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", NULL, NULL },
467 { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", NULL, NULL },
468 { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", NULL, NULL },
469 { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", NULL, NULL },
470 { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", NULL, NULL },
471 { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", NULL, NULL },
472 { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", NULL, NULL },
473 { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", NULL, NULL },
474 { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", NULL, NULL },
475 { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", NULL, NULL },
476 { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", NULL, NULL },
481 ExifMarker ExifUnknownMarkersList[] = {
482 { 0x0000, EXIF_FORMAT_UNKNOWN, 0, "unknown", NULL, NULL },
483 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1, "unknown", NULL, NULL },
484 { 0x0000, EXIF_FORMAT_STRING, -1, "unknown", NULL, NULL },
485 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1, "unknown", NULL, NULL },
486 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1, "unknown", NULL, NULL },
487 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "unknown", NULL, NULL },
488 { 0x0000, EXIF_FORMAT_BYTE, -1, "unknown", NULL, NULL },
489 { 0x0000, EXIF_FORMAT_UNDEFINED, -1, "unknown", NULL, NULL },
490 { 0x0000, EXIF_FORMAT_SHORT, -1, "unknown", NULL, NULL },
491 { 0x0000, EXIF_FORMAT_LONG, -1, "unknown", NULL, NULL },
492 { 0x0000, EXIF_FORMAT_RATIONAL, -1, "unknown", NULL, NULL },
493 { 0x0000, EXIF_FORMAT_FLOAT, -1, "unknown", NULL, NULL },
494 { 0x0000, EXIF_FORMAT_DOUBLE, -1, "unknown", NULL, NULL },
497 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
500 *-----------------------------------------------------------------------------
502 *-----------------------------------------------------------------------------
505 ExifItem *exif_item_new(ExifFormatType format, guint tag,
506 guint elements, const ExifMarker *marker)
510 item = g_new0(ExifItem, 1);
511 item->format = format;
513 item->marker = marker;
514 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, gboolean 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 || 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 (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 ExifData *exif_get_original(ExifData *processed)
1193 void exif_free(ExifData *exif)
1202 ExifItem *item = work->data;
1204 exif_item_free(item);
1207 g_list_free(exif->items);
1212 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
1219 if (!path) return NULL;
1221 pathl = path_from_utf8(path);
1222 if (map_file(pathl, &f, &size) == -1)
1229 exif = g_new0(ExifData, 1);
1230 exif->path = g_strdup(path);
1232 res = exif_jpeg_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1235 res = exif_tiff_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1240 FormatRawExifType exif_type;
1241 FormatRawExifParseFunc exif_parse_func;
1244 exif_type = format_raw_exif_offset(f, size, &offset, &exif_parse_func);
1247 case FORMAT_RAW_EXIF_NONE:
1250 case FORMAT_RAW_EXIF_TIFF:
1251 res = exif_tiff_parse(exif, (guchar *)f + offset, size - offset,
1252 ExifKnownMarkersList);
1254 case FORMAT_RAW_EXIF_JPEG:
1255 res = exif_jpeg_parse(exif, (guchar *)f + offset, size - offset,
1256 ExifKnownMarkersList);
1258 case FORMAT_RAW_EXIF_IFD_II:
1259 case FORMAT_RAW_EXIF_IFD_MM:
1260 res = exif_parse_IFD_table(exif, (guchar *)f, offset, size - offset,
1261 (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1262 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1263 0, ExifKnownMarkersList);
1265 case FORMAT_RAW_EXIF_PROPRIETARY:
1266 if (exif_parse_func)
1268 res = exif_parse_func((guchar *)f + offset, size - offset, exif);
1280 unmap_file(f, size);
1282 if (exif) exif->items = g_list_reverse(exif->items);
1287 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1291 if (!key) return NULL;
1300 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1305 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1308 static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
1310 const ExifMarker *marker;
1317 if (!item) return NULL;
1319 marker = item->marker;
1320 if (!marker) return NULL;
1323 ne = item->elements;
1324 if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1325 string = g_string_new("");
1326 switch (item->format)
1328 case EXIF_FORMAT_UNKNOWN:
1330 case EXIF_FORMAT_BYTE_UNSIGNED:
1331 case EXIF_FORMAT_BYTE:
1332 case EXIF_FORMAT_UNDEFINED:
1333 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1338 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1339 item->format == EXIF_FORMAT_UNDEFINED)
1341 val = ((guchar *)data)[0];
1345 val = (guchar)(((gchar *)data)[0]);
1348 result = exif_text_list_find_value(marker->list, (guint)val);
1349 string = g_string_append(string, result);
1354 string = string_append_raw_bytes(string, data, ne);
1357 case EXIF_FORMAT_STRING:
1358 if (item->data) string = g_string_append(string, (gchar *)(item->data));
1360 case EXIF_FORMAT_SHORT_UNSIGNED:
1361 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1365 result = exif_text_list_find_value(marker->list, ((guint16 *)data)[0]);
1366 string = g_string_append(string, result);
1369 else for (i = 0; i < ne; i++)
1371 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1372 ((guint16 *)data)[i]);
1375 case EXIF_FORMAT_LONG_UNSIGNED:
1376 for (i = 0; i < ne; i++)
1378 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1379 (gulong)((guint32 *)data)[i]);
1382 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1383 for (i = 0; i < ne; i++)
1387 r = &((ExifRational *)data)[i];
1388 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1389 (gulong)r->num, (gulong)r->den);
1392 case EXIF_FORMAT_SHORT:
1393 for (i = 0; i < ne; i++)
1395 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1396 ((gint16 *)data)[i]);
1399 case EXIF_FORMAT_LONG:
1400 for (i = 0; i < ne; i++)
1402 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1403 (glong)((gint32 *)data)[i]);
1406 case EXIF_FORMAT_RATIONAL:
1407 for (i = 0; i < ne; i++)
1411 r = &((ExifRational *)data)[i];
1412 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1413 (glong)r->num, (glong)r->den);
1416 case EXIF_FORMAT_FLOAT:
1417 for (i = 0; i < ne; i++)
1419 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1420 ((float *)data)[i]);
1423 case EXIF_FORMAT_DOUBLE:
1424 for (i = 0; i < ne; i++)
1426 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1427 ((gdouble *)data)[i]);
1432 if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1433 item->format != EXIF_FORMAT_STRING)
1435 g_string_append(string, " ...");
1439 g_string_free(string, FALSE);
1444 gchar *exif_item_get_string(ExifItem *item, gint idx)
1446 return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
1449 gchar *exif_item_get_data_as_text(ExifItem *item)
1451 return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
1454 gint exif_item_get_integer(ExifItem *item, gint *value)
1456 if (!item) return FALSE;
1457 if (!item->elements) return FALSE;
1459 switch (item->format)
1461 case EXIF_FORMAT_SHORT:
1462 *value = (gint)(((gint16 *)(item->data))[0]);
1465 case EXIF_FORMAT_SHORT_UNSIGNED:
1466 *value = (gint)(((guint16 *)(item->data))[0]);
1469 case EXIF_FORMAT_LONG:
1470 *value = (gint)(((gint32 *)(item->data))[0]);
1473 case EXIF_FORMAT_LONG_UNSIGNED:
1474 /* FIXME: overflow possible */
1475 *value = (gint)(((guint32 *)(item->data))[0]);
1478 /* all other type return FALSE */
1485 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1487 if (!item) return NULL;
1488 if (n >= item->elements) return NULL;
1490 if (item->format == EXIF_FORMAT_RATIONAL ||
1491 item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1493 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1494 return &((ExifRational *)(item->data))[n];
1500 gchar *exif_get_tag_description_by_key(const gchar *key)
1504 if (!key) return NULL;
1507 while (ExifKnownMarkersList[i].tag > 0)
1509 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1514 while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1516 if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
1523 static void exif_write_item(FILE *f, ExifItem *item)
1527 text = exif_item_get_data_as_text(item);
1530 gchar *tag = exif_item_get_tag_name(item);
1531 g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1538 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1540 if (!f || !exif) return;
1542 g_fprintf(f, " tag format key value\n");
1543 g_fprintf(f, "----------------------------------------------------\n");
1545 if (human_readable_list)
1550 while (ExifFormattedList[i].key)
1554 text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1557 g_fprintf(f, " %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1574 exif_write_item(f, item);
1577 g_fprintf(f, "----------------------------------------------------\n");
1580 gboolean exif_write(ExifData *exif)
1582 log_printf("Not compiled with EXIF write support\n");
1586 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
1588 log_printf("Not compiled with EXIF write support\n");
1593 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
1598 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1603 if (!key) return NULL;
1605 /* convert xmp key to exif key */
1606 if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
1608 if (format == METADATA_FORMATTED)
1612 text = exif_get_formatted_by_key(exif, key, &key_valid);
1613 if (key_valid) return g_list_append(NULL, text);
1616 item = exif_get_item(exif, key);
1617 if (!item) return NULL;
1619 str = exif_item_get_data_as_text_full(item, format);
1621 if (!str) return NULL;
1623 return g_list_append(NULL, str);
1626 typedef struct _UnmapData UnmapData;
1634 static GList *exif_unmap_list = 0;
1636 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1645 if (!exif) return NULL;
1648 fd = open(path, O_RDONLY);
1656 if (fstat(fd, &st) == -1)
1661 map_len = st.st_size;
1662 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1665 if (map_data == MAP_FAILED)
1670 if (format_raw_img_exif_offsets(map_data, map_len, &offset, NULL) && offset)
1674 DEBUG_1("%s: offset %u", path, offset);
1676 *data_len = map_len - offset;
1677 ud = g_new(UnmapData, 1);
1678 ud->ptr = map_data + offset;
1679 ud->map_data = map_data;
1680 ud->map_len = map_len;
1682 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1686 munmap(map_data, map_len);
1691 void exif_free_preview(guchar *buf)
1693 GList *work = exif_unmap_list;
1697 UnmapData *ud = (UnmapData *)work->data;
1700 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1706 g_assert_not_reached();
1709 void exif_init(void)
1714 /* not HAVE_EXIV2 */
1715 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */