1 /*****************************************************************************\
2 Mode3.cpp : Implimentation for the Mode3 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"
36 Mode3::Mode3 (unsigned int RasterSize) : Compressor (RasterSize, true)
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;
45 memset (SeedRow, 0x0, inputsize);
59 memset (SeedRow, 0x0, inputsize);
62 bool Mode3::Process (RASTERDATA *input)
65 (myplane == COLORTYPE_COLOR && input->rasterdata[COLORTYPE_COLOR] == NULL) ||
66 (myplane == COLORTYPE_BLACK && input->rasterdata[COLORTYPE_BLACK] == NULL)) // flushing pipeline
76 unsigned int uOrgSize = input->rastersize[myplane];
77 unsigned int size = input->rastersize[myplane];
80 BYTE *pszSptr = SeedRow;
81 BYTE *pszInPtr = input->rasterdata[myplane];
84 BYTE *pszOutPtr = compressBuf;
92 while ((*pszSptr == *pszInPtr) && (uOffset < size))
107 pszCurPtr = pszInPtr;
111 while ((*pszSptr != *pszInPtr) && ucByteCount < size && ucByteCount < 8)
120 *pszOutPtr++ = ((ucByteCount << 5) | uOffset);
125 *pszOutPtr++ = ((ucByteCount << 5) | 31);
127 while (uOffset >= 255)
132 *pszOutPtr++ = uOffset;
135 size -= (ucByteCount);
136 memcpy (pszOutPtr, pszCurPtr, ucByteCount);
137 pszOutPtr += ucByteCount;
140 compressedsize = pszOutPtr - compressBuf;
141 memcpy (SeedRow, input->rasterdata[myplane], uOrgSize);
147 bool Mode3::NextOutputRaster (RASTERDATA& next_raster)
149 if (iRastersReady == 0)
154 if (myplane == COLORTYPE_COLOR && compressedsize != 0)
156 next_raster.rastersize[COLORTYPE_COLOR] = compressedsize;
157 next_raster.rasterdata[COLORTYPE_COLOR] = compressBuf;
161 next_raster.rastersize[COLORTYPE_COLOR] = 0;
162 next_raster.rasterdata[COLORTYPE_COLOR] = raster.rasterdata[COLORTYPE_COLOR];
165 next_raster.rastersize[COLORTYPE_BLACK] = raster.rastersize[COLORTYPE_BLACK];
166 next_raster.rasterdata[COLORTYPE_BLACK] = raster.rasterdata[COLORTYPE_BLACK];