| 1 |
/*-------------------------------------------------------------------------- |
| 2 |
This file contains subroutines for use by Jpegtopnm to handle the |
| 3 |
EXIF header. |
| 4 |
|
| 5 |
The code is adapted from the program Jhead by Matthaias Wandel |
| 6 |
December 1999 - August 2000, and contributed to the public domain. |
| 7 |
Bryan Henderson adapted it to Netpbm in September 2001. Bryan |
| 8 |
added more of Wandel's code, from Jhead 1.9 dated December 2002 in |
| 9 |
January 2003. |
| 10 |
|
| 11 |
Bryan fundamentally rewrote it in March 2023 because it wasn't properly |
| 12 |
dealing with the main image vs thumbnail IFDs. |
| 13 |
--------------------------------------------------------------------------*/ |
| 14 |
|
| 15 |
/* |
| 16 |
N.B. "EXIF" refers to a whole image file format; some people think it is |
| 17 |
just the format of the camera model, orientation, etc. data within the |
| 18 |
image file. EXIF is a subset of JFIF; an EXIF file is JFIF file |
| 19 |
containing an EXIF header in the form of a JFIF APP1 marker. |
| 20 |
|
| 21 |
An EXIF header is generated by some digital cameras and contains information |
| 22 |
about the circumstances of the creation of the image (camera settings, |
| 23 |
etc.). |
| 24 |
|
| 25 |
The EXIF header uses the TIFF format, only it contains only tag values and |
| 26 |
no actual image. |
| 27 |
|
| 28 |
Note that the image format called EXIF is simply JFIF with an EXIF header, |
| 29 |
i.e. a subformat of JFIF. |
| 30 |
|
| 31 |
See the EXIF specs at http://exif.org (2001.09.01). |
| 32 |
|
| 33 |
The basic format of the EXIF header is a sequence of IFDs (directories). I |
| 34 |
believe the first IFD is always for the main image and the 2nd IFD is for a |
| 35 |
thumbnail image and is not present if there is no thumbnail image in the |
| 36 |
file. |
| 37 |
|
| 38 |
A directory is a sequence of tag/value pairs. |
| 39 |
|
| 40 |
Each IFD can contain SubIFD, as the value of an EXIF Offset or Interop |
| 41 |
Offset tag. |
| 42 |
|
| 43 |
*/ |
| 44 |
|
| 45 |
|
| 46 |
#include "pm_config.h" |
| 47 |
#include <stdio.h> |
| 48 |
#include <stdlib.h> |
| 49 |
#include <math.h> |
| 50 |
#include <string.h> |
| 51 |
#include <time.h> |
| 52 |
#include <errno.h> |
| 53 |
#include <limits.h> |
| 54 |
#include <ctype.h> |
| 55 |
#include <assert.h> |
| 56 |
|
| 57 |
#if MSVCRT |
| 58 |
#include <sys/utime.h> |
| 59 |
#else |
| 60 |
#include <utime.h> |
| 61 |
#include <sys/types.h> |
| 62 |
#include <unistd.h> |
| 63 |
#include <errno.h> |
| 64 |
#endif |
| 65 |
|
| 66 |
#include "pm_c_util.h" |
| 67 |
#include "pm.h" |
| 68 |
#include "mallocvar.h" |
| 69 |
#include "nstring.h" |
| 70 |
|
| 71 |
#include "exif.h" |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
enum Orientation { |
| 76 |
ORIENT_NORMAL, |
| 77 |
ORIENT_FLIP_HORIZ, /* left right reversed mirror */ |
| 78 |
ORIENT_ROTATE_180, /* upside down */ |
| 79 |
ORIENT_FLIP_VERT, /* upside down mirror */ |
| 80 |
ORIENT_TRANSPOSE, /* Flipped about top-left <--> bottom-right axis*/ |
| 81 |
ORIENT_ROTATE_90, /* rotate 90 cw to right it */ |
| 82 |
ORIENT_TRANSVERSE, /* flipped about top-right <--> bottom-left axis */ |
| 83 |
ORIENT_ROTATE_270 /* rotate 270 to right it */ |
| 84 |
}; |
| 85 |
|
| 86 |
typedef struct { |
| 87 |
unsigned short tag; |
| 88 |
const char * desc; |
| 89 |
} TagTableEntry; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
/* Describes format descriptor */ |
| 94 |
static int const bytesPerFormat[] = {0,1,1,2,4,8,1,1,2,4,8,4,8}; |
| 95 |
#define NUM_FORMATS 12 |
| 96 |
|
| 97 |
#define FMT_BYTE 1 |
| 98 |
#define FMT_STRING 2 |
| 99 |
#define FMT_USHORT 3 |
| 100 |
#define FMT_ULONG 4 |
| 101 |
#define FMT_URATIONAL 5 |
| 102 |
#define FMT_SBYTE 6 |
| 103 |
#define FMT_UNDEFINED 7 |
| 104 |
#define FMT_SSHORT 8 |
| 105 |
#define FMT_SLONG 9 |
| 106 |
#define FMT_SRATIONAL 10 |
| 107 |
#define FMT_SINGLE 11 |
| 108 |
#define FMT_DOUBLE 12 |
| 109 |
|
| 110 |
/* Describes tag values */ |
| 111 |
|
| 112 |
#define TAG_EXIF_OFFSET 0x8769 |
| 113 |
#define TAG_INTEROP_OFFSET 0xa005 |
| 114 |
|
| 115 |
#define TAG_MAKE 0x010F |
| 116 |
#define TAG_MODEL 0x0110 |
| 117 |
|
| 118 |
#define TAG_ORIENTATION 0x0112 |
| 119 |
|
| 120 |
#define TAG_XRESOLUTION 0x011A |
| 121 |
#define TAG_YRESOLUTION 0x011B |
| 122 |
|
| 123 |
#define TAG_EXPOSURETIME 0x829A |
| 124 |
#define TAG_FNUMBER 0x829D |
| 125 |
|
| 126 |
#define TAG_SHUTTERSPEED 0x9201 |
| 127 |
#define TAG_APERTURE 0x9202 |
| 128 |
#define TAG_MAXAPERTURE 0x9205 |
| 129 |
#define TAG_FOCALLENGTH 0x920A |
| 130 |
|
| 131 |
#define TAG_DATETIME_ORIGINAL 0x9003 |
| 132 |
#define TAG_USERCOMMENT 0x9286 |
| 133 |
|
| 134 |
#define TAG_SUBJECT_DISTANCE 0x9206 |
| 135 |
#define TAG_FLASH 0x9209 |
| 136 |
|
| 137 |
#define TAG_FOCALPLANEXRES 0xa20E |
| 138 |
#define TAG_FOCALPLANEUNITS 0xa210 |
| 139 |
#define TAG_EXIF_IMAGEWIDTH 0xA002 |
| 140 |
#define TAG_EXIF_IMAGELENGTH 0xA003 |
| 141 |
|
| 142 |
/* the following is added 05-jan-2001 vcs */ |
| 143 |
#define TAG_EXPOSURE_BIAS 0x9204 |
| 144 |
#define TAG_WHITEBALANCE 0x9208 |
| 145 |
#define TAG_METERING_MODE 0x9207 |
| 146 |
#define TAG_EXPOSURE_PROGRAM 0x8822 |
| 147 |
#define TAG_ISO_EQUIVALENT 0x8827 |
| 148 |
#define TAG_COMPRESSION_LEVEL 0x9102 |
| 149 |
|
| 150 |
#define TAG_THUMBNAIL_OFFSET 0x0201 |
| 151 |
#define TAG_THUMBNAIL_LENGTH 0x0202 |
| 152 |
|
| 153 |
static TagTableEntry const tagTable[] = { |
| 154 |
{ 0x100, "ImageWidth"}, |
| 155 |
{ 0x101, "ImageLength"}, |
| 156 |
{ 0x102, "BitsPerSample"}, |
| 157 |
{ 0x103, "Compression"}, |
| 158 |
{ 0x106, "PhotometricInterpretation"}, |
| 159 |
{ 0x10A, "FillOrder"}, |
| 160 |
{ 0x10D, "DocumentName"}, |
| 161 |
{ 0x10E, "ImageDescription"}, |
| 162 |
{ 0x10F, "Make"}, |
| 163 |
{ 0x110, "Model"}, |
| 164 |
{ 0x111, "StripOffsets"}, |
| 165 |
{ 0x112, "Orientation"}, |
| 166 |
{ 0x115, "SamplesPerPixel"}, |
| 167 |
{ 0x116, "RowsPerStrip"}, |
| 168 |
{ 0x117, "StripByteCounts"}, |
| 169 |
{ 0x11A, "XResolution"}, |
| 170 |
{ 0x11B, "YResolution"}, |
| 171 |
{ 0x11C, "PlanarConfiguration"}, |
| 172 |
{ 0x128, "ResolutionUnit"}, |
| 173 |
{ 0x12D, "TransferFunction"}, |
| 174 |
{ 0x131, "Software"}, |
| 175 |
{ 0x132, "DateTime"}, |
| 176 |
{ 0x13B, "Artist"}, |
| 177 |
{ 0x13E, "WhitePoint"}, |
| 178 |
{ 0x13F, "PrimaryChromaticities"}, |
| 179 |
{ 0x156, "TransferRange"}, |
| 180 |
{ 0x200, "JPEGProc"}, |
| 181 |
{ 0x201, "ThumbnailOffset"}, |
| 182 |
{ 0x202, "ThumbnailLength"}, |
| 183 |
{ 0x211, "YCbCrCoefficients"}, |
| 184 |
{ 0x212, "YCbCrSubSampling"}, |
| 185 |
{ 0x213, "YCbCrPositioning"}, |
| 186 |
{ 0x214, "ReferenceBlackWhite"}, |
| 187 |
{ 0x828D, "CFARepeatPatternDim"}, |
| 188 |
{ 0x828E, "CFAPattern"}, |
| 189 |
{ 0x828F, "BatteryLevel"}, |
| 190 |
{ 0x8298, "Copyright"}, |
| 191 |
{ 0x829A, "ExposureTime"}, |
| 192 |
{ 0x829D, "FNumber"}, |
| 193 |
{ 0x83BB, "IPTC/NAA"}, |
| 194 |
{ 0x8769, "ExifOffset"}, |
| 195 |
{ 0x8773, "InterColorProfile"}, |
| 196 |
{ 0x8822, "ExposureProgram"}, |
| 197 |
{ 0x8824, "SpectralSensitivity"}, |
| 198 |
{ 0x8825, "GPSInfo"}, |
| 199 |
{ 0x8827, "ISOSpeedRatings"}, |
| 200 |
{ 0x8828, "OECF"}, |
| 201 |
{ 0x9000, "ExifVersion"}, |
| 202 |
{ 0x9003, "DateTimeOriginal"}, |
| 203 |
{ 0x9004, "DateTimeDigitized"}, |
| 204 |
{ 0x9101, "ComponentsConfiguration"}, |
| 205 |
{ 0x9102, "CompressedBitsPerPixel"}, |
| 206 |
{ 0x9201, "ShutterSpeedValue"}, |
| 207 |
{ 0x9202, "ApertureValue"}, |
| 208 |
{ 0x9203, "BrightnessValue"}, |
| 209 |
{ 0x9204, "ExposureBiasValue"}, |
| 210 |
{ 0x9205, "MaxApertureValue"}, |
| 211 |
{ 0x9206, "SubjectDistance"}, |
| 212 |
{ 0x9207, "MeteringMode"}, |
| 213 |
{ 0x9208, "LightSource"}, |
| 214 |
{ 0x9209, "Flash"}, |
| 215 |
{ 0x920A, "FocalLength"}, |
| 216 |
{ 0x927C, "MakerNote"}, |
| 217 |
{ 0x9286, "UserComment"}, |
| 218 |
{ 0x9290, "SubSecTime"}, |
| 219 |
{ 0x9291, "SubSecTimeOriginal"}, |
| 220 |
{ 0x9292, "SubSecTimeDigitized"}, |
| 221 |
{ 0xA000, "FlashPixVersion"}, |
| 222 |
{ 0xA001, "ColorSpace"}, |
| 223 |
{ 0xA002, "ExifImageWidth"}, |
| 224 |
{ 0xA003, "ExifImageLength"}, |
| 225 |
{ 0xA005, "InteroperabilityOffset"}, |
| 226 |
{ 0xA20B, "FlashEnergy"}, /* 0x920B in TIFF/EP */ |
| 227 |
{ 0xA20C, "SpatialFrequencyResponse"}, /* 0x920C - - */ |
| 228 |
{ 0xA20E, "FocalPlaneXResolution"}, /* 0x920E - - */ |
| 229 |
{ 0xA20F, "FocalPlaneYResolution"}, /* 0x920F - - */ |
| 230 |
{ 0xA210, "FocalPlaneResolutionUnit"}, /* 0x9210 - - */ |
| 231 |
{ 0xA214, "SubjectLocation"}, /* 0x9214 - - */ |
| 232 |
{ 0xA215, "ExposureIndex"}, /* 0x9215 - - */ |
| 233 |
{ 0xA217, "SensingMethod"}, /* 0x9217 - - */ |
| 234 |
{ 0xA300, "FileSource"}, |
| 235 |
{ 0xA301, "SceneType"}, |
| 236 |
{ 0, NULL} |
| 237 |
} ; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
typedef enum { ORDER_NORMAL, ORDER_MOTOROLA } ByteOrder; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
static uint16_t |
| 246 |
get16u(const void * const data, |
| 247 |
ByteOrder const byteOrder) { |
| 248 |
/*-------------------------------------------------------------------------- |
| 249 |
Convert a 16 bit unsigned value from file's native byte order |
| 250 |
--------------------------------------------------------------------------*/ |
| 251 |
if (byteOrder == ORDER_MOTOROLA) { |
| 252 |
return (((const unsigned char *)data)[0] << 8) | |
| 253 |
((const unsigned char *)data)[1]; |
| 254 |
} else { |
| 255 |
return (((const unsigned char *)data)[1] << 8) | |
| 256 |
((const unsigned char *)data)[0]; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
static int32_t |
| 263 |
get32s(const void * const data, |
| 264 |
ByteOrder const byteOrder) { |
| 265 |
/*-------------------------------------------------------------------------- |
| 266 |
Convert a 32 bit signed value from file's native byte order |
| 267 |
--------------------------------------------------------------------------*/ |
| 268 |
if (byteOrder == ORDER_MOTOROLA) { |
| 269 |
return |
| 270 |
(((const char *)data)[0] << 24) | |
| 271 |
(((const unsigned char *)data)[1] << 16) | |
| 272 |
(((const unsigned char *)data)[2] << 8 ) | |
| 273 |
(((const unsigned char *)data)[3] << 0 ); |
| 274 |
} else { |
| 275 |
return |
| 276 |
(((const char *)data)[3] << 24) | |
| 277 |
(((const unsigned char *)data)[2] << 16) | |
| 278 |
(((const unsigned char *)data)[1] << 8 ) | |
| 279 |
(((const unsigned char *)data)[0] << 0 ); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
static uint32_t |
| 286 |
get32u(const void * const data, |
| 287 |
ByteOrder const byteOrder) { |
| 288 |
/*-------------------------------------------------------------------------- |
| 289 |
Convert a 32 bit unsigned value from file's native byte order |
| 290 |
--------------------------------------------------------------------------*/ |
| 291 |
return (uint32_t)get32s(data, byteOrder) & 0xffffffff; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
static const char * |
| 297 |
numberTraceValue(const void * const valueP, |
| 298 |
int const format, |
| 299 |
unsigned int const byteCt, |
| 300 |
ByteOrder const byteOrder) { |
| 301 |
/*-------------------------------------------------------------------------- |
| 302 |
Format for display a number represented in any of the numeric formats |
| 303 |
--------------------------------------------------------------------------*/ |
| 304 |
const char * retval; |
| 305 |
|
| 306 |
switch(format) { |
| 307 |
case FMT_SBYTE: |
| 308 |
case FMT_BYTE: |
| 309 |
pm_asprintf(&retval, "%02x", *(unsigned char *)valueP); |
| 310 |
break; |
| 311 |
case FMT_USHORT: |
| 312 |
pm_asprintf(&retval, "%d",get16u(valueP, byteOrder)); |
| 313 |
break; |
| 314 |
case FMT_ULONG: |
| 315 |
case FMT_SLONG: |
| 316 |
pm_asprintf(&retval, "%d",get32s(valueP, byteOrder)); |
| 317 |
break; |
| 318 |
case FMT_SSHORT: |
| 319 |
pm_asprintf(&retval, "%hd",(signed short)get16u(valueP, byteOrder)); |
| 320 |
break; |
| 321 |
case FMT_URATIONAL: |
| 322 |
case FMT_SRATIONAL: |
| 323 |
pm_asprintf(&retval, "%d/%d", |
| 324 |
get32s(valueP, byteOrder), |
| 325 |
get32s(4+(char *)valueP, |
| 326 |
byteOrder)); |
| 327 |
break; |
| 328 |
case FMT_SINGLE: |
| 329 |
pm_asprintf(&retval, "%f",(double)*(float *)valueP); |
| 330 |
break; |
| 331 |
case FMT_DOUBLE: |
| 332 |
pm_asprintf(&retval, "%f",*(double *)valueP); |
| 333 |
break; |
| 334 |
default: { |
| 335 |
char * hex; |
| 336 |
|
| 337 |
MALLOCARRAY(hex, byteCt*2 + 1); |
| 338 |
if (!hex) |
| 339 |
retval = pm_strsol; |
| 340 |
else { |
| 341 |
unsigned int i; |
| 342 |
for (i = 0; i < byteCt && i < 16; ++i) { |
| 343 |
sprintf(&hex[i*2], "%02x", |
| 344 |
((const unsigned char *)valueP)[i]); |
| 345 |
} |
| 346 |
pm_asprintf(&retval, "Unknown format %d: %s", format, hex); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
return retval; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
static double |
| 356 |
numericValue(const void * const valuePtr, |
| 357 |
int const format, |
| 358 |
ByteOrder const byteOrder) { |
| 359 |
/*-------------------------------------------------------------------------- |
| 360 |
Evaluate number, be it int, rational, or float from directory. |
| 361 |
--------------------------------------------------------------------------*/ |
| 362 |
double value; |
| 363 |
|
| 364 |
switch(format) { |
| 365 |
case FMT_SBYTE: |
| 366 |
value = *(signed char *)valuePtr; |
| 367 |
break; |
| 368 |
case FMT_BYTE: |
| 369 |
value = *(unsigned char *)valuePtr; |
| 370 |
break; |
| 371 |
case FMT_USHORT: |
| 372 |
value = get16u(valuePtr, byteOrder); |
| 373 |
break; |
| 374 |
case FMT_ULONG: |
| 375 |
value = get32u(valuePtr, byteOrder); |
| 376 |
break; |
| 377 |
case FMT_URATIONAL: |
| 378 |
case FMT_SRATIONAL: { |
| 379 |
int num, den; |
| 380 |
num = get32s(valuePtr, byteOrder); |
| 381 |
den = get32s(4+(char *)valuePtr, byteOrder); |
| 382 |
value = den == 0 ? 0 : (double)(num/den); |
| 383 |
} break; |
| 384 |
case FMT_SSHORT: |
| 385 |
value = (signed short)get16u(valuePtr, byteOrder); |
| 386 |
break; |
| 387 |
case FMT_SLONG: |
| 388 |
value = get32s(valuePtr, byteOrder); |
| 389 |
break; |
| 390 |
|
| 391 |
/* Not sure if this is correct (never seen float used in Exif format) */ |
| 392 |
case FMT_SINGLE: |
| 393 |
value = (double)*(float *)valuePtr; |
| 394 |
break; |
| 395 |
case FMT_DOUBLE: |
| 396 |
value = *(double *)valuePtr; |
| 397 |
break; |
| 398 |
} |
| 399 |
return value; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
static const char * |
| 405 |
stringTraceValue(const unsigned char * const value, |
| 406 |
unsigned int const valueSz) { |
| 407 |
|
| 408 |
const char * retval; |
| 409 |
char * buffer; |
| 410 |
|
| 411 |
MALLOCARRAY(buffer, valueSz + 1); |
| 412 |
if (!buffer) |
| 413 |
retval = pm_strsol; |
| 414 |
else { |
| 415 |
unsigned int i; |
| 416 |
bool noPrint; |
| 417 |
/* We're in a sequence of unprintable characters. We put one |
| 418 |
'?' in the value for the whole sequence. |
| 419 |
*/ |
| 420 |
unsigned int outCursor; |
| 421 |
|
| 422 |
outCursor = 0; /* initial value */ |
| 423 |
|
| 424 |
for (i = 0, noPrint = false; i < valueSz; ++i) { |
| 425 |
if (ISPRINT(value[i])) { |
| 426 |
buffer[outCursor++] = value[i]; |
| 427 |
noPrint = false; |
| 428 |
} else { |
| 429 |
if (!noPrint) { |
| 430 |
buffer[outCursor++] = '?'; |
| 431 |
noPrint = true; |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
buffer[outCursor++] = '\0'; |
| 436 |
|
| 437 |
retval = buffer; |
| 438 |
} |
| 439 |
return retval; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
static void |
| 445 |
traceTag(int const tag, |
| 446 |
int const format, |
| 447 |
const unsigned char * const value, |
| 448 |
unsigned int const valueSz, |
| 449 |
ByteOrder const byteOrder) { |
| 450 |
|
| 451 |
const char * tagNm; |
| 452 |
const char * tagValue; |
| 453 |
unsigned int i; |
| 454 |
|
| 455 |
for (i = 0, tagNm = NULL; tagTable[i].tag; ++i) { |
| 456 |
if (tagTable[i].tag == tag) |
| 457 |
tagNm = pm_strdup(tagTable[i].desc); |
| 458 |
} |
| 459 |
|
| 460 |
if (!tagNm) |
| 461 |
pm_asprintf(&tagNm, "Unknown Tag %04x", tag); |
| 462 |
|
| 463 |
/* Show tag value. */ |
| 464 |
switch (format) { |
| 465 |
|
| 466 |
case FMT_UNDEFINED: |
| 467 |
/* Undefined is typically an ascii string. */ |
| 468 |
case FMT_STRING: { |
| 469 |
tagValue = stringTraceValue(value, valueSz); |
| 470 |
} break; |
| 471 |
|
| 472 |
default: |
| 473 |
/* Handle arrays of numbers later (will there ever be?)*/ |
| 474 |
tagValue = numberTraceValue(value, format, valueSz, byteOrder); |
| 475 |
} |
| 476 |
pm_message("%s = \"%s\"", tagNm, tagValue); |
| 477 |
|
| 478 |
pm_strfree(tagValue); |
| 479 |
pm_strfree(tagNm); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
static void |
| 485 |
initializeIfd(exif_ifd * const ifdP) { |
| 486 |
|
| 487 |
ifdP->cameraMake = NULL; |
| 488 |
ifdP->cameraModel = NULL; |
| 489 |
ifdP->dateTime = NULL; |
| 490 |
ifdP->xResolutionP = NULL; |
| 491 |
ifdP->yResolutionP = NULL; |
| 492 |
ifdP->orientationP = NULL; |
| 493 |
ifdP->isColorP = NULL; |
| 494 |
ifdP->flashP = NULL; |
| 495 |
ifdP->focalLengthP = NULL; |
| 496 |
ifdP->exposureTimeP = NULL; |
| 497 |
ifdP->shutterSpeedP = NULL; |
| 498 |
ifdP->apertureFNumberP = NULL; |
| 499 |
ifdP->distanceP = NULL; |
| 500 |
ifdP->exposureBiasP = NULL; |
| 501 |
ifdP->whiteBalanceP = NULL; |
| 502 |
ifdP->meteringModeP = NULL; |
| 503 |
ifdP->exposureProgramP = NULL; |
| 504 |
ifdP->isoEquivalentP = NULL; |
| 505 |
ifdP->compressionLevelP = NULL; |
| 506 |
ifdP->comments = NULL; |
| 507 |
ifdP->thumbnailOffsetP = NULL; |
| 508 |
ifdP->thumbnailLengthP = NULL; |
| 509 |
ifdP->exifImageLengthP = NULL; |
| 510 |
ifdP->exifImageWidthP = NULL; |
| 511 |
ifdP->focalPlaneXResP = NULL; |
| 512 |
ifdP->focalPlaneUnitsP = NULL; |
| 513 |
ifdP->thumbnail = NULL; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
static void |
| 519 |
freeIfPresent(const void * const arg) { |
| 520 |
|
| 521 |
if (arg) |
| 522 |
free((void *)arg); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
static void |
| 528 |
strfreeIfPresent(const char * const arg) { |
| 529 |
|
| 530 |
if (arg) |
| 531 |
pm_strfree(arg); |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
static void |
| 537 |
terminateIfd(exif_ifd * const ifdP) { |
| 538 |
|
| 539 |
strfreeIfPresent(ifdP->cameraMake ); |
| 540 |
strfreeIfPresent(ifdP->cameraModel ); |
| 541 |
strfreeIfPresent(ifdP->dateTime ); |
| 542 |
strfreeIfPresent(ifdP->comments ); |
| 543 |
freeIfPresent(ifdP->xResolutionP ); |
| 544 |
freeIfPresent(ifdP->yResolutionP ); |
| 545 |
freeIfPresent(ifdP->orientationP ); |
| 546 |
freeIfPresent(ifdP->isColorP ); |
| 547 |
freeIfPresent(ifdP->flashP ); |
| 548 |
freeIfPresent(ifdP->focalLengthP ); |
| 549 |
freeIfPresent(ifdP->exposureTimeP ); |
| 550 |
freeIfPresent(ifdP->shutterSpeedP ); |
| 551 |
freeIfPresent(ifdP->apertureFNumberP ); |
| 552 |
freeIfPresent(ifdP->distanceP ); |
| 553 |
freeIfPresent(ifdP->exposureBiasP ); |
| 554 |
freeIfPresent(ifdP->whiteBalanceP ); |
| 555 |
freeIfPresent(ifdP->meteringModeP ); |
| 556 |
freeIfPresent(ifdP->exposureProgramP ); |
| 557 |
freeIfPresent(ifdP->isoEquivalentP ); |
| 558 |
freeIfPresent(ifdP->compressionLevelP ); |
| 559 |
freeIfPresent(ifdP->thumbnailOffsetP ); |
| 560 |
freeIfPresent(ifdP->thumbnailLengthP ); |
| 561 |
freeIfPresent(ifdP->exifImageLengthP ); |
| 562 |
freeIfPresent(ifdP->exifImageWidthP ); |
| 563 |
freeIfPresent(ifdP->focalPlaneXResP ); |
| 564 |
freeIfPresent(ifdP->focalPlaneUnitsP ); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
static const char * |
| 570 |
commentValue(const unsigned char * const valuePtr, |
| 571 |
unsigned int const valueSz) { |
| 572 |
|
| 573 |
/* Olympus has this padded with trailing spaces. We stop the copy |
| 574 |
where those start. |
| 575 |
*/ |
| 576 |
const char * const value = (const char *)valuePtr; |
| 577 |
|
| 578 |
const char * retval; |
| 579 |
char * buffer; /* malloc'ed */ |
| 580 |
unsigned int cursor; |
| 581 |
unsigned int end; |
| 582 |
|
| 583 |
for (end = valueSz; end > 0 && value[end] == ' '; --end); |
| 584 |
|
| 585 |
/* Skip "ASCII" if it is there */ |
| 586 |
if (end >= 5 && memeq(value, "ASCII", 5)) |
| 587 |
cursor = 5; |
| 588 |
else |
| 589 |
cursor = 0; |
| 590 |
|
| 591 |
/* Skip consecutive blanks and NULs */ |
| 592 |
|
| 593 |
for (; |
| 594 |
cursor < valueSz && |
| 595 |
(value[cursor] == '\0' || value[cursor] == ' '); |
| 596 |
++cursor); |
| 597 |
|
| 598 |
/* Copy the rest as the comment */ |
| 599 |
|
| 600 |
MALLOCARRAY(buffer, end - cursor + 1); |
| 601 |
if (!buffer) |
| 602 |
retval = pm_strsol; |
| 603 |
else { |
| 604 |
unsigned int outCursor; |
| 605 |
for (outCursor = 0; cursor < end; ++cursor) |
| 606 |
buffer[outCursor++] = value[cursor]; |
| 607 |
|
| 608 |
buffer[outCursor++] = '\0'; |
| 609 |
|
| 610 |
retval = buffer; |
| 611 |
} |
| 612 |
return retval; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
/* Forward declaration for recursion */ |
| 618 |
|
| 619 |
static void |
| 620 |
processIfd(const unsigned char * const exifData, |
| 621 |
unsigned int const exifLength, |
| 622 |
const unsigned char * const ifdData, |
| 623 |
ByteOrder const byteOrder, |
| 624 |
bool const wantTagTrace, |
| 625 |
exif_ifd * const ifdP, |
| 626 |
const unsigned char ** const nextIfdPP, |
| 627 |
const char ** const errorP); |
| 628 |
|
| 629 |
|
| 630 |
static void |
| 631 |
processDirEntry(const unsigned char * const dirEntry, |
| 632 |
const unsigned char * const exifData, |
| 633 |
unsigned int const exifLength, |
| 634 |
ByteOrder const byteOrder, |
| 635 |
bool const wantTagTrace, |
| 636 |
exif_ifd * const ifdP, |
| 637 |
const char ** const errorP) { |
| 638 |
|
| 639 |
int const tag = get16u(&dirEntry[0], byteOrder); |
| 640 |
int const format = get16u(&dirEntry[2], byteOrder); |
| 641 |
int const components = get32u(&dirEntry[4], byteOrder); |
| 642 |
|
| 643 |
const unsigned char * valuePtr; |
| 644 |
/* This actually can point to a variety of things; it must be cast to |
| 645 |
other types when used. But we use it as a byte-by-byte cursor, so |
| 646 |
we declare it as a pointer to a generic byte here. |
| 647 |
*/ |
| 648 |
unsigned int valueSz; |
| 649 |
|
| 650 |
*errorP = NULL; /* initial assumption */ |
| 651 |
|
| 652 |
if ((format-1) >= NUM_FORMATS) { |
| 653 |
/* (-1) catches illegal zero case as unsigned underflows |
| 654 |
to positive large. |
| 655 |
*/ |
| 656 |
pm_message("Illegal number format %d for tag %04x", format, tag); |
| 657 |
return; |
| 658 |
} |
| 659 |
|
| 660 |
valueSz = components * bytesPerFormat[format]; |
| 661 |
|
| 662 |
if (valueSz > 4) { |
| 663 |
unsigned const offsetVal = get32u(&dirEntry[8], byteOrder); |
| 664 |
/* If its bigger than 4 bytes, the dir entry contains an offset.*/ |
| 665 |
if (offsetVal + valueSz > exifLength) { |
| 666 |
/* Bogus pointer offset and / or bytecount value */ |
| 667 |
pm_message("Illegal pointer offset value in EXIF " |
| 668 |
"for tag %04x. " |
| 669 |
"Offset %d bytes %d ExifLen %d\n", |
| 670 |
tag, offsetVal, valueSz, exifLength); |
| 671 |
return; |
| 672 |
} |
| 673 |
valuePtr = &exifData[offsetVal]; |
| 674 |
} else { |
| 675 |
/* 4 bytes or less and value is in the dir entry itself */ |
| 676 |
valuePtr = &dirEntry[8]; |
| 677 |
} |
| 678 |
|
| 679 |
if (wantTagTrace) |
| 680 |
traceTag(tag, format, valuePtr, valueSz, byteOrder); |
| 681 |
/* TODO: Need to deal with nonterminated strings in tag value */ |
| 682 |
/* TODO: Deal with repeated tag */ |
| 683 |
/* Extract useful components of tag */ |
| 684 |
switch (tag) { |
| 685 |
case TAG_MAKE: |
| 686 |
ifdP->cameraMake = pm_strdup((const char*)valuePtr); |
| 687 |
break; |
| 688 |
|
| 689 |
case TAG_MODEL: |
| 690 |
ifdP->cameraModel = pm_strdup((const char*)valuePtr); |
| 691 |
break; |
| 692 |
|
| 693 |
case TAG_XRESOLUTION: |
| 694 |
MALLOCVAR_NOFAIL(ifdP->xResolutionP); |
| 695 |
*ifdP->xResolutionP = numericValue(valuePtr, format, byteOrder); |
| 696 |
break; |
| 697 |
|
| 698 |
case TAG_YRESOLUTION: |
| 699 |
MALLOCVAR_NOFAIL(ifdP->yResolutionP); |
| 700 |
*ifdP->yResolutionP = numericValue(valuePtr, format, byteOrder); |
| 701 |
break; |
| 702 |
|
| 703 |
case TAG_DATETIME_ORIGINAL: |
| 704 |
ifdP->dateTime = pm_strdup((const char*)valuePtr); |
| 705 |
break; |
| 706 |
|
| 707 |
case TAG_USERCOMMENT: |
| 708 |
ifdP->comments = commentValue(valuePtr, valueSz); |
| 709 |
break; |
| 710 |
|
| 711 |
case TAG_FNUMBER: |
| 712 |
/* Simplest way of expressing aperture, so I trust it the most. |
| 713 |
(replace any existing value, as it will be based on a less useful |
| 714 |
tag that came earlier in the IFD). |
| 715 |
*/ |
| 716 |
if (ifdP->apertureFNumberP) |
| 717 |
free(ifdP->apertureFNumberP); |
| 718 |
|
| 719 |
MALLOCVAR_NOFAIL(ifdP->apertureFNumberP); |
| 720 |
*ifdP->apertureFNumberP = |
| 721 |
(float)numericValue(valuePtr, format, byteOrder); |
| 722 |
break; |
| 723 |
|
| 724 |
case TAG_APERTURE: |
| 725 |
case TAG_MAXAPERTURE: |
| 726 |
/* If we already have aperture information, it probably came from an |
| 727 |
FNUMBER tag and is superior, so we leave it alone |
| 728 |
*/ |
| 729 |
if (!ifdP->apertureFNumberP) { |
| 730 |
MALLOCVAR_NOFAIL(ifdP->apertureFNumberP); |
| 731 |
*ifdP->apertureFNumberP = (float) |
| 732 |
exp(numericValue(valuePtr, format, byteOrder) |
| 733 |
* log(2) * 0.5); |
| 734 |
} |
| 735 |
break; |
| 736 |
|
| 737 |
case TAG_FOCALLENGTH: |
| 738 |
/* Nice digital cameras actually save the focal length |
| 739 |
as a function of how farthey are zoomed in. |
| 740 |
*/ |
| 741 |
|
| 742 |
MALLOCVAR_NOFAIL(ifdP->focalLengthP); |
| 743 |
*ifdP->focalLengthP = |
| 744 |
(float)numericValue(valuePtr, format, byteOrder); |
| 745 |
break; |
| 746 |
|
| 747 |
case TAG_SUBJECT_DISTANCE: |
| 748 |
/* Inidcates the distacne the autofocus camera is focused to. |
| 749 |
Tends to be less accurate as distance increases. |
| 750 |
*/ |
| 751 |
MALLOCVAR_NOFAIL(ifdP->distanceP); |
| 752 |
*ifdP->distanceP = |
| 753 |
(float)numericValue(valuePtr, format, byteOrder); |
| 754 |
break; |
| 755 |
|
| 756 |
case TAG_EXPOSURETIME: |
| 757 |
MALLOCVAR_NOFAIL(ifdP->exposureTimeP); |
| 758 |
*ifdP->exposureTimeP = |
| 759 |
(float)numericValue(valuePtr, format, byteOrder); |
| 760 |
break; |
| 761 |
|
| 762 |
case TAG_SHUTTERSPEED: |
| 763 |
MALLOCVAR_NOFAIL(ifdP->shutterSpeedP); |
| 764 |
*ifdP->shutterSpeedP = |
| 765 |
1 << (unsigned int)numericValue(valuePtr, format, byteOrder); |
| 766 |
break; |
| 767 |
|
| 768 |
case TAG_FLASH: { |
| 769 |
unsigned int const tagValue = |
| 770 |
(unsigned int)numericValue(valuePtr, format, byteOrder); |
| 771 |
|
| 772 |
MALLOCVAR_NOFAIL(ifdP->flashP); |
| 773 |
|
| 774 |
*ifdP->flashP = ((tagValue & 0x7) != 0); |
| 775 |
|
| 776 |
} break; |
| 777 |
|
| 778 |
case TAG_ORIENTATION: { |
| 779 |
unsigned int const tagValue = |
| 780 |
(unsigned int)numericValue(valuePtr, format, byteOrder); |
| 781 |
|
| 782 |
if (tagValue < 1 || tagValue > 8) |
| 783 |
pm_asprintf(errorP, "Unrecognized orientation value %d", |
| 784 |
tagValue); |
| 785 |
else { |
| 786 |
MALLOCVAR_NOFAIL(ifdP->orientationP); |
| 787 |
|
| 788 |
switch (tagValue) { |
| 789 |
case 1: *ifdP->orientationP = ORIENT_NORMAL; break; |
| 790 |
case 2: *ifdP->orientationP = ORIENT_FLIP_HORIZ; break; |
| 791 |
case 3: *ifdP->orientationP = ORIENT_ROTATE_180; break; |
| 792 |
case 4: *ifdP->orientationP = ORIENT_FLIP_VERT; break; |
| 793 |
case 5: *ifdP->orientationP = ORIENT_TRANSPOSE; break; |
| 794 |
case 6: *ifdP->orientationP = ORIENT_ROTATE_90; break; |
| 795 |
case 7: *ifdP->orientationP = ORIENT_TRANSVERSE; break; |
| 796 |
case 8: *ifdP->orientationP = ORIENT_ROTATE_270; break; |
| 797 |
default: |
| 798 |
assert(false); |
| 799 |
} |
| 800 |
} |
| 801 |
} break; |
| 802 |
|
| 803 |
case TAG_EXIF_IMAGELENGTH: |
| 804 |
MALLOCVAR_NOFAIL(ifdP->exifImageLengthP); |
| 805 |
*ifdP->exifImageLengthP = |
| 806 |
(unsigned int)numericValue(valuePtr, format, byteOrder); |
| 807 |
break; |
| 808 |
|
| 809 |
case TAG_EXIF_IMAGEWIDTH: |
| 810 |
MALLOCVAR_NOFAIL(ifdP->exifImageWidthP); |
| 811 |
*ifdP->exifImageWidthP = |
| 812 |
(unsigned int)numericValue(valuePtr, format, byteOrder); |
| 813 |
break; |
| 814 |
|
| 815 |
case TAG_FOCALPLANEXRES: |
| 816 |
MALLOCVAR_NOFAIL(ifdP->focalPlaneXResP); |
| 817 |
*ifdP->focalPlaneXResP = numericValue(valuePtr, format, byteOrder); |
| 818 |
break; |
| 819 |
|
| 820 |
case TAG_FOCALPLANEUNITS: { |
| 821 |
int const tagValue = (int)numericValue(valuePtr, format, byteOrder); |
| 822 |
|
| 823 |
if (tagValue < 1 || tagValue > 5) { |
| 824 |
pm_asprintf(errorP, "Unrecognized FOCALPLANEUNITS value %d. " |
| 825 |
"We know only 1, 2, 3, 4, and 5", |
| 826 |
tagValue); |
| 827 |
} else { |
| 828 |
MALLOCVAR_NOFAIL(ifdP->focalPlaneUnitsP); |
| 829 |
|
| 830 |
switch (tagValue) { |
| 831 |
case 1: *ifdP->focalPlaneUnitsP = 25.4; break; /* 1 inch */ |
| 832 |
case 2: *ifdP->focalPlaneUnitsP = 100.0; break; /* 1 meter */ |
| 833 |
case 3: *ifdP->focalPlaneUnitsP = 10.0; break; /* 1 centimeter*/ |
| 834 |
case 4: *ifdP->focalPlaneUnitsP = 1.0; break; /* 1 millimeter*/ |
| 835 |
case 5: *ifdP->focalPlaneUnitsP = .001; break; /* 1 micrometer*/ |
| 836 |
} |
| 837 |
/* According to the information I was using, 2 means meters. But |
| 838 |
looking at the Cannon powershot's files, inches is the only |
| 839 |
sensible value. |
| 840 |
*/ |
| 841 |
} |
| 842 |
} break; |
| 843 |
|
| 844 |
/* Remaining cases contributed by: Volker C. Schoech |
| 845 |
([email protected]) |
| 846 |
*/ |
| 847 |
|
| 848 |
case TAG_EXPOSURE_BIAS: |
| 849 |
MALLOCVAR_NOFAIL(ifdP->exposureBiasP); |
| 850 |
*ifdP->exposureBiasP = |
| 851 |
(float) numericValue(valuePtr, format, byteOrder); |
| 852 |
break; |
| 853 |
|
| 854 |
case TAG_WHITEBALANCE: |
| 855 |
MALLOCVAR_NOFAIL(ifdP->whiteBalanceP); |
| 856 |
*ifdP->whiteBalanceP = (int)numericValue(valuePtr, format, byteOrder); |
| 857 |
break; |
| 858 |
|
| 859 |
case TAG_METERING_MODE: |
| 860 |
MALLOCVAR_NOFAIL(ifdP->meteringModeP); |
| 861 |
*ifdP->meteringModeP = (int)numericValue(valuePtr, format, byteOrder); |
| 862 |
break; |
| 863 |
|
| 864 |
case TAG_EXPOSURE_PROGRAM: |
| 865 |
MALLOCVAR_NOFAIL(ifdP->exposureProgramP); |
| 866 |
*ifdP->exposureProgramP = |
| 867 |
(int)numericValue(valuePtr, format, byteOrder); |
| 868 |
break; |
| 869 |
|
| 870 |
case TAG_ISO_EQUIVALENT: { |
| 871 |
int const tagValue = (int)numericValue(valuePtr, format, byteOrder); |
| 872 |
|
| 873 |
MALLOCVAR_NOFAIL(ifdP->isoEquivalentP); |
| 874 |
if (tagValue < 50) |
| 875 |
*ifdP->isoEquivalentP = tagValue * 200; |
| 876 |
else |
| 877 |
*ifdP->isoEquivalentP = tagValue; |
| 878 |
} break; |
| 879 |
|
| 880 |
case TAG_COMPRESSION_LEVEL: |
| 881 |
MALLOCVAR_NOFAIL(ifdP->compressionLevelP); |
| 882 |
*ifdP->compressionLevelP = |
| 883 |
(int)numericValue(valuePtr, format, byteOrder); |
| 884 |
break; |
| 885 |
|
| 886 |
case TAG_THUMBNAIL_OFFSET: |
| 887 |
MALLOCVAR_NOFAIL(ifdP->thumbnailOffsetP); |
| 888 |
*ifdP->thumbnailOffsetP = (unsigned int) |
| 889 |
numericValue(valuePtr, format, byteOrder); |
| 890 |
break; |
| 891 |
|
| 892 |
case TAG_THUMBNAIL_LENGTH: |
| 893 |
MALLOCVAR_NOFAIL(ifdP->thumbnailLengthP); |
| 894 |
*ifdP->thumbnailLengthP = (unsigned int) |
| 895 |
numericValue(valuePtr, format, byteOrder); |
| 896 |
break; |
| 897 |
|
| 898 |
case TAG_EXIF_OFFSET: |
| 899 |
case TAG_INTEROP_OFFSET: { |
| 900 |
unsigned int const subIfdOffset = get32u(valuePtr, byteOrder); |
| 901 |
if (subIfdOffset + 4 > exifLength) |
| 902 |
pm_message("Invalid exif or interop offset " |
| 903 |
"directory link. Offset is %u, " |
| 904 |
"but Exif data is only %u bytes.", |
| 905 |
subIfdOffset, exifLength); |
| 906 |
else { |
| 907 |
/* Process the chain of IFDs starting at 'subIfdOffset'. |
| 908 |
Merge whatever tags are in them into *ifdP |
| 909 |
*/ |
| 910 |
const unsigned char * nextIfdP; |
| 911 |
|
| 912 |
for (nextIfdP = exifData + subIfdOffset; nextIfdP;) { |
| 913 |
const char * error; |
| 914 |
|
| 915 |
if (wantTagTrace) |
| 916 |
pm_message("Processing subIFD"); |
| 917 |
|
| 918 |
processIfd(exifData, exifLength, exifData + subIfdOffset, |
| 919 |
byteOrder, wantTagTrace, |
| 920 |
ifdP, &nextIfdP, &error); |
| 921 |
|
| 922 |
if (error) { |
| 923 |
pm_asprintf(errorP, "Failed to process " |
| 924 |
"ExifOffset/InteropOffset tag. %s", error); |
| 925 |
pm_strfree(error); |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
} break; |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
static void |
| 936 |
locateNextIfd(const unsigned char * const exifData, |
| 937 |
unsigned int const exifLength, |
| 938 |
ByteOrder const byteOrder, |
| 939 |
const unsigned char * const nextIfdLinkPtr, |
| 940 |
const unsigned char ** const nextIfdPP, |
| 941 |
const char ** const errorP) { |
| 942 |
|
| 943 |
if (nextIfdLinkPtr + 4 > exifData + exifLength) |
| 944 |
pm_asprintf(errorP, "EXIF header ends before next-IFD link"); |
| 945 |
else { |
| 946 |
if (nextIfdLinkPtr + 4 <= exifData + exifLength) { |
| 947 |
unsigned int const nextIfdLink = |
| 948 |
get32u(nextIfdLinkPtr, byteOrder); |
| 949 |
/* Offset in EXIF header of next IFD, or zero if none */ |
| 950 |
|
| 951 |
if (nextIfdLink) { |
| 952 |
if (nextIfdLink + 4 >= exifLength) { |
| 953 |
pm_asprintf(errorP, "Next IFD link is %u, " |
| 954 |
"but EXIF header is only %u bytes long", |
| 955 |
nextIfdLink, exifLength); |
| 956 |
} else |
| 957 |
*nextIfdPP = exifData + nextIfdLink; |
| 958 |
} else |
| 959 |
*nextIfdPP = NULL; |
| 960 |
} |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
static void |
| 967 |
processIfd(const unsigned char * const exifData, |
| 968 |
unsigned int const exifLength, |
| 969 |
const unsigned char * const ifdData, |
| 970 |
ByteOrder const byteOrder, |
| 971 |
bool const wantTagTrace, |
| 972 |
exif_ifd * const ifdP, |
| 973 |
const unsigned char ** const nextIfdPP, |
| 974 |
const char ** const errorP) { |
| 975 |
/*-------------------------------------------------------------------------- |
| 976 |
Process one EXIF IFD (Image File Directory). |
| 977 |
|
| 978 |
The text of the IFD is at 'ifdData'. |
| 979 |
|
| 980 |
The text of the EXIF header of which the IFD is part is the 'exifLength' |
| 981 |
bytes at 'exifData'. |
| 982 |
|
| 983 |
Return the contents of the directory (tags) as *ifdP. |
| 984 |
|
| 985 |
Return as *nextIfdPP a pointer into the EXIF header at 'exifData' |
| 986 |
to the next IFD in the chain, or NULL if this is the last IFD. |
| 987 |
--------------------------------------------------------------------------*/ |
| 988 |
unsigned int const numDirEntries = get16u(&ifdData[0], byteOrder); |
| 989 |
|
| 990 |
unsigned int de; |
| 991 |
|
| 992 |
*errorP = NULL; /* initial value */ |
| 993 |
|
| 994 |
#define DIR_ENTRY_ADDR(Start, Entry) (Start + 2 + 12*(Entry)) |
| 995 |
|
| 996 |
if (wantTagTrace) |
| 997 |
pm_message("Processing IFD at %p with %u entries", |
| 998 |
ifdData, numDirEntries); |
| 999 |
|
| 1000 |
for (de = 0; de < numDirEntries && !*errorP; ++de) { |
| 1001 |
const char * error; |
| 1002 |
processDirEntry(DIR_ENTRY_ADDR(ifdData, de), exifData, exifLength, |
| 1003 |
byteOrder, wantTagTrace, ifdP, |
| 1004 |
&error); |
| 1005 |
|
| 1006 |
if (error) { |
| 1007 |
pm_asprintf(errorP, "Failed to process tag %u. %s", |
| 1008 |
de, error); |
| 1009 |
pm_strfree(error); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
locateNextIfd(exifData, exifLength, byteOrder, |
| 1014 |
DIR_ENTRY_ADDR(ifdData, numDirEntries), |
| 1015 |
nextIfdPP, errorP); |
| 1016 |
|
| 1017 |
if (ifdP->thumbnailOffsetP && ifdP->thumbnailLengthP) { |
| 1018 |
if (*ifdP->thumbnailOffsetP + *ifdP->thumbnailLengthP <= exifLength) { |
| 1019 |
/* The thumbnail pointer appears to be valid. Store it. */ |
| 1020 |
ifdP->thumbnail = exifData + *ifdP->thumbnailOffsetP; |
| 1021 |
ifdP->thumbnailSize = *ifdP->thumbnailLengthP; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
if (wantTagTrace) |
| 1025 |
pm_message("Done processing IFD at %p", ifdData); |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
void |
| 1031 |
exif_parse(const unsigned char * const exifData, |
| 1032 |
unsigned int const length, |
| 1033 |
exif_ImageInfo * const imageInfoP, |
| 1034 |
bool const wantTagTrace, |
| 1035 |
const char ** const errorP) { |
| 1036 |
/*-------------------------------------------------------------------------- |
| 1037 |
Interpret an EXIF APP1 marker |
| 1038 |
|
| 1039 |
'exifData' is the actual Exif data; it does not include the |
| 1040 |
"Exif" identifier and length field that often prefix Exif data. |
| 1041 |
|
| 1042 |
'length' is the length of the Exif section. |
| 1043 |
--------------------------------------------------------------------------*/ |
| 1044 |
ByteOrder byteOrder; |
| 1045 |
unsigned int firstOffset; |
| 1046 |
|
| 1047 |
*errorP = NULL; /* initial assumption */ |
| 1048 |
|
| 1049 |
if (wantTagTrace) |
| 1050 |
pm_message("Exif header %u bytes long", length); |
| 1051 |
|
| 1052 |
if (memeq(exifData + 0, "II" , 2)) { |
| 1053 |
if (wantTagTrace) |
| 1054 |
pm_message("Exif header in Intel order"); |
| 1055 |
byteOrder = ORDER_NORMAL; |
| 1056 |
} else { |
| 1057 |
if (memeq(exifData + 0, "MM", 2)) { |
| 1058 |
if (wantTagTrace) |
| 1059 |
pm_message("Exif header in Motorola order"); |
| 1060 |
byteOrder = ORDER_MOTOROLA; |
| 1061 |
} else { |
| 1062 |
pm_asprintf(errorP, "Invalid alignment marker in Exif " |
| 1063 |
"data. First two bytes are '%c%c' (0x%02x%02x) " |
| 1064 |
"instead of 'II' or 'MM'.", |
| 1065 |
exifData[0], exifData[1], exifData[0], exifData[1]); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
if (!*errorP) { |
| 1069 |
unsigned short const start = get16u(exifData + 2, byteOrder); |
| 1070 |
/* Check the next value for correctness. */ |
| 1071 |
if (start != 0x002a) { |
| 1072 |
pm_asprintf(errorP, "Invalid Exif header start. " |
| 1073 |
"two bytes after the alignment marker " |
| 1074 |
"should be 0x002a, but is 0x%04x", |
| 1075 |
start); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
if (!*errorP) { |
| 1079 |
const char * error; |
| 1080 |
const unsigned char * nextIfdP; |
| 1081 |
|
| 1082 |
firstOffset = get32u(exifData + 4, byteOrder); |
| 1083 |
if (firstOffset < 8 || firstOffset > 16) { |
| 1084 |
/* I used to ensure this was set to 8 (website I used |
| 1085 |
indicated it's 8) but PENTAX Optio 230 has it set |
| 1086 |
differently, and uses it as offset. (Sept 11 2002) |
| 1087 |
*/ |
| 1088 |
pm_message("Suspicious offset of first IFD value in Exif header"); |
| 1089 |
} |
| 1090 |
|
| 1091 |
initializeIfd(&imageInfoP->mainImage); |
| 1092 |
initializeIfd(&imageInfoP->thumbnailImage); |
| 1093 |
|
| 1094 |
if (wantTagTrace) |
| 1095 |
pm_message("Processing main image IFD (IFD0)"); |
| 1096 |
|
| 1097 |
processIfd(exifData, length, exifData + firstOffset, byteOrder, |
| 1098 |
wantTagTrace, |
| 1099 |
&imageInfoP->mainImage, &nextIfdP, &error); |
| 1100 |
|
| 1101 |
if (error) { |
| 1102 |
pm_asprintf(errorP, "Failed to process main image IFD. %s", |
| 1103 |
error); |
| 1104 |
pm_strfree(error); |
| 1105 |
} |
| 1106 |
|
| 1107 |
if (nextIfdP) { |
| 1108 |
const char * error; |
| 1109 |
|
| 1110 |
if (wantTagTrace) |
| 1111 |
pm_message("Processing thumbnail IFD (IFD1)"); |
| 1112 |
|
| 1113 |
processIfd(exifData, length, nextIfdP, byteOrder, |
| 1114 |
wantTagTrace, |
| 1115 |
&imageInfoP->thumbnailImage, &nextIfdP, &error); |
| 1116 |
|
| 1117 |
if (error) { |
| 1118 |
pm_asprintf(errorP, |
| 1119 |
"Failed to process thumbnail image IFD. %s", |
| 1120 |
error); |
| 1121 |
pm_strfree(error); |
| 1122 |
} else { |
| 1123 |
if (nextIfdP) { |
| 1124 |
pm_message("Ignoring third IFD in EXIF header because " |
| 1125 |
"We understand only two -- one for the main " |
| 1126 |
"image and one for the thumbnail"); |
| 1127 |
} |
| 1128 |
} |
| 1129 |
} |
| 1130 |
if (!*errorP) { |
| 1131 |
/* Compute the CCD width, in millimeters. */ |
| 1132 |
if (imageInfoP->mainImage.focalPlaneXResP && |
| 1133 |
imageInfoP->mainImage.focalPlaneUnitsP && |
| 1134 |
imageInfoP->mainImage.exifImageWidthP && |
| 1135 |
imageInfoP->mainImage.exifImageLengthP) { |
| 1136 |
|
| 1137 |
unsigned int const maxDim = |
| 1138 |
MAX(*imageInfoP->mainImage.exifImageWidthP, |
| 1139 |
*imageInfoP->mainImage.exifImageLengthP); |
| 1140 |
|
| 1141 |
MALLOCVAR_NOFAIL(imageInfoP->ccdWidthP); |
| 1142 |
*imageInfoP->ccdWidthP = |
| 1143 |
(float)(maxDim * |
| 1144 |
*imageInfoP->mainImage.focalPlaneUnitsP / |
| 1145 |
*imageInfoP->mainImage.focalPlaneXResP); |
| 1146 |
} else |
| 1147 |
imageInfoP->ccdWidthP = NULL; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
static void |
| 1155 |
showIfd(const exif_ifd * const ifdP) { |
| 1156 |
|
| 1157 |
if (ifdP->cameraMake) |
| 1158 |
pm_message("Camera make : %s", ifdP->cameraMake); |
| 1159 |
|
| 1160 |
if (ifdP->cameraModel) |
| 1161 |
pm_message("Camera model : %s", ifdP->cameraModel); |
| 1162 |
|
| 1163 |
if (ifdP->dateTime) |
| 1164 |
pm_message("Date/Time : %s", ifdP->dateTime); |
| 1165 |
|
| 1166 |
if (ifdP->xResolutionP && ifdP->yResolutionP) |
| 1167 |
pm_message("Resolution : %f x %f", |
| 1168 |
*ifdP->xResolutionP, *ifdP->yResolutionP); |
| 1169 |
|
| 1170 |
if (ifdP->orientationP) { |
| 1171 |
/* Note that orientation is usually understood to be the orientation |
| 1172 |
of the camera, not of the image. The top, bottom, left, and right |
| 1173 |
sides of an image are defined in the JFIF format. |
| 1174 |
|
| 1175 |
But values such as "flip horizontal" make no sense for that. |
| 1176 |
*/ |
| 1177 |
|
| 1178 |
const char * orientDisp; |
| 1179 |
|
| 1180 |
switch (*ifdP->orientationP) { |
| 1181 |
case ORIENT_NORMAL: orientDisp = "Normal"; break; |
| 1182 |
case ORIENT_FLIP_HORIZ: orientDisp = "Flip horizontal"; break; |
| 1183 |
case ORIENT_ROTATE_180: orientDisp = "Rotate 180"; break; |
| 1184 |
case ORIENT_FLIP_VERT: orientDisp = "Flip vertical"; break; |
| 1185 |
case ORIENT_TRANSPOSE: orientDisp = "Transpose"; break; |
| 1186 |
case ORIENT_ROTATE_90: orientDisp = "Rotate 90"; break; |
| 1187 |
case ORIENT_TRANSVERSE: orientDisp = "Transverse"; break; |
| 1188 |
case ORIENT_ROTATE_270: orientDisp = "Rotate 270"; break; |
| 1189 |
} |
| 1190 |
|
| 1191 |
pm_message("Orientation : %s", orientDisp); |
| 1192 |
} |
| 1193 |
|
| 1194 |
if (ifdP->isColorP) |
| 1195 |
pm_message("Color/bw : %s", |
| 1196 |
*ifdP->isColorP ? "Color" : "Black and white"); |
| 1197 |
|
| 1198 |
if (ifdP->flashP) |
| 1199 |
pm_message("Flash used : %s", |
| 1200 |
*ifdP->flashP ? "Yes" :"No"); |
| 1201 |
|
| 1202 |
if (ifdP->exposureTimeP) { |
| 1203 |
const char * timeDisp; |
| 1204 |
const char * recipDisp; |
| 1205 |
|
| 1206 |
if (*ifdP->exposureTimeP < 0.010) { |
| 1207 |
pm_asprintf(&timeDisp, "%6.4f s", *ifdP->exposureTimeP); |
| 1208 |
} else { |
| 1209 |
pm_asprintf(&timeDisp, "%5.3f s", *ifdP->exposureTimeP); |
| 1210 |
} |
| 1211 |
/* We've seen the EXPOSURETIME tag be present but contain zero. |
| 1212 |
I don't know why. |
| 1213 |
*/ |
| 1214 |
if (*ifdP->exposureTimeP <= 0.5 && *ifdP->exposureTimeP > 0) { |
| 1215 |
pm_asprintf(&recipDisp, " (1/%d)", |
| 1216 |
(int)(0.5 + 1 / *ifdP->exposureTimeP)); |
| 1217 |
} else |
| 1218 |
recipDisp = pm_strdup(""); |
| 1219 |
|
| 1220 |
pm_message("Exposure time: %s %s", timeDisp, recipDisp); |
| 1221 |
|
| 1222 |
pm_strfree(recipDisp); |
| 1223 |
pm_strfree(timeDisp); |
| 1224 |
} |
| 1225 |
if (ifdP->shutterSpeedP) |
| 1226 |
pm_message("Shutter speed: 1/%u", *ifdP->shutterSpeedP); |
| 1227 |
|
| 1228 |
if (ifdP->apertureFNumberP) { |
| 1229 |
pm_message("Aperture : f/%3.1f", *ifdP->apertureFNumberP); |
| 1230 |
} |
| 1231 |
if (ifdP->distanceP) { |
| 1232 |
if (*ifdP->distanceP < 0) |
| 1233 |
pm_message("Focus dist. : Infinite"); |
| 1234 |
else |
| 1235 |
pm_message("Focus dist. :%5.2fm", *ifdP->distanceP); |
| 1236 |
} |
| 1237 |
|
| 1238 |
if (ifdP->isoEquivalentP) |
| 1239 |
pm_message("ISO equiv. : %2d", *ifdP->isoEquivalentP); |
| 1240 |
|
| 1241 |
if (ifdP->exposureBiasP) |
| 1242 |
pm_message("Exposure bias: %4.2f", *ifdP->exposureBiasP); |
| 1243 |
|
| 1244 |
if (ifdP->whiteBalanceP) { |
| 1245 |
const char * whiteBalanceDisp; |
| 1246 |
|
| 1247 |
switch(*ifdP->whiteBalanceP) { |
| 1248 |
case 1: whiteBalanceDisp = "sunny"; break; |
| 1249 |
case 2: whiteBalanceDisp = "fluorescent"; break; |
| 1250 |
case 3: whiteBalanceDisp = "incandescent"; break; |
| 1251 |
default: whiteBalanceDisp = "cloudy"; break; |
| 1252 |
} |
| 1253 |
pm_message("Whitebalance : %s", whiteBalanceDisp); |
| 1254 |
} |
| 1255 |
if (ifdP->meteringModeP) { |
| 1256 |
const char * meteringModeDisp; |
| 1257 |
|
| 1258 |
switch(*ifdP->meteringModeP) { |
| 1259 |
case 2: meteringModeDisp = "center weight"; break; |
| 1260 |
case 3: meteringModeDisp = "spot"; break; |
| 1261 |
case 5: meteringModeDisp = "matrix"; break; |
| 1262 |
default: meteringModeDisp = "?"; break; |
| 1263 |
} |
| 1264 |
pm_message("Metering Mode: %s", meteringModeDisp); |
| 1265 |
} |
| 1266 |
if (ifdP->exposureProgramP) { |
| 1267 |
const char * exposureDisp; |
| 1268 |
|
| 1269 |
switch(*ifdP->exposureProgramP) { |
| 1270 |
case 2: exposureDisp = "program (auto)"; break; |
| 1271 |
case 3: exposureDisp = "aperture priority (semi-auto)"; break; |
| 1272 |
case 4: exposureDisp = "shutter priority (semi-auto)"; break; |
| 1273 |
default: exposureDisp = "?"; break; |
| 1274 |
} |
| 1275 |
pm_message("Exposure : %s", exposureDisp); |
| 1276 |
} |
| 1277 |
if (ifdP->compressionLevelP) { |
| 1278 |
const char * jpegQualityDisp; |
| 1279 |
|
| 1280 |
switch(*ifdP->compressionLevelP) { |
| 1281 |
case 1: jpegQualityDisp = "basic"; break; |
| 1282 |
case 2: jpegQualityDisp = "normal"; break; |
| 1283 |
case 4: jpegQualityDisp = "fine"; break; |
| 1284 |
default: jpegQualityDisp = "?"; break; |
| 1285 |
} |
| 1286 |
pm_message("Jpeg Quality : %s", jpegQualityDisp); |
| 1287 |
} |
| 1288 |
|
| 1289 |
if (ifdP->comments) { |
| 1290 |
char * buffer; |
| 1291 |
|
| 1292 |
MALLOCARRAY(buffer, strlen(ifdP->comments) + 1); |
| 1293 |
|
| 1294 |
if (!buffer) |
| 1295 |
pm_message("Out of memory allocating a buffer for %u " |
| 1296 |
"characters of comments", |
| 1297 |
(unsigned)strlen(ifdP->comments)); |
| 1298 |
else { |
| 1299 |
unsigned int i; |
| 1300 |
unsigned int outCursor; |
| 1301 |
|
| 1302 |
strcpy(buffer, "Comment: "); /* Permanently in buffer */ |
| 1303 |
|
| 1304 |
outCursor = 10; /* initial value */ |
| 1305 |
|
| 1306 |
for (i = 0; ifdP->comments[i]; ++i) { |
| 1307 |
char const c = ifdP->comments[i]; |
| 1308 |
if (c == '\n') { |
| 1309 |
buffer[outCursor++] = '\0'; |
| 1310 |
pm_message("%s", buffer); |
| 1311 |
outCursor = 10; |
| 1312 |
} else |
| 1313 |
buffer[outCursor++] = c; |
| 1314 |
} |
| 1315 |
if (outCursor > 10) |
| 1316 |
pm_message("%s", buffer); |
| 1317 |
|
| 1318 |
free(buffer); |
| 1319 |
} |
| 1320 |
} |
| 1321 |
} |
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
void |
| 1326 |
exif_showImageInfo(const exif_ImageInfo * const imageInfoP) { |
| 1327 |
/*-------------------------------------------------------------------------- |
| 1328 |
Show the collected image info, displaying camera F-stop and shutter |
| 1329 |
speed in a consistent and legible fashion. |
| 1330 |
--------------------------------------------------------------------------*/ |
| 1331 |
showIfd(&imageInfoP->mainImage); |
| 1332 |
|
| 1333 |
if (imageInfoP->mainImage.focalLengthP) { |
| 1334 |
const char * mm35equiv; |
| 1335 |
|
| 1336 |
if (imageInfoP->ccdWidthP) { |
| 1337 |
pm_asprintf(&mm35equiv, " (35mm equivalent: %dmm)", |
| 1338 |
(int) (*imageInfoP->mainImage.focalLengthP / |
| 1339 |
*imageInfoP->ccdWidthP * 36 + 0.5)); |
| 1340 |
} else |
| 1341 |
mm35equiv = pm_strdup(""); |
| 1342 |
|
| 1343 |
pm_message("Focal length : %4.1fmm %s", |
| 1344 |
*imageInfoP->mainImage.focalLengthP, mm35equiv); |
| 1345 |
|
| 1346 |
pm_strfree(mm35equiv); |
| 1347 |
} |
| 1348 |
|
| 1349 |
if (imageInfoP->ccdWidthP) |
| 1350 |
pm_message("CCD width : %2.4fmm", *imageInfoP->ccdWidthP); |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
void |
| 1356 |
exif_terminateImageInfo(exif_ImageInfo * const imageInfoP) { |
| 1357 |
|
| 1358 |
terminateIfd(&imageInfoP->mainImage); |
| 1359 |
terminateIfd(&imageInfoP->thumbnailImage); |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
|