1 /*
2 * Dither test program for CUPS.
3 *
4 * Try the following:
5 *
6 * testdither 0 255 > filename.ppm
7 * testdither 0 127 255 > filename.ppm
8 * testdither 0 85 170 255 > filename.ppm
9 * testdither 0 63 127 170 198 227 255 > filename.ppm
10 * testdither 0 210 383 > filename.ppm
11 * testdither 0 82 255 > filename.ppm
12 *
13 * Copyright 2007-2011 by Apple Inc.
14 * Copyright 1993-2005 by Easy Software Products.
15 *
16 * These coded instructions, statements, and computer programs are the
17 * property of Apple Inc. and are protected by Federal copyright
18 * law. Distribution and use rights are outlined in the file "COPYING"
19 * which should have been included with this file.
20 *
21 * Contents:
22 *
23 * main() - Test dithering and output a PPM file.
24 * usage() - Show program usage...
25 */
26
27 /*
28 * Include necessary headers.
29 */
30
31 #include "driver.h"
32 #include <config.h>
33 #include <string.h>
34 #include <ctype.h>
35
36
37 /*
38 * Local functions...
39 */
40
41 void usage(void);
42
43
44 /*
45 * 'main()' - Test dithering and output a PPM file.
46 */
47
48 int /* O - Exit status */
main(int argc,char * argv[])49 main(int argc, /* I - Number of command-line arguments */
50 char *argv[]) /* I - Command-line arguments */
51 {
52 int x, y; /* Current coordinate in image */
53 short line[512]; /* Line to dither */
54 unsigned char pixels[512], /* Dither pixels */
55 *pixptr; /* Pointer in line */
56 int output; /* Output pixel */
57 cups_lut_t *lut; /* Dither lookup table */
58 cups_dither_t *dither; /* Dither state */
59 int nlutvals; /* Number of lookup values */
60 float lutvals[16]; /* Lookup values */
61 int pixvals[16]; /* Pixel values */
62
63
64 /*
65 * See if we have lookup table values on the command-line...
66 */
67
68 if (argc > 1)
69 {
70 /*
71 * Yes, collect them...
72 */
73
74 nlutvals = 0;
75
76 for (x = 1; x < argc; x ++)
77 if (isdigit(argv[x][0]) && nlutvals < 16)
78 {
79 pixvals[nlutvals] = atoi(argv[x]);
80 lutvals[nlutvals] = atof(argv[x]) / 255.0;
81 nlutvals ++;
82 }
83 else
84 usage();
85
86 /*
87 * See if we have at least 2 values...
88 */
89
90 if (nlutvals < 2)
91 usage();
92 }
93 else
94 {
95 /*
96 * Otherwise use the default 2-entry LUT with values of 0 and 255...
97 */
98
99 nlutvals = 2;
100 lutvals[0] = 0.0;
101 lutvals[1] = 1.0;
102 pixvals[0] = 0;
103 pixvals[1] = 255;
104 }
105
106 /*
107 * Create the lookup table and dither state...
108 */
109
110 lut = cupsLutNew(nlutvals, lutvals);
111 dither = cupsDitherNew(512);
112
113 /*
114 * Put out the PGM header for a raw 256x256x8-bit grayscale file...
115 */
116
117 puts("P5\n512\n512\n255");
118
119 /*
120 * Dither 512 lines, which are written out in 256 image lines...
121 */
122
123 for (y = 0; y < 512; y ++)
124 {
125 /*
126 * Create the grayscale data for the current line...
127 */
128
129 for (x = 0; x < 512; x ++)
130 line[x] = 4095 * ((y / 32) * 16 + x / 32) / 255;
131
132 /*
133 * Dither the line...
134 */
135
136 cupsDitherLine(dither, lut, line, 1, pixels);
137
138 if (y == 0)
139 {
140 fputs("DEBUG: pixels =", stderr);
141 for (x = 0; x < 512; x ++)
142 fprintf(stderr, " %d", pixels[x]);
143 fputs("\n", stderr);
144 }
145
146 /*
147 * Add or set the output pixel values...
148 */
149
150 for (x = 0, pixptr = pixels; x < 512; x ++, pixptr ++)
151 {
152 output = 255 - pixvals[*pixptr];
153
154 if (output < 0)
155 putchar(0);
156 else
157 putchar(output);
158 }
159 }
160
161 /*
162 * Free the dither state and lookup table...
163 */
164
165 cupsDitherDelete(dither);
166 cupsLutDelete(lut);
167
168 /*
169 * Return with no errors...
170 */
171
172 return (0);
173 }
174
175
176 /*
177 * 'usage()' - Show program usage...
178 */
179
180 void
usage(void)181 usage(void)
182 {
183 puts("Usage: testdither [val1 val2 [... val16]] >filename.ppm");
184 exit(1);
185 }
186
187