]> Creatis software - clitk.git/blob - utilities/CxImage/ximaexif.cpp
8d9404b8ad67fe052509e2d2412be68aa9cec3e1
[clitk.git] / utilities / CxImage / ximaexif.cpp
1 /*\r
2  * File:        ximaexif.cpp\r
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
7  */\r
8 \r
9 #include "ximajpg.h"\r
10 \r
11 #if CXIMAGEJPG_SUPPORT_EXIF\r
12 \r
13 ////////////////////////////////////////////////////////////////////////////////\r
14 CxImageJPG::CxExifInfo::CxExifInfo(EXIFINFO* info)\r
15 {\r
16         if (info) {\r
17                 m_exifinfo = info;\r
18                 freeinfo = false;\r
19         } else {\r
20                 m_exifinfo = new EXIFINFO;\r
21                 memset(m_exifinfo,0,sizeof(EXIFINFO));\r
22                 freeinfo = true;\r
23         }\r
24 \r
25         m_szLastError[0]='\0';\r
26         ExifImageWidth = MotorolaOrder = 0;\r
27         SectionsRead=0;\r
28         memset(&Sections, 0, MAX_SECTIONS * sizeof(Section_t));\r
29 }\r
30 ////////////////////////////////////////////////////////////////////////////////\r
31 CxImageJPG::CxExifInfo::~CxExifInfo()\r
32 {\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
35 }\r
36 ////////////////////////////////////////////////////////////////////////////////\r
37 bool CxImageJPG::CxExifInfo::DecodeExif(CxFile * hFile, int nReadMode)\r
38 {\r
39     int a;\r
40     int HaveCom = FALSE;\r
41 \r
42     a = hFile->GetC();\r
43 \r
44     if (a != 0xff || hFile->GetC() != M_SOI){\r
45         return FALSE;\r
46     }\r
47 \r
48     for(;;){\r
49         int itemlen;\r
50         int marker = 0;\r
51         int ll,lh, got;\r
52         BYTE * Data;\r
53 \r
54         if (SectionsRead >= MAX_SECTIONS){\r
55                         strcpy(m_szLastError,"Too many sections in jpg file");\r
56                         return false;\r
57         }\r
58 \r
59         for (a=0;a<7;a++){\r
60             marker = hFile->GetC();\r
61             if (marker != 0xff) break;\r
62 \r
63             if (a >= 6){\r
64                 printf("too many padding bytes\n");\r
65                 return false;\r
66             }\r
67         }\r
68 \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
72                         return false;\r
73         }\r
74 \r
75         Sections[SectionsRead].Type = marker;\r
76 \r
77         // Read the length of the section.\r
78         lh = hFile->GetC();\r
79         ll = hFile->GetC();\r
80 \r
81         itemlen = (lh << 8) | ll;\r
82 \r
83         if (itemlen < 2){\r
84             strcpy(m_szLastError,"invalid marker");\r
85                         return false;\r
86         }\r
87 \r
88         Sections[SectionsRead].Size = itemlen;\r
89 \r
90         Data = (BYTE *)malloc(itemlen);\r
91         if (Data == NULL){\r
92             strcpy(m_szLastError,"Could not allocate memory");\r
93                         return false;\r
94         }\r
95         Sections[SectionsRead].Data = Data;\r
96 \r
97         // Store first two pre-read bytes.\r
98         Data[0] = (BYTE)lh;\r
99         Data[1] = (BYTE)ll;\r
100 \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
104                         return false;\r
105         }\r
106         SectionsRead += 1;\r
107 \r
108         switch(marker){\r
109 \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
113                     int cp, ep, size;\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
119 \r
120                     size = ep-cp;\r
121                     Data = (BYTE *)malloc(size);\r
122                     if (Data == NULL){\r
123                         strcpy(m_szLastError,"could not allocate data for entire image");\r
124                                                 return false;\r
125                     }\r
126 \r
127                     got = hFile->Read(Data, 1, size);\r
128                     if (got != size){\r
129                         strcpy(m_szLastError,"could not read the rest of the image");\r
130                                                 return false;\r
131                     }\r
132 \r
133                     Sections[SectionsRead].Data = Data;\r
134                     Sections[SectionsRead].Size = size;\r
135                     Sections[SectionsRead].Type = PSEUDO_IMAGE_MARKER;\r
136                     SectionsRead ++;\r
137                 }\r
138                 return true;\r
139 \r
140             case M_EOI:   // in case it's a tables-only JPEG stream\r
141                 printf("No image in jpeg!\n");\r
142                 return FALSE;\r
143 \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
149                 }else{\r
150                     process_COM(Data, itemlen);\r
151                     HaveCom = TRUE;\r
152                 }\r
153                 break;\r
154 \r
155             case M_JFIF:\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
162                 break;\r
163 \r
164             case M_EXIF:\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
170                 }else{\r
171                     // Discard this section.\r
172                     free(Sections[--SectionsRead].Data);\r
173                                         Sections[SectionsRead].Data=0;\r
174                 }\r
175                 break;\r
176 \r
177             case M_SOF0: \r
178             case M_SOF1: \r
179             case M_SOF2: \r
180             case M_SOF3: \r
181             case M_SOF5: \r
182             case M_SOF6: \r
183             case M_SOF7: \r
184             case M_SOF9: \r
185             case M_SOF10:\r
186             case M_SOF11:\r
187             case M_SOF13:\r
188             case M_SOF14:\r
189             case M_SOF15:\r
190                 process_SOFn(Data, marker);\r
191                 break;\r
192             default:\r
193                 // Skip any other sections.\r
194                 //if (ShowTags) printf("Jpeg section marker 0x%02x size %d\n",marker, itemlen);\r
195                 break;\r
196         }\r
197     }\r
198         return true;\r
199 }\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
206 {\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
210 \r
211     ExifImageWidth = 0;\r
212 \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
217                         return false;\r
218                 }\r
219     }\r
220 \r
221     if (memcmp(CharBuf+6,"II",2) == 0){\r
222         MotorolaOrder = 0;\r
223     }else{\r
224         if (memcmp(CharBuf+6,"MM",2) == 0){\r
225             MotorolaOrder = 1;\r
226         }else{\r
227             strcpy(m_szLastError,"Invalid Exif alignment marker.");\r
228                         return false;\r
229         }\r
230     }\r
231 \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
235                 return false;\r
236     }\r
237 \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
244                 return false;\r
245     }*/\r
246 \r
247     unsigned char * LastExifRefd = CharBuf;\r
248 \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
251                 return false;\r
252 \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
256                         return false;\r
257         }\r
258 \r
259     /* This is how far the interesting (non thumbnail) part of the exif went. */\r
260     // int ExifSettingsLength = LastExifRefd - CharBuf;\r
261 \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
265     }\r
266 \r
267         return true;\r
268 }\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
273 {\r
274     return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];\r
275 }\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
281 {\r
282     if (MotorolaOrder){\r
283         return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];\r
284     }else{\r
285         return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];\r
286     }\r
287 }\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
293 {\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
297     }else{\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
300     }\r
301 }\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
307 {\r
308     return (unsigned long)Get32s(Long) & 0xffffffff;\r
309 }\r
310 ////////////////////////////////////////////////////////////////////////////////\r
311 \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
315 \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
328 \r
329 /* Describes tag values */\r
330 \r
331 #define TAG_EXIF_VERSION      0x9000\r
332 #define TAG_EXIF_OFFSET       0x8769\r
333 #define TAG_INTEROP_OFFSET    0xa005\r
334 \r
335 #define TAG_MAKE              0x010F\r
336 #define TAG_MODEL             0x0110\r
337 \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
342 \r
343 #define TAG_EXPOSURETIME      0x829A\r
344 #define TAG_FNUMBER           0x829D\r
345 \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
351 \r
352 #define TAG_DATETIME_ORIGINAL 0x9003\r
353 #define TAG_USERCOMMENT       0x9286\r
354 \r
355 #define TAG_SUBJECT_DISTANCE  0x9206\r
356 #define TAG_FLASH             0x9209\r
357 \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
363 \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
371 \r
372 #define TAG_THUMBNAIL_OFFSET  0x0201\r
373 #define TAG_THUMBNAIL_LENGTH  0x0202\r
374 \r
375 \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
381 {\r
382     int de;\r
383     int a;\r
384     int NumDirEntries;\r
385     unsigned ThumbnailOffset = 0;\r
386     unsigned ThumbnailSize = 0;\r
387 \r
388     if (NestingLevel > 4){\r
389         strcpy(m_szLastError,"Maximum directory nesting exceeded (corrupt exif header)");\r
390         return false;\r
391     }\r
392 \r
393     NumDirEntries = Get16u(DirStart);\r
394 \r
395     if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength)){\r
396         strcpy(m_szLastError,"Illegally sized directory");\r
397                 return false;\r
398     }\r
399 \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
406             */\r
407         int ByteCount;\r
408         unsigned char * DirEntry;\r
409         DirEntry = DirStart+2+12*de;\r
410 \r
411         Tag = Get16u(DirEntry);\r
412         Format = Get16u(DirEntry+2);\r
413         Components = Get32u(DirEntry+4);\r
414 \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
418                         return false;\r
419                 }\r
420 \r
421         ByteCount = Components * BytesPerFormat[Format];\r
422 \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
430                                 return false;\r
431             }\r
432             ValuePtr = OffsetBase+OffsetVal;\r
433         }else{\r
434             /* 4 bytes or less and value is in the dir entry itself */\r
435             ValuePtr = DirEntry+8;\r
436         }\r
437 \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
442             */\r
443             *LastExifRefdP = ValuePtr+ByteCount;\r
444         }\r
445 \r
446         /* Extract useful components of tag */\r
447         switch(Tag){\r
448 \r
449             case TAG_MAKE:\r
450                 strncpy(m_exifinfo->CameraMake, (char*)ValuePtr, 31);\r
451                 break;\r
452 \r
453             case TAG_MODEL:\r
454                 strncpy(m_exifinfo->CameraModel, (char*)ValuePtr, 39);\r
455                 break;\r
456 \r
457                         case TAG_EXIF_VERSION:\r
458                                 strncpy(m_exifinfo->Version,(char*)ValuePtr, 4);\r
459                                 break;\r
460 \r
461             case TAG_DATETIME_ORIGINAL:\r
462                 strncpy(m_exifinfo->DateTime, (char*)ValuePtr, 19);\r
463                 break;\r
464 \r
465             case TAG_USERCOMMENT:\r
466                 // Olympus has this padded with trailing spaces. Remove these first. \r
467                 for (a=ByteCount;;){\r
468                     a--;\r
469                     if (((char*)ValuePtr)[a] == ' '){\r
470                         ((char*)ValuePtr)[a] = '\0';\r
471                     }else{\r
472                         break;\r
473                     }\r
474                     if (a == 0) break;\r
475                 }\r
476 \r
477                 /* Copy the comment */\r
478                 if (memcmp(ValuePtr, "ASCII",5) == 0){\r
479                     for (a=5;a<10;a++){\r
480                         char c;\r
481                         c = ((char*)ValuePtr)[a];\r
482                         if (c != '\0' && c != ' '){\r
483                             strncpy(m_exifinfo->Comments, (char*)ValuePtr+a, 199);\r
484                             break;\r
485                         }\r
486                     }\r
487                     \r
488                 }else{\r
489                     strncpy(m_exifinfo->Comments, (char*)ValuePtr, 199);\r
490                 }\r
491                 break;\r
492 \r
493             case TAG_FNUMBER:\r
494                 /* Simplest way of expressing aperture, so I trust it the most.\r
495                    (overwrite previously computd value if there is one)\r
496                    */\r
497                 m_exifinfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format);\r
498                 break;\r
499 \r
500             case TAG_APERTURE:\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
504                  information yet. \r
505                 */\r
506                 if (m_exifinfo->ApertureFNumber == 0){\r
507                     m_exifinfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0f)*0.5);\r
508                 }\r
509                 break;\r
510 \r
511                         case TAG_BRIGHTNESS:\r
512                                 m_exifinfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format);\r
513                                 break;\r
514 \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
518                 */\r
519 \r
520                 m_exifinfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format);\r
521                 break;\r
522 \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
526                 */\r
527                 m_exifinfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format);\r
528                 break;\r
529 \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
533                    if there is one) \r
534                 */\r
535                 m_exifinfo->ExposureTime = \r
536                     (float)ConvertAnyFormat(ValuePtr, Format);\r
537                 break;\r
538 \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
543                 */\r
544                 if (m_exifinfo->ExposureTime == 0){\r
545                     m_exifinfo->ExposureTime = (float)\r
546                         (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2.0f)));\r
547                 }\r
548                 break;\r
549 \r
550             case TAG_FLASH:\r
551                 if ((int)ConvertAnyFormat(ValuePtr, Format) & 7){\r
552                     m_exifinfo->FlashUsed = 1;\r
553                 }else{\r
554                     m_exifinfo->FlashUsed = 0;\r
555                 }\r
556                 break;\r
557 \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
563                 }\r
564                 break;\r
565 \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
570                 */\r
571                 a = (int)ConvertAnyFormat(ValuePtr, Format);\r
572                 if (ExifImageWidth < a) ExifImageWidth = a;\r
573                 break;\r
574 \r
575             case TAG_FOCALPLANEXRES:\r
576                 m_exifinfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format);\r
577                 break;\r
578 \r
579             case TAG_FOCALPLANEYRES:\r
580                 m_exifinfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format);\r
581                 break;\r
582 \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
590                 }\r
591                 break;\r
592 \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
600                 }\r
601                 break;\r
602 \r
603                 // Remaining cases contributed by: Volker C. Schoech <schoech(at)gmx(dot)de>\r
604 \r
605             case TAG_EXPOSURE_BIAS:\r
606                 m_exifinfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format);\r
607                 break;\r
608 \r
609             case TAG_WHITEBALANCE:\r
610                 m_exifinfo->Whitebalance = (int)ConvertAnyFormat(ValuePtr, Format);\r
611                 break;\r
612 \r
613             case TAG_METERING_MODE:\r
614                 m_exifinfo->MeteringMode = (int)ConvertAnyFormat(ValuePtr, Format);\r
615                 break;\r
616 \r
617             case TAG_EXPOSURE_PROGRAM:\r
618                 m_exifinfo->ExposureProgram = (int)ConvertAnyFormat(ValuePtr, Format);\r
619                 break;\r
620 \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
624                 break;\r
625 \r
626             case TAG_COMPRESSION_LEVEL:\r
627                 m_exifinfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format);\r
628                 break;\r
629 \r
630             case TAG_XRESOLUTION:\r
631                 m_exifinfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format);\r
632                 break;\r
633             case TAG_YRESOLUTION:\r
634                 m_exifinfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format);\r
635                 break;\r
636 \r
637             case TAG_THUMBNAIL_OFFSET:\r
638                 ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format);\r
639                 break;\r
640 \r
641             case TAG_THUMBNAIL_LENGTH:\r
642                 ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format);\r
643                 break;\r
644 \r
645         }\r
646 \r
647         if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET){\r
648             unsigned char * SubdirStart;\r
649                         unsigned Offset = Get32u(ValuePtr);\r
650                         if (Offset>8){\r
651                                 SubdirStart = OffsetBase + Offset;\r
652                                 if (SubdirStart < OffsetBase || \r
653                                         SubdirStart > OffsetBase+ExifLength){\r
654                                         strcpy(m_szLastError,"Illegal subdirectory link");\r
655                                         return false;\r
656                                 }\r
657                                 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP, NestingLevel+1);\r
658                         }\r
659             continue;\r
660         }\r
661     }\r
662 \r
663 \r
664     {\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
668            committee!  \r
669         */\r
670         unsigned char * SubdirStart;\r
671         unsigned Offset;\r
672         Offset = Get16u(DirStart+2+12*NumDirEntries);\r
673         if (Offset){\r
674             SubdirStart = OffsetBase + Offset;\r
675             if (SubdirStart < OffsetBase \r
676                 || SubdirStart > OffsetBase+ExifLength){\r
677                 strcpy(m_szLastError,"Illegal subdirectory link");\r
678                                 return false;\r
679             }\r
680             ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP, NestingLevel+1);\r
681         }\r
682     }\r
683 \r
684 \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
690         }\r
691     }\r
692 \r
693         return true;\r
694 }\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
700 {\r
701     double Value;\r
702     Value = 0;\r
703 \r
704     switch(Format){\r
705         case FMT_SBYTE:     Value = *(signed char *)ValuePtr;  break;\r
706         case FMT_BYTE:      Value = *(unsigned char *)ValuePtr;        break;\r
707 \r
708         case FMT_USHORT:    Value = Get16u(ValuePtr);          break;\r
709         case FMT_ULONG:     Value = Get32u(ValuePtr);          break;\r
710 \r
711         case FMT_URATIONAL:\r
712         case FMT_SRATIONAL: \r
713             {\r
714                 int Num,Den;\r
715                 Num = Get32s(ValuePtr);\r
716                 Den = Get32s(4+(char *)ValuePtr);\r
717                 if (Den == 0){\r
718                     Value = 0;\r
719                 }else{\r
720                     Value = (double)Num/Den;\r
721                 }\r
722                 break;\r
723             }\r
724 \r
725         case FMT_SSHORT:    Value = (signed short)Get16u(ValuePtr);  break;\r
726         case FMT_SLONG:     Value = Get32s(ValuePtr);                break;\r
727 \r
728         /* Not sure if this is correct (never seen float used in Exif format)\r
729          */\r
730         case FMT_SINGLE:    Value = (double)*(float *)ValuePtr;      break;\r
731         case FMT_DOUBLE:    Value = *(double *)ValuePtr;             break;\r
732     }\r
733     return Value;\r
734 }\r
735 ////////////////////////////////////////////////////////////////////////////////\r
736 void CxImageJPG::CxExifInfo::process_COM (const BYTE * Data, int length)\r
737 {\r
738     int ch;\r
739     char Comment[MAX_COMMENT+1];\r
740     int nch;\r
741     int a;\r
742 \r
743     nch = 0;\r
744 \r
745     if (length > MAX_COMMENT) length = MAX_COMMENT; // Truncate if it won't fit in our structure.\r
746 \r
747     for (a=2;a<length;a++){\r
748         ch = Data[a];\r
749 \r
750         if (ch == '\r' && Data[a+1] == '\n') continue; // Remove cr followed by lf.\r
751 \r
752         if (isprint(ch) || ch == '\n' || ch == '\t'){\r
753             Comment[nch++] = (char)ch;\r
754         }else{\r
755             Comment[nch++] = '?';\r
756         }\r
757     }\r
758 \r
759     Comment[nch] = '\0'; // Null terminate\r
760 \r
761     //if (ShowTags) printf("COM marker comment: %s\n",Comment);\r
762 \r
763     strcpy(m_exifinfo->Comments,Comment);\r
764 }\r
765 ////////////////////////////////////////////////////////////////////////////////\r
766 void CxImageJPG::CxExifInfo::process_SOFn (const BYTE * Data, int marker)\r
767 {\r
768     int data_precision, num_components;\r
769 \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
774 \r
775     if (num_components == 3){\r
776         m_exifinfo->IsColor = 1;\r
777     }else{\r
778         m_exifinfo->IsColor = 0;\r
779     }\r
780 \r
781     m_exifinfo->Process = marker;\r
782 \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
785 }\r
786 ////////////////////////////////////////////////////////////////////////////////\r
787 /**\r
788  * this will work only on a CxImageJPG object, if the image originally has valid EXIF data\r
789  \verbatim\r
790         CxImageJPG jpg;\r
791         CxIOFile in,out;\r
792         in.Open("D:\\exif_in.jpg","rb");\r
793         out.Open("D:\\exif_out.jpg","w+b");\r
794         jpg.Decode(&in);\r
795         if (jpg.IsValid()){\r
796                 jpg.RotateLeft();\r
797                 jpg.Encode(&out);\r
798         }\r
799  \endverbatim\r
800 */\r
801 bool CxImageJPG::CxExifInfo::EncodeExif(CxFile * hFile)\r
802 {\r
803     int a;\r
804 \r
805     if (FindSection(M_SOS)==NULL){\r
806                 strcpy(m_szLastError,"Can't write exif : didn't read all");\r
807                 return false;\r
808     }\r
809 \r
810     // Initial static jpeg marker.\r
811         hFile->PutC(0xff);\r
812         hFile->PutC(0xd8);\r
813     \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
817             0xff, M_JFIF,\r
818             0x00, 0x10, 'J' , 'F' , 'I' , 'F' , 0x00, 0x01, \r
819             0x01, 0x01, 0x01, 0x2C, 0x01, 0x2C, 0x00, 0x00 \r
820         };\r
821         hFile->Write(JfifHead, 18, 1);\r
822     }\r
823 \r
824     // Write all the misc sections\r
825     for (a=0;a<SectionsRead-1;a++){\r
826         hFile->PutC(0xff);\r
827         hFile->PutC((unsigned char)(Sections[a].Type));\r
828         hFile->Write(Sections[a].Data, Sections[a].Size, 1);\r
829     }\r
830 \r
831     // Write the remaining image data.\r
832     hFile->Write(Sections[a].Data, Sections[a].Size, 1);\r
833 \r
834         return true;\r
835 }\r
836 ////////////////////////////////////////////////////////////////////////////////\r
837 void CxImageJPG::CxExifInfo::DiscardAllButExif()\r
838 {\r
839     Section_t ExifKeeper;\r
840     Section_t CommentKeeper;\r
841     int a;\r
842 \r
843     memset(&ExifKeeper, 0, sizeof(ExifKeeper));\r
844     memset(&CommentKeeper, 0, sizeof(ExifKeeper));\r
845 \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
851         }else{\r
852             free(Sections[a].Data);\r
853                         Sections[a].Data = 0;\r
854         }\r
855     }\r
856     SectionsRead = 0;\r
857     if (ExifKeeper.Type){\r
858         Sections[SectionsRead++] = ExifKeeper;\r
859     }\r
860     if (CommentKeeper.Type){\r
861         Sections[SectionsRead++] = CommentKeeper;\r
862     }\r
863 }\r
864 ////////////////////////////////////////////////////////////////////////////////\r
865 void* CxImageJPG::CxExifInfo::FindSection(int SectionType)\r
866 {\r
867     int a;\r
868     for (a=0;a<SectionsRead-1;a++){\r
869         if (Sections[a].Type == SectionType){\r
870             return &Sections[a];\r
871         }\r
872     }\r
873     // Could not be found.\r
874     return NULL;\r
875 }\r
876 ////////////////////////////////////////////////////////////////////////////////\r
877 #endif  // CXIMAGEJPG_SUPPORT_EXIF\r
878 \r