~ubuntu-branches/ubuntu/raring/hplip/raring

« back to all changes in this revision

Viewing changes to prnt/hpcups/ModeJbig.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mark Purcell
  • Date: 2009-12-14 20:08:44 UTC
  • mfrom: (2.1.118 lucid)
  • Revision ID: james.westby@ubuntu.com-20091214200844-z8qhqwgppbu3t7ze
Tags: 3.9.10-4
KBSD patch from KiBi (Closes: #560796)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
31
#include "CommonDefinitions.h"
 
32
#include "Compressor.h"
 
33
#include "Pipeline.h"
 
34
#include "ModeJbig.h"
 
35
#include "hpjbig_wrapper.h"
 
36
#include <dlfcn.h>
 
37
#include "Utils.h"
 
38
 
 
39
extern "C"
 
40
{
 
41
int (*HPLJJBGCompress) (int iWidth, int iHeight, unsigned char **pBuff,
 
42
                        HPLJZjcBuff *pOutBuff, HPLJZjsJbgEncSt *pJbgEncSt);
 
43
int (*HPLJSoInit) (int iFlag);
 
44
}
 
45
 
 
46
const BYTE ModeJbig::szByte1[256] =
 
47
    {
 
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,
 
69
        170, 170, 170, 170,
 
70
    };
 
71
const BYTE ModeJbig::szByte2[256] =
 
72
    {
 
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,
 
94
        160, 162, 168, 170,
 
95
    };
 
96
 
 
97
 
 
98
ModeJbig::ModeJbig (unsigned int RasterSize) : Compressor (RasterSize, false)
 
99
{
 
100
    m_iWidth = ((RasterSize + 31) / 32) * 4;
 
101
    m_iPlaneNumber  = 0;
 
102
    m_iCurrentPlane = 0;
 
103
    m_bLastBand     = false;
 
104
    for (int i = 0; i < 4; i++)
 
105
    {
 
106
        m_iCurRasterPerPlane[i] = 0;
 
107
    }
 
108
 
 
109
    m_iP[0] = 0;
 
110
}
 
111
 
 
112
ModeJbig::~ModeJbig()
 
113
{
 
114
    if (m_hHPLibHandle)
 
115
    {
 
116
        dlclose(m_hHPLibHandle);
 
117
    }
 
118
    if (m_pszInputRasterData)
 
119
    {
 
120
        delete [] m_pszInputRasterData;
 
121
    }
 
122
}
 
123
 
 
124
DRIVER_ERROR ModeJbig::Init(int iLastRaster, int iPlanes, int iBPP, ZJPLATFORM zj_platform)
 
125
{
 
126
    m_iLastRaster  = iLastRaster;
 
127
    m_iOrgHeight   = iLastRaster;
 
128
    m_iPlanes      = iPlanes;
 
129
    m_iBPP         = iBPP;
 
130
    m_ezj_platform = zj_platform;
 
131
 
 
132
    m_hHPLibHandle = LoadPlugin ("lj.so");
 
133
    if (m_hHPLibHandle)
 
134
    {
 
135
        dlerror ();
 
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)))
 
139
        {
 
140
            return PLUGIN_LIBRARY_MISSING;
 
141
        }
 
142
    }
 
143
    else
 
144
    {
 
145
        return PLUGIN_LIBRARY_MISSING;
 
146
    }
 
147
 
 
148
    if (iPlanes == 4)
 
149
    {
 
150
        m_iP[0] = 3;
 
151
        m_iP[1] = 0;
 
152
        m_iP[2] = 1;
 
153
        m_iP[3] = 2;
 
154
    }
 
155
 
 
156
    int    buffer_size = m_iWidth * m_iLastRaster * m_iPlanes * m_iBPP;
 
157
    m_pszInputRasterData = new BYTE[buffer_size];
 
158
    if (m_pszInputRasterData == NULL)
 
159
    {
 
160
        return ALLOCMEM_ERROR;
 
161
    }
 
162
    compressBuf = new BYTE[m_iWidth * m_iLastRaster * m_iBPP];
 
163
    if (compressBuf == NULL)
 
164
    {
 
165
        return ALLOCMEM_ERROR;
 
166
    }
 
167
    m_pszCurPtr = m_pszInputRasterData;
 
168
    memset(m_pszCurPtr, 0, buffer_size);
 
169
    return NO_ERROR;
 
170
}
 
171
 
 
172
void ModeJbig::Flush()
 
173
{
 
174
    if (m_iCurRasterPerPlane[m_iPlaneNumber] > 0)
 
175
    {
 
176
        int    height = m_iLastRaster;
 
177
        m_iLastRaster = m_iCurRasterPerPlane[m_iPlaneNumber];
 
178
        if (m_iPlanes == 1) {
 
179
            compress();
 
180
            m_iLastRaster = height;
 
181
        }
 
182
        iRastersReady = 1;
 
183
    }
 
184
}
 
185
 
 
186
bool ModeJbig::Process (RASTERDATA* input)
 
187
{
 
188
    if (input==NULL)
 
189
    {
 
190
        compressedsize = 0;
 
191
        iRastersReady  = 0;
 
192
        return false;
 
193
    }
 
194
 
 
195
    bool    bResult = false;
 
196
    switch(m_ezj_platform)
 
197
    {
 
198
        case ZJSTREAM:
 
199
            bResult = processZJStream(input);
 
200
            break;
 
201
        case ZXSTREAM:
 
202
            bResult = processZXStream(input);
 
203
            break;
 
204
        case ZJCOLOR:
 
205
            if (m_iPlanes == 1)
 
206
            {
 
207
                bResult = processZXStream(input);
 
208
                break;
 
209
            }
 
210
            bResult = processZJColor(input);
 
211
            break;
 
212
    }
 
213
    if (iRastersReady)
 
214
    {
 
215
        m_iCurRasterPerPlane[m_iPlaneNumber] = 0;
 
216
        m_pszCurPtr = m_pszInputRasterData;
 
217
    }
 
218
    return bResult;
 
219
}
 
220
 
 
221
bool ModeJbig::processZJStream(RASTERDATA *input)
 
222
{
 
223
    if (input->rasterdata[COLORTYPE_COLOR])
 
224
    {
 
225
        memcpy(m_pszCurPtr, input->rasterdata[COLORTYPE_COLOR],
 
226
               input->rastersize[COLORTYPE_COLOR]);
 
227
    }
 
228
 
 
229
    m_iCurRasterPerPlane[0]++;
 
230
    m_pszCurPtr += m_iWidth;
 
231
 
 
232
    if (m_iCurRasterPerPlane[0] == m_iLastRaster)
 
233
    {
 
234
        compress();
 
235
        iRastersReady = 1;
 
236
        return true;
 
237
    }
 
238
    return false;
 
239
}
 
240
 
 
241
bool ModeJbig::processZXStream(RASTERDATA *input)
 
242
{
 
243
    if (input->rasterdata[COLORTYPE_COLOR])
 
244
    {
 
245
        for (int i = 0; i < input->rastersize[COLORTYPE_COLOR]; i++)
 
246
        {
 
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);
 
251
        }
 
252
    }
 
253
 
 
254
    m_iCurRasterPerPlane[0]++;
 
255
    m_pszCurPtr += m_iWidth * m_iBPP;
 
256
    if (m_iCurRasterPerPlane[0] == m_iLastRaster)
 
257
    {
 
258
        compress();
 
259
        iRastersReady = 1;
 
260
        return true;
 
261
    }
 
262
    return false;
 
263
}
 
264
bool ModeJbig::processZJColor(RASTERDATA *input)
 
265
{
 
266
    BYTE    *p = m_pszCurPtr + (m_iP[m_iPlaneNumber] * m_iWidth * m_iBPP) * m_iLastRaster;
 
267
    if (input->rasterdata[COLORTYPE_COLOR])
 
268
    {
 
269
        for (int i = 0; i < input->rastersize[COLORTYPE_COLOR]; i++)
 
270
        {
 
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);
 
275
        }
 
276
    }
 
277
 
 
278
    m_iCurRasterPerPlane[m_iPlaneNumber]++;
 
279
    if (m_iCurRasterPerPlane[m_iPlaneNumber] == m_iLastRaster && m_iPlaneNumber == 3)
 
280
    {
 
281
        iRastersReady = 1;
 
282
        m_pszCurPtr = m_pszInputRasterData;
 
283
        m_iPlaneNumber = 0;
 
284
        return true;
 
285
    }
 
286
    m_iPlaneNumber++;
 
287
    if (m_iPlaneNumber == 4)
 
288
    {
 
289
        m_pszCurPtr += m_iBPP * m_iWidth;
 
290
        m_iPlaneNumber = 0;
 
291
    }
 
292
    return false;
 
293
}
 
294
 
 
295
bool ModeJbig::NextOutputRaster(RASTERDATA& next_raster)
 
296
{
 
297
    if (iRastersReady == 0)
 
298
    {
 
299
        return false;
 
300
    }
 
301
 
 
302
    if (m_iPlanes == 4)
 
303
    {
 
304
        compress(m_iCurrentPlane++);
 
305
    }
 
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;
 
310
 
 
311
    if (m_iPlanes == 1 || (m_iCurrentPlane == 4))
 
312
    {
 
313
        iRastersReady = 0;
 
314
        m_iCurrentPlane = 0;
 
315
        m_iLastRaster = m_iOrgHeight;
 
316
    }
 
317
    return true;
 
318
}
 
319
 
 
320
void ModeJbig::compress (int plane_number)
 
321
{
 
322
    HPLJZjcBuff         myBuffer;
 
323
 
 
324
    HPLJZjsJbgEncSt   se;
 
325
    myBuffer.pszCompressedData = compressBuf;
 
326
    myBuffer.dwTotalSize = 0;
 
327
    BYTE    *p = m_pszInputRasterData + (m_iWidth * m_iBPP * m_iLastRaster * plane_number);
 
328
 
 
329
    memset (myBuffer.pszCompressedData, 0, m_iWidth * m_iLastRaster * m_iBPP);
 
330
    HPLJJBGCompress (m_iWidth * 8 * m_iBPP, m_iLastRaster, &p, &myBuffer, &se);
 
331
 
 
332
    compressedsize = myBuffer.dwTotalSize;
 
333
    memcpy(compressBuf+compressedsize, &se, sizeof(se));
 
334
    iRastersReady = 1;
 
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);
 
339
    return;
 
340
}
 
341