fixes for /dev/misc/pvr opening problems in some conditions
[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         FILE * hFile = fopen(filename, "r");
94         if(!hFile) return false;
95
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) return false;
111
112         for(;;)
113         {
114                 int marker = 0;
115                 int ll,lh, got, itemlen;
116                 unsigned char * Data;
117
118                 if (SectionsRead >= MAX_SECTIONS)
119                 {
120                         strcpy(m_szLastError,"Too many sections in jpg file"); return false;
121                 }
122
123                 for (a=0;a<7;a++)
124                 {
125                         marker = fgetc(hFile);
126                         if (marker != 0xff) break;
127
128                         if (a >= 6)
129                         {
130                                 strcpy(m_szLastError,"too many padding unsigned chars\n"); return false;
131                         }
132                 }
133
134                 if (marker == 0xff)
135                 {
136                         strcpy(m_szLastError,"too many padding unsigned chars!"); return false;
137                 }
138
139                 Sections[SectionsRead].Type = marker;
140
141                 lh = fgetc(hFile);
142                 ll = fgetc(hFile);
143
144                 itemlen = (lh << 8) | ll;
145
146                 if (itemlen < 2)
147                 {
148                         strcpy(m_szLastError,"invalid marker"); return false;
149                 }
150                 Sections[SectionsRead].Size = itemlen;
151
152                 Data = (unsigned char *)malloc(itemlen);
153                 if (Data == NULL)
154                 {
155                         strcpy(m_szLastError,"Could not allocate memory"); return false;
156                 }
157                 Sections[SectionsRead].Data = Data;
158
159
160                 Data[0] = (unsigned char)lh;
161                 Data[1] = (unsigned char)ll;
162
163                 got = fread(Data+2, 1, itemlen-2,hFile);
164                 if (got != itemlen-2)
165                 {
166                         strcpy(m_szLastError,"Premature end of file?"); return false;
167                 }
168                 SectionsRead += 1;
169
170                 switch(marker)
171                 {
172                 case M_SOS:
173                         return true;
174                 case M_EOI:
175                         printf("No image in jpeg!\n");
176                         return false;
177                 case M_COM:
178                         if (HaveCom)
179                         {
180                                 free(Sections[--SectionsRead].Data);
181                                 Sections[SectionsRead].Data=0;
182                         }
183                         else
184                         {
185                                 process_COM(Data, itemlen);
186                                 HaveCom = 1;
187                         }
188                         break;
189                 case M_JFIF:
190                         free(Sections[--SectionsRead].Data);
191                         Sections[SectionsRead].Data=0;
192                         break;
193                 case M_EXIF:
194                         if (memcmp(Data+2, "Exif", 4) == 0)
195                         {
196                                 m_exifinfo->IsExif = process_EXIF((unsigned char *)Data+2, itemlen);
197                         }
198                         else
199                         {
200                                 free(Sections[--SectionsRead].Data);
201                                 Sections[SectionsRead].Data=0;
202                         }
203                         break;
204                 case M_SOF0:
205                 case M_SOF1:
206                 case M_SOF2:
207                 case M_SOF3:
208                 case M_SOF5:
209                 case M_SOF6:
210                 case M_SOF7:
211                 case M_SOF9:
212                 case M_SOF10:
213                 case M_SOF11:
214                 case M_SOF13:
215                 case M_SOF14:
216                 case M_SOF15:
217                         process_SOFn(Data, marker);
218                         break;
219                 default:
220                         break;
221                 }
222         }
223
224         fclose(hFile);
225         return true;
226 }
227
228 bool Cexif::process_EXIF(unsigned char * CharBuf, unsigned int length)
229 {
230         m_exifinfo->Comments[0] = '\0';
231         ExifImageWidth = 0;
232
233         static const unsigned char ExifHeader[] = "Exif\0\0";
234         if(memcmp(CharBuf+0, ExifHeader,6))
235         {
236                 strcpy(m_szLastError,"Incorrect Exif header"); return false;
237         }
238
239         if (memcmp(CharBuf+6,"II",2) == 0) MotorolaOrder = 0;
240         else
241         {
242                 if (memcmp(CharBuf+6,"MM",2) == 0) MotorolaOrder = 1;
243                 else
244                 {
245                         strcpy(m_szLastError,"Invalid Exif alignment marker."); return false;
246                 }
247         }
248
249         if (Get16u(CharBuf+8) != 0x2a)
250         {
251                 strcpy(m_szLastError,"Invalid Exif start (1)"); return false;
252         }
253         int FirstOffset = Get32u(CharBuf+10);
254         if (FirstOffset < 8 || FirstOffset > 16)
255         {
256                 strcpy(m_szLastError,"Suspicious offset of first IFD value"); return 0;
257         }
258         unsigned char * LastExifRefd = CharBuf;
259
260         if (!ProcessExifDir(CharBuf+14, CharBuf+6, length-6, m_exifinfo, &LastExifRefd)) return false;
261
262         if (m_exifinfo->FocalplaneXRes != 0)
263                 m_exifinfo->CCDWidth = (float)(ExifImageWidth * m_exifinfo->FocalplaneUnits / m_exifinfo->FocalplaneXRes);
264
265         return true;
266 }
267
268 int Cexif::Get16m(void * Short)
269 {
270         return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
271 }
272
273 int Cexif::Get16u(void * Short)
274 {
275         if (MotorolaOrder)
276         {
277                 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
278         }
279         else
280         {
281                 return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
282         }
283 }
284
285 long Cexif::Get32s(void * Long)
286 {
287         if (MotorolaOrder)
288         {
289                 return  ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16) | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
290         }
291         else
292         {
293                 return  ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16) | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
294         }
295 }
296
297 unsigned long Cexif::Get32u(void * Long)
298 {
299         return (unsigned long)Get32s(Long) & 0xffffffff;
300 }
301
302 bool Cexif::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength, EXIFINFO * const m_exifinfo, unsigned char ** const LastExifRefdP )
303 {
304         int de, a, NumDirEntries;
305         unsigned ThumbnailOffset = 0;
306         unsigned ThumbnailSize = 0;
307
308         NumDirEntries = Get16u(DirStart);
309
310         if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength))
311         {
312                 strcpy(m_szLastError,"Illegally sized directory"); return 0;
313         }
314
315         for (de=0;de<NumDirEntries;de++)
316         {
317                 int Tag, Format, Components;
318                 unsigned char * ValuePtr;
319                 int BytesCount;
320                 unsigned char * DirEntry;
321                 DirEntry = DirStart+2+12*de;
322                 Tag = Get16u(DirEntry);
323                 Format = Get16u(DirEntry+2);
324                 Components = Get32u(DirEntry+4);
325
326                 if ((Format-1) >= NUM_FORMATS)
327                 {
328                         strcpy(m_szLastError,"Illegal format code in EXIF dir"); return 0;
329                 }
330
331                 BytesCount = Components * BytesPerFormat[Format];
332
333                 if (BytesCount > 4)
334                 {
335                         unsigned OffsetVal;
336                         OffsetVal = Get32u(DirEntry+8);
337                         if (OffsetVal+BytesCount > ExifLength)
338                         {
339                                 strcpy(m_szLastError,"Illegal pointer offset value in EXIF."); return 0;
340                         }
341                         ValuePtr = OffsetBase+OffsetVal;
342                 }
343                 else ValuePtr = DirEntry+8;
344
345                 if (*LastExifRefdP < ValuePtr+BytesCount) *LastExifRefdP = ValuePtr+BytesCount;
346
347                 switch(Tag)
348                 {
349                 case TAG_MAKE:
350                         strncpy(m_exifinfo->CameraMake, (char*)ValuePtr, 31);
351                         break;
352                 case TAG_MODEL:
353                         strncpy(m_exifinfo->CameraModel, (char*)ValuePtr, 39);
354                         break;
355                 case TAG_EXIF_VERSION:
356                         strncpy(m_exifinfo->Version,(char*)ValuePtr, 4);
357                         break;
358                 case TAG_DATETIME_ORIGINAL:
359                         strncpy(m_exifinfo->DateTime, (char*)ValuePtr, 19);
360                         break;
361                 case TAG_USERCOMMENT:
362                         for (a=BytesCount;;)
363                         {
364                                 a--;
365                                 if (((char*)ValuePtr)[a] == ' ') ((char*)ValuePtr)[a] = '\0';
366                                 else break;
367
368                                 if (a == 0) break;
369                         }
370
371                         if (memcmp(ValuePtr, "ASCII",5) == 0)
372                         {
373                                 for (a=5;a<10;a++)
374                                 {
375                                         char c;
376                                         c = ((char*)ValuePtr)[a];
377                                         if (c != '\0' && c != ' ')
378                                         {
379                                                 strncpy(m_exifinfo->Comments, (char*)ValuePtr+a, 199);
380                                                 break;
381                                         }
382                                 }
383
384                         }
385                         else strncpy(m_exifinfo->Comments, (char*)ValuePtr, 199);
386                         break;
387                 case TAG_FNUMBER:
388                         m_exifinfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format);
389                         break;
390                 case TAG_APERTURE:
391                 case TAG_MAXAPERTURE:
392                         if (m_exifinfo->ApertureFNumber == 0)
393                         {
394                                 //m_exifinfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5);
395                         }
396                         break;
397                 case TAG_BRIGHTNESS:
398                         m_exifinfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format);
399                         break;
400                 case TAG_FOCALLENGTH:
401                         m_exifinfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format);
402                         break;
403                 case TAG_SUBJECT_DISTANCE:
404                         m_exifinfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format);
405                         break;
406                 case TAG_EXPOSURETIME:
407                         m_exifinfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format);
408                         break;
409                 case TAG_SHUTTERSPEED:
410                         if (m_exifinfo->ExposureTime == 0)
411                         {
412                                 //m_exifinfo->ExposureTime = (float) (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2)));
413                         }
414                         break;
415                 case TAG_FLASH:
416                         if ((int)ConvertAnyFormat(ValuePtr, Format) & 7) strcpy(m_exifinfo->FlashUsed,"fire");
417                         else strcpy(m_exifinfo->FlashUsed,"not fired");
418                         break;
419                 case TAG_ORIENTATION:
420                         m_exifinfo->Orient = (int)ConvertAnyFormat(ValuePtr, Format);
421                         switch((int)ConvertAnyFormat(ValuePtr, Format))
422                         {
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 rotation value");
432                         }
433                         break;
434                 case TAG_EXIF_IMAGELENGTH:
435                 case TAG_EXIF_IMAGEWIDTH:
436                         a = (int)ConvertAnyFormat(ValuePtr, Format);
437                         if (ExifImageWidth < a) ExifImageWidth = a;
438                         break;
439                 case TAG_FOCALPLANEXRES:
440                         m_exifinfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format);
441                         break;
442                 case TAG_FOCALPLANEYRES:
443                         m_exifinfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format);
444                         break;
445                 case TAG_RESOLUTIONUNIT:
446                         switch((int)ConvertAnyFormat(ValuePtr, Format))
447                         {
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");
451                         }
452                         break;
453                 case TAG_FOCALPLANEUNITS:
454                         switch((int)ConvertAnyFormat(ValuePtr, Format))
455                         {
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;
461                         }
462                         break;
463                 case TAG_EXPOSURE_BIAS:
464                         m_exifinfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format);
465                         break;
466                 case TAG_WHITEBALANCE:
467                         switch((int)ConvertAnyFormat(ValuePtr, Format))
468                         {
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;
480                         }
481                         break;
482                 case TAG_METERING_MODE:
483                         switch((int)ConvertAnyFormat(ValuePtr, Format))
484                         {
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;
493                         }
494                         break;
495                 case TAG_EXPOSURE_PROGRAM:
496                         switch((int)ConvertAnyFormat(ValuePtr, Format))
497                         {
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;
508                         }
509                         break;
510                 case TAG_ISO_EQUIVALENT:
511                         m_exifinfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format);
512                         if ( m_exifinfo->ISOequivalent < 50 ) m_exifinfo->ISOequivalent *= 200;
513                         break;
514                 case TAG_COMPRESSION_LEVEL:
515                         m_exifinfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format);
516                         break;
517                 case TAG_XRESOLUTION:
518                         m_exifinfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format);
519                         break;
520                 case TAG_YRESOLUTION:
521                         m_exifinfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format);
522                         break;
523                 case TAG_THUMBNAIL_OFFSET:
524                         ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format);
525                         break;
526                 case TAG_THUMBNAIL_LENGTH:
527                         ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format);
528                         break;
529                 }
530
531                 if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET)
532                 {
533                         unsigned char * SubdirStart;
534                         SubdirStart = OffsetBase + Get32u(ValuePtr);
535                         if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
536                         {
537                                 strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
538                         }
539                         ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
540                         continue;
541                 }
542         }
543
544
545         unsigned char * SubdirStart;
546         unsigned Offset;
547         Offset = Get16u(DirStart+2+12*NumDirEntries);
548         if (Offset)
549         {
550                 SubdirStart = OffsetBase + Offset;
551                 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
552                 {
553                         strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
554                 }
555                 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
556         }
557
558         if (ThumbnailSize && ThumbnailOffset && m_exifinfo->Thumnailstate)
559         {
560                 if (ThumbnailSize + ThumbnailOffset <= ExifLength)
561                 {
562                         if(FILE *tf = fopen(THUMBNAILTMPFILE, "w"))
563                         {
564                                 fwrite( OffsetBase + ThumbnailOffset, ThumbnailSize, 1, tf);
565                                 fclose(tf);
566                                 m_exifinfo->Thumnailstate = 2;
567                         }
568                 }
569         }
570
571         return 1;
572 }
573
574 double Cexif::ConvertAnyFormat(void * ValuePtr, int Format)
575 {
576         double Value = 0;
577
578         switch(Format)
579         {
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;
584                 case FMT_URATIONAL:
585                 case FMT_SRATIONAL:
586                 {
587                         int Num = Get32s(ValuePtr);
588                         int Den = Get32s(4+(char *)ValuePtr);
589                         if (Den == 0) Value = 0;
590                         else Value = (double)Num/Den;
591                         break;
592                 }
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;
597         }
598         return Value;
599 }
600
601 void Cexif::process_COM (const unsigned char * Data, int length)
602 {
603         int ch,a;
604         char Comment[MAX_COMMENT+1];
605         int nch=0;
606
607         if (length > MAX_COMMENT) length = MAX_COMMENT;
608
609         for (a=2;a<length;a++)
610         {
611                 ch = Data[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++] = '?';
615         }
616         Comment[nch] = '\0';
617         strcpy(m_exifinfo->Comments,Comment);
618 }
619
620 void Cexif::process_SOFn (const unsigned char * Data, int marker)
621 {
622         int data_precision, num_components;
623
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];
628
629         if (num_components == 3) strcpy(m_exifinfo->IsColor,"yes");
630         else strcpy(m_exifinfo->IsColor,"no");
631
632         m_exifinfo->Process = marker;
633 }
634