3 * Purpose: Platform Independent JBG Image Class Loader and Writer
4 * 18/Aug/2002 Davide Pizzolato - www.xdp.it
5 * CxImage version 6.0.0 02/Feb/2008
10 #if CXIMAGE_SUPPORT_JBG
14 #define JBIG_BUFSIZE 8192
16 ////////////////////////////////////////////////////////////////////////////////
17 #if CXIMAGE_SUPPORT_DECODE
18 ////////////////////////////////////////////////////////////////////////////////
19 bool CxImageJBG::Decode(CxFile *hFile)
21 if (hFile == NULL) return false;
23 struct jbg_dec_state jbig_state;
24 unsigned long xmax = 4294967295UL, ymax = 4294967295UL;
25 unsigned int len, cnt;
31 jbg_dec_init(&jbig_state);
32 jbg_dec_maxsize(&jbig_state, xmax, ymax);
34 buffer = (BYTE*)malloc(JBIG_BUFSIZE);
35 if (!buffer) cx_throw("Sorry, not enough memory available!");
39 len = hFile->Read(buffer, 1, JBIG_BUFSIZE);
43 while (len > 0 && (result == JBG_EAGAIN || result == JBG_EOK)) {
44 result = jbg_dec_in(&jbig_state, p, len, &cnt);
48 } while (result == JBG_EAGAIN || result == JBG_EOK);
51 cx_throw("Problem while reading input file");
52 if (result != JBG_EOK && result != JBG_EOK_INTR)
53 cx_throw("Problem with input file");
55 int w, h, bpp, planes, ew;
57 w = jbg_dec_getwidth(&jbig_state);
58 h = jbg_dec_getheight(&jbig_state);
59 planes = jbg_dec_getplanes(&jbig_state);
63 if (info.nEscape == -1){
66 info.dwType = CXIMAGE_FORMAT_JBG;
67 cx_throw("output dimensions returned");
73 BYTE* binary_image = jbg_dec_getimage(&jbig_state, 0);
75 if (!Create(w,h,1,CXIMAGE_FORMAT_JBG))
78 SetPaletteColor(0,255,255,255);
79 SetPaletteColor(1,0,0,0);
81 CImageIterator iter(this);
83 for (int i=0;i<h;i++){
84 iter.SetRow(binary_image+i*ew,ew);
91 cx_throw("cannot decode images with more than 1 plane");
94 jbg_dec_free(&jbig_state);
98 jbg_dec_free(&jbig_state);
99 if (buffer) free(buffer);
100 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
101 if (info.nEscape == -1 && info.dwType == CXIMAGE_FORMAT_JBG) return true;
106 ////////////////////////////////////////////////////////////////////////////////
107 #endif //CXIMAGE_SUPPORT_DECODE
108 ////////////////////////////////////////////////////////////////////////////////
109 bool CxImageJBG::Encode(CxFile * hFile)
111 if (EncodeSafeCheck(hFile)) return false;
113 if (head.biBitCount != 1){
114 strcpy(info.szLastError,"JBG can save only 1-bpp images");
118 int w, h, bpp, planes, ew, i, j, x, y;
127 RGBQUAD *rgb = GetPalette();
128 if (CompareColors(&rgb[0],&rgb[1])<0) mask=255; else mask=0;
130 BYTE *buffer = (BYTE*)malloc(ew*h*2);
132 strcpy(info.szLastError,"Sorry, not enough memory available!");
138 j= (h-y-1)*info.dwEffWidth;
139 for (x=0; x<ew; x++){
140 buffer[i + x]=info.pImage[j + x]^mask;
144 struct jbg_enc_state jbig_state;
145 jbg_enc_init(&jbig_state, w, h, planes, &buffer, jbig_data_out, hFile);
147 //jbg_enc_layers(&jbig_state, 2);
148 //jbg_enc_lrlmax(&jbig_state, 800, 600);
150 // Specify a few other options (each is ignored if negative)
151 int dl = -1, dh = -1, d = -1, l0 = -1, mx = -1;
152 int options = JBG_TPDON | JBG_TPBON | JBG_DPON;
153 int order = JBG_ILEAVE | JBG_SMID;
154 jbg_enc_lrange(&jbig_state, dl, dh);
155 jbg_enc_options(&jbig_state, order, options, l0, mx, -1);
157 // now encode everything and send it to data_out()
158 jbg_enc_out(&jbig_state);
160 // give encoder a chance to free its temporary data structures
161 jbg_enc_free(&jbig_state);
166 strcpy(info.szLastError,"Problem while writing JBG file");
172 ////////////////////////////////////////////////////////////////////////////////
173 #endif // CXIMAGE_SUPPORT_JBG