1 /*****************************************************************************\
2 ModeJbig.cpp : Implementation for the ModeJbig class
4 Copyright (c) 1996 - 2009, Hewlett-Packard Co.
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. Neither the name of Hewlett-Packard nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
22 NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 TO, PATENT INFRINGEMENT; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 Author: Naga Samrat Chowdary Narla,
31 \*****************************************************************************/
33 #include "CommonDefinitions.h"
34 #include "Compressor.h"
37 #include "hpjbig_wrapper.h"
43 int (*HPLJJBGCompress) (int iWidth, int iHeight, unsigned char **pBuff,
44 HPLJZjcBuff *pOutBuff, HPLJZjsJbgEncSt *pJbgEncSt);
45 int (*HPLJSoInit) (int iFlag);
48 const BYTE ModeJbig::szByte1[256] =
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2,
52 2, 2, 2, 2, 2, 2, 2, 2, 8, 8, 8, 8,
53 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
54 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
55 10, 10, 10, 10, 32, 32, 32, 32, 32, 32, 32, 32,
56 32, 32, 32, 32, 32, 32, 32, 32, 34, 34, 34, 34,
57 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
58 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
59 40, 40, 40, 40, 42, 42, 42, 42, 42, 42, 42, 42,
60 42, 42, 42, 42, 42, 42, 42, 42, 128, 128, 128, 128,
61 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
62 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
63 130, 130, 130, 130, 136, 136, 136, 136, 136, 136, 136, 136,
64 136, 136, 136, 136, 136, 136, 136, 136, 138, 138, 138, 138,
65 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138,
66 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160,
67 160, 160, 160, 160, 162, 162, 162, 162, 162, 162, 162, 162,
68 162, 162, 162, 162, 162, 162, 162, 162, 168, 168, 168, 168,
69 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
70 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
73 const BYTE ModeJbig::szByte2[256] =
75 0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
76 160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
77 128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
78 32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
79 0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
80 160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
81 128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
82 32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
83 0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
84 160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
85 128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
86 32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
87 0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
88 160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
89 128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
90 32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
91 0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
92 160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
93 128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
94 32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
95 0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
100 ModeJbig::ModeJbig (unsigned int RasterSize) : Compressor (RasterSize, false)
103 m_pszInputRasterData = 0;
104 m_iWidth = ((RasterSize + 31) / 32) * 4;
108 for (int i = 0; i < 4; i++)
110 m_iCurRasterPerPlane[i] = 0;
116 ModeJbig::~ModeJbig()
120 dlclose(m_hHPLibHandle);
122 if (m_pszInputRasterData)
124 delete [] m_pszInputRasterData;
128 DRIVER_ERROR ModeJbig::Init(int iLastRaster, int iPlanes, int iBPP, ZJPLATFORM zj_platform)
130 m_iLastRaster = iLastRaster;
131 m_iOrgHeight = iLastRaster;
134 m_ezj_platform = zj_platform;
136 m_hHPLibHandle = LoadPlugin ("lj.so");
140 *(void **) (&HPLJJBGCompress) = dlsym (m_hHPLibHandle, "hp_encode_bits_to_jbig");
141 *(void **) (&HPLJSoInit) = dlsym (m_hHPLibHandle, "hp_init_lib");
142 if (!HPLJSoInit || (HPLJSoInit && !HPLJSoInit (1)))
144 return PLUGIN_LIBRARY_MISSING;
149 return PLUGIN_LIBRARY_MISSING;
158 if(zj_platform == ZJCOLOR2)
165 int buffer_size = m_iWidth * m_iLastRaster * m_iPlanes * m_iBPP;
166 m_pszInputRasterData = new BYTE[buffer_size];
167 if (m_pszInputRasterData == NULL)
169 return ALLOCMEM_ERROR;
171 compressBuf = new BYTE[m_iWidth * m_iLastRaster * m_iBPP];
172 if (compressBuf == NULL)
174 return ALLOCMEM_ERROR;
176 m_pszCurPtr = m_pszInputRasterData;
177 memset(m_pszCurPtr, 0, buffer_size);
181 void ModeJbig::Flush()
183 if (m_iCurRasterPerPlane[m_iPlaneNumber] > 0)
185 int height = m_iLastRaster;
186 m_iLastRaster = m_iCurRasterPerPlane[m_iPlaneNumber];
187 if (m_iPlanes == 1) {
189 m_iLastRaster = height;
195 bool ModeJbig::Process (RASTERDATA* input)
204 bool bResult = false;
205 switch(m_ezj_platform)
208 bResult = processZJStream(input);
211 bResult = processZXStream(input);
217 bResult = processZXStream(input);
220 bResult = processZJColor(input);
225 m_iCurRasterPerPlane[m_iPlaneNumber] = 0;
226 m_pszCurPtr = m_pszInputRasterData;
231 bool ModeJbig::processZJStream(RASTERDATA *input)
233 if (input->rasterdata[COLORTYPE_COLOR])
235 memcpy(m_pszCurPtr, input->rasterdata[COLORTYPE_COLOR],
236 input->rastersize[COLORTYPE_COLOR]);
239 m_iCurRasterPerPlane[0]++;
240 m_pszCurPtr += m_iWidth;
242 if (m_iCurRasterPerPlane[0] == m_iLastRaster)
251 bool ModeJbig::processZXStream(RASTERDATA *input)
253 if (input->rasterdata[COLORTYPE_COLOR])
255 for (int i = 0; i < input->rastersize[COLORTYPE_COLOR]; i++)
257 m_pszCurPtr[i*m_iBPP] = szByte1[input->rasterdata[COLORTYPE_COLOR][i]];
258 m_pszCurPtr[i*m_iBPP+1] = szByte2[input->rasterdata[COLORTYPE_COLOR][i]];
259 m_pszCurPtr[i*m_iBPP] |= (m_pszCurPtr[i*m_iBPP] >> 1);
260 m_pszCurPtr[i*m_iBPP+1] |= (m_pszCurPtr[i*m_iBPP+1] >> 1);
264 m_iCurRasterPerPlane[0]++;
265 m_pszCurPtr += m_iWidth * m_iBPP;
266 if (m_iCurRasterPerPlane[0] == m_iLastRaster)
274 bool ModeJbig::processZJColor(RASTERDATA *input)
276 BYTE *p = m_pszCurPtr + (m_iP[m_iPlaneNumber] * m_iWidth * m_iBPP) * m_iLastRaster;
277 if (input->rasterdata[COLORTYPE_COLOR])
279 for (int i = 0; i < input->rastersize[COLORTYPE_COLOR]; i++)
281 p[i*m_iBPP] = szByte1[input->rasterdata[COLORTYPE_COLOR][i]];
282 p[i*m_iBPP+1] = szByte2[input->rasterdata[COLORTYPE_COLOR][i]];
283 p[i*m_iBPP] |= (p[i*m_iBPP] >> 1);
284 p[i*m_iBPP+1] |= (p[i*m_iBPP+1] >> 1);
288 m_iCurRasterPerPlane[m_iPlaneNumber]++;
289 if (m_iCurRasterPerPlane[m_iPlaneNumber] == m_iLastRaster && m_iPlaneNumber == 3)
292 m_pszCurPtr = m_pszInputRasterData;
297 if (m_iPlaneNumber == 4)
299 m_pszCurPtr += m_iBPP * m_iWidth;
305 bool ModeJbig::NextOutputRaster(RASTERDATA& next_raster)
307 if (iRastersReady == 0)
314 compress(m_iCurrentPlane++);
316 next_raster.rastersize[COLORTYPE_COLOR] = compressedsize;
317 next_raster.rasterdata[COLORTYPE_COLOR] = compressBuf;
318 next_raster.rastersize[COLORTYPE_BLACK] = 0;
319 next_raster.rasterdata[COLORTYPE_BLACK] = NULL;
321 if (m_iPlanes == 1 || (m_iCurrentPlane == 4))
325 m_iLastRaster = m_iOrgHeight;
330 void ModeJbig::compress (int plane_number)
332 HPLJZjcBuff myBuffer;
335 myBuffer.pszCompressedData = compressBuf;
336 myBuffer.dwTotalSize = 0;
337 BYTE *p = m_pszInputRasterData + (m_iWidth * m_iBPP * m_iLastRaster * plane_number);
339 memset (myBuffer.pszCompressedData, 0, m_iWidth * m_iLastRaster * m_iBPP);
340 HPLJJBGCompress (m_iWidth * 8 * m_iBPP, m_iLastRaster, &p, &myBuffer, &se);
342 compressedsize = myBuffer.dwTotalSize;
343 memcpy(compressBuf+compressedsize, &se, sizeof(se));
345 m_iCurRasterPerPlane[plane_number] = 0;
346 m_pszCurPtr = m_pszInputRasterData;
347 int buffer_size = m_iWidth * m_iLastRaster * m_iBPP;
348 memset(p, 0, buffer_size);