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