1 | /*
|
---|
2 | * File: ximawbmp.cpp
|
---|
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
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "ximawbmp.h"
|
---|
9 |
|
---|
10 | #if CXIMAGE_SUPPORT_WBMP
|
---|
11 |
|
---|
12 | #include "ximaiter.h"
|
---|
13 |
|
---|
14 | ////////////////////////////////////////////////////////////////////////////////
|
---|
15 | #if CXIMAGE_SUPPORT_DECODE
|
---|
16 | ////////////////////////////////////////////////////////////////////////////////
|
---|
17 | bool CxImageWBMP::Decode(CxFile *hFile)
|
---|
18 | {
|
---|
19 | if (hFile == NULL) return false;
|
---|
20 |
|
---|
21 | WBMPHEADER wbmpHead;
|
---|
22 |
|
---|
23 | cx_try
|
---|
24 | {
|
---|
25 | ReadOctet(hFile, &wbmpHead.Type);
|
---|
26 |
|
---|
27 | DWORD dat;
|
---|
28 | ReadOctet(hFile, &dat);
|
---|
29 | wbmpHead.FixHeader = (BYTE)dat;
|
---|
30 |
|
---|
31 | ReadOctet(hFile, &wbmpHead.ImageWidth);
|
---|
32 | ReadOctet(hFile, &wbmpHead.ImageHeight);
|
---|
33 |
|
---|
34 | if (hFile->Eof())
|
---|
35 | cx_throw("Not a WBMP");
|
---|
36 |
|
---|
37 | if (wbmpHead.Type != 0)
|
---|
38 | cx_throw("Unsupported WBMP type");
|
---|
39 |
|
---|
40 | head.biWidth = wbmpHead.ImageWidth;
|
---|
41 | head.biHeight= wbmpHead.ImageHeight;
|
---|
42 |
|
---|
43 | if (head.biWidth<=0 || head.biHeight<=0)
|
---|
44 | cx_throw("Corrupted WBMP");
|
---|
45 |
|
---|
46 | if (info.nEscape == -1){
|
---|
47 | info.dwType = CXIMAGE_FORMAT_WBMP;
|
---|
48 | return true;
|
---|
49 | }
|
---|
50 |
|
---|
51 | Create(head.biWidth, head.biHeight, 1, CXIMAGE_FORMAT_WBMP);
|
---|
52 | if (!IsValid()) cx_throw("WBMP Create failed");
|
---|
53 | SetGrayPalette();
|
---|
54 |
|
---|
55 | int linewidth=(head.biWidth+7)/8;
|
---|
56 | CImageIterator iter(this);
|
---|
57 | iter.Upset();
|
---|
58 | for (long y=0; y < head.biHeight; y++){
|
---|
59 | hFile->Read(iter.GetRow(),linewidth,1);
|
---|
60 | iter.PrevRow();
|
---|
61 | }
|
---|
62 |
|
---|
63 | } cx_catch {
|
---|
64 | if (strcmp(message,"")) strncpy(info.szLastError,message,255);
|
---|
65 | return FALSE;
|
---|
66 | }
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 | ////////////////////////////////////////////////////////////////////////////////
|
---|
70 | bool CxImageWBMP::ReadOctet(CxFile * hFile, DWORD *data)
|
---|
71 | {
|
---|
72 | BYTE c;
|
---|
73 | *data = 0;
|
---|
74 | do {
|
---|
75 | if (hFile->Eof()) return false;
|
---|
76 | c = (BYTE)hFile->GetC();
|
---|
77 | *data <<= 7;
|
---|
78 | *data |= (c & 0x7F);
|
---|
79 | } while ((c&0x80)!=0);
|
---|
80 | return true;
|
---|
81 | }
|
---|
82 | ////////////////////////////////////////////////////////////////////////////////
|
---|
83 | #endif //CXIMAGE_SUPPORT_DECODE
|
---|
84 | ////////////////////////////////////////////////////////////////////////////////
|
---|
85 | #if CXIMAGE_SUPPORT_ENCODE
|
---|
86 | ////////////////////////////////////////////////////////////////////////////////
|
---|
87 | bool CxImageWBMP::Encode(CxFile * hFile)
|
---|
88 | {
|
---|
89 | if (EncodeSafeCheck(hFile)) return false;
|
---|
90 |
|
---|
91 | //check format limits
|
---|
92 | if (head.biBitCount!=1){
|
---|
93 | strcpy(info.szLastError,"Can't save this image as WBMP");
|
---|
94 | return false;
|
---|
95 | }
|
---|
96 |
|
---|
97 | WBMPHEADER wbmpHead;
|
---|
98 | wbmpHead.Type=0;
|
---|
99 | wbmpHead.FixHeader=0;
|
---|
100 | wbmpHead.ImageWidth=head.biWidth;
|
---|
101 | wbmpHead.ImageHeight=head.biHeight;
|
---|
102 |
|
---|
103 | // Write the file header
|
---|
104 | hFile->PutC('\0');
|
---|
105 | hFile->PutC('\0');
|
---|
106 | WriteOctet(hFile,wbmpHead.ImageWidth);
|
---|
107 | WriteOctet(hFile,wbmpHead.ImageHeight);
|
---|
108 | // Write the pixels
|
---|
109 | int linewidth=(wbmpHead.ImageWidth+7)/8;
|
---|
110 | CImageIterator iter(this);
|
---|
111 | iter.Upset();
|
---|
112 | for (DWORD y=0; y < wbmpHead.ImageHeight; y++){
|
---|
113 | hFile->Write(iter.GetRow(),linewidth,1);
|
---|
114 | iter.PrevRow();
|
---|
115 | }
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 | ////////////////////////////////////////////////////////////////////////////////
|
---|
119 | bool CxImageWBMP::WriteOctet(CxFile * hFile, const DWORD data)
|
---|
120 | {
|
---|
121 | int ns = 0;
|
---|
122 | while (data>>(ns+7)) ns+=7;
|
---|
123 | while (ns>0){
|
---|
124 | if (!hFile->PutC(0x80 | (BYTE)(data>>ns))) return false;
|
---|
125 | ns-=7;
|
---|
126 | }
|
---|
127 | if (!(hFile->PutC((BYTE)(0x7F & data)))) return false;
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 | ////////////////////////////////////////////////////////////////////////////////
|
---|
131 | #endif // CXIMAGE_SUPPORT_ENCODE
|
---|
132 | ////////////////////////////////////////////////////////////////////////////////
|
---|
133 | #endif // CXIMAGE_SUPPORT_WBMP
|
---|
134 |
|
---|