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
10 #if CXIMAGE_SUPPORT_JBG
\r
12 #include "ximaiter.h"
\r
14 #define JBIG_BUFSIZE 8192
\r
16 ////////////////////////////////////////////////////////////////////////////////
\r
17 #if CXIMAGE_SUPPORT_DECODE
\r
18 ////////////////////////////////////////////////////////////////////////////////
\r
19 bool CxImageJBG::Decode(CxFile *hFile)
\r
21 if (hFile == NULL) return false;
\r
23 struct jbg_dec_state jbig_state;
\r
24 unsigned long xmax = 4294967295UL, ymax = 4294967295UL;
\r
25 unsigned int len, cnt;
\r
31 jbg_dec_init(&jbig_state);
\r
32 jbg_dec_maxsize(&jbig_state, xmax, ymax);
\r
34 buffer = (BYTE*)malloc(JBIG_BUFSIZE);
\r
35 if (!buffer) cx_throw("Sorry, not enough memory available!");
\r
37 result = JBG_EAGAIN;
\r
39 len = hFile->Read(buffer, 1, JBIG_BUFSIZE);
\r
43 while (len > 0 && (result == JBG_EAGAIN || result == JBG_EOK)) {
\r
44 result = jbg_dec_in(&jbig_state, p, len, &cnt);
\r
48 } while (result == JBG_EAGAIN || result == JBG_EOK);
\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
55 int w, h, bpp, planes, ew;
\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
63 if (info.nEscape == -1){
\r
66 info.dwType = CXIMAGE_FORMAT_JBG;
\r
67 cx_throw("output dimensions returned");
\r
73 BYTE* binary_image = jbg_dec_getimage(&jbig_state, 0);
\r
75 if (!Create(w,h,1,CXIMAGE_FORMAT_JBG))
\r
78 SetPaletteColor(0,255,255,255);
\r
79 SetPaletteColor(1,0,0,0);
\r
81 CImageIterator iter(this);
\r
83 for (int i=0;i<h;i++){
\r
84 iter.SetRow(binary_image+i*ew,ew);
\r
91 cx_throw("cannot decode images with more than 1 plane");
\r
94 jbg_dec_free(&jbig_state);
\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
106 ////////////////////////////////////////////////////////////////////////////////
\r
107 #endif //CXIMAGE_SUPPORT_DECODE
\r
108 ////////////////////////////////////////////////////////////////////////////////
\r
109 bool CxImageJBG::Encode(CxFile * hFile)
\r
111 if (EncodeSafeCheck(hFile)) return false;
\r
113 if (head.biBitCount != 1){
\r
114 strcpy(info.szLastError,"JBG can save only 1-bpp images");
\r
118 int w, h, bpp, planes, ew, i, j, x, y;
\r
123 bpp = (planes+7)>>3;
\r
127 RGBQUAD *rgb = GetPalette();
\r
128 if (CompareColors(&rgb[0],&rgb[1])<0) mask=255; else mask=0;
\r
130 BYTE *buffer = (BYTE*)malloc(ew*h*2);
\r
132 strcpy(info.szLastError,"Sorry, not enough memory available!");
\r
136 for (y=0; y<h; y++){
\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
144 struct jbg_enc_state jbig_state;
\r
145 jbg_enc_init(&jbig_state, w, h, planes, &buffer, jbig_data_out, hFile);
\r
147 //jbg_enc_layers(&jbig_state, 2);
\r
148 //jbg_enc_lrlmax(&jbig_state, 800, 600);
\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
157 // now encode everything and send it to data_out()
\r
158 jbg_enc_out(&jbig_state);
\r
160 // give encoder a chance to free its temporary data structures
\r
161 jbg_enc_free(&jbig_state);
\r
165 if (hFile->Error()){
\r
166 strcpy(info.szLastError,"Problem while writing JBG file");
\r
172 ////////////////////////////////////////////////////////////////////////////////
\r
173 #endif // CXIMAGE_SUPPORT_JBG
\r