blob: e407e313485acad557a7578d65fa34598de41e72 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26/*
27 * This file contains macro definitions for the Encoding category of
28 * the macros used by the generic scaleloop function.
29 *
30 * This implementation uses an ordered dithering error matrix to
31 * produce a moderately high quality version of an image with only
32 * an 8-bit (or less) RGB colormap. The ordered dithering technique
33 * does not rely on the order in which the pixels are processed so
34 * this file can be used in cases where the ImageProducer has not
35 * specified the TopDownLeftRight delivery hint. The ordered dither
36 * technique is also much faster than the Floyd-Steinberg error diffusion
37 * algorithm so this implementation would also be appropriate for
38 * cases where performance is critical such as the processing of a
39 * video stream.
40 *
41 * This file can be used to provide the default implementation of the
42 * Encoding macros for RGB colormapped displays.
43 */
44
45/*
46 * These definitions vector the standard macro names to the "Color"
47 * versions of those macros only if the "DitherDeclared" keyword has
48 * not yet been defined elsewhere. The "DitherDeclared" keyword is
49 * also defined here to claim ownership of the primary implementation
50 * even though this file does not rely on the definitions in any other
51 * files.
52 */
53#ifndef DitherDeclared
54#define DitherDeclared
55#define DeclareDitherVars DeclareAllColorDitherVars
56#define InitDither InitColorDither
57#define StartDitherLine StartColorDitherLine
58#define DitherPixel ColorDitherPixel
59#define DitherBufComplete ColorDitherBufComplete
60#endif
61
62#define DeclareAllColorDitherVars \
63 DeclareColorDitherVars \
64 int relx, rely;
65
66#define DeclareColorDitherVars \
67 extern uns_ordered_dither_array img_oda_red; \
68 extern uns_ordered_dither_array img_oda_green; \
69 extern uns_ordered_dither_array img_oda_blue;
70
71#define InitColorDither(cvdata, clrdata, dstTW) \
72 do {} while (0)
73
74#define StartColorDitherLine(cvdata, dstX1, dstY) \
75 do { \
76 relx = dstX1 & 7; \
77 rely = dstY & 7; \
78 } while (0)
79
80/*
81 * The adjustments below are gross, but they are required due to
82 * the way color lookups are done.
83 * The second set of adjustments simply clips the values generated
84 * by the ordered dithering values to a limit of 256 which represents
85 * full intensity.
86 * The first set of adjustments prepares for the fact that when
87 * the final lookup is done, maximum intensity is represented by
88 * the value 256, but the input values go from 0 to 255. As a
89 * result, the maximum input intensity needs to be mapped from
90 * 255 to 256. The Floyd-Steinberg lookups use a rounding
91 * calculation to handle mapping the values near 255 to the maximum
92 * intensity, but ordered dithering uses a truncating calculation
93 * so the value 255 will be rounded down to the second highest
94 * intensity thereby causing an occasionaly dark pixel when rendering
95 * the maximum input intensity. Other intensities (less than 255)
96 * are left alone since modifying them would slightly disturb their
97 * error distribution. In particular, for red, the value 0xe0 has
98 * a maximum error of 0x1f added to it which must not be mapped to
99 * the maximum intensity since intensity 0xe0 can be represented
100 * exactly. So, a calculated 0xff (0xe0 + 0x1f) needs to be left
101 * less than 256, but a natural 255, or a calculated (>=) 256
102 * should be mapped to maximum intensity.
103 */
104#define ColorDitherPixel(dstX, dstY, pixel, red, green, blue) \
105 do { \
106 if (red == 255) { \
107 red = 256; \
108 } else { \
109 red += img_oda_red[relx][rely]; \
110 if (red > 255) red = 256; \
111 } \
112 if (green == 255) { \
113 green = 256; \
114 } else { \
115 green += img_oda_green[relx][rely]; \
116 if (green > 255) green = 256; \
117 } \
118 if (blue == 255) { \
119 blue = 256; \
120 } else { \
121 blue += img_oda_blue[relx][rely]; \
122 if (blue > 255) blue = 256; \
123 } \
124 pixel = ColorCubeOrdMapUns(red, green, blue); \
125 relx = (relx + 1) & 7; \
126 } while (0)
127
128#define ColorDitherBufComplete(cvdata, dstX1) \
129 do {} while (0)