00001 /* 00002 /-------------------------------------------------------------------- 00003 | 00004 | $Id: plpicdec_8h-source.html,v 1.4 2004/09/15 15:26:29 uzadow Exp $ 00005 | 00006 | Copyright (c) 1996-2002 Ulrich von Zadow 00007 | 00008 \-------------------------------------------------------------------- 00009 */ 00010 00011 #ifndef INCL_PLPICDEC 00012 #define INCL_PLPICDEC 00013 00014 #ifndef INCL_PLBITMAP 00015 #include "plbitmap.h" 00016 #endif 00017 00018 #ifndef INCL_PLDATASRC 00019 #include "pldatasrc.h" 00020 #endif 00021 00022 //! PLPicDecoder is an abstract base class. It defines common routines 00023 //! for all decoders. Decoders for specific file formats can be 00024 //! derived from this class. Objects of this class interact with a 00025 //! PLDataSource to decode bitmaps. 00026 class PLPicDecoder : public PLBmpInfo 00027 { 00028 00029 public: 00030 //! Empty constructor. The actual initialization takes place in a 00031 //! derived class. 00032 PLPicDecoder 00033 (); 00034 00035 //! Destructor. Frees memory allocated. 00036 virtual ~PLPicDecoder 00037 (); 00038 00039 //! Decodes a picture in a file and stores the results in pBmp. 00040 //! BPPWanted is the number of bits per pixel in the bitmap 00041 //! returned. Valid values for BPPWanted are 8, 16, 24, 32 and 0. 00042 //! 0 will result in either an 8 or a 32 bpp bitmap being returned, 00043 //! depending on the source bitmap. Of course, if BPPWanted is less than the 00044 //! bpp of the file, some data is lost. 00045 //! Note that internally, all data is decoded to 8 or 32 bpp first in all 00046 //! cases, so using 16 or 24 as BPPWanted will cause in a copy and format 00047 //! conversion operation. 00048 //! 00049 //! As an exception, BPPWanted can be 1 for b/w tiff files. 00050 virtual void MakeBmpFromFile 00051 ( const char * pszFName, 00052 PLBmp * pBmp, 00053 const PLPixelFormat& pfWanted = PLPixelFormat::DONTCARE, 00054 PLIProgressNotification * pProgNot = NULL 00055 ); 00056 00057 #ifdef _WINDOWS 00058 //! Decodes a picture in a file and stores the results in pBmp. 00059 //! Wide-Character version (windows only). 00060 virtual void MakeBmpFromFileW 00061 ( const wchar_t * pszwFName, 00062 PLBmp * pBmp, 00063 const PLPixelFormat& pf = PLPixelFormat::DONTCARE, 00064 PLIProgressNotification * pProgNot = NULL 00065 ); 00066 #endif 00067 00068 00069 //! Decodes a picture in an url (http://, ftp:://, ...) and stores the results in pBmp. 00070 //! BPPWanted is the number of bits per pixel in the bitmap 00071 //! returned. Valid values for BPPWanted are 8, 16, 24, 32 and 0. 00072 //! 0 will result in either an 8 or a 32 bpp bitmap being returned, 00073 //! depending on the source bitmap. Of course, if BPPWanted is less than the 00074 //! bpp of the file, some data is lost. 00075 //! Note that internally, all data is decoded to 8 or 32 bpp first in all 00076 //! cases, so using 16 or 24 as BPPWanted will cause in a copy and format 00077 //! conversion operation. 00078 //! 00079 //! As an exception, BPPWanted can be 1 for b/w tiff files. 00080 virtual void MakeBmpFromURL 00081 ( const char * pszURL, 00082 PLBmp * pBmp, 00083 const PLPixelFormat& pf = PLPixelFormat::DONTCARE, 00084 PLIProgressNotification * pProgNot = NULL 00085 ); 00086 00087 #ifdef _WINDOWS 00088 //! Decodes a picture in a windows resource by creating a resource data 00089 //! source and calling MakeBmp with this data source. 00090 //! BPPWanted is the number of bits per pixel in the bitmap 00091 //! returned. Valid values for BPPWanted are 8, 16, 24, 32 and 0. 00092 //! 0 will result in either an 8 or a 32 bpp bitmap being returned, 00093 //! depending on the source bitmap. Of course, if BPPWanted is less than the 00094 //! bpp of the file, some data is lost. 00095 //! Note that internally, all data is decoded to 8 or 32 bpp first in all 00096 //! cases, so using 16 or 24 as BPPWanted will cause in a copy and format 00097 //! conversion operation. 00098 //! 00099 //! As an exception, BPPWanted can be 1 for b/w tiff files. 00100 //! 00101 //! ResType is the windows resource type (e.g. "JPEG") to be used. 00102 //! If a module handle is passed as hResModule, then the resource 00103 //! will be loaded from this module, otherwise, the resource 00104 //! will be searched for by AfxFindResourceHandle (! MFC dependent!) 00105 virtual void MakeBmpFromResource 00106 ( HINSTANCE hInstResource, int ResourceID, 00107 PLBmp * pBmp, 00108 const PLPixelFormat& pf = PLPixelFormat::DONTCARE, 00109 const char* ResType = NULL, 00110 HMODULE hResModule = 0 00111 ); 00112 #endif 00113 00114 //! Decodes a picture from a memory location which directly resembles 00115 //! the image file as it would be on disc. The result is stored in pBmp. 00116 //! BPPWanted is the number of bits per pixel in the bitmap 00117 //! returned. Valid values for BPPWanted are 8, 16, 24, 32 and 0. 00118 //! 0 will result in either an 8 or a 32 bpp bitmap being returned, 00119 //! depending on the source bitmap. Of course, if BPPWanted is less than the 00120 //! bpp of the file, some data is lost. 00121 //! Note that internally, all data is decoded to 8 or 32 bpp first in all 00122 //! cases, so using 16 or 24 as BPPWanted will cause in a copy and format 00123 //! conversion operation. 00124 //! 00125 //! As an exception, BPPWanted can be 1 for b/w tiff files. 00126 virtual void MakeBmpFromMemory 00127 ( unsigned char * ucMemSrc, 00128 int MemSrcSize, 00129 PLBmp * pBmp, 00130 const PLPixelFormat& pf = PLPixelFormat::DONTCARE, 00131 PLIProgressNotification * pProgNot = NULL 00132 ); 00133 00134 //! 00135 void OpenFile 00136 ( const char * pszFName, 00137 PLIProgressNotification * pProgNot = NULL 00138 ); 00139 00140 #ifdef _WINDOWS 00141 //! 00142 void OpenFileW 00143 ( const wchar_t * pszwFName, 00144 PLIProgressNotification * pProgNot = NULL 00145 ); 00146 #endif 00147 //! Decodes an image in an already-opened data source and 00148 //! stores the results in pBmp. 00149 //! Open should be called before, close after this routine is called. 00150 virtual void MakeBmp 00151 ( PLBmp * pBmp, 00152 const PLPixelFormat& pf = PLPixelFormat::DONTCARE 00153 ); 00154 00155 //! 00156 virtual void Close 00157 (); 00158 00159 //! Sets the amount and destination of debug traces output by the 00160 //! debug version. pszFName contains either a valid file name or is 00161 //! NULL. If it contains a file name, this file is used to store 00162 //! debug information. If pszFName is NULL, the destination is 00163 //! either the MSVC debug console or stderr depending on the 00164 //! version of the library. Valid values for Level are: <BR> 00165 //! <BR> 00166 //! 0: Trace only errors.<BR> 00167 //! 1: Trace top-level calls.<BR> 00168 //! 2: Trace picture format information<BR> 00169 //! 3: Trace all miscellaneous info.<BR> 00170 //! <BR> 00171 //! The trace configuration is global to all decoders. 00172 static void SetTraceConfig 00173 ( int Level, 00174 char * pszFName 00175 ); 00176 00177 //! 00178 virtual void Open (PLDataSource * pDataSrc)=0; 00179 00180 //! Decodes an image. Assumes that Open() and Bmp.Create() have already 00181 //! been called. 00182 virtual void GetImage (PLBmpBase & Bmp) = 0; 00183 00184 // Used only by PLAnyPicDecoder. 00185 void SetDataSrc (PLDataSource * pDataSrc); 00186 00187 // This function is needed by callbacks outside of any object, 00188 // so it's public and static. It should not be called from 00189 // outside of the library. 00190 static void raiseError 00191 ( int Code, 00192 char * pszErr 00193 ); 00194 00195 //! Called to output status messages to the current debug console 00196 static void Trace 00197 ( int TraceLevel, 00198 const char * pszMessage 00199 ); 00200 00201 protected: 00202 // TODO: What is this doing here?! 00203 PLBYTE * unpackPictRow 00204 ( PLBYTE * pLineBuf, 00205 PLDataSource * pDataSrc, 00206 int Width, 00207 int rowBytes, 00208 int SrcBytes 00209 ); 00210 00211 PLBYTE ReadByte 00212 ( PLDataSource * pDataSrc 00213 ); 00214 00215 // Machine-independent routines for byte-order conversion. 00216 00217 PLWORD ReadIWord 00218 ( PLDataSource * pDataSrc 00219 ); 00220 00221 PLWORD ReadMWord 00222 ( PLDataSource * pDataSrc 00223 ); 00224 00225 PLLONG ReadILong 00226 ( PLDataSource * pDataSrc 00227 ); 00228 00229 PLLONG ReadMLong 00230 ( PLDataSource * pDataSrc 00231 ); 00232 00233 PLDataSource * m_pDataSrc; 00234 00235 private: 00236 /////////////////////////////////////////////////////////////////// 00237 // Member variables. 00238 00239 static int m_TraceLevel; // 0: Trace only errors. 00240 // 1: Trace top-level calls. 00241 // 2: Trace picture format info. 00242 // 3: Trace misc. info. 00243 static char * m_pszTraceFName; // Name of trace file. NULL if trace to 00244 // MSVC debug console. 00245 }; 00246 00247 00248 inline PLBYTE PLPicDecoder::ReadByte 00249 ( PLDataSource * pDataSrc 00250 ) 00251 { 00252 return *(pDataSrc->ReadNBytes (1)); 00253 } 00254 00255 00256 inline PLWORD PLPicDecoder::ReadIWord 00257 ( PLDataSource * pDataSrc 00258 ) 00259 { 00260 PLBYTE * pData = pDataSrc->Read2Bytes (); 00261 // This should work regardless of the destination byte order ;-) 00262 return pData[0] + (pData[1]<<8); 00263 } 00264 00265 00266 inline PLWORD PLPicDecoder::ReadMWord 00267 ( PLDataSource * pDataSrc 00268 ) 00269 { 00270 PLBYTE * pData = pDataSrc->Read2Bytes (); 00271 return pData[1] + (pData[0]<<8); 00272 } 00273 00274 00275 inline PLLONG PLPicDecoder::ReadILong 00276 ( PLDataSource * pDataSrc 00277 ) 00278 { 00279 PLBYTE * pData = pDataSrc->Read4Bytes (); 00280 return pData[0] + (pData[1]<<8) + (pData[2]<<16) + (pData[3]<<24); 00281 } 00282 00283 inline PLLONG PLPicDecoder::ReadMLong 00284 ( PLDataSource * pDataSrc 00285 ) 00286 { 00287 PLBYTE * pData = pDataSrc->Read4Bytes (); 00288 return pData[3] + (pData[2]<<8) + (pData[1]<<16) + (pData[0]<<24); 00289 } 00290 00291 #endif 00292 /* 00293 /-------------------------------------------------------------------- 00294 | 00295 | $Log: plpicdec_8h-source.html,v $ 00295 | Revision 1.4 2004/09/15 15:26:29 uzadow 00295 | Linux compatibility changes, doc update. 00295 | 00296 | Revision 1.15 2004/09/09 16:52:49 artcom 00297 | refactored PixelFormat 00298 | 00299 | Revision 1.14 2004/06/19 20:13:17 uzadow 00300 | Documentation update. 00301 | 00302 | Revision 1.13 2004/06/19 17:34:27 uzadow 00303 | Documentation update 00304 | 00305 | Revision 1.12 2004/06/19 16:49:07 uzadow 00306 | Changed GetImage so it works with PLBmpBase 00307 | 00308 | Revision 1.11 2004/06/06 12:56:38 uzadow 00309 | Doxygenified documentation. 00310 | 00311 | Revision 1.10 2003/08/03 12:54:10 uzadow 00312 | Fixed broken linux build. 00313 | 00314 | Revision 1.9 2003/08/03 12:03:20 uzadow 00315 | Added unicode support; fixed some header includes. 00316 | 00317 | Revision 1.8 2003/03/19 14:33:13 uzadow 00318 | Added Rect.Contains 00319 | 00320 | Revision 1.7 2003/02/15 21:26:58 uzadow 00321 | Added win32 version of url data source. 00322 | 00323 | Revision 1.6 2002/08/04 20:08:01 uzadow 00324 | Added PLBmpInfo class, ability to extract metainformation from images without loading the whole image and proper greyscale support. 00325 | 00326 | Revision 1.5 2002/03/06 22:46:54 uzadow 00327 | Fixed major PLAnyDec bug 00328 | 00329 | Revision 1.4 2002/03/03 16:29:55 uzadow 00330 | Re-added BPPWanted. 00331 | 00332 | Revision 1.3 2001/10/21 17:12:40 uzadow 00333 | Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel. 00334 | 00335 | Revision 1.2 2001/10/06 22:03:26 uzadow 00336 | Added PL prefix to basic data types. 00337 | 00338 | Revision 1.1 2001/09/16 19:03:22 uzadow 00339 | Added global name prefix PL, changed most filenames. 00340 | 00341 | Revision 1.8 2000/12/18 22:42:52 uzadow 00342 | Replaced RGBAPIXEL with PLPixel32. 00343 | 00344 | Revision 1.7 2000/03/30 21:24:15 Ulrich von Zadow 00345 | Added MakeBmpFromMemory() function by Markus Ewald 00346 | 00347 | Revision 1.6 2000/01/16 20:43:14 anonymous 00348 | Removed MFC dependencies 00349 | 00350 | Revision 1.5 2000/01/11 21:40:30 Ulrich von Zadow 00351 | Added instance handle parameter to LoadFromResource() 00352 | 00353 | Revision 1.4 2000/01/08 15:51:30 Ulrich von Zadow 00354 | Misc. modifications to png encoder. 00355 | 00356 | Revision 1.3 1999/11/08 22:12:51 Ulrich von Zadow 00357 | Andreas Koepf: Added resource type as parameter to 00358 | MakeBmpFromResource 00359 | 00360 | 00361 \-------------------------------------------------------------------- 00362 */