]> Creatis software - clitk.git/blob - utilities/CxImage/ximaenc.cpp
cosmetic
[clitk.git] / utilities / CxImage / ximaenc.cpp
1 // xImaCodec.cpp : Encode Decode functions
2 /* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
3  * CxImage version 6.0.0 02/Feb/2008
4  */
5
6 #include "ximage.h"
7
8 #if CXIMAGE_SUPPORT_JPG
9 #include "ximajpg.h"
10 #endif
11
12 #if CXIMAGE_SUPPORT_GIF
13 #include "ximagif.h"
14 #endif
15
16 #if CXIMAGE_SUPPORT_PNG
17 #include "ximapng.h"
18 #endif
19
20 #if CXIMAGE_SUPPORT_MNG
21 #include "ximamng.h"
22 #endif
23
24 #if CXIMAGE_SUPPORT_BMP
25 #include "ximabmp.h"
26 #endif
27
28 #if CXIMAGE_SUPPORT_ICO
29 #include "ximaico.h"
30 #endif
31
32 #if CXIMAGE_SUPPORT_TIF
33 #include "ximatif.h"
34 #endif
35
36 #if CXIMAGE_SUPPORT_TGA
37 #include "ximatga.h"
38 #endif
39
40 #if CXIMAGE_SUPPORT_PCX
41 #include "ximapcx.h"
42 #endif
43
44 #if CXIMAGE_SUPPORT_WBMP
45 #include "ximawbmp.h"
46 #endif
47
48 #if CXIMAGE_SUPPORT_WMF
49 #include "ximawmf.h" // <vho> - WMF/EMF support
50 #endif
51
52 #if CXIMAGE_SUPPORT_JBG
53 #include "ximajbg.h"
54 #endif
55
56 #if CXIMAGE_SUPPORT_JASPER
57 #include "ximajas.h"
58 #endif
59
60 #if CXIMAGE_SUPPORT_SKA
61 #include "ximaska.h"
62 #endif
63
64 #if CXIMAGE_SUPPORT_RAW
65 #include "ximaraw.h"
66 #endif
67
68 ////////////////////////////////////////////////////////////////////////////////
69 #if CXIMAGE_SUPPORT_ENCODE
70 ////////////////////////////////////////////////////////////////////////////////
71 bool CxImage::EncodeSafeCheck(CxFile *hFile)
72 {
73         if (hFile==NULL) {
74                 strcpy(info.szLastError,CXIMAGE_ERR_NOFILE);
75                 return true;
76         }
77
78         if (pDib==NULL){
79                 strcpy(info.szLastError,CXIMAGE_ERR_NOIMAGE);
80                 return true;
81         }
82         return false;
83 }
84 ////////////////////////////////////////////////////////////////////////////////
85 //#ifdef WIN32
86 //bool CxImage::Save(LPCWSTR filename, DWORD imagetype)
87 //{
88 //      FILE* hFile;    //file handle to write the image
89 //      if ((hFile=_wfopen(filename,L"wb"))==NULL)  return false;
90 //      bool bOK = Encode(hFile,imagetype);
91 //      fclose(hFile);
92 //      return bOK;
93 //}
94 //#endif //WIN32
95 ////////////////////////////////////////////////////////////////////////////////
96 // For UNICODE support: char -> TCHAR
97 /**
98  * Saves to disk the image in a specific format.
99  * \param filename: file name
100  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
101  * \return true if everything is ok
102  */
103 bool CxImage::Save(const TCHAR * filename, DWORD imagetype)
104 {
105         FILE* hFile;    //file handle to write the image
106
107 #ifdef WIN32
108         if ((hFile=_tfopen(filename,_T("wb")))==NULL)  return false;    // For UNICODE support
109 #else
110         if ((hFile=fopen(filename,"wb"))==NULL)  return false;
111 #endif
112
113         bool bOK = Encode(hFile,imagetype);
114         fclose(hFile);
115         return bOK;
116 }
117 ////////////////////////////////////////////////////////////////////////////////
118 /**
119  * Saves to disk the image in a specific format.
120  * \param hFile: file handle, open and enabled for writing.
121  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
122  * \return true if everything is ok
123  */
124 bool CxImage::Encode(FILE *hFile, DWORD imagetype)
125 {
126         CxIOFile file(hFile);
127         return Encode(&file,imagetype);
128 }
129 ////////////////////////////////////////////////////////////////////////////////
130 /**
131  * Saves to memory buffer the image in a specific format.
132  * \param buffer: output memory buffer pointer. Must be NULL,
133  * the function allocates and fill the memory,
134  * the application must free the buffer, see also FreeMemory().
135  * \param size: output memory buffer size.
136  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
137  * \return true if everything is ok
138  */
139 bool CxImage::Encode(BYTE * &buffer, long &size, DWORD imagetype)
140 {
141         if (buffer!=NULL){
142                 strcpy(info.szLastError,"the buffer must be empty");
143                 return false;
144         }
145         CxMemFile file;
146         file.Open();
147         if(Encode(&file,imagetype)){
148                 buffer=file.GetBuffer();
149                 size=file.Size();
150                 return true;
151         }
152         return false;
153 }
154 ////////////////////////////////////////////////////////////////////////////////
155 /**
156  * Saves to disk the image in a specific format.
157  * \param hFile: file handle (CxMemFile or CxIOFile), with write access.
158  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
159  * \return true if everything is ok
160  * \sa ENUM_CXIMAGE_FORMATS
161  */
162 bool CxImage::Encode(CxFile *hFile, DWORD imagetype)
163 {
164
165 #if CXIMAGE_SUPPORT_BMP
166
167         if (imagetype==CXIMAGE_FORMAT_BMP){
168                 CxImageBMP newima;
169                 newima.Ghost(this);
170                 if (newima.Encode(hFile)){
171                         return true;
172                 } else {
173                         strcpy(info.szLastError,newima.GetLastError());
174                         return false;
175                 }
176         }
177 #endif
178 #if CXIMAGE_SUPPORT_ICO
179         if (imagetype==CXIMAGE_FORMAT_ICO){
180                 CxImageICO newima;
181                 newima.Ghost(this);
182                 if (newima.Encode(hFile)){
183                         return true;
184                 } else {
185                         strcpy(info.szLastError,newima.GetLastError());
186                         return false;
187                 }
188         }
189 #endif
190 #if CXIMAGE_SUPPORT_TIF
191         if (imagetype==CXIMAGE_FORMAT_TIF){
192                 CxImageTIF newima;
193                 newima.Ghost(this);
194                 if (newima.Encode(hFile)){
195                         return true;
196                 } else {
197                         strcpy(info.szLastError,newima.GetLastError());
198                         return false;
199                 }
200         }
201 #endif
202 #if CXIMAGE_SUPPORT_JPG
203         if (imagetype==CXIMAGE_FORMAT_JPG){
204                 CxImageJPG newima;
205                 newima.Ghost(this);
206                 if (newima.Encode(hFile)){
207                         return true;
208                 } else {
209                         strcpy(info.szLastError,newima.GetLastError());
210                         return false;
211                 }
212         }
213 #endif
214 #if CXIMAGE_SUPPORT_GIF
215         if (imagetype==CXIMAGE_FORMAT_GIF){
216                 CxImageGIF newima;
217                 newima.Ghost(this);
218                 if (newima.Encode(hFile)){
219                         return true;
220                 } else {
221                         strcpy(info.szLastError,newima.GetLastError());
222                         return false;
223                 }
224         }
225 #endif
226 #if CXIMAGE_SUPPORT_PNG
227         if (imagetype==CXIMAGE_FORMAT_PNG){
228                 CxImagePNG newima;
229                 newima.Ghost(this);
230                 if (newima.Encode(hFile)){
231                         return true;
232                 } else {
233                         strcpy(info.szLastError,newima.GetLastError());
234                         return false;
235                 }
236         }
237 #endif
238 #if CXIMAGE_SUPPORT_MNG
239         if (imagetype==CXIMAGE_FORMAT_MNG){
240                 CxImageMNG newima;
241                 newima.Ghost(this);
242                 if (newima.Encode(hFile)){
243                         return true;
244                 } else {
245                         strcpy(info.szLastError,newima.GetLastError());
246                         return false;
247                 }
248         }
249 #endif
250 #if CXIMAGE_SUPPORT_TGA
251         if (imagetype==CXIMAGE_FORMAT_TGA){
252                 CxImageTGA newima;
253                 newima.Ghost(this);
254                 if (newima.Encode(hFile)){
255                         return true;
256                 } else {
257                         strcpy(info.szLastError,newima.GetLastError());
258                         return false;
259                 }
260         }
261 #endif
262 #if CXIMAGE_SUPPORT_PCX
263         if (imagetype==CXIMAGE_FORMAT_PCX){
264                 CxImagePCX newima;
265                 newima.Ghost(this);
266                 if (newima.Encode(hFile)){
267                         return true;
268                 } else {
269                         strcpy(info.szLastError,newima.GetLastError());
270                         return false;
271                 }
272         }
273 #endif
274 #if CXIMAGE_SUPPORT_WBMP
275         if (imagetype==CXIMAGE_FORMAT_WBMP){
276                 CxImageWBMP newima;
277                 newima.Ghost(this);
278                 if (newima.Encode(hFile)){
279                         return true;
280                 } else {
281                         strcpy(info.szLastError,newima.GetLastError());
282                         return false;
283                 }
284         }
285 #endif
286 #if CXIMAGE_SUPPORT_WMF && CXIMAGE_SUPPORT_WINDOWS // <vho> - WMF/EMF support
287         if (imagetype==CXIMAGE_FORMAT_WMF){
288                 CxImageWMF newima;
289                 newima.Ghost(this);
290                 if (newima.Encode(hFile)){
291                         return true;
292                 } else {
293                         strcpy(info.szLastError,newima.GetLastError());
294                         return false;
295                 }
296         }
297 #endif
298 #if CXIMAGE_SUPPORT_JBG
299         if (imagetype==CXIMAGE_FORMAT_JBG){
300                 CxImageJBG newima;
301                 newima.Ghost(this);
302                 if (newima.Encode(hFile)){
303                         return true;
304                 } else {
305                         strcpy(info.szLastError,newima.GetLastError());
306                         return false;
307                 }
308         }
309 #endif
310 #if CXIMAGE_SUPPORT_JASPER
311         if (
312  #if    CXIMAGE_SUPPORT_JP2
313                 imagetype==CXIMAGE_FORMAT_JP2 || 
314  #endif
315  #if    CXIMAGE_SUPPORT_JPC
316                 imagetype==CXIMAGE_FORMAT_JPC || 
317  #endif
318  #if    CXIMAGE_SUPPORT_PGX
319                 imagetype==CXIMAGE_FORMAT_PGX || 
320  #endif
321  #if    CXIMAGE_SUPPORT_PNM
322                 imagetype==CXIMAGE_FORMAT_PNM || 
323  #endif
324  #if    CXIMAGE_SUPPORT_RAS
325                 imagetype==CXIMAGE_FORMAT_RAS || 
326  #endif
327                  false ){
328                 CxImageJAS newima;
329                 newima.Ghost(this);
330                 if (newima.Encode(hFile,imagetype)){
331                         return true;
332                 } else {
333                         strcpy(info.szLastError,newima.GetLastError());
334                         return false;
335                 }
336         }
337 #endif
338
339 #if CXIMAGE_SUPPORT_SKA
340         if (imagetype==CXIMAGE_FORMAT_SKA){
341                 CxImageSKA newima;
342                 newima.Ghost(this);
343                 if (newima.Encode(hFile)){
344                         return true;
345                 } else {
346                         strcpy(info.szLastError,newima.GetLastError());
347                         return false;
348                 }
349         }
350 #endif
351
352 #if CXIMAGE_SUPPORT_RAW
353         if (imagetype==CXIMAGE_FORMAT_RAW){
354                 CxImageRAW newima;
355                 newima.Ghost(this);
356                 if (newima.Encode(hFile)){
357                         return true;
358                 } else {
359                         strcpy(info.szLastError,newima.GetLastError());
360                         return false;
361                 }
362         }
363 #endif
364
365         strcpy(info.szLastError,"Encode: Unknown format");
366         return false;
367 }
368 ////////////////////////////////////////////////////////////////////////////////
369 /**
370  * Saves to disk or memory pagecount images, referenced by an array of CxImage pointers.
371  * \param hFile: file handle.
372  * \param pImages: array of CxImage pointers.
373  * \param pagecount: number of images.
374  * \param imagetype: can be CXIMAGE_FORMAT_TIF or CXIMAGE_FORMAT_GIF.
375  * \return true if everything is ok
376  */
377 bool CxImage::Encode(FILE * hFile, CxImage ** pImages, int pagecount, DWORD imagetype)
378 {
379         CxIOFile file(hFile);
380         return Encode(&file, pImages, pagecount,imagetype);
381 }
382 ////////////////////////////////////////////////////////////////////////////////
383 /**
384  * Saves to disk or memory pagecount images, referenced by an array of CxImage pointers.
385  * \param hFile: file handle (CxMemFile or CxIOFile), with write access.
386  * \param pImages: array of CxImage pointers.
387  * \param pagecount: number of images.
388  * \param imagetype: can be CXIMAGE_FORMAT_TIF, CXIMAGE_FORMAT_GIF or CXIMAGE_FORMAT_ICO.
389  * \return true if everything is ok
390  */
391 bool CxImage::Encode(CxFile * hFile, CxImage ** pImages, int pagecount, DWORD imagetype)
392 {
393 #if CXIMAGE_SUPPORT_TIF
394         if (imagetype==CXIMAGE_FORMAT_TIF){
395                 CxImageTIF newima;
396                 newima.Ghost(this);
397                 if (newima.Encode(hFile,pImages,pagecount)){
398                         return true;
399                 } else {
400                         strcpy(info.szLastError,newima.GetLastError());
401                         return false;
402                 }
403         }
404 #endif
405 #if CXIMAGE_SUPPORT_GIF
406         if (imagetype==CXIMAGE_FORMAT_GIF){
407                 CxImageGIF newima;
408                 newima.Ghost(this);
409                 if (newima.Encode(hFile,pImages,pagecount)){
410                         return true;
411                 } else {
412                         strcpy(info.szLastError,newima.GetLastError());
413                         return false;
414                 }
415         }
416 #endif
417 #if CXIMAGE_SUPPORT_ICO
418         if (imagetype==CXIMAGE_FORMAT_ICO){
419                 CxImageICO newima;
420                 newima.Ghost(this);
421                 if (newima.Encode(hFile,pImages,pagecount)){
422                         return true;
423                 } else {
424                         strcpy(info.szLastError,newima.GetLastError());
425                         return false;
426                 }
427         }
428 #endif
429         strcpy(info.szLastError,"Multipage Encode, Unsupported operation for this format");
430         return false;
431 }
432
433 ////////////////////////////////////////////////////////////////////////////////
434 /**
435  * exports the image into a RGBA buffer, Useful for OpenGL applications.
436  * \param buffer: output memory buffer pointer. Must be NULL,
437  * the function allocates and fill the memory,
438  * the application must free the buffer, see also FreeMemory().
439  * \param size: output memory buffer size.
440  * \param bFlipY: direction of Y axis. default = false.
441  * \return true if everything is ok
442  */
443 bool CxImage::Encode2RGBA(BYTE * &buffer, long &size, bool bFlipY)
444 {
445         if (buffer!=NULL){
446                 strcpy(info.szLastError,"the buffer must be empty");
447                 return false;
448         }
449         CxMemFile file;
450         file.Open();
451         if(Encode2RGBA(&file,bFlipY)){
452                 buffer=file.GetBuffer();
453                 size=file.Size();
454                 return true;
455         }
456         return false;
457 }
458 ////////////////////////////////////////////////////////////////////////////////
459 /**
460  * exports the image into a RGBA buffer, Useful for OpenGL applications.
461  * \param hFile: file handle (CxMemFile or CxIOFile), with write access.
462  * \param bFlipY: direction of Y axis. default = false.
463  * \return true if everything is ok
464  */
465 bool CxImage::Encode2RGBA(CxFile *hFile, bool bFlipY)
466 {
467         if (EncodeSafeCheck(hFile)) return false;
468
469         for (long y1 = 0; y1 < head.biHeight; y1++) {
470                 long y = bFlipY ? head.biHeight - 1 - y1 : y1;
471                 for(long x = 0; x < head.biWidth; x++) {
472                         RGBQUAD color = BlindGetPixelColor(x,y);
473                         hFile->PutC(color.rgbRed);
474                         hFile->PutC(color.rgbGreen);
475                         hFile->PutC(color.rgbBlue);
476                         hFile->PutC(color.rgbReserved);
477                 }
478         }
479         return true;
480 }
481
482 ////////////////////////////////////////////////////////////////////////////////
483 #endif //CXIMAGE_SUPPORT_ENCODE
484 ////////////////////////////////////////////////////////////////////////////////
485
486 ////////////////////////////////////////////////////////////////////////////////
487 #if CXIMAGE_SUPPORT_DECODE
488 ////////////////////////////////////////////////////////////////////////////////
489 // For UNICODE support: char -> TCHAR
490 /**
491  * Reads from disk the image in a specific format.
492  * - If decoding fails using the specified image format,
493  * the function will try the automatic file format recognition.
494  *
495  * \param filename: file name
496  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
497  * \return true if everything is ok
498  */
499 bool CxImage::Load(const TCHAR * filename, DWORD imagetype)
500 //bool CxImage::Load(const char * filename, DWORD imagetype)
501 {
502         /*FILE* hFile;  //file handle to read the image
503         if ((hFile=fopen(filename,"rb"))==NULL)  return false;
504         bool bOK = Decode(hFile,imagetype);
505         fclose(hFile);*/
506
507         /* automatic file type recognition */
508         bool bOK = false;
509         if ( GetTypeIndexFromId(imagetype) ){
510                 FILE* hFile;    //file handle to read the image
511
512 #ifdef WIN32
513                 if ((hFile=_tfopen(filename,_T("rb")))==NULL)  return false;    // For UNICODE support
514 #else
515                 if ((hFile=fopen(filename,"rb"))==NULL)  return false;
516 #endif
517
518                 bOK = Decode(hFile,imagetype);
519                 fclose(hFile);
520                 if (bOK) return bOK;
521         }
522
523         char szError[256];
524         strcpy(szError,info.szLastError); //save the first error
525
526         // if failed, try automatic recognition of the file...
527         FILE* hFile;
528
529 #ifdef WIN32
530         if ((hFile=_tfopen(filename,_T("rb")))==NULL)  return false;    // For UNICODE support
531 #else
532         if ((hFile=fopen(filename,"rb"))==NULL)  return false;
533 #endif
534
535         bOK = Decode(hFile,CXIMAGE_FORMAT_UNKNOWN);
536         fclose(hFile);
537
538         if (!bOK && imagetype > 0) strcpy(info.szLastError,szError); //restore the first error
539
540         return bOK;
541 }
542 ////////////////////////////////////////////////////////////////////////////////
543 #ifdef WIN32
544 //bool CxImage::Load(LPCWSTR filename, DWORD imagetype)
545 //{
546 //      /*FILE* hFile;  //file handle to read the image
547 //      if ((hFile=_wfopen(filename, L"rb"))==NULL)  return false;
548 //      bool bOK = Decode(hFile,imagetype);
549 //      fclose(hFile);*/
550 //
551 //      /* automatic file type recognition */
552 //      bool bOK = false;
553 //      if ( GetTypeIndexFromId(imagetype) ){
554 //              FILE* hFile;    //file handle to read the image
555 //              if ((hFile=_wfopen(filename,L"rb"))==NULL)  return false;
556 //              bOK = Decode(hFile,imagetype);
557 //              fclose(hFile);
558 //              if (bOK) return bOK;
559 //      }
560 //
561 //      char szError[256];
562 //      strcpy(szError,info.szLastError); //save the first error
563 //
564 //      // if failed, try automatic recognition of the file...
565 //      FILE* hFile;    //file handle to read the image
566 //      if ((hFile=_wfopen(filename,L"rb"))==NULL)  return false;
567 //      bOK = Decode(hFile,CXIMAGE_FORMAT_UNKNOWN);
568 //      fclose(hFile);
569 //
570 //      if (!bOK && imagetype > 0) strcpy(info.szLastError,szError); //restore the first error
571 //
572 //      return bOK;
573 //}
574 ////////////////////////////////////////////////////////////////////////////////
575 /**
576  * Loads an image from the application resources.
577  * \param hRes: the resource handle returned by FindResource().
578  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS.
579  * \param hModule: NULL for internal resource, or external application/DLL hinstance returned by LoadLibray.
580  * \return true if everything is ok
581  */
582 bool CxImage::LoadResource(HRSRC hRes, DWORD imagetype, HMODULE hModule)
583 {
584         DWORD rsize=SizeofResource(hModule,hRes);
585         HGLOBAL hMem=::LoadResource(hModule,hRes);
586         if (hMem){
587                 char* lpVoid=(char*)LockResource(hMem);
588                 if (lpVoid){
589                         // FILE* fTmp=tmpfile(); doesn't work with network
590                         /*char tmpPath[MAX_PATH] = {0};
591                         char tmpFile[MAX_PATH] = {0};
592                         GetTempPath(MAX_PATH,tmpPath);
593                         GetTempFileName(tmpPath,"IMG",0,tmpFile);
594                         FILE* fTmp=fopen(tmpFile,"w+b");
595                         if (fTmp){
596                                 fwrite(lpVoid,rsize,1,fTmp);
597                                 fseek(fTmp,0,SEEK_SET);
598                                 bool bOK = Decode(fTmp,imagetype);
599                                 fclose(fTmp);
600                                 DeleteFile(tmpFile);
601                                 return bOK;
602                         }*/
603
604                         CxMemFile fTmp((BYTE*)lpVoid,rsize);
605                         return Decode(&fTmp,imagetype);
606                 }
607         } else strcpy(info.szLastError,"Unable to load resource!");
608         return false;
609 }
610 #endif //WIN32
611 ////////////////////////////////////////////////////////////////////////////////
612 /**
613  * Constructor from file name, see Load()
614  * \param filename: file name
615  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
616  */
617 // 
618 // > filename: file name
619 // > imagetype: specify the image format (CXIMAGE_FORMAT_BMP,...)
620 // For UNICODE support: char -> TCHAR
621 CxImage::CxImage(const TCHAR * filename, DWORD imagetype)
622 //CxImage::CxImage(const char * filename, DWORD imagetype)
623 {
624         Startup(imagetype);
625         Load(filename,imagetype);
626 }
627 ////////////////////////////////////////////////////////////////////////////////
628 /**
629  * Constructor from file handle, see Decode()
630  * \param stream: file handle, with read access.
631  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
632  */
633 CxImage::CxImage(FILE * stream, DWORD imagetype)
634 {
635         Startup(imagetype);
636         Decode(stream,imagetype);
637 }
638 ////////////////////////////////////////////////////////////////////////////////
639 /**
640  * Constructor from CxFile object, see Decode()
641  * \param stream: file handle (CxMemFile or CxIOFile), with read access.
642  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
643  */
644 CxImage::CxImage(CxFile * stream, DWORD imagetype)
645 {
646         Startup(imagetype);
647         Decode(stream,imagetype);
648 }
649 ////////////////////////////////////////////////////////////////////////////////
650 /**
651  * Constructor from memory buffer, see Decode()
652  * \param buffer: memory buffer
653  * \param size: size of buffer
654  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
655  */
656 CxImage::CxImage(BYTE * buffer, DWORD size, DWORD imagetype)
657 {
658         Startup(imagetype);
659         CxMemFile stream(buffer,size);
660         Decode(&stream,imagetype);
661 }
662 ////////////////////////////////////////////////////////////////////////////////
663 /**
664  * Loads an image from memory buffer
665  * \param buffer: memory buffer
666  * \param size: size of buffer
667  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
668  * \return true if everything is ok
669  */
670 bool CxImage::Decode(BYTE * buffer, DWORD size, DWORD imagetype)
671 {
672         CxMemFile file(buffer,size);
673         return Decode(&file,imagetype);
674 }
675 ////////////////////////////////////////////////////////////////////////////////
676 /**
677  * Loads an image from file handle.
678  * \param hFile: file handle, with read access.
679  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
680  * \return true if everything is ok
681  */
682 bool CxImage::Decode(FILE *hFile, DWORD imagetype)
683 {
684         CxIOFile file(hFile);
685         return Decode(&file,imagetype);
686 }
687 ////////////////////////////////////////////////////////////////////////////////
688 /**
689  * Loads an image from CxFile object
690  * \param hFile: file handle (CxMemFile or CxIOFile), with read access.
691  * \param imagetype: file format, see ENUM_CXIMAGE_FORMATS
692  * \return true if everything is ok
693  * \sa ENUM_CXIMAGE_FORMATS
694  */
695 bool CxImage::Decode(CxFile *hFile, DWORD imagetype)
696 {
697         if (hFile == NULL){
698                 strcpy(info.szLastError,CXIMAGE_ERR_NOFILE);
699                 return false;
700         }
701
702         if (imagetype==CXIMAGE_FORMAT_UNKNOWN){
703                 DWORD pos = hFile->Tell();
704 #if CXIMAGE_SUPPORT_BMP
705                 { CxImageBMP newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
706 #endif
707 #if CXIMAGE_SUPPORT_JPG
708                 { CxImageJPG newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
709 #endif
710 #if CXIMAGE_SUPPORT_ICO
711                 { CxImageICO newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
712 #endif
713 #if CXIMAGE_SUPPORT_GIF
714                 { CxImageGIF newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
715 #endif
716 #if CXIMAGE_SUPPORT_PNG
717                 { CxImagePNG newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
718 #endif
719 #if CXIMAGE_SUPPORT_TIF
720                 { CxImageTIF newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
721 #endif
722 #if CXIMAGE_SUPPORT_MNG
723                 { CxImageMNG newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
724 #endif
725 #if CXIMAGE_SUPPORT_TGA
726                 { CxImageTGA newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
727 #endif
728 #if CXIMAGE_SUPPORT_PCX
729                 { CxImagePCX newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
730 #endif
731 #if CXIMAGE_SUPPORT_WBMP
732                 { CxImageWBMP newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
733 #endif
734 #if CXIMAGE_SUPPORT_WMF && CXIMAGE_SUPPORT_WINDOWS
735                 { CxImageWMF newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
736 #endif
737 #if CXIMAGE_SUPPORT_JBG
738                 { CxImageJBG newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
739 #endif
740 #if CXIMAGE_SUPPORT_JASPER
741                 { CxImageJAS newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
742 #endif
743 #if CXIMAGE_SUPPORT_SKA
744                 { CxImageSKA newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
745 #endif
746 #if CXIMAGE_SUPPORT_RAW
747                 { CxImageRAW newima; newima.CopyInfo(*this); if (newima.Decode(hFile)) { Transfer(newima); return true; } else hFile->Seek(pos,SEEK_SET); }
748 #endif
749         }
750
751 #if CXIMAGE_SUPPORT_BMP
752         if (imagetype==CXIMAGE_FORMAT_BMP){
753                 CxImageBMP newima;
754                 newima.CopyInfo(*this);
755                 if (newima.Decode(hFile)){
756                         Transfer(newima);
757                         return true;
758                 } else {
759                         strcpy(info.szLastError,newima.GetLastError());
760                         return false;
761                 }
762         }
763 #endif
764 #if CXIMAGE_SUPPORT_JPG
765         if (imagetype==CXIMAGE_FORMAT_JPG){
766                 CxImageJPG newima;
767                 newima.CopyInfo(*this); // <ignacio>
768                 if (newima.Decode(hFile)){
769                         Transfer(newima);
770                         return true;
771                 } else {
772                         strcpy(info.szLastError,newima.GetLastError());
773                         return false;
774                 }
775         }
776 #endif
777 #if CXIMAGE_SUPPORT_ICO
778         if (imagetype==CXIMAGE_FORMAT_ICO){
779                 CxImageICO newima;
780                 newima.CopyInfo(*this);
781                 if (newima.Decode(hFile)){
782                         Transfer(newima);
783                         return true;
784                 } else {
785                         info.nNumFrames = newima.info.nNumFrames;
786                         strcpy(info.szLastError,newima.GetLastError());
787                         return false;
788                 }
789         }
790 #endif
791 #if CXIMAGE_SUPPORT_GIF
792         if (imagetype==CXIMAGE_FORMAT_GIF){
793                 CxImageGIF newima;
794                 newima.CopyInfo(*this);
795                 if (newima.Decode(hFile)){
796                         Transfer(newima);
797                         return true;
798                 } else {
799                         info.nNumFrames = newima.info.nNumFrames;
800                         strcpy(info.szLastError,newima.GetLastError());
801                         return false;
802                 }
803         }
804 #endif
805 #if CXIMAGE_SUPPORT_PNG
806         if (imagetype==CXIMAGE_FORMAT_PNG){
807                 CxImagePNG newima;
808                 newima.CopyInfo(*this);
809                 if (newima.Decode(hFile)){
810                         Transfer(newima);
811                         return true;
812                 } else {
813                         strcpy(info.szLastError,newima.GetLastError());
814                         return false;
815                 }
816         }
817 #endif
818 #if CXIMAGE_SUPPORT_TIF
819         if (imagetype==CXIMAGE_FORMAT_TIF){
820                 CxImageTIF newima;
821                 newima.CopyInfo(*this);
822                 if (newima.Decode(hFile)){
823                         Transfer(newima);
824                         return true;
825                 } else {
826                         info.nNumFrames = newima.info.nNumFrames;
827                         strcpy(info.szLastError,newima.GetLastError());
828                         return false;
829                 }
830         }
831 #endif
832 #if CXIMAGE_SUPPORT_MNG
833         if (imagetype==CXIMAGE_FORMAT_MNG){
834                 CxImageMNG newima;
835                 newima.CopyInfo(*this);
836                 if (newima.Decode(hFile)){
837                         Transfer(newima);
838                         return true;
839                 } else {
840                         info.nNumFrames = newima.info.nNumFrames;
841                         strcpy(info.szLastError,newima.GetLastError());
842                         return false;
843                 }
844         }
845 #endif
846 #if CXIMAGE_SUPPORT_TGA
847         if (imagetype==CXIMAGE_FORMAT_TGA){
848                 CxImageTGA newima;
849                 newima.CopyInfo(*this);
850                 if (newima.Decode(hFile)){
851                         Transfer(newima);
852                         return true;
853                 } else {
854                         strcpy(info.szLastError,newima.GetLastError());
855                         return false;
856                 }
857         }
858 #endif
859 #if CXIMAGE_SUPPORT_PCX
860         if (imagetype==CXIMAGE_FORMAT_PCX){
861                 CxImagePCX newima;
862                 newima.CopyInfo(*this);
863                 if (newima.Decode(hFile)){
864                         Transfer(newima);
865                         return true;
866                 } else {
867                         strcpy(info.szLastError,newima.GetLastError());
868                         return false;
869                 }
870         }
871 #endif
872 #if CXIMAGE_SUPPORT_WBMP
873         if (imagetype==CXIMAGE_FORMAT_WBMP){
874                 CxImageWBMP newima;
875                 newima.CopyInfo(*this);
876                 if (newima.Decode(hFile)){
877                         Transfer(newima);
878                         return true;
879                 } else {
880                         strcpy(info.szLastError,newima.GetLastError());
881                         return false;
882                 }
883         }
884 #endif
885 #if CXIMAGE_SUPPORT_WMF && CXIMAGE_SUPPORT_WINDOWS // vho - WMF support
886         if (imagetype == CXIMAGE_FORMAT_WMF){
887                 CxImageWMF newima;
888                 newima.CopyInfo(*this);
889                 if (newima.Decode(hFile)){
890                         Transfer(newima);
891                         return true;
892                 } else {
893                         strcpy(info.szLastError,newima.GetLastError());
894                         return false;
895                 }
896         }
897 #endif
898 #if CXIMAGE_SUPPORT_JBG
899         if (imagetype==CXIMAGE_FORMAT_JBG){
900                 CxImageJBG newima;
901                 newima.CopyInfo(*this);
902                 if (newima.Decode(hFile)){
903                         Transfer(newima);
904                         return true;
905                 } else {
906                         strcpy(info.szLastError,newima.GetLastError());
907                         return false;
908                 }
909         }
910 #endif
911 #if CXIMAGE_SUPPORT_JASPER
912         if (
913  #if    CXIMAGE_SUPPORT_JP2
914                 imagetype==CXIMAGE_FORMAT_JP2 || 
915  #endif
916  #if    CXIMAGE_SUPPORT_JPC
917                 imagetype==CXIMAGE_FORMAT_JPC || 
918  #endif
919  #if    CXIMAGE_SUPPORT_PGX
920                 imagetype==CXIMAGE_FORMAT_PGX || 
921  #endif
922  #if    CXIMAGE_SUPPORT_PNM
923                 imagetype==CXIMAGE_FORMAT_PNM || 
924  #endif
925  #if    CXIMAGE_SUPPORT_RAS
926                 imagetype==CXIMAGE_FORMAT_RAS || 
927  #endif
928                  false ){
929                 CxImageJAS newima;
930                 newima.CopyInfo(*this);
931                 if (newima.Decode(hFile,imagetype)){
932                         Transfer(newima);
933                         return true;
934                 } else {
935                         strcpy(info.szLastError,newima.GetLastError());
936                         return false;
937                 }
938         }
939 #endif
940 #if CXIMAGE_SUPPORT_SKA
941         if (imagetype==CXIMAGE_FORMAT_SKA){
942                 CxImageSKA newima;
943                 newima.CopyInfo(*this);
944                 if (newima.Decode(hFile)){
945                         Transfer(newima);
946                         return true;
947                 } else {
948                         strcpy(info.szLastError,newima.GetLastError());
949                         return false;
950                 }
951         }
952 #endif
953
954 #if CXIMAGE_SUPPORT_RAW
955         if (imagetype==CXIMAGE_FORMAT_RAW){
956                 CxImageRAW newima;
957                 newima.CopyInfo(*this);
958                 if (newima.Decode(hFile)){
959                         Transfer(newima);
960                         return true;
961                 } else {
962                         strcpy(info.szLastError,newima.GetLastError());
963                         return false;
964                 }
965         }
966 #endif
967
968         strcpy(info.szLastError,"Decode: Unknown or wrong format");
969         return false;
970 }
971 ////////////////////////////////////////////////////////////////////////////////
972 /**
973  * Loads an image from CxFile object
974  * \param hFile: file handle (CxMemFile or CxIOFile), with read access.
975  * \param imagetype: file format, default = 0 (CXIMAGE_FORMAT_UNKNOWN)
976  * \return : if imagetype is not 0, the function returns true when imagetype
977  *  matches the file image format. If imagetype is 0, the function returns true
978  *  when the file image format is recognized as a supported format.
979  *  If the returned value is true, use GetHeight(), GetWidth() or GetType()
980  *  to retrieve the basic image information.
981  * \sa ENUM_CXIMAGE_FORMATS
982  */
983 bool CxImage::CheckFormat(CxFile * hFile, DWORD imagetype)
984 {
985         SetType(CXIMAGE_FORMAT_UNKNOWN);
986         SetEscape(-1);
987
988         if (!Decode(hFile,imagetype))
989                 return false;
990
991         if (GetType() == CXIMAGE_FORMAT_UNKNOWN || GetType() != imagetype)
992                 return false;
993
994         return true;
995 }
996 ////////////////////////////////////////////////////////////////////////////////
997 bool CxImage::CheckFormat(BYTE * buffer, DWORD size, DWORD imagetype)
998 {
999         if (buffer==NULL || size==NULL){
1000                 strcpy(info.szLastError,"invalid or empty buffer");
1001                 return false;
1002         }
1003         CxMemFile file(buffer,size);
1004         return CheckFormat(&file,imagetype);
1005 }
1006 ////////////////////////////////////////////////////////////////////////////////
1007 #endif //CXIMAGE_SUPPORT_DECODE
1008 ////////////////////////////////////////////////////////////////////////////////