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
59 #include <glib/gprintf.h>
63 #include "format-raw.h"
65 #include "jpeg-parser.h"
67 #include "ui-fileops.h"
72 *-----------------------------------------------------------------------------
74 *-----------------------------------------------------------------------------
77 ExifFormatAttrib ExifFormatList[] = {
78 { EXIF_FORMAT_UNKNOWN, 1, "unknown", "unknown" },
79 { EXIF_FORMAT_BYTE_UNSIGNED, 1, "ubyte", "unsigned byte" },
80 { EXIF_FORMAT_STRING, 1, "string", "string" },
81 { EXIF_FORMAT_SHORT_UNSIGNED, 2, "ushort", "unsigned short" },
82 { EXIF_FORMAT_LONG_UNSIGNED, 4, "ulong", "unsigned long" },
83 { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
84 { EXIF_FORMAT_BYTE, 1, "byte", "byte" },
85 { EXIF_FORMAT_UNDEFINED, 1, "undefined", "undefined" },
86 { EXIF_FORMAT_SHORT, 2, "sshort", "signed short" },
87 { EXIF_FORMAT_LONG, 4, "slong", "signed long" },
88 { EXIF_FORMAT_RATIONAL, 8, "srational", "signed rational" },
89 { EXIF_FORMAT_FLOAT, 4, "float", "float" },
90 { EXIF_FORMAT_DOUBLE, 8, "double", "double" },
91 { static_cast<ExifFormatType>(-1), 0, nullptr, nullptr }
94 /* tags that are special, or need special treatment */
95 #define TAG_EXIFOFFSET 0x8769
96 #define TAG_EXIFMAKERNOTE 0x927c
97 #define TAG_GPSOFFSET 0x8825
101 *-----------------------------------------------------------------------------
103 *-----------------------------------------------------------------------------
105 static ExifTextList ExifCompressionList[] = {
106 { 1, "Uncompressed" },
108 { 3, "T4/Group 3 Fax" },
109 { 4, "T6/Group 4 Fax" },
111 { 6, "JPEG (old style)" },
113 { 8, "Adobe Deflate" },
115 { 10, "JBIG Color" },
117 { 32771, "CCIRLEW" },
118 { 32773, "PackBits" },
119 { 32809, "ThunderScan" },
120 { 32895, "IT8CTPAD" },
124 { 32908, "PixasFilm" },
125 { 32909, "PixasLog" },
126 { 32946, "Deflate" },
130 { 34677, "SGILog24" },
131 { 34712, "JPEF 2000" },
132 { 34713, "Nikon NEF Compressed" },
136 static ExifTextList ExifOrientationList[] = {
137 { EXIF_ORIENTATION_UNKNOWN, N_("unknown") },
138 { EXIF_ORIENTATION_TOP_LEFT, N_("top left") },
139 { EXIF_ORIENTATION_TOP_RIGHT, N_("top right") },
140 { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
141 { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
142 { EXIF_ORIENTATION_LEFT_TOP, N_("left top") },
143 { EXIF_ORIENTATION_RIGHT_TOP, N_("right top") },
144 { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
145 { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
149 static ExifTextList ExifUnitList[] = {
150 { EXIF_UNIT_UNKNOWN, N_("unknown") },
151 { EXIF_UNIT_NOUNIT, "" },
152 { EXIF_UNIT_INCH, N_("inch") },
153 { EXIF_UNIT_CENTIMETER, N_("centimeter") },
157 static ExifTextList ExifYCbCrPosList[] = {
163 static ExifTextList ExifMeteringModeList[] = {
164 { 0, N_("unknown") },
165 { 1, N_("average") },
166 { 2, N_("center weighted") },
168 { 4, N_("multi-spot") },
169 { 5, N_("multi-segment") },
170 { 6, N_("partial") },
171 { 255, N_("other") },
175 static ExifTextList ExifExposureProgramList[] = {
176 { 0, N_("not defined") },
179 { 3, N_("aperture") },
180 { 4, N_("shutter") },
181 { 5, N_("creative") },
183 { 7, N_("portrait") },
184 { 8, N_("landscape") },
188 static ExifTextList ExifLightSourceList[] = {
189 { 0, N_("unknown") },
190 { 1, N_("daylight") },
191 { 2, N_("fluorescent") },
192 { 3, N_("tungsten (incandescent)") },
194 { 9, N_("fine weather") },
195 { 10, N_("cloudy weather") },
197 { 12, N_("daylight fluorescent") },
198 { 13, N_("day white fluorescent") },
199 { 14, N_("cool white fluorescent") },
200 { 15, N_("white fluorescent") },
201 { 17, N_("standard light A") },
202 { 18, N_("standard light B") },
203 { 19, N_("standard light C") },
208 { 24, N_("ISO studio tungsten") },
209 { 255, N_("other") },
213 static ExifTextList ExifFlashList[] = {
216 { 5, N_("yes, not detected by strobe") },
217 { 7, N_("yes, detected by strobe") },
221 static ExifTextList ExifColorSpaceList[] = {
223 { 65535,N_("uncalibrated") },
227 static ExifTextList ExifSensorList[] = {
228 { 1, N_("not defined") },
229 { 2, N_("1 chip color area") },
230 { 2, N_("2 chip color area") },
231 { 4, N_("3 chip color area") },
232 { 5, N_("color sequential area") },
233 { 7, N_("trilinear") },
234 { 8, N_("color sequential linear") },
238 static ExifTextList ExifSourceList[] = {
239 { 3, N_("digital still camera") },
243 static ExifTextList ExifSceneList[] = {
244 { 1, N_("direct photo") },
248 static ExifTextList ExifCustRenderList[] = {
254 static ExifTextList ExifExposureModeList[] = {
257 { 2, N_("auto bracket") },
261 static ExifTextList ExifWhiteBalanceList[] = {
267 static ExifTextList ExifSceneCaptureList[] = {
268 { 0, N_("standard") },
269 { 1, N_("landscape") },
270 { 2, N_("portrait") },
271 { 3, N_("night scene") },
275 static ExifTextList ExifGainControlList[] = {
277 { 1, N_("low gain up") },
278 { 2, N_("high gain up") },
279 { 3, N_("low gain down") },
280 { 4, N_("high gain down") },
284 static ExifTextList ExifContrastList[] = {
291 static ExifTextList ExifSaturationList[] = {
298 static ExifTextList ExifSharpnessList[] = {
305 static ExifTextList ExifSubjectRangeList[] = {
306 { 0, N_("unknown") },
309 { 3, N_("distant") },
314 Tag names should match to exiv2 keys, https://www.exiv2.org/metadata.html
315 Tags that don't match are not supported by exiv2 and should not be used anywhere in the code
318 ExifMarker ExifKnownMarkersList[] = {
319 { 0x0100, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageWidth", N_("Image Width"), nullptr },
320 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageLength", N_("Image Height"), nullptr },
321 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.BitsPerSample", N_("Bits per Sample/Pixel"), nullptr },
322 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Compression", N_("Compression"), ExifCompressionList },
323 { 0x010e, EXIF_FORMAT_STRING, -1, "Exif.Image.ImageDescription", N_("Image description"), nullptr },
324 { 0x010f, EXIF_FORMAT_STRING, -1, "Exif.Image.Make", N_("Camera make"), nullptr },
325 { 0x0110, EXIF_FORMAT_STRING, -1, "Exif.Image.Model", N_("Camera model"), nullptr },
326 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Orientation", N_("Orientation"), ExifOrientationList },
327 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.XResolution", N_("X resolution"), nullptr },
328 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.YResolution", N_("Y Resolution"), nullptr },
329 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.ResolutionUnit", N_("Resolution units"), ExifUnitList },
330 { 0x0131, EXIF_FORMAT_STRING, -1, "Exif.Image.Software", N_("Firmware"), nullptr },
331 { 0x0132, EXIF_FORMAT_STRING, 20, "Exif.Image.DateTime", N_("Date"), nullptr },
332 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2, "Exif.Image.WhitePoint", N_("White point"), nullptr },
333 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), nullptr },
334 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), nullptr },
335 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.YCbCrPositioning", N_("YCbCr positioning"), ExifYCbCrPosList },
336 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.ReferenceBlackWhite",N_("Black white reference"), nullptr },
337 { 0x8298, EXIF_FORMAT_STRING, -1, "Exif.Image.Copyright", N_("Copyright"), nullptr },
338 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ExifTag", N_("SubIFD Exif offset"), nullptr },
340 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureTime", N_("Exposure time (seconds)"), nullptr },
341 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FNumber", N_("FNumber"), nullptr },
342 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureProgram", N_("Exposure program"), ExifExposureProgramList },
343 { 0x8824, EXIF_FORMAT_STRING, -1, "Exif.Photo.SpectralSensitivity",N_("Spectral Sensitivity"), nullptr },
344 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.ISOSpeedRatings", N_("ISO sensitivity"), nullptr },
345 { 0x8828, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.OECF", N_("Optoelectric conversion factor"), nullptr },
346 { 0x9000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.ExifVersion", N_("Exif version"), nullptr },
347 { 0x9003, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeOriginal", N_("Date original"), nullptr },
348 { 0x9004, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeDigitized", N_("Date digitized"), nullptr },
349 { 0x9101, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), nullptr },
350 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1, "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), nullptr },
351 { 0x9201, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), nullptr },
352 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ApertureValue", N_("Aperture"), nullptr },
353 { 0x9203, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.BrightnessValue", N_("Brightness"), nullptr },
354 { 0x9204, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), nullptr },
355 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.MaxApertureValue", N_("Maximum aperture"), nullptr },
356 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.SubjectDistance", N_("Subject distance"), nullptr },
357 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.MeteringMode", N_("Metering mode"), ExifMeteringModeList },
358 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.LightSource", N_("Light source"), ExifLightSourceList },
359 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Flash", N_("Flash"), ExifFlashList },
360 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalLength", N_("Focal length"), nullptr },
361 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SubjectArea", N_("Subject area"), nullptr },
362 { 0x927c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.MakerNote", N_("MakerNote"), nullptr },
363 { 0x9286, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.UserComment", N_("UserComment"), nullptr },
364 { 0x9290, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTime", N_("Subsecond time"), nullptr },
365 { 0x9291, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), nullptr },
366 { 0x9292, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), nullptr },
367 { 0xa000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.FlashpixVersion", N_("FlashPix version"), nullptr },
368 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ColorSpace", N_("Colorspace"), ExifColorSpaceList },
369 /* ExifImageWidth, ExifImageHeight can also be unsigned short */
370 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelXDimension", N_("Width"), nullptr },
371 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelYDimension", N_("Height"), nullptr },
372 { 0xa004, EXIF_FORMAT_STRING, -1, "Exif.Photo.RelatedSoundFile", N_("Audio data"), nullptr },
373 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifInteroperabilityOffset", N_("ExifR98 extension"), nullptr },
374 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", N_("Flash strength"), nullptr },
375 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), nullptr },
376 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), nullptr },
377 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), nullptr },
378 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalPlaneResolutionUnit", N_("Pixel density units"), ExifUnitList },
379 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Photo.SubjectLocation", N_("Subject location"), nullptr },
380 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", N_("ISO sensitivity"), nullptr },
381 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SensingMethod", N_("Sensor type"), ExifSensorList },
382 { 0xa300, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.FileSource", N_("Source type"), ExifSourceList },
383 { 0xa301, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.SceneType", N_("Scene type"), ExifSceneList },
384 { 0xa302, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.CFAPattern", N_("Color filter array pattern"), nullptr },
385 /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
386 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.CustomRendered", N_("Render process"), ExifCustRenderList },
387 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureMode", N_("Exposure mode"), ExifExposureModeList },
388 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.WhiteBalance", N_("White balance"), ExifWhiteBalanceList },
389 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.DigitalZoomRatio", N_("Digital zoom ratio"), nullptr },
390 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), nullptr },
391 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SceneCaptureType", N_("Scene capture type"), ExifSceneCaptureList },
392 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.GainControl", N_("Gain control"), ExifGainControlList },
393 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Contrast", N_("Contrast"), ExifContrastList },
394 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Saturation", N_("Saturation"), ExifSaturationList },
395 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Sharpness", N_("Sharpness"), ExifSharpnessList },
396 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.DeviceSettingDescription",N_("Device setting"), nullptr },
397 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SubjectDistanceRange",N_("Subject range"), ExifSubjectRangeList },
398 { 0xa420, EXIF_FORMAT_STRING, -1, "Exif.Photo.ImageUniqueID", N_("Image serial number"), nullptr },
399 /* place known, but undocumented or lesser used tags here */
400 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.NewSubfileType", nullptr, nullptr },
401 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubfileType", nullptr, nullptr },
402 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3, "Exif.Image.TransferFunction", nullptr, nullptr },
403 { 0x013b, EXIF_FORMAT_STRING, -1, "Exif.Image.Artist", "Artist", nullptr },
404 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Predictor", nullptr, nullptr },
405 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileWidth", nullptr, nullptr },
406 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileLength", nullptr, nullptr },
407 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1, "TileOffsets", nullptr, nullptr },
408 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1, "TileByteCounts", nullptr, nullptr },
409 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1, "Exif.Image.SubIFDs", nullptr, nullptr },
410 { 0x015b, EXIF_FORMAT_UNDEFINED, -1, "JPEGTables", nullptr, nullptr },
411 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Image.CFARepeatPatternDim", nullptr, nullptr },
412 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Exif.Image.CFAPattern", nullptr, nullptr },
413 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.BatteryLevel", nullptr, nullptr },
414 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1, "IPTC/NAA", nullptr, nullptr },
415 { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", nullptr, nullptr },
416 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1, "GPSInfo", "SubIFD GPS offset", nullptr },
417 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Interlace", nullptr, nullptr },
418 { 0x882a, EXIF_FORMAT_SHORT, 1, "TimeZoneOffset", nullptr, nullptr },
419 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SelfTimerMode", nullptr, nullptr },
420 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", nullptr, nullptr },
421 { 0x920c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.SpatialFrequencyResponse", nullptr, nullptr },
422 { 0x920d, EXIF_FORMAT_UNDEFINED, -1, "Noise", nullptr, nullptr },
423 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1, "ImageNumber", nullptr, nullptr },
424 { 0x9212, EXIF_FORMAT_STRING, 1, "SecurityClassification", nullptr, nullptr },
425 { 0x9213, EXIF_FORMAT_STRING, -1, "ImageHistory", nullptr, nullptr },
426 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", nullptr, nullptr },
427 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4, "TIFF/EPStandardID", nullptr, nullptr },
432 ExifMarker ExifKnownGPSInfoMarkersList[] = {
433 /* The following do not work at the moment as the tag value 0x0000 has a
434 * special meaning. */
435 /* { 0x0000, EXIF_FORMAT_BYTE, -1, "Exif.GPSInfo.GPSVersionID", NULL, NULL }, */
436 { 0x0001, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLatitudeRef", nullptr, nullptr },
437 { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", nullptr, nullptr },
438 { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", nullptr, nullptr },
439 { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", nullptr, nullptr },
440 { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", nullptr, nullptr },
441 { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", nullptr, nullptr },
442 { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", nullptr, nullptr },
443 { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", nullptr, nullptr },
444 { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", nullptr, nullptr },
445 { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", nullptr, nullptr },
446 { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", nullptr, nullptr },
447 { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", nullptr, nullptr },
448 { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", nullptr, nullptr },
449 { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", nullptr, nullptr },
450 { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", nullptr, nullptr },
451 { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", nullptr, nullptr },
452 { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", nullptr, nullptr },
453 { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", nullptr, nullptr },
454 { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", nullptr, nullptr },
455 { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", nullptr, nullptr },
456 { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", nullptr, nullptr },
457 { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", nullptr, nullptr },
458 { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", nullptr, nullptr },
459 { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", nullptr, nullptr },
460 { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", nullptr, nullptr },
461 { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", nullptr, nullptr },
462 { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", nullptr, nullptr },
463 { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", nullptr, nullptr },
464 { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", nullptr, nullptr },
465 { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", nullptr, nullptr },
470 ExifMarker ExifUnknownMarkersList[] = {
471 { 0x0000, EXIF_FORMAT_UNKNOWN, 0, "unknown", nullptr, nullptr },
472 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1, "unknown", nullptr, nullptr },
473 { 0x0000, EXIF_FORMAT_STRING, -1, "unknown", nullptr, nullptr },
474 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1, "unknown", nullptr, nullptr },
475 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1, "unknown", nullptr, nullptr },
476 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "unknown", nullptr, nullptr },
477 { 0x0000, EXIF_FORMAT_BYTE, -1, "unknown", nullptr, nullptr },
478 { 0x0000, EXIF_FORMAT_UNDEFINED, -1, "unknown", nullptr, nullptr },
479 { 0x0000, EXIF_FORMAT_SHORT, -1, "unknown", nullptr, nullptr },
480 { 0x0000, EXIF_FORMAT_LONG, -1, "unknown", nullptr, nullptr },
481 { 0x0000, EXIF_FORMAT_RATIONAL, -1, "unknown", nullptr, nullptr },
482 { 0x0000, EXIF_FORMAT_FLOAT, -1, "unknown", nullptr, nullptr },
483 { 0x0000, EXIF_FORMAT_DOUBLE, -1, "unknown", nullptr, nullptr },
486 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
489 *-----------------------------------------------------------------------------
491 *-----------------------------------------------------------------------------
494 ExifItem *exif_item_new(ExifFormatType format, guint tag,
495 guint elements, const ExifMarker *marker)
499 item = g_new0(ExifItem, 1);
500 item->format = format;
502 item->marker = marker;
503 item->elements = elements;
507 case EXIF_FORMAT_UNKNOWN:
508 /* unknown, data is NULL */
511 case EXIF_FORMAT_BYTE_UNSIGNED:
512 item->data_len = sizeof(gchar) * elements;
514 case EXIF_FORMAT_STRING:
515 item->data_len = sizeof(gchar) * elements;
517 case EXIF_FORMAT_SHORT_UNSIGNED:
518 item->data_len = sizeof(guint16) * elements;
520 case EXIF_FORMAT_LONG_UNSIGNED:
521 item->data_len = sizeof(guint32) * elements;
523 case EXIF_FORMAT_RATIONAL_UNSIGNED:
524 item->data_len = sizeof(ExifRational) * elements;
526 case EXIF_FORMAT_BYTE:
527 item->data_len = sizeof(gchar) * elements;
529 case EXIF_FORMAT_UNDEFINED:
530 item->data_len = sizeof(gchar) * elements;
532 case EXIF_FORMAT_SHORT:
533 item->data_len = sizeof(gint16) * elements;
535 case EXIF_FORMAT_LONG:
536 item->data_len = sizeof(gint32) * elements;
538 case EXIF_FORMAT_RATIONAL:
539 item->data_len = sizeof(ExifRational) * elements;
541 case EXIF_FORMAT_FLOAT:
542 item->data_len = sizeof(float) * elements;
544 case EXIF_FORMAT_DOUBLE:
545 item->data_len = sizeof(gdouble) * elements;
549 item->data = g_malloc0(item->data_len);
554 static void exif_item_free(ExifItem *item)
562 gchar *exif_item_get_tag_name(ExifItem *item)
564 if (!item || !item->marker) return nullptr;
565 return g_strdup(item->marker->key);
568 guint exif_item_get_tag_id(ExifItem *item)
574 guint exif_item_get_elements(ExifItem *item)
577 return item->elements;
580 gchar *exif_item_get_data(ExifItem *item, guint *data_len)
583 *data_len = item->data_len;
584 #if GLIB_CHECK_VERSION(2,68,0)
585 return static_cast<gchar*>(g_memdup2(item->data, item->data_len));
587 return static_cast<gchar*>(g_memdup(item->data, item->data_len));
591 guint exif_item_get_format_id(ExifItem *item)
593 if (!item) return EXIF_FORMAT_UNKNOWN;
598 gchar *exif_item_get_description(ExifItem *item)
600 if (!item || !item->marker) return nullptr;
601 return g_strdup(_(item->marker->description));
604 const gchar *exif_item_get_format_name(ExifItem *item, gboolean brief)
606 if (!item || !item->marker) return nullptr;
607 return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
610 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
614 for (i = 0 ; i < ne; i++)
616 guchar c = (static_cast<gchar *>(data))[i];
617 if (c < 32 || c > 127) c = '.';
618 g_string_append_printf(string, "%c", c);
620 string = g_string_append(string, " : ");
621 for (i = 0 ; i < ne; i++)
639 g_string_append_printf(string, "%s%02x", spacer, (static_cast<gchar *>(data))[i]);
646 gchar *exif_text_list_find_value(ExifTextList *list, guint value)
648 gchar *result = nullptr;
652 while (!result && list[i].value >= 0)
654 if (value == static_cast<guint>(list[i].value)) result = g_strdup(_(list[i].description));
657 if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
664 *-------------------------------------------------------------------
666 *-------------------------------------------------------------------
669 /* note: the align_buf is used to avoid alignment issues (on sparc) */
671 guint16 exif_byte_get_int16(guchar *f, ExifByteOrder bo)
675 memcpy(&align_buf, f, sizeof(guint16));
677 if (bo == EXIF_BYTE_ORDER_INTEL)
678 return GUINT16_FROM_LE(align_buf);
680 return GUINT16_FROM_BE(align_buf);
683 guint32 exif_byte_get_int32(guchar *f, ExifByteOrder bo)
687 memcpy(&align_buf, f, sizeof(guint32));
689 if (bo == EXIF_BYTE_ORDER_INTEL)
690 return GUINT32_FROM_LE(align_buf);
692 return GUINT32_FROM_BE(align_buf);
695 void exif_byte_put_int16(guchar *f, guint16 n, ExifByteOrder bo)
699 if (bo == EXIF_BYTE_ORDER_INTEL)
701 align_buf = GUINT16_TO_LE(n);
705 align_buf = GUINT16_TO_BE(n);
708 memcpy(f, &align_buf, sizeof(guint16));
711 void exif_byte_put_int32(guchar *f, guint32 n, ExifByteOrder bo)
715 if (bo == EXIF_BYTE_ORDER_INTEL)
717 align_buf = GUINT32_TO_LE(n);
721 align_buf = GUINT32_TO_BE(n);
724 memcpy(f, &align_buf, sizeof(guint32));
729 *-------------------------------------------------------------------
731 *-------------------------------------------------------------------
734 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list)
738 if (!list) return nullptr;
740 while (list[i].tag != 0 && list[i].tag != tag)
745 return (list[i].tag == 0 ? nullptr : &list[i]);
748 static void rational_from_data(ExifRational *r, gpointer src, ExifByteOrder bo)
750 r->num = exif_byte_get_int32(static_cast<guchar *>(src), bo);
751 r->den = exif_byte_get_int32(static_cast<guchar *>(src) + sizeof(guint32), bo);
754 /* src_format and item->format must be compatible
755 * and not overrun src or item->data.
757 void exif_item_copy_data(ExifItem *item, gpointer src, guint len,
758 ExifFormatType src_format, ExifByteOrder bo)
765 bs = ExifFormatList[item->format].size;
770 ExifFormatList[src_format].size * ne > len)
772 gchar *tag = exif_item_get_tag_name(item);
773 log_printf("exif tag %s data size mismatch\n", tag);
778 switch (item->format)
780 case EXIF_FORMAT_UNKNOWN:
782 case EXIF_FORMAT_BYTE_UNSIGNED:
783 case EXIF_FORMAT_BYTE:
784 case EXIF_FORMAT_UNDEFINED:
785 memcpy(dest, src, len);
787 case EXIF_FORMAT_STRING:
788 memcpy(dest, src, len);
789 /* string is NULL terminated, make sure this is true */
790 if ((static_cast<gchar *>(dest))[len - 1] != '\0') (static_cast<gchar *>(dest))[len - 1] = '\0';
792 case EXIF_FORMAT_SHORT_UNSIGNED:
793 case EXIF_FORMAT_SHORT:
794 for (i = 0; i < ne; i++)
796 (static_cast<guint16 *>(dest))[i] = exif_byte_get_int16(static_cast<guchar *>(src) + i * bs, bo);
799 case EXIF_FORMAT_LONG_UNSIGNED:
800 case EXIF_FORMAT_LONG:
801 if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
802 src_format == EXIF_FORMAT_SHORT)
804 /* a short fits into a long, so allow it */
807 ss = ExifFormatList[src_format].size;
808 for (i = 0; i < ne; i++)
810 (static_cast<gint32 *>(dest))[i] =
811 static_cast<gint32>(exif_byte_get_int16(static_cast<guchar *>(src) + i * ss, bo));
816 for (i = 0; i < ne; i++)
818 (static_cast<gint32 *>(dest))[i] =
819 exif_byte_get_int32(static_cast<guchar *>(src) + i * bs, bo);
823 case EXIF_FORMAT_RATIONAL_UNSIGNED:
824 case EXIF_FORMAT_RATIONAL:
825 for (i = 0; i < ne; i++)
827 rational_from_data(&(static_cast<ExifRational *>(dest))[i], static_cast<guchar *>(src) + i * bs, bo);
830 case EXIF_FORMAT_FLOAT:
831 for (i = 0; i < ne; i++)
833 (static_cast<float *>(dest))[i] = exif_byte_get_int32(static_cast<guchar *>(src) + i * bs, bo);
836 case EXIF_FORMAT_DOUBLE:
837 for (i = 0; i < ne; i++)
841 rational_from_data(&r, static_cast<guchar *>(src) + i * bs, bo);
842 if (r.den) (static_cast<gdouble *>(dest))[i] = static_cast<gdouble>(r.num) / r.den;
848 static gint exif_parse_IFD_entry(ExifData *exif, guchar *tiff, guint offset,
849 guint size, ExifByteOrder bo,
851 const ExifMarker *list)
859 const ExifMarker *marker;
862 tag = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_TAG, bo);
863 format = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
864 count = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_COUNT, bo);
865 data_val = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_DATA, bo);
867 /* Check tag type. If it does not match, either the format is wrong,
868 * either it is a unknown tag; so it is not really an error.
870 marker = exif_marker_from_tag(tag, list);
873 if (format >= EXIF_FORMAT_COUNT)
875 log_printf("warning: exif tag 0x%4x has invalid format %d\n", tag, format);
878 /* allow non recognized tags to be displayed */
879 marker = &ExifUnknownMarkersList[format];
881 if (marker->format != format)
883 /* Some cameras got mixed up signed/unsigned_rational
884 * eg KODAK DC4800 on object_distance tag
886 * FIXME: what exactly is this test trying to do?
887 * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
889 if ((marker->format != EXIF_FORMAT_RATIONAL_UNSIGNED || format != EXIF_FORMAT_RATIONAL) &&
890 (marker->format != EXIF_FORMAT_RATIONAL || format != EXIF_FORMAT_RATIONAL_UNSIGNED) &&
891 /* short fits into a long so allow this mismatch
892 * as well (some tags allowed to be unsigned short _or_ unsigned long)
894 (marker->format != EXIF_FORMAT_LONG_UNSIGNED || format != EXIF_FORMAT_SHORT_UNSIGNED) )
896 if (format < EXIF_FORMAT_COUNT)
898 log_printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
899 marker->key, ExifFormatList[format].short_name,
900 ExifFormatList[marker->format].short_name);
904 log_printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
905 marker->key, format, marker->format,
906 ExifFormatList[marker->format].short_name);
912 /* Where is the data, is it available?
914 if (marker->components > 0 && static_cast<guint>(marker->components) != count)
916 log_printf("warning: exif tag %s has %d elements, exif spec requests %d\n",
917 marker->key, count, marker->components);
920 data_length = ExifFormatList[marker->format].size * count;
923 data_offset = data_val;
924 if (size < data_offset || size < data_offset + data_length)
926 log_printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
932 data_offset = offset + EXIF_TIFD_OFFSET_DATA;
935 item = exif_item_new(marker->format, tag, count, marker);
936 exif_item_copy_data(item, tiff + data_offset, data_length, static_cast<ExifFormatType>(format), bo);
937 exif->items = g_list_prepend(exif->items, item);
939 if (list == ExifKnownMarkersList)
944 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, list);
947 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, ExifKnownGPSInfoMarkersList);
949 case TAG_EXIFMAKERNOTE:
950 format_exif_makernote_parse(exif, tiff, data_val, size, bo);
958 gint exif_parse_IFD_table(ExifData *exif,
959 guchar *tiff, guint offset,
960 guint size, ExifByteOrder bo,
962 const ExifMarker *list)
967 /* limit damage from infinite loops */
968 if (level > EXIF_TIFF_MAX_LEVELS) return -1;
970 /* We should be able to read number of entries in IFD0) */
971 if (size < offset + 2) return -1;
973 count = exif_byte_get_int16(tiff + offset, bo);
976 /* Entries and next IFD offset must be readable */
977 if (size < offset + count * EXIF_TIFD_SIZE + 4) return -1;
979 for (i = 0; i < count; i++)
981 exif_parse_IFD_entry(exif, tiff, offset + i * EXIF_TIFD_SIZE, size, bo, level, list);
988 *-------------------------------------------------------------------
990 *-------------------------------------------------------------------
993 gint exif_tiff_directory_offset(guchar *data, const guint len,
994 guint *offset, ExifByteOrder *bo)
996 if (len < 8) return FALSE;
998 if (memcmp(data, "II", 2) == 0)
1000 *bo = EXIF_BYTE_ORDER_INTEL;
1002 else if (memcmp(data, "MM", 2) == 0)
1004 *bo = EXIF_BYTE_ORDER_MOTOROLA;
1011 if (exif_byte_get_int16(data + 2, *bo) != 0x002A)
1016 *offset = exif_byte_get_int32(data + 4, *bo);
1018 return (*offset < len);
1021 gint exif_tiff_parse(ExifData *exif, guchar *tiff, guint size, ExifMarker *list)
1026 if (!exif_tiff_directory_offset(tiff, size, &offset, &bo)) return -1;
1028 return exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, list);
1033 *-------------------------------------------------------------------
1035 *-------------------------------------------------------------------
1038 /* jpeg container format:
1039 all data markers start with 0XFF
1040 2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
1041 4 byte long data segment markers in format: 0xFFTTSSSSNNN...
1042 FF: 1 byte standard marker identifier
1043 TT: 1 byte data type
1044 SSSS: 2 bytes in Motorola byte alignment for length of the data.
1045 This value includes these 2 bytes in the count, making actual
1046 length of NN... == SSSS - 2.
1047 NNN.: the data in this segment
1049 static ExifMarker jpeg_color_marker = { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", nullptr, nullptr };
1051 void exif_add_jpeg_color_profile(ExifData *exif, guchar *cp_data, guint cp_length)
1053 ExifItem *item = exif_item_new(jpeg_color_marker.format, jpeg_color_marker.tag, 1,
1054 &jpeg_color_marker);
1056 item->data = cp_data;
1057 item->elements = cp_length;
1058 item->data_len = cp_length;
1059 exif->items = g_list_prepend(exif->items, item);
1063 static gint exif_jpeg_parse(ExifData *exif,
1064 guchar *data, guint size,
1067 guint seg_offset = 0;
1068 guint seg_length = 0;
1072 memcmp(data, "\xFF\xD8", 2) != 0)
1077 if (jpeg_segment_find(data, size, JPEG_MARKER_APP1,
1079 &seg_offset, &seg_length))
1081 res = exif_tiff_parse(exif, data + seg_offset + 6, seg_length - 6, list);
1084 if (exif_jpeg_parse_color(exif, data, size))
1092 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1094 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1095 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1096 return reinterpret_cast<guchar *>(exif_item_get_data(prof_item, data_len));
1101 gchar* exif_get_image_comment(FileData*)
1103 log_printf("%s", _("Can't get image comment: not compiled with Exiv2.\n"));
1104 return g_strdup("");
1107 void exif_set_image_comment(FileData*, const gchar*)
1109 log_printf("%s", _("Can't set image comment: not compiled with Exiv2.\n"));
1114 *-------------------------------------------------------------------
1116 *-------------------------------------------------------------------
1120 ExifItem *exif_get_first_item(ExifData *exif)
1124 auto ret = static_cast<ExifItem *>(exif->items->data);
1125 exif->current = exif->items->next;
1128 exif->current = nullptr;
1132 ExifItem *exif_get_next_item(ExifData *exif)
1136 auto ret = static_cast<ExifItem *>(exif->current->data);
1137 exif->current = exif->current->next;
1143 static gint map_file(const gchar *path, void **mapping, gint *size)
1148 fd = open(path, O_RDONLY);
1155 if (fstat(fd, &fs) == -1)
1164 *mapping = mmap(nullptr, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1165 if (*mapping == MAP_FAILED)
1176 static gint unmap_file(gpointer mapping, gint size)
1178 if (munmap(mapping, size) == -1)
1187 ExifData *exif_get_original(ExifData *processed)
1192 void exif_free(ExifData *exif)
1196 g_list_free_full(exif->items, reinterpret_cast<GDestroyNotify>(exif_item_free));
1201 ExifData *exif_read(gchar *path, gchar *, GHashTable *)
1208 if (!path) return nullptr;
1210 pathl = path_from_utf8(path);
1211 if (map_file(pathl, &f, &size) == -1)
1218 exif = g_new0(ExifData, 1);
1219 exif->path = g_strdup(path);
1221 res = exif_jpeg_parse(exif, static_cast<guchar *>(f), size, ExifKnownMarkersList);
1224 res = exif_tiff_parse(exif, static_cast<guchar *>(f), size, ExifKnownMarkersList);
1229 FormatRawExifType exif_type;
1230 FormatRawExifParseFunc exif_parse_func;
1233 exif_type = format_raw_exif_offset(static_cast<guchar*>(f), size, &offset, &exif_parse_func);
1236 case FORMAT_RAW_EXIF_NONE:
1239 case FORMAT_RAW_EXIF_TIFF:
1240 res = exif_tiff_parse(exif, static_cast<guchar *>(f) + offset, size - offset,
1241 ExifKnownMarkersList);
1243 case FORMAT_RAW_EXIF_JPEG:
1244 res = exif_jpeg_parse(exif, static_cast<guchar *>(f) + offset, size - offset,
1245 ExifKnownMarkersList);
1247 case FORMAT_RAW_EXIF_IFD_II:
1248 case FORMAT_RAW_EXIF_IFD_MM:
1249 res = exif_parse_IFD_table(exif, static_cast<guchar *>(f), offset, size - offset,
1250 (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1251 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1252 0, ExifKnownMarkersList);
1254 case FORMAT_RAW_EXIF_PROPRIETARY:
1255 if (exif_parse_func)
1257 res = exif_parse_func(static_cast<guchar *>(f) + offset, size - offset, exif);
1269 unmap_file(f, size);
1271 if (exif) exif->items = g_list_reverse(exif->items);
1276 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1280 if (!key) return nullptr;
1287 item = static_cast<ExifItem*>(work->data);
1289 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1294 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1297 static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
1299 const ExifMarker *marker;
1305 if (!item) return nullptr;
1307 marker = item->marker;
1308 if (!marker) return nullptr;
1311 ne = item->elements;
1312 if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1313 string = g_string_new("");
1314 switch (item->format)
1316 case EXIF_FORMAT_UNKNOWN:
1318 case EXIF_FORMAT_BYTE_UNSIGNED:
1319 case EXIF_FORMAT_BYTE:
1320 case EXIF_FORMAT_UNDEFINED:
1321 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1326 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1327 item->format == EXIF_FORMAT_UNDEFINED)
1329 val = (static_cast<guchar *>(data))[0];
1333 val = static_cast<guchar>((static_cast<gchar *>(data))[0]);
1336 result = exif_text_list_find_value(marker->list, static_cast<guint>(val));
1337 string = g_string_append(string, result);
1342 string = string_append_raw_bytes(string, data, ne);
1345 case EXIF_FORMAT_STRING:
1346 if (item->data) string = g_string_append(string, (gchar *)(item->data));
1348 case EXIF_FORMAT_SHORT_UNSIGNED:
1349 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1353 result = exif_text_list_find_value(marker->list, (static_cast<guint16 *>(data))[0]);
1354 string = g_string_append(string, result);
1357 else for (i = 0; i < ne; i++)
1359 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1360 (static_cast<guint16 *>(data))[i]);
1363 case EXIF_FORMAT_LONG_UNSIGNED:
1364 for (i = 0; i < ne; i++)
1366 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1367 static_cast<gulong>((static_cast<guint32 *>(data))[i]));
1370 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1371 for (i = 0; i < ne; i++)
1375 r = &(static_cast<ExifRational *>(data))[i];
1376 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1377 static_cast<gulong>(r->num), static_cast<gulong>(r->den));
1380 case EXIF_FORMAT_SHORT:
1381 for (i = 0; i < ne; i++)
1383 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1384 (static_cast<gint16 *>(data))[i]);
1387 case EXIF_FORMAT_LONG:
1388 for (i = 0; i < ne; i++)
1390 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1391 static_cast<glong>((static_cast<gint32 *>(data))[i]));
1394 case EXIF_FORMAT_RATIONAL:
1395 for (i = 0; i < ne; i++)
1399 r = &(static_cast<ExifRational *>(data))[i];
1400 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1401 static_cast<glong>(r->num), static_cast<glong>(r->den));
1404 case EXIF_FORMAT_FLOAT:
1405 for (i = 0; i < ne; i++)
1407 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1408 (static_cast<float *>(data))[i]);
1411 case EXIF_FORMAT_DOUBLE:
1412 for (i = 0; i < ne; i++)
1414 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1415 (static_cast<gdouble *>(data))[i]);
1420 if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1421 item->format != EXIF_FORMAT_STRING)
1423 g_string_append(string, " ...");
1426 return g_string_free(string, FALSE);
1429 gchar *exif_item_get_string(ExifItem *item, gint)
1431 return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
1434 gchar *exif_item_get_data_as_text(ExifItem *item, ExifData *)
1436 return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
1439 gint exif_item_get_integer(ExifItem *item, gint *value)
1441 if (!item) return FALSE;
1442 if (!item->elements) return FALSE;
1444 switch (item->format)
1446 case EXIF_FORMAT_SHORT:
1447 *value = static_cast<gint>((static_cast<gint16 *>(item->data))[0]);
1450 case EXIF_FORMAT_SHORT_UNSIGNED:
1451 *value = static_cast<gint>((static_cast<guint16 *>(item->data))[0]);
1454 case EXIF_FORMAT_LONG:
1455 *value = static_cast<gint>((static_cast<gint32 *>(item->data))[0]);
1458 case EXIF_FORMAT_LONG_UNSIGNED: /**< @FIXME overflow possible */
1459 *value = static_cast<gint>((static_cast<guint32 *>(item->data))[0]);
1462 /* all other type return FALSE */
1469 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1471 if (!item) return nullptr;
1472 if (n >= item->elements) return nullptr;
1474 if (item->format == EXIF_FORMAT_RATIONAL ||
1475 item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1477 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1478 return &(static_cast<ExifRational *>(item->data))[n];
1484 gchar *exif_get_tag_description_by_key(const gchar *key)
1488 if (!key) return nullptr;
1491 while (ExifKnownMarkersList[i].tag > 0)
1493 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1498 while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1500 if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
1507 static void exif_write_item(FILE *f, ExifItem *item, ExifData *exif)
1511 text = exif_item_get_data_as_text(item, exif);
1514 gchar *tag = exif_item_get_tag_name(item);
1515 g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1522 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1524 if (!f || !exif) return;
1526 g_fprintf(f, " tag format key value\n");
1527 g_fprintf(f, "----------------------------------------------------\n");
1529 if (human_readable_list)
1534 while (ExifFormattedList[i].key)
1538 text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, nullptr);
1541 g_fprintf(f, " %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1555 item = static_cast<ExifItem*>(work->data);
1558 exif_write_item(f, item, exif);
1561 g_fprintf(f, "----------------------------------------------------\n");
1564 gboolean exif_write(ExifData *)
1566 log_printf("Not compiled with EXIF write support\n");
1570 gboolean exif_write_sidecar(ExifData *, gchar *)
1572 log_printf("Not compiled with EXIF write support\n");
1577 gint exif_update_metadata(ExifData *, const gchar *, const GList *)
1582 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1587 if (!key) return nullptr;
1589 /* convert xmp key to exif key */
1590 if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
1592 if (format == METADATA_FORMATTED)
1596 text = exif_get_formatted_by_key(exif, key, &key_valid);
1597 if (key_valid) return g_list_append(nullptr, text);
1600 item = exif_get_item(exif, key);
1601 if (!item) return nullptr;
1603 str = exif_item_get_data_as_text_full(item, format);
1605 if (!str) return nullptr;
1607 return g_list_append(nullptr, str);
1617 static GList *exif_unmap_list = nullptr;
1619 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint, gint)
1628 if (!exif) return nullptr;
1631 fd = open(path, O_RDONLY);
1639 if (fstat(fd, &st) == -1)
1644 map_len = st.st_size;
1645 map_data = static_cast<guchar *>(mmap(nullptr, map_len, PROT_READ, MAP_PRIVATE, fd, 0));
1648 if (map_data == MAP_FAILED)
1653 if (format_raw_img_exif_offsets(map_data, map_len, &offset, nullptr) && offset)
1657 DEBUG_1("%s: offset %u", path, offset);
1659 *data_len = map_len - offset;
1660 ud = g_new(UnmapData, 1);
1661 ud->ptr = map_data + offset;
1662 ud->map_data = map_data;
1663 ud->map_len = map_len;
1665 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1669 munmap(map_data, map_len);
1674 void exif_free_preview(const guchar *buf)
1676 GList *work = exif_unmap_list;
1680 auto ud = static_cast<UnmapData *>(work->data);
1683 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1689 g_assert_not_reached();
1696 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */