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