Tizen 2.1 base
[platform/upstream/hplip.git] / prnt / hpcups / ModeJbig.cpp
1 /*****************************************************************************\
2   ModeJbig.cpp : Implementation for the ModeJbig class
3
4   Copyright (c) 1996 - 2009, Hewlett-Packard Co.
5   All rights reserved.
6
7   Redistribution and use in source and binary forms, with or without
8   modification, are permitted provided that the following conditions
9   are met:
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.
18
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
30   Author: Naga Samrat Chowdary Narla,
31 \*****************************************************************************/
32
33 #include "CommonDefinitions.h"
34 #include "Compressor.h"
35 #include "Pipeline.h"
36 #include "ModeJbig.h"
37 #include "hpjbig_wrapper.h"
38 #include <dlfcn.h>
39 #include "Utils.h"
40
41 extern "C"
42 {
43 int (*HPLJJBGCompress) (int iWidth, int iHeight, unsigned char **pBuff,
44                         HPLJZjcBuff *pOutBuff, HPLJZjsJbgEncSt *pJbgEncSt);
45 int (*HPLJSoInit) (int iFlag);
46 }
47
48 const BYTE ModeJbig::szByte1[256] =
49     {
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,
71         170, 170, 170, 170,
72     };
73 const BYTE ModeJbig::szByte2[256] =
74     {
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,
96         160, 162, 168, 170,
97     };
98
99
100 ModeJbig::ModeJbig (unsigned int RasterSize) : Compressor (RasterSize, false)
101 {
102     m_hHPLibHandle = 0;
103     m_pszInputRasterData = 0;
104     m_iWidth = ((RasterSize + 31) / 32) * 4;
105     m_iPlaneNumber  = 0;
106     m_iCurrentPlane = 0;
107     m_bLastBand     = false;
108     for (int i = 0; i < 4; i++)
109     {
110         m_iCurRasterPerPlane[i] = 0;
111     }
112
113     m_iP[0] = 0;
114 }
115
116 ModeJbig::~ModeJbig()
117 {
118     if (m_hHPLibHandle)
119     {
120         dlclose(m_hHPLibHandle);
121     }
122     if (m_pszInputRasterData)
123     {
124         delete [] m_pszInputRasterData;
125     }
126 }
127
128 DRIVER_ERROR ModeJbig::Init(int iLastRaster, int iPlanes, int iBPP, ZJPLATFORM zj_platform)
129 {
130     m_iLastRaster  = iLastRaster;
131     m_iOrgHeight   = iLastRaster;
132     m_iPlanes      = iPlanes;
133     m_iBPP         = iBPP;
134     m_ezj_platform = zj_platform;
135
136     m_hHPLibHandle = LoadPlugin ("lj.so");
137     if (m_hHPLibHandle)
138     {
139         dlerror ();
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)))
143         {
144             return PLUGIN_LIBRARY_MISSING;
145         }
146     }
147     else
148     {
149         return PLUGIN_LIBRARY_MISSING;
150     }
151
152     if (iPlanes == 4)
153     {
154         m_iP[0] = 3;
155         m_iP[1] = 0;
156         m_iP[2] = 1;
157         m_iP[3] = 2;
158         if(zj_platform == ZJCOLOR2)
159         {
160           m_iP[1] = 2;
161           m_iP[3] = 0;
162         } 
163     }
164
165     int    buffer_size = m_iWidth * m_iLastRaster * m_iPlanes * m_iBPP;
166     m_pszInputRasterData = new BYTE[buffer_size];
167     if (m_pszInputRasterData == NULL)
168     {
169         return ALLOCMEM_ERROR;
170     }
171     compressBuf = new BYTE[m_iWidth * m_iLastRaster * m_iBPP];
172     if (compressBuf == NULL)
173     {
174         return ALLOCMEM_ERROR;
175     }
176     m_pszCurPtr = m_pszInputRasterData;
177     memset(m_pszCurPtr, 0, buffer_size);
178     return NO_ERROR;
179 }
180
181 void ModeJbig::Flush()
182 {
183     if (m_iCurRasterPerPlane[m_iPlaneNumber] > 0)
184     {
185         int    height = m_iLastRaster;
186         m_iLastRaster = m_iCurRasterPerPlane[m_iPlaneNumber];
187         if (m_iPlanes == 1) {
188             compress();
189             m_iLastRaster = height;
190         }
191         iRastersReady = 1;
192     }
193 }
194
195 bool ModeJbig::Process (RASTERDATA* input)
196 {
197     if (input==NULL)
198     {
199         compressedsize = 0;
200         iRastersReady  = 0;
201         return false;
202     }
203
204     bool    bResult = false;
205     switch(m_ezj_platform)
206     {
207         case ZJSTREAM:
208             bResult = processZJStream(input);
209             break;
210         case ZXSTREAM:
211             bResult = processZXStream(input);
212             break;
213         case ZJCOLOR:
214         case ZJCOLOR2:
215             if (m_iPlanes == 1)
216             {
217                 bResult = processZXStream(input);
218                 break;
219             }
220             bResult = processZJColor(input);
221             break;
222     }
223     if (iRastersReady)
224     {
225         m_iCurRasterPerPlane[m_iPlaneNumber] = 0;
226         m_pszCurPtr = m_pszInputRasterData;
227     }
228     return bResult;
229 }
230
231 bool ModeJbig::processZJStream(RASTERDATA *input)
232 {
233     if (input->rasterdata[COLORTYPE_COLOR])
234     {
235         memcpy(m_pszCurPtr, input->rasterdata[COLORTYPE_COLOR],
236                input->rastersize[COLORTYPE_COLOR]);
237     }
238
239     m_iCurRasterPerPlane[0]++;
240     m_pszCurPtr += m_iWidth;
241
242     if (m_iCurRasterPerPlane[0] == m_iLastRaster)
243     {
244         compress();
245         iRastersReady = 1;
246         return true;
247     }
248     return false;
249 }
250
251 bool ModeJbig::processZXStream(RASTERDATA *input)
252 {
253     if (input->rasterdata[COLORTYPE_COLOR])
254     {
255         for (int i = 0; i < input->rastersize[COLORTYPE_COLOR]; i++)
256         {
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);
261         }
262     }
263
264     m_iCurRasterPerPlane[0]++;
265     m_pszCurPtr += m_iWidth * m_iBPP;
266     if (m_iCurRasterPerPlane[0] == m_iLastRaster)
267     {
268         compress();
269         iRastersReady = 1;
270         return true;
271     }
272     return false;
273 }
274 bool ModeJbig::processZJColor(RASTERDATA *input)
275 {
276     BYTE    *p = m_pszCurPtr + (m_iP[m_iPlaneNumber] * m_iWidth * m_iBPP) * m_iLastRaster;
277     if (input->rasterdata[COLORTYPE_COLOR])
278     {
279         for (int i = 0; i < input->rastersize[COLORTYPE_COLOR]; i++)
280         {
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);
285         }
286     }
287
288     m_iCurRasterPerPlane[m_iPlaneNumber]++;
289     if (m_iCurRasterPerPlane[m_iPlaneNumber] == m_iLastRaster && m_iPlaneNumber == 3)
290     {
291         iRastersReady = 1;
292         m_pszCurPtr = m_pszInputRasterData;
293         m_iPlaneNumber = 0;
294         return true;
295     }
296     m_iPlaneNumber++;
297     if (m_iPlaneNumber == 4)
298     {
299         m_pszCurPtr += m_iBPP * m_iWidth;
300         m_iPlaneNumber = 0;
301     }
302     return false;
303 }
304
305 bool ModeJbig::NextOutputRaster(RASTERDATA& next_raster)
306 {
307     if (iRastersReady == 0)
308     {
309         return false;
310     }
311
312     if (m_iPlanes == 4)
313     {
314         compress(m_iCurrentPlane++);
315     }
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;
320
321     if (m_iPlanes == 1 || (m_iCurrentPlane == 4))
322     {
323         iRastersReady = 0;
324         m_iCurrentPlane = 0;
325         m_iLastRaster = m_iOrgHeight;
326     }
327     return true;
328 }
329
330 void ModeJbig::compress (int plane_number)
331 {
332     HPLJZjcBuff         myBuffer;
333
334     HPLJZjsJbgEncSt   se;
335     myBuffer.pszCompressedData = compressBuf;
336     myBuffer.dwTotalSize = 0;
337     BYTE    *p = m_pszInputRasterData + (m_iWidth * m_iBPP * m_iLastRaster * plane_number);
338
339     memset (myBuffer.pszCompressedData, 0, m_iWidth * m_iLastRaster * m_iBPP);
340     HPLJJBGCompress (m_iWidth * 8 * m_iBPP, m_iLastRaster, &p, &myBuffer, &se);
341
342     compressedsize = myBuffer.dwTotalSize;
343     memcpy(compressBuf+compressedsize, &se, sizeof(se));
344     iRastersReady = 1;
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);
349     return;
350 }
351