Tizen 2.1 base
[platform/upstream/hplip.git] / prnt / hpijs / htmtxhi.cpp
1 /*****************************************************************************\
2   htmtxhi.cpp : Implimentation for Multilevel (HiFipe) dither matrix
3
4   Copyright (c) 1994 - 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 //
33 //  Filename     :  HTMTXHI.CPP
34 //
35 //  Module       :  Interlaken
36 //
37 //  Description  :  Multilevel(HiFipe) dither matrix based halftoning
38 //
39 //============================================================================
40
41 //=============================================================================
42 //  Header file dependencies
43 //=============================================================================
44
45 /*
46 #ifndef INCLUDED_HPENVTYP
47 #include "HPEnvTyp.h"
48 #endif
49 #ifndef INCLUDED_HTINTF
50 #include "HTIntf.h"
51 #endif
52 #ifndef INCLUDED_HTWORLD
53 #include "HTWorld.h"
54 #endif
55 #ifndef INCLUDED_HPASSERT
56 #include "HPAssert.h"
57 #endif
58 #ifndef INCLUDED_HTMATRIX
59 #include "htmatrix.h"
60 #endif
61 */
62
63 #include "header.h"
64 #include "halftoner.h"
65
66 APDK_BEGIN_NAMESPACE
67
68 #define Dither4LevelHiFipe(matrix,bitMask)\
69 {\
70     tone = (*inputPtr++ );\
71     fedResPtr = fedResTbl + (tone << 2);\
72     level = *fedResPtr++;\
73     if (*(fedResPtr) >= (HPByte)matrix )\
74     {\
75             level++;\
76     }\
77         switch (level)\
78         {\
79             case 0:\
80                 break;\
81             case 1:\
82                 rasterByte1 |= bitMask;\
83                 break;\
84             case 2:\
85                 rasterByte2 |= bitMask;\
86                 break;\
87             case 3:\
88                 rasterByte2 |= bitMask; rasterByte1 |= bitMask;\
89                 break;\
90             case 4:\
91                 rasterByte3 |= bitMask;\
92                 break;\
93             case 5:\
94                 rasterByte3 |= bitMask; rasterByte1 |= bitMask;\
95                 break;\
96             case 6:\
97                 rasterByte3 |= bitMask; rasterByte2 |= bitMask;\
98                 break;\
99             case 7:\
100                 rasterByte3 |= bitMask; rasterByte2 |= bitMask; rasterByte1 |= bitMask;\
101                 break;\
102         }\
103 }
104
105 // Each dither matrix has a header of five bytes
106 //    1st byte    (Size (in bytes) of row in the matrix) - 1
107 //    2nd byte    Size (in bytes) of row in the matrix
108 //    3rd byte    Black offset
109 //    4th byte    Cyan offset
110 //    5th byte    Magenta offset
111 //
112 //    With matrix actually starting on the 6th Byte.
113 //
114 //    Black, cyan, and magenta are all offset into different
115 //    locations in the dither matrix.  (Yellow uses the same offsets as
116 //    magenta)
117 //
118
119 // Ditherparms should be set up as Fed with the following additions
120 // ditherParms->fSqueezeOffset = first dirty pixel, if white has been skipped at the beginning of the row
121 // ditherParmsPtr->fMatrixRowSize = Byte 2 of the Header
122 // ditherParmsPtr->fDitherCellOffset = Black, Cyan, or Magenta Offset respectively
123
124 //    To select the right row of the matrix for each color
125 //
126 //    ditherParms->fMatrixV1 = ((matrixStart + rasterIndex + DitherCellOffset) % (MatrixRowSize)) * (MatrixRowSize)
127
128 DRIVER_ERROR Halftoner::HTMATRIXHI_KCMY   (THTDitherParmsPtr ditherParmsPtr,
129                            HPUInt16          count)
130 {
131 #if HPUNUSEDVAR
132 #pragma unused(envP1)
133 #endif
134
135     THTDitherParmsPtr       ditherParms = ditherParmsPtr+count;
136     HPByte                  tone;
137     HPUInt8                 rasterByte1,rasterByte2, rasterByte3;
138     HPUInt8                 level;
139     HPInt16                 pixelCount;
140
141     HPUInt16                numLoop = ditherParms->fNumPix;
142     HPBytePtr               inputPtr = ditherParms->fInput;
143     HPBytePtr               outputPtr1, outputPtr2, outputPtr3;
144
145     HPBytePtr               fedResTbl = (HPBytePtr)ditherParms->fFEDResPtr;
146     HPBytePtr               fedResPtr;
147
148     HPBytePtr       matrixV    = ditherParms->fMatrixV1;
149     HPBytePtr       matrixH;
150     HPUInt16        index;
151     HPUInt16        matrixRowSize = ditherParms->fMatrixRowSize;
152
153     rasterByte1 = 0;
154     rasterByte2 = 0;
155     rasterByte3 = 0;
156
157     outputPtr1 = ditherParms->fOutput1;
158     outputPtr2 = ditherParms->fOutput2;
159     outputPtr3 = ditherParms->fOutput3;
160
161     if (!ditherParms->fSymmetricFlag)
162         return SYSTEM_ERROR;    // matrixHI_KCMY asymetric not supported
163
164     index = (ditherParms->fDitherCellOffset + ditherParms->fSqueezeOffset) % (matrixRowSize);
165     matrixH = matrixV + index;
166
167     for (pixelCount = numLoop + 8; (pixelCount -= 8) > 0; )
168     {
169         // if we've reached end of matrix we need to reset
170         // our row pointer to the start of the row
171         if ( index == matrixRowSize )
172         {
173             matrixH = matrixV;
174             index = 0;
175         }
176
177         Dither4LevelHiFipe(*matrixH,0x80);
178         matrixH++;
179         Dither4LevelHiFipe(*matrixH,0x40);
180         matrixH++;
181         Dither4LevelHiFipe(*matrixH,0x20);
182         matrixH++;
183         Dither4LevelHiFipe(*matrixH,0x10);
184         matrixH++;
185         Dither4LevelHiFipe(*matrixH,0x08);
186         matrixH++;
187         Dither4LevelHiFipe(*matrixH,0x04);
188         matrixH++;
189         Dither4LevelHiFipe(*matrixH,0x02);
190         matrixH++;
191         Dither4LevelHiFipe(*matrixH,0x01);
192         matrixH++;
193
194         *outputPtr1++ |= rasterByte1;
195         if (outputPtr2)
196             *outputPtr2++ |= rasterByte2;
197         rasterByte1 = 0;
198         rasterByte2 = 0;
199
200         index += 8;
201     }
202     return NO_ERROR;
203 }
204
205 unsigned char BayerMatrix[] =
206 {
207     0x07,   0x08,   0x00,   0x00,   0x00,
208     0x2,    0x82,   0x22,   0xa2,   0xa,    0x8a,   0x2a,   0xaa,
209     0xc2,   0x42,   0xe2,   0x62,   0xca,   0x4a,   0xea,   0x6a,
210     0x32,   0xb2,   0x12,   0x92,   0x3a,   0xba,   0x1a,   0x9a,
211     0xf2,   0x72,   0xd2,   0x52,   0xfa,   0x7a,   0xda,   0x5a,
212     0xe,    0x8e,   0x2e,   0xae,   0x6,    0x86,   0x26,   0xa6,
213     0xce,   0x4e,   0xee,   0x6e,   0xc6,   0x46,   0xe6,   0x66,
214     0x3e,   0xbe,   0x1e,   0x9e,   0x36,   0xb6,   0x16,   0x96,
215     0xfe,   0x7e,   0xde,   0x5e,   0xf6,   0x76,   0xd6,   0x56,
216 };
217
218 APDK_END_NAMESPACE
219
220