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
58 #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" },
94 { static_cast<ExifFormatType>(-1), 0, nullptr, nullptr }
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"), nullptr },
323 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageLength", N_("Image Height"), nullptr },
324 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.BitsPerSample", N_("Bits per Sample/Pixel"), nullptr },
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"), nullptr },
327 { 0x010f, EXIF_FORMAT_STRING, -1, "Exif.Image.Make", N_("Camera make"), nullptr },
328 { 0x0110, EXIF_FORMAT_STRING, -1, "Exif.Image.Model", N_("Camera model"), nullptr },
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"), nullptr },
331 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.YResolution", N_("Y Resolution"), nullptr },
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"), nullptr },
334 { 0x0132, EXIF_FORMAT_STRING, 20, "Exif.Image.DateTime", N_("Date"), nullptr },
335 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2, "Exif.Image.WhitePoint", N_("White point"), nullptr },
336 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), nullptr },
337 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), nullptr },
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"), nullptr },
340 { 0x8298, EXIF_FORMAT_STRING, -1, "Exif.Image.Copyright", N_("Copyright"), nullptr },
341 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ExifTag", N_("SubIFD Exif offset"), nullptr },
343 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureTime", N_("Exposure time (seconds)"), nullptr },
344 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FNumber", N_("FNumber"), nullptr },
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"), nullptr },
347 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.ISOSpeedRatings", N_("ISO sensitivity"), nullptr },
348 { 0x8828, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.OECF", N_("Optoelectric conversion factor"), nullptr },
349 { 0x9000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.ExifVersion", N_("Exif version"), nullptr },
350 { 0x9003, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeOriginal", N_("Date original"), nullptr },
351 { 0x9004, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeDigitized", N_("Date digitized"), nullptr },
352 { 0x9101, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), nullptr },
353 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1, "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), nullptr },
354 { 0x9201, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), nullptr },
355 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ApertureValue", N_("Aperture"), nullptr },
356 { 0x9203, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.BrightnessValue", N_("Brightness"), nullptr },
357 { 0x9204, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), nullptr },
358 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.MaxApertureValue", N_("Maximum aperture"), nullptr },
359 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.SubjectDistance", N_("Subject distance"), nullptr },
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"), nullptr },
364 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SubjectArea", N_("Subject area"), nullptr },
365 { 0x927c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.MakerNote", N_("MakerNote"), nullptr },
366 { 0x9286, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.UserComment", N_("UserComment"), nullptr },
367 { 0x9290, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTime", N_("Subsecond time"), nullptr },
368 { 0x9291, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), nullptr },
369 { 0x9292, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), nullptr },
370 { 0xa000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.FlashpixVersion", N_("FlashPix version"), nullptr },
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"), nullptr },
374 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelYDimension", N_("Height"), nullptr },
375 { 0xa004, EXIF_FORMAT_STRING, -1, "Exif.Photo.RelatedSoundFile", N_("Audio data"), nullptr },
376 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifInteroperabilityOffset", N_("ExifR98 extension"), nullptr },
377 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", N_("Flash strength"), nullptr },
378 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), nullptr },
379 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), nullptr },
380 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), nullptr },
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"), nullptr },
383 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", N_("ISO sensitivity"), nullptr },
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"), nullptr },
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"), nullptr },
393 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), nullptr },
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"), nullptr },
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"), nullptr },
402 /* place known, but undocumented or lesser used tags here */
403 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.NewSubfileType", nullptr, nullptr },
404 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubfileType", nullptr, nullptr },
405 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3, "Exif.Image.TransferFunction", nullptr, nullptr },
406 { 0x013b, EXIF_FORMAT_STRING, -1, "Exif.Image.Artist", "Artist", nullptr },
407 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Predictor", nullptr, nullptr },
408 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileWidth", nullptr, nullptr },
409 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileLength", nullptr, nullptr },
410 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1, "TileOffsets", nullptr, nullptr },
411 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1, "TileByteCounts", nullptr, nullptr },
412 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1, "Exif.Image.SubIFDs", nullptr, nullptr },
413 { 0x015b, EXIF_FORMAT_UNDEFINED, -1, "JPEGTables", nullptr, nullptr },
414 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Image.CFARepeatPatternDim", nullptr, nullptr },
415 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Exif.Image.CFAPattern", nullptr, nullptr },
416 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.BatteryLevel", nullptr, nullptr },
417 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1, "IPTC/NAA", nullptr, nullptr },
418 { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", nullptr, nullptr },
419 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1, "GPSInfo", "SubIFD GPS offset", nullptr },
420 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Interlace", nullptr, nullptr },
421 { 0x882a, EXIF_FORMAT_SHORT, 1, "TimeZoneOffset", nullptr, nullptr },
422 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SelfTimerMode", nullptr, nullptr },
423 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", nullptr, nullptr },
424 { 0x920c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.SpatialFrequencyResponse", nullptr, nullptr },
425 { 0x920d, EXIF_FORMAT_UNDEFINED, -1, "Noise", nullptr, nullptr },
426 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1, "ImageNumber", nullptr, nullptr },
427 { 0x9212, EXIF_FORMAT_STRING, 1, "SecurityClassification", nullptr, nullptr },
428 { 0x9213, EXIF_FORMAT_STRING, -1, "ImageHistory", nullptr, nullptr },
429 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", nullptr, nullptr },
430 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4, "TIFF/EPStandardID", nullptr, nullptr },
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", nullptr, nullptr },
440 { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", nullptr, nullptr },
441 { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", nullptr, nullptr },
442 { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", nullptr, nullptr },
443 { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", nullptr, nullptr },
444 { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", nullptr, nullptr },
445 { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", nullptr, nullptr },
446 { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", nullptr, nullptr },
447 { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", nullptr, nullptr },
448 { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", nullptr, nullptr },
449 { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", nullptr, nullptr },
450 { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", nullptr, nullptr },
451 { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", nullptr, nullptr },
452 { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", nullptr, nullptr },
453 { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", nullptr, nullptr },
454 { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", nullptr, nullptr },
455 { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", nullptr, nullptr },
456 { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", nullptr, nullptr },
457 { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", nullptr, nullptr },
458 { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", nullptr, nullptr },
459 { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", nullptr, nullptr },
460 { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", nullptr, nullptr },
461 { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", nullptr, nullptr },
462 { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", nullptr, nullptr },
463 { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", nullptr, nullptr },
464 { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", nullptr, nullptr },
465 { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", nullptr, nullptr },
466 { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", nullptr, nullptr },
467 { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", nullptr, nullptr },
468 { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", nullptr, nullptr },
473 ExifMarker ExifUnknownMarkersList[] = {
474 { 0x0000, EXIF_FORMAT_UNKNOWN, 0, "unknown", nullptr, nullptr },
475 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1, "unknown", nullptr, nullptr },
476 { 0x0000, EXIF_FORMAT_STRING, -1, "unknown", nullptr, nullptr },
477 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1, "unknown", nullptr, nullptr },
478 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1, "unknown", nullptr, nullptr },
479 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "unknown", nullptr, nullptr },
480 { 0x0000, EXIF_FORMAT_BYTE, -1, "unknown", nullptr, nullptr },
481 { 0x0000, EXIF_FORMAT_UNDEFINED, -1, "unknown", nullptr, nullptr },
482 { 0x0000, EXIF_FORMAT_SHORT, -1, "unknown", nullptr, nullptr },
483 { 0x0000, EXIF_FORMAT_LONG, -1, "unknown", nullptr, nullptr },
484 { 0x0000, EXIF_FORMAT_RATIONAL, -1, "unknown", nullptr, nullptr },
485 { 0x0000, EXIF_FORMAT_FLOAT, -1, "unknown", nullptr, nullptr },
486 { 0x0000, EXIF_FORMAT_DOUBLE, -1, "unknown", nullptr, nullptr },
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 nullptr;
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 #if GLIB_CHECK_VERSION(2,68,0)
588 return static_cast<gchar*>(g_memdup2(item->data, item->data_len));
590 return static_cast<gchar*>(g_memdup(item->data, item->data_len));
594 guint exif_item_get_format_id(ExifItem *item)
596 if (!item) return EXIF_FORMAT_UNKNOWN;
601 gchar *exif_item_get_description(ExifItem *item)
603 if (!item || !item->marker) return nullptr;
604 return g_strdup(_(item->marker->description));
607 const gchar *exif_item_get_format_name(ExifItem *item, gboolean brief)
609 if (!item || !item->marker) return nullptr;
610 return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
613 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
617 for (i = 0 ; i < ne; i++)
619 guchar c = (static_cast<gchar *>(data))[i];
620 if (c < 32 || c > 127) c = '.';
621 g_string_append_printf(string, "%c", c);
623 string = g_string_append(string, " : ");
624 for (i = 0 ; i < ne; i++)
642 g_string_append_printf(string, "%s%02x", spacer, (static_cast<gchar *>(data))[i]);
649 gchar *exif_text_list_find_value(ExifTextList *list, guint value)
651 gchar *result = nullptr;
655 while (!result && list[i].value >= 0)
657 if (value == static_cast<guint>(list[i].value)) result = g_strdup(_(list[i].description));
660 if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
667 *-------------------------------------------------------------------
669 *-------------------------------------------------------------------
672 /* note: the align_buf is used to avoid alignment issues (on sparc) */
674 guint16 exif_byte_get_int16(guchar *f, ExifByteOrder bo)
678 memcpy(&align_buf, f, sizeof(guint16));
680 if (bo == EXIF_BYTE_ORDER_INTEL)
681 return GUINT16_FROM_LE(align_buf);
683 return GUINT16_FROM_BE(align_buf);
686 guint32 exif_byte_get_int32(guchar *f, ExifByteOrder bo)
690 memcpy(&align_buf, f, sizeof(guint32));
692 if (bo == EXIF_BYTE_ORDER_INTEL)
693 return GUINT32_FROM_LE(align_buf);
695 return GUINT32_FROM_BE(align_buf);
698 void exif_byte_put_int16(guchar *f, guint16 n, ExifByteOrder bo)
702 if (bo == EXIF_BYTE_ORDER_INTEL)
704 align_buf = GUINT16_TO_LE(n);
708 align_buf = GUINT16_TO_BE(n);
711 memcpy(f, &align_buf, sizeof(guint16));
714 void exif_byte_put_int32(guchar *f, guint32 n, ExifByteOrder bo)
718 if (bo == EXIF_BYTE_ORDER_INTEL)
720 align_buf = GUINT32_TO_LE(n);
724 align_buf = GUINT32_TO_BE(n);
727 memcpy(f, &align_buf, sizeof(guint32));
732 *-------------------------------------------------------------------
734 *-------------------------------------------------------------------
737 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list)
741 if (!list) return nullptr;
743 while (list[i].tag != 0 && list[i].tag != tag)
748 return (list[i].tag == 0 ? nullptr : &list[i]);
751 static void rational_from_data(ExifRational *r, gpointer src, ExifByteOrder bo)
753 r->num = exif_byte_get_int32(static_cast<guchar *>(src), bo);
754 r->den = exif_byte_get_int32(static_cast<guchar *>(src) + sizeof(guint32), bo);
757 /* src_format and item->format must be compatible
758 * and not overrun src or item->data.
760 void exif_item_copy_data(ExifItem *item, gpointer src, guint len,
761 ExifFormatType src_format, ExifByteOrder bo)
768 bs = ExifFormatList[item->format].size;
773 ExifFormatList[src_format].size * ne > len)
775 gchar *tag = exif_item_get_tag_name(item);
776 log_printf("exif tag %s data size mismatch\n", tag);
781 switch (item->format)
783 case EXIF_FORMAT_UNKNOWN:
785 case EXIF_FORMAT_BYTE_UNSIGNED:
786 case EXIF_FORMAT_BYTE:
787 case EXIF_FORMAT_UNDEFINED:
788 memcpy(dest, src, len);
790 case EXIF_FORMAT_STRING:
791 memcpy(dest, src, len);
792 /* string is NULL terminated, make sure this is true */
793 if ((static_cast<gchar *>(dest))[len - 1] != '\0') (static_cast<gchar *>(dest))[len - 1] = '\0';
795 case EXIF_FORMAT_SHORT_UNSIGNED:
796 case EXIF_FORMAT_SHORT:
797 for (i = 0; i < ne; i++)
799 (static_cast<guint16 *>(dest))[i] = exif_byte_get_int16(static_cast<guchar *>(src) + i * bs, bo);
802 case EXIF_FORMAT_LONG_UNSIGNED:
803 case EXIF_FORMAT_LONG:
804 if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
805 src_format == EXIF_FORMAT_SHORT)
807 /* a short fits into a long, so allow it */
810 ss = ExifFormatList[src_format].size;
811 for (i = 0; i < ne; i++)
813 (static_cast<gint32 *>(dest))[i] =
814 static_cast<gint32>(exif_byte_get_int16(static_cast<guchar *>(src) + i * ss, bo));
819 for (i = 0; i < ne; i++)
821 (static_cast<gint32 *>(dest))[i] =
822 exif_byte_get_int32(static_cast<guchar *>(src) + i * bs, bo);
826 case EXIF_FORMAT_RATIONAL_UNSIGNED:
827 case EXIF_FORMAT_RATIONAL:
828 for (i = 0; i < ne; i++)
830 rational_from_data(&(static_cast<ExifRational *>(dest))[i], static_cast<guchar *>(src) + i * bs, bo);
833 case EXIF_FORMAT_FLOAT:
834 for (i = 0; i < ne; i++)
836 (static_cast<float *>(dest))[i] = exif_byte_get_int32(static_cast<guchar *>(src) + i * bs, bo);
839 case EXIF_FORMAT_DOUBLE:
840 for (i = 0; i < ne; i++)
844 rational_from_data(&r, static_cast<guchar *>(src) + i * bs, bo);
845 if (r.den) (static_cast<gdouble *>(dest))[i] = static_cast<gdouble>(r.num) / r.den;
851 static gint exif_parse_IFD_entry(ExifData *exif, guchar *tiff, guint offset,
852 guint size, ExifByteOrder bo,
854 const ExifMarker *list)
862 const ExifMarker *marker;
865 tag = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_TAG, bo);
866 format = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
867 count = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_COUNT, bo);
868 data_val = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_DATA, bo);
870 /* Check tag type. If it does not match, either the format is wrong,
871 * either it is a unknown tag; so it is not really an error.
873 marker = exif_marker_from_tag(tag, list);
876 if (format >= EXIF_FORMAT_COUNT)
878 log_printf("warning: exif tag 0x%4x has invalid format %d\n", tag, format);
881 /* allow non recognized tags to be displayed */
882 marker = &ExifUnknownMarkersList[format];
884 if (marker->format != format)
886 /* Some cameras got mixed up signed/unsigned_rational
887 * eg KODAK DC4800 on object_distance tag
889 * FIXME: what exactly is this test trying to do?
890 * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
892 if ((marker->format != EXIF_FORMAT_RATIONAL_UNSIGNED || format != EXIF_FORMAT_RATIONAL) &&
893 (marker->format != EXIF_FORMAT_RATIONAL || format != EXIF_FORMAT_RATIONAL_UNSIGNED) &&
894 /* short fits into a long so allow this mismatch
895 * as well (some tags allowed to be unsigned short _or_ unsigned long)
897 (marker->format != EXIF_FORMAT_LONG_UNSIGNED || format != EXIF_FORMAT_SHORT_UNSIGNED) )
899 if (format < EXIF_FORMAT_COUNT)
901 log_printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
902 marker->key, ExifFormatList[format].short_name,
903 ExifFormatList[marker->format].short_name);
907 log_printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
908 marker->key, format, marker->format,
909 ExifFormatList[marker->format].short_name);
915 /* Where is the data, is it available?
917 if (marker->components > 0 && static_cast<guint>(marker->components) != count)
919 log_printf("warning: exif tag %s has %d elements, exif spec requests %d\n",
920 marker->key, count, marker->components);
923 data_length = ExifFormatList[marker->format].size * count;
926 data_offset = data_val;
927 if (size < data_offset || size < data_offset + data_length)
929 log_printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
935 data_offset = offset + EXIF_TIFD_OFFSET_DATA;
938 item = exif_item_new(marker->format, tag, count, marker);
939 exif_item_copy_data(item, tiff + data_offset, data_length, static_cast<ExifFormatType>(format), bo);
940 exif->items = g_list_prepend(exif->items, item);
942 if (list == ExifKnownMarkersList)
947 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, list);
950 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, ExifKnownGPSInfoMarkersList);
952 case TAG_EXIFMAKERNOTE:
953 format_exif_makernote_parse(exif, tiff, data_val, size, bo);
961 gint exif_parse_IFD_table(ExifData *exif,
962 guchar *tiff, guint offset,
963 guint size, ExifByteOrder bo,
965 const ExifMarker *list)
970 /* limit damage from infinite loops */
971 if (level > EXIF_TIFF_MAX_LEVELS) return -1;
973 /* We should be able to read number of entries in IFD0) */
974 if (size < offset + 2) return -1;
976 count = exif_byte_get_int16(tiff + offset, bo);
979 /* Entries and next IFD offset must be readable */
980 if (size < offset + count * EXIF_TIFD_SIZE + 4) return -1;
982 for (i = 0; i < count; i++)
984 exif_parse_IFD_entry(exif, tiff, offset + i * EXIF_TIFD_SIZE, size, bo, level, list);
991 *-------------------------------------------------------------------
993 *-------------------------------------------------------------------
996 gint exif_tiff_directory_offset(guchar *data, const guint len,
997 guint *offset, ExifByteOrder *bo)
999 if (len < 8) return FALSE;
1001 if (memcmp(data, "II", 2) == 0)
1003 *bo = EXIF_BYTE_ORDER_INTEL;
1005 else if (memcmp(data, "MM", 2) == 0)
1007 *bo = EXIF_BYTE_ORDER_MOTOROLA;
1014 if (exif_byte_get_int16(data + 2, *bo) != 0x002A)
1019 *offset = exif_byte_get_int32(data + 4, *bo);
1021 return (*offset < len);
1024 gint exif_tiff_parse(ExifData *exif, guchar *tiff, guint size, ExifMarker *list)
1029 if (!exif_tiff_directory_offset(tiff, size, &offset, &bo)) return -1;
1031 return exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, list);
1036 *-------------------------------------------------------------------
1038 *-------------------------------------------------------------------
1041 /* jpeg container format:
1042 all data markers start with 0XFF
1043 2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
1044 4 byte long data segment markers in format: 0xFFTTSSSSNNN...
1045 FF: 1 byte standard marker identifier
1046 TT: 1 byte data type
1047 SSSS: 2 bytes in Motorola byte alignment for length of the data.
1048 This value includes these 2 bytes in the count, making actual
1049 length of NN... == SSSS - 2.
1050 NNN.: the data in this segment
1052 static ExifMarker jpeg_color_marker = { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", nullptr, nullptr };
1054 void exif_add_jpeg_color_profile(ExifData *exif, guchar *cp_data, guint cp_length)
1056 ExifItem *item = exif_item_new(jpeg_color_marker.format, jpeg_color_marker.tag, 1,
1057 &jpeg_color_marker);
1059 item->data = cp_data;
1060 item->elements = cp_length;
1061 item->data_len = cp_length;
1062 exif->items = g_list_prepend(exif->items, item);
1066 static gint exif_jpeg_parse(ExifData *exif,
1067 guchar *data, guint size,
1070 guint seg_offset = 0;
1071 guint seg_length = 0;
1075 memcmp(data, "\xFF\xD8", 2) != 0)
1080 if (jpeg_segment_find(data, size, JPEG_MARKER_APP1,
1082 &seg_offset, &seg_length))
1084 res = exif_tiff_parse(exif, data + seg_offset + 6, seg_length - 6, list);
1087 if (exif_jpeg_parse_color(exif, data, size))
1095 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1097 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1098 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1099 return reinterpret_cast<guchar *>(exif_item_get_data(prof_item, data_len));
1104 gchar* exif_get_image_comment(FileData*)
1106 log_printf("%s", _("Can't get image comment: not compiled with Exiv2.\n"));
1107 return g_strdup("");
1110 void exif_set_image_comment(FileData*, const gchar*)
1112 log_printf("%s", _("Can't set image comment: not compiled with Exiv2.\n"));
1117 *-------------------------------------------------------------------
1119 *-------------------------------------------------------------------
1123 ExifItem *exif_get_first_item(ExifData *exif)
1127 auto ret = static_cast<ExifItem *>(exif->items->data);
1128 exif->current = exif->items->next;
1131 exif->current = nullptr;
1135 ExifItem *exif_get_next_item(ExifData *exif)
1139 auto ret = static_cast<ExifItem *>(exif->current->data);
1140 exif->current = exif->current->next;
1146 static gint map_file(const gchar *path, void **mapping, gint *size)
1151 fd = open(path, O_RDONLY);
1158 if (fstat(fd, &fs) == -1)
1167 *mapping = mmap(nullptr, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1168 if (*mapping == MAP_FAILED)
1179 static gint unmap_file(gpointer mapping, gint size)
1181 if (munmap(mapping, size) == -1)
1190 ExifData *exif_get_original(ExifData *processed)
1195 void exif_free(ExifData *exif)
1199 g_list_free_full(exif->items, reinterpret_cast<GDestroyNotify>(exif_item_free));
1204 ExifData *exif_read(gchar *path, gchar *, GHashTable *)
1211 if (!path) return nullptr;
1213 pathl = path_from_utf8(path);
1214 if (map_file(pathl, &f, &size) == -1)
1221 exif = g_new0(ExifData, 1);
1222 exif->path = g_strdup(path);
1224 res = exif_jpeg_parse(exif, static_cast<guchar *>(f), size, ExifKnownMarkersList);
1227 res = exif_tiff_parse(exif, static_cast<guchar *>(f), size, ExifKnownMarkersList);
1232 FormatRawExifType exif_type;
1233 FormatRawExifParseFunc exif_parse_func;
1236 exif_type = format_raw_exif_offset(static_cast<guchar*>(f), size, &offset, &exif_parse_func);
1239 case FORMAT_RAW_EXIF_NONE:
1242 case FORMAT_RAW_EXIF_TIFF:
1243 res = exif_tiff_parse(exif, static_cast<guchar *>(f) + offset, size - offset,
1244 ExifKnownMarkersList);
1246 case FORMAT_RAW_EXIF_JPEG:
1247 res = exif_jpeg_parse(exif, static_cast<guchar *>(f) + offset, size - offset,
1248 ExifKnownMarkersList);
1250 case FORMAT_RAW_EXIF_IFD_II:
1251 case FORMAT_RAW_EXIF_IFD_MM:
1252 res = exif_parse_IFD_table(exif, static_cast<guchar *>(f), offset, size - offset,
1253 (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1254 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1255 0, ExifKnownMarkersList);
1257 case FORMAT_RAW_EXIF_PROPRIETARY:
1258 if (exif_parse_func)
1260 res = exif_parse_func(static_cast<guchar *>(f) + offset, size - offset, exif);
1272 unmap_file(f, size);
1274 if (exif) exif->items = g_list_reverse(exif->items);
1279 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1283 if (!key) return nullptr;
1290 item = static_cast<ExifItem*>(work->data);
1292 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1297 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1300 static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
1302 const ExifMarker *marker;
1308 if (!item) return nullptr;
1310 marker = item->marker;
1311 if (!marker) return nullptr;
1314 ne = item->elements;
1315 if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1316 string = g_string_new("");
1317 switch (item->format)
1319 case EXIF_FORMAT_UNKNOWN:
1321 case EXIF_FORMAT_BYTE_UNSIGNED:
1322 case EXIF_FORMAT_BYTE:
1323 case EXIF_FORMAT_UNDEFINED:
1324 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1329 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1330 item->format == EXIF_FORMAT_UNDEFINED)
1332 val = (static_cast<guchar *>(data))[0];
1336 val = static_cast<guchar>((static_cast<gchar *>(data))[0]);
1339 result = exif_text_list_find_value(marker->list, static_cast<guint>(val));
1340 string = g_string_append(string, result);
1345 string = string_append_raw_bytes(string, data, ne);
1348 case EXIF_FORMAT_STRING:
1349 if (item->data) string = g_string_append(string, (gchar *)(item->data));
1351 case EXIF_FORMAT_SHORT_UNSIGNED:
1352 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1356 result = exif_text_list_find_value(marker->list, (static_cast<guint16 *>(data))[0]);
1357 string = g_string_append(string, result);
1360 else for (i = 0; i < ne; i++)
1362 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1363 (static_cast<guint16 *>(data))[i]);
1366 case EXIF_FORMAT_LONG_UNSIGNED:
1367 for (i = 0; i < ne; i++)
1369 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1370 static_cast<gulong>((static_cast<guint32 *>(data))[i]));
1373 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1374 for (i = 0; i < ne; i++)
1378 r = &(static_cast<ExifRational *>(data))[i];
1379 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1380 static_cast<gulong>(r->num), static_cast<gulong>(r->den));
1383 case EXIF_FORMAT_SHORT:
1384 for (i = 0; i < ne; i++)
1386 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1387 (static_cast<gint16 *>(data))[i]);
1390 case EXIF_FORMAT_LONG:
1391 for (i = 0; i < ne; i++)
1393 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1394 static_cast<glong>((static_cast<gint32 *>(data))[i]));
1397 case EXIF_FORMAT_RATIONAL:
1398 for (i = 0; i < ne; i++)
1402 r = &(static_cast<ExifRational *>(data))[i];
1403 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1404 static_cast<glong>(r->num), static_cast<glong>(r->den));
1407 case EXIF_FORMAT_FLOAT:
1408 for (i = 0; i < ne; i++)
1410 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1411 (static_cast<float *>(data))[i]);
1414 case EXIF_FORMAT_DOUBLE:
1415 for (i = 0; i < ne; i++)
1417 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1418 (static_cast<gdouble *>(data))[i]);
1423 if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1424 item->format != EXIF_FORMAT_STRING)
1426 g_string_append(string, " ...");
1429 return g_string_free(string, FALSE);
1432 gchar *exif_item_get_string(ExifItem *item, gint)
1434 return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
1437 gchar *exif_item_get_data_as_text(ExifItem *item, ExifData *)
1439 return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
1442 gint exif_item_get_integer(ExifItem *item, gint *value)
1444 if (!item) return FALSE;
1445 if (!item->elements) return FALSE;
1447 switch (item->format)
1449 case EXIF_FORMAT_SHORT:
1450 *value = static_cast<gint>((static_cast<gint16 *>(item->data))[0]);
1453 case EXIF_FORMAT_SHORT_UNSIGNED:
1454 *value = static_cast<gint>((static_cast<guint16 *>(item->data))[0]);
1457 case EXIF_FORMAT_LONG:
1458 *value = static_cast<gint>((static_cast<gint32 *>(item->data))[0]);
1461 case EXIF_FORMAT_LONG_UNSIGNED: /**< @FIXME overflow possible */
1462 *value = static_cast<gint>((static_cast<guint32 *>(item->data))[0]);
1465 /* all other type return FALSE */
1472 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1474 if (!item) return nullptr;
1475 if (n >= item->elements) return nullptr;
1477 if (item->format == EXIF_FORMAT_RATIONAL ||
1478 item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1480 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1481 return &(static_cast<ExifRational *>(item->data))[n];
1487 gchar *exif_get_tag_description_by_key(const gchar *key)
1491 if (!key) return nullptr;
1494 while (ExifKnownMarkersList[i].tag > 0)
1496 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1501 while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1503 if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
1510 static void exif_write_item(FILE *f, ExifItem *item, ExifData *exif)
1514 text = exif_item_get_data_as_text(item, exif);
1517 gchar *tag = exif_item_get_tag_name(item);
1518 g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1525 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1527 if (!f || !exif) return;
1529 g_fprintf(f, " tag format key value\n");
1530 g_fprintf(f, "----------------------------------------------------\n");
1532 if (human_readable_list)
1537 while (ExifFormattedList[i].key)
1541 text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, nullptr);
1544 g_fprintf(f, " %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1558 item = static_cast<ExifItem*>(work->data);
1561 exif_write_item(f, item, exif);
1564 g_fprintf(f, "----------------------------------------------------\n");
1567 gboolean exif_write(ExifData *)
1569 log_printf("Not compiled with EXIF write support\n");
1573 gboolean exif_write_sidecar(ExifData *, gchar *)
1575 log_printf("Not compiled with EXIF write support\n");
1580 gint exif_update_metadata(ExifData *, const gchar *, const GList *)
1585 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1590 if (!key) return nullptr;
1592 /* convert xmp key to exif key */
1593 if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
1595 if (format == METADATA_FORMATTED)
1599 text = exif_get_formatted_by_key(exif, key, &key_valid);
1600 if (key_valid) return g_list_append(nullptr, text);
1603 item = exif_get_item(exif, key);
1604 if (!item) return nullptr;
1606 str = exif_item_get_data_as_text_full(item, format);
1608 if (!str) return nullptr;
1610 return g_list_append(nullptr, str);
1620 static GList *exif_unmap_list = nullptr;
1622 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint, gint)
1631 if (!exif) return nullptr;
1634 fd = open(path, O_RDONLY);
1642 if (fstat(fd, &st) == -1)
1647 map_len = st.st_size;
1648 map_data = static_cast<guchar *>(mmap(nullptr, map_len, PROT_READ, MAP_PRIVATE, fd, 0));
1651 if (map_data == MAP_FAILED)
1656 if (format_raw_img_exif_offsets(map_data, map_len, &offset, nullptr) && offset)
1660 DEBUG_1("%s: offset %u", path, offset);
1662 *data_len = map_len - offset;
1663 ud = g_new(UnmapData, 1);
1664 ud->ptr = map_data + offset;
1665 ud->map_data = map_data;
1666 ud->map_len = map_len;
1668 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1672 munmap(map_data, map_len);
1677 void exif_free_preview(guchar *buf)
1679 GList *work = exif_unmap_list;
1683 auto ud = static_cast<UnmapData *>(work->data);
1686 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1692 g_assert_not_reached();
1699 using dummy_variable = int;
1701 /* not HAVE_EXIV2 */
1702 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */