Debian Codegraph @

xo/dither.cxx

raw
1 /* dither.cxx
2 $Id: dither.cxx,v 1.3 1997/10/18 04:57:32 elf Exp $
3
4 written by Marc Singer
5 22 March 1997
6
7 This file is part of the project XO. See the file README for
8 more information.
9
10 Copyright (C) 1997 Marc Singer
11
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 in a file called COPYING along with this program; if not, write to
24 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
25 02139, USA.
26
27 -----------
28 DESCRIPTION
29 -----------
30
31 Creates an 8bpp bitmap from the given 24bpp image using a fast
32 ordered dither.
33
34 */
35
36 #include "standard.h"
37 #include "dither.h"
38
39 //#include <math.h>
40 #include <assert.h>
41 #include <stdlib.h>
42
43
44 typedef unsigned long DWORD;
45 typedef unsigned short WORD;
46 typedef short UINT;
47 typedef unsigned char BYTE;
48
49 typedef struct {
50 // UINT bfType; //'B' 'M'
51 DWORD bfSize;
52 UINT bfReserved1;
53 UINT bfReserved2;
54 DWORD bfOffBits;
55 } HDR_DIBFILE;
56
57 typedef struct{
58 DWORD biSize;
59 DWORD biWidth;
60 DWORD biHeight;
61 WORD biPlanes;
62 WORD biBitCount;
63 DWORD biCompression;
64 DWORD biSizeImage;
65 DWORD biXPelsPerMeter;
66 DWORD biYPelsPerMeter;
67 DWORD biClrUsed;
68 DWORD biClrImportant;
69 } HDR_DIB;
70
71 typedef struct {
72 unsigned8 rgbBlue;
73 unsigned8 rgbGreen;
74 unsigned8 rgbRed;
75 unsigned8 rgbReserved;
76 } RGBQUAD;
77
78 #define ROWWIDTH(width,depth) ((((width)*(depth))/8 + 3)& ~0x3)
79 #define _rgb(r,g,b) (b),(g),(r)
80 #define _error(peThis,pePrev,i,c) \
81 ((((peThis[((i) - 1)*3 + (c)])*7) \
82 + ((pePrev[((i) - 1)*3 + (c)])*3) \
83 + ((pePrev[((i) )*3 + (c)])*5))/16)
84
85
86 /* LDither::create_matrix
87
88 creates the dithering matrix for the specified dimension. The
89 values are scaled by C_SHADES_MAX.
90
91 */
92
93 void LDither::create_matrix (int dim)
94 {
95 if (m_dim == dim && m_pMatrix)
96 return;
97
98 if (m_pMatrix) {
99 free (m_pMatrix);
100 m_pMatrix = NULL;
101 }
102
103 int x, y, *dat;
104
105 assert (dim == 4); // If this fails, we need to eliminate
106 // some of the performance code in
107 // dither.h
108
109 m_dim = (1 << dim);
110 m_dim2 = m_dim*m_dim;
111
112 // Precompute some inner-loop values
113 m_level_red = levels (m_cRed);
114 m_level_green = levels (m_cGreen);
115 m_level_blue = levels (m_cBlue);
116 m_dither_divisor = m_dim2*C_SHADES_MAX;
117
118 m_pMatrix = (int **) malloc ((m_dim *sizeof(int*)) + // pointers
119 (m_dim2*sizeof(int))); // data
120
121 assert (m_pMatrix);
122
123 dat = (int*) &m_pMatrix[m_dim];
124 for (y = 0; y < m_dim; y++)
125 m_pMatrix[y] = &dat[y*m_dim];
126
127 for (y = 0; y < m_dim; y++)
128 for (x = 0; x < m_dim; x++)
129 m_pMatrix[y][x] = C_SHADES_MAX*matrix_value (y, x, dim);
130 }
131
132
133 /* LDither::dither
134
135 dithers cy rows of cx pixels. The source pixels are in Windows 24
136 bit DIB format. The output is Windows 8 bit DIB format.
137
138 */
139
140 void LDither::dither (int xOrg, int yOrg, int cx, int cy,
141 unsigned8* pbSrc, unsigned8* pbDst)
142 {
143 int y, mask = (m_dim - 1);
144 register int x, d;
145 register int *pMatrix;
146 int cbRowSrcExtra = ROWWIDTH (cx, 24) - cx*3;
147 int cbRowDstExtra = ROWWIDTH (cx, 8) - cx;
148
149 for (y = 0; y < cy; ++y, pbSrc += cbRowSrcExtra, pbDst += cbRowDstExtra)
150 for (pMatrix = m_pMatrix[(y + yOrg) & mask], x = cx; x--; pbSrc += 3) {
151 d = pMatrix[(x + xOrg) & mask];
152 #if 0
153 *pbDst++ = color_index (dither (pbSrc[2], d, m_cRed),
154 dither (pbSrc[1], d, m_cGreen),
155 dither (pbSrc[0], d, m_cBlue));
156 #else
157 *pbDst++ = color_index (dither_red (pbSrc[2], d),
158 dither_green (pbSrc[1], d),
159 dither_blue (pbSrc[0], d));
160 #endif
161 }
162 }
163
164
165 /* LDither::init
166
167 initialize the dithering control structures. This includes the
168 scalar parameters, the lookup table and the dithering matrix.
169
170 */
171
172 void LDither::init (int dim, int cRed, int cGreen, int cBlue)
173 {
174 if (m_cRed == cRed && m_cGreen == cGreen && m_cBlue == cBlue && m_rgRGB)
175 return;
176
177 m_cRed = cRed;
178 m_cGreen = cGreen;
179 m_cBlue = cBlue;
180 m_cColors = cRed*cGreen*cBlue;
181
182 if (m_cColors > C_COLOR_MAX) {
183 fprintf (stderr, "too many shades %d, max %d\n", m_cColors, C_COLOR_MAX);
184 return;
185 }
186 if (m_cRed < 2) {
187 fprintf (stderr, "too few shades for red, minimum of 2\n");
188 return;
189 }
190 if (m_cGreen < 2)
191 fprintf (stderr, "too few shades for green, minimum of 2\n");
192 if (m_cBlue < 2)
193 fprintf (stderr, "too few shades for blue, minimum of 2\n");
194
195 register int r, g, b, i;
196
197 if (m_rgRGB) {
198 free (m_rgRGB);
199 m_rgRGB = NULL;
200 }
201
202 m_rgRGB = (RGBQUAD*) malloc (m_cColors*sizeof (RGBQUAD));
203 memset (m_rgRGB, 0, m_cColors*sizeof (RGBQUAD));
204
205 RGBQUAD* rgRGB = (RGBQUAD*) m_rgRGB;
206
207 for (r = 0; r < m_cRed; r++)
208 for (g = 0; g < m_cGreen; g++)
209 for (b = 0; b < m_cBlue; b++) {
210 i = color_index (r,g,b);
211 rgRGB[i].rgbRed = (r*(C_COLOR_MAX-1)/(m_cRed - 1));
212 rgRGB[i].rgbGreen = (g*(C_COLOR_MAX-1)/(m_cGreen - 1));
213 rgRGB[i].rgbBlue = (b*(C_COLOR_MAX-1)/(m_cBlue - 1));
214 }
215
216 create_matrix (dim);
217 }
218
219
220 /* LDither::matrix_value
221
222 returns the value for a dithering matrix of square matrix, size
223 high and wide, at the point x, y.
224
225 From netpbm-mar-1994 which got it from Graphics Gems, p. 714.
226
227 */
228
229 int LDither::matrix_value (int y, int x, int size)
230 {
231 assert (size > 0);
232
233 register int d;
234
235 /* Think of d as the density. At every iteration, d is shifted left
236 * one and a new bit is put in the low bit based on x and y. If x
237 * is odd and y is even, or visa versa, then a bit is shifted in.
238 * This generates the checkerboard pattern seen in dithering. This
239 * quantity is shifted again and the low bit of y is added in. This
240 * whole thing interleaves a checkerboard pattern and y's bits which
241 * is what you want. */
242
243 for (d = 0; size--; x >>= 1, y >>= 1)
244 d = (d << 2) | (((x & 1) ^ (y & 1)) << 1) | (y & 1);
245 return d;
246 }
247
248 void LDither::release_this (void)
249 {
250 if (m_pMatrix) {
251 free (m_pMatrix);
252 m_pMatrix = NULL;
253 }
254
255 if (m_rgRGB) {
256 free (m_rgRGB);
257 m_rgRGB = NULL;
258 }
259 }
260
261
262 #if defined (TEST_DITHER)
263
264 main (int argc, char** argv)
265 {
266 if (argc != 2) {
267 fprintf (stderr, "usage: <source_bitmap_filename>\n");
268
269 exit (1);
270 }
271 char* sz = argv[1];
272
273 HDR_DIBFILE hdr_file;
274 HDR_DIB hdr;
275 int height;
276 int width;
277
278 FILE* fp = fopen (sz, "rb");
279 UINT bftype;
280 fread (&bftype, 1, sizeof (bftype), fp);
281 if ( bftype != (('B' << 8) | 'M')
282 && bftype != (('M' << 8) | 'B')) {
283 fprintf (stderr, "file %s is not a bitmap\n", sz);
284 exit (2);
285 }
286
287 fread (&hdr_file, 1, sizeof (hdr_file), fp);
288 fread (&hdr, 1, sizeof (hdr), fp);
289 if (hdr.biBitCount != 24) {
290 fprintf (stderr, "source file must be 24 bits/pel");
291 exit (3);
292 }
293
294 height = hdr.biHeight;
295 width = hdr.biWidth;
296 int cbRowSrc = ROWWIDTH (width, 24);
297
298 unsigned8* pbSrc = (unsigned8*) malloc (height*cbRowSrc);
299 fseek (fp, hdr_file.bfOffBits, SEEK_SET);
300 fread (pbSrc, 1, height*cbRowSrc, fp);
301 fclose (fp);
302
303 int cbRowDst = ROWWIDTH (width, 8);
304 memset (&hdr_file, 0, sizeof (hdr_file));
305 hdr_file.bfOffBits = sizeof (hdr_file) + 2 + sizeof (hdr)
306 + sizeof (RGBQUAD)*256;
307 hdr_file.bfSize = hdr_file.bfOffBits + cbRowDst*height;
308 memset (&hdr, 0, sizeof (hdr));
309 hdr.biSize = sizeof (hdr);
310 hdr.biWidth = width;
311 hdr.biHeight = height;
312 hdr.biPlanes = 1;
313 hdr.biBitCount = 8;
314 hdr.biSizeImage = cbRowDst*height;
315
316 fwrite ("B", 1, 1, stdout);
317 fwrite ("M", 1, 1, stdout);
318 fwrite (&hdr_file, 1, sizeof (hdr_file), stdout);
319 fwrite (&hdr, 1, sizeof (hdr), stdout);
320
321 LDither dither;
322 dither.init ();
323
324 fwrite (dither.palette (), dither.colors (), sizeof (RGBQUAD), stdout);
325 RGBQUAD rgb;
326 memset (&rgb, 0, sizeof (RGBQUAD));
327 for (int i = 256 - dither.colors (); i--; )
328 fwrite (&rgb, 1, sizeof (RGBQUAD), stdout);
329
330 unsigned8* pbDst = (unsigned8*) malloc (height*cbRowDst);
331 memset (pbDst, 0, height*cbRowDst);
332
333 dither.dither (width, height, pbSrc, pbDst);
334
335 fwrite (pbDst, height, cbRowDst, stdout);
336
337 fflush (stdout);
338
339 exit (0);
340 }
341
342 #endif