3 * Purpose: Platform Independent WBMP Image Class Loader and Writer
\r
4 * 12/Jul/2002 Davide Pizzolato - www.xdp.it
\r
5 * CxImage version 6.0.0 02/Feb/2008
\r
8 #include "ximawbmp.h"
\r
10 #if CXIMAGE_SUPPORT_WBMP
\r
12 #include "ximaiter.h"
\r
14 ////////////////////////////////////////////////////////////////////////////////
\r
15 #if CXIMAGE_SUPPORT_DECODE
\r
16 ////////////////////////////////////////////////////////////////////////////////
\r
17 bool CxImageWBMP::Decode(CxFile *hFile)
\r
19 if (hFile == NULL) return false;
\r
21 WBMPHEADER wbmpHead;
\r
25 ReadOctet(hFile, &wbmpHead.Type);
\r
28 ReadOctet(hFile, &dat);
\r
29 wbmpHead.FixHeader = (BYTE)dat;
\r
31 ReadOctet(hFile, &wbmpHead.ImageWidth);
\r
32 ReadOctet(hFile, &wbmpHead.ImageHeight);
\r
35 cx_throw("Not a WBMP");
\r
37 if (wbmpHead.Type != 0)
\r
38 cx_throw("Unsupported WBMP type");
\r
40 head.biWidth = wbmpHead.ImageWidth;
\r
41 head.biHeight= wbmpHead.ImageHeight;
\r
43 if (head.biWidth<=0 || head.biHeight<=0)
\r
44 cx_throw("Corrupted WBMP");
\r
46 if (info.nEscape == -1){
\r
47 info.dwType = CXIMAGE_FORMAT_WBMP;
\r
51 Create(head.biWidth, head.biHeight, 1, CXIMAGE_FORMAT_WBMP);
\r
52 if (!IsValid()) cx_throw("WBMP Create failed");
\r
55 int linewidth=(head.biWidth+7)/8;
\r
56 CImageIterator iter(this);
\r
58 for (long y=0; y < head.biHeight; y++){
\r
59 hFile->Read(iter.GetRow(),linewidth,1);
\r
64 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
\r
69 ////////////////////////////////////////////////////////////////////////////////
\r
70 bool CxImageWBMP::ReadOctet(CxFile * hFile, DWORD *data)
\r
75 if (hFile->Eof()) return false;
\r
76 c = (BYTE)hFile->GetC();
\r
78 *data |= (c & 0x7F);
\r
79 } while ((c&0x80)!=0);
\r
82 ////////////////////////////////////////////////////////////////////////////////
\r
83 #endif //CXIMAGE_SUPPORT_DECODE
\r
84 ////////////////////////////////////////////////////////////////////////////////
\r
85 #if CXIMAGE_SUPPORT_ENCODE
\r
86 ////////////////////////////////////////////////////////////////////////////////
\r
87 bool CxImageWBMP::Encode(CxFile * hFile)
\r
89 if (EncodeSafeCheck(hFile)) return false;
\r
91 //check format limits
\r
92 if (head.biBitCount!=1){
\r
93 strcpy(info.szLastError,"Can't save this image as WBMP");
\r
97 WBMPHEADER wbmpHead;
\r
99 wbmpHead.FixHeader=0;
\r
100 wbmpHead.ImageWidth=head.biWidth;
\r
101 wbmpHead.ImageHeight=head.biHeight;
\r
103 // Write the file header
\r
106 WriteOctet(hFile,wbmpHead.ImageWidth);
\r
107 WriteOctet(hFile,wbmpHead.ImageHeight);
\r
108 // Write the pixels
\r
109 int linewidth=(wbmpHead.ImageWidth+7)/8;
\r
110 CImageIterator iter(this);
\r
112 for (DWORD y=0; y < wbmpHead.ImageHeight; y++){
\r
113 hFile->Write(iter.GetRow(),linewidth,1);
\r
118 ////////////////////////////////////////////////////////////////////////////////
\r
119 bool CxImageWBMP::WriteOctet(CxFile * hFile, const DWORD data)
\r
122 while (data>>(ns+7)) ns+=7;
\r
124 if (!hFile->PutC(0x80 | (BYTE)(data>>ns))) return false;
\r
127 if (!(hFile->PutC((BYTE)(0x7F & data)))) return false;
\r
130 ////////////////////////////////////////////////////////////////////////////////
\r
131 #endif // CXIMAGE_SUPPORT_ENCODE
\r
132 ////////////////////////////////////////////////////////////////////////////////
\r
133 #endif // CXIMAGE_SUPPORT_WBMP
\r