1 /*
2 * Test the new RGB color separation code for CUPS.
3 *
4 * Copyright 2007-2011 by Apple Inc.
5 * Copyright 1993-2006 by Easy Software Products, All Rights Reserved.
6 *
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "COPYING"
10 * which should have been included with this file.
11 *
12 * Contents:
13 *
14 * main() - Do color rgb tests.
15 * test_gray() - Test grayscale rgbs...
16 * test_rgb() - Test color rgbs...
17 */
18
19 /*
20 * Include necessary headers.
21 */
22
23 #include <config.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include "driver.h"
27 #include <sys/stat.h>
28
29 #ifdef USE_LCMS1
30 # include <lcms.h>
31 #endif /* USE_LCMS1 */
32
33
34 void test_gray(cups_sample_t *samples, int num_samples,
35 int cube_size, int num_comps, const char *basename);
36 void test_rgb(cups_sample_t *samples, int num_samples,
37 int cube_size, int num_comps,
38 const char *basename);
39
40
41 /*
42 * 'main()' - Do color rgb tests.
43 */
44
45 int /* O - Exit status */
main(int argc,char * argv[])46 main(int argc, /* I - Number of command-line arguments */
47 char *argv[]) /* I - Command-line arguments */
48 {
49 static cups_sample_t CMYK[] = /* Basic 4-color sep */
50 {
51 /*{ r, g, b }, { C, M, Y, K }*/
52 { { 0, 0, 0 }, { 0, 0, 0, 255 } },
53 { { 255, 0, 0 }, { 0, 255, 240, 0 } },
54 { { 0, 255, 0 }, { 200, 0, 200, 0 } },
55 { { 255, 255, 0 }, { 0, 0, 240, 0 } },
56 { { 0, 0, 255 }, { 200, 200, 0, 0 } },
57 { { 255, 0, 255 }, { 0, 200, 0, 0 } },
58 { { 0, 255, 255 }, { 200, 0, 0, 0 } },
59 { { 255, 255, 255 }, { 0, 0, 0, 0 } }
60 };
61
62
63 /*
64 * Make the test directory...
65 */
66
67 mkdir("test", 0755);
68
69 /*
70 * Run tests for CMYK and CMYK separations...
71 */
72
73 test_rgb(CMYK, 8, 2, 4, "test/rgb-cmyk");
74
75 test_gray(CMYK, 8, 2, 4, "test/gray-cmyk");
76
77 /*
78 * Return with no errors...
79 */
80
81 return (0);
82 }
83
84
85 /*
86 * 'test_gray()' - Test grayscale rgbs...
87 */
88
89 void
test_gray(cups_sample_t * samples,int num_samples,int cube_size,int num_comps,const char * basename)90 test_gray(cups_sample_t *samples, /* I - Sample values */
91 int num_samples, /* I - Number of samples */
92 int cube_size, /* I - Cube size */
93 int num_comps, /* I - Number of components */
94 const char *basename) /* I - Base filename of output */
95 {
96 int i; /* Looping var */
97 char filename[255]; /* Output filename */
98 char line[255]; /* Line from PPM file */
99 int width, height; /* Width and height of test image */
100 int x, y; /* Current coordinate in image */
101 int r, g, b; /* Current RGB color */
102 unsigned char input[7000]; /* Line to rgbarate */
103 unsigned char output[48000], /* Output rgb data */
104 *outptr; /* Pointer in output */
105 FILE *in; /* Input PPM file */
106 FILE *out[CUPS_MAX_CHAN];
107 /* Output PGM files */
108 FILE *comp; /* Composite output */
109 cups_rgb_t *rgb; /* Color separation */
110
111
112 /*
113 * Open the test image...
114 */
115
116 in = fopen("image.pgm", "rb");
117 while (fgets(line, sizeof(line), in) != NULL)
118 if (isdigit(line[0]))
119 break;
120
121 sscanf(line, "%d%d", &width, &height);
122
123 fgets(line, sizeof(line), in);
124
125 /*
126 * Create the color rgb...
127 */
128
129 rgb = cupsRGBNew(num_samples, samples, cube_size, num_comps);
130
131 /*
132 * Open the color rgb files...
133 */
134
135 for (i = 0; i < num_comps; i ++)
136 {
137 sprintf(filename, "%s%d.pgm", basename, i);
138 out[i] = fopen(filename, "wb");
139
140 fprintf(out[i], "P5\n%d %d 255\n", width, height);
141 }
142
143 sprintf(filename, "%s.ppm", basename);
144 comp = fopen(filename, "wb");
145
146 fprintf(comp, "P6\n%d %d 255\n", width, height);
147
148 /*
149 * Read the image and do the rgbs...
150 */
151
152 for (y = 0; y < height; y ++)
153 {
154 fread(input, width, 1, in);
155
156 cupsRGBDoGray(rgb, input, output, width);
157
158 for (x = 0, outptr = output; x < width; x ++, outptr += num_comps)
159 {
160 for (i = 0; i < num_comps; i ++)
161 putc(255 - outptr[i], out[i]);
162
163 r = 255;
164 g = 255;
165 b = 255;
166
167 r -= outptr[0];
168 g -= outptr[1];
169 b -= outptr[2];
170
171 r -= outptr[3];
172 g -= outptr[3];
173 b -= outptr[3];
174
175 if (num_comps > 4)
176 {
177 r -= outptr[4] / 2;
178 g -= outptr[5] / 2;
179 }
180
181 if (num_comps > 6)
182 {
183 r -= outptr[6] / 2;
184 g -= outptr[6] / 2;
185 b -= outptr[6] / 2;
186 }
187
188 if (r < 0)
189 putc(0, comp);
190 else
191 putc(r, comp);
192
193 if (g < 0)
194 putc(0, comp);
195 else
196 putc(g, comp);
197
198 if (b < 0)
199 putc(0, comp);
200 else
201 putc(b, comp);
202 }
203 }
204
205 for (i = 0; i < num_comps; i ++)
206 fclose(out[i]);
207
208 fclose(comp);
209 fclose(in);
210
211 cupsRGBDelete(rgb);
212 }
213
214
215 /*
216 * 'test_rgb()' - Test color rgbs...
217 */
218
219 void
test_rgb(cups_sample_t * samples,int num_samples,int cube_size,int num_comps,const char * basename)220 test_rgb(cups_sample_t *samples, /* I - Sample values */
221 int num_samples, /* I - Number of samples */
222 int cube_size, /* I - Cube size */
223 int num_comps, /* I - Number of components */
224 const char *basename) /* I - Base filename of output */
225 {
226 int i; /* Looping var */
227 char filename[255]; /* Output filename */
228 char line[255]; /* Line from PPM file */
229 int width, height; /* Width and height of test image */
230 int x, y; /* Current coordinate in image */
231 int r, g, b; /* Current RGB color */
232 unsigned char input[7000]; /* Line to rgbarate */
233 unsigned char output[48000], /* Output rgb data */
234 *outptr; /* Pointer in output */
235 FILE *in; /* Input PPM file */
236 FILE *out[CUPS_MAX_CHAN];
237 /* Output PGM files */
238 FILE *comp; /* Composite output */
239 cups_rgb_t *rgb; /* Color separation */
240
241
242 /*
243 * Open the test image...
244 */
245
246 in = fopen("image.ppm", "rb");
247 while (fgets(line, sizeof(line), in) != NULL)
248 if (isdigit(line[0]))
249 break;
250
251 sscanf(line, "%d%d", &width, &height);
252
253 fgets(line, sizeof(line), in);
254
255 /*
256 * Create the color rgb...
257 */
258
259 rgb = cupsRGBNew(num_samples, samples, cube_size, num_comps);
260
261 /*
262 * Open the color rgb files...
263 */
264
265 for (i = 0; i < num_comps; i ++)
266 {
267 sprintf(filename, "%s%d.pgm", basename, i);
268 out[i] = fopen(filename, "wb");
269
270 fprintf(out[i], "P5\n%d %d 255\n", width, height);
271 }
272
273 sprintf(filename, "%s.ppm", basename);
274 comp = fopen(filename, "wb");
275
276 fprintf(comp, "P6\n%d %d 255\n", width, height);
277
278 /*
279 * Read the image and do the rgbs...
280 */
281
282 for (y = 0; y < height; y ++)
283 {
284 fread(input, width, 3, in);
285
286 cupsRGBDoRGB(rgb, input, output, width);
287
288 for (x = 0, outptr = output; x < width; x ++, outptr += num_comps)
289 {
290 for (i = 0; i < num_comps; i ++)
291 putc(255 - outptr[i], out[i]);
292
293 r = 255;
294 g = 255;
295 b = 255;
296
297 r -= outptr[0];
298 g -= outptr[1];
299 b -= outptr[2];
300
301 r -= outptr[3];
302 g -= outptr[3];
303 b -= outptr[3];
304
305 if (num_comps > 4)
306 {
307 r -= outptr[4] / 2;
308 g -= outptr[5] / 2;
309 }
310
311 if (num_comps > 6)
312 {
313 r -= outptr[6] / 2;
314 g -= outptr[6] / 2;
315 b -= outptr[6] / 2;
316 }
317
318 if (r < 0)
319 putc(0, comp);
320 else
321 putc(r, comp);
322
323 if (g < 0)
324 putc(0, comp);
325 else
326 putc(g, comp);
327
328 if (b < 0)
329 putc(0, comp);
330 else
331 putc(b, comp);
332 }
333 }
334
335 for (i = 0; i < num_comps; i ++)
336 fclose(out[i]);
337
338 fclose(comp);
339 fclose(in);
340
341 cupsRGBDelete(rgb);
342 }
343
344