Tizen 2.1 base
[platform/upstream/hplip.git] / prnt / hpcups / Mode2.cpp
1 /*****************************************************************************\
2   Mode2.cpp : implementaiton of Mode2 class
3
4   Copyright (c) 1996 - 2001, 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 "Mode2.h"
35
36 Mode2::Mode2 (unsigned int RasterSize)
37     : Compressor (RasterSize, false)
38 {
39     compressBuf = (BYTE *) new BYTE[RasterSize];
40         if (compressBuf == NULL)
41             constructor_error = ALLOCMEM_ERROR;
42 }
43
44 Mode2::~Mode2()
45 { }
46
47 bool Mode2::Process (RASTERDATA* input)
48 {
49     BYTE* pDst = compressBuf;
50     int ndstcount = 0;
51
52     if (input==NULL || 
53         (myplane == COLORTYPE_COLOR && input->rasterdata[COLORTYPE_COLOR] == NULL) ||
54         (myplane == COLORTYPE_BLACK && input->rasterdata[COLORTYPE_BLACK] == NULL))    // flushing pipeline
55     {
56         compressedsize=0;
57
58         iRastersReady=0;
59         return false;
60     }
61
62     unsigned int size = input->rastersize[myplane];
63
64     for (unsigned int ni = 0; ni < size;)
65     {
66         if ( ni + 1 < size && input->rasterdata[myplane][ ni ] == input->rasterdata[myplane][ ni + 1 ] )
67         {
68             unsigned int nrepeatcount;
69             for ( ni += 2, nrepeatcount = 1; ni < size && nrepeatcount < 127; ++ni, ++nrepeatcount )
70             {
71                 if ( input->rasterdata[myplane][ ni ] != input->rasterdata[myplane][ ni - 1 ] )
72                 {
73                     break;
74                 }
75             }
76             int tmprepeat = 0 - nrepeatcount;
77             BYTE trunc = (BYTE) tmprepeat;
78             pDst[ ndstcount++ ] = trunc;
79             pDst[ ndstcount++ ] = input->rasterdata[myplane][ ni - 1 ];
80         }
81         else
82         {
83             int nliteralcount;
84             int nfirst = ni;
85             for ( ++ni, nliteralcount = 0; ni < size && nliteralcount < 127; ++ni, ++nliteralcount )
86             {
87                 if ( input->rasterdata[myplane][ ni ] == input->rasterdata[myplane][ ni - 1 ] )
88                 {
89                     --ni;
90                     --nliteralcount;
91                     break;
92                 }
93             }
94             pDst[ ndstcount++ ] = (BYTE) nliteralcount;
95             for ( int nj = 0; nj <= nliteralcount; ++nj )
96             {
97                 pDst[ ndstcount++ ] = input->rasterdata[myplane][ nfirst++ ];
98             }
99         }
100     }
101
102     size = ndstcount;
103     compressedsize = size;
104     iRastersReady = 1;
105     return true;
106 }
107
108 bool Mode2::NextOutputRaster(RASTERDATA& next_raster)
109 {
110     if (iRastersReady==0){
111         return false;
112     }
113     if (myplane == COLORTYPE_COLOR && compressedsize != 0)
114     {
115         next_raster.rastersize[COLORTYPE_COLOR] = compressedsize;
116         next_raster.rasterdata[COLORTYPE_COLOR] = compressBuf;
117     }
118     else
119     {
120         next_raster.rastersize[COLORTYPE_COLOR] = 0;
121         next_raster.rasterdata[COLORTYPE_COLOR] = raster.rasterdata[COLORTYPE_COLOR];
122     }
123
124     if (myplane == COLORTYPE_BLACK && compressedsize != 0)
125     {
126         next_raster.rastersize[COLORTYPE_BLACK] = compressedsize;
127         next_raster.rasterdata[COLORTYPE_BLACK] = compressBuf;
128     }
129     else
130     {
131         next_raster.rastersize[COLORTYPE_BLACK] = 0;
132         next_raster.rasterdata[COLORTYPE_BLACK] = raster.rasterdata[COLORTYPE_BLACK];
133     }
134
135     iRastersReady=0;
136     return true;
137 }
138