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.
29
\*****************************************************************************/
31
#include "CommonDefinitions.h"
32
#include "Compressor.h"
35
#include "hpjbig_wrapper.h"
41
int (*HPLJJBGCompress) (int iWidth, int iHeight, unsigned char **pBuff,
42
HPLJZjcBuff *pOutBuff, HPLJZjsJbgEncSt *pJbgEncSt);
43
int (*HPLJSoInit) (int iFlag);
46
const BYTE ModeJbig::szByte1[256] =
48
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49
0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2,
50
2, 2, 2, 2, 2, 2, 2, 2, 8, 8, 8, 8,
51
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
52
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
53
10, 10, 10, 10, 32, 32, 32, 32, 32, 32, 32, 32,
54
32, 32, 32, 32, 32, 32, 32, 32, 34, 34, 34, 34,
55
34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34,
56
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
57
40, 40, 40, 40, 42, 42, 42, 42, 42, 42, 42, 42,
58
42, 42, 42, 42, 42, 42, 42, 42, 128, 128, 128, 128,
59
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
60
130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130, 130,
61
130, 130, 130, 130, 136, 136, 136, 136, 136, 136, 136, 136,
62
136, 136, 136, 136, 136, 136, 136, 136, 138, 138, 138, 138,
63
138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138, 138,
64
160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160, 160,
65
160, 160, 160, 160, 162, 162, 162, 162, 162, 162, 162, 162,
66
162, 162, 162, 162, 162, 162, 162, 162, 168, 168, 168, 168,
67
168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168, 168,
68
170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170,
71
const BYTE ModeJbig::szByte2[256] =
73
0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
74
160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
75
128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
76
32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
77
0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
78
160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
79
128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
80
32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
81
0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
82
160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
83
128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
84
32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
85
0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
86
160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
87
128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
88
32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
89
0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
90
160, 162, 168, 170, 0, 2, 8, 10, 32, 34, 40, 42,
91
128, 130, 136, 138, 160, 162, 168, 170, 0, 2, 8, 10,
92
32, 34, 40, 42, 128, 130, 136, 138, 160, 162, 168, 170,
93
0, 2, 8, 10, 32, 34, 40, 42, 128, 130, 136, 138,
98
ModeJbig::ModeJbig (unsigned int RasterSize) : Compressor (RasterSize, false)
100
m_iWidth = ((RasterSize + 31) / 32) * 4;
104
for (int i = 0; i < 4; i++)
106
m_iCurRasterPerPlane[i] = 0;
112
ModeJbig::~ModeJbig()
116
dlclose(m_hHPLibHandle);
118
if (m_pszInputRasterData)
120
delete [] m_pszInputRasterData;
124
DRIVER_ERROR ModeJbig::Init(int iLastRaster, int iPlanes, int iBPP, ZJPLATFORM zj_platform)
126
m_iLastRaster = iLastRaster;
127
m_iOrgHeight = iLastRaster;
130
m_ezj_platform = zj_platform;
132
m_hHPLibHandle = LoadPlugin ("lj.so");
136
*(void **) (&HPLJJBGCompress) = dlsym (m_hHPLibHandle, "hp_encode_bits_to_jbig");
137
*(void **) (&HPLJSoInit) = dlsym (m_hHPLibHandle, "hp_init_lib");
138
if (!HPLJSoInit || (HPLJSoInit && !HPLJSoInit (1)))
140
return PLUGIN_LIBRARY_MISSING;
145
return PLUGIN_LIBRARY_MISSING;
156
int buffer_size = m_iWidth * m_iLastRaster * m_iPlanes * m_iBPP;
157
m_pszInputRasterData = new BYTE[buffer_size];
158
if (m_pszInputRasterData == NULL)
160
return ALLOCMEM_ERROR;
162
compressBuf = new BYTE[m_iWidth * m_iLastRaster * m_iBPP];
163
if (compressBuf == NULL)
165
return ALLOCMEM_ERROR;
167
m_pszCurPtr = m_pszInputRasterData;
168
memset(m_pszCurPtr, 0, buffer_size);
172
void ModeJbig::Flush()
174
if (m_iCurRasterPerPlane[m_iPlaneNumber] > 0)
176
int height = m_iLastRaster;
177
m_iLastRaster = m_iCurRasterPerPlane[m_iPlaneNumber];
178
if (m_iPlanes == 1) {
180
m_iLastRaster = height;
186
bool ModeJbig::Process (RASTERDATA* input)
195
bool bResult = false;
196
switch(m_ezj_platform)
199
bResult = processZJStream(input);
202
bResult = processZXStream(input);
207
bResult = processZXStream(input);
210
bResult = processZJColor(input);
215
m_iCurRasterPerPlane[m_iPlaneNumber] = 0;
216
m_pszCurPtr = m_pszInputRasterData;
221
bool ModeJbig::processZJStream(RASTERDATA *input)
223
if (input->rasterdata[COLORTYPE_COLOR])
225
memcpy(m_pszCurPtr, input->rasterdata[COLORTYPE_COLOR],
226
input->rastersize[COLORTYPE_COLOR]);
229
m_iCurRasterPerPlane[0]++;
230
m_pszCurPtr += m_iWidth;
232
if (m_iCurRasterPerPlane[0] == m_iLastRaster)
241
bool ModeJbig::processZXStream(RASTERDATA *input)
243
if (input->rasterdata[COLORTYPE_COLOR])
245
for (int i = 0; i < input->rastersize[COLORTYPE_COLOR]; i++)
247
m_pszCurPtr[i*m_iBPP] = szByte1[input->rasterdata[COLORTYPE_COLOR][i]];
248
m_pszCurPtr[i*m_iBPP+1] = szByte2[input->rasterdata[COLORTYPE_COLOR][i]];
249
m_pszCurPtr[i*m_iBPP] |= (m_pszCurPtr[i*m_iBPP] >> 1);
250
m_pszCurPtr[i*m_iBPP+1] |= (m_pszCurPtr[i*m_iBPP+1] >> 1);
254
m_iCurRasterPerPlane[0]++;
255
m_pszCurPtr += m_iWidth * m_iBPP;
256
if (m_iCurRasterPerPlane[0] == m_iLastRaster)
264
bool ModeJbig::processZJColor(RASTERDATA *input)
266
BYTE *p = m_pszCurPtr + (m_iP[m_iPlaneNumber] * m_iWidth * m_iBPP) * m_iLastRaster;
267
if (input->rasterdata[COLORTYPE_COLOR])
269
for (int i = 0; i < input->rastersize[COLORTYPE_COLOR]; i++)
271
p[i*m_iBPP] = szByte1[input->rasterdata[COLORTYPE_COLOR][i]];
272
p[i*m_iBPP+1] = szByte2[input->rasterdata[COLORTYPE_COLOR][i]];
273
p[i*m_iBPP] |= (p[i*m_iBPP] >> 1);
274
p[i*m_iBPP+1] |= (p[i*m_iBPP+1] >> 1);
278
m_iCurRasterPerPlane[m_iPlaneNumber]++;
279
if (m_iCurRasterPerPlane[m_iPlaneNumber] == m_iLastRaster && m_iPlaneNumber == 3)
282
m_pszCurPtr = m_pszInputRasterData;
287
if (m_iPlaneNumber == 4)
289
m_pszCurPtr += m_iBPP * m_iWidth;
295
bool ModeJbig::NextOutputRaster(RASTERDATA& next_raster)
297
if (iRastersReady == 0)
304
compress(m_iCurrentPlane++);
306
next_raster.rastersize[COLORTYPE_COLOR] = compressedsize;
307
next_raster.rasterdata[COLORTYPE_COLOR] = compressBuf;
308
next_raster.rastersize[COLORTYPE_BLACK] = 0;
309
next_raster.rasterdata[COLORTYPE_BLACK] = NULL;
311
if (m_iPlanes == 1 || (m_iCurrentPlane == 4))
315
m_iLastRaster = m_iOrgHeight;
320
void ModeJbig::compress (int plane_number)
322
HPLJZjcBuff myBuffer;
325
myBuffer.pszCompressedData = compressBuf;
326
myBuffer.dwTotalSize = 0;
327
BYTE *p = m_pszInputRasterData + (m_iWidth * m_iBPP * m_iLastRaster * plane_number);
329
memset (myBuffer.pszCompressedData, 0, m_iWidth * m_iLastRaster * m_iBPP);
330
HPLJJBGCompress (m_iWidth * 8 * m_iBPP, m_iLastRaster, &p, &myBuffer, &se);
332
compressedsize = myBuffer.dwTotalSize;
333
memcpy(compressBuf+compressedsize, &se, sizeof(se));
335
m_iCurRasterPerPlane[plane_number] = 0;
336
m_pszCurPtr = m_pszInputRasterData;
337
int buffer_size = m_iWidth * m_iLastRaster * m_iBPP;
338
memset(p, 0, buffer_size);