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>
64 #include <glib/gprintf.h>
70 #include "jpeg_parser.h"
72 #include "format_raw.h"
73 #include "ui_fileops.h"
77 *-----------------------------------------------------------------------------
79 *-----------------------------------------------------------------------------
82 ExifFormatAttrib ExifFormatList[] = {
83 { EXIF_FORMAT_UNKNOWN, 1, "unknown", "unknown" },
84 { EXIF_FORMAT_BYTE_UNSIGNED, 1, "ubyte", "unsigned byte" },
85 { EXIF_FORMAT_STRING, 1, "string", "string" },
86 { EXIF_FORMAT_SHORT_UNSIGNED, 2, "ushort", "unsigned short" },
87 { EXIF_FORMAT_LONG_UNSIGNED, 4, "ulong", "unsigned long" },
88 { EXIF_FORMAT_RATIONAL_UNSIGNED,8, "urational", "unsigned rational" },
89 { EXIF_FORMAT_BYTE, 1, "byte", "byte" },
90 { EXIF_FORMAT_UNDEFINED, 1, "undefined", "undefined" },
91 { EXIF_FORMAT_SHORT, 2, "sshort", "signed short" },
92 { EXIF_FORMAT_LONG, 4, "slong", "signed long" },
93 { EXIF_FORMAT_RATIONAL, 8, "srational", "signed rational" },
94 { EXIF_FORMAT_FLOAT, 4, "float", "float" },
95 { EXIF_FORMAT_DOUBLE, 8, "double", "double" },
99 /* tags that are special, or need special treatment */
100 #define TAG_EXIFOFFSET 0x8769
101 #define TAG_EXIFMAKERNOTE 0x927c
102 #define TAG_GPSOFFSET 0x8825
106 *-----------------------------------------------------------------------------
108 *-----------------------------------------------------------------------------
110 static ExifTextList ExifCompressionList[] = {
111 { 1, "Uncompressed" },
113 { 3, "T4/Group 3 Fax" },
114 { 4, "T6/Group 4 Fax" },
116 { 6, "JPEG (old style)" },
118 { 8, "Adobe Deflate" },
120 { 10, "JBIG Color" },
122 { 32771, "CCIRLEW" },
123 { 32773, "PackBits" },
124 { 32809, "ThunderScan" },
125 { 32895, "IT8CTPAD" },
129 { 32908, "PixasFilm" },
130 { 32909, "PixasLog" },
131 { 32946, "Deflate" },
135 { 34677, "SGILog24" },
136 { 34712, "JPEF 2000" },
137 { 34713, "Nikon NEF Compressed" },
141 static ExifTextList ExifOrientationList[] = {
142 { EXIF_ORIENTATION_UNKNOWN, N_("unknown") },
143 { EXIF_ORIENTATION_TOP_LEFT, N_("top left") },
144 { EXIF_ORIENTATION_TOP_RIGHT, N_("top right") },
145 { EXIF_ORIENTATION_BOTTOM_RIGHT,N_("bottom right") },
146 { EXIF_ORIENTATION_BOTTOM_LEFT, N_("bottom left") },
147 { EXIF_ORIENTATION_LEFT_TOP, N_("left top") },
148 { EXIF_ORIENTATION_RIGHT_TOP, N_("right top") },
149 { EXIF_ORIENTATION_RIGHT_BOTTOM,N_("right bottom") },
150 { EXIF_ORIENTATION_LEFT_BOTTOM, N_("left bottom") },
154 static ExifTextList ExifUnitList[] = {
155 { EXIF_UNIT_UNKNOWN, N_("unknown") },
156 { EXIF_UNIT_NOUNIT, "" },
157 { EXIF_UNIT_INCH, N_("inch") },
158 { EXIF_UNIT_CENTIMETER, N_("centimeter") },
162 static ExifTextList ExifYCbCrPosList[] = {
168 static ExifTextList ExifMeteringModeList[] = {
169 { 0, N_("unknown") },
170 { 1, N_("average") },
171 { 2, N_("center weighted") },
173 { 4, N_("multi-spot") },
174 { 5, N_("multi-segment") },
175 { 6, N_("partial") },
176 { 255, N_("other") },
180 static ExifTextList ExifExposureProgramList[] = {
181 { 0, N_("not defined") },
184 { 3, N_("aperture") },
185 { 4, N_("shutter") },
186 { 5, N_("creative") },
188 { 7, N_("portrait") },
189 { 8, N_("landscape") },
193 static ExifTextList ExifLightSourceList[] = {
194 { 0, N_("unknown") },
195 { 1, N_("daylight") },
196 { 2, N_("fluorescent") },
197 { 3, N_("tungsten (incandescent)") },
199 { 9, N_("fine weather") },
200 { 10, N_("cloudy weather") },
202 { 12, N_("daylight fluorescent") },
203 { 13, N_("day white fluorescent") },
204 { 14, N_("cool white fluorescent") },
205 { 15, N_("white fluorescent") },
206 { 17, N_("standard light A") },
207 { 18, N_("standard light B") },
208 { 19, N_("standard light C") },
213 { 24, N_("ISO studio tungsten") },
214 { 255, N_("other") },
218 static ExifTextList ExifFlashList[] = {
221 { 5, N_("yes, not detected by strobe") },
222 { 7, N_("yes, detected by strobe") },
226 static ExifTextList ExifColorSpaceList[] = {
228 { 65535,N_("uncalibrated") },
232 static ExifTextList ExifSensorList[] = {
233 { 1, N_("not defined") },
234 { 2, N_("1 chip color area") },
235 { 2, N_("2 chip color area") },
236 { 4, N_("3 chip color area") },
237 { 5, N_("color sequential area") },
238 { 7, N_("trilinear") },
239 { 8, N_("color sequential linear") },
243 static ExifTextList ExifSourceList[] = {
244 { 3, N_("digital still camera") },
248 static ExifTextList ExifSceneList[] = {
249 { 1, N_("direct photo") },
253 static ExifTextList ExifCustRenderList[] = {
259 static ExifTextList ExifExposureModeList[] = {
262 { 2, N_("auto bracket") },
266 static ExifTextList ExifWhiteBalanceList[] = {
272 static ExifTextList ExifSceneCaptureList[] = {
273 { 0, N_("standard") },
274 { 1, N_("landscape") },
275 { 2, N_("portrait") },
276 { 3, N_("night scene") },
280 static ExifTextList ExifGainControlList[] = {
282 { 1, N_("low gain up") },
283 { 2, N_("high gain up") },
284 { 3, N_("low gain down") },
285 { 4, N_("high gain down") },
289 static ExifTextList ExifContrastList[] = {
296 static ExifTextList ExifSaturationList[] = {
303 static ExifTextList ExifSharpnessList[] = {
310 static ExifTextList ExifSubjectRangeList[] = {
311 { 0, N_("unknown") },
314 { 3, N_("distant") },
319 Tag names should match to exiv2 keys, http://www.exiv2.org/metadata.html
320 Tags that don't match are not supported by exiv2 and should not be used anywhere in the code
323 ExifMarker ExifKnownMarkersList[] = {
324 { 0x0100, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageWidth", N_("Image Width"), NULL },
325 { 0x0101, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ImageLength", N_("Image Height"), NULL },
326 { 0x0102, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.BitsPerSample", N_("Bits per Sample/Pixel"), NULL },
327 { 0x0103, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Compression", N_("Compression"), ExifCompressionList },
328 { 0x010e, EXIF_FORMAT_STRING, -1, "Exif.Image.ImageDescription", N_("Image description"), NULL },
329 { 0x010f, EXIF_FORMAT_STRING, -1, "Exif.Image.Make", N_("Camera make"), NULL },
330 { 0x0110, EXIF_FORMAT_STRING, -1, "Exif.Image.Model", N_("Camera model"), NULL },
331 { 0x0112, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.Orientation", N_("Orientation"), ExifOrientationList },
332 { 0x011a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.XResolution", N_("X resolution"), NULL },
333 { 0x011b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.YResolution", N_("Y Resolution"), NULL },
334 { 0x0128, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.ResolutionUnit", N_("Resolution units"), ExifUnitList },
335 { 0x0131, EXIF_FORMAT_STRING, -1, "Exif.Image.Software", N_("Firmware"), NULL },
336 { 0x0132, EXIF_FORMAT_STRING, 20, "Exif.Image.DateTime", N_("Date"), NULL },
337 { 0x013e, EXIF_FORMAT_RATIONAL_UNSIGNED, 2, "Exif.Image.WhitePoint", N_("White point"), NULL },
338 { 0x013f, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.PrimaryChromaticities",N_("Primary chromaticities"), NULL },
339 { 0x0211, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.Image.YCbCrCoefficients", N_("YCbCy coefficients"), NULL },
340 { 0x0213, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Image.YCbCrPositioning", N_("YCbCr positioning"), ExifYCbCrPosList },
341 { 0x0214, EXIF_FORMAT_RATIONAL_UNSIGNED, 6, "Exif.Image.ReferenceBlackWhite",N_("Black white reference"), NULL },
342 { 0x8298, EXIF_FORMAT_STRING, -1, "Exif.Image.Copyright", N_("Copyright"), NULL },
343 { 0x8769, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.ExifTag", N_("SubIFD Exif offset"), NULL },
345 { 0x829a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureTime", N_("Exposure time (seconds)"), NULL },
346 { 0x829d, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FNumber", N_("FNumber"), NULL },
347 { 0x8822, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureProgram", N_("Exposure program"), ExifExposureProgramList },
348 { 0x8824, EXIF_FORMAT_STRING, -1, "Exif.Photo.SpectralSensitivity",N_("Spectral Sensitivity"), NULL },
349 { 0x8827, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.ISOSpeedRatings", N_("ISO sensitivity"), NULL },
350 { 0x8828, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.OECF", N_("Optoelectric conversion factor"), NULL },
351 { 0x9000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.ExifVersion", N_("Exif version"), NULL },
352 { 0x9003, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeOriginal", N_("Date original"), NULL },
353 { 0x9004, EXIF_FORMAT_STRING, 20, "Exif.Photo.DateTimeDigitized", N_("Date digitized"), NULL },
354 { 0x9101, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.ComponentsConfiguration",N_("Pixel format"), NULL },
355 { 0x9102, EXIF_FORMAT_RATIONAL_UNSIGNED,1, "Exif.Photo.CompressedBitsPerPixel",N_("Compression ratio"), NULL },
356 { 0x9201, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ShutterSpeedValue", N_("Shutter speed"), NULL },
357 { 0x9202, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ApertureValue", N_("Aperture"), NULL },
358 { 0x9203, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.BrightnessValue", N_("Brightness"), NULL },
359 { 0x9204, EXIF_FORMAT_RATIONAL, 1, "Exif.Photo.ExposureBiasValue", N_("Exposure bias"), NULL },
360 { 0x9205, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.MaxApertureValue", N_("Maximum aperture"), NULL },
361 { 0x9206, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.SubjectDistance", N_("Subject distance"), NULL },
362 { 0x9207, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.MeteringMode", N_("Metering mode"), ExifMeteringModeList },
363 { 0x9208, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.LightSource", N_("Light source"), ExifLightSourceList },
364 { 0x9209, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Flash", N_("Flash"), ExifFlashList },
365 { 0x920a, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalLength", N_("Focal length"), NULL },
366 { 0x9214, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SubjectArea", N_("Subject area"), NULL },
367 { 0x927c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.MakerNote", N_("MakerNote"), NULL },
368 { 0x9286, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.UserComment", N_("UserComment"), NULL },
369 { 0x9290, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTime", N_("Subsecond time"), NULL },
370 { 0x9291, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeOriginal",N_("Subsecond time original"), NULL },
371 { 0x9292, EXIF_FORMAT_STRING, -1, "Exif.Photo.SubSecTimeDigitized",N_("Subsecond time digitized"), NULL },
372 { 0xa000, EXIF_FORMAT_UNDEFINED, 4, "Exif.Photo.FlashpixVersion", N_("FlashPix version"), NULL },
373 { 0xa001, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ColorSpace", N_("Colorspace"), ExifColorSpaceList },
374 /* ExifImageWidth, ExifImageHeight can also be unsigned short */
375 { 0xa002, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelXDimension", N_("Width"), NULL },
376 { 0xa003, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Photo.PixelYDimension", N_("Height"), NULL },
377 { 0xa004, EXIF_FORMAT_STRING, -1, "Exif.Photo.RelatedSoundFile", N_("Audio data"), NULL },
378 { 0xa005, EXIF_FORMAT_LONG_UNSIGNED, 1, "ExifInteroperabilityOffset", N_("ExifR98 extension"), NULL },
379 { 0xa20b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", N_("Flash strength"), NULL },
380 { 0xa20c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SpatialFrequencyResponse",N_("Spatial frequency response"), NULL },
381 { 0xa20e, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneXResolution", N_("X Pixel density"), NULL },
382 { 0xa20f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FocalPlaneYResolution", N_("Y Pixel density"), NULL },
383 { 0xa210, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalPlaneResolutionUnit", N_("Pixel density units"), ExifUnitList },
384 { 0x0214, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Photo.SubjectLocation", N_("Subject location"), NULL },
385 { 0xa215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", N_("ISO sensitivity"), NULL },
386 { 0xa217, EXIF_FORMAT_SHORT_UNSIGNED, -1, "Exif.Photo.SensingMethod", N_("Sensor type"), ExifSensorList },
387 { 0xa300, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.FileSource", N_("Source type"), ExifSourceList },
388 { 0xa301, EXIF_FORMAT_UNDEFINED, 1, "Exif.Photo.SceneType", N_("Scene type"), ExifSceneList },
389 { 0xa302, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.CFAPattern", N_("Color filter array pattern"), NULL },
390 /* tags a4xx were added for Exif 2.2 (not just these - some above, as well) */
391 { 0xa401, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.CustomRendered", N_("Render process"), ExifCustRenderList },
392 { 0xa402, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.ExposureMode", N_("Exposure mode"), ExifExposureModeList },
393 { 0xa403, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.WhiteBalance", N_("White balance"), ExifWhiteBalanceList },
394 { 0xa404, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.DigitalZoomRatio", N_("Digital zoom ratio"), NULL },
395 { 0xa405, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.FocalLengthIn35mmFilm",N_("Focal length (35mm)"), NULL },
396 { 0xa406, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SceneCaptureType", N_("Scene capture type"), ExifSceneCaptureList },
397 { 0xa407, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.GainControl", N_("Gain control"), ExifGainControlList },
398 { 0xa408, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Contrast", N_("Contrast"), ExifContrastList },
399 { 0xa409, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Saturation", N_("Saturation"), ExifSaturationList },
400 { 0xa40a, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.Sharpness", N_("Sharpness"), ExifSharpnessList },
401 { 0xa40b, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.DeviceSettingDescription",N_("Device setting"), NULL },
402 { 0xa40c, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Exif.Photo.SubjectDistanceRange",N_("Subject range"), ExifSubjectRangeList },
403 { 0xa420, EXIF_FORMAT_STRING, -1, "Exif.Photo.ImageUniqueID", N_("Image serial number"), NULL },
404 /* place known, but undocumented or lesser used tags here */
405 { 0x00fe, EXIF_FORMAT_LONG_UNSIGNED, 1, "Exif.Image.NewSubfileType", NULL, NULL },
406 { 0x00ff, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SubfileType", NULL, NULL },
407 { 0x012d, EXIF_FORMAT_SHORT_UNSIGNED, 3, "Exif.Image.TransferFunction", NULL, NULL },
408 { 0x013b, EXIF_FORMAT_STRING, -1, "Exif.Image.Artist", "Artist", NULL },
409 { 0x013d, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Predictor", NULL, NULL },
410 { 0x0142, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileWidth", NULL, NULL },
411 { 0x0143, EXIF_FORMAT_SHORT_UNSIGNED, 1, "TileLength", NULL, NULL },
412 { 0x0144, EXIF_FORMAT_LONG_UNSIGNED, -1, "TileOffsets", NULL, NULL },
413 { 0x0145, EXIF_FORMAT_SHORT_UNSIGNED, -1, "TileByteCounts", NULL, NULL },
414 { 0x014a, EXIF_FORMAT_LONG_UNSIGNED, -1, "Exif.Image.SubIFDs", NULL, NULL },
415 { 0x015b, EXIF_FORMAT_UNDEFINED, -1, "JPEGTables", NULL, NULL },
416 { 0x828d, EXIF_FORMAT_SHORT_UNSIGNED, 2, "Exif.Image.CFARepeatPatternDim", NULL, NULL },
417 { 0x828e, EXIF_FORMAT_BYTE_UNSIGNED, -1, "Exif.Image.CFAPattern", NULL, NULL },
418 { 0x828f, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Image.BatteryLevel", NULL, NULL },
419 { 0x83bb, EXIF_FORMAT_LONG_UNSIGNED, -1, "IPTC/NAA", NULL, NULL },
420 { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL },
421 { 0x8825, EXIF_FORMAT_LONG_UNSIGNED, 1, "GPSInfo", "SubIFD GPS offset", NULL },
422 { 0x8829, EXIF_FORMAT_SHORT_UNSIGNED, 1, "Interlace", NULL, NULL },
423 { 0x882a, EXIF_FORMAT_SHORT, 1, "TimeZoneOffset", NULL, NULL },
424 { 0x882b, EXIF_FORMAT_SHORT_UNSIGNED, 1, "SelfTimerMode", NULL, NULL },
425 { 0x920b, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.FlashEnergy", NULL, NULL },
426 { 0x920c, EXIF_FORMAT_UNDEFINED, -1, "Exif.Photo.SpatialFrequencyResponse", NULL, NULL },
427 { 0x920d, EXIF_FORMAT_UNDEFINED, -1, "Noise", NULL, NULL },
428 { 0x9211, EXIF_FORMAT_LONG_UNSIGNED, 1, "ImageNumber", NULL, NULL },
429 { 0x9212, EXIF_FORMAT_STRING, 1, "SecurityClassification", NULL, NULL },
430 { 0x9213, EXIF_FORMAT_STRING, -1, "ImageHistory", NULL, NULL },
431 { 0x9215, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.Photo.ExposureIndex", NULL, NULL },
432 { 0x9216, EXIF_FORMAT_BYTE_UNSIGNED, 4, "TIFF/EPStandardID", NULL, NULL },
437 ExifMarker ExifKnownGPSInfoMarkersList[] = {
438 /* The following do not work at the moment as the tag value 0x0000 has a
439 * special meaning. */
440 /* { 0x0000, EXIF_FORMAT_BYTE, -1, "Exif.GPSInfo.GPSVersionID", NULL, NULL }, */
441 { 0x0001, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLatitudeRef", NULL, NULL },
442 { 0x0002, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLatitude", NULL, NULL },
443 { 0x0003, EXIF_FORMAT_STRING, 2, "Exif.GPSInfo.GPSLongitudeRef", NULL, NULL },
444 { 0x0004, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSLongitude", NULL, NULL },
445 { 0x0005, EXIF_FORMAT_BYTE_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitudeRef", NULL, NULL },
446 { 0x0006, EXIF_FORMAT_RATIONAL_UNSIGNED, 1, "Exif.GPSInfo.GPSAltitude", NULL, NULL },
447 { 0x0007, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSTimeStamp", NULL, NULL },
448 { 0x0008, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSatellites", NULL, NULL },
449 { 0x0009, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSStatus", NULL, NULL },
450 { 0x000a, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMeasureMode", NULL, NULL },
451 { 0x000b, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDOP", NULL, NULL },
452 { 0x000c, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSSpeedRef", NULL, NULL },
453 { 0x000d, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSSpeed", NULL, NULL },
454 { 0x000e, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSTrackRef", NULL, NULL },
455 { 0x000f, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSTrack", NULL, NULL },
456 { 0x0010, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSImgDirectionRef", NULL, NULL },
457 { 0x0011, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSImgDirection", NULL, NULL },
458 { 0x0012, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSMapDatum", NULL, NULL },
459 { 0x0013, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLatitudeRef", NULL, NULL },
460 { 0x0014, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLatitude", NULL, NULL },
461 { 0x0015, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestLongitudeRef", NULL, NULL },
462 { 0x0016, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestLongitude", NULL, NULL },
463 { 0x0017, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestBearingRef", NULL, NULL },
464 { 0x0018, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestBearing", NULL, NULL },
465 { 0x0019, EXIF_FORMAT_STRING, -1, "Exif.GPSInfo.GPSDestDistanceRef", NULL, NULL },
466 { 0x001a, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "Exif.GPSInfo.GPSDestDistance", NULL, NULL },
467 { 0x001b, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSProcessingMethod", NULL, NULL },
468 { 0x001c, EXIF_FORMAT_UNDEFINED, -1, "Exif.GPSInfo.GPSAreaInformation", NULL, NULL },
469 { 0x001d, EXIF_FORMAT_RATIONAL_UNSIGNED, 3, "Exif.GPSInfo.GPSDateStamp", NULL, NULL },
470 { 0x001e, EXIF_FORMAT_SHORT, -1, "Exif.GPSInfo.GPSDifferential", NULL, NULL },
475 ExifMarker ExifUnknownMarkersList[] = {
476 { 0x0000, EXIF_FORMAT_UNKNOWN, 0, "unknown", NULL, NULL },
477 { 0x0000, EXIF_FORMAT_BYTE_UNSIGNED, -1, "unknown", NULL, NULL },
478 { 0x0000, EXIF_FORMAT_STRING, -1, "unknown", NULL, NULL },
479 { 0x0000, EXIF_FORMAT_SHORT_UNSIGNED, -1, "unknown", NULL, NULL },
480 { 0x0000, EXIF_FORMAT_LONG_UNSIGNED, -1, "unknown", NULL, NULL },
481 { 0x0000, EXIF_FORMAT_RATIONAL_UNSIGNED, -1, "unknown", NULL, NULL },
482 { 0x0000, EXIF_FORMAT_BYTE, -1, "unknown", NULL, NULL },
483 { 0x0000, EXIF_FORMAT_UNDEFINED, -1, "unknown", NULL, NULL },
484 { 0x0000, EXIF_FORMAT_SHORT, -1, "unknown", NULL, NULL },
485 { 0x0000, EXIF_FORMAT_LONG, -1, "unknown", NULL, NULL },
486 { 0x0000, EXIF_FORMAT_RATIONAL, -1, "unknown", NULL, NULL },
487 { 0x0000, EXIF_FORMAT_FLOAT, -1, "unknown", NULL, NULL },
488 { 0x0000, EXIF_FORMAT_DOUBLE, -1, "unknown", NULL, NULL },
491 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list);
494 *-----------------------------------------------------------------------------
496 *-----------------------------------------------------------------------------
499 ExifItem *exif_item_new(ExifFormatType format, guint tag,
500 guint elements, const ExifMarker *marker)
504 item = g_new0(ExifItem, 1);
505 item->format = format;
507 item->marker = marker;
508 item->elements = elements;
512 case EXIF_FORMAT_UNKNOWN:
513 /* unknown, data is NULL */
516 case EXIF_FORMAT_BYTE_UNSIGNED:
517 item->data_len = sizeof(gchar) * elements;
519 case EXIF_FORMAT_STRING:
520 item->data_len = sizeof(gchar) * elements;
522 case EXIF_FORMAT_SHORT_UNSIGNED:
523 item->data_len = sizeof(guint16) * elements;
525 case EXIF_FORMAT_LONG_UNSIGNED:
526 item->data_len = sizeof(guint32) * elements;
528 case EXIF_FORMAT_RATIONAL_UNSIGNED:
529 item->data_len = sizeof(ExifRational) * elements;
531 case EXIF_FORMAT_BYTE:
532 item->data_len = sizeof(gchar) * elements;
534 case EXIF_FORMAT_UNDEFINED:
535 item->data_len = sizeof(gchar) * elements;
537 case EXIF_FORMAT_SHORT:
538 item->data_len = sizeof(gint16) * elements;
540 case EXIF_FORMAT_LONG:
541 item->data_len = sizeof(gint32) * elements;
543 case EXIF_FORMAT_RATIONAL:
544 item->data_len = sizeof(ExifRational) * elements;
546 case EXIF_FORMAT_FLOAT:
547 item->data_len = sizeof(float) * elements;
549 case EXIF_FORMAT_DOUBLE:
550 item->data_len = sizeof(gdouble) * elements;
554 item->data = g_malloc0(item->data_len);
559 static void exif_item_free(ExifItem *item)
567 gchar *exif_item_get_tag_name(ExifItem *item)
569 if (!item || !item->marker) return NULL;
570 return g_strdup(item->marker->key);
573 guint exif_item_get_tag_id(ExifItem *item)
579 guint exif_item_get_elements(ExifItem *item)
582 return item->elements;
585 gchar *exif_item_get_data(ExifItem *item, guint *data_len)
588 *data_len = item->data_len;
589 return g_memdup(item->data, item->data_len);
592 guint exif_item_get_format_id(ExifItem *item)
594 if (!item) return EXIF_FORMAT_UNKNOWN;
599 gchar *exif_item_get_description(ExifItem *item)
601 if (!item || !item->marker) return NULL;
602 return g_strdup(_(item->marker->description));
605 const gchar *exif_item_get_format_name(ExifItem *item, gboolean brief)
607 if (!item || !item->marker) return NULL;
608 return (brief) ? ExifFormatList[item->format].short_name : ExifFormatList[item->format].description;
611 static GString *string_append_raw_bytes(GString *string, gpointer data, gint ne)
615 for (i = 0 ; i < ne; i++)
617 guchar c = ((gchar *)data)[i];
618 if (c < 32 || c > 127) c = '.';
619 g_string_append_printf(string, "%c", c);
621 string = g_string_append(string, " : ");
622 for (i = 0 ; i < ne; i++)
640 g_string_append_printf(string, "%s%02x", spacer, ((gchar *)data)[i]);
647 gchar *exif_text_list_find_value(ExifTextList *list, guint value)
649 gchar *result = NULL;
653 while (!result && list[i].value >= 0)
655 if (value == (guint) list[i].value) result = g_strdup(_(list[i].description));
658 if (!result) result = g_strdup_printf("%d (%s)", value, _("unknown"));
665 *-------------------------------------------------------------------
667 *-------------------------------------------------------------------
670 /* note: the align_buf is used to avoid alignment issues (on sparc) */
672 guint16 exif_byte_get_int16(guchar *f, ExifByteOrder bo)
676 memcpy(&align_buf, f, sizeof(guint16));
678 if (bo == EXIF_BYTE_ORDER_INTEL)
679 return GUINT16_FROM_LE(align_buf);
681 return GUINT16_FROM_BE(align_buf);
684 guint32 exif_byte_get_int32(guchar *f, ExifByteOrder bo)
688 memcpy(&align_buf, f, sizeof(guint32));
690 if (bo == EXIF_BYTE_ORDER_INTEL)
691 return GUINT32_FROM_LE(align_buf);
693 return GUINT32_FROM_BE(align_buf);
696 void exif_byte_put_int16(guchar *f, guint16 n, ExifByteOrder bo)
700 if (bo == EXIF_BYTE_ORDER_INTEL)
702 align_buf = GUINT16_TO_LE(n);
706 align_buf = GUINT16_TO_BE(n);
709 memcpy(f, &align_buf, sizeof(guint16));
712 void exif_byte_put_int32(guchar *f, guint32 n, ExifByteOrder bo)
716 if (bo == EXIF_BYTE_ORDER_INTEL)
718 align_buf = GUINT32_TO_LE(n);
722 align_buf = GUINT32_TO_BE(n);
725 memcpy(f, &align_buf, sizeof(guint32));
730 *-------------------------------------------------------------------
732 *-------------------------------------------------------------------
735 static const ExifMarker *exif_marker_from_tag(guint16 tag, const ExifMarker *list)
739 if (!list) return NULL;
741 while (list[i].tag != 0 && list[i].tag != tag)
746 return (list[i].tag == 0 ? NULL : &list[i]);
749 static void rational_from_data(ExifRational *r, gpointer src, ExifByteOrder bo)
751 r->num = exif_byte_get_int32(src, bo);
752 r->den = exif_byte_get_int32(src + sizeof(guint32), bo);
755 /* src_format and item->format must be compatible
756 * and not overrun src or item->data.
758 void exif_item_copy_data(ExifItem *item, gpointer src, guint len,
759 ExifFormatType src_format, ExifByteOrder bo)
766 bs = ExifFormatList[item->format].size;
771 ExifFormatList[src_format].size * ne > len)
773 gchar *tag = exif_item_get_tag_name(item);
774 log_printf("exif tag %s data size mismatch\n", tag);
779 switch (item->format)
781 case EXIF_FORMAT_UNKNOWN:
783 case EXIF_FORMAT_BYTE_UNSIGNED:
784 case EXIF_FORMAT_BYTE:
785 case EXIF_FORMAT_UNDEFINED:
786 memcpy(dest, src, len);
788 case EXIF_FORMAT_STRING:
789 memcpy(dest, src, len);
790 /* string is NULL terminated, make sure this is true */
791 if (((gchar *)dest)[len - 1] != '\0') ((gchar *)dest)[len - 1] = '\0';
793 case EXIF_FORMAT_SHORT_UNSIGNED:
794 case EXIF_FORMAT_SHORT:
795 for (i = 0; i < ne; i++)
797 ((guint16 *)dest)[i] = exif_byte_get_int16(src + i * bs, bo);
800 case EXIF_FORMAT_LONG_UNSIGNED:
801 case EXIF_FORMAT_LONG:
802 if (src_format == EXIF_FORMAT_SHORT_UNSIGNED ||
803 src_format == EXIF_FORMAT_SHORT)
805 /* a short fits into a long, so allow it */
808 ss = ExifFormatList[src_format].size;
809 for (i = 0; i < ne; i++)
811 ((gint32 *)dest)[i] =
812 (gint32)exif_byte_get_int16(src + i * ss, bo);
817 for (i = 0; i < ne; i++)
819 ((gint32 *)dest)[i] =
820 exif_byte_get_int32(src + i * bs, bo);
824 case EXIF_FORMAT_RATIONAL_UNSIGNED:
825 case EXIF_FORMAT_RATIONAL:
826 for (i = 0; i < ne; i++)
828 rational_from_data(&((ExifRational *)dest)[i], src + i * bs, bo);
831 case EXIF_FORMAT_FLOAT:
832 for (i = 0; i < ne; i++)
834 ((float *)dest)[i] = exif_byte_get_int32(src + i * bs, bo);
837 case EXIF_FORMAT_DOUBLE:
838 for (i = 0; i < ne; i++)
842 rational_from_data(&r, src + i * bs, bo);
843 if (r.den) ((gdouble *)dest)[i] = (gdouble)r.num / r.den;
849 static gint exif_parse_IFD_entry(ExifData *exif, guchar *tiff, guint offset,
850 guint size, ExifByteOrder bo,
852 const ExifMarker *list)
860 const ExifMarker *marker;
863 tag = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_TAG, bo);
864 format = exif_byte_get_int16(tiff + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
865 count = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_COUNT, bo);
866 data_val = exif_byte_get_int32(tiff + offset + EXIF_TIFD_OFFSET_DATA, bo);
868 /* Check tag type. If it does not match, either the format is wrong,
869 * either it is a unknown tag; so it is not really an error.
871 marker = exif_marker_from_tag(tag, list);
874 if (format >= EXIF_FORMAT_COUNT)
876 log_printf("warning: exif tag 0x%4x has invalid format %d\n", tag, format);
879 /* allow non recognized tags to be displayed */
880 marker = &ExifUnknownMarkersList[format];
882 if (marker->format != format)
884 /* Some cameras got mixed up signed/unsigned_rational
885 * eg KODAK DC4800 on object_distance tag
887 * FIXME: what exactly is this test trying to do?
888 * ok, so this test is to allow the case of swapped signed/unsigned mismatch to leak through?
890 if (!(marker->format == EXIF_FORMAT_RATIONAL_UNSIGNED && format == EXIF_FORMAT_RATIONAL) &&
891 !(marker->format == EXIF_FORMAT_RATIONAL && format == EXIF_FORMAT_RATIONAL_UNSIGNED) &&
892 /* short fits into a long so allow this mismatch
893 * as well (some tags allowed to be unsigned short _or_ unsigned long)
895 !(marker->format == EXIF_FORMAT_LONG_UNSIGNED && format == EXIF_FORMAT_SHORT_UNSIGNED) )
897 if (format < EXIF_FORMAT_COUNT)
899 log_printf("warning: exif tag %s format mismatch, found %s exif spec requests %s\n",
900 marker->key, ExifFormatList[format].short_name,
901 ExifFormatList[marker->format].short_name);
905 log_printf("warning: exif tag %s format mismatch, found unknown id %d exif spec requests %d (%s)\n",
906 marker->key, format, marker->format,
907 ExifFormatList[marker->format].short_name);
913 /* Where is the data, is it available?
915 if (marker->components > 0 && (guint) marker->components != count)
917 log_printf("warning: exif tag %s has %d elements, exif spec requests %d\n",
918 marker->key, count, marker->components);
921 data_length = ExifFormatList[marker->format].size * count;
924 data_offset = data_val;
925 if (size < data_offset || size < data_offset + data_length)
927 log_printf("warning: exif tag %s data will overrun end of file, ignored.\n", marker->key);
933 data_offset = offset + EXIF_TIFD_OFFSET_DATA;
936 item = exif_item_new(marker->format, tag, count, marker);
937 exif_item_copy_data(item, tiff + data_offset, data_length, format, bo);
938 exif->items = g_list_prepend(exif->items, item);
940 if (list == ExifKnownMarkersList)
945 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, list);
948 exif_parse_IFD_table(exif, tiff, data_val, size, bo, level + 1, ExifKnownGPSInfoMarkersList);
950 case TAG_EXIFMAKERNOTE:
951 format_exif_makernote_parse(exif, tiff, data_val, size, bo);
959 gint exif_parse_IFD_table(ExifData *exif,
960 guchar *tiff, guint offset,
961 guint size, ExifByteOrder bo,
963 const ExifMarker *list)
968 /* limit damage from infinite loops */
969 if (level > EXIF_TIFF_MAX_LEVELS) return -1;
971 /* We should be able to read number of entries in IFD0) */
972 if (size < offset + 2) return -1;
974 count = exif_byte_get_int16(tiff + offset, bo);
977 /* Entries and next IFD offset must be readable */
978 if (size < offset + count * EXIF_TIFD_SIZE + 4) return -1;
980 for (i = 0; i < count; i++)
982 exif_parse_IFD_entry(exif, tiff, offset + i * EXIF_TIFD_SIZE, size, bo, level, list);
989 *-------------------------------------------------------------------
991 *-------------------------------------------------------------------
994 gint exif_tiff_directory_offset(guchar *data, const guint len,
995 guint *offset, ExifByteOrder *bo)
997 if (len < 8) return FALSE;
999 if (memcmp(data, "II", 2) == 0)
1001 *bo = EXIF_BYTE_ORDER_INTEL;
1003 else if (memcmp(data, "MM", 2) == 0)
1005 *bo = EXIF_BYTE_ORDER_MOTOROLA;
1012 if (exif_byte_get_int16(data + 2, *bo) != 0x002A)
1017 *offset = exif_byte_get_int32(data + 4, *bo);
1019 return (*offset < len);
1022 gint exif_tiff_parse(ExifData *exif, guchar *tiff, guint size, ExifMarker *list)
1027 if (!exif_tiff_directory_offset(tiff, size, &offset, &bo)) return -1;
1029 return exif_parse_IFD_table(exif, tiff, offset, size, bo, 0, list);
1034 *-------------------------------------------------------------------
1036 *-------------------------------------------------------------------
1039 #define JPEG_MARKER 0xFF
1040 #define JPEG_MARKER_SOI 0xD8
1041 #define JPEG_MARKER_EOI 0xD9
1042 #define JPEG_MARKER_APP1 0xE1
1043 #define JPEG_MARKER_APP2 0xE2
1045 /* jpeg container format:
1046 all data markers start with 0XFF
1047 2 byte long file start and end markers: 0xFFD8(SOI) and 0XFFD9(EOI)
1048 4 byte long data segment markers in format: 0xFFTTSSSSNNN...
1049 FF: 1 byte standard marker identifier
1050 TT: 1 byte data type
1051 SSSS: 2 bytes in Motorola byte alignment for length of the data.
1052 This value includes these 2 bytes in the count, making actual
1053 length of NN... == SSSS - 2.
1054 NNN.: the data in this segment
1056 static ExifMarker jpeg_color_marker = { 0x8773, EXIF_FORMAT_UNDEFINED, -1, "Exif.Image.InterColorProfile", NULL, NULL };
1058 void exif_add_jpeg_color_profile(ExifData *exif, guchar *cp_data, guint cp_length)
1060 ExifItem *item = exif_item_new(jpeg_color_marker.format, jpeg_color_marker.tag, 1,
1061 &jpeg_color_marker);
1063 item->data = cp_data;
1064 item->elements = cp_length;
1065 item->data_len = cp_length;
1066 exif->items = g_list_prepend(exif->items, item);
1070 static gint exif_jpeg_parse(ExifData *exif,
1071 guchar *data, guint size,
1074 guint seg_offset = 0;
1075 guint seg_length = 0;
1079 memcmp(data, "\xFF\xD8", 2) != 0)
1084 if (jpeg_segment_find(data, size, JPEG_MARKER_APP1,
1086 &seg_offset, &seg_length))
1088 res = exif_tiff_parse(exif, data + seg_offset + 6, seg_length - 6, list);
1091 if (exif_jpeg_parse_color(exif, data, size))
1099 guchar *exif_get_color_profile(ExifData *exif, guint *data_len)
1101 ExifItem *prof_item = exif_get_item(exif, "Exif.Image.InterColorProfile");
1102 if (prof_item && exif_item_get_format_id(prof_item) == EXIF_FORMAT_UNDEFINED)
1103 return (guchar *) exif_item_get_data(prof_item, data_len);
1108 gchar* exif_get_image_comment(FileData* fd)
1110 log_printf("%s", _("Can't get image comment: not compiled with Exiv2.\n"));
1111 return g_strdup("");
1114 void exif_set_image_comment(FileData* fd, const gchar* comment)
1116 log_printf("%s", _("Can't set image comment: not compiled with Exiv2.\n"));
1121 *-------------------------------------------------------------------
1123 *-------------------------------------------------------------------
1127 ExifItem *exif_get_first_item(ExifData *exif)
1131 ExifItem *ret = (ExifItem *)exif->items->data;
1132 exif->current = exif->items->next;
1135 exif->current = NULL;
1139 ExifItem *exif_get_next_item(ExifData *exif)
1143 ExifItem *ret = (ExifItem *)exif->current->data;
1144 exif->current = exif->current->next;
1150 static gint map_file(const gchar *path, void **mapping, gint *size)
1155 fd = open(path, O_RDONLY);
1162 if (fstat(fd, &fs) == -1)
1171 *mapping = mmap(0, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1172 if (*mapping == MAP_FAILED)
1183 static gint unmap_file(gpointer mapping, gint size)
1185 if (munmap(mapping, size) == -1)
1194 ExifData *exif_get_original(ExifData *processed)
1199 void exif_free(ExifData *exif)
1208 ExifItem *item = work->data;
1210 exif_item_free(item);
1213 g_list_free(exif->items);
1218 ExifData *exif_read(gchar *path, gchar *sidecar_path, GHashTable *modified_xmp)
1225 if (!path) return NULL;
1227 pathl = path_from_utf8(path);
1228 if (map_file(pathl, &f, &size) == -1)
1235 exif = g_new0(ExifData, 1);
1236 exif->path = g_strdup(path);
1238 res = exif_jpeg_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1241 res = exif_tiff_parse(exif, (guchar *)f, size, ExifKnownMarkersList);
1246 FormatRawExifType exif_type;
1247 FormatRawExifParseFunc exif_parse_func;
1250 exif_type = format_raw_exif_offset(f, size, &offset, &exif_parse_func);
1253 case FORMAT_RAW_EXIF_NONE:
1256 case FORMAT_RAW_EXIF_TIFF:
1257 res = exif_tiff_parse(exif, (guchar *)f + offset, size - offset,
1258 ExifKnownMarkersList);
1260 case FORMAT_RAW_EXIF_JPEG:
1261 res = exif_jpeg_parse(exif, (guchar *)f + offset, size - offset,
1262 ExifKnownMarkersList);
1264 case FORMAT_RAW_EXIF_IFD_II:
1265 case FORMAT_RAW_EXIF_IFD_MM:
1266 res = exif_parse_IFD_table(exif, (guchar *)f, offset, size - offset,
1267 (exif_type == FORMAT_RAW_EXIF_IFD_II) ?
1268 EXIF_BYTE_ORDER_INTEL : EXIF_BYTE_ORDER_MOTOROLA,
1269 0, ExifKnownMarkersList);
1271 case FORMAT_RAW_EXIF_PROPRIETARY:
1272 if (exif_parse_func)
1274 res = exif_parse_func((guchar *)f + offset, size - offset, exif);
1286 unmap_file(f, size);
1288 if (exif) exif->items = g_list_reverse(exif->items);
1293 ExifItem *exif_get_item(ExifData *exif, const gchar *key)
1297 if (!key) return NULL;
1306 if (item->marker->key && strcmp(key, item->marker->key) == 0) return item;
1311 #define EXIF_DATA_AS_TEXT_MAX_COUNT 16
1314 static gchar *exif_item_get_data_as_text_full(ExifItem *item, MetadataFormat format)
1316 const ExifMarker *marker;
1323 if (!item) return NULL;
1325 marker = item->marker;
1326 if (!marker) return NULL;
1329 ne = item->elements;
1330 if (ne > EXIF_DATA_AS_TEXT_MAX_COUNT) ne = EXIF_DATA_AS_TEXT_MAX_COUNT;
1331 string = g_string_new("");
1332 switch (item->format)
1334 case EXIF_FORMAT_UNKNOWN:
1336 case EXIF_FORMAT_BYTE_UNSIGNED:
1337 case EXIF_FORMAT_BYTE:
1338 case EXIF_FORMAT_UNDEFINED:
1339 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1344 if (item->format == EXIF_FORMAT_BYTE_UNSIGNED ||
1345 item->format == EXIF_FORMAT_UNDEFINED)
1347 val = ((guchar *)data)[0];
1351 val = (guchar)(((gchar *)data)[0]);
1354 result = exif_text_list_find_value(marker->list, (guint)val);
1355 string = g_string_append(string, result);
1360 string = string_append_raw_bytes(string, data, ne);
1363 case EXIF_FORMAT_STRING:
1364 if (item->data) string = g_string_append(string, (gchar *)(item->data));
1366 case EXIF_FORMAT_SHORT_UNSIGNED:
1367 if (ne == 1 && marker->list && format == METADATA_FORMATTED)
1371 result = exif_text_list_find_value(marker->list, ((guint16 *)data)[0]);
1372 string = g_string_append(string, result);
1375 else for (i = 0; i < ne; i++)
1377 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1378 ((guint16 *)data)[i]);
1381 case EXIF_FORMAT_LONG_UNSIGNED:
1382 for (i = 0; i < ne; i++)
1384 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1385 (gulong)((guint32 *)data)[i]);
1388 case EXIF_FORMAT_RATIONAL_UNSIGNED:
1389 for (i = 0; i < ne; i++)
1393 r = &((ExifRational *)data)[i];
1394 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1395 (gulong)r->num, (gulong)r->den);
1398 case EXIF_FORMAT_SHORT:
1399 for (i = 0; i < ne; i++)
1401 g_string_append_printf(string, "%s%hd", (i > 0) ? ", " : "",
1402 ((gint16 *)data)[i]);
1405 case EXIF_FORMAT_LONG:
1406 for (i = 0; i < ne; i++)
1408 g_string_append_printf(string, "%s%ld", (i > 0) ? ", " : "",
1409 (glong)((gint32 *)data)[i]);
1412 case EXIF_FORMAT_RATIONAL:
1413 for (i = 0; i < ne; i++)
1417 r = &((ExifRational *)data)[i];
1418 g_string_append_printf(string, "%s%ld/%ld", (i > 0) ? ", " : "",
1419 (glong)r->num, (glong)r->den);
1422 case EXIF_FORMAT_FLOAT:
1423 for (i = 0; i < ne; i++)
1425 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1426 ((float *)data)[i]);
1429 case EXIF_FORMAT_DOUBLE:
1430 for (i = 0; i < ne; i++)
1432 g_string_append_printf(string, "%s%f", (i > 0) ? ", " : "",
1433 ((gdouble *)data)[i]);
1438 if (item->elements > EXIF_DATA_AS_TEXT_MAX_COUNT &&
1439 item->format != EXIF_FORMAT_STRING)
1441 g_string_append(string, " ...");
1445 g_string_free(string, FALSE);
1450 gchar *exif_item_get_string(ExifItem *item, gint idx)
1452 return exif_item_get_data_as_text_full(item, METADATA_PLAIN);
1455 gchar *exif_item_get_data_as_text(ExifItem *item)
1457 return exif_item_get_data_as_text_full(item, METADATA_FORMATTED);
1460 gint exif_item_get_integer(ExifItem *item, gint *value)
1462 if (!item) return FALSE;
1463 if (!item->elements) return FALSE;
1465 switch (item->format)
1467 case EXIF_FORMAT_SHORT:
1468 *value = (gint)(((gint16 *)(item->data))[0]);
1471 case EXIF_FORMAT_SHORT_UNSIGNED:
1472 *value = (gint)(((guint16 *)(item->data))[0]);
1475 case EXIF_FORMAT_LONG:
1476 *value = (gint)(((gint32 *)(item->data))[0]);
1479 case EXIF_FORMAT_LONG_UNSIGNED: /**< @FIXME overflow possible */
1480 *value = (gint)(((guint32 *)(item->data))[0]);
1483 /* all other type return FALSE */
1490 ExifRational *exif_item_get_rational(ExifItem *item, gint *sign, guint n)
1492 if (!item) return NULL;
1493 if (n >= item->elements) return NULL;
1495 if (item->format == EXIF_FORMAT_RATIONAL ||
1496 item->format == EXIF_FORMAT_RATIONAL_UNSIGNED)
1498 if (sign) *sign = (item->format == EXIF_FORMAT_RATIONAL);
1499 return &((ExifRational *)(item->data))[n];
1505 gchar *exif_get_tag_description_by_key(const gchar *key)
1509 if (!key) return NULL;
1512 while (ExifKnownMarkersList[i].tag > 0)
1514 if (strcmp(key, ExifKnownMarkersList[i].key) == 0) return g_strdup(_(ExifKnownMarkersList[i].description));
1519 while (ExifKnownGPSInfoMarkersList[i].tag > 0)
1521 if (strcmp(key, ExifKnownGPSInfoMarkersList[i].key) == 0) return g_strdup(_(ExifKnownGPSInfoMarkersList[i].description));
1528 static void exif_write_item(FILE *f, ExifItem *item)
1532 text = exif_item_get_data_as_text(item);
1535 gchar *tag = exif_item_get_tag_name(item);
1536 g_fprintf(f, "%4x %9s %30s %s\n", item->tag, ExifFormatList[item->format].short_name,
1543 void exif_write_data_list(ExifData *exif, FILE *f, gint human_readable_list)
1545 if (!f || !exif) return;
1547 g_fprintf(f, " tag format key value\n");
1548 g_fprintf(f, "----------------------------------------------------\n");
1550 if (human_readable_list)
1555 while (ExifFormattedList[i].key)
1559 text = exif_get_formatted_by_key(exif, ExifFormattedList[i].key, NULL);
1562 g_fprintf(f, " %9s %30s %s\n", "string", ExifFormattedList[i].key, text);
1579 exif_write_item(f, item);
1582 g_fprintf(f, "----------------------------------------------------\n");
1585 gboolean exif_write(ExifData *exif)
1587 log_printf("Not compiled with EXIF write support\n");
1591 gboolean exif_write_sidecar(ExifData *exif, gchar *path)
1593 log_printf("Not compiled with EXIF write support\n");
1598 gint exif_update_metadata(ExifData *exif, const gchar *key, const GList *values)
1603 GList *exif_get_metadata(ExifData *exif, const gchar *key, MetadataFormat format)
1608 if (!key) return NULL;
1610 /* convert xmp key to exif key */
1611 if (strcmp(key, "Xmp.tiff.Orientation") == 0) key = "Exif.Image.Orientation";
1613 if (format == METADATA_FORMATTED)
1617 text = exif_get_formatted_by_key(exif, key, &key_valid);
1618 if (key_valid) return g_list_append(NULL, text);
1621 item = exif_get_item(exif, key);
1622 if (!item) return NULL;
1624 str = exif_item_get_data_as_text_full(item, format);
1626 if (!str) return NULL;
1628 return g_list_append(NULL, str);
1631 typedef struct _UnmapData UnmapData;
1639 static GList *exif_unmap_list = 0;
1641 guchar *exif_get_preview(ExifData *exif, guint *data_len, gint requested_width, gint requested_height)
1650 if (!exif) return NULL;
1653 fd = open(path, O_RDONLY);
1661 if (fstat(fd, &st) == -1)
1666 map_len = st.st_size;
1667 map_data = (guchar *) mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
1670 if (map_data == MAP_FAILED)
1675 if (format_raw_img_exif_offsets(map_data, map_len, &offset, NULL) && offset)
1679 DEBUG_1("%s: offset %u", path, offset);
1681 *data_len = map_len - offset;
1682 ud = g_new(UnmapData, 1);
1683 ud->ptr = map_data + offset;
1684 ud->map_data = map_data;
1685 ud->map_len = map_len;
1687 exif_unmap_list = g_list_prepend(exif_unmap_list, ud);
1691 munmap(map_data, map_len);
1696 void exif_free_preview(guchar *buf)
1698 GList *work = exif_unmap_list;
1702 UnmapData *ud = (UnmapData *)work->data;
1705 exif_unmap_list = g_list_remove_link(exif_unmap_list, work);
1711 g_assert_not_reached();
1714 void exif_init(void)
1719 /* not HAVE_EXIV2 */
1720 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */