blob: a0056e066a7863060a595999ad3828b6859c47e3 [file] [log] [blame]
The Android Open Source Project34a25642009-03-03 19:30:03 -08001//--------------------------------------------------------------------------
2// Program to pull the information out of various types of EXIF digital
3// camera files and show it in a reasonably consistent way
4//
5// This module parses the very complicated exif structures.
6//
7// Matthias Wandel
8//--------------------------------------------------------------------------
9#include "jhead.h"
10
11#include <math.h>
12#include <ctype.h>
13#include <utils/Log.h>
14
15static unsigned char * DirWithThumbnailPtrs;
16static double FocalplaneXRes;
17static double FocalplaneUnits;
18static int ExifImageWidth;
19static int MotorolaOrder = 0;
20
21// for fixing the rotation.
22static void * OrientationPtr[2];
23static int OrientationNumFormat[2];
24int NumOrientations = 0;
25
26
27// Define the line below to turn on poor man's debugging output
28#undef SUPERDEBUG
29
30#ifdef SUPERDEBUG
31#define printf LOGE
32#endif
33
34//--------------------------------------------------------------------------
35// Table of Jpeg encoding process names
36static const TagTable_t ProcessTable[] = {
37 { M_SOF0, "Baseline", 0, 0},
38 { M_SOF1, "Extended sequential", 0, 0},
39 { M_SOF2, "Progressive", 0, 0},
40 { M_SOF3, "Lossless", 0, 0},
41 { M_SOF5, "Differential sequential", 0, 0},
42 { M_SOF6, "Differential progressive", 0, 0},
43 { M_SOF7, "Differential lossless", 0, 0},
44 { M_SOF9, "Extended sequential, arithmetic coding", 0, 0},
45 { M_SOF10, "Progressive, arithmetic coding", 0, 0},
46 { M_SOF11, "Lossless, arithmetic coding", 0, 0},
47 { M_SOF13, "Differential sequential, arithmetic coding", 0, 0},
48 { M_SOF14, "Differential progressive, arithmetic coding", 0, 0},
49 { M_SOF15, "Differential lossless, arithmetic coding", 0, 0},
50};
51
52#define PROCESS_TABLE_SIZE (sizeof(ProcessTable) / sizeof(TagTable_t))
53
54// 1 - "The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side."
55// 2 - "The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side."
56// 3 - "The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side."
57// 4 - "The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side."
58
59// 5 - "The 0th row is the visual left-hand side of of the image, and the 0th column is the visual top."
60// 6 - "The 0th row is the visual right-hand side of of the image, and the 0th column is the visual top."
61// 7 - "The 0th row is the visual right-hand side of of the image, and the 0th column is the visual bottom."
62// 8 - "The 0th row is the visual left-hand side of of the image, and the 0th column is the visual bottom."
63
64// Note: The descriptions here are the same as the name of the command line
65// option to pass to jpegtran to right the image
66
67static const char * OrientTab[9] = {
68 "Undefined",
69 "Normal", // 1
70 "flip horizontal", // left right reversed mirror
71 "rotate 180", // 3
72 "flip vertical", // upside down mirror
73 "transpose", // Flipped about top-left <--> bottom-right axis.
74 "rotate 90", // rotate 90 cw to right it.
75 "transverse", // flipped about top-right <--> bottom-left axis
76 "rotate 270", // rotate 270 to right it.
77};
78
79const int BytesPerFormat[] = {0,1,1,2,4,8,1,1,2,4,8,4,8};
80
81//--------------------------------------------------------------------------
82// Describes tag values
83
Chih-Chung Changd6a02c32009-03-24 20:34:28 -070084#define TAG_INTEROP_INDEX 0x0001
85#define TAG_INTEROP_VERSION 0x0002
86#define TAG_IMAGE_WIDTH 0x0100
87#define TAG_IMAGE_LENGTH 0x0101
88#define TAG_BITS_PER_SAMPLE 0x0102
89#define TAG_COMPRESSION 0x0103
90#define TAG_PHOTOMETRIC_INTERP 0x0106
91#define TAG_FILL_ORDER 0x010A
92#define TAG_DOCUMENT_NAME 0x010D
93#define TAG_IMAGE_DESCRIPTION 0x010E
94#define TAG_MAKE 0x010F
95#define TAG_MODEL 0x0110
96#define TAG_SRIP_OFFSET 0x0111
97#define TAG_ORIENTATION 0x0112
98#define TAG_SAMPLES_PER_PIXEL 0x0115
99#define TAG_ROWS_PER_STRIP 0x0116
100#define TAG_STRIP_BYTE_COUNTS 0x0117
101#define TAG_X_RESOLUTION 0x011A
102#define TAG_Y_RESOLUTION 0x011B
103#define TAG_PLANAR_CONFIGURATION 0x011C
104#define TAG_RESOLUTION_UNIT 0x0128
105#define TAG_TRANSFER_FUNCTION 0x012D
106#define TAG_SOFTWARE 0x0131
107#define TAG_DATETIME 0x0132
108#define TAG_ARTIST 0x013B
109#define TAG_WHITE_POINT 0x013E
110#define TAG_PRIMARY_CHROMATICITIES 0x013F
111#define TAG_TRANSFER_RANGE 0x0156
112#define TAG_JPEG_PROC 0x0200
113#define TAG_THUMBNAIL_OFFSET 0x0201
114#define TAG_THUMBNAIL_LENGTH 0x0202
115#define TAG_Y_CB_CR_COEFFICIENTS 0x0211
116#define TAG_Y_CB_CR_SUB_SAMPLING 0x0212
117#define TAG_Y_CB_CR_POSITIONING 0x0213
118#define TAG_REFERENCE_BLACK_WHITE 0x0214
119#define TAG_RELATED_IMAGE_WIDTH 0x1001
120#define TAG_RELATED_IMAGE_LENGTH 0x1002
121#define TAG_CFA_REPEAT_PATTERN_DIM 0x828D
122#define TAG_CFA_PATTERN1 0x828E
123#define TAG_BATTERY_LEVEL 0x828F
124#define TAG_COPYRIGHT 0x8298
125#define TAG_EXPOSURETIME 0x829A
126#define TAG_FNUMBER 0x829D
127#define TAG_IPTC_NAA 0x83BB
128#define TAG_EXIF_OFFSET 0x8769
129#define TAG_INTER_COLOR_PROFILE 0x8773
130#define TAG_EXPOSURE_PROGRAM 0x8822
131#define TAG_SPECTRAL_SENSITIVITY 0x8824
132#define TAG_GPSINFO 0x8825
133#define TAG_ISO_EQUIVALENT 0x8827
134#define TAG_OECF 0x8828
135#define TAG_EXIF_VERSION 0x9000
136#define TAG_DATETIME_ORIGINAL 0x9003
137#define TAG_DATETIME_DIGITIZED 0x9004
138#define TAG_COMPONENTS_CONFIG 0x9101
139#define TAG_CPRS_BITS_PER_PIXEL 0x9102
140#define TAG_SHUTTERSPEED 0x9201
141#define TAG_APERTURE 0x9202
142#define TAG_BRIGHTNESS_VALUE 0x9203
143#define TAG_EXPOSURE_BIAS 0x9204
144#define TAG_MAXAPERTURE 0x9205
145#define TAG_SUBJECT_DISTANCE 0x9206
146#define TAG_METERING_MODE 0x9207
147#define TAG_LIGHT_SOURCE 0x9208
148#define TAG_FLASH 0x9209
149#define TAG_FOCALLENGTH 0x920A
150#define TAG_MAKER_NOTE 0x927C
151#define TAG_USERCOMMENT 0x9286
152#define TAG_SUBSEC_TIME 0x9290
153#define TAG_SUBSEC_TIME_ORIG 0x9291
154#define TAG_SUBSEC_TIME_DIG 0x9292
155
156#define TAG_WINXP_TITLE 0x9c9b // Windows XP - not part of exif standard.
157#define TAG_WINXP_COMMENT 0x9c9c // Windows XP - not part of exif standard.
158#define TAG_WINXP_AUTHOR 0x9c9d // Windows XP - not part of exif standard.
159#define TAG_WINXP_KEYWORDS 0x9c9e // Windows XP - not part of exif standard.
160#define TAG_WINXP_SUBJECT 0x9c9f // Windows XP - not part of exif standard.
161
162#define TAG_FLASH_PIX_VERSION 0xA000
163#define TAG_COLOR_SPACE 0xA001
164#define TAG_EXIF_IMAGEWIDTH 0xA002
165#define TAG_EXIF_IMAGELENGTH 0xA003
166#define TAG_RELATED_AUDIO_FILE 0xA004
167#define TAG_INTEROP_OFFSET 0xA005
168#define TAG_FLASH_ENERGY 0xA20B
169#define TAG_SPATIAL_FREQ_RESP 0xA20C
170#define TAG_FOCAL_PLANE_XRES 0xA20E
171#define TAG_FOCAL_PLANE_YRES 0xA20F
172#define TAG_FOCAL_PLANE_UNITS 0xA210
173#define TAG_SUBJECT_LOCATION 0xA214
174#define TAG_EXPOSURE_INDEX 0xA215
175#define TAG_SENSING_METHOD 0xA217
176#define TAG_FILE_SOURCE 0xA300
177#define TAG_SCENE_TYPE 0xA301
178#define TAG_CFA_PATTERN 0xA302
179#define TAG_CUSTOM_RENDERED 0xA401
180#define TAG_EXPOSURE_MODE 0xA402
181#define TAG_WHITEBALANCE 0xA403
182#define TAG_DIGITALZOOMRATIO 0xA404
183#define TAG_FOCALLENGTH_35MM 0xA405
184#define TAG_SCENE_CAPTURE_TYPE 0xA406
185#define TAG_GAIN_CONTROL 0xA407
186#define TAG_CONTRAST 0xA408
187#define TAG_SATURATION 0xA409
188#define TAG_SHARPNESS 0xA40A
189#define TAG_DISTANCE_RANGE 0xA40C
The Android Open Source Project34a25642009-03-03 19:30:03 -0800190
191// TODO: replace the ", 0" values in this table with the correct format, e.g. ", FMT_USHORT"
192static const TagTable_t TagTable[] = {
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700193 { TAG_INTEROP_INDEX, "InteropIndex", 0, 0},
194 { TAG_INTEROP_VERSION, "InteropVersion", 0, 0},
195 { TAG_IMAGE_WIDTH, "ImageWidth", FMT_USHORT, 1},
196 { TAG_IMAGE_LENGTH, "ImageLength", FMT_USHORT, 1},
197 { TAG_BITS_PER_SAMPLE, "BitsPerSample", FMT_USHORT, 3},
198 { TAG_COMPRESSION, "Compression", FMT_USHORT, 1},
199 { TAG_PHOTOMETRIC_INTERP, "PhotometricInterpretation", FMT_USHORT, 1},
200 { TAG_FILL_ORDER, "FillOrder", 0, 0},
201 { TAG_DOCUMENT_NAME, "DocumentName", 0, 0},
202 { TAG_IMAGE_DESCRIPTION, "ImageDescription", 0, 0 },
203 { TAG_MAKE, "Make", FMT_STRING, -1},
204 { TAG_MODEL, "Model", FMT_STRING, -1},
205 { TAG_SRIP_OFFSET, "StripOffsets", FMT_USHORT, 1},
206 { TAG_ORIENTATION, "Orientation", FMT_USHORT, 1},
207 { TAG_SAMPLES_PER_PIXEL, "SamplesPerPixel", FMT_USHORT, 3},
208 { TAG_ROWS_PER_STRIP, "RowsPerStrip", FMT_USHORT, 1},
209 { TAG_STRIP_BYTE_COUNTS, "StripByteCounts", FMT_USHORT, 1},
210 { TAG_X_RESOLUTION, "XResolution", FMT_URATIONAL, 1},
211 { TAG_Y_RESOLUTION, "YResolution", FMT_URATIONAL, 1},
212 { TAG_PLANAR_CONFIGURATION, "PlanarConfiguration", FMT_USHORT, 1},
213 { TAG_RESOLUTION_UNIT, "ResolutionUnit", FMT_USHORT, 1},
214 { TAG_TRANSFER_FUNCTION, "TransferFunction", FMT_USHORT, 768},
215 { TAG_SOFTWARE, "Software", FMT_STRING, -1},
216 { TAG_DATETIME, "DateTime", FMT_STRING, 20},
217 { TAG_ARTIST, "Artist", FMT_STRING, -1},
218 { TAG_WHITE_POINT, "WhitePoint", FMT_SRATIONAL, 2},
219 { TAG_PRIMARY_CHROMATICITIES, "PrimaryChromaticities", FMT_SRATIONAL, 6},
220 { TAG_TRANSFER_RANGE, "TransferRange", 0, 0},
221 { TAG_JPEG_PROC, "JPEGProc", 0, 0},
222 { TAG_THUMBNAIL_OFFSET, "ThumbnailOffset", 0, 0},
223 { TAG_THUMBNAIL_LENGTH, "ThumbnailLength", 0, 0},
224 { TAG_Y_CB_CR_COEFFICIENTS, "YCbCrCoefficients", FMT_SRATIONAL, 3},
225 { TAG_Y_CB_CR_SUB_SAMPLING, "YCbCrSubSampling", FMT_USHORT, 2},
226 { TAG_Y_CB_CR_POSITIONING, "YCbCrPositioning", FMT_USHORT, 1},
227 { TAG_REFERENCE_BLACK_WHITE, "ReferenceBlackWhite", FMT_SRATIONAL, 6},
228 { TAG_RELATED_IMAGE_WIDTH, "RelatedImageWidth", 0, 0},
229 { TAG_RELATED_IMAGE_LENGTH, "RelatedImageLength", 0, 0},
230 { TAG_CFA_REPEAT_PATTERN_DIM, "CFARepeatPatternDim", 0, 0},
231 { TAG_CFA_PATTERN1, "CFAPattern", 0, 0},
232 { TAG_BATTERY_LEVEL, "BatteryLevel", 0, 0},
233 { TAG_COPYRIGHT, "Copyright", FMT_STRING, -1},
234 { TAG_EXPOSURETIME, "ExposureTime", FMT_USHORT, 1},
235 { TAG_FNUMBER, "FNumber", FMT_SRATIONAL, 1},
236 { TAG_IPTC_NAA, "IPTC/NAA", 0, 0},
237 { TAG_EXIF_OFFSET, "ExifOffset", 0, 0},
238 { TAG_INTER_COLOR_PROFILE, "InterColorProfile", 0, 0},
239 { TAG_EXPOSURE_PROGRAM, "ExposureProgram", FMT_SSHORT, 1},
240 { TAG_SPECTRAL_SENSITIVITY, "SpectralSensitivity", FMT_STRING, -1},
241 { TAG_GPSINFO, "GPS Dir offset", 0, 0},
242 { TAG_ISO_EQUIVALENT, "ISOSpeedRatings", FMT_SSHORT, -1},
243 { TAG_OECF, "OECF", 0, 0},
244 { TAG_EXIF_VERSION, "ExifVersion", FMT_BYTE, 4},
245 { TAG_DATETIME_ORIGINAL, "DateTimeOriginal", FMT_STRING, 20},
246 { TAG_DATETIME_DIGITIZED, "DateTimeDigitized", FMT_STRING, 20},
247 { TAG_COMPONENTS_CONFIG, "ComponentsConfiguration", FMT_BYTE, 4},
248 { TAG_CPRS_BITS_PER_PIXEL, "CompressedBitsPerPixel", FMT_SRATIONAL, 1},
249 { TAG_SHUTTERSPEED, "ShutterSpeedValue", FMT_SRATIONAL, 1},
250 { TAG_APERTURE, "ApertureValue", FMT_URATIONAL, 1},
251 { TAG_BRIGHTNESS_VALUE, "BrightnessValue", FMT_SRATIONAL, 1},
252 { TAG_EXPOSURE_BIAS, "ExposureBiasValue", FMT_SRATIONAL, 1},
253 { TAG_MAXAPERTURE, "MaxApertureValue", FMT_URATIONAL, 1},
254 { TAG_SUBJECT_DISTANCE, "SubjectDistance", FMT_URATIONAL, 1},
255 { TAG_METERING_MODE, "MeteringMode", FMT_USHORT, 1},
256 { TAG_LIGHT_SOURCE, "LightSource", FMT_USHORT, 1},
257 { TAG_FLASH, "Flash", FMT_USHORT, 1},
258 { TAG_FOCALLENGTH, "FocalLength", FMT_URATIONAL, 1},
259 { TAG_MAKER_NOTE, "MakerNote", FMT_STRING, -1},
260 { TAG_USERCOMMENT, "UserComment", FMT_STRING, -1},
261 { TAG_SUBSEC_TIME, "SubSecTime", FMT_STRING, -1},
262 { TAG_SUBSEC_TIME_ORIG, "SubSecTimeOriginal", FMT_STRING, -1},
263 { TAG_SUBSEC_TIME_DIG, "SubSecTimeDigitized", FMT_STRING, -1},
264 { TAG_WINXP_TITLE, "Windows-XP Title", 0, 0},
265 { TAG_WINXP_COMMENT, "Windows-XP comment", 0, 0},
266 { TAG_WINXP_AUTHOR, "Windows-XP author", 0, 0},
267 { TAG_WINXP_KEYWORDS, "Windows-XP keywords", 0, 0},
268 { TAG_WINXP_SUBJECT, "Windows-XP subject", 0, 0},
269 { TAG_FLASH_PIX_VERSION, "FlashPixVersion", FMT_BYTE, 4},
270 { TAG_COLOR_SPACE, "ColorSpace", FMT_USHORT, 1},
271 { TAG_EXIF_IMAGEWIDTH, "ExifImageWidth", 0, 0},
272 { TAG_EXIF_IMAGELENGTH, "ExifImageLength", 0, 0},
273 { TAG_RELATED_AUDIO_FILE, "RelatedAudioFile", 0, 0},
274 { TAG_INTEROP_OFFSET, "InteroperabilityOffset", 0, 0},
275 { TAG_FLASH_ENERGY, "FlashEnergy", FMT_URATIONAL, 1},
276 { TAG_SPATIAL_FREQ_RESP, "SpatialFrequencyResponse", FMT_STRING, -1},
277 { TAG_FOCAL_PLANE_XRES, "FocalPlaneXResolution", FMT_URATIONAL, 1},
278 { TAG_FOCAL_PLANE_YRES, "FocalPlaneYResolution", FMT_URATIONAL, 1},
279 { TAG_FOCAL_PLANE_UNITS, "FocalPlaneResolutionUnit", FMT_USHORT, 1},
280 { TAG_SUBJECT_LOCATION, "SubjectLocation", FMT_USHORT, 2},
281 { TAG_EXPOSURE_INDEX, "ExposureIndex", FMT_URATIONAL, 1},
282 { TAG_SENSING_METHOD, "SensingMethod", FMT_USHORT, 1},
283 { TAG_FILE_SOURCE, "FileSource", 0, 1},
284 { TAG_SCENE_TYPE, "SceneType", 0, 1},
285 { TAG_CFA_PATTERN, "CFA Pattern", 0, -1},
286 { TAG_CUSTOM_RENDERED, "CustomRendered", FMT_USHORT, 1},
287 { TAG_EXPOSURE_MODE, "ExposureMode", FMT_USHORT, 1},
288 { TAG_WHITEBALANCE, "WhiteBalance", FMT_USHORT, 1},
289 { TAG_DIGITALZOOMRATIO, "DigitalZoomRatio", FMT_URATIONAL, 1},
290 { TAG_FOCALLENGTH_35MM, "FocalLengthIn35mmFilm", FMT_USHORT, 1},
291 { TAG_SCENE_CAPTURE_TYPE, "SceneCaptureType", FMT_USHORT, 1},
292 { TAG_GAIN_CONTROL, "GainControl", FMT_URATIONAL, 1},
293 { TAG_CONTRAST, "Contrast", FMT_USHORT, 1},
294 { TAG_SATURATION, "Saturation", FMT_USHORT, 1},
295 { TAG_SHARPNESS, "Sharpness", FMT_USHORT, 1},
296 { TAG_DISTANCE_RANGE, "SubjectDistanceRange", FMT_USHORT, 1},
The Android Open Source Project34a25642009-03-03 19:30:03 -0800297} ;
298
299#define TAG_TABLE_SIZE (sizeof(TagTable) / sizeof(TagTable_t))
300
301int TagNameToValue(const char* tagName)
302{
303 unsigned int i;
304 for (i = 0; i < TAG_TABLE_SIZE; i++) {
305 if (strcmp(TagTable[i].Desc, tagName) == 0) {
306 printf("found tag %s val %d", TagTable[i].Desc, TagTable[i].Tag);
307 return TagTable[i].Tag;
308 }
309 }
310 printf("tag %s NOT FOUND", tagName);
311 return -1;
312}
313
314//--------------------------------------------------------------------------
315// Convert a 16 bit unsigned value to file's native byte order
316//--------------------------------------------------------------------------
317static void Put16u(void * Short, unsigned short PutValue)
318{
319 if (MotorolaOrder){
320 ((uchar *)Short)[0] = (uchar)(PutValue>>8);
321 ((uchar *)Short)[1] = (uchar)PutValue;
322 }else{
323 ((uchar *)Short)[0] = (uchar)PutValue;
324 ((uchar *)Short)[1] = (uchar)(PutValue>>8);
325 }
326}
327
328//--------------------------------------------------------------------------
329// Convert a 16 bit unsigned value from file's native byte order
330//--------------------------------------------------------------------------
331int Get16u(void * Short)
332{
333 if (MotorolaOrder){
334 return (((uchar *)Short)[0] << 8) | ((uchar *)Short)[1];
335 }else{
336 return (((uchar *)Short)[1] << 8) | ((uchar *)Short)[0];
337 }
338}
339
340//--------------------------------------------------------------------------
341// Convert a 32 bit signed value from file's native byte order
342//--------------------------------------------------------------------------
343int Get32s(void * Long)
344{
345 if (MotorolaOrder){
346 return ((( char *)Long)[0] << 24) | (((uchar *)Long)[1] << 16)
347 | (((uchar *)Long)[2] << 8 ) | (((uchar *)Long)[3] << 0 );
348 }else{
349 return ((( char *)Long)[3] << 24) | (((uchar *)Long)[2] << 16)
350 | (((uchar *)Long)[1] << 8 ) | (((uchar *)Long)[0] << 0 );
351 }
352}
353
354//--------------------------------------------------------------------------
355// Convert a 32 bit unsigned value to file's native byte order
356//--------------------------------------------------------------------------
357void Put32u(void * Value, unsigned PutValue)
358{
359 if (MotorolaOrder){
360 ((uchar *)Value)[0] = (uchar)(PutValue>>24);
361 ((uchar *)Value)[1] = (uchar)(PutValue>>16);
362 ((uchar *)Value)[2] = (uchar)(PutValue>>8);
363 ((uchar *)Value)[3] = (uchar)PutValue;
364 }else{
365 ((uchar *)Value)[0] = (uchar)PutValue;
366 ((uchar *)Value)[1] = (uchar)(PutValue>>8);
367 ((uchar *)Value)[2] = (uchar)(PutValue>>16);
368 ((uchar *)Value)[3] = (uchar)(PutValue>>24);
369 }
370}
371
372//--------------------------------------------------------------------------
373// Convert a 32 bit unsigned value from file's native byte order
374//--------------------------------------------------------------------------
375unsigned Get32u(void * Long)
376{
377 return (unsigned)Get32s(Long) & 0xffffffff;
378}
379
380//--------------------------------------------------------------------------
381// Display a number as one of its many formats
382//--------------------------------------------------------------------------
383void PrintFormatNumber(void * ValuePtr, int Format, int ByteCount)
384{
385 int s,n;
386
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700387 for(n=0;n<16;n++){
The Android Open Source Project34a25642009-03-03 19:30:03 -0800388 switch(Format){
389 case FMT_SBYTE:
390 case FMT_BYTE: printf("%02x",*(uchar *)ValuePtr); s=1; break;
391 case FMT_USHORT: printf("%d",Get16u(ValuePtr)); s=2; break;
392 case FMT_ULONG:
393 case FMT_SLONG: printf("%d",Get32s(ValuePtr)); s=4; break;
394 case FMT_SSHORT: printf("%hd",(signed short)Get16u(ValuePtr)); s=2; break;
395 case FMT_URATIONAL:
396 case FMT_SRATIONAL:
397 printf("%d/%d",Get32s(ValuePtr), Get32s(4+(char *)ValuePtr));
398 s = 8;
399 break;
400
401 case FMT_SINGLE: printf("%f",(double)*(float *)ValuePtr); s=8; break;
402 case FMT_DOUBLE: printf("%f",*(double *)ValuePtr); s=8; break;
403 default:
404 printf("Unknown format %d:", Format);
405 return;
406 }
407 ByteCount -= s;
408 if (ByteCount <= 0) break;
409 printf(", ");
410 ValuePtr = (void *)((char *)ValuePtr + s);
411
412 }
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700413 if (n >= 16) printf("...");
The Android Open Source Project34a25642009-03-03 19:30:03 -0800414}
415
416
417//--------------------------------------------------------------------------
418// Evaluate number, be it int, rational, or float from directory.
419//--------------------------------------------------------------------------
420double ConvertAnyFormat(void * ValuePtr, int Format)
421{
422 double Value;
423 Value = 0;
424
425 switch(Format){
426 case FMT_SBYTE: Value = *(signed char *)ValuePtr; break;
427 case FMT_BYTE: Value = *(uchar *)ValuePtr; break;
428
429 case FMT_USHORT: Value = Get16u(ValuePtr); break;
430 case FMT_ULONG: Value = Get32u(ValuePtr); break;
431
432 case FMT_URATIONAL:
433 case FMT_SRATIONAL:
434 {
435 int Num,Den;
436 Num = Get32s(ValuePtr);
437 Den = Get32s(4+(char *)ValuePtr);
438 if (Den == 0){
439 Value = 0;
440 }else{
441 Value = (double)Num/Den;
442 }
443 break;
444 }
445
446 case FMT_SSHORT: Value = (signed short)Get16u(ValuePtr); break;
447 case FMT_SLONG: Value = Get32s(ValuePtr); break;
448
449 // Not sure if this is correct (never seen float used in Exif format)
450 case FMT_SINGLE: Value = (double)*(float *)ValuePtr; break;
451 case FMT_DOUBLE: Value = *(double *)ValuePtr; break;
452
453 default:
454 ErrNonfatal("Illegal format code %d",Format,0);
455 }
456 return Value;
457}
458
459//--------------------------------------------------------------------------
460// Process one of the nested EXIF directories.
461//--------------------------------------------------------------------------
462static void ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase,
463 unsigned ExifLength, int NestingLevel)
464{
465 int de;
466 int a;
467 int NumDirEntries;
468 unsigned ThumbnailOffset = 0;
469 unsigned ThumbnailSize = 0;
470 char IndentString[25];
471
472 printf("ProcessExifDir");
473 if (NestingLevel > 4){
474 ErrNonfatal("Maximum directory nesting exceeded (corrupt exif header)", 0,0);
475 return;
476 }
477
478 memset(IndentString, ' ', 25);
479 IndentString[NestingLevel * 4] = '\0';
480
481
482 NumDirEntries = Get16u(DirStart);
483 #define DIR_ENTRY_ADDR(Start, Entry) (Start+2+12*(Entry))
484
485 {
486 unsigned char * DirEnd;
487 DirEnd = DIR_ENTRY_ADDR(DirStart, NumDirEntries);
488 if (DirEnd+4 > (OffsetBase+ExifLength)){
489 if (DirEnd+2 == OffsetBase+ExifLength || DirEnd == OffsetBase+ExifLength){
490 // Version 1.3 of jhead would truncate a bit too much.
491 // This also caught later on as well.
492 }else{
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700493 ErrNonfatal("Illegally sized exif subdirectory (%d entries)",NumDirEntries,0);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800494 return;
495 }
496 }
497 if (DumpExifMap){
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700498 printf("Map: %05d-%05d: Directory\n",(int)(DirStart-OffsetBase), (int)(DirEnd+4-OffsetBase));
The Android Open Source Project34a25642009-03-03 19:30:03 -0800499 }
500
501
502 }
503
504 if (ShowTags){
505 printf("(dir has %d entries)\n",NumDirEntries);
506 }
507
508 for (de=0;de<NumDirEntries;de++){
509 int Tag, Format, Components;
510 unsigned char * ValuePtr;
511 int ByteCount;
512 unsigned char * DirEntry;
513 DirEntry = DIR_ENTRY_ADDR(DirStart, de);
514
515 Tag = Get16u(DirEntry);
516 Format = Get16u(DirEntry+2);
517 Components = Get32u(DirEntry+4);
518
519 if ((Format-1) >= NUM_FORMATS) {
520 // (-1) catches illegal zero case as unsigned underflows to positive large.
521 ErrNonfatal("Illegal number format %d for tag %04x", Format, Tag);
522 continue;
523 }
524
525 if ((unsigned)Components > 0x10000){
526 ErrNonfatal("Illegal number of components %d for tag %04x", Components, Tag);
527 continue;
528 }
529
530 ByteCount = Components * BytesPerFormat[Format];
531
532 if (ByteCount > 4){
533 unsigned OffsetVal;
534 OffsetVal = Get32u(DirEntry+8);
535 // If its bigger than 4 bytes, the dir entry contains an offset.
536 if (OffsetVal+ByteCount > ExifLength){
537 // Bogus pointer offset and / or bytecount value
538 ErrNonfatal("Illegal value pointer for tag %04x", Tag,0);
539 continue;
540 }
541 ValuePtr = OffsetBase+OffsetVal;
542
543 if (OffsetVal > ImageInfo.LargestExifOffset){
544 ImageInfo.LargestExifOffset = OffsetVal;
545 }
546
547 if (DumpExifMap){
548 printf("Map: %05d-%05d: Data for tag %04x\n",OffsetVal, OffsetVal+ByteCount, Tag);
549 }
550 }else{
551 // 4 bytes or less and value is in the dir entry itself
552 ValuePtr = DirEntry+8;
553 }
554
555 if (Tag == TAG_MAKER_NOTE){
556 if (ShowTags){
557 printf("%s Maker note: ",IndentString);
558 }
559 ProcessMakerNote(ValuePtr, ByteCount, OffsetBase, ExifLength);
560 continue;
561 }
562
563 if (ShowTags){
564 // Show tag name
565 for (a=0;;a++){
566 if (a >= (int)TAG_TABLE_SIZE){
Nick Kralevich70059dd2010-05-14 11:48:59 -0700567 printf("%s", IndentString);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800568 printf(" Unknown Tag %04x Value = ", Tag);
569 break;
570 }
571 if (TagTable[a].Tag == Tag){
Nick Kralevich70059dd2010-05-14 11:48:59 -0700572 printf("%s", IndentString);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800573 printf(" %s = ",TagTable[a].Desc);
574 break;
575 }
576 }
577
578 // Show tag value.
579 switch(Format){
580 case FMT_BYTE:
581 if(ByteCount>1){
582 printf("%.*ls\n", ByteCount/2, (wchar_t *)ValuePtr);
583 }else{
584 PrintFormatNumber(ValuePtr, Format, ByteCount);
585 printf("\n");
586 }
587 break;
588
589 case FMT_UNDEFINED:
590 // Undefined is typically an ascii string.
591
592 case FMT_STRING:
593 // String arrays printed without function call (different from int arrays)
594 {
595 printf("\"%s\"", ValuePtr);
596// int NoPrint = 0;
597// printf("\"");
598// for (a=0;a<ByteCount;a++){
599// if (ValuePtr[a] >= 32){
600// putchar(ValuePtr[a]);
601// NoPrint = 0;
602// }else{
603// // Avoiding indicating too many unprintable characters of proprietary
604// // bits of binary information this program may not know how to parse.
605// if (!NoPrint && a != ByteCount-1){
606// putchar('?');
607// NoPrint = 1;
608// }
609// }
610// }
611// printf("\"\n");
612 }
613 break;
614
615 default:
616 // Handle arrays of numbers later (will there ever be?)
617 PrintFormatNumber(ValuePtr, Format, ByteCount);
618 printf("\n");
619 }
620 }
621
622 // Extract useful components of tag
623 switch(Tag){
624
625 case TAG_MAKE:
626 strncpy(ImageInfo.CameraMake, (char *)ValuePtr, ByteCount < 31 ? ByteCount : 31);
627 break;
628
629 case TAG_MODEL:
630 strncpy(ImageInfo.CameraModel, (char *)ValuePtr, ByteCount < 39 ? ByteCount : 39);
631 break;
632
633 case TAG_DATETIME_ORIGINAL:
634 // If we get a DATETIME_ORIGINAL, we use that one.
635 strncpy(ImageInfo.DateTime, (char *)ValuePtr, 19);
636 // Fallthru...
637
638 case TAG_DATETIME_DIGITIZED:
639 case TAG_DATETIME:
640 if (!isdigit(ImageInfo.DateTime[0])){
641 // If we don't already have a DATETIME_ORIGINAL, use whatever
642 // time fields we may have.
643 strncpy(ImageInfo.DateTime, (char *)ValuePtr, 19);
644 }
645
646 if (ImageInfo.numDateTimeTags >= MAX_DATE_COPIES){
647 ErrNonfatal("More than %d date fields! This is nuts", MAX_DATE_COPIES, 0);
648 break;
649 }
650 ImageInfo.DateTimeOffsets[ImageInfo.numDateTimeTags++] =
651 (char *)ValuePtr - (char *)OffsetBase;
652 break;
653
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700654 case TAG_WINXP_COMMENT:
655 if (ImageInfo.Comments[0]){ // We already have a jpeg comment.
656 // Already have a comment (probably windows comment), skip this one.
657 if (ShowTags) printf("Windows XP commend and other comment in header\n");
658 break; // Already have a windows comment, skip this one.
659 }
660
661 if (ByteCount > 1){
662 if (ByteCount > MAX_COMMENT_SIZE) ByteCount = MAX_COMMENT_SIZE;
663 memcpy(ImageInfo.Comments, ValuePtr, ByteCount);
664 ImageInfo.CommentWidchars = ByteCount/2;
665 }
666 break;
The Android Open Source Project34a25642009-03-03 19:30:03 -0800667
668 case TAG_USERCOMMENT:
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700669 if (ImageInfo.Comments[0]){ // We already have a jpeg comment.
670 // Already have a comment (probably windows comment), skip this one.
671 if (ShowTags) printf("Multiple comments in exif header\n");
672 break; // Already have a windows comment, skip this one.
673 }
674
675 // Comment is often padded with trailing spaces. Remove these first.
The Android Open Source Project34a25642009-03-03 19:30:03 -0800676 for (a=ByteCount;;){
677 a--;
678 if ((ValuePtr)[a] == ' '){
679 (ValuePtr)[a] = '\0';
680 }else{
681 break;
682 }
683 if (a == 0) break;
684 }
685
686 // Copy the comment
687 if (memcmp(ValuePtr, "ASCII",5) == 0){
688 for (a=5;a<10;a++){
689 int c;
690 c = (ValuePtr)[a];
691 if (c != '\0' && c != ' '){
692 strncpy(ImageInfo.Comments, (char *)ValuePtr+a, 199);
693 break;
694 }
695 }
The Android Open Source Project34a25642009-03-03 19:30:03 -0800696 }else{
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700697 strncpy(ImageInfo.Comments, (char *)ValuePtr, MAX_COMMENT_SIZE-1);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800698 }
699 break;
700
701 case TAG_FNUMBER:
702 // Simplest way of expressing aperture, so I trust it the most.
703 // (overwrite previously computd value if there is one)
704 ImageInfo.ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format);
705 break;
706
707 case TAG_APERTURE:
708 case TAG_MAXAPERTURE:
709 // More relevant info always comes earlier, so only use this field if we don't
710 // have appropriate aperture information yet.
711 if (ImageInfo.ApertureFNumber == 0){
712 ImageInfo.ApertureFNumber
713 = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5);
714 }
715 break;
716
717 case TAG_FOCALLENGTH:
718 // Nice digital cameras actually save the focal length as a function
719 // of how farthey are zoomed in.
Wu-cheng Li574d52d2010-01-31 22:34:33 +0800720 ImageInfo.FocalLength.num = Get32u(ValuePtr);
721 ImageInfo.FocalLength.denom = Get32u(4+(char *)ValuePtr);
The Android Open Source Project34a25642009-03-03 19:30:03 -0800722 break;
723
724 case TAG_SUBJECT_DISTANCE:
725 // Inidcates the distacne the autofocus camera is focused to.
726 // Tends to be less accurate as distance increases.
727 ImageInfo.Distance = (float)ConvertAnyFormat(ValuePtr, Format);
728 break;
729
730 case TAG_EXPOSURETIME:
731 // Simplest way of expressing exposure time, so I trust it most.
732 // (overwrite previously computd value if there is one)
733 ImageInfo.ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format);
734 break;
735
736 case TAG_SHUTTERSPEED:
737 // More complicated way of expressing exposure time, so only use
738 // this value if we don't already have it from somewhere else.
739 if (ImageInfo.ExposureTime == 0){
740 ImageInfo.ExposureTime
741 = (float)(1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2)));
742 }
743 break;
744
745
746 case TAG_FLASH:
747 ImageInfo.FlashUsed=(int)ConvertAnyFormat(ValuePtr, Format);
748 break;
749
750 case TAG_ORIENTATION:
751 if (NumOrientations >= 2){
752 // Can have another orientation tag for the thumbnail, but if there's
753 // a third one, things are stringae.
754 ErrNonfatal("More than two orientation tags!",0,0);
755 break;
756 }
757 OrientationPtr[NumOrientations] = ValuePtr;
758 OrientationNumFormat[NumOrientations] = Format;
759 if (NumOrientations == 0){
760 ImageInfo.Orientation = (int)ConvertAnyFormat(ValuePtr, Format);
761 }
762 if (ImageInfo.Orientation < 0 || ImageInfo.Orientation > 8){
763 ErrNonfatal("Undefined rotation value %d", ImageInfo.Orientation, 0);
764 ImageInfo.Orientation = 0;
765 }
766 NumOrientations += 1;
767 break;
768
769 case TAG_EXIF_IMAGELENGTH:
770 case TAG_EXIF_IMAGEWIDTH:
771 // Use largest of height and width to deal with images that have been
772 // rotated to portrait format.
773 a = (int)ConvertAnyFormat(ValuePtr, Format);
774 if (ExifImageWidth < a) ExifImageWidth = a;
775 break;
776
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700777 case TAG_FOCAL_PLANE_XRES:
The Android Open Source Project34a25642009-03-03 19:30:03 -0800778 FocalplaneXRes = ConvertAnyFormat(ValuePtr, Format);
779 break;
780
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700781 case TAG_FOCAL_PLANE_UNITS:
The Android Open Source Project34a25642009-03-03 19:30:03 -0800782 switch((int)ConvertAnyFormat(ValuePtr, Format)){
783 case 1: FocalplaneUnits = 25.4; break; // inch
784 case 2:
785 // According to the information I was using, 2 means meters.
786 // But looking at the Cannon powershot's files, inches is the only
787 // sensible value.
788 FocalplaneUnits = 25.4;
789 break;
790
791 case 3: FocalplaneUnits = 10; break; // centimeter
792 case 4: FocalplaneUnits = 1; break; // millimeter
793 case 5: FocalplaneUnits = .001; break; // micrometer
794 }
795 break;
796
797 case TAG_EXPOSURE_BIAS:
798 ImageInfo.ExposureBias = (float)ConvertAnyFormat(ValuePtr, Format);
799 break;
800
801 case TAG_WHITEBALANCE:
802 ImageInfo.Whitebalance = (int)ConvertAnyFormat(ValuePtr, Format);
803 break;
804
805 case TAG_LIGHT_SOURCE:
806 ImageInfo.LightSource = (int)ConvertAnyFormat(ValuePtr, Format);
807 break;
808
809 case TAG_METERING_MODE:
810 ImageInfo.MeteringMode = (int)ConvertAnyFormat(ValuePtr, Format);
811 break;
812
813 case TAG_EXPOSURE_PROGRAM:
814 ImageInfo.ExposureProgram = (int)ConvertAnyFormat(ValuePtr, Format);
815 break;
816
817 case TAG_EXPOSURE_INDEX:
818 if (ImageInfo.ISOequivalent == 0){
819 // Exposure index and ISO equivalent are often used interchangeably,
820 // so we will do the same in jhead.
821 // http://photography.about.com/library/glossary/bldef_ei.htm
822 ImageInfo.ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format);
823 }
824 break;
825
826 case TAG_EXPOSURE_MODE:
827 ImageInfo.ExposureMode = (int)ConvertAnyFormat(ValuePtr, Format);
828 break;
829
830 case TAG_ISO_EQUIVALENT:
831 ImageInfo.ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format);
832 if ( ImageInfo.ISOequivalent < 50 ){
833 // Fixes strange encoding on some older digicams.
834 ImageInfo.ISOequivalent *= 200;
835 }
836 break;
837
838 case TAG_DIGITALZOOMRATIO:
839 ImageInfo.DigitalZoomRatio = (float)ConvertAnyFormat(ValuePtr, Format);
840 break;
841
842 case TAG_THUMBNAIL_OFFSET:
843 ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format);
844 DirWithThumbnailPtrs = DirStart;
845 break;
846
847 case TAG_THUMBNAIL_LENGTH:
848 ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format);
849 ImageInfo.ThumbnailSizeOffset = ValuePtr-OffsetBase;
850 break;
851
852 case TAG_EXIF_OFFSET:
853 if (ShowTags) printf("%s Exif Dir:",IndentString);
854
855 case TAG_INTEROP_OFFSET:
856 if (Tag == TAG_INTEROP_OFFSET && ShowTags) printf("%s Interop Dir:",IndentString);
857 {
858 unsigned char * SubdirStart;
859 SubdirStart = OffsetBase + Get32u(ValuePtr);
860 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength){
861 ErrNonfatal("Illegal exif or interop ofset directory link",0,0);
862 }else{
863 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, NestingLevel+1);
864 }
865 continue;
866 }
867 break;
868
869 case TAG_GPSINFO:
870 if (ShowTags) printf("%s GPS info dir:",IndentString);
871 {
872 unsigned char * SubdirStart;
873 SubdirStart = OffsetBase + Get32u(ValuePtr);
874 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength){
875 ErrNonfatal("Illegal GPS directory link",0,0);
876 }else{
877 ProcessGpsInfo(SubdirStart, ByteCount, OffsetBase, ExifLength);
878 }
879 continue;
880 }
881 break;
882
883 case TAG_FOCALLENGTH_35MM:
884 // The focal length equivalent 35 mm is a 2.2 tag (defined as of April 2002)
885 // if its present, use it to compute equivalent focal length instead of
886 // computing it from sensor geometry and actual focal length.
887 ImageInfo.FocalLength35mmEquiv = (unsigned)ConvertAnyFormat(ValuePtr, Format);
888 break;
Chih-Chung Changd6a02c32009-03-24 20:34:28 -0700889
890 case TAG_DISTANCE_RANGE:
891 // Three possible standard values:
892 // 1 = macro, 2 = close, 3 = distant
893 ImageInfo.DistanceRange = (int)ConvertAnyFormat(ValuePtr, Format);
894 break;
The Android Open Source Project34a25642009-03-03 19:30:03 -0800895 }
896 }
897
898
899 {
900 // In addition to linking to subdirectories via exif tags,
901 // there's also a potential link to another directory at the end of each
902 // directory. this has got to be the result of a committee!
903 unsigned char * SubdirStart;
904 unsigned Offset;
905
906 if (DIR_ENTRY_ADDR(DirStart, NumDirEntries) + 4 <= OffsetBase+ExifLength){
907 printf("DirStart %d offset from dirstart %d", (int)DirStart, 2+12*NumDirEntries);
908 Offset = Get32u(DirStart+2+12*NumDirEntries);
909 if (Offset){
910 SubdirStart = OffsetBase + Offset;
911 if (SubdirStart > OffsetBase+ExifLength || SubdirStart < OffsetBase){
912 printf("SubdirStart %d OffsetBase %d ExifLength %d Offset %d",
913 (int)SubdirStart, (int)OffsetBase, ExifLength, Offset);
914 if (SubdirStart > OffsetBase && SubdirStart < OffsetBase+ExifLength+20){
915 // Jhead 1.3 or earlier would crop the whole directory!
916 // As Jhead produces this form of format incorrectness,
917 // I'll just let it pass silently
918 if (ShowTags) printf("Thumbnail removed with Jhead 1.3 or earlier\n");
919 }else{
920 ErrNonfatal("Illegal subdirectory link",0,0);
921 }
922 }else{
923 if (SubdirStart <= OffsetBase+ExifLength){
924 if (ShowTags) printf("%s Continued directory ",IndentString);
925 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, NestingLevel+1);
926 }
927 }
928 if (Offset > ImageInfo.LargestExifOffset){
929 ImageInfo.LargestExifOffset = Offset;
930 }
931 }
932 }else{
933 // The exif header ends before the last next directory pointer.
934 }
935 }
936
937 if (ThumbnailOffset){
938 ImageInfo.ThumbnailAtEnd = FALSE;
939
940 if (DumpExifMap){
941 printf("Map: %05d-%05d: Thumbnail\n",ThumbnailOffset, ThumbnailOffset+ThumbnailSize);
942 }
943
944 if (ThumbnailOffset <= ExifLength){
945 if (ThumbnailSize > ExifLength-ThumbnailOffset){
946 // If thumbnail extends past exif header, only save the part that
947 // actually exists. Canon's EOS viewer utility will do this - the
948 // thumbnail extracts ok with this hack.
949 ThumbnailSize = ExifLength-ThumbnailOffset;
950 if (ShowTags) printf("Thumbnail incorrectly placed in header\n");
951
952 }
953 // The thumbnail pointer appears to be valid. Store it.
954 ImageInfo.ThumbnailOffset = ThumbnailOffset;
955 ImageInfo.ThumbnailSize = ThumbnailSize;
956
957 if (ShowTags){
958 printf("Thumbnail size: %d bytes\n",ThumbnailSize);
959 }
960 }
961 }
962 printf("returning from ProcessExifDir");
963}
964
965
966//--------------------------------------------------------------------------
967// Process a EXIF marker
968// Describes all the drivel that most digital cameras include...
969//--------------------------------------------------------------------------
970void process_EXIF (unsigned char * ExifSection, unsigned int length)
971{
972 int FirstOffset;
973
974 FocalplaneXRes = 0;
975 FocalplaneUnits = 0;
976 ExifImageWidth = 0;
977 NumOrientations = 0;
978
979 if (ShowTags){
980 printf("Exif header %d bytes long\n",length);
981 }
982
983 { // Check the EXIF header component
984 static uchar ExifHeader[] = "Exif\0\0";
985 if (memcmp(ExifSection+2, ExifHeader,6)){
986 ErrNonfatal("Incorrect Exif header",0,0);
987 return;
988 }
989 }
990
991 if (memcmp(ExifSection+8,"II",2) == 0){
992 if (ShowTags) printf("Exif section in Intel order\n");
993 MotorolaOrder = 0;
994 }else{
995 if (memcmp(ExifSection+8,"MM",2) == 0){
996 if (ShowTags) printf("Exif section in Motorola order\n");
997 MotorolaOrder = 1;
998 }else{
999 ErrNonfatal("Invalid Exif alignment marker.",0,0);
1000 return;
1001 }
1002 }
1003
1004 // Check the next value for correctness.
1005 if (Get16u(ExifSection+10) != 0x2a){
1006 ErrNonfatal("Invalid Exif start (1)",0,0);
1007 return;
1008 }
1009
1010 FirstOffset = Get32u(ExifSection+12);
1011 if (FirstOffset < 8 || FirstOffset > 16){
1012 // Usually set to 8, but other values valid too.
1013 ErrNonfatal("Suspicious offset of first IFD value",0,0);
1014 return;
1015 }
1016
1017 DirWithThumbnailPtrs = NULL;
1018
1019
1020 // First directory starts 16 bytes in. All offset are relative to 8 bytes in.
1021 ProcessExifDir(ExifSection+8+FirstOffset, ExifSection+8, length-8, 0);
1022
1023 ImageInfo.ThumbnailAtEnd = ImageInfo.ThumbnailOffset >= ImageInfo.LargestExifOffset ? TRUE : FALSE;
1024#ifdef SUPERDEBUG
1025 printf("Thumbnail %s end", (ImageInfo.ThumbnailAtEnd ? "at" : "NOT at"));
1026#endif
1027 if (DumpExifMap){
1028 unsigned a,b;
1029 printf("Map: %05d- End of exif\n",length-8);
1030// for (a=0;a<length-8;a+= 10){
1031// printf("Map: %05d ",a);
1032// for (b=0;b<10;b++) printf(" %02x",*(ExifSection+8+a+b));
1033// printf("\n");
1034// }
1035 for (a = 0; a < length - 8; ++a) {
1036 unsigned char c = *(ExifSection+8+a);
1037 unsigned pc = isprint(c) ? c : ' ';
1038 printf("Map: %4d %02x %c", a, c, pc);
1039 }
1040 }
1041
1042
1043 // Compute the CCD width, in millimeters.
1044 if (FocalplaneXRes != 0){
1045 // Note: With some cameras, its not possible to compute this correctly because
1046 // they don't adjust the indicated focal plane resolution units when using less
1047 // than maximum resolution, so the CCDWidth value comes out too small. Nothing
1048 // that Jhad can do about it - its a camera problem.
1049 ImageInfo.CCDWidth = (float)(ExifImageWidth * FocalplaneUnits / FocalplaneXRes);
1050
Wu-cheng Li574d52d2010-01-31 22:34:33 +08001051 if (ImageInfo.FocalLength.num != 0 && ImageInfo.FocalLength.denom != 0
1052 && ImageInfo.FocalLength35mmEquiv == 0){
The Android Open Source Project34a25642009-03-03 19:30:03 -08001053 // Compute 35 mm equivalent focal length based on sensor geometry if we haven't
1054 // already got it explicitly from a tag.
Wu-cheng Li574d52d2010-01-31 22:34:33 +08001055 ImageInfo.FocalLength35mmEquiv = (int)(
1056 (double)ImageInfo.FocalLength.num / ImageInfo.FocalLength.denom
1057 / ImageInfo.CCDWidth * 36 + 0.5);
The Android Open Source Project34a25642009-03-03 19:30:03 -08001058 }
1059 }
1060}
1061
1062static const TagTable_t* TagToTagTableEntry(unsigned short tag)
1063{
1064 unsigned int i;
1065 for (i = 0; i < TAG_TABLE_SIZE; i++) {
1066 if (TagTable[i].Tag == tag) {
1067 printf("found tag %d", tag);
1068 int format = TagTable[i].Format;
1069 if (format == 0) {
1070 printf("tag %s format not defined ***** YOU MUST ADD THE FORMAT TO THE TagTable in exif.c!!!!", TagTable[i].Desc);
1071 return NULL;
1072 }
1073 return &TagTable[i];
1074 }
1075 }
1076 printf("tag %d NOT FOUND", tag);
1077 return NULL;
1078}
1079
1080static void writeExifTagAndData(int tag,
1081 int format,
1082 long components,
1083 long value,
1084 int valueInString,
1085 char* Buffer,
1086 int* DirIndex,
1087 int* DataWriteIndex) {
1088 Put16u(Buffer+ (*DirIndex), tag); // Tag
1089 Put16u(Buffer+(*DirIndex) + 2, format); // Format
1090 if (format == FMT_STRING && components == -1) {
1091 components = strlen((char*)value) + 1; // account for null terminator
1092 if (components & 1) ++components; // no odd lengths
1093 }
Tyler Luubd900942011-10-12 17:46:32 -05001094 if (format == FMT_UNDEFINED && components == -1) {
1095 // check if this UNDEFINED format is actually ASCII (as it usually is)
1096 // if so, we can calculate the size
1097 if(memcmp((char*)value, ExifAsciiPrefix, sizeof(ExifAsciiPrefix)) == 0) {
1098 components = sizeof(ExifAsciiPrefix) +
1099 strlen((char*)value + sizeof(ExifAsciiPrefix)) + 1;
1100 if (components & 1) ++components; // no odd lengths
1101 }
1102 }
The Android Open Source Project34a25642009-03-03 19:30:03 -08001103 Put32u(Buffer+(*DirIndex) + 4, components); // Components
1104 printf("# components: %ld", components);
1105 if (format == FMT_STRING) {
1106 // short strings can fit right in the long, otherwise have to
1107 // go in the data area
1108 if (components <= 4) {
1109 strcpy(Buffer+(*DirIndex) + 8, (char*)value);
1110 } else {
1111 Put32u(Buffer+(*DirIndex) + 8, (*DataWriteIndex)-8); // Pointer
1112 printf("copying value %s to %d", (char*)value, (*DataWriteIndex));
1113 strncpy(Buffer+(*DataWriteIndex), (char*)value, components);
1114 (*DataWriteIndex) += components;
1115 }
Tyler Luubd900942011-10-12 17:46:32 -05001116 } else if ((format == FMT_UNDEFINED) &&
1117 (memcmp((char*)value, ExifAsciiPrefix, sizeof(ExifAsciiPrefix)) == 0)) {
1118 // short strings can fit right in the long, otherwise have to
1119 // go in the data area
1120 if (components <= 4) {
1121 memcpy(Buffer+(*DirIndex) + 8, (char*)value, components);
1122 } else {
1123 Put32u(Buffer+(*DirIndex) + 8, (*DataWriteIndex)-8); // Pointer
1124 printf("copying %s to %d", (char*)value + sizeof(ExifAsciiPrefix), (*DataWriteIndex));
1125 memcpy(Buffer+(*DataWriteIndex), (char*)value, components);
1126 (*DataWriteIndex) += components;
1127 }
The Android Open Source Project34a25642009-03-03 19:30:03 -08001128 } else if (!valueInString) {
1129 Put32u(Buffer+(*DirIndex) + 8, value); // Value
1130 } else {
1131 Put32u(Buffer+(*DirIndex) + 8, (*DataWriteIndex)-8); // Pointer
1132 char* curElement = strtok((char*)value, ",");
1133 int i;
1134 for (i = 0; i < components && curElement != NULL; i++) {
1135#ifdef SUPERDEBUG
1136 printf("processing component %s format %s", curElement, formatStr(format));
1137#endif
1138 // elements are separated by commas
1139 if (format == FMT_URATIONAL) {
1140 char* separator = strchr(curElement, '/');
1141 if (separator) {
1142 unsigned int numerator = atoi(curElement);
1143 unsigned int denominator = atoi(separator + 1);
1144 Put32u(Buffer+(*DataWriteIndex), numerator);
1145 Put32u(Buffer+(*DataWriteIndex) + 4, denominator);
1146 (*DataWriteIndex) += 8;
1147 }
1148 } else if (format == FMT_SRATIONAL) {
1149 char* separator = strchr(curElement, '/');
1150 if (separator) {
1151 int numerator = atoi(curElement);
1152 int denominator = atoi(separator + 1);
1153 Put32u(Buffer+(*DataWriteIndex), numerator);
1154 Put32u(Buffer+(*DataWriteIndex) + 4, denominator);
1155 (*DataWriteIndex) += 8;
1156 }
1157 } else {
1158 // TODO: doesn't handle multiple components yet -- if more than one, have to put in data write area.
1159 value = atoi(curElement);
1160 Put32u(Buffer+(*DirIndex) + 8, value); // Value
1161 }
1162 curElement = strtok(NULL, ",");
1163 }
1164 }
1165 (*DirIndex) += 12;
1166}
1167
1168#ifdef SUPERDEBUG
1169char* formatStr(int format) {
1170 switch (format) {
1171 case FMT_BYTE: return "FMT_BYTE"; break;
1172 case FMT_STRING: return "FMT_STRING"; break;
1173 case FMT_USHORT: return "FMT_USHORT"; break;
1174 case FMT_ULONG: return "FMT_ULONG"; break;
1175 case FMT_URATIONAL: return "FMT_URATIONAL"; break;
1176 case FMT_SBYTE: return "FMT_SBYTE"; break;
1177 case FMT_UNDEFINED: return "FMT_UNDEFINED"; break;
1178 case FMT_SSHORT: return "FMT_SSHORT"; break;
1179 case FMT_SLONG: return "FMT_SLONG"; break;
1180 case FMT_SRATIONAL: return "FMT_SRATIONAL"; break;
1181 case FMT_SINGLE: return "FMT_SINGLE"; break;
1182 case FMT_DOUBLE: return "FMT_SINGLE"; break;
1183 default: return "UNKNOWN";
1184 }
1185}
1186#endif
1187
1188//--------------------------------------------------------------------------
1189// Create minimal exif header - just date and thumbnail pointers,
1190// so that date and thumbnail may be filled later.
1191//--------------------------------------------------------------------------
Takahiro Okada2645b482011-01-27 20:06:00 +09001192static void create_EXIF_internal(ExifElement_t* elements, int exifTagCount, int gpsTagCount, char* Buffer)
The Android Open Source Project34a25642009-03-03 19:30:03 -08001193{
The Android Open Source Project34a25642009-03-03 19:30:03 -08001194 unsigned short NumEntries;
1195 int DataWriteIndex;
1196 int DirIndex;
Tyler Luu0aba81d2011-09-27 15:47:22 -05001197 int DirExifLink = 0;
The Android Open Source Project34a25642009-03-03 19:30:03 -08001198
1199#ifdef SUPERDEBUG
1200 LOGE("create_EXIF %d exif elements, %d gps elements", exifTagCount, gpsTagCount);
1201#endif
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001202
The Android Open Source Project34a25642009-03-03 19:30:03 -08001203 MotorolaOrder = 0;
1204
1205 memcpy(Buffer+2, "Exif\0\0II",8);
1206 Put16u(Buffer+10, 0x2a);
1207
1208 DataWriteIndex = 16;
1209 Put32u(Buffer+12, DataWriteIndex-8); // first IFD offset. Means start 16 bytes in.
1210
1211 {
1212 DirIndex = DataWriteIndex;
1213 NumEntries = 2 + exifTagCount; // the two extra are the datetime and the thumbnail
1214 if (gpsTagCount) {
1215 ++NumEntries; // allow for the GPS info tag
1216 }
1217 DataWriteIndex += 2 + NumEntries*12 + 4;
1218
1219 Put16u(Buffer+DirIndex, NumEntries); // Number of entries
1220 DirIndex += 2;
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001221
1222 // Entries go here...
The Android Open Source Project34a25642009-03-03 19:30:03 -08001223 {
1224 // Date/time entry
1225 char* dateTime = NULL;
1226 char dateBuf[20];
1227 if (ImageInfo.numDateTimeTags){
1228 // If we had a pre-existing exif header, use time from that.
1229 dateTime = ImageInfo.DateTime;
1230 } else {
1231 // Oterwise, use the file's timestamp.
1232 FileTimeAsString(dateBuf);
1233 dateTime = dateBuf;
1234 }
1235 writeExifTagAndData(TAG_DATETIME,
1236 FMT_STRING,
1237 20,
1238 (long)(char*)dateBuf,
1239 FALSE,
1240 Buffer,
1241 &DirIndex,
1242 &DataWriteIndex);
1243
1244 }
1245 if (exifTagCount > 0) {
1246 int i;
1247 for (i = 0; i < exifTagCount + gpsTagCount; i++) {
1248 if (elements[i].GpsTag) {
1249 continue;
1250 }
1251 const TagTable_t* entry = TagToTagTableEntry(elements[i].Tag);
1252 if (entry == NULL) {
1253 continue;
1254 }
1255#ifdef SUPERDEBUG
1256 LOGE("create_EXIF saving tag %x value \"%s\"",elements[i].Tag, elements[i].Value);
1257#endif
1258 writeExifTagAndData(elements[i].Tag,
1259 entry->Format,
1260 entry->DataLength,
1261 (long)elements[i].Value,
1262 TRUE,
1263 Buffer,
1264 &DirIndex,
1265 &DataWriteIndex);
1266 }
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001267
The Android Open Source Project34a25642009-03-03 19:30:03 -08001268 if (gpsTagCount) {
1269 // Link to gps dir entry
1270 writeExifTagAndData(TAG_GPSINFO,
1271 FMT_ULONG,
1272 1,
1273 DataWriteIndex-8,
1274 FALSE,
1275 Buffer,
1276 &DirIndex,
1277 &DataWriteIndex);
1278 }
1279
1280 // Link to exif dir entry
1281 int exifDirPtr = DataWriteIndex-8;
1282 if (gpsTagCount) {
1283 exifDirPtr += 2 + gpsTagCount*12 + 4;
1284 }
Tyler Luu0aba81d2011-09-27 15:47:22 -05001285 DirExifLink = DirIndex;
The Android Open Source Project34a25642009-03-03 19:30:03 -08001286 writeExifTagAndData(TAG_EXIF_OFFSET,
1287 FMT_ULONG,
1288 1,
1289 exifDirPtr,
1290 FALSE,
1291 Buffer,
1292 &DirIndex,
1293 &DataWriteIndex);
1294 }
1295
1296 // End of directory - contains optional link to continued directory.
Tyler Luu0aba81d2011-09-27 15:47:22 -05001297 Put32u(Buffer+DirIndex, 0);
The Android Open Source Project34a25642009-03-03 19:30:03 -08001298 printf("Ending Exif section DirIndex = %d DataWriteIndex %d", DirIndex, DataWriteIndex);
1299 }
1300
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001301
The Android Open Source Project34a25642009-03-03 19:30:03 -08001302 // GPS Section
1303 if (gpsTagCount) {
1304 DirIndex = DataWriteIndex;
1305 printf("Starting GPS section DirIndex = %d", DirIndex);
1306 NumEntries = gpsTagCount;
1307 DataWriteIndex += 2 + NumEntries*12 + 4;
1308
1309 Put16u(Buffer+DirIndex, NumEntries); // Number of entries
1310 DirIndex += 2;
1311 {
1312 int i;
1313 for (i = 0; i < exifTagCount + gpsTagCount; i++) {
1314 if (!elements[i].GpsTag) {
1315 continue;
1316 }
1317 const TagTable_t* entry = GpsTagToTagTableEntry(elements[i].Tag);
1318 if (entry == NULL) {
1319 continue;
1320 }
1321#ifdef SUPERDEBUG
1322 LOGE("create_EXIF saving GPS tag %x value \"%s\"",elements[i].Tag, elements[i].Value);
1323#endif
1324 writeExifTagAndData(elements[i].Tag,
1325 entry->Format,
1326 entry->DataLength,
1327 (long)elements[i].Value,
1328 TRUE,
1329 Buffer,
1330 &DirIndex,
1331 &DataWriteIndex);
1332 }
1333 }
1334
1335 // End of directory - contains optional link to continued directory.
1336 Put32u(Buffer+DirIndex, 0);
1337 printf("Ending GPS section DirIndex = %d DataWriteIndex %d", DirIndex, DataWriteIndex);
1338 }
1339
1340 {
Tyler Luu0aba81d2011-09-27 15:47:22 -05001341 // Overwriting TAG_EXIF_OFFSET which links to this directory
1342 Put32u(Buffer+DirExifLink+8, DataWriteIndex-8);
The Android Open Source Project34a25642009-03-03 19:30:03 -08001343
1344 printf("Starting Thumbnail section DirIndex = %d", DirIndex);
1345 DirIndex = DataWriteIndex;
1346 NumEntries = 2;
1347 DataWriteIndex += 2 + NumEntries*12 + 4;
1348
1349 Put16u(Buffer+DirIndex, NumEntries); // Number of entries
1350 DirIndex += 2;
1351 {
1352 // Link to exif dir entry
1353 writeExifTagAndData(TAG_THUMBNAIL_OFFSET,
1354 FMT_ULONG,
1355 1,
1356 DataWriteIndex-8,
1357 FALSE,
1358 Buffer,
1359 &DirIndex,
1360 &DataWriteIndex);
1361 }
1362
1363 {
1364 // Link to exif dir entry
1365 writeExifTagAndData(TAG_THUMBNAIL_LENGTH,
1366 FMT_ULONG,
1367 1,
1368 0,
1369 FALSE,
1370 Buffer,
1371 &DirIndex,
1372 &DataWriteIndex);
1373 }
1374
1375 // End of directory - contains optional link to continued directory.
1376 Put32u(Buffer+DirIndex, 0);
1377 printf("Ending Thumbnail section DirIndex = %d DataWriteIndex %d", DirIndex, DataWriteIndex);
1378 }
1379
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001380
The Android Open Source Project34a25642009-03-03 19:30:03 -08001381 Buffer[0] = (unsigned char)(DataWriteIndex >> 8);
1382 Buffer[1] = (unsigned char)DataWriteIndex;
1383
1384 // Remove old exif section, if there was one.
1385 RemoveSectionType(M_EXIF);
1386
1387 {
1388 // Sections need malloced buffers, so do that now, especially because
1389 // we now know how big it needs to be allocated.
1390 unsigned char * NewBuf = malloc(DataWriteIndex);
1391 if (NewBuf == NULL){
1392 ErrFatal("Could not allocate memory");
1393 }
1394 memcpy(NewBuf, Buffer, DataWriteIndex);
1395
1396 CreateSection(M_EXIF, NewBuf, DataWriteIndex);
1397
1398 // Re-parse new exif section, now that its in place
1399 // otherwise, we risk touching data that has already been freed.
1400 process_EXIF(NewBuf, DataWriteIndex);
1401 }
1402}
1403
Takahiro Okada2645b482011-01-27 20:06:00 +09001404void create_EXIF(ExifElement_t* elements, int exifTagCount, int gpsTagCount)
1405{
1406 // It is hard to calculate exact necessary size for editing the exif
1407 // header dynamically, so we are using the maximum size of EXIF, 64K
1408 const int EXIF_MAX_SIZE = 1024*64;
1409 char* Buffer = malloc(EXIF_MAX_SIZE);
1410
1411 if (Buffer != NULL) {
1412 create_EXIF_internal(elements, exifTagCount, gpsTagCount, Buffer);
1413 free(Buffer);
1414 } else {
1415 ErrFatal("Could not allocate memory");
1416 }
1417}
1418
The Android Open Source Project34a25642009-03-03 19:30:03 -08001419//--------------------------------------------------------------------------
1420// Cler the rotation tag in the exif header to 1.
1421//--------------------------------------------------------------------------
1422const char * ClearOrientation(void)
1423{
1424 int a;
1425 if (NumOrientations == 0) return NULL;
1426
1427 for (a=0;a<NumOrientations;a++){
1428 switch(OrientationNumFormat[a]){
1429 case FMT_SBYTE:
1430 case FMT_BYTE:
1431 *(uchar *)(OrientationPtr[a]) = 1;
1432 break;
1433
1434 case FMT_USHORT:
1435 Put16u(OrientationPtr[a], 1);
1436 break;
1437
1438 case FMT_ULONG:
1439 case FMT_SLONG:
1440 memset(OrientationPtr, 0, 4);
1441 // Can't be bothered to write generic Put32 if I only use it once.
1442 if (MotorolaOrder){
1443 ((uchar *)OrientationPtr[a])[3] = 1;
1444 }else{
1445 ((uchar *)OrientationPtr[a])[0] = 1;
1446 }
1447 break;
1448
1449 default:
1450 return NULL;
1451 }
1452 }
1453
1454 return OrientTab[ImageInfo.Orientation];
1455}
1456
1457
1458
1459//--------------------------------------------------------------------------
1460// Remove thumbnail out of the exif image.
1461//--------------------------------------------------------------------------
1462int RemoveThumbnail(unsigned char * ExifSection)
1463{
1464 if (!DirWithThumbnailPtrs ||
1465 ImageInfo.ThumbnailOffset == 0 ||
1466 ImageInfo.ThumbnailSize == 0){
1467 // No thumbnail, or already deleted it.
1468 return 0;
1469 }
1470 if (ImageInfo.ThumbnailAtEnd == FALSE){
1471 ErrNonfatal("Thumbnail is not at end of header, can't chop it off", 0, 0);
1472 return 0;
1473 }
1474
1475 {
1476 int de;
1477 int NumDirEntries;
1478 NumDirEntries = Get16u(DirWithThumbnailPtrs);
1479
1480 for (de=0;de<NumDirEntries;de++){
1481 int Tag;
1482 unsigned char * DirEntry;
1483 DirEntry = DIR_ENTRY_ADDR(DirWithThumbnailPtrs, de);
1484 Tag = Get16u(DirEntry);
1485 if (Tag == TAG_THUMBNAIL_LENGTH){
1486 // Set length to zero.
1487 if (Get16u(DirEntry+2) != FMT_ULONG){
1488 // non standard format encoding. Can't do it.
1489 ErrNonfatal("Can't remove thumbnail", 0, 0);
1490 return 0;
1491 }
1492 Put32u(DirEntry+8, 0);
1493 }
1494 }
1495 }
1496
1497 // This is how far the non thumbnail data went.
1498 return ImageInfo.ThumbnailOffset+8;
1499
1500}
1501
1502
1503//--------------------------------------------------------------------------
1504// Convert exif time to Unix time structure
1505//--------------------------------------------------------------------------
1506int Exif2tm(struct tm * timeptr, char * ExifTime)
1507{
1508 int a;
1509
1510 timeptr->tm_wday = -1;
1511
1512 // Check for format: YYYY:MM:DD HH:MM:SS format.
1513 // Date and time normally separated by a space, but also seen a ':' there, so
1514 // skip the middle space with '%*c' so it can be any character.
1515 a = sscanf(ExifTime, "%d%*c%d%*c%d%*c%d:%d:%d",
1516 &timeptr->tm_year, &timeptr->tm_mon, &timeptr->tm_mday,
1517 &timeptr->tm_hour, &timeptr->tm_min, &timeptr->tm_sec);
1518
1519
1520 if (a == 6){
1521 timeptr->tm_isdst = -1;
1522 timeptr->tm_mon -= 1; // Adjust for unix zero-based months
1523 timeptr->tm_year -= 1900; // Adjust for year starting at 1900
1524 return TRUE; // worked.
1525 }
1526
1527 return FALSE; // Wasn't in Exif date format.
1528}
1529
1530
1531//--------------------------------------------------------------------------
1532// Show the collected image info, displaying camera F-stop and shutter speed
1533// in a consistent and legible fashion.
1534//--------------------------------------------------------------------------
1535void ShowImageInfo(int ShowFileInfo)
1536{
1537 if (ShowFileInfo){
1538 printf("File name : %s\n",ImageInfo.FileName);
1539 printf("File size : %d bytes\n",ImageInfo.FileSize);
1540
1541 {
1542 char Temp[20];
1543 FileTimeAsString(Temp);
1544 printf("File date : %s\n",Temp);
1545 }
1546 }
1547
1548 if (ImageInfo.CameraMake[0]){
1549 printf("Camera make : %s\n",ImageInfo.CameraMake);
1550 printf("Camera model : %s\n",ImageInfo.CameraModel);
1551 }
1552 if (ImageInfo.DateTime[0]){
1553 printf("Date/Time : %s\n",ImageInfo.DateTime);
1554 }
1555 printf("Resolution : %d x %d\n",ImageInfo.Width, ImageInfo.Height);
1556
1557 if (ImageInfo.Orientation > 1){
1558 // Only print orientation if one was supplied, and if its not 1 (normal orientation)
1559 printf("Orientation : %s\n", OrientTab[ImageInfo.Orientation]);
1560 }
1561
1562 if (ImageInfo.IsColor == 0){
1563 printf("Color/bw : Black and white\n");
1564 }
1565
1566 if (ImageInfo.FlashUsed >= 0){
1567 if (ImageInfo.FlashUsed & 1){
1568 printf("Flash used : Yes");
1569 switch (ImageInfo.FlashUsed){
1570 case 0x5: printf(" (Strobe light not detected)"); break;
1571 case 0x7: printf(" (Strobe light detected) "); break;
1572 case 0x9: printf(" (manual)"); break;
1573 case 0xd: printf(" (manual, return light not detected)"); break;
1574 case 0xf: printf(" (manual, return light detected)"); break;
1575 case 0x19:printf(" (auto)"); break;
1576 case 0x1d:printf(" (auto, return light not detected)"); break;
1577 case 0x1f:printf(" (auto, return light detected)"); break;
1578 case 0x41:printf(" (red eye reduction mode)"); break;
1579 case 0x45:printf(" (red eye reduction mode return light not detected)"); break;
1580 case 0x47:printf(" (red eye reduction mode return light detected)"); break;
1581 case 0x49:printf(" (manual, red eye reduction mode)"); break;
1582 case 0x4d:printf(" (manual, red eye reduction mode, return light not detected)"); break;
1583 case 0x4f:printf(" (red eye reduction mode, return light detected)"); break;
1584 case 0x59:printf(" (auto, red eye reduction mode)"); break;
1585 case 0x5d:printf(" (auto, red eye reduction mode, return light not detected)"); break;
1586 case 0x5f:printf(" (auto, red eye reduction mode, return light detected)"); break;
1587 }
1588 }else{
1589 printf("Flash used : No");
1590 switch (ImageInfo.FlashUsed){
1591 case 0x18:printf(" (auto)"); break;
1592 }
1593 }
1594 printf("\n");
1595 }
1596
1597
Wu-cheng Li574d52d2010-01-31 22:34:33 +08001598 if (ImageInfo.FocalLength.num != 0 && ImageInfo.FocalLength.denom != 0) {
1599 printf("Focal length : %4.1fmm",(double)ImageInfo.FocalLength.num / ImageInfo.FocalLength.denom);
The Android Open Source Project34a25642009-03-03 19:30:03 -08001600 if (ImageInfo.FocalLength35mmEquiv){
1601 printf(" (35mm equivalent: %dmm)", ImageInfo.FocalLength35mmEquiv);
1602 }
1603 printf("\n");
1604 }
1605
1606 if (ImageInfo.DigitalZoomRatio > 1){
1607 // Digital zoom used. Shame on you!
1608 printf("Digital Zoom : %1.3fx\n", (double)ImageInfo.DigitalZoomRatio);
1609 }
1610
1611 if (ImageInfo.CCDWidth){
1612 printf("CCD width : %4.2fmm\n",(double)ImageInfo.CCDWidth);
1613 }
1614
1615 if (ImageInfo.ExposureTime){
1616 if (ImageInfo.ExposureTime < 0.010){
1617 printf("Exposure time: %6.4f s ",(double)ImageInfo.ExposureTime);
1618 }else{
1619 printf("Exposure time: %5.3f s ",(double)ImageInfo.ExposureTime);
1620 }
1621 if (ImageInfo.ExposureTime <= 0.5){
1622 printf(" (1/%d)",(int)(0.5 + 1/ImageInfo.ExposureTime));
1623 }
1624 printf("\n");
1625 }
1626 if (ImageInfo.ApertureFNumber){
1627 printf("Aperture : f/%3.1f\n",(double)ImageInfo.ApertureFNumber);
1628 }
1629 if (ImageInfo.Distance){
1630 if (ImageInfo.Distance < 0){
1631 printf("Focus dist. : Infinite\n");
1632 }else{
1633 printf("Focus dist. : %4.2fm\n",(double)ImageInfo.Distance);
1634 }
1635 }
1636
1637 if (ImageInfo.ISOequivalent){
1638 printf("ISO equiv. : %2d\n",(int)ImageInfo.ISOequivalent);
1639 }
1640
1641 if (ImageInfo.ExposureBias){
1642 // If exposure bias was specified, but set to zero, presumably its no bias at all,
1643 // so only show it if its nonzero.
1644 printf("Exposure bias: %4.2f\n",(double)ImageInfo.ExposureBias);
1645 }
1646
1647 switch(ImageInfo.Whitebalance) {
1648 case 1:
1649 printf("Whitebalance : Manual\n");
1650 break;
1651 case 0:
1652 printf("Whitebalance : Auto\n");
1653 break;
1654 }
1655
1656 //Quercus: 17-1-2004 Added LightSource, some cams return this, whitebalance or both
1657 switch(ImageInfo.LightSource) {
1658 case 1:
1659 printf("Light Source : Daylight\n");
1660 break;
1661 case 2:
1662 printf("Light Source : Fluorescent\n");
1663 break;
1664 case 3:
1665 printf("Light Source : Incandescent\n");
1666 break;
1667 case 4:
1668 printf("Light Source : Flash\n");
1669 break;
1670 case 9:
1671 printf("Light Source : Fine weather\n");
1672 break;
1673 case 11:
1674 printf("Light Source : Shade\n");
1675 break;
1676 default:; //Quercus: 17-1-2004 There are many more modes for this, check Exif2.2 specs
1677 // If it just says 'unknown' or we don't know it, then
1678 // don't bother showing it - it doesn't add any useful information.
1679 }
1680
1681 if (ImageInfo.MeteringMode){ // 05-jan-2001 vcs
1682 switch(ImageInfo.MeteringMode) {
1683 case 2:
1684 printf("Metering Mode: center weight\n");
1685 break;
1686 case 3:
1687 printf("Metering Mode: spot\n");
1688 break;
1689 case 5:
1690 printf("Metering Mode: matrix\n");
1691 break;
1692 }
1693 }
1694
1695 if (ImageInfo.ExposureProgram){ // 05-jan-2001 vcs
1696 switch(ImageInfo.ExposureProgram) {
1697 case 1:
1698 printf("Exposure : Manual\n");
1699 break;
1700 case 2:
1701 printf("Exposure : program (auto)\n");
1702 break;
1703 case 3:
1704 printf("Exposure : aperture priority (semi-auto)\n");
1705 break;
1706 case 4:
1707 printf("Exposure : shutter priority (semi-auto)\n");
1708 break;
1709 case 5:
1710 printf("Exposure : Creative Program (based towards depth of field)\n");
1711 break;
1712 case 6:
1713 printf("Exposure : Action program (based towards fast shutter speed)\n");
1714 break;
1715 case 7:
1716 printf("Exposure : Portrait Mode\n");
1717 break;
1718 case 8:
1719 printf("Exposure : LandscapeMode \n");
1720 break;
1721 default:
1722 break;
1723 }
1724 }
1725 switch(ImageInfo.ExposureMode){
1726 case 0: // Automatic (not worth cluttering up output for)
1727 break;
1728 case 1: printf("Exposure Mode: Manual\n");
1729 break;
1730 case 2: printf("Exposure Mode: Auto bracketing\n");
1731 break;
1732 }
1733
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001734 if (ImageInfo.DistanceRange) {
1735 printf("Focus range : ");
1736 switch(ImageInfo.DistanceRange) {
1737 case 1:
1738 printf("macro");
1739 break;
1740 case 2:
1741 printf("close");
1742 break;
1743 case 3:
1744 printf("distant");
1745 break;
1746 }
1747 printf("\n");
1748 }
1749
1750
The Android Open Source Project34a25642009-03-03 19:30:03 -08001751
1752 if (ImageInfo.Process != M_SOF0){
1753 // don't show it if its the plain old boring 'baseline' process, but do
1754 // show it if its something else, like 'progressive' (used on web sometimes)
1755 int a;
1756 for (a=0;;a++){
1757 if (a >= (int)PROCESS_TABLE_SIZE){
1758 // ran off the end of the table.
1759 printf("Jpeg process : Unknown\n");
1760 break;
1761 }
1762 if (ProcessTable[a].Tag == ImageInfo.Process){
1763 printf("Jpeg process : %s\n",ProcessTable[a].Desc);
1764 break;
1765 }
1766 }
1767 }
1768
1769 if (ImageInfo.GpsInfoPresent){
1770 printf("GPS Latitude : %s\n",ImageInfo.GpsLat);
1771 printf("GPS Longitude: %s\n",ImageInfo.GpsLong);
1772 if (ImageInfo.GpsAlt[0]) printf("GPS Altitude : %s\n",ImageInfo.GpsAlt);
1773 }
1774
1775 // Print the comment. Print 'Comment:' for each new line of comment.
1776 if (ImageInfo.Comments[0]){
1777 int a,c;
1778 printf("Comment : ");
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001779 if (!ImageInfo.CommentWidchars){
1780 for (a=0;a<MAX_COMMENT_SIZE;a++){
1781 c = ImageInfo.Comments[a];
1782 if (c == '\0') break;
1783 if (c == '\n'){
1784 // Do not start a new line if the string ends with a carriage return.
1785 if (ImageInfo.Comments[a+1] != '\0'){
1786 printf("\nComment : ");
1787 }else{
1788 printf("\n");
1789 }
The Android Open Source Project34a25642009-03-03 19:30:03 -08001790 }else{
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001791 putchar(c);
The Android Open Source Project34a25642009-03-03 19:30:03 -08001792 }
The Android Open Source Project34a25642009-03-03 19:30:03 -08001793 }
Chih-Chung Changd6a02c32009-03-24 20:34:28 -07001794 printf("\n");
1795 }else{
1796 printf("%.*ls\n", ImageInfo.CommentWidchars, (wchar_t *)ImageInfo.Comments);
The Android Open Source Project34a25642009-03-03 19:30:03 -08001797 }
The Android Open Source Project34a25642009-03-03 19:30:03 -08001798 }
1799 if (ImageInfo.ThumbnailOffset){
1800 printf("Map: %05d-%05d: Thumbnail\n",ImageInfo.ThumbnailOffset, ImageInfo.ThumbnailOffset+ImageInfo.ThumbnailSize);
1801 } else {
1802 printf("NO thumbnail");
1803 }
1804}
1805
1806
1807//--------------------------------------------------------------------------
1808// Summarize highlights of image info on one line (suitable for grep-ing)
1809//--------------------------------------------------------------------------
1810void ShowConciseImageInfo(void)
1811{
1812 printf("\"%s\"",ImageInfo.FileName);
1813
1814 printf(" %dx%d",ImageInfo.Width, ImageInfo.Height);
1815
1816 if (ImageInfo.ExposureTime){
1817 if (ImageInfo.ExposureTime <= 0.5){
1818 printf(" (1/%d)",(int)(0.5 + 1/ImageInfo.ExposureTime));
1819 }else{
1820 printf(" (%1.1f)",ImageInfo.ExposureTime);
1821 }
1822 }
1823
1824 if (ImageInfo.ApertureFNumber){
1825 printf(" f/%3.1f",(double)ImageInfo.ApertureFNumber);
1826 }
1827
1828 if (ImageInfo.FocalLength35mmEquiv){
1829 printf(" f(35)=%dmm",ImageInfo.FocalLength35mmEquiv);
1830 }
1831
1832 if (ImageInfo.FlashUsed >= 0 && ImageInfo.FlashUsed & 1){
1833 printf(" (flash)");
1834 }
1835
1836 if (ImageInfo.IsColor == 0){
1837 printf(" (bw)");
1838 }
1839
1840 printf("\n");
1841}