Tizen 2.1 base
[platform/upstream/hplip.git] / prnt / hpcups / Halftoner.h
1 /*****************************************************************************\
2   Halftoner.h : Interface for the Halftoner 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
32 #ifndef HALFTONER_H
33 #define HALFTONER_H
34
35 ////////////////////////////////////////////////////////////////////////////
36 // UMPQUA
37 //
38 // Encapsulation of buffers and data needed by Halftoner color-matching and
39 // halftoning code.
40
41 class Halftoner : public Processor
42 {
43 public:
44     Halftoner(PrintMode* pPM,
45         unsigned int iInputWidth,
46         int iNumRows[],        // for mixed-res cases
47         int HiResFactor,        // when base-res is multiple of 300
48         bool matrixbased
49         );
50     virtual ~Halftoner();
51
52     bool Process(RASTERDATA* pbyInputKRGBRaster);
53     void Flush();
54     void Restart();         // set up for new page or blanks
55
56     // items required by Processor
57     unsigned int GetMaxOutputWidth();
58     bool    NextOutputRaster(RASTERDATA &next_raster);
59     bool LastPlane();
60     bool FirstPlane();
61     unsigned int PlaneSize()
62         { return OutputWidth[iColor] / 8 + (OutputWidth[iColor] % 8); }
63
64     unsigned int ColorPlaneCount;
65     unsigned int InputWidth;    // # of pixels input per colorplane
66     unsigned int OutputWidth[MAXCOLORPLANES];   // # of pixels output per colorplane
67     unsigned char ColorDepth[MAXCOLORPLANES];
68     // how many rows needed relative to base resolution -- all 1 unless mixed-res
69     unsigned char NumRows[MAXCOLORPLANES];
70     // color plane data
71     // for current interface, we must maintain mapping of
72     //  0=K, 1=C, 2=M, 3=Y
73     BYTE* ColorPlane[MAXCOLORPLANES][MAXCOLORROWS][MAXCOLORDEPTH];
74
75     unsigned int StartPlane;    // since planes are ordered KCMY, if no K, this is 1
76     unsigned int EndPlane;      // usually Y, could be Mlight
77     int ResBoost;
78
79 private:
80     void Interpolate(const uint32_t *start,const unsigned long i,
81                      unsigned char r,unsigned char g,unsigned char b,
82                      unsigned char *blackout, unsigned char *cyanout,
83                       unsigned char *magentaout, unsigned char *yellowout, bool);
84     void FreeBuffers();
85
86     bool Forward16PixelsNonWhite(BYTE * inputPtr)
87     {
88 //        return ((*(uint32_t *)(inputPtr) != 0x0) || (*(((uint32_t *)(inputPtr)) + 1) != 0x0)  ||
89 //            (*(((uint32_t *)(inputPtr)) + 2) != 0x0) || (*(((uint32_t *)(inputPtr)) + 3) != 0x0));
90         for (int i=0; i < 16; i++)
91         {
92             if ((*inputPtr++) != 0)
93             {
94                 return true;
95             }
96         }
97         return false;
98     }
99
100     bool Backward16PixelsNonWhite(BYTE * inputPtr)
101     {
102 //        return ((*(uint32_t *)(inputPtr) != 0x0) || (*(((uint32_t *)(inputPtr)) - 1) != 0x0)  ||
103 //            (*(((uint32_t *)(inputPtr)) - 2) != 0x0) || (*(((uint32_t *)(inputPtr)) - 3) != 0x0));
104         for (int i=0; i < 16; i++)
105         {
106             if ((*inputPtr--) !=0 )
107             {
108                 return true;
109             }
110         }
111         return false;
112     }
113     unsigned int getOutputWidth();
114
115     short* ErrBuff[6];
116
117     short   nNextRaster;
118
119     short          fRasterOdd;
120     const unsigned char* fBlackFEDResPtr;
121     const unsigned char* fColorFEDResPtr;
122
123     unsigned int AdjustedInputWidth;    // InputWidth padded to be divisible by 8
124     void PixelMultiply(unsigned char* buffer, unsigned int width, unsigned int factor);
125
126     unsigned int iColor, iRow, iPlane;
127     unsigned int PlaneCount();          // tells how many layers (colors,hifipe,multirow)
128
129     bool started;
130
131     BYTE* tempBuffer;
132     BYTE* tempBuffer2;
133     BYTE* originalKData;
134     unsigned int oddbits;
135     void CleanOddBits(unsigned int iColor, unsigned int iRow);
136
137     uint32_t hold_random;
138     inline BYTE RandomNumber()
139     {
140        hold_random = (hold_random * 214013) + 2531011;
141        return (BYTE)((hold_random >> 16) & 0xFF);
142     } //RandomNumber
143
144
145     typedef struct THTDitherParms
146     {
147         unsigned short    fNumPix;            // Dirty Pixels to be dithered
148         BYTE *   fInput;             // Pixel array to dither
149         BYTE *   fOutput1;           // Output raster binary & hifipe plane 1
150         BYTE *   fOutput2;           // Output raster hifipe plane 2 (2-bit)
151         BYTE *   fOutput3;           // Output raster hifipe plane 3 (3-bit)
152
153         const BYTE *  fFEDResPtr;         // brkpnt table
154
155         short *    fErr;            // Current error buffer
156         short                 fRasterEvenOrOdd;// Serpentine (Forward/Backward)
157
158         bool                  fSymmetricFlag;   // Are we symmetric
159
160         bool                  fHifipe;          // Are we doing Hifipe?
161
162         unsigned short       fMatrixRowSize;
163         BYTE *               fMatrixV1;
164         unsigned short       fDitherCellOffset;
165         unsigned short       fSqueezeOffset;
166
167         short *             fWeightTablePtr; // Error Diffusion threshold table
168         short                 fOffsetPick;     // Random# offset for threshold
169         bool                  fVerticalExpFlag; // Are we vertically expanding
170     } THTDitherParms, *THTDitherParmsPtr;
171
172     bool usematrix;
173
174     DRIVER_ERROR HTMATRIXHI_KCMY(THTDitherParmsPtr ditherParmsPtr, unsigned short count);
175
176     BYTE HPRand() // normalize to 5..79
177     { BYTE b=RandomNumber() % 74; b+= 5; return b; }
178
179
180     void HTEDiffOpen   (THTDitherParmsPtr ditherParmsPtr,
181         unsigned short          count);
182
183     THTDitherParms  fDitherParms[6];
184
185     THTDitherParmsPtr    ditherParms;
186     short                tone;
187     short                *diffusionErrorPtr;
188     short                tmpShortStore;
189     BYTE                 rasterByte1, rasterByte2, rasterByte3;
190     BYTE                 level;
191     short                pixelCount;
192     short                thValue;
193
194     short                *errPtr;
195     unsigned short       numLoop;
196
197     BYTE                 *inputPtr;
198     BYTE                 *outputPtr1;
199     BYTE                 *outputPtr2;
200     BYTE                 *outputPtr3;
201
202     const BYTE           *fedResTbl;
203     const BYTE           *fedResPtr;
204
205     bool                  symmetricFlag;
206
207     bool                  doNext8Pixels;
208
209     bool                  hifipe;
210
211     void BACKWARD_FED(short thresholdValue, unsigned int bitMask);
212     void FORWARD_FED(short thresholdValue, unsigned int bitMask);
213 }; // Halftoner
214
215 #endif // HALFTONER_H
216