]> Creatis software - clitk.git/blob - utilities/CxImage/ximajbg.cpp
Ensure compilation when CLITK_USE_PACS_CONNECTION is OFF
[clitk.git] / utilities / CxImage / ximajbg.cpp
1 /*\r
2  * File:        ximajbg.cpp\r
3  * Purpose:     Platform Independent JBG Image Class Loader and Writer\r
4  * 18/Aug/2002 Davide Pizzolato - www.xdp.it\r
5  * CxImage version 6.0.0 02/Feb/2008\r
6  */\r
7 \r
8 #include "ximajbg.h"\r
9 \r
10 #if CXIMAGE_SUPPORT_JBG\r
11 \r
12 #include "ximaiter.h"\r
13 \r
14 #define JBIG_BUFSIZE 8192\r
15 \r
16 ////////////////////////////////////////////////////////////////////////////////\r
17 #if CXIMAGE_SUPPORT_DECODE\r
18 ////////////////////////////////////////////////////////////////////////////////\r
19 bool CxImageJBG::Decode(CxFile *hFile)\r
20 {\r
21         if (hFile == NULL) return false;\r
22 \r
23         struct jbg_dec_state jbig_state;\r
24         unsigned long xmax = 4294967295UL, ymax = 4294967295UL;\r
25         unsigned int len, cnt;\r
26         BYTE *buffer,*p;\r
27         int result;\r
28 \r
29   cx_try\r
30   {\r
31         jbg_dec_init(&jbig_state);\r
32         jbg_dec_maxsize(&jbig_state, xmax, ymax);\r
33 \r
34         buffer = (BYTE*)malloc(JBIG_BUFSIZE);\r
35         if (!buffer) cx_throw("Sorry, not enough memory available!");\r
36 \r
37         result = JBG_EAGAIN;\r
38         do {\r
39                 len = hFile->Read(buffer, 1, JBIG_BUFSIZE);\r
40                 if (!len) break;\r
41                 cnt = 0;\r
42                 p = buffer;\r
43                 while (len > 0 && (result == JBG_EAGAIN || result == JBG_EOK)) {\r
44                         result = jbg_dec_in(&jbig_state, p, len, &cnt);\r
45                         p += cnt;\r
46                         len -= cnt;\r
47                 }\r
48         } while (result == JBG_EAGAIN || result == JBG_EOK);\r
49 \r
50         if (hFile->Error())\r
51                 cx_throw("Problem while reading input file");\r
52         if (result != JBG_EOK && result != JBG_EOK_INTR)\r
53                 cx_throw("Problem with input file"); \r
54 \r
55         int w, h, bpp, planes, ew;\r
56 \r
57         w = jbg_dec_getwidth(&jbig_state);\r
58         h = jbg_dec_getheight(&jbig_state);\r
59         planes = jbg_dec_getplanes(&jbig_state);\r
60         bpp = (planes+7)>>3;\r
61         ew = (w + 7)>>3;\r
62 \r
63         if (info.nEscape == -1){\r
64                 head.biWidth = w;\r
65                 head.biHeight= h;\r
66                 info.dwType = CXIMAGE_FORMAT_JBG;\r
67                 cx_throw("output dimensions returned");\r
68         }\r
69 \r
70         switch (planes){\r
71         case 1:\r
72                 {\r
73                         BYTE* binary_image = jbg_dec_getimage(&jbig_state, 0);\r
74 \r
75                         if (!Create(w,h,1,CXIMAGE_FORMAT_JBG))\r
76                                 cx_throw("");\r
77 \r
78                         SetPaletteColor(0,255,255,255);\r
79                         SetPaletteColor(1,0,0,0);\r
80 \r
81                         CImageIterator iter(this);\r
82                         iter.Upset();\r
83                         for (int i=0;i<h;i++){\r
84                                 iter.SetRow(binary_image+i*ew,ew);\r
85                                 iter.PrevRow();\r
86                         }\r
87 \r
88                         break;\r
89                 }\r
90         default:\r
91                 cx_throw("cannot decode images with more than 1 plane");\r
92         }\r
93 \r
94         jbg_dec_free(&jbig_state);\r
95         free(buffer);\r
96 \r
97   } cx_catch {\r
98         jbg_dec_free(&jbig_state);\r
99         if (buffer) free(buffer);\r
100         if (strcmp(message,"")) strncpy(info.szLastError,message,255);\r
101         if (info.nEscape == -1 && info.dwType == CXIMAGE_FORMAT_JBG) return true;\r
102         return false;\r
103   }\r
104         return true;\r
105 }\r
106 ////////////////////////////////////////////////////////////////////////////////\r
107 #endif //CXIMAGE_SUPPORT_DECODE\r
108 ////////////////////////////////////////////////////////////////////////////////\r
109 bool CxImageJBG::Encode(CxFile * hFile)\r
110 {\r
111         if (EncodeSafeCheck(hFile)) return false;\r
112 \r
113         if (head.biBitCount != 1){\r
114                 strcpy(info.szLastError,"JBG can save only 1-bpp images");\r
115                 return false;\r
116         }\r
117 \r
118         int w, h, bpp, planes, ew, i, j, x, y;\r
119 \r
120         w = head.biWidth;\r
121         h = head.biHeight;\r
122         planes = 1;\r
123         bpp = (planes+7)>>3;\r
124         ew = (w + 7)>>3;\r
125 \r
126         BYTE mask;\r
127         RGBQUAD *rgb = GetPalette();\r
128         if (CompareColors(&rgb[0],&rgb[1])<0) mask=255; else mask=0;\r
129 \r
130         BYTE *buffer = (BYTE*)malloc(ew*h*2);\r
131         if (!buffer) {\r
132                 strcpy(info.szLastError,"Sorry, not enough memory available!");\r
133                 return false;\r
134         }\r
135 \r
136         for (y=0; y<h; y++){\r
137                 i= y*ew;\r
138                 j= (h-y-1)*info.dwEffWidth;\r
139                 for (x=0; x<ew; x++){\r
140                         buffer[i + x]=info.pImage[j + x]^mask;\r
141                 }\r
142         }\r
143 \r
144         struct jbg_enc_state jbig_state;\r
145         jbg_enc_init(&jbig_state, w, h, planes, &buffer, jbig_data_out, hFile);\r
146 \r
147     //jbg_enc_layers(&jbig_state, 2);\r
148     //jbg_enc_lrlmax(&jbig_state, 800, 600);\r
149 \r
150         // Specify a few other options (each is ignored if negative)\r
151         int dl = -1, dh = -1, d = -1, l0 = -1, mx = -1;\r
152         int options = JBG_TPDON | JBG_TPBON | JBG_DPON;\r
153         int order = JBG_ILEAVE | JBG_SMID;\r
154         jbg_enc_lrange(&jbig_state, dl, dh);\r
155         jbg_enc_options(&jbig_state, order, options, l0, mx, -1);\r
156 \r
157         // now encode everything and send it to data_out()\r
158         jbg_enc_out(&jbig_state);\r
159 \r
160         // give encoder a chance to free its temporary data structures\r
161         jbg_enc_free(&jbig_state);\r
162 \r
163         free(buffer);\r
164 \r
165         if (hFile->Error()){\r
166                 strcpy(info.szLastError,"Problem while writing JBG file");\r
167                 return false;\r
168         }\r
169 \r
170         return true;\r
171 }\r
172 ////////////////////////////////////////////////////////////////////////////////\r
173 #endif  // CXIMAGE_SUPPORT_JBG\r
174 \r