2 * Copyright (C) 2003, 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
5 * Authors: Eric Swalens, Quy Tonthat
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * The tags were added with information from the FREE document:
22 * http://www.ba.wakwak.com/~tsuruzoh/Computer/Digicams/exif-e.html
24 * For the official Exif Format, please refer to:
26 * http://www.exif.org/specifications.html (PDF spec sheets)
29 * Additional tag formats should be added to the proper
30 * location in ExifKnownMarkersList[].
32 * Human readable output (that needs additional processing of data to
33 * be useable) can be defined by adding a key to ExifFormattedList[],
34 * then handling that tag in the function exif_get_formatted_by_key().
35 * The human readable formatted keys must begin with the character 'f'.
37 * Unsupported at this time:
42 * Convert data to useable form in the ??_as_text function for:
43 * ComponentsConfiguration
44 * UserComment (convert this to UTF-8?)
45 * Add support for marker tag 0x0000
56 #include <sys/types.h>
62 #include <glib/gprintf.h>
68 #include "jpeg_parser.h"
70 #include "format_raw.h"
71 #include "ui_fileops.h"
75 *-----------------------------------------------------------------------------
77 *-----------------------------------------------------------------------------
80 ExifFormatAttrib ExifFormatList[] = {
81 { EXIF_FORMAT_UNKNOWN, 1, "unknown", "unknown" },
82 { EXIF_FORMAT_BYTE_UNSIGNED, 1, "ubyte", "unsigned byte" },
83 { EXIF_FORMAT_STRING, 1, "string", "string" },
84 { EXIF_FORMAT_SHORT_UNSIGNED, 2, "ushort", "unsigned short" },
85 { EXIF_FORMAT_LONG_UNSIGNED, 4, "ulong", "unsigned long" },
86 { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
87 { EXIF_FORMAT_BYTE, 1, "byte", "byte" },
88 { EXIF_FORMAT_UNDEFINED, 1, "undefined", "undefined" },
89 { EXIF_FORMAT_SHORT, 2, "sshort", "signed short" },
90 { EXIF_FORMAT_LONG, 4, "slong", "signed long" },
91 { EXIF_FORMAT_RATIONAL, 8, "srational", "signed rational" },
92 { EXIF_FORMAT_FLOAT, 4, "float", "float" },
93 { EXIF_FORMAT_DOUBLE, 8, "double", "double" },
97 /* tags that are special, or need special treatment */
98 #define TAG_EXIFOFFSET 0x8769
99 #define TAG_EXIFMAKERNOTE 0x927c
100 #define TAG_GPSOFFSET 0x8825
104 *-----------------------------------------------------------------------------
106 *-----------------------------------------------------------------------------
108 static ExifTextList ExifCompressionList[] = {
109 { 1, "Uncompressed" },
111 { 3, "T4/Group 3 Fax" },
112 { 4, "T6/Group 4 Fax" },
114 { 6, "JPEG (old style)" },
116 { 8, "Adobe Deflate" },
118 { 10, "JBIG Color" },
120 { 32771, "CCIRLEW" },
121 { 32773, "PackBits" },
122 { 32809, "ThunderScan" },
123 { 32895, "IT8CTPAD" },
127 { 32908, "PixasFilm" },
128 { 32909, "PixasLog" },
129 { 32946, "Deflate" },
133 { 34677, "SGILog24" },
134 { 34712, "JPEF 2000" },
135 { 34713, "Nikon NEF Compressed" },
139 static ExifTextList ExifOrientationList[] = {
140 { EXIF_ORIENTATION_UNKNOWN, N_("unknown") },
141 { EXIF_ORIENTATION_TOP_LEFT, N_("top left") },
142 { EXIF_ORIENTATION_TOP_RIGHT, N_("top right") },
143 { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
144 { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
145 { EXIF_ORIENTATION_LEFT_TOP, N_("left top") },
146 { EXIF_ORIENTATION_RIGHT_TOP, N_("right top") },
147 { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
148 { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
152 static ExifTextList ExifUnitList[] = {
153 { EXIF_UNIT_UNKNOWN, N_("unknown") },
154 { EXIF_UNIT_NOUNIT, "" },
155 { EXIF_UNIT_INCH, N_("inch") },
156 { EXIF_UNIT_CENTIMETER, N_("centimeter") },
160 static ExifTextList ExifYCbCrPosList[] = {
166 static ExifTextList ExifMeteringModeList[] = {
167 { 0, N_("unknown") },
168 { 1, N_("average") },
169 { 2, N_("center weighted") },
171 { 4, N_("multi-spot") },
172 { 5, N_("multi-segment") },
173 { 6, N_("partial") },
174 { 255, N_("other") },
178 static ExifTextList ExifExposureProgramList[] = {
179 { 0, N_("not defined") },
182 { 3, N_("aperture") },
183 { 4, N_("shutter") },
184 { 5, N_("creative") },
186 { 7, N_("portrait") },
187 { 8, N_("landscape") },
191 static ExifTextList ExifLightSourceList[] = {
192 { 0, N_("unknown") },
193 { 1, N_("daylight") },
194 { 2, N_("fluorescent") },
195 { 3, N_("tungsten (incandescent)") },
197 { 9, N_("fine weather") },
198 { 10, N_("cloudy weather") },
200 { 12, N_("daylight fluorescent") },
201 { 13, N_("day white fluorescent") },
202 { 14, N_("cool white fluorescent") },
203 { 15, N_("white fluorescent") },
204 { 17, N_("standard light A") },
205 { 18, N_("standard light B") },
206 { 19, N_("standard light C") },
211 { 24, N_("ISO studio tungsten") },
212 { 255, N_("other") },
216 static ExifTextList ExifFlashList[] = {
219 { 5, N_("yes, not detected by strobe") },
220 { 7, N_("yes, detected by strobe") },
224 static ExifTextList ExifColorSpaceList[] = {
226 { 65535,N_("uncalibrated") },
230 static ExifTextList ExifSensorList[] = {
231 { 1, N_("not defined") },
232 { 2, N_("1 chip color area") },
233 { 2, N_("2 chip color area") },
234 { 4, N_("3 chip color area") },
235 { 5, N_("color sequential area") },
236 { 7, N_("trilinear") },
237 { 8, N_("color sequential linear") },
241 static ExifTextList ExifSourceList[] = {
242 { 3, N_("digital still camera") },
246 static ExifTextList ExifSceneList[] = {
247 { 1, N_("direct photo") },
251 static ExifTextList ExifCustRenderList[] = {
257 static ExifTextList ExifExposureModeList[] = {
260 { 2, N_("auto bracket") },
264 static ExifTextList ExifWhiteBalanceList[] = {
270 static ExifTextList ExifSceneCaptureList[] = {
271 { 0, N_("standard") },
272 { 1, N_("landscape") },
273 { 2, N_("portrait") },
274 { 3, N_("night scene") },
278 static ExifTextList ExifGainControlList[] = {
280 { 1, N_("low gain up") },
281 { 2, N_("high gain up") },
282 { 3, N_("low gain down") },
283 { 4, N_("high gain down") },
287 static ExifTextList ExifContrastList[] = {
294 static ExifTextList ExifSaturationList[] = {
301 static ExifTextList ExifSharpnessList[] = {
308 static ExifTextList ExifSubjectRangeList[] = {
309 { 0, N_("unknown") },
312 { 3, N_("distant") },
317 Tag names should match to exiv2 keys, https://www.exiv2.org/metadata.html
318 Tags that don't match are not supported by exiv2 and should not be used anywhere in the code
321 ExifMarker ExifKnownMarkersList[] = {
322 { 0x0100, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageWidth", N_("Image Width"), NULL },
323 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageLength", N_("Image Height"), NULL },
324 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.BitsPerSample", N_("Bits per Sample/Pixel"), NULL },
325 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Compression", N_("Compression"), ExifCompressionList },
326 { 0x010e, EXIF_FORMAT_STRING, -1, "Exif.Image.ImageDescription", N_("Image description"), NULL },
327 { 0x010f, EXIF_FORMAT_STRING, -1, "Exif.Image.Make", N_("Camera make"), NULL },
328 { 0x0110, EXIF_FORMAT_STRING, -1, "Exif.Image.Model", N_("Camera model"), NULL },
329 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Orientation", N_("Orientation"), ExifOrientationList },
330 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.XResolution", N_("X resolution"), NULL },
331 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.YResolution", N_("Y Resolution"), NULL },
332 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.ResolutionUnit", N_("Resolution units"), ExifUnitList },
333 { 0x0131, EXIF_FORMAT_STRING, -1, "Exif.Image.Software", N_("Firmware"), NULL },
334 { 0x0132, EXIF_FORMAT_STRING, 20, "Exif.Image.DateTime", N_("Date"), NULL },
335 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2, "Exif.Image.WhitePoint", N_("White point"), NULL },
336 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), NULL },
337 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), NULL },
338 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.YCbCrPositioning", N_("YCbCr positioning"), ExifYCbCrPosList },
339 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.ReferenceBlackWhite",N_("Black white reference"), NULL },
340 { 0x8298, EXIF_FORMAT_STRING, -1, "Exif.Image.Copyright", N_("Copyright"), NULL },
341 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ExifTag", N_("SubIFD Exif offset"), NULL },
343 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureTime", N_("Exposure time (seconds)"), NULL },
344 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FNumber", N_("FNumber"), NULL },
345 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureProgram", N_("Exposure program"), ExifExposureProgramList },
346 { 0x8824, EXIF_FORMAT_STRING, -1, "Exif.Photo.SpectralSensitivity",N_("Spectral Sensitivity"), NULL },
347 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.ISOSpeedRatings", N_("ISO sensitivity"), NULL },
348 { 0x8828, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.OECF", N_("Optoelectric conversion factor"), NULL },
349 { 0x9000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.ExifVersion", N_("Exif version"), NULL },
350 { 0x9003, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeOriginal", N_("Date original"), NULL },
351 { 0x9004, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeDigitized", N_("Date digitized"), NULL },
352 { 0x9101, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), NULL },
353 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1, "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), NULL },
354 { 0x9201, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), NULL },
355 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ApertureValue", N_("Aperture"), NULL },
356 { 0x9203, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.BrightnessValue", N_("Brightness"), NULL },
357 { 0x9204, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), NULL },
358 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.MaxApertureValue", N_("Maximum aperture"), NULL },
359 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.SubjectDistance", N_("Subject distance"), NULL },
360 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.MeteringMode", N_("Metering mode"), ExifMeteringModeList },
361 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.LightSource", N_("Light source"), ExifLightSourceList },
362 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Flash", N_("Flash"), ExifFlashList },
363 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalLength", N_("Focal length"), NULL },
364 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SubjectArea", N_("Subject area"), NULL },
365 { 0x927c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.MakerNote", N_("MakerNote"), NULL },
366 { 0x9286, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.UserComment", N_("UserComment"), NULL },
367 { 0x9290, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTime", N_("Subsecond time"), NULL },
368 { 0x9291, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), NULL },
369 { 0x9292, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), NULL },
370 { 0xa000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.FlashpixVersion", N_("FlashPix version"), NULL },
371 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ColorSpace", N_("Colorspace"), ExifColorSpaceList },
372 /* ExifImageWidth, ExifImageHeight can also be unsigned short */
373 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelXDimension", N_("Width"), NULL },
374 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelYDimension", N_("Height"), NULL },
375 { 0xa004, EXIF_FORMAT_STRING, -1, "Exif.Photo.RelatedSoundFile", N_("Audio data"), NULL },
376 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifInteroperabilityOffset", N_("ExifR98 extension"), NULL },
377 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", N_("Flash strength"), NULL },
378 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), NULL },
379 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), NULL },
380 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), NULL },
381 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalPlaneResolutionUnit", N_("Pixel density units"), ExifUnitList },
382 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Photo.SubjectLocation", N_("Subject location"), NULL },
383 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", N_("ISO sensitivity"), NULL },
384 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SensingMethod", N_("Sensor type"), ExifSensorList },
385 { 0xa300, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.FileSource", N_("Source type"), ExifSourceList },
386 { 0xa301, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.SceneType", N_("Scene type"), ExifSceneList },
387 { 0xa302, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.CFAPattern", N_("Color filter array pattern"), NULL },
388 /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
389 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.CustomRendered", N_("Render process"), ExifCustRenderList },
390 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureMode", N_("Exposure mode"), ExifExposureModeList },
391 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.WhiteBalance", N_("White balance"), ExifWhiteBalanceList },
392 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.DigitalZoomRatio", N_("Digital zoom ratio"), NULL },
393 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), NULL },
394 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SceneCaptureType", N_("Scene capture type"), ExifSceneCaptureList },
395 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.GainControl", N_("Gain control"), ExifGainControlList },
396 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Contrast", N_("Contrast"), ExifContrastList },
397 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Saturation", N_("Saturation"), ExifSaturationList },
398 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Sharpness", N_("Sharpness"), ExifSharpnessList },
399 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.DeviceSettingDescription",N_("Device setting"), NULL },
400 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SubjectDistanceRange",N_("Subject range"), ExifSubjectRangeList },
401 { 0xa420, EXIF_FORMAT_STRING, -1, "Exif.Photo.ImageUniqueID", N_("Image serial number"), NULL },
402 /* place known, but undocumented or lesser used tags here */
403 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.NewSubfileType", NULL, NULL },
404 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubfileType", NULL, NULL },
405 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3, "Exif.Image.TransferFunction", NULL, NULL },
406 { 0x013b, EXIF_FORMAT_STRING, -1, "Exif.Image.Artist", "Artist", NULL },
407 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Predictor", NULL, NULL },
408 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileWidth", NULL, NULL },
409 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileLength", NULL, NULL },
410 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1, "TileOffsets", NULL, NULL },
411 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1, "TileByteCounts", NULL, NULL },
412 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1, "Exif.Image.SubIFDs", NULL, NULL },
413 { 0x015b, EXIF_FORMAT_UNDEFINED, -1, "JPEGTables", NULL, NULL },
414 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Image.CFARepeatPatternDim", NULL, NULL },
415 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Exif.Image.CFAPattern", NULL, NULL },
416 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.BatteryLevel", NULL, NULL },
417 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1, "IPTC/NAA", NULL, NULL },
418 { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL },
419 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1, "GPSInfo", "SubIFD GPS offset", NULL },
420 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Interlace", NULL, NULL },
421 { 0x882a, EXIF_FORMAT_SHORT, 1, "TimeZoneOffset", NULL, NULL },
422 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SelfTimerMode", NULL, NULL },
423 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", NULL, NULL },
424 { 0x920c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.SpatialFrequencyResponse", NULL, NULL },
425 { 0x920d, EXIF_FORMAT_UNDEFINED, -1, "Noise", NULL, NULL },
426 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1, "ImageNumber", NULL, NULL },
427 { 0x9212, EXIF_FORMAT_STRING, 1, "SecurityClassification", NULL, NULL },
428 { 0x9213, EXIF_FORMAT_STRING, -1, "ImageHistory", NULL, NULL },
429 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", NULL, NULL },
430 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4, "TIFF/EPStandardID", NULL, NULL },
435 ExifMarker ExifKnownGPSInfoMarkersList[] = {
436 /* The following do not work at the moment as the tag value 0x0000 has a
437 * special meaning. */
438 /* { 0x0000, EXIF_FORMAT_BYTE, -1, "Exif.GPSInfo.GPSVersionID", NULL, NULL }, */
439 { 0x0001, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLatitudeRef", NULL, NULL },
440 { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", NULL, NULL },
441 { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", NULL, NULL },
442 { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", NULL, NULL },
443 { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", NULL, NULL },
444 { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", NULL, NULL },
445 { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", NULL, NULL },
446 { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", NULL, NULL },
447 { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", NULL, NULL },
448 { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", NULL, NULL },
449 { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", NULL, NULL },
450 { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", NULL, NULL },
451 { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", NULL, NULL },
452 { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", NULL, NULL },
453 { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", NULL, NULL },
454 { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", NULL, NULL },
455 { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", NULL, NULL },
456 { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", NULL, NULL },
457 { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", NULL, NULL },
458 { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", NULL, NULL },
459 { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", NULL, NULL },
460 { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", NULL, NULL },
461 { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", NULL, NULL },
462 { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", NULL, NULL },
463 { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", NULL, NULL },
464 { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", NULL, NULL },
465 { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", NULL, NULL },
466 { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", NULL, NULL },
467 { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", NULL, NULL },
468 { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", NULL, NULL },
473 ExifMarker ExifUnknownMarkersList[] = {
474 { 0x0000, EXIF_FORMAT_UNKNOWN, 0, "unknown", NULL, NULL },
475 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1, "unknown", NULL, NULL },
476 { 0x0000, EXIF_FORMAT_STRING, -1, "unknown", NULL, NULL },
477 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1, "unknown", NULL, NULL },
478 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1, "unknown", NULL, NULL },
479 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "unknown", NULL, NULL },
480 { 0x0000, EXIF_FORMAT_BYTE, -1, "unknown", NULL, NULL },
481 { 0x0000, EXIF_FORMAT_UNDEFINED, -1, "unknown", NULL, NULL },
482 { 0x0000, EXIF_FORMAT_SHORT, -1, "unknown", NULL, NULL },
483 { 0x0000, EXIF_FORMAT_LONG, -1, "unknown", NULL, NULL },
484 { 0x0000, EXIF_FORMAT_RATIONAL, -1, "unknown", NULL, NULL },
485 { 0x0000, EXIF_FORMAT_FLOAT, -1, "unknown", NULL, NULL },
486 { 0x0000, EXIF_FORMAT_DOUBLE, -1, "unknown", NULL, NULL },
489 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
492 *-----------------------------------------------------------------------------
494 *-----------------------------------------------------------------------------
497 ExifItem *exif_item_new(ExifFormatType format, guint tag,
498 guint elements, const ExifMarker *marker)
502 item = g_new0(ExifItem, 1);
503 item->format = format;
505 item->marker = marker;
506 item->elements = elements;
510 case EXIF_FORMAT_UNKNOWN:
511 /* unknown, data is NULL */
514 case EXIF_FORMAT_BYTE_UNSIGNED:
515 item->data_len = sizeof(gchar) * elements;
517 case EXIF_FORMAT_STRING:
518 item->data_len = sizeof(gchar) * elements;
520 case EXIF_FORMAT_SHORT_UNSIGNED:
521 item->data_len = sizeof(guint16) * elements;
523 case EXIF_FORMAT_LONG_UNSIGNED:
524 item->data_len = sizeof(guint32) * elements;
526 case EXIF_FORMAT_RATIONAL_UNSIGNED:
527 item->data_len = sizeof(ExifRational) * elements;
529 case EXIF_FORMAT_BYTE:
530 item->data_len = sizeof(gchar) * elements;
532 case EXIF_FORMAT_UNDEFINED:
533 item->data_len = sizeof(gchar) * elements;
535 case EXIF_FORMAT_SHORT:
536 item->data_len = sizeof(gint16) * elements;
538 case EXIF_FORMAT_LONG:
539 item->data_len = sizeof(gint32) * elements;
541 case EXIF_FORMAT_RATIONAL:
542 item->data_len = sizeof(ExifRational) * elements;
544 case EXIF_FORMAT_FLOAT:
545 item->data_len = sizeof(float) * elements;
547 case EXIF_FORMAT_DOUBLE:
548 item->data_len = sizeof(gdouble) * elements;
552 item->data = g_malloc0(item->data_len);
557 static void exif_item_free(ExifItem *item)
565 gchar *exif_item_get_tag_name(ExifItem *item)
567 if (!item || !item->marker) return NULL;
568 return g_strdup(item->marker->key);
571 guint exif_item_get_tag_id(ExifItem *item)
577 guint exif_item_get_elements(ExifItem *item)
580 return item->elements;
583 gchar *exif_item_get_data(ExifItem *item, guint *data_len)
586 *data_len = item->data_len;
587 return g_memdup(item->data, item->data_len);
590 guint exif_item_get_format_id(ExifItem *item)
592 if (!item) return EXIF_FORMAT_UNKNOWN;
597 gchar *exif_item_get_description(ExifItem *item)
599 if (!item || !item->marker) return NULL;
600 return g_strdup(_(item->marker->description));
603 const gchar *exif_item_get_format_name(ExifItem *item, gboolean brief)
605 if (!item || !item->marker) return NULL;
606 return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
609 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
613 for (i = 0 ; i < ne; i++)
615 guchar c = ((gchar *)data)[i];
616 if (c < 32 || c > 127) c = '.';
617 g_string_append_printf(string, "%c", c);
619 string = g_string_append(string, " : ");
620 for (i = 0 ; i < ne; i++)
638 g_string_append_printf(string, "%s%02x", spacer, ((gchar *)data)[i]);
645 gchar *exif_text_list_find_value(ExifTextList *list, guint value)
647 gchar *result = NULL;
651 while (!result && list[i].value >= 0)
653 if (value == (guint) list[i].value) result = g_strdup(_(list[i].description));
656 if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
663 *-------------------------------------------------------------------
665 *-------------------------------------------------------------------
668 /* note: the align_buf is used to avoid alignment issues (on sparc) */
670 guint16 exif_byte_get_int16(guchar *f, ExifByteOrder bo)
674 memcpy(&align_buf, f, sizeof(guint16));
676 if (bo == EXIF_BYTE_ORDER_INTEL)
677 return GUINT16_FROM_LE(align_buf);
679 return GUINT16_FROM_BE(align_buf);
682 guint32 exif_byte_get_int32(guchar *f, ExifByteOrder bo)
686 memcpy(&align_buf, f, sizeof(guint32));
688 if (bo == EXIF_BYTE_ORDER_INTEL)
689 return GUINT32_FROM_LE(align_buf);
691 return GUINT32_FROM_BE(align_buf);
694 void exif_byte_put_int16(guchar *f, guint16 n, ExifByteOrder bo)
698 if (bo == EXIF_BYTE_ORDER_INTEL)
700 align_buf = GUINT16_TO_LE(n);
704 align_buf = GUINT16_TO_BE(n);
707 memcpy(f, &align_buf, sizeof(guint16));
710 void exif_byte_put_int32(guchar *f, guint32 n, ExifByteOrder bo)
714 if (bo == EXIF_BYTE_ORDER_INTEL)
716 align_buf = GUINT32_TO_LE(n);
720 align_buf = GUINT32_TO_BE(n);
723 memcpy(f, &align_buf, sizeof(guint32));
728 *-------------------------------------------------------------------
730 *-------------------------------------------------------------------
733 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list)
737 if (!list) return NULL;
739 while (list[i].tag != 0 && list[i].tag != tag)
744 return (list[i].tag == 0 ? NULL : &list[i]);
747 static void rational_from_data(ExifRational *r, gpointer src, ExifByteOrder bo)
749 r->num = exif_byte_get_int32(src, bo);
750 r->den = exif_byte_get_int32(src + sizeof(guint32), bo);
753 /* src_format and item->format must be compatible
754 * and not overrun src or item->data.
756 void exif_item_copy_data(ExifItem *item, gpointer src, guint len,
757 ExifFormatType src_format, ExifByteOrder bo)
764 bs = ExifFormatList[item->format].size;
769 ExifFormatList[src_format].size * ne > len)
771 gchar *tag = exif_item_get_tag_name(item);
772 log_printf("exif tag %s data size mismatch\n", tag);
777 switch (item->format)
779 case EXIF_FORMAT_UNKNOWN:
781 case EXIF_FORMAT_BYTE_UNSIGNED:
782 case EXIF_FORMAT_BYTE:
783 case EXIF_FORMAT_UNDEFINED:
784 memcpy(dest, src, len);
786 case EXIF_FORMAT_STRING:
787 memcpy(dest, src, len);
788 /* string is NULL terminated, make sure this is true */
789 if (((gchar *)dest)[len - 1] != '\0') ((gchar *)dest)[len - 1] = '\0';
791 case EXIF_FORMAT_SHORT_UNSIGNED:
792 case EXIF_FORMAT_SHORT:
793 for (i = 0; i < ne; i++)
795 ((guint16 *)dest)[i] = exif_byte_get_int16(src + i * bs, bo);
798 case EXIF_FORMAT_LONG_UNSIGNED:
799 case EXIF_FORMAT_LONG:
800 if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
801 src_format == EXIF_FORMAT_SHORT)
803 /* a short fits into a long, so allow it */
806 ss = ExifFormatList[src_format].size;
807 for (i = 0; i < ne; i++)
809 ((gint32 *)dest)[i] =
810 (gint32)exif_byte_get_int16(src + i * ss, bo);
815 for (i = 0; i < ne; i++)
817 ((gint32 *)dest)[i] =
818 exif_byte_get_int32(src + i * bs, bo);
822 case EXIF_FORMAT_RATIONAL_UNSIGNED:
823 case EXIF_FORMAT_RATIONAL:
824 for (i = 0; i < ne; i++)
826 rational_from_data(&((ExifRational *)dest)[i], src + i * bs, bo);
829 case EXIF_FORMAT_FLOAT:
830 for (i = 0; i < ne; i++)
832 ((float *)dest)[i] = exif_byte_get_int32(src + i * bs, bo);
835 case EXIF_FORMAT_DOUBLE:
836 for (i = 0; i < ne; i++)
840 rational_from_data(&r, src + i * bs, bo);
841 if (r.den) ((gdouble *)dest)[i] = (gdouble)r.num / r.den;
847 static gint exif_parse_IFD_entry(ExifData *exif, guchar *tiff, guint offset,
848 guint size, ExifByteOrder bo,
850 const ExifMarker *list)
858 const ExifMarker *marker;
861 tag = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_TAG, bo);
862 format = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
863 count = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_COUNT, bo);
864 data_val = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_DATA, bo);
866 /* Check tag type. If it does not match, either the format is wrong,
867 * either it is a unknown tag; so it is not really an error.
869 marker = exif_marker_from_tag(tag, list);
872 if (format >= EXIF_FORMAT_COUNT)
874 log_printf("warning: exif tag 0x%4x has invalid format %d\n", tag, format);
877 /* allow non recognized tags to be displayed */
878 marker = &ExifUnknownMarkersList[format];
880 if (marker->format != format)
882 /* Some cameras got mixed up signed/unsigned_rational
883 * eg KODAK DC4800 on object_distance tag
885 * FIXME: what exactly is this test trying to do?
886 * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
888 if (!(marker->format == EXIF_FORMAT_RATIONAL_UNSIGNED && format == EXIF_FORMAT_RATIONAL) &&
889 !(marker->format == EXIF_FORMAT_RATIONAL && format == EXIF_FORMAT_RATIONAL_UNSIGNED) &&
890 /* short fits into a long so allow this mismatch
891 * as well (some tags allowed to be unsigned short _or_ unsigned long)
893 !(marker->format == EXIF_FORMAT_LONG_UNSIGNED && format == EXIF_FORMAT_SHORT_UNSIGNED) )
895 if (format < EXIF_FORMAT_COUNT)
897 log_printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
898 marker->key, ExifFormatList[format].short_name,
899 ExifFormatList[marker->format].short_name);
903 log_printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
904 marker->key, format, marker->format,
905 ExifFormatList[marker->format].short_name);
911 /* Where is the data, is it available?
913 if (marker->components > 0 && (guint) marker->components != count)
915 log_printf("warning: exif tag %s has %d elements, exif spec requests %d\n",
916 marker->key, count, marker->components);
919 data_length = ExifFormatList[marker->format].size * count;
922 data_offset = data_val;
923 if (size < data_offset || size < data_offset + data_length)
925 log_printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
931 data_offset = offset + EXIF_TIFD_OFFSET_DATA;
934 item = exif_item_new(marker->format, tag, count, marker);
935 exif_item_copy_data(item, tiff + data_offset, data_length, format, bo);
936 exif->items = g_list_prepend(exif->items, item);
938 if (list == ExifKnownMarkersList)
943 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, list);
946 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, ExifKnownGPSInfoMarkersList);
948 case TAG_EXIFMAKERNOTE:
949 format_exif_makernote_parse(exif, tiff, data_val, size, bo);
957 gint exif_parse_IFD_table(ExifData *exif,
958 guchar *tiff, guint offset,
959 guint size, ExifByteOrder bo,
961 const ExifMarker *list)
966 /* limit damage from infinite loops */
967 if (level > EXIF_TIFF_MAX_LEVELS) return -1;
969 /* We should be able to read number of entries in IFD0) */
970 if (size < offset + 2) return -1;
972 count = exif_byte_get_int16(tiff + offset, bo);
975 /* Entries and next IFD offset must be readable */
976 if (size < offset + count * EXIF_TIFD_SIZE + 4) return -1;
978 for (i = 0; i < count; i++)
980 exif_parse_IFD_entry(exif, tiff, offset + i * EXIF_TIFD_SIZE, size, bo, level, list);
987 *-------------------------------------------------------------------
989 *-------------------------------------------------------------------
992 gint exif_tiff_directory_offset(guchar *data, const guint len,
993 guint *offset, ExifByteOrder *bo)
995 if (len < 8) return FALSE;
997 if (memcmp(data, "II", 2) == 0)
999 *bo = EXIF_BYTE_ORDER_INTEL;
1001 else if (memcmp(data, "MM", 2) == 0)
1003 *bo = EXIF_BYTE_ORDER_MOTOROLA;
1010 if (exif_byte_get_int16(data + 2, *bo) != 0x002A)
1015 *offset = exif_byte_get_int32(data + 4, *bo);
1017 return (*offset < len);
1020 gint exif_tiff_parse(ExifData *exif, guchar *tiff, guint size, ExifMarker *list)
1025 if (!exif_tiff_directory_offset(tiff, size, &offset, &bo)) return -1;
1027 return exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, list);
1032 *-------------------------------------------------------------------
1034 *-------------------------------------------------------------------
1037 #define JPEG_MARKER 0xFF
1038 #define JPEG_MARKER_SOI 0xD8
1039 #define JPEG_MARKER_EOI 0xD9
1040 #define JPEG_MARKER_APP1 0xE1
1041 #define JPEG_MARKER_APP2 0xE2
1043 /* jpeg container format:
1044 all data markers start with 0XFF
1045 2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
1046 4 byte long data segment markers in format: 0xFFTTSSSSNNN...
1047 FF: 1 byte standard marker identifier
1048 TT: 1 byte data type
1049 SSSS: 2 bytes in Motorola byte alignment for length of the data.
1050 This value includes these 2 bytes in the count, making actual
1051 length of NN... == SSSS - 2.
1052 NNN.: the data in this segment
1054 static ExifMarker jpeg_color_marker = { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL };
1056 void exif_add_jpeg_color_profile(ExifData *exif, guchar *cp_data, guint cp_length)
1058 ExifItem *item = exif_item_new(jpeg_color_marker.format, jpeg_color_marker.tag, 1,
1059 &jpeg_color_marker);
1061 item->data = cp_data;
1062 item->elements = cp_length;
1063 item->data_len = cp_length;
1064 exif->items = g_list_prepend(exif->items, item);
1068 static gint exif_jpeg_parse(ExifData *exif,
1069 guchar *data, guint size,
1072 guint seg_offset = 0;
1073 guint seg_length = 0;
1077 memcmp(data, "\xFF\xD8", 2) != 0)
1082 if (jpeg_segment_find(data, size, JPEG_MARKER_APP1,
1084 &seg_offset, &seg_length))
1086 res = exif_tiff_parse(exif, data + seg_offset + 6, seg_length - 6, list);
1089 if (exif_jpeg_parse_color(exif, data, size))
1097 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1099 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1100 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1101 return (guchar *) exif_item_get_data(prof_item, data_len);
1106 gchar* exif_get_image_comment(FileData* fd)
1108 log_printf("%s", _("Can't get image comment: not compiled with Exiv2.\n"));
1109 return g_strdup("");
1112 void exif_set_image_comment(FileData* fd, const gchar* comment)
1114 log_printf("%s", _("Can't set image comment: not compiled with Exiv2.\n"));
1119 *-------------------------------------------------------------------
1121 *-------------------------------------------------------------------
1125 ExifItem *exif_get_first_item(ExifData *exif)
1129 ExifItem *ret = (ExifItem *)exif->items->data;
1130 exif->current = exif->items->next;
1133 exif->current = NULL;
1137 ExifItem *exif_get_next_item(ExifData *exif)
1141 ExifItem *ret = (ExifItem *)exif->current->data;
1142 exif->current = exif->current->next;
1148 static gint map_file(const gchar *path, void **mapping, gint *size)
1153 fd = open(path, O_RDONLY);
1160 if (fstat(fd, &fs) == -1)
1169 *mapping = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1170 if (*mapping == MAP_FAILED)
1181 static gint unmap_file(gpointer mapping, gint size)
1183 if (munmap(mapping, size) == -1)
1192 ExifData *exif_get_original(ExifData *processed)
1197 void exif_free(ExifData *exif)
1206 ExifItem *item = work->data;
1208 exif_item_free(item);
1211 g_list_free(exif->items);
1216 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
1223 if (!path) return NULL;
1225 pathl = path_from_utf8(path);
1226 if (map_file(pathl, &f, &size) == -1)
1233 exif = g_new0(ExifData, 1);
1234 exif->path = g_strdup(path);
1236 res = exif_jpeg_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1239 res = exif_tiff_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1244 FormatRawExifType exif_type;
1245 FormatRawExifParseFunc exif_parse_func;
1248 exif_type = format_raw_exif_offset(f, size, &offset, &exif_parse_func);
1251 case FORMAT_RAW_EXIF_NONE:
1254 case FORMAT_RAW_EXIF_TIFF:
1255 res = exif_tiff_parse(exif, (guchar *)f + offset, size - offset,
1256 ExifKnownMarkersList);
1258 case FORMAT_RAW_EXIF_JPEG:
1259 res = exif_jpeg_parse(exif, (guchar *)f + offset, size - offset,
1260 ExifKnownMarkersList);
1262 case FORMAT_RAW_EXIF_IFD_II:
1263 case FORMAT_RAW_EXIF_IFD_MM:
1264 res = exif_parse_IFD_table(exif, (guchar *)f, offset, size - offset,
1265 (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1266 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1267 0, ExifKnownMarkersList);
1269 case FORMAT_RAW_EXIF_PROPRIETARY:
1270 if (exif_parse_func)
1272 res = exif_parse_func((guchar *)f + offset, size - offset, exif);
1284 unmap_file(f, size);
1286 if (exif) exif->items = g_list_reverse(exif->items);
1291 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1295 if (!key) return NULL;
1304 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1309 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1312 static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
1314 const ExifMarker *marker;
1321 if (!item) return NULL;
1323 marker = item->marker;
1324 if (!marker) return NULL;
1327 ne = item->elements;
1328 if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1329 string = g_string_new("");
1330 switch (item->format)
1332 case EXIF_FORMAT_UNKNOWN:
1334 case EXIF_FORMAT_BYTE_UNSIGNED:
1335 case EXIF_FORMAT_BYTE:
1336 case EXIF_FORMAT_UNDEFINED:
1337 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1342 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1343 item->format == EXIF_FORMAT_UNDEFINED)
1345 val = ((guchar *)data)[0];
1349 val = (guchar)(((gchar *)data)[0]);
1352 result = exif_text_list_find_value(marker->list, (guint)val);
1353 string = g_string_append(string, result);
1358 string = string_append_raw_bytes(string, data, ne);
1361 case EXIF_FORMAT_STRING:
1362 if (item->data) string = g_string_append(string, (gchar *)(item->data));
1364 case EXIF_FORMAT_SHORT_UNSIGNED:
1365 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1369 result = exif_text_list_find_value(marker->list, ((guint16 *)data)[0]);
1370 string = g_string_append(string, result);
1373 else for (i = 0; i < ne; i++)
1375 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1376 ((guint16 *)data)[i]);
1379 case EXIF_FORMAT_LONG_UNSIGNED:
1380 for (i = 0; i < ne; i++)
1382 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1383 (gulong)((guint32 *)data)[i]);
1386 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1387 for (i = 0; i < ne; i++)
1391 r = &((ExifRational *)data)[i];
1392 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1393 (gulong)r->num, (gulong)r->den);
1396 case EXIF_FORMAT_SHORT:
1397 for (i = 0; i < ne; i++)
1399 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1400 ((gint16 *)data)[i]);
1403 case EXIF_FORMAT_LONG:
1404 for (i = 0; i < ne; i++)
1406 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1407 (glong)((gint32 *)data)[i]);
1410 case EXIF_FORMAT_RATIONAL:
1411 for (i = 0; i < ne; i++)
1415 r = &((ExifRational *)data)[i];
1416 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1417 (glong)r->num, (glong)r->den);
1420 case EXIF_FORMAT_FLOAT:
1421 for (i = 0; i < ne; i++)
1423 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1424 ((float *)data)[i]);
1427 case EXIF_FORMAT_DOUBLE:
1428 for (i = 0; i < ne; i++)
1430 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1431 ((gdouble *)data)[i]);
1436 if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1437 item->format != EXIF_FORMAT_STRING)
1439 g_string_append(string, " ...");
1443 g_string_free(string, FALSE);
1448 gchar *exif_item_get_string(ExifItem *item, gint idx)
1450 return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
1453 gchar *exif_item_get_data_as_text(ExifItem *item)
1455 return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
1458 gint exif_item_get_integer(ExifItem *item, gint *value)
1460 if (!item) return FALSE;
1461 if (!item->elements) return FALSE;
1463 switch (item->format)
1465 case EXIF_FORMAT_SHORT:
1466 *value = (gint)(((gint16 *)(item->data))[0]);
1469 case EXIF_FORMAT_SHORT_UNSIGNED:
1470 *value = (gint)(((guint16 *)(item->data))[0]);
1473 case EXIF_FORMAT_LONG:
1474 *value = (gint)(((gint32 *)(item->data))[0]);
1477 case EXIF_FORMAT_LONG_UNSIGNED: /**< @FIXME overflow possible */
1478 *value = (gint)(((guint32 *)(item->data))[0]);
1481 /* all other type return FALSE */
1488 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1490 if (!item) return NULL;
1491 if (n >= item->elements) return NULL;
1493 if (item->format == EXIF_FORMAT_RATIONAL ||
1494 item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1496 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1497 return &((ExifRational *)(item->data))[n];
1503 gchar *exif_get_tag_description_by_key(const gchar *key)
1507 if (!key) return NULL;
1510 while (ExifKnownMarkersList[i].tag > 0)
1512 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1517 while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1519 if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
1526 static void exif_write_item(FILE *f, ExifItem *item)
1530 text = exif_item_get_data_as_text(item);
1533 gchar *tag = exif_item_get_tag_name(item);
1534 g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1541 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1543 if (!f || !exif) return;
1545 g_fprintf(f, " tag format key value\n");
1546 g_fprintf(f, "----------------------------------------------------\n");
1548 if (human_readable_list)
1553 while (ExifFormattedList[i].key)
1557 text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1560 g_fprintf(f, " %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1577 exif_write_item(f, item);
1580 g_fprintf(f, "----------------------------------------------------\n");
1583 gboolean exif_write(ExifData *exif)
1585 log_printf("Not compiled with EXIF write support\n");
1589 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
1591 log_printf("Not compiled with EXIF write support\n");
1596 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
1601 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1606 if (!key) return NULL;
1608 /* convert xmp key to exif key */
1609 if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
1611 if (format == METADATA_FORMATTED)
1615 text = exif_get_formatted_by_key(exif, key, &key_valid);
1616 if (key_valid) return g_list_append(NULL, text);
1619 item = exif_get_item(exif, key);
1620 if (!item) return NULL;
1622 str = exif_item_get_data_as_text_full(item, format);
1624 if (!str) return NULL;
1626 return g_list_append(NULL, str);
1629 typedef struct _UnmapData UnmapData;
1637 static GList *exif_unmap_list = 0;
1639 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1648 if (!exif) return NULL;
1651 fd = open(path, O_RDONLY);
1659 if (fstat(fd, &st) == -1)
1664 map_len = st.st_size;
1665 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1668 if (map_data == MAP_FAILED)
1673 if (format_raw_img_exif_offsets(map_data, map_len, &offset, NULL) && offset)
1677 DEBUG_1("%s: offset %u", path, offset);
1679 *data_len = map_len - offset;
1680 ud = g_new(UnmapData, 1);
1681 ud->ptr = map_data + offset;
1682 ud->map_data = map_data;
1683 ud->map_len = map_len;
1685 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1689 munmap(map_data, map_len);
1694 void exif_free_preview(guchar *buf)
1696 GList *work = exif_unmap_list;
1700 UnmapData *ud = (UnmapData *)work->data;
1703 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1709 g_assert_not_reached();
1712 void exif_init(void)
1717 /* not HAVE_EXIV2 */
1718 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */