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