source: liacs/MIR2010/SourceCode/cximage/ximajbg.cpp@ 273

Last change on this file since 273 was 95, checked in by Rick van der Zwet, 15 years ago

Bad boy, improper move of directory

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