23 #define NUM_FORMATS 12
28 #define FMT_URATIONAL 5
30 #define FMT_UNDEFINED 7
33 #define FMT_SRATIONAL 10
37 #define TAG_EXIF_VERSION 0x9000
38 #define TAG_EXIF_OFFSET 0x8769
39 #define TAG_INTEROP_OFFSET 0xa005
40 #define TAG_MAKE 0x010F
41 #define TAG_MODEL 0x0110
42 #define TAG_ORIENTATION 0x0112
43 #define TAG_XRESOLUTION 0x011A
44 #define TAG_YRESOLUTION 0x011B
45 #define TAG_RESOLUTIONUNIT 0x0128
46 #define TAG_EXPOSURETIME 0x829A
47 #define TAG_FNUMBER 0x829D
48 #define TAG_SHUTTERSPEED 0x9201
49 #define TAG_APERTURE 0x9202
50 #define TAG_BRIGHTNESS 0x9203
51 #define TAG_MAXAPERTURE 0x9205
52 #define TAG_FOCALLENGTH 0x920A
53 #define TAG_DATETIME_ORIGINAL 0x9003
54 #define TAG_USERCOMMENT 0x9286
55 #define TAG_SUBJECT_DISTANCE 0x9206
56 #define TAG_FLASH 0x9209
57 #define TAG_FOCALPLANEXRES 0xa20E
58 #define TAG_FOCALPLANEYRES 0xa20F
59 #define TAG_FOCALPLANEUNITS 0xa210
60 #define TAG_EXIF_IMAGEWIDTH 0xA002
61 #define TAG_EXIF_IMAGELENGTH 0xA003
62 #define TAG_EXPOSURE_BIAS 0x9204
63 #define TAG_WHITEBALANCE 0x9208
64 #define TAG_METERING_MODE 0x9207
65 #define TAG_EXPOSURE_PROGRAM 0x8822
66 #define TAG_ISO_EQUIVALENT 0x8827
67 #define TAG_COMPRESSION_LEVEL 0x9102
68 #define TAG_THUMBNAIL_OFFSET 0x0201
69 #define TAG_THUMBNAIL_LENGTH 0x0202
80 void Cexif::ClearExif()
84 for(int i=0;i<MAX_SECTIONS;i++)
85 if(Sections[i].Data) free(Sections[i].Data);
91 bool Cexif::DecodeExif(const char *filename, int Thumb)
94 FILE * hFile = fopen(filename, "r");
95 if(!hFile) return ret;
97 m_exifinfo = new EXIFINFO;
98 memset(m_exifinfo,0,sizeof(EXIFINFO));
100 m_exifinfo->Thumnailstate = Thumb;
102 m_szLastError[0]='\0';
103 ExifImageWidth = MotorolaOrder = SectionsRead=0;
104 memset(&Sections, 0, MAX_SECTIONS * sizeof(Section_t));
107 int a = fgetc(hFile);
108 strcpy(m_szLastError,"EXIF-Data not found");
110 if (a != 0xff || fgetc(hFile) != M_SOI)
111 goto decode_exif_out;
116 int ll,lh, got, itemlen;
117 unsigned char * Data;
119 if (SectionsRead >= MAX_SECTIONS)
121 strcpy(m_szLastError,"Too many sections in jpg file");
122 goto decode_exif_out;
127 marker = fgetc(hFile);
128 if (marker != 0xff) break;
132 strcpy(m_szLastError,"too many padding unsigned chars\n");
133 goto decode_exif_out;
139 strcpy(m_szLastError,"too many padding unsigned chars!");
140 goto decode_exif_out;
143 Sections[SectionsRead].Type = marker;
148 itemlen = (lh << 8) | ll;
152 strcpy(m_szLastError,"invalid marker");
153 goto decode_exif_out;
155 Sections[SectionsRead].Size = itemlen;
157 Data = (unsigned char *)malloc(itemlen);
160 strcpy(m_szLastError,"Could not allocate memory");
161 goto decode_exif_out;
163 Sections[SectionsRead].Data = Data;
166 Data[0] = (unsigned char)lh;
167 Data[1] = (unsigned char)ll;
169 got = fread(Data+2, 1, itemlen-2,hFile);
170 if (got != itemlen-2)
172 strcpy(m_szLastError,"Premature end of file?");
173 goto decode_exif_out;
182 printf("No image in jpeg!\n");
183 goto decode_exif_out;
187 free(Sections[--SectionsRead].Data);
188 Sections[SectionsRead].Data=0;
192 process_COM(Data, itemlen);
197 free(Sections[--SectionsRead].Data);
198 Sections[SectionsRead].Data=0;
201 if (memcmp(Data+2, "Exif", 4) == 0)
203 m_exifinfo->IsExif = process_EXIF((unsigned char *)Data+2, itemlen);
207 free(Sections[--SectionsRead].Data);
208 Sections[SectionsRead].Data=0;
224 process_SOFn(Data, marker);
237 bool Cexif::process_EXIF(unsigned char * CharBuf, unsigned int length)
239 m_exifinfo->Comments[0] = '\0';
242 static const unsigned char ExifHeader[] = "Exif\0\0";
243 if(memcmp(CharBuf+0, ExifHeader,6))
245 strcpy(m_szLastError,"Incorrect Exif header"); return false;
248 if (memcmp(CharBuf+6,"II",2) == 0) MotorolaOrder = 0;
251 if (memcmp(CharBuf+6,"MM",2) == 0) MotorolaOrder = 1;
254 strcpy(m_szLastError,"Invalid Exif alignment marker."); return false;
258 if (Get16u(CharBuf+8) != 0x2a)
260 strcpy(m_szLastError,"Invalid Exif start (1)"); return false;
262 int FirstOffset = Get32u(CharBuf+10);
263 if (FirstOffset < 8 || FirstOffset > 16)
265 strcpy(m_szLastError,"Suspicious offset of first IFD value"); return 0;
267 unsigned char * LastExifRefd = CharBuf;
269 if (!ProcessExifDir(CharBuf+14, CharBuf+6, length-6, m_exifinfo, &LastExifRefd)) return false;
271 if (m_exifinfo->FocalplaneXRes != 0)
272 m_exifinfo->CCDWidth = (float)(ExifImageWidth * m_exifinfo->FocalplaneUnits / m_exifinfo->FocalplaneXRes);
277 int Cexif::Get16m(void * Short)
279 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
282 int Cexif::Get16u(void * Short)
286 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
290 return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
294 long Cexif::Get32s(void * Long)
298 return ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16) | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
302 return ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16) | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
306 unsigned long Cexif::Get32u(void * Long)
308 return (unsigned long)Get32s(Long) & 0xffffffff;
311 bool Cexif::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength, EXIFINFO * const m_exifinfo, unsigned char ** const LastExifRefdP )
313 int de, a, NumDirEntries;
314 unsigned ThumbnailOffset = 0;
315 unsigned ThumbnailSize = 0;
317 NumDirEntries = Get16u(DirStart);
319 if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength))
321 strcpy(m_szLastError,"Illegally sized directory"); return 0;
324 for (de=0;de<NumDirEntries;de++)
326 int Tag, Format, Components;
327 unsigned char * ValuePtr;
329 unsigned char * DirEntry;
330 DirEntry = DirStart+2+12*de;
331 Tag = Get16u(DirEntry);
332 Format = Get16u(DirEntry+2);
333 Components = Get32u(DirEntry+4);
335 if ((Format-1) >= NUM_FORMATS)
337 strcpy(m_szLastError,"Illegal format code in EXIF dir"); return 0;
340 BytesCount = Components * BytesPerFormat[Format];
345 OffsetVal = Get32u(DirEntry+8);
346 if (OffsetVal+BytesCount > ExifLength)
348 strcpy(m_szLastError,"Illegal pointer offset value in EXIF."); return 0;
350 ValuePtr = OffsetBase+OffsetVal;
352 else ValuePtr = DirEntry+8;
354 if (*LastExifRefdP < ValuePtr+BytesCount) *LastExifRefdP = ValuePtr+BytesCount;
359 strncpy(m_exifinfo->CameraMake, (char*)ValuePtr, 31);
362 strncpy(m_exifinfo->CameraModel, (char*)ValuePtr, 39);
364 case TAG_EXIF_VERSION:
365 strncpy(m_exifinfo->Version,(char*)ValuePtr, 4);
367 case TAG_DATETIME_ORIGINAL:
368 strncpy(m_exifinfo->DateTime, (char*)ValuePtr, 19);
370 case TAG_USERCOMMENT:
374 if (((char*)ValuePtr)[a] == ' ') ((char*)ValuePtr)[a] = '\0';
380 if (memcmp(ValuePtr, "ASCII",5) == 0)
385 c = ((char*)ValuePtr)[a];
386 if (c != '\0' && c != ' ')
388 strncpy(m_exifinfo->Comments, (char*)ValuePtr+a, 199);
394 else strncpy(m_exifinfo->Comments, (char*)ValuePtr, 199);
397 m_exifinfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format);
400 case TAG_MAXAPERTURE:
401 if (m_exifinfo->ApertureFNumber == 0)
403 //m_exifinfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5);
407 m_exifinfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format);
409 case TAG_FOCALLENGTH:
410 m_exifinfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format);
412 case TAG_SUBJECT_DISTANCE:
413 m_exifinfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format);
415 case TAG_EXPOSURETIME:
416 m_exifinfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format);
418 case TAG_SHUTTERSPEED:
419 if (m_exifinfo->ExposureTime == 0)
421 //m_exifinfo->ExposureTime = (float) (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2)));
425 if ((int)ConvertAnyFormat(ValuePtr, Format) & 7) strcpy(m_exifinfo->FlashUsed,"fire");
426 else strcpy(m_exifinfo->FlashUsed,"not fired");
428 case TAG_ORIENTATION:
429 m_exifinfo->Orient = (int)ConvertAnyFormat(ValuePtr, Format);
430 switch((int)ConvertAnyFormat(ValuePtr, Format))
432 case 1: strcpy(m_exifinfo->Orientation,"Top-Left"); break;
433 case 2: strcpy(m_exifinfo->Orientation,"Top-Right"); break;
434 case 3: strcpy(m_exifinfo->Orientation,"Bottom-Right"); break;
435 case 4: strcpy(m_exifinfo->Orientation,"Bottom-Left"); break;
436 case 5: strcpy(m_exifinfo->Orientation,"Left-Top"); break;
437 case 6: strcpy(m_exifinfo->Orientation,"Right-Top"); break;
438 case 7: strcpy(m_exifinfo->Orientation,"Right-Bottom"); break;
439 case 8: strcpy(m_exifinfo->Orientation,"Left-Bottom"); break;
440 default: strcpy(m_exifinfo->Orientation,"Undefined rotation value");
443 case TAG_EXIF_IMAGELENGTH:
444 case TAG_EXIF_IMAGEWIDTH:
445 a = (int)ConvertAnyFormat(ValuePtr, Format);
446 if (ExifImageWidth < a) ExifImageWidth = a;
448 case TAG_FOCALPLANEXRES:
449 m_exifinfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format);
451 case TAG_FOCALPLANEYRES:
452 m_exifinfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format);
454 case TAG_RESOLUTIONUNIT:
455 switch((int)ConvertAnyFormat(ValuePtr, Format))
457 case 2: strcpy(m_exifinfo->ResolutionUnit,"inches"); break;
458 case 3: strcpy(m_exifinfo->ResolutionUnit,"centimeters"); break;
459 default: strcpy(m_exifinfo->ResolutionUnit,"reserved");
462 case TAG_FOCALPLANEUNITS:
463 switch((int)ConvertAnyFormat(ValuePtr, Format))
465 case 1: m_exifinfo->FocalplaneUnits = 1.0f; break;
466 case 2: m_exifinfo->FocalplaneUnits = 1.0f; break;
467 case 3: m_exifinfo->FocalplaneUnits = 0.3937007874f; break;
468 case 4: m_exifinfo->FocalplaneUnits = 0.03937007874f; break;
469 case 5: m_exifinfo->FocalplaneUnits = 0.00003937007874f;
472 case TAG_EXPOSURE_BIAS:
473 m_exifinfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format);
475 case TAG_WHITEBALANCE:
476 switch((int)ConvertAnyFormat(ValuePtr, Format))
478 case 0: strcpy(m_exifinfo->LightSource,"unknown"); break;
479 case 1: strcpy(m_exifinfo->LightSource,"Daylight"); break;
480 case 2: strcpy(m_exifinfo->LightSource,"Fluorescent"); break;
481 case 3: strcpy(m_exifinfo->LightSource,"Tungsten"); break;
482 case 17: strcpy(m_exifinfo->LightSource,"Standard light A"); break;
483 case 18: strcpy(m_exifinfo->LightSource,"Standard light B"); break;
484 case 19: strcpy(m_exifinfo->LightSource,"Standard light C"); break;
485 case 20: strcpy(m_exifinfo->LightSource,"D55"); break;
486 case 21: strcpy(m_exifinfo->LightSource,"D65"); break;
487 case 22: strcpy(m_exifinfo->LightSource,"D75"); break;
488 default: strcpy(m_exifinfo->LightSource,"other"); break;
491 case TAG_METERING_MODE:
492 switch((int)ConvertAnyFormat(ValuePtr, Format))
494 case 0: strcpy(m_exifinfo->MeteringMode,"unknown"); break;
495 case 1: strcpy(m_exifinfo->MeteringMode,"Average"); break;
496 case 2: strcpy(m_exifinfo->MeteringMode,"Center-Weighted-Average"); break;
497 case 3: strcpy(m_exifinfo->MeteringMode,"Spot"); break;
498 case 4: strcpy(m_exifinfo->MeteringMode,"MultiSpot"); break;
499 case 5: strcpy(m_exifinfo->MeteringMode,"Pattern"); break;
500 case 6: strcpy(m_exifinfo->MeteringMode,"Partial"); break;
501 default: strcpy(m_exifinfo->MeteringMode,"other"); break;
504 case TAG_EXPOSURE_PROGRAM:
505 switch((int)ConvertAnyFormat(ValuePtr, Format))
507 case 0: strcpy(m_exifinfo->ExposureProgram,"not defined"); break;
508 case 1: strcpy(m_exifinfo->ExposureProgram,"Manual"); break;
509 case 2: strcpy(m_exifinfo->ExposureProgram,"Normal program"); break;
510 case 3: strcpy(m_exifinfo->ExposureProgram,"Aperture priority"); break;
511 case 4: strcpy(m_exifinfo->ExposureProgram,"Shutter priority"); break;
512 case 5: strcpy(m_exifinfo->ExposureProgram,"Creative program"); break;
513 case 6: strcpy(m_exifinfo->ExposureProgram,"Action program"); break;
514 case 7: strcpy(m_exifinfo->ExposureProgram,"Portrait mode"); break;
515 case 8: strcpy(m_exifinfo->ExposureProgram,"Landscape mode"); break;
516 default: strcpy(m_exifinfo->ExposureProgram,"reserved"); break;
519 case TAG_ISO_EQUIVALENT:
520 m_exifinfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format);
521 if ( m_exifinfo->ISOequivalent < 50 ) m_exifinfo->ISOequivalent *= 200;
523 case TAG_COMPRESSION_LEVEL:
524 m_exifinfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format);
526 case TAG_XRESOLUTION:
527 m_exifinfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format);
529 case TAG_YRESOLUTION:
530 m_exifinfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format);
532 case TAG_THUMBNAIL_OFFSET:
533 ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format);
535 case TAG_THUMBNAIL_LENGTH:
536 ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format);
540 if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET)
542 unsigned char * SubdirStart;
543 SubdirStart = OffsetBase + Get32u(ValuePtr);
544 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
546 strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
548 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
554 unsigned char * SubdirStart;
556 Offset = Get16u(DirStart+2+12*NumDirEntries);
559 SubdirStart = OffsetBase + Offset;
560 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
562 strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
564 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
567 if (ThumbnailSize && ThumbnailOffset && m_exifinfo->Thumnailstate)
569 if (ThumbnailSize + ThumbnailOffset <= ExifLength)
571 if(FILE *tf = fopen(THUMBNAILTMPFILE, "w"))
573 fwrite( OffsetBase + ThumbnailOffset, ThumbnailSize, 1, tf);
575 m_exifinfo->Thumnailstate = 2;
583 double Cexif::ConvertAnyFormat(void * ValuePtr, int Format)
589 case FMT_SBYTE: Value = *(signed char *)ValuePtr; break;
590 case FMT_BYTE: Value = *(unsigned char *)ValuePtr; break;
591 case FMT_USHORT: Value = Get16u(ValuePtr); break;
592 case FMT_ULONG: Value = Get32u(ValuePtr); break;
596 int Num = Get32s(ValuePtr);
597 int Den = Get32s(4+(char *)ValuePtr);
598 if (Den == 0) Value = 0;
599 else Value = (double)Num/Den;
602 case FMT_SSHORT: Value = (signed short)Get16u(ValuePtr); break;
603 case FMT_SLONG: Value = Get32s(ValuePtr); break;
604 case FMT_SINGLE: Value = (double)*(float *)ValuePtr; break;
605 case FMT_DOUBLE: Value = *(double *)ValuePtr; break;
610 void Cexif::process_COM (const unsigned char * Data, int length)
613 char Comment[MAX_COMMENT+1];
616 if (length > MAX_COMMENT) length = MAX_COMMENT;
618 for (a=2;a<length;a++)
621 if (ch == '\r' && Data[a+1] == '\n') continue;
622 if ((ch>=0x20) || ch == '\n' || ch == '\t') Comment[nch++] = (char)ch;
623 else Comment[nch++] = '?';
626 strcpy(m_exifinfo->Comments,Comment);
629 void Cexif::process_SOFn (const unsigned char * Data, int marker)
631 int data_precision, num_components;
633 data_precision = Data[2];
634 m_exifinfo->Height = Get16m((void*)(Data+3));
635 m_exifinfo->Width = Get16m((void*)(Data+5));
636 num_components = Data[7];
638 if (num_components == 3) strcpy(m_exifinfo->IsColor,"yes");
639 else strcpy(m_exifinfo->IsColor,"no");
641 m_exifinfo->Process = marker;