8383ee2ff0f725624659b0f286351fb88357aca5
[enigma2.git] / lib / gdi / picexif.cpp
1 #include "picexif.h"
2
3 #define M_SOF0  0xC0
4 #define M_SOF1  0xC1
5 #define M_SOF2  0xC2
6 #define M_SOF3  0xC3
7 #define M_SOF5  0xC5
8 #define M_SOF6  0xC6
9 #define M_SOF7  0xC7
10 #define M_SOF9  0xC9
11 #define M_SOF10 0xCA
12 #define M_SOF11 0xCB
13 #define M_SOF13 0xCD
14 #define M_SOF14 0xCE
15 #define M_SOF15 0xCF
16 #define M_SOI   0xD8
17 #define M_EOI   0xD9
18 #define M_SOS   0xDA
19 #define M_JFIF  0xE0
20 #define M_EXIF  0xE1
21 #define M_COM   0xFE
22
23 #define NUM_FORMATS   12
24 #define FMT_BYTE       1
25 #define FMT_STRING     2
26 #define FMT_USHORT     3
27 #define FMT_ULONG      4
28 #define FMT_URATIONAL  5
29 #define FMT_SBYTE      6
30 #define FMT_UNDEFINED  7
31 #define FMT_SSHORT     8
32 #define FMT_SLONG      9
33 #define FMT_SRATIONAL 10
34 #define FMT_SINGLE    11
35 #define FMT_DOUBLE    12
36
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
70
71
72 Cexif::Cexif()
73 {
74 }
75
76 Cexif::~Cexif()
77 {
78 }
79
80 void Cexif::ClearExif()
81 {
82         if(freeinfo)
83         {
84                 for(int i=0;i<MAX_SECTIONS;i++)
85                         if(Sections[i].Data) free(Sections[i].Data);
86                                 delete m_exifinfo;
87                 freeinfo = false;
88         }
89 }
90
91 bool Cexif::DecodeExif(const char *filename, int Thumb)
92 {
93         bool ret = false;
94         FILE * hFile = fopen(filename, "r");
95         if(!hFile) return ret;
96
97         m_exifinfo = new EXIFINFO;
98         memset(m_exifinfo,0,sizeof(EXIFINFO));
99         freeinfo = true;
100         m_exifinfo->Thumnailstate = Thumb;
101
102         m_szLastError[0]='\0';
103         ExifImageWidth = MotorolaOrder = SectionsRead=0;
104         memset(&Sections, 0, MAX_SECTIONS * sizeof(Section_t));
105
106         int HaveCom = 0;
107         int a = fgetc(hFile);
108         strcpy(m_szLastError,"EXIF-Data not found");
109
110         if (a != 0xff || fgetc(hFile) != M_SOI)
111                 goto decode_exif_out;
112
113         for(;;)
114         {
115                 int marker = 0;
116                 int ll,lh, got, itemlen;
117                 unsigned char * Data;
118
119                 if (SectionsRead >= MAX_SECTIONS)
120                 {
121                         strcpy(m_szLastError,"Too many sections in jpg file");
122                         goto decode_exif_out;
123                 }
124
125                 for (a=0;a<7;a++)
126                 {
127                         marker = fgetc(hFile);
128                         if (marker != 0xff) break;
129
130                         if (a >= 6)
131                         {
132                                 strcpy(m_szLastError,"too many padding unsigned chars\n");
133                                 goto decode_exif_out;
134                         }
135                 }
136
137                 if (marker == 0xff)
138                 {
139                         strcpy(m_szLastError,"too many padding unsigned chars!");
140                         goto decode_exif_out;
141                 }
142
143                 Sections[SectionsRead].Type = marker;
144
145                 lh = fgetc(hFile);
146                 ll = fgetc(hFile);
147
148                 itemlen = (lh << 8) | ll;
149
150                 if (itemlen < 2)
151                 {
152                         strcpy(m_szLastError,"invalid marker");
153                         goto decode_exif_out;
154                 }
155                 Sections[SectionsRead].Size = itemlen;
156
157                 Data = (unsigned char *)malloc(itemlen);
158                 if (Data == NULL)
159                 {
160                         strcpy(m_szLastError,"Could not allocate memory");
161                         goto decode_exif_out;
162                 }
163                 Sections[SectionsRead].Data = Data;
164
165
166                 Data[0] = (unsigned char)lh;
167                 Data[1] = (unsigned char)ll;
168
169                 got = fread(Data+2, 1, itemlen-2,hFile);
170                 if (got != itemlen-2)
171                 {
172                         strcpy(m_szLastError,"Premature end of file?");
173                         goto decode_exif_out;
174                 }
175                 SectionsRead += 1;
176
177                 switch(marker)
178                 {
179                 case M_SOS:
180                         goto decode_exif_out;
181                 case M_EOI:
182                         printf("No image in jpeg!\n");
183                         goto decode_exif_out;
184                 case M_COM:
185                         if (HaveCom)
186                         {
187                                 free(Sections[--SectionsRead].Data);
188                                 Sections[SectionsRead].Data=0;
189                         }
190                         else
191                         {
192                                 process_COM(Data, itemlen);
193                                 HaveCom = 1;
194                         }
195                         break;
196                 case M_JFIF:
197                         free(Sections[--SectionsRead].Data);
198                         Sections[SectionsRead].Data=0;
199                         break;
200                 case M_EXIF:
201                         if (memcmp(Data+2, "Exif", 4) == 0)
202                         {
203                                 m_exifinfo->IsExif = process_EXIF((unsigned char *)Data+2, itemlen);
204                         }
205                         else
206                         {
207                                 free(Sections[--SectionsRead].Data);
208                                 Sections[SectionsRead].Data=0;
209                         }
210                         break;
211                 case M_SOF0:
212                 case M_SOF1:
213                 case M_SOF2:
214                 case M_SOF3:
215                 case M_SOF5:
216                 case M_SOF6:
217                 case M_SOF7:
218                 case M_SOF9:
219                 case M_SOF10:
220                 case M_SOF11:
221                 case M_SOF13:
222                 case M_SOF14:
223                 case M_SOF15:
224                         process_SOFn(Data, marker);
225                         break;
226                 default:
227                         break;
228                 }
229         }
230         ret = true;
231
232 decode_exif_out:
233         fclose(hFile);
234         return ret;
235 }
236
237 bool Cexif::process_EXIF(unsigned char * CharBuf, unsigned int length)
238 {
239         m_exifinfo->Comments[0] = '\0';
240         ExifImageWidth = 0;
241
242         static const unsigned char ExifHeader[] = "Exif\0\0";
243         if(memcmp(CharBuf+0, ExifHeader,6))
244         {
245                 strcpy(m_szLastError,"Incorrect Exif header"); return false;
246         }
247
248         if (memcmp(CharBuf+6,"II",2) == 0) MotorolaOrder = 0;
249         else
250         {
251                 if (memcmp(CharBuf+6,"MM",2) == 0) MotorolaOrder = 1;
252                 else
253                 {
254                         strcpy(m_szLastError,"Invalid Exif alignment marker."); return false;
255                 }
256         }
257
258         if (Get16u(CharBuf+8) != 0x2a)
259         {
260                 strcpy(m_szLastError,"Invalid Exif start (1)"); return false;
261         }
262         int FirstOffset = Get32u(CharBuf+10);
263         if (FirstOffset < 8 || FirstOffset > 16)
264         {
265                 strcpy(m_szLastError,"Suspicious offset of first IFD value"); return 0;
266         }
267         unsigned char * LastExifRefd = CharBuf;
268
269         if (!ProcessExifDir(CharBuf+14, CharBuf+6, length-6, m_exifinfo, &LastExifRefd)) return false;
270
271         if (m_exifinfo->FocalplaneXRes != 0)
272                 m_exifinfo->CCDWidth = (float)(ExifImageWidth * m_exifinfo->FocalplaneUnits / m_exifinfo->FocalplaneXRes);
273
274         return true;
275 }
276
277 int Cexif::Get16m(void * Short)
278 {
279         return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
280 }
281
282 int Cexif::Get16u(void * Short)
283 {
284         if (MotorolaOrder)
285         {
286                 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
287         }
288         else
289         {
290                 return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
291         }
292 }
293
294 long Cexif::Get32s(void * Long)
295 {
296         if (MotorolaOrder)
297         {
298                 return  ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16) | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
299         }
300         else
301         {
302                 return  ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16) | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
303         }
304 }
305
306 unsigned long Cexif::Get32u(void * Long)
307 {
308         return (unsigned long)Get32s(Long) & 0xffffffff;
309 }
310
311 bool Cexif::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength, EXIFINFO * const m_exifinfo, unsigned char ** const LastExifRefdP )
312 {
313         int de, a, NumDirEntries;
314         unsigned ThumbnailOffset = 0;
315         unsigned ThumbnailSize = 0;
316
317         NumDirEntries = Get16u(DirStart);
318
319         if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength))
320         {
321                 strcpy(m_szLastError,"Illegally sized directory"); return 0;
322         }
323
324         for (de=0;de<NumDirEntries;de++)
325         {
326                 int Tag, Format, Components;
327                 unsigned char * ValuePtr;
328                 int BytesCount;
329                 unsigned char * DirEntry;
330                 DirEntry = DirStart+2+12*de;
331                 Tag = Get16u(DirEntry);
332                 Format = Get16u(DirEntry+2);
333                 Components = Get32u(DirEntry+4);
334
335                 if ((Format-1) >= NUM_FORMATS)
336                 {
337                         strcpy(m_szLastError,"Illegal format code in EXIF dir"); return 0;
338                 }
339
340                 BytesCount = Components * BytesPerFormat[Format];
341
342                 if (BytesCount > 4)
343                 {
344                         unsigned OffsetVal;
345                         OffsetVal = Get32u(DirEntry+8);
346                         if (OffsetVal+BytesCount > ExifLength)
347                         {
348                                 strcpy(m_szLastError,"Illegal pointer offset value in EXIF."); return 0;
349                         }
350                         ValuePtr = OffsetBase+OffsetVal;
351                 }
352                 else ValuePtr = DirEntry+8;
353
354                 if (*LastExifRefdP < ValuePtr+BytesCount) *LastExifRefdP = ValuePtr+BytesCount;
355
356                 switch(Tag)
357                 {
358                 case TAG_MAKE:
359                         strncpy(m_exifinfo->CameraMake, (char*)ValuePtr, 31);
360                         break;
361                 case TAG_MODEL:
362                         strncpy(m_exifinfo->CameraModel, (char*)ValuePtr, 39);
363                         break;
364                 case TAG_EXIF_VERSION:
365                         strncpy(m_exifinfo->Version,(char*)ValuePtr, 4);
366                         break;
367                 case TAG_DATETIME_ORIGINAL:
368                         strncpy(m_exifinfo->DateTime, (char*)ValuePtr, 19);
369                         break;
370                 case TAG_USERCOMMENT:
371                         for (a=BytesCount;;)
372                         {
373                                 a--;
374                                 if (((char*)ValuePtr)[a] == ' ') ((char*)ValuePtr)[a] = '\0';
375                                 else break;
376
377                                 if (a == 0) break;
378                         }
379
380                         if (memcmp(ValuePtr, "ASCII",5) == 0)
381                         {
382                                 for (a=5;a<10;a++)
383                                 {
384                                         char c;
385                                         c = ((char*)ValuePtr)[a];
386                                         if (c != '\0' && c != ' ')
387                                         {
388                                                 strncpy(m_exifinfo->Comments, (char*)ValuePtr+a, 199);
389                                                 break;
390                                         }
391                                 }
392
393                         }
394                         else strncpy(m_exifinfo->Comments, (char*)ValuePtr, 199);
395                         break;
396                 case TAG_FNUMBER:
397                         m_exifinfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format);
398                         break;
399                 case TAG_APERTURE:
400                 case TAG_MAXAPERTURE:
401                         if (m_exifinfo->ApertureFNumber == 0)
402                         {
403                                 //m_exifinfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5);
404                         }
405                         break;
406                 case TAG_BRIGHTNESS:
407                         m_exifinfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format);
408                         break;
409                 case TAG_FOCALLENGTH:
410                         m_exifinfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format);
411                         break;
412                 case TAG_SUBJECT_DISTANCE:
413                         m_exifinfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format);
414                         break;
415                 case TAG_EXPOSURETIME:
416                         m_exifinfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format);
417                         break;
418                 case TAG_SHUTTERSPEED:
419                         if (m_exifinfo->ExposureTime == 0)
420                         {
421                                 //m_exifinfo->ExposureTime = (float) (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2)));
422                         }
423                         break;
424                 case TAG_FLASH:
425                         if ((int)ConvertAnyFormat(ValuePtr, Format) & 7) strcpy(m_exifinfo->FlashUsed,"fire");
426                         else strcpy(m_exifinfo->FlashUsed,"not fired");
427                         break;
428                 case TAG_ORIENTATION:
429                         m_exifinfo->Orient = (int)ConvertAnyFormat(ValuePtr, Format);
430                         switch((int)ConvertAnyFormat(ValuePtr, Format))
431                         {
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");
441                         }
442                         break;
443                 case TAG_EXIF_IMAGELENGTH:
444                 case TAG_EXIF_IMAGEWIDTH:
445                         a = (int)ConvertAnyFormat(ValuePtr, Format);
446                         if (ExifImageWidth < a) ExifImageWidth = a;
447                         break;
448                 case TAG_FOCALPLANEXRES:
449                         m_exifinfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format);
450                         break;
451                 case TAG_FOCALPLANEYRES:
452                         m_exifinfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format);
453                         break;
454                 case TAG_RESOLUTIONUNIT:
455                         switch((int)ConvertAnyFormat(ValuePtr, Format))
456                         {
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");
460                         }
461                         break;
462                 case TAG_FOCALPLANEUNITS:
463                         switch((int)ConvertAnyFormat(ValuePtr, Format))
464                         {
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;
470                         }
471                         break;
472                 case TAG_EXPOSURE_BIAS:
473                         m_exifinfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format);
474                         break;
475                 case TAG_WHITEBALANCE:
476                         switch((int)ConvertAnyFormat(ValuePtr, Format))
477                         {
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;
489                         }
490                         break;
491                 case TAG_METERING_MODE:
492                         switch((int)ConvertAnyFormat(ValuePtr, Format))
493                         {
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;
502                         }
503                         break;
504                 case TAG_EXPOSURE_PROGRAM:
505                         switch((int)ConvertAnyFormat(ValuePtr, Format))
506                         {
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;
517                         }
518                         break;
519                 case TAG_ISO_EQUIVALENT:
520                         m_exifinfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format);
521                         if ( m_exifinfo->ISOequivalent < 50 ) m_exifinfo->ISOequivalent *= 200;
522                         break;
523                 case TAG_COMPRESSION_LEVEL:
524                         m_exifinfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format);
525                         break;
526                 case TAG_XRESOLUTION:
527                         m_exifinfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format);
528                         break;
529                 case TAG_YRESOLUTION:
530                         m_exifinfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format);
531                         break;
532                 case TAG_THUMBNAIL_OFFSET:
533                         ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format);
534                         break;
535                 case TAG_THUMBNAIL_LENGTH:
536                         ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format);
537                         break;
538                 }
539
540                 if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET)
541                 {
542                         unsigned char * SubdirStart;
543                         SubdirStart = OffsetBase + Get32u(ValuePtr);
544                         if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
545                         {
546                                 strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
547                         }
548                         ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
549                         continue;
550                 }
551         }
552
553
554         unsigned char * SubdirStart;
555         unsigned Offset;
556         Offset = Get16u(DirStart+2+12*NumDirEntries);
557         if (Offset)
558         {
559                 SubdirStart = OffsetBase + Offset;
560                 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
561                 {
562                         strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
563                 }
564                 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
565         }
566
567         if (ThumbnailSize && ThumbnailOffset && m_exifinfo->Thumnailstate)
568         {
569                 if (ThumbnailSize + ThumbnailOffset <= ExifLength)
570                 {
571                         if(FILE *tf = fopen(THUMBNAILTMPFILE, "w"))
572                         {
573                                 fwrite( OffsetBase + ThumbnailOffset, ThumbnailSize, 1, tf);
574                                 fclose(tf);
575                                 m_exifinfo->Thumnailstate = 2;
576                         }
577                 }
578         }
579
580         return 1;
581 }
582
583 double Cexif::ConvertAnyFormat(void * ValuePtr, int Format)
584 {
585         double Value = 0;
586
587         switch(Format)
588         {
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;
593                 case FMT_URATIONAL:
594                 case FMT_SRATIONAL:
595                 {
596                         int Num = Get32s(ValuePtr);
597                         int Den = Get32s(4+(char *)ValuePtr);
598                         if (Den == 0) Value = 0;
599                         else Value = (double)Num/Den;
600                         break;
601                 }
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;
606         }
607         return Value;
608 }
609
610 void Cexif::process_COM (const unsigned char * Data, int length)
611 {
612         int ch,a;
613         char Comment[MAX_COMMENT+1];
614         int nch=0;
615
616         if (length > MAX_COMMENT) length = MAX_COMMENT;
617
618         for (a=2;a<length;a++)
619         {
620                 ch = Data[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++] = '?';
624         }
625         Comment[nch] = '\0';
626         strcpy(m_exifinfo->Comments,Comment);
627 }
628
629 void Cexif::process_SOFn (const unsigned char * Data, int marker)
630 {
631         int data_precision, num_components;
632
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];
637
638         if (num_components == 3) strcpy(m_exifinfo->IsColor,"yes");
639         else strcpy(m_exifinfo->IsColor,"no");
640
641         m_exifinfo->Process = marker;
642 }
643