Tizen 2.1 base
[platform/upstream/hplip.git] / prnt / hpcups / Mode3.cpp
1 /*****************************************************************************\
2   Mode3.cpp : Implimentation for the Mode3 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 "Mode3.h"
35
36 Mode3::Mode3 (unsigned int RasterSize) : Compressor (RasterSize, true)
37 {
38     // Worst case is when two rows are completely different
39     // In that case, one command byte is added for every 8 bytes
40     // In the worst case, compression expands data by 50%
41     compressBuf = new BYTE[RasterSize + RasterSize/2];
42     if (compressBuf == NULL)
43         constructor_error = ALLOCMEM_ERROR;
44     
45     memset (SeedRow, 0x0, inputsize);
46 }
47
48 Mode3::~Mode3 ()
49 {
50 }
51
52 void Mode3::Flush ()
53 {
54     if (!seeded)
55         return;
56     compressedsize = 0;
57     iRastersReady  = 0;
58     seeded         = false;
59     memset (SeedRow, 0x0, inputsize);
60 }
61
62 bool Mode3::Process (RASTERDATA *input)
63 {
64     if (input==NULL || 
65         (myplane == COLORTYPE_COLOR && input->rasterdata[COLORTYPE_COLOR] == NULL) ||
66         (myplane == COLORTYPE_BLACK && input->rasterdata[COLORTYPE_BLACK] == NULL))    // flushing pipeline
67     {
68         Flush();
69         return false;
70     }
71     else
72     {
73         seeded = true;
74     }
75
76     unsigned    int     uOrgSize = input->rastersize[myplane];
77     unsigned    int     size = input->rastersize[myplane];
78     unsigned    int     uOffset;
79
80     BYTE        *pszSptr  = SeedRow;
81     BYTE        *pszInPtr = input->rasterdata[myplane];
82     BYTE        *pszCurPtr;
83     BYTE        ucByteCount;
84     BYTE        *pszOutPtr = compressBuf;
85
86     while (size > 0)
87     {
88         uOffset = 0;
89
90         if (seeded)
91         {
92             while ((*pszSptr == *pszInPtr) && (uOffset < size))
93             {
94                 pszSptr++;
95                 pszInPtr++;
96                 uOffset++;
97             }
98         }
99
100         if (uOffset >= size)
101         {
102           break;
103         }
104
105         size -= uOffset;
106
107         pszCurPtr = pszInPtr;
108         ucByteCount = 1;
109         pszSptr++;
110         pszInPtr++;
111         while ((*pszSptr != *pszInPtr) && ucByteCount < size && ucByteCount < 8)
112         {
113             pszSptr++;
114             pszInPtr++;
115             ucByteCount++;
116         }
117         ucByteCount--;
118         if (uOffset < 31)
119         {
120             *pszOutPtr++ = ((ucByteCount << 5) | uOffset);
121         }
122         else
123         {
124             uOffset -= 31;
125             *pszOutPtr++ = ((ucByteCount << 5) | 31);
126
127             while (uOffset >= 255)
128             {
129                 *pszOutPtr++ = 255;
130                 uOffset -= 255;
131             }
132             *pszOutPtr++ = uOffset;
133         }
134         ucByteCount++;
135         size -= (ucByteCount);
136         memcpy (pszOutPtr, pszCurPtr, ucByteCount);
137         pszOutPtr += ucByteCount;
138     }
139
140     compressedsize = pszOutPtr - compressBuf;
141     memcpy (SeedRow, input->rasterdata[myplane], uOrgSize);
142     seeded = true;
143     iRastersReady = 1;
144     return true;
145 }
146
147 bool Mode3::NextOutputRaster (RASTERDATA& next_raster)
148 {
149         if (iRastersReady == 0)
150         {
151         return false;
152     }
153
154     if (myplane == COLORTYPE_COLOR && compressedsize != 0)
155     {
156         next_raster.rastersize[COLORTYPE_COLOR] = compressedsize;
157         next_raster.rasterdata[COLORTYPE_COLOR] = compressBuf;
158     }
159     else
160     {
161         next_raster.rastersize[COLORTYPE_COLOR] = 0;
162         next_raster.rasterdata[COLORTYPE_COLOR] = raster.rasterdata[COLORTYPE_COLOR];
163     }
164
165     next_raster.rastersize[COLORTYPE_BLACK] = raster.rastersize[COLORTYPE_BLACK];
166     next_raster.rasterdata[COLORTYPE_BLACK] = raster.rasterdata[COLORTYPE_BLACK];
167
168     iRastersReady = 0;
169     return true;
170 }
171