3 * Purpose: EXIF reader
\r
4 * 18/Aug/2002 Davide Pizzolato - www.xdp.it
\r
5 * CxImage version 6.0.0 02/Feb/2008
\r
6 * based on jhead-1.8 by Matthias Wandel <mwandel(at)rim(dot)net>
\r
11 #if CXIMAGEJPG_SUPPORT_EXIF
\r
13 ////////////////////////////////////////////////////////////////////////////////
\r
14 CxImageJPG::CxExifInfo::CxExifInfo(EXIFINFO* info)
\r
20 m_exifinfo = new EXIFINFO;
\r
21 memset(m_exifinfo,0,sizeof(EXIFINFO));
\r
25 m_szLastError[0]='\0';
\r
26 ExifImageWidth = MotorolaOrder = 0;
\r
28 memset(&Sections, 0, MAX_SECTIONS * sizeof(Section_t));
\r
30 ////////////////////////////////////////////////////////////////////////////////
\r
31 CxImageJPG::CxExifInfo::~CxExifInfo()
\r
33 for(int i=0;i<MAX_SECTIONS;i++) if(Sections[i].Data) free(Sections[i].Data);
\r
34 if (freeinfo) delete m_exifinfo;
\r
36 ////////////////////////////////////////////////////////////////////////////////
\r
37 bool CxImageJPG::CxExifInfo::DecodeExif(CxFile * hFile, int nReadMode)
\r
40 int HaveCom = FALSE;
\r
44 if (a != 0xff || hFile->GetC() != M_SOI){
\r
54 if (SectionsRead >= MAX_SECTIONS){
\r
55 strcpy(m_szLastError,"Too many sections in jpg file");
\r
60 marker = hFile->GetC();
\r
61 if (marker != 0xff) break;
\r
64 printf("too many padding bytes\n");
\r
69 if (marker == 0xff){
\r
70 // 0xff is legal padding, but if we get that many, something's wrong.
\r
71 strcpy(m_szLastError,"too many padding bytes!");
\r
75 Sections[SectionsRead].Type = marker;
\r
77 // Read the length of the section.
\r
81 itemlen = (lh << 8) | ll;
\r
84 strcpy(m_szLastError,"invalid marker");
\r
88 Sections[SectionsRead].Size = itemlen;
\r
90 Data = (BYTE *)malloc(itemlen);
\r
92 strcpy(m_szLastError,"Could not allocate memory");
\r
95 Sections[SectionsRead].Data = Data;
\r
97 // Store first two pre-read bytes.
\r
101 got = hFile->Read(Data+2, 1, itemlen-2); // Read the whole section.
\r
102 if (got != itemlen-2){
\r
103 strcpy(m_szLastError,"Premature end of file?");
\r
110 case M_SOS: // stop before hitting compressed data
\r
111 // If reading entire image is requested, read the rest of the data.
\r
112 if (nReadMode & EXIF_READ_IMAGE){
\r
114 // Determine how much file is left.
\r
115 cp = hFile->Tell();
\r
116 hFile->Seek(0, SEEK_END);
\r
117 ep = hFile->Tell();
\r
118 hFile->Seek(cp, SEEK_SET);
\r
121 Data = (BYTE *)malloc(size);
\r
123 strcpy(m_szLastError,"could not allocate data for entire image");
\r
127 got = hFile->Read(Data, 1, size);
\r
129 strcpy(m_szLastError,"could not read the rest of the image");
\r
133 Sections[SectionsRead].Data = Data;
\r
134 Sections[SectionsRead].Size = size;
\r
135 Sections[SectionsRead].Type = PSEUDO_IMAGE_MARKER;
\r
140 case M_EOI: // in case it's a tables-only JPEG stream
\r
141 printf("No image in jpeg!\n");
\r
144 case M_COM: // Comment section
\r
145 if (HaveCom || ((nReadMode & EXIF_READ_EXIF) == 0)){
\r
146 // Discard this section.
\r
147 free(Sections[--SectionsRead].Data);
\r
148 Sections[SectionsRead].Data=0;
\r
150 process_COM(Data, itemlen);
\r
156 // Regular jpegs always have this tag, exif images have the exif
\r
157 // marker instead, althogh ACDsee will write images with both markers.
\r
158 // this program will re-create this marker on absence of exif marker.
\r
159 // hence no need to keep the copy from the file.
\r
160 free(Sections[--SectionsRead].Data);
\r
161 Sections[SectionsRead].Data=0;
\r
165 // Seen files from some 'U-lead' software with Vivitar scanner
\r
166 // that uses marker 31 for non exif stuff. Thus make sure
\r
167 // it says 'Exif' in the section before treating it as exif.
\r
168 if ((nReadMode & EXIF_READ_EXIF) && memcmp(Data+2, "Exif", 4) == 0){
\r
169 m_exifinfo->IsExif = process_EXIF((BYTE *)Data+2, itemlen);
\r
171 // Discard this section.
\r
172 free(Sections[--SectionsRead].Data);
\r
173 Sections[SectionsRead].Data=0;
\r
190 process_SOFn(Data, marker);
\r
193 // Skip any other sections.
\r
194 //if (ShowTags) printf("Jpeg section marker 0x%02x size %d\n",marker, itemlen);
\r
200 ////////////////////////////////////////////////////////////////////////////////
\r
201 /*--------------------------------------------------------------------------
\r
202 Process a EXIF marker
\r
203 Describes all the drivel that most digital cameras include...
\r
204 --------------------------------------------------------------------------*/
\r
205 bool CxImageJPG::CxExifInfo::process_EXIF(unsigned char * CharBuf, unsigned int length)
\r
207 m_exifinfo->FlashUsed = 0;
\r
208 /* If it's from a digicam, and it used flash, it says so. */
\r
209 m_exifinfo->Comments[0] = '\0'; /* Initial value - null string */
\r
211 ExifImageWidth = 0;
\r
213 { /* Check the EXIF header component */
\r
214 static const unsigned char ExifHeader[] = "Exif\0\0";
\r
215 if (memcmp(CharBuf+0, ExifHeader,6)){
\r
216 strcpy(m_szLastError,"Incorrect Exif header");
\r
221 if (memcmp(CharBuf+6,"II",2) == 0){
\r
224 if (memcmp(CharBuf+6,"MM",2) == 0){
\r
227 strcpy(m_szLastError,"Invalid Exif alignment marker.");
\r
232 /* Check the next two values for correctness. */
\r
233 if (Get16u(CharBuf+8) != 0x2a){
\r
234 strcpy(m_szLastError,"Invalid Exif start (1)");
\r
238 int FirstOffset = Get32u(CharBuf+10);
\r
239 /* <Richard Collins>
\r
240 if (FirstOffset < 8 || FirstOffset > 16){
\r
241 // I used to ensure this was set to 8 (website I used indicated its 8)
\r
242 // but PENTAX Optio 230 has it set differently, and uses it as offset. (Sept 11 2002)
\r
243 strcpy(m_szLastError,"Suspicious offset of first IFD value");
\r
247 unsigned char * LastExifRefd = CharBuf;
\r
249 /* First directory starts 16 bytes in. Offsets start at 8 bytes in. */
\r
250 if (!ProcessExifDir(CharBuf+14, CharBuf+6, length-6, m_exifinfo, &LastExifRefd))
\r
253 /* <Richard Collins> give a chance for a second directory */
\r
254 if (FirstOffset > 8) {
\r
255 if (!ProcessExifDir(CharBuf+14+FirstOffset-8, CharBuf+6, length-6, m_exifinfo, &LastExifRefd))
\r
259 /* This is how far the interesting (non thumbnail) part of the exif went. */
\r
260 // int ExifSettingsLength = LastExifRefd - CharBuf;
\r
262 /* Compute the CCD width, in milimeters. */
\r
263 if (m_exifinfo->FocalplaneXRes != 0){
\r
264 m_exifinfo->CCDWidth = (float)(ExifImageWidth * m_exifinfo->FocalplaneUnits / m_exifinfo->FocalplaneXRes);
\r
269 //--------------------------------------------------------------------------
\r
270 // Get 16 bits motorola order (always) for jpeg header stuff.
\r
271 //--------------------------------------------------------------------------
\r
272 int CxImageJPG::CxExifInfo::Get16m(void * Short)
\r
274 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
\r
276 ////////////////////////////////////////////////////////////////////////////////
\r
277 /*--------------------------------------------------------------------------
\r
278 Convert a 16 bit unsigned value from file's native byte order
\r
279 --------------------------------------------------------------------------*/
\r
280 int CxImageJPG::CxExifInfo::Get16u(void * Short)
\r
282 if (MotorolaOrder){
\r
283 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
\r
285 return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
\r
288 ////////////////////////////////////////////////////////////////////////////////
\r
289 /*--------------------------------------------------------------------------
\r
290 Convert a 32 bit signed value from file's native byte order
\r
291 --------------------------------------------------------------------------*/
\r
292 long CxImageJPG::CxExifInfo::Get32s(void * Long)
\r
294 if (MotorolaOrder){
\r
295 return ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16)
\r
296 | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
\r
298 return ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16)
\r
299 | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
\r
302 ////////////////////////////////////////////////////////////////////////////////
\r
303 /*--------------------------------------------------------------------------
\r
304 Convert a 32 bit unsigned value from file's native byte order
\r
305 --------------------------------------------------------------------------*/
\r
306 unsigned long CxImageJPG::CxExifInfo::Get32u(void * Long)
\r
308 return (unsigned long)Get32s(Long) & 0xffffffff;
\r
310 ////////////////////////////////////////////////////////////////////////////////
\r
312 /* Describes format descriptor */
\r
313 static const int BytesPerFormat[] = {0,1,1,2,4,8,1,1,2,4,8,4,8};
\r
314 #define NUM_FORMATS 12
\r
316 #define FMT_BYTE 1
\r
317 #define FMT_STRING 2
\r
318 #define FMT_USHORT 3
\r
319 #define FMT_ULONG 4
\r
320 #define FMT_URATIONAL 5
\r
321 #define FMT_SBYTE 6
\r
322 #define FMT_UNDEFINED 7
\r
323 #define FMT_SSHORT 8
\r
324 #define FMT_SLONG 9
\r
325 #define FMT_SRATIONAL 10
\r
326 #define FMT_SINGLE 11
\r
327 #define FMT_DOUBLE 12
\r
329 /* Describes tag values */
\r
331 #define TAG_EXIF_VERSION 0x9000
\r
332 #define TAG_EXIF_OFFSET 0x8769
\r
333 #define TAG_INTEROP_OFFSET 0xa005
\r
335 #define TAG_MAKE 0x010F
\r
336 #define TAG_MODEL 0x0110
\r
338 #define TAG_ORIENTATION 0x0112
\r
339 #define TAG_XRESOLUTION 0x011A
\r
340 #define TAG_YRESOLUTION 0x011B
\r
341 #define TAG_RESOLUTIONUNIT 0x0128
\r
343 #define TAG_EXPOSURETIME 0x829A
\r
344 #define TAG_FNUMBER 0x829D
\r
346 #define TAG_SHUTTERSPEED 0x9201
\r
347 #define TAG_APERTURE 0x9202
\r
348 #define TAG_BRIGHTNESS 0x9203
\r
349 #define TAG_MAXAPERTURE 0x9205
\r
350 #define TAG_FOCALLENGTH 0x920A
\r
352 #define TAG_DATETIME_ORIGINAL 0x9003
\r
353 #define TAG_USERCOMMENT 0x9286
\r
355 #define TAG_SUBJECT_DISTANCE 0x9206
\r
356 #define TAG_FLASH 0x9209
\r
358 #define TAG_FOCALPLANEXRES 0xa20E
\r
359 #define TAG_FOCALPLANEYRES 0xa20F
\r
360 #define TAG_FOCALPLANEUNITS 0xa210
\r
361 #define TAG_EXIF_IMAGEWIDTH 0xA002
\r
362 #define TAG_EXIF_IMAGELENGTH 0xA003
\r
364 /* the following is added 05-jan-2001 vcs */
\r
365 #define TAG_EXPOSURE_BIAS 0x9204
\r
366 #define TAG_WHITEBALANCE 0x9208
\r
367 #define TAG_METERING_MODE 0x9207
\r
368 #define TAG_EXPOSURE_PROGRAM 0x8822
\r
369 #define TAG_ISO_EQUIVALENT 0x8827
\r
370 #define TAG_COMPRESSION_LEVEL 0x9102
\r
372 #define TAG_THUMBNAIL_OFFSET 0x0201
\r
373 #define TAG_THUMBNAIL_LENGTH 0x0202
\r
376 /*--------------------------------------------------------------------------
\r
377 Process one of the nested EXIF directories.
\r
378 --------------------------------------------------------------------------*/
\r
379 bool CxImageJPG::CxExifInfo::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength,
\r
380 EXIFINFO * const m_exifinfo, unsigned char ** const LastExifRefdP, int NestingLevel)
\r
385 unsigned ThumbnailOffset = 0;
\r
386 unsigned ThumbnailSize = 0;
\r
388 if (NestingLevel > 4){
\r
389 strcpy(m_szLastError,"Maximum directory nesting exceeded (corrupt exif header)");
\r
393 NumDirEntries = Get16u(DirStart);
\r
395 if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength)){
\r
396 strcpy(m_szLastError,"Illegally sized directory");
\r
400 for (de=0;de<NumDirEntries;de++){
\r
401 int Tag, Format, Components;
\r
402 unsigned char * ValuePtr;
\r
403 /* This actually can point to a variety of things; it must be
\r
404 cast to other types when used. But we use it as a byte-by-byte
\r
405 cursor, so we declare it as a pointer to a generic byte here.
\r
408 unsigned char * DirEntry;
\r
409 DirEntry = DirStart+2+12*de;
\r
411 Tag = Get16u(DirEntry);
\r
412 Format = Get16u(DirEntry+2);
\r
413 Components = Get32u(DirEntry+4);
\r
415 if ((Format-1) >= NUM_FORMATS) {
\r
416 /* (-1) catches illegal zero case as unsigned underflows to positive large */
\r
417 strcpy(m_szLastError,"Illegal format code in EXIF dir");
\r
421 ByteCount = Components * BytesPerFormat[Format];
\r
423 if (ByteCount > 4){
\r
424 unsigned OffsetVal;
\r
425 OffsetVal = Get32u(DirEntry+8);
\r
426 /* If its bigger than 4 bytes, the dir entry contains an offset.*/
\r
427 if (OffsetVal+ByteCount > ExifLength){
\r
428 /* Bogus pointer offset and / or bytecount value */
\r
429 strcpy(m_szLastError,"Illegal pointer offset value in EXIF.");
\r
432 ValuePtr = OffsetBase+OffsetVal;
\r
434 /* 4 bytes or less and value is in the dir entry itself */
\r
435 ValuePtr = DirEntry+8;
\r
438 if (*LastExifRefdP < ValuePtr+ByteCount){
\r
439 /* Keep track of last byte in the exif header that was
\r
440 actually referenced. That way, we know where the
\r
441 discardable thumbnail data begins.
\r
443 *LastExifRefdP = ValuePtr+ByteCount;
\r
446 /* Extract useful components of tag */
\r
450 strncpy(m_exifinfo->CameraMake, (char*)ValuePtr, 31);
\r
454 strncpy(m_exifinfo->CameraModel, (char*)ValuePtr, 39);
\r
457 case TAG_EXIF_VERSION:
\r
458 strncpy(m_exifinfo->Version,(char*)ValuePtr, 4);
\r
461 case TAG_DATETIME_ORIGINAL:
\r
462 strncpy(m_exifinfo->DateTime, (char*)ValuePtr, 19);
\r
465 case TAG_USERCOMMENT:
\r
466 // Olympus has this padded with trailing spaces. Remove these first.
\r
467 for (a=ByteCount;;){
\r
469 if (((char*)ValuePtr)[a] == ' '){
\r
470 ((char*)ValuePtr)[a] = '\0';
\r
477 /* Copy the comment */
\r
478 if (memcmp(ValuePtr, "ASCII",5) == 0){
\r
479 for (a=5;a<10;a++){
\r
481 c = ((char*)ValuePtr)[a];
\r
482 if (c != '\0' && c != ' '){
\r
483 strncpy(m_exifinfo->Comments, (char*)ValuePtr+a, 199);
\r
489 strncpy(m_exifinfo->Comments, (char*)ValuePtr, 199);
\r
494 /* Simplest way of expressing aperture, so I trust it the most.
\r
495 (overwrite previously computd value if there is one)
\r
497 m_exifinfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format);
\r
501 case TAG_MAXAPERTURE:
\r
502 /* More relevant info always comes earlier, so only
\r
503 use this field if we don't have appropriate aperture
\r
506 if (m_exifinfo->ApertureFNumber == 0){
\r
507 m_exifinfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0f)*0.5);
\r
511 case TAG_BRIGHTNESS:
\r
512 m_exifinfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format);
\r
515 case TAG_FOCALLENGTH:
\r
516 /* Nice digital cameras actually save the focal length
\r
517 as a function of how farthey are zoomed in.
\r
520 m_exifinfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format);
\r
523 case TAG_SUBJECT_DISTANCE:
\r
524 /* Inidcates the distacne the autofocus camera is focused to.
\r
525 Tends to be less accurate as distance increases.
\r
527 m_exifinfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format);
\r
530 case TAG_EXPOSURETIME:
\r
531 /* Simplest way of expressing exposure time, so I
\r
532 trust it most. (overwrite previously computd value
\r
535 m_exifinfo->ExposureTime =
\r
536 (float)ConvertAnyFormat(ValuePtr, Format);
\r
539 case TAG_SHUTTERSPEED:
\r
540 /* More complicated way of expressing exposure time,
\r
541 so only use this value if we don't already have it
\r
542 from somewhere else.
\r
544 if (m_exifinfo->ExposureTime == 0){
\r
545 m_exifinfo->ExposureTime = (float)
\r
546 (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0f)));
\r
551 if ((int)ConvertAnyFormat(ValuePtr, Format) & 7){
\r
552 m_exifinfo->FlashUsed = 1;
\r
554 m_exifinfo->FlashUsed = 0;
\r
558 case TAG_ORIENTATION:
\r
559 m_exifinfo->Orientation = (int)ConvertAnyFormat(ValuePtr, Format);
\r
560 if (m_exifinfo->Orientation < 1 || m_exifinfo->Orientation > 8){
\r
561 strcpy(m_szLastError,"Undefined rotation value");
\r
562 m_exifinfo->Orientation = 0;
\r
566 case TAG_EXIF_IMAGELENGTH:
\r
567 case TAG_EXIF_IMAGEWIDTH:
\r
568 /* Use largest of height and width to deal with images
\r
569 that have been rotated to portrait format.
\r
571 a = (int)ConvertAnyFormat(ValuePtr, Format);
\r
572 if (ExifImageWidth < a) ExifImageWidth = a;
\r
575 case TAG_FOCALPLANEXRES:
\r
576 m_exifinfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format);
\r
579 case TAG_FOCALPLANEYRES:
\r
580 m_exifinfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format);
\r
583 case TAG_RESOLUTIONUNIT:
\r
584 switch((int)ConvertAnyFormat(ValuePtr, Format)){
\r
585 case 1: m_exifinfo->ResolutionUnit = 1.0f; break; /* 1 inch */
\r
586 case 2: m_exifinfo->ResolutionUnit = 1.0f; break;
\r
587 case 3: m_exifinfo->ResolutionUnit = 0.3937007874f; break; /* 1 centimeter*/
\r
588 case 4: m_exifinfo->ResolutionUnit = 0.03937007874f; break; /* 1 millimeter*/
\r
589 case 5: m_exifinfo->ResolutionUnit = 0.00003937007874f; /* 1 micrometer*/
\r
593 case TAG_FOCALPLANEUNITS:
\r
594 switch((int)ConvertAnyFormat(ValuePtr, Format)){
\r
595 case 1: m_exifinfo->FocalplaneUnits = 1.0f; break; /* 1 inch */
\r
596 case 2: m_exifinfo->FocalplaneUnits = 1.0f; break;
\r
597 case 3: m_exifinfo->FocalplaneUnits = 0.3937007874f; break; /* 1 centimeter*/
\r
598 case 4: m_exifinfo->FocalplaneUnits = 0.03937007874f; break; /* 1 millimeter*/
\r
599 case 5: m_exifinfo->FocalplaneUnits = 0.00003937007874f; /* 1 micrometer*/
\r
603 // Remaining cases contributed by: Volker C. Schoech <schoech(at)gmx(dot)de>
\r
605 case TAG_EXPOSURE_BIAS:
\r
606 m_exifinfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format);
\r
609 case TAG_WHITEBALANCE:
\r
610 m_exifinfo->Whitebalance = (int)ConvertAnyFormat(ValuePtr, Format);
\r
613 case TAG_METERING_MODE:
\r
614 m_exifinfo->MeteringMode = (int)ConvertAnyFormat(ValuePtr, Format);
\r
617 case TAG_EXPOSURE_PROGRAM:
\r
618 m_exifinfo->ExposureProgram = (int)ConvertAnyFormat(ValuePtr, Format);
\r
621 case TAG_ISO_EQUIVALENT:
\r
622 m_exifinfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format);
\r
623 if ( m_exifinfo->ISOequivalent < 50 ) m_exifinfo->ISOequivalent *= 200;
\r
626 case TAG_COMPRESSION_LEVEL:
\r
627 m_exifinfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format);
\r
630 case TAG_XRESOLUTION:
\r
631 m_exifinfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format);
\r
633 case TAG_YRESOLUTION:
\r
634 m_exifinfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format);
\r
637 case TAG_THUMBNAIL_OFFSET:
\r
638 ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format);
\r
641 case TAG_THUMBNAIL_LENGTH:
\r
642 ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format);
\r
647 if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET){
\r
648 unsigned char * SubdirStart;
\r
649 unsigned Offset = Get32u(ValuePtr);
\r
651 SubdirStart = OffsetBase + Offset;
\r
652 if (SubdirStart < OffsetBase ||
\r
653 SubdirStart > OffsetBase+ExifLength){
\r
654 strcpy(m_szLastError,"Illegal subdirectory link");
\r
657 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP, NestingLevel+1);
\r
665 /* In addition to linking to subdirectories via exif tags,
\r
666 there's also a potential link to another directory at the end
\r
667 of each directory. This has got to be the result of a
\r
670 unsigned char * SubdirStart;
\r
672 Offset = Get16u(DirStart+2+12*NumDirEntries);
\r
674 SubdirStart = OffsetBase + Offset;
\r
675 if (SubdirStart < OffsetBase
\r
676 || SubdirStart > OffsetBase+ExifLength){
\r
677 strcpy(m_szLastError,"Illegal subdirectory link");
\r
680 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP, NestingLevel+1);
\r
685 if (ThumbnailSize && ThumbnailOffset){
\r
686 if (ThumbnailSize + ThumbnailOffset <= ExifLength){
\r
687 /* The thumbnail pointer appears to be valid. Store it. */
\r
688 m_exifinfo->ThumbnailPointer = OffsetBase + ThumbnailOffset;
\r
689 m_exifinfo->ThumbnailSize = ThumbnailSize;
\r
695 ////////////////////////////////////////////////////////////////////////////////
\r
696 /*--------------------------------------------------------------------------
\r
697 Evaluate number, be it int, rational, or float from directory.
\r
698 --------------------------------------------------------------------------*/
\r
699 double CxImageJPG::CxExifInfo::ConvertAnyFormat(void * ValuePtr, int Format)
\r
705 case FMT_SBYTE: Value = *(signed char *)ValuePtr; break;
\r
706 case FMT_BYTE: Value = *(unsigned char *)ValuePtr; break;
\r
708 case FMT_USHORT: Value = Get16u(ValuePtr); break;
\r
709 case FMT_ULONG: Value = Get32u(ValuePtr); break;
\r
711 case FMT_URATIONAL:
\r
712 case FMT_SRATIONAL:
\r
715 Num = Get32s(ValuePtr);
\r
716 Den = Get32s(4+(char *)ValuePtr);
\r
720 Value = (double)Num/Den;
\r
725 case FMT_SSHORT: Value = (signed short)Get16u(ValuePtr); break;
\r
726 case FMT_SLONG: Value = Get32s(ValuePtr); break;
\r
728 /* Not sure if this is correct (never seen float used in Exif format)
\r
730 case FMT_SINGLE: Value = (double)*(float *)ValuePtr; break;
\r
731 case FMT_DOUBLE: Value = *(double *)ValuePtr; break;
\r
735 ////////////////////////////////////////////////////////////////////////////////
\r
736 void CxImageJPG::CxExifInfo::process_COM (const BYTE * Data, int length)
\r
739 char Comment[MAX_COMMENT+1];
\r
745 if (length > MAX_COMMENT) length = MAX_COMMENT; // Truncate if it won't fit in our structure.
\r
747 for (a=2;a<length;a++){
\r
750 if (ch == '\r' && Data[a+1] == '\n') continue; // Remove cr followed by lf.
\r
752 if (isprint(ch) || ch == '\n' || ch == '\t'){
\r
753 Comment[nch++] = (char)ch;
\r
755 Comment[nch++] = '?';
\r
759 Comment[nch] = '\0'; // Null terminate
\r
761 //if (ShowTags) printf("COM marker comment: %s\n",Comment);
\r
763 strcpy(m_exifinfo->Comments,Comment);
\r
765 ////////////////////////////////////////////////////////////////////////////////
\r
766 void CxImageJPG::CxExifInfo::process_SOFn (const BYTE * Data, int marker)
\r
768 int data_precision, num_components;
\r
770 data_precision = Data[2];
\r
771 m_exifinfo->Height = Get16m((void*)(Data+3));
\r
772 m_exifinfo->Width = Get16m((void*)(Data+5));
\r
773 num_components = Data[7];
\r
775 if (num_components == 3){
\r
776 m_exifinfo->IsColor = 1;
\r
778 m_exifinfo->IsColor = 0;
\r
781 m_exifinfo->Process = marker;
\r
783 //if (ShowTags) printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
\r
784 // ImageInfo.Width, ImageInfo.Height, num_components, data_precision);
\r
786 ////////////////////////////////////////////////////////////////////////////////
\r
788 * this will work only on a CxImageJPG object, if the image originally has valid EXIF data
\r
792 in.Open("D:\\exif_in.jpg","rb");
\r
793 out.Open("D:\\exif_out.jpg","w+b");
\r
795 if (jpg.IsValid()){
\r
801 bool CxImageJPG::CxExifInfo::EncodeExif(CxFile * hFile)
\r
805 if (FindSection(M_SOS)==NULL){
\r
806 strcpy(m_szLastError,"Can't write exif : didn't read all");
\r
810 // Initial static jpeg marker.
\r
814 if (Sections[0].Type != M_EXIF && Sections[0].Type != M_JFIF){
\r
815 // The image must start with an exif or jfif marker. If we threw those away, create one.
\r
816 static BYTE JfifHead[18] = {
\r
818 0x00, 0x10, 'J' , 'F' , 'I' , 'F' , 0x00, 0x01,
\r
819 0x01, 0x01, 0x01, 0x2C, 0x01, 0x2C, 0x00, 0x00
\r
821 hFile->Write(JfifHead, 18, 1);
\r
824 // Write all the misc sections
\r
825 for (a=0;a<SectionsRead-1;a++){
\r
827 hFile->PutC((unsigned char)(Sections[a].Type));
\r
828 hFile->Write(Sections[a].Data, Sections[a].Size, 1);
\r
831 // Write the remaining image data.
\r
832 hFile->Write(Sections[a].Data, Sections[a].Size, 1);
\r
836 ////////////////////////////////////////////////////////////////////////////////
\r
837 void CxImageJPG::CxExifInfo::DiscardAllButExif()
\r
839 Section_t ExifKeeper;
\r
840 Section_t CommentKeeper;
\r
843 memset(&ExifKeeper, 0, sizeof(ExifKeeper));
\r
844 memset(&CommentKeeper, 0, sizeof(ExifKeeper));
\r
846 for (a=0;a<SectionsRead;a++){
\r
847 if (Sections[a].Type == M_EXIF && ExifKeeper.Type == 0){
\r
848 ExifKeeper = Sections[a];
\r
849 }else if (Sections[a].Type == M_COM && CommentKeeper.Type == 0){
\r
850 CommentKeeper = Sections[a];
\r
852 free(Sections[a].Data);
\r
853 Sections[a].Data = 0;
\r
857 if (ExifKeeper.Type){
\r
858 Sections[SectionsRead++] = ExifKeeper;
\r
860 if (CommentKeeper.Type){
\r
861 Sections[SectionsRead++] = CommentKeeper;
\r
864 ////////////////////////////////////////////////////////////////////////////////
\r
865 void* CxImageJPG::CxExifInfo::FindSection(int SectionType)
\r
868 for (a=0;a<SectionsRead-1;a++){
\r
869 if (Sections[a].Type == SectionType){
\r
870 return &Sections[a];
\r
873 // Could not be found.
\r
876 ////////////////////////////////////////////////////////////////////////////////
\r
877 #endif // CXIMAGEJPG_SUPPORT_EXIF
\r