gdi/picexif.cpp: more and more FD leak stuff
[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_false;
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_false;
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_false;
134                         }
135                 }
136
137                 if (marker == 0xff)
138                 {
139                         strcpy(m_szLastError,"too many padding unsigned chars!");
140                         goto decode_exif_out_false;
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_false;
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_false;
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_false;
174                 }
175                 SectionsRead += 1;
176
177                 switch(marker)
178                 {
179                 case M_SOS:
180                         goto decode_exif_out_true;
181                 case M_EOI:
182                         printf("No image in jpeg!\n");
183                         goto decode_exif_out_false;
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
231 decode_exif_out_true:
232         ret = true;
233
234 decode_exif_out_false:
235         fclose(hFile);
236         return ret;
237 }
238
239 bool Cexif::process_EXIF(unsigned char * CharBuf, unsigned int length)
240 {
241         m_exifinfo->Comments[0] = '\0';
242         ExifImageWidth = 0;
243
244         static const unsigned char ExifHeader[] = "Exif\0\0";
245         if(memcmp(CharBuf+0, ExifHeader,6))
246         {
247                 strcpy(m_szLastError,"Incorrect Exif header"); return false;
248         }
249
250         if (memcmp(CharBuf+6,"II",2) == 0) MotorolaOrder = 0;
251         else
252         {
253                 if (memcmp(CharBuf+6,"MM",2) == 0) MotorolaOrder = 1;
254                 else
255                 {
256                         strcpy(m_szLastError,"Invalid Exif alignment marker."); return false;
257                 }
258         }
259
260         if (Get16u(CharBuf+8) != 0x2a)
261         {
262                 strcpy(m_szLastError,"Invalid Exif start (1)"); return false;
263         }
264         int FirstOffset = Get32u(CharBuf+10);
265         if (FirstOffset < 8 || FirstOffset > 16)
266         {
267                 strcpy(m_szLastError,"Suspicious offset of first IFD value"); return 0;
268         }
269         unsigned char * LastExifRefd = CharBuf;
270
271         if (!ProcessExifDir(CharBuf+14, CharBuf+6, length-6, m_exifinfo, &LastExifRefd)) return false;
272
273         if (m_exifinfo->FocalplaneXRes != 0)
274                 m_exifinfo->CCDWidth = (float)(ExifImageWidth * m_exifinfo->FocalplaneUnits / m_exifinfo->FocalplaneXRes);
275
276         return true;
277 }
278
279 int Cexif::Get16m(void * Short)
280 {
281         return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
282 }
283
284 int Cexif::Get16u(void * Short)
285 {
286         if (MotorolaOrder)
287         {
288                 return (((unsigned char *)Short)[0] << 8) | ((unsigned char *)Short)[1];
289         }
290         else
291         {
292                 return (((unsigned char *)Short)[1] << 8) | ((unsigned char *)Short)[0];
293         }
294 }
295
296 long Cexif::Get32s(void * Long)
297 {
298         if (MotorolaOrder)
299         {
300                 return  ((( char *)Long)[0] << 24) | (((unsigned char *)Long)[1] << 16) | (((unsigned char *)Long)[2] << 8 ) | (((unsigned char *)Long)[3] << 0 );
301         }
302         else
303         {
304                 return  ((( char *)Long)[3] << 24) | (((unsigned char *)Long)[2] << 16) | (((unsigned char *)Long)[1] << 8 ) | (((unsigned char *)Long)[0] << 0 );
305         }
306 }
307
308 unsigned long Cexif::Get32u(void * Long)
309 {
310         return (unsigned long)Get32s(Long) & 0xffffffff;
311 }
312
313 bool Cexif::ProcessExifDir(unsigned char * DirStart, unsigned char * OffsetBase, unsigned ExifLength, EXIFINFO * const m_exifinfo, unsigned char ** const LastExifRefdP )
314 {
315         int de, a, NumDirEntries;
316         unsigned ThumbnailOffset = 0;
317         unsigned ThumbnailSize = 0;
318
319         NumDirEntries = Get16u(DirStart);
320
321         if ((DirStart+2+NumDirEntries*12) > (OffsetBase+ExifLength))
322         {
323                 strcpy(m_szLastError,"Illegally sized directory"); return 0;
324         }
325
326         for (de=0;de<NumDirEntries;de++)
327         {
328                 int Tag, Format, Components;
329                 unsigned char * ValuePtr;
330                 int BytesCount;
331                 unsigned char * DirEntry;
332                 DirEntry = DirStart+2+12*de;
333                 Tag = Get16u(DirEntry);
334                 Format = Get16u(DirEntry+2);
335                 Components = Get32u(DirEntry+4);
336
337                 if ((Format-1) >= NUM_FORMATS)
338                 {
339                         strcpy(m_szLastError,"Illegal format code in EXIF dir"); return 0;
340                 }
341
342                 BytesCount = Components * BytesPerFormat[Format];
343
344                 if (BytesCount > 4)
345                 {
346                         unsigned OffsetVal;
347                         OffsetVal = Get32u(DirEntry+8);
348                         if (OffsetVal+BytesCount > ExifLength)
349                         {
350                                 strcpy(m_szLastError,"Illegal pointer offset value in EXIF."); return 0;
351                         }
352                         ValuePtr = OffsetBase+OffsetVal;
353                 }
354                 else ValuePtr = DirEntry+8;
355
356                 if (*LastExifRefdP < ValuePtr+BytesCount) *LastExifRefdP = ValuePtr+BytesCount;
357
358                 switch(Tag)
359                 {
360                 case TAG_MAKE:
361                         strncpy(m_exifinfo->CameraMake, (char*)ValuePtr, 31);
362                         break;
363                 case TAG_MODEL:
364                         strncpy(m_exifinfo->CameraModel, (char*)ValuePtr, 39);
365                         break;
366                 case TAG_EXIF_VERSION:
367                         strncpy(m_exifinfo->Version,(char*)ValuePtr, 4);
368                         break;
369                 case TAG_DATETIME_ORIGINAL:
370                         strncpy(m_exifinfo->DateTime, (char*)ValuePtr, 19);
371                         break;
372                 case TAG_USERCOMMENT:
373                         for (a=BytesCount;;)
374                         {
375                                 a--;
376                                 if (((char*)ValuePtr)[a] == ' ') ((char*)ValuePtr)[a] = '\0';
377                                 else break;
378
379                                 if (a == 0) break;
380                         }
381
382                         if (memcmp(ValuePtr, "ASCII",5) == 0)
383                         {
384                                 for (a=5;a<10;a++)
385                                 {
386                                         char c;
387                                         c = ((char*)ValuePtr)[a];
388                                         if (c != '\0' && c != ' ')
389                                         {
390                                                 strncpy(m_exifinfo->Comments, (char*)ValuePtr+a, 199);
391                                                 break;
392                                         }
393                                 }
394
395                         }
396                         else strncpy(m_exifinfo->Comments, (char*)ValuePtr, 199);
397                         break;
398                 case TAG_FNUMBER:
399                         m_exifinfo->ApertureFNumber = (float)ConvertAnyFormat(ValuePtr, Format);
400                         break;
401                 case TAG_APERTURE:
402                 case TAG_MAXAPERTURE:
403                         if (m_exifinfo->ApertureFNumber == 0)
404                         {
405                                 //m_exifinfo->ApertureFNumber = (float)exp(ConvertAnyFormat(ValuePtr, Format)*log(2)*0.5);
406                         }
407                         break;
408                 case TAG_BRIGHTNESS:
409                         m_exifinfo->Brightness = (float)ConvertAnyFormat(ValuePtr, Format);
410                         break;
411                 case TAG_FOCALLENGTH:
412                         m_exifinfo->FocalLength = (float)ConvertAnyFormat(ValuePtr, Format);
413                         break;
414                 case TAG_SUBJECT_DISTANCE:
415                         m_exifinfo->Distance = (float)ConvertAnyFormat(ValuePtr, Format);
416                         break;
417                 case TAG_EXPOSURETIME:
418                         m_exifinfo->ExposureTime = (float)ConvertAnyFormat(ValuePtr, Format);
419                         break;
420                 case TAG_SHUTTERSPEED:
421                         if (m_exifinfo->ExposureTime == 0)
422                         {
423                                 //m_exifinfo->ExposureTime = (float) (1/exp(ConvertAnyFormat(ValuePtr, Format)*log(2)));
424                         }
425                         break;
426                 case TAG_FLASH:
427                         if ((int)ConvertAnyFormat(ValuePtr, Format) & 7) strcpy(m_exifinfo->FlashUsed,"fire");
428                         else strcpy(m_exifinfo->FlashUsed,"not fired");
429                         break;
430                 case TAG_ORIENTATION:
431                         m_exifinfo->Orient = (int)ConvertAnyFormat(ValuePtr, Format);
432                         switch((int)ConvertAnyFormat(ValuePtr, Format))
433                         {
434                         case 1:         strcpy(m_exifinfo->Orientation,"Top-Left"); break;
435                         case 2:         strcpy(m_exifinfo->Orientation,"Top-Right"); break;
436                         case 3:         strcpy(m_exifinfo->Orientation,"Bottom-Right"); break;
437                         case 4:         strcpy(m_exifinfo->Orientation,"Bottom-Left"); break;
438                         case 5:         strcpy(m_exifinfo->Orientation,"Left-Top"); break;
439                         case 6:         strcpy(m_exifinfo->Orientation,"Right-Top"); break;
440                         case 7:         strcpy(m_exifinfo->Orientation,"Right-Bottom"); break;
441                         case 8:         strcpy(m_exifinfo->Orientation,"Left-Bottom"); break;
442                         default:        strcpy(m_exifinfo->Orientation,"Undefined rotation value");
443                         }
444                         break;
445                 case TAG_EXIF_IMAGELENGTH:
446                 case TAG_EXIF_IMAGEWIDTH:
447                         a = (int)ConvertAnyFormat(ValuePtr, Format);
448                         if (ExifImageWidth < a) ExifImageWidth = a;
449                         break;
450                 case TAG_FOCALPLANEXRES:
451                         m_exifinfo->FocalplaneXRes = (float)ConvertAnyFormat(ValuePtr, Format);
452                         break;
453                 case TAG_FOCALPLANEYRES:
454                         m_exifinfo->FocalplaneYRes = (float)ConvertAnyFormat(ValuePtr, Format);
455                         break;
456                 case TAG_RESOLUTIONUNIT:
457                         switch((int)ConvertAnyFormat(ValuePtr, Format))
458                         {
459                                 case 2: strcpy(m_exifinfo->ResolutionUnit,"inches"); break;
460                                 case 3: strcpy(m_exifinfo->ResolutionUnit,"centimeters"); break;
461                                 default: strcpy(m_exifinfo->ResolutionUnit,"reserved");
462                         }
463                         break;
464                 case TAG_FOCALPLANEUNITS:
465                         switch((int)ConvertAnyFormat(ValuePtr, Format))
466                         {
467                                 case 1: m_exifinfo->FocalplaneUnits = 1.0f; break;
468                                 case 2: m_exifinfo->FocalplaneUnits = 1.0f; break;
469                                 case 3: m_exifinfo->FocalplaneUnits = 0.3937007874f; break;
470                                 case 4: m_exifinfo->FocalplaneUnits = 0.03937007874f; break;
471                                 case 5: m_exifinfo->FocalplaneUnits = 0.00003937007874f;
472                         }
473                         break;
474                 case TAG_EXPOSURE_BIAS:
475                         m_exifinfo->ExposureBias = (float) ConvertAnyFormat(ValuePtr, Format);
476                         break;
477                 case TAG_WHITEBALANCE:
478                         switch((int)ConvertAnyFormat(ValuePtr, Format))
479                         {
480                                 case 0: strcpy(m_exifinfo->LightSource,"unknown"); break;
481                                 case 1: strcpy(m_exifinfo->LightSource,"Daylight"); break;
482                                 case 2: strcpy(m_exifinfo->LightSource,"Fluorescent"); break;
483                                 case 3: strcpy(m_exifinfo->LightSource,"Tungsten"); break;
484                                 case 17: strcpy(m_exifinfo->LightSource,"Standard light A"); break;
485                                 case 18: strcpy(m_exifinfo->LightSource,"Standard light B"); break;
486                                 case 19: strcpy(m_exifinfo->LightSource,"Standard light C"); break;
487                                 case 20: strcpy(m_exifinfo->LightSource,"D55"); break;
488                                 case 21: strcpy(m_exifinfo->LightSource,"D65"); break;
489                                 case 22: strcpy(m_exifinfo->LightSource,"D75"); break;
490                                 default: strcpy(m_exifinfo->LightSource,"other"); break;
491                         }
492                         break;
493                 case TAG_METERING_MODE:
494                         switch((int)ConvertAnyFormat(ValuePtr, Format))
495                         {
496                                 case 0: strcpy(m_exifinfo->MeteringMode,"unknown"); break;
497                                 case 1: strcpy(m_exifinfo->MeteringMode,"Average"); break;
498                                 case 2: strcpy(m_exifinfo->MeteringMode,"Center-Weighted-Average"); break;
499                                 case 3: strcpy(m_exifinfo->MeteringMode,"Spot"); break;
500                                 case 4: strcpy(m_exifinfo->MeteringMode,"MultiSpot"); break;
501                                 case 5: strcpy(m_exifinfo->MeteringMode,"Pattern"); break;
502                                 case 6: strcpy(m_exifinfo->MeteringMode,"Partial"); break;
503                                 default: strcpy(m_exifinfo->MeteringMode,"other"); break;
504                         }
505                         break;
506                 case TAG_EXPOSURE_PROGRAM:
507                         switch((int)ConvertAnyFormat(ValuePtr, Format))
508                         {
509                                 case 0: strcpy(m_exifinfo->ExposureProgram,"not defined"); break;
510                                 case 1: strcpy(m_exifinfo->ExposureProgram,"Manual"); break;
511                                 case 2: strcpy(m_exifinfo->ExposureProgram,"Normal program"); break;
512                                 case 3: strcpy(m_exifinfo->ExposureProgram,"Aperture priority"); break;
513                                 case 4: strcpy(m_exifinfo->ExposureProgram,"Shutter priority"); break;
514                                 case 5: strcpy(m_exifinfo->ExposureProgram,"Creative program"); break;
515                                 case 6: strcpy(m_exifinfo->ExposureProgram,"Action program"); break;
516                                 case 7: strcpy(m_exifinfo->ExposureProgram,"Portrait mode"); break;
517                                 case 8: strcpy(m_exifinfo->ExposureProgram,"Landscape mode"); break;
518                                 default: strcpy(m_exifinfo->ExposureProgram,"reserved"); break;
519                         }
520                         break;
521                 case TAG_ISO_EQUIVALENT:
522                         m_exifinfo->ISOequivalent = (int)ConvertAnyFormat(ValuePtr, Format);
523                         if ( m_exifinfo->ISOequivalent < 50 ) m_exifinfo->ISOequivalent *= 200;
524                         break;
525                 case TAG_COMPRESSION_LEVEL:
526                         m_exifinfo->CompressionLevel = (int)ConvertAnyFormat(ValuePtr, Format);
527                         break;
528                 case TAG_XRESOLUTION:
529                         m_exifinfo->Xresolution = (float)ConvertAnyFormat(ValuePtr, Format);
530                         break;
531                 case TAG_YRESOLUTION:
532                         m_exifinfo->Yresolution = (float)ConvertAnyFormat(ValuePtr, Format);
533                         break;
534                 case TAG_THUMBNAIL_OFFSET:
535                         ThumbnailOffset = (unsigned)ConvertAnyFormat(ValuePtr, Format);
536                         break;
537                 case TAG_THUMBNAIL_LENGTH:
538                         ThumbnailSize = (unsigned)ConvertAnyFormat(ValuePtr, Format);
539                         break;
540                 }
541
542                 if (Tag == TAG_EXIF_OFFSET || Tag == TAG_INTEROP_OFFSET)
543                 {
544                         unsigned char * SubdirStart;
545                         SubdirStart = OffsetBase + Get32u(ValuePtr);
546                         if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
547                         {
548                                 strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
549                         }
550                         ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
551                         continue;
552                 }
553         }
554
555
556         unsigned char * SubdirStart;
557         unsigned Offset;
558         Offset = Get16u(DirStart+2+12*NumDirEntries);
559         if (Offset)
560         {
561                 SubdirStart = OffsetBase + Offset;
562                 if (SubdirStart < OffsetBase || SubdirStart > OffsetBase+ExifLength)
563                 {
564                         strcpy(m_szLastError,"Illegal subdirectory link"); return 0;
565                 }
566                 ProcessExifDir(SubdirStart, OffsetBase, ExifLength, m_exifinfo, LastExifRefdP);
567         }
568
569         if (ThumbnailSize && ThumbnailOffset && m_exifinfo->Thumnailstate)
570         {
571                 if (ThumbnailSize + ThumbnailOffset <= ExifLength)
572                 {
573                         if(FILE *tf = fopen(THUMBNAILTMPFILE, "w"))
574                         {
575                                 fwrite( OffsetBase + ThumbnailOffset, ThumbnailSize, 1, tf);
576                                 fclose(tf);
577                                 m_exifinfo->Thumnailstate = 2;
578                         }
579                 }
580         }
581
582         return 1;
583 }
584
585 double Cexif::ConvertAnyFormat(void * ValuePtr, int Format)
586 {
587         double Value = 0;
588
589         switch(Format)
590         {
591                 case FMT_SBYTE:         Value = *(signed char *)ValuePtr;       break;
592                 case FMT_BYTE:          Value = *(unsigned char *)ValuePtr;     break;
593                 case FMT_USHORT:        Value = Get16u(ValuePtr);               break;
594                 case FMT_ULONG:         Value = Get32u(ValuePtr);               break;
595                 case FMT_URATIONAL:
596                 case FMT_SRATIONAL:
597                 {
598                         int Num = Get32s(ValuePtr);
599                         int Den = Get32s(4+(char *)ValuePtr);
600                         if (Den == 0) Value = 0;
601                         else Value = (double)Num/Den;
602                         break;
603                 }
604                 case FMT_SSHORT:        Value = (signed short)Get16u(ValuePtr); break;
605                 case FMT_SLONG:         Value = Get32s(ValuePtr);               break;
606                 case FMT_SINGLE:        Value = (double)*(float *)ValuePtr;     break;
607                 case FMT_DOUBLE:        Value = *(double *)ValuePtr;            break;
608         }
609         return Value;
610 }
611
612 void Cexif::process_COM (const unsigned char * Data, int length)
613 {
614         int ch,a;
615         char Comment[MAX_COMMENT+1];
616         int nch=0;
617
618         if (length > MAX_COMMENT) length = MAX_COMMENT;
619
620         for (a=2;a<length;a++)
621         {
622                 ch = Data[a];
623                 if (ch == '\r' && Data[a+1] == '\n') continue;
624                 if ((ch>=0x20) || ch == '\n' || ch == '\t') Comment[nch++] = (char)ch;
625                 else Comment[nch++] = '?';
626         }
627         Comment[nch] = '\0';
628         strcpy(m_exifinfo->Comments,Comment);
629 }
630
631 void Cexif::process_SOFn (const unsigned char * Data, int marker)
632 {
633         int data_precision, num_components;
634
635         data_precision = Data[2];
636         m_exifinfo->Height = Get16m((void*)(Data+3));
637         m_exifinfo->Width = Get16m((void*)(Data+5));
638         num_components = Data[7];
639
640         if (num_components == 3) strcpy(m_exifinfo->IsColor,"yes");
641         else strcpy(m_exifinfo->IsColor,"no");
642
643         m_exifinfo->Process = marker;
644 }
645