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