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)
93 FILE * hFile = fopen(filename, "r");
94 if(!hFile) return false;
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) return false;
115 int ll,lh, got, itemlen;
116 unsigned char * Data;
118 if (SectionsRead >= MAX_SECTIONS)
120 strcpy(m_szLastError,"Too many sections in jpg file"); return false;
125 marker = fgetc(hFile);
126 if (marker != 0xff) break;
130 strcpy(m_szLastError,"too many padding unsigned chars\n"); return false;
136 strcpy(m_szLastError,"too many padding unsigned chars!"); return false;
139 Sections[SectionsRead].Type = marker;
144 itemlen = (lh << 8) | ll;
148 strcpy(m_szLastError,"invalid marker"); return false;
150 Sections[SectionsRead].Size = itemlen;
152 Data = (unsigned char *)malloc(itemlen);
155 strcpy(m_szLastError,"Could not allocate memory"); return false;
157 Sections[SectionsRead].Data = Data;
160 Data[0] = (unsigned char)lh;
161 Data[1] = (unsigned char)ll;
163 got = fread(Data+2, 1, itemlen-2,hFile);
164 if (got != itemlen-2)
166 strcpy(m_szLastError,"Premature end of file?"); return false;
175 printf("No image in jpeg!\n");
180 free(Sections[--SectionsRead].Data);
181 Sections[SectionsRead].Data=0;
185 process_COM(Data, itemlen);
190 free(Sections[--SectionsRead].Data);
191 Sections[SectionsRead].Data=0;
194 if (memcmp(Data+2, "Exif", 4) == 0)
196 m_exifinfo->IsExif = process_EXIF((unsigned char *)Data+2, itemlen);
200 free(Sections[--SectionsRead].Data);
201 Sections[SectionsRead].Data=0;
217 process_SOFn(Data, marker);
228 bool Cexif::process_EXIF(unsigned char * CharBuf, unsigned int length)
230 m_exifinfo->Comments[0] = '\0';
233 static const unsigned char ExifHeader[] = "Exif\0\0";
234 if(memcmp(CharBuf+0, ExifHeader,6))
236 strcpy(m_szLastError,"Incorrect Exif header"); return false;
239 if (memcmp(CharBuf+6,"II",2) == 0) MotorolaOrder = 0;
242 if (memcmp(CharBuf+6,"MM",2) == 0) MotorolaOrder = 1;
245 strcpy(m_szLastError,"Invalid Exif alignment marker."); return false;
249 if (Get16u(CharBuf+8) != 0x2a)
251 strcpy(m_szLastError,"Invalid Exif start (1)"); return false;
253 int FirstOffset = Get32u(CharBuf+10);
254 if (FirstOffset < 8 || FirstOffset > 16)
256 strcpy(m_szLastError,"Suspicious offset of first IFD value"); return 0;
258 unsigned char * LastExifRefd = CharBuf;
260 if (!ProcessExifDir(CharBuf+14, CharBuf+6, length-6, m_exifinfo, &LastExifRefd)) return false;
262 if (m_exifinfo->FocalplaneXRes != 0)
263 m_exifinfo->CCDWidth = (float)(ExifImageWidth * m_exifinfo->FocalplaneUnits / m_exifinfo->FocalplaneXRes);
268 int Cexif::Get16m(void * Short)
270 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
273 int Cexif::Get16u(void * Short)
277 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
281 return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
285 long Cexif::Get32s(void * Long)
289 return ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16) | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
293 return ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16) | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
297 unsigned long Cexif::Get32u(void * Long)
299 return (unsigned long)Get32s(Long) & 0xffffffff;
302 bool Cexif::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength, EXIFINFO * const m_exifinfo, unsigned char ** const LastExifRefdP )
304 int de, a, NumDirEntries;
305 unsigned ThumbnailOffset = 0;
306 unsigned ThumbnailSize = 0;
308 NumDirEntries = Get16u(DirStart);
310 if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength))
312 strcpy(m_szLastError,"Illegally sized directory"); return 0;
315 for (de=0;de<NumDirEntries;de++)
317 int Tag, Format, Components;
318 unsigned char * ValuePtr;
320 unsigned char * DirEntry;
321 DirEntry = DirStart+2+12*de;
322 Tag = Get16u(DirEntry);
323 Format = Get16u(DirEntry+2);
324 Components = Get32u(DirEntry+4);
326 if ((Format-1) >= NUM_FORMATS)
328 strcpy(m_szLastError,"Illegal format code in EXIF dir"); return 0;
331 BytesCount = Components * BytesPerFormat[Format];
336 OffsetVal = Get32u(DirEntry+8);
337 if (OffsetVal+BytesCount > ExifLength)
339 strcpy(m_szLastError,"Illegal pointer offset value in EXIF."); return 0;
341 ValuePtr = OffsetBase+OffsetVal;
343 else ValuePtr = DirEntry+8;
345 if (*LastExifRefdP < ValuePtr+BytesCount) *LastExifRefdP = ValuePtr+BytesCount;
350 strncpy(m_exifinfo->CameraMake, (char*)ValuePtr, 31);
353 strncpy(m_exifinfo->CameraModel, (char*)ValuePtr, 39);
355 case TAG_EXIF_VERSION:
356 strncpy(m_exifinfo->Version,(char*)ValuePtr, 4);
358 case TAG_DATETIME_ORIGINAL:
359 strncpy(m_exifinfo->DateTime, (char*)ValuePtr, 19);
361 case TAG_USERCOMMENT:
365 if (((char*)ValuePtr)[a] == ' ') ((char*)ValuePtr)[a] = '\0';
371 if (memcmp(ValuePtr, "ASCII",5) == 0)
376 c = ((char*)ValuePtr)[a];
377 if (c != '\0' && c != ' ')
379 strncpy(m_exifinfo->Comments, (char*)ValuePtr+a, 199);
385 else strncpy(m_exifinfo->Comments, (char*)ValuePtr, 199);
388 m_exifinfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format);
391 case TAG_MAXAPERTURE:
392 if (m_exifinfo->ApertureFNumber == 0)
394 //m_exifinfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5);
398 m_exifinfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format);
400 case TAG_FOCALLENGTH:
401 m_exifinfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format);
403 case TAG_SUBJECT_DISTANCE:
404 m_exifinfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format);
406 case TAG_EXPOSURETIME:
407 m_exifinfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format);
409 case TAG_SHUTTERSPEED:
410 if (m_exifinfo->ExposureTime == 0)
412 //m_exifinfo->ExposureTime = (float) (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2)));
416 if ((int)ConvertAnyFormat(ValuePtr, Format) & 7) strcpy(m_exifinfo->FlashUsed,"fire");
417 else strcpy(m_exifinfo->FlashUsed,"not fired");
419 case TAG_ORIENTATION:
420 m_exifinfo->Orient = (int)ConvertAnyFormat(ValuePtr, Format);
421 switch((int)ConvertAnyFormat(ValuePtr, Format))
423 case 1: strcpy(m_exifinfo->Orientation,"Top-Left"); break;
424 case 2: strcpy(m_exifinfo->Orientation,"Top-Right"); break;
425 case 3: strcpy(m_exifinfo->Orientation,"Bottom-Right"); break;
426 case 4: strcpy(m_exifinfo->Orientation,"Bottom-Left"); break;
427 case 5: strcpy(m_exifinfo->Orientation,"Left-Top"); break;
428 case 6: strcpy(m_exifinfo->Orientation,"Right-Top"); break;
429 case 7: strcpy(m_exifinfo->Orientation,"Right-Bottom"); break;
430 case 8: strcpy(m_exifinfo->Orientation,"Left-Bottom"); break;
431 default: strcpy(m_exifinfo->Orientation,"Undefined"); break;
434 case TAG_EXIF_IMAGELENGTH:
435 case TAG_EXIF_IMAGEWIDTH:
436 a = (int)ConvertAnyFormat(ValuePtr, Format);
437 if (ExifImageWidth < a) ExifImageWidth = a;
439 case TAG_FOCALPLANEXRES:
440 m_exifinfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format);
442 case TAG_FOCALPLANEYRES:
443 m_exifinfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format);
445 case TAG_RESOLUTIONUNIT:
446 switch((int)ConvertAnyFormat(ValuePtr, Format))
448 case 2: strcpy(m_exifinfo->ResolutionUnit,"inches"); break;
449 case 3: strcpy(m_exifinfo->ResolutionUnit,"centimeters"); break;
450 default: strcpy(m_exifinfo->ResolutionUnit,"reserved");
453 case TAG_FOCALPLANEUNITS:
454 switch((int)ConvertAnyFormat(ValuePtr, Format))
456 case 1: m_exifinfo->FocalplaneUnits = 1.0f; break;
457 case 2: m_exifinfo->FocalplaneUnits = 1.0f; break;
458 case 3: m_exifinfo->FocalplaneUnits = 0.3937007874f; break;
459 case 4: m_exifinfo->FocalplaneUnits = 0.03937007874f; break;
460 case 5: m_exifinfo->FocalplaneUnits = 0.00003937007874f;
463 case TAG_EXPOSURE_BIAS:
464 m_exifinfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format);
466 case TAG_WHITEBALANCE:
467 switch((int)ConvertAnyFormat(ValuePtr, Format))
469 case 0: strcpy(m_exifinfo->LightSource,"unknown"); break;
470 case 1: strcpy(m_exifinfo->LightSource,"Daylight"); break;
471 case 2: strcpy(m_exifinfo->LightSource,"Fluorescent"); break;
472 case 3: strcpy(m_exifinfo->LightSource,"Tungsten"); break;
473 case 17: strcpy(m_exifinfo->LightSource,"Standard light A"); break;
474 case 18: strcpy(m_exifinfo->LightSource,"Standard light B"); break;
475 case 19: strcpy(m_exifinfo->LightSource,"Standard light C"); break;
476 case 20: strcpy(m_exifinfo->LightSource,"D55"); break;
477 case 21: strcpy(m_exifinfo->LightSource,"D65"); break;
478 case 22: strcpy(m_exifinfo->LightSource,"D75"); break;
479 default: strcpy(m_exifinfo->LightSource,"other"); break;
482 case TAG_METERING_MODE:
483 switch((int)ConvertAnyFormat(ValuePtr, Format))
485 case 0: strcpy(m_exifinfo->MeteringMode,"unknown"); break;
486 case 1: strcpy(m_exifinfo->MeteringMode,"Average"); break;
487 case 2: strcpy(m_exifinfo->MeteringMode,"Center-Weighted-Average"); break;
488 case 3: strcpy(m_exifinfo->MeteringMode,"Spot"); break;
489 case 4: strcpy(m_exifinfo->MeteringMode,"MultiSpot"); break;
490 case 5: strcpy(m_exifinfo->MeteringMode,"Pattern"); break;
491 case 6: strcpy(m_exifinfo->MeteringMode,"Partial"); break;
492 default: strcpy(m_exifinfo->MeteringMode,"other"); break;
495 case TAG_EXPOSURE_PROGRAM:
496 switch((int)ConvertAnyFormat(ValuePtr, Format))
498 case 0: strcpy(m_exifinfo->ExposureProgram,"not defined"); break;
499 case 1: strcpy(m_exifinfo->ExposureProgram,"Manual"); break;
500 case 2: strcpy(m_exifinfo->ExposureProgram,"Normal program"); break;
501 case 3: strcpy(m_exifinfo->ExposureProgram,"Aperture priority"); break;
502 case 4: strcpy(m_exifinfo->ExposureProgram,"Shutter priority"); break;
503 case 5: strcpy(m_exifinfo->ExposureProgram,"Creative program"); break;
504 case 6: strcpy(m_exifinfo->ExposureProgram,"Action program"); break;
505 case 7: strcpy(m_exifinfo->ExposureProgram,"Portrait mode"); break;
506 case 8: strcpy(m_exifinfo->ExposureProgram,"Landscape mode"); break;
507 default: strcpy(m_exifinfo->ExposureProgram,"reserved"); break;
510 case TAG_ISO_EQUIVALENT:
511 m_exifinfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format);
512 if ( m_exifinfo->ISOequivalent < 50 ) m_exifinfo->ISOequivalent *= 200;
514 case TAG_COMPRESSION_LEVEL:
515 m_exifinfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format);
517 case TAG_XRESOLUTION:
518 m_exifinfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format);
520 case TAG_YRESOLUTION:
521 m_exifinfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format);
523 case TAG_THUMBNAIL_OFFSET:
524 ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format);
526 case TAG_THUMBNAIL_LENGTH:
527 ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format);
531 if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET)
533 unsigned char * SubdirStart;
534 SubdirStart = OffsetBase + Get32u(ValuePtr);
535 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
537 strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
539 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
545 unsigned char * SubdirStart;
547 Offset = Get16u(DirStart+2+12*NumDirEntries);
550 SubdirStart = OffsetBase + Offset;
551 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
553 strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
555 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
558 if (ThumbnailSize && ThumbnailOffset && m_exifinfo->Thumnailstate)
560 if (ThumbnailSize + ThumbnailOffset <= ExifLength)
562 if(FILE *tf = fopen(THUMBNAILTMPFILE, "w"))
564 fwrite( OffsetBase + ThumbnailOffset, ThumbnailSize, 1, tf);
566 m_exifinfo->Thumnailstate = 2;
574 double Cexif::ConvertAnyFormat(void * ValuePtr, int Format)
580 case FMT_SBYTE: Value = *(signed char *)ValuePtr; break;
581 case FMT_BYTE: Value = *(unsigned char *)ValuePtr; break;
582 case FMT_USHORT: Value = Get16u(ValuePtr); break;
583 case FMT_ULONG: Value = Get32u(ValuePtr); break;
587 int Num = Get32s(ValuePtr);
588 int Den = Get32s(4+(char *)ValuePtr);
589 if (Den == 0) Value = 0;
590 else Value = (double)Num/Den;
593 case FMT_SSHORT: Value = (signed short)Get16u(ValuePtr); break;
594 case FMT_SLONG: Value = Get32s(ValuePtr); break;
595 case FMT_SINGLE: Value = (double)*(float *)ValuePtr; break;
596 case FMT_DOUBLE: Value = *(double *)ValuePtr; break;
601 void Cexif::process_COM (const unsigned char * Data, int length)
604 char Comment[MAX_COMMENT+1];
607 if (length > MAX_COMMENT) length = MAX_COMMENT;
609 for (a=2;a<length;a++)
612 if (ch == '\r' && Data[a+1] == '\n') continue;
613 if ((ch>=0x20) || ch == '\n' || ch == '\t') Comment[nch++] = (char)ch;
614 else Comment[nch++] = '?';
617 strcpy(m_exifinfo->Comments,Comment);
620 void Cexif::process_SOFn (const unsigned char * Data, int marker)
622 int data_precision, num_components;
624 data_precision = Data[2];
625 m_exifinfo->Height = Get16m((void*)(Data+3));
626 m_exifinfo->Width = Get16m((void*)(Data+5));
627 num_components = Data[7];
629 if (num_components == 3) strcpy(m_exifinfo->IsColor,"yes");
630 else strcpy(m_exifinfo->IsColor,"no");
632 m_exifinfo->Process = marker;