1 /*****************************************************************************\
2 Mode2.cpp : implementaiton of Mode2 class
4 Copyright (c) 1996 - 2001, 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 Mode2::Mode2 (unsigned int RasterSize)
37 : Compressor (RasterSize, false)
39 compressBuf = (BYTE *) new BYTE[RasterSize];
40 if (compressBuf == NULL)
41 constructor_error = ALLOCMEM_ERROR;
47 bool Mode2::Process (RASTERDATA* input)
49 BYTE* pDst = compressBuf;
53 (myplane == COLORTYPE_COLOR && input->rasterdata[COLORTYPE_COLOR] == NULL) ||
54 (myplane == COLORTYPE_BLACK && input->rasterdata[COLORTYPE_BLACK] == NULL)) // flushing pipeline
62 unsigned int size = input->rastersize[myplane];
64 for (unsigned int ni = 0; ni < size;)
66 if ( ni + 1 < size && input->rasterdata[myplane][ ni ] == input->rasterdata[myplane][ ni + 1 ] )
68 unsigned int nrepeatcount;
69 for ( ni += 2, nrepeatcount = 1; ni < size && nrepeatcount < 127; ++ni, ++nrepeatcount )
71 if ( input->rasterdata[myplane][ ni ] != input->rasterdata[myplane][ ni - 1 ] )
76 int tmprepeat = 0 - nrepeatcount;
77 BYTE trunc = (BYTE) tmprepeat;
78 pDst[ ndstcount++ ] = trunc;
79 pDst[ ndstcount++ ] = input->rasterdata[myplane][ ni - 1 ];
85 for ( ++ni, nliteralcount = 0; ni < size && nliteralcount < 127; ++ni, ++nliteralcount )
87 if ( input->rasterdata[myplane][ ni ] == input->rasterdata[myplane][ ni - 1 ] )
94 pDst[ ndstcount++ ] = (BYTE) nliteralcount;
95 for ( int nj = 0; nj <= nliteralcount; ++nj )
97 pDst[ ndstcount++ ] = input->rasterdata[myplane][ nfirst++ ];
103 compressedsize = size;
108 bool Mode2::NextOutputRaster(RASTERDATA& next_raster)
110 if (iRastersReady==0){
113 if (myplane == COLORTYPE_COLOR && compressedsize != 0)
115 next_raster.rastersize[COLORTYPE_COLOR] = compressedsize;
116 next_raster.rasterdata[COLORTYPE_COLOR] = compressBuf;
120 next_raster.rastersize[COLORTYPE_COLOR] = 0;
121 next_raster.rasterdata[COLORTYPE_COLOR] = raster.rasterdata[COLORTYPE_COLOR];
124 if (myplane == COLORTYPE_BLACK && compressedsize != 0)
126 next_raster.rastersize[COLORTYPE_BLACK] = compressedsize;
127 next_raster.rasterdata[COLORTYPE_BLACK] = compressBuf;
131 next_raster.rastersize[COLORTYPE_BLACK] = 0;
132 next_raster.rasterdata[COLORTYPE_BLACK] = raster.rasterdata[COLORTYPE_BLACK];