Tizen 2.1 base
[platform/upstream/cups-filters.git] / cupsfilters / testcmyk.c
1 /*
2  * "$Id$"
3  *
4  *   Test the CMYK color separation code for CUPS.
5  *
6  *   Copyright 2007-2011 by Apple Inc.
7  *   Copyright 1993-2006 by Easy Software Products, All Rights Reserved.
8  *
9  *   These coded instructions, statements, and computer programs are the
10  *   property of Apple Inc. and are protected by Federal copyright
11  *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
12  *   which should have been included with this file.  If this file is
13  *   file is missing or damaged, see the license at "http://www.cups.org/".
14  *
15  * Contents:
16  *
17  *   test_gray() - Test grayscale separations...
18  *   test_rgb()  - Test color separations...
19  *   main()      - Do color separation tests.
20  */
21
22 /*
23  * Include necessary headers.
24  */
25
26 #include <config.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include "driver.h"
30 #include <sys/stat.h>
31
32
33 void    test_gray(int num_comps, const char *basename);
34 void    test_rgb(int num_comps, const char *basename);
35
36
37 /*
38  * 'main()' - Do color separation tests.
39  */
40
41 int                                             /* O - Exit status */
42 main(int  argc,                                 /* I - Number of command-line arguments */
43      char *argv[])                              /* I - Command-line arguments */
44 {
45  /*
46   * Make the test directory...
47   */
48
49   mkdir("test", 0755);
50
51  /*
52   * Run tests for K, Kk, CMY, CMYK, CcMmYK, and CcMmYKk separations...
53   */
54
55   test_rgb(1, "test/K-rgb");
56   test_rgb(2, "test/Kk-rgb");
57   test_rgb(3, "test/CMY-rgb");
58   test_rgb(4, "test/CMYK-rgb");
59   test_rgb(6, "test/CcMmYK-rgb");
60   test_rgb(7, "test/CcMmYKk-rgb");
61
62   test_gray(1, "test/K-gray");
63   test_gray(2, "test/Kk-gray");
64   test_gray(3, "test/CMY-gray");
65   test_gray(4, "test/CMYK-gray");
66   test_gray(6, "test/CcMmYK-gray");
67   test_gray(7, "test/CcMmYKk-gray");
68
69  /*
70   * Return with no errors...
71   */
72
73   return (0);
74 }
75
76
77 /*
78  * 'test_gray()' - Test grayscale separations...
79  */
80
81 void
82 test_gray(int        num_comps,         /* I - Number of components */
83           const char *basename)         /* I - Base filename of output */
84 {
85   int                   i;              /* Looping var */
86   char                  filename[255];  /* Output filename */
87   char                  line[255];      /* Line from PGM file */
88   int                   width, height;  /* Width and height of test image */
89   int                   x, y;           /* Current coordinate in image */
90   int                   r, g, b;        /* Current RGB color */
91   unsigned char         input[7000];    /* Line to separate */
92   short                 output[48000],  /* Output separation data */
93                         *outptr;        /* Pointer in output */
94   FILE                  *in;            /* Input PPM file */
95   FILE                  *out[CUPS_MAX_CHAN];
96                                         /* Output PGM files */
97   FILE                  *comp;          /* Composite output */
98   cups_cmyk_t           *cmyk;          /* Color separation */
99
100
101  /*
102   * Open the test image...
103   */
104
105   in = fopen("image.pgm", "rb");
106   while (fgets(line, sizeof(line), in) != NULL)
107     if (isdigit(line[0]))
108       break;
109
110   sscanf(line, "%d%d", &width, &height);
111
112   fgets(line, sizeof(line), in);
113
114  /*
115   * Create the color separation...
116   */
117
118   cmyk = cupsCMYKNew(num_comps);
119
120   switch (num_comps)
121   {
122     case 2 : /* Kk */
123         cupsCMYKSetLtDk(cmyk, 0, 0.5, 1.0);
124         break;
125
126     case 4 :
127         cupsCMYKSetGamma(cmyk, 2, 1.0, 0.9);
128         cupsCMYKSetBlack(cmyk, 0.5, 1.0);
129         break;
130
131     case 6 : /* CcMmYK */
132         cupsCMYKSetLtDk(cmyk, 0, 0.5, 1.0);
133         cupsCMYKSetLtDk(cmyk, 2, 0.5, 1.0);
134         cupsCMYKSetGamma(cmyk, 4, 1.0, 0.9);
135         cupsCMYKSetBlack(cmyk, 0.5, 1.0);
136         break;
137
138     case 7 : /* CcMmYKk */
139         cupsCMYKSetLtDk(cmyk, 0, 0.5, 1.0);
140         cupsCMYKSetLtDk(cmyk, 2, 0.5, 1.0);
141         cupsCMYKSetGamma(cmyk, 4, 1.0, 0.9);
142         cupsCMYKSetLtDk(cmyk, 5, 0.5, 1.0);
143         break;
144   }
145
146  /*
147   * Open the color separation files...
148   */
149
150   for (i = 0; i < num_comps; i ++)
151   {
152     sprintf(filename, "%s%d.pgm", basename, i);
153     out[i] = fopen(filename, "wb");
154
155     fprintf(out[i], "P5\n%d %d 255\n", width, height);
156   }
157
158   sprintf(filename, "%s.ppm", basename);
159   comp = fopen(filename, "wb");
160
161   fprintf(comp, "P6\n%d %d 255\n", width, height);
162
163  /*
164   * Read the image and do the separations...
165   */
166
167   for (y = 0; y < height; y ++)
168   {
169     fread(input, width, 1, in);
170
171     cupsCMYKDoGray(cmyk, input, output, width);
172
173     for (x = 0, outptr = output; x < width; x ++, outptr += num_comps)
174     {
175       for (i = 0; i < num_comps; i ++)
176         putc(255 - 255 * outptr[i] / 4095, out[i]);
177
178       r = 4095;
179       g = 4095;
180       b = 4095;
181
182       switch (num_comps)
183       {
184         case 1 :
185             r -= outptr[0];
186             g -= outptr[0];
187             b -= outptr[0];
188             break;
189         case 2 :
190             r -= outptr[0];
191             g -= outptr[0];
192             b -= outptr[0];
193
194             r -= outptr[1] / 2;
195             g -= outptr[1] / 2;
196             b -= outptr[1] / 2;
197             break;
198         case 3 :
199             r -= outptr[0];
200             g -= outptr[1];
201             b -= outptr[2];
202             break;
203         case 4 :
204             r -= outptr[0];
205             g -= outptr[1];
206             b -= outptr[2];
207
208             r -= outptr[3];
209             g -= outptr[3];
210             b -= outptr[3];
211             break;
212         case 6 :
213             r -= outptr[0] + outptr[1] / 2;
214             g -= outptr[2] + outptr[3] / 3;
215             b -= outptr[4];
216
217             r -= outptr[5];
218             g -= outptr[5];
219             b -= outptr[5];
220             break;
221         case 7 :
222             r -= outptr[0] + outptr[1] / 2;
223             g -= outptr[2] + outptr[3] / 3;
224             b -= outptr[4];
225
226             r -= outptr[5] + outptr[6] / 2;
227             g -= outptr[5] + outptr[6] / 2;
228             b -= outptr[5] + outptr[6] / 2;
229             break;
230       }
231
232       if (r < 0)
233         putc(0, comp);
234       else
235         putc(255 * r / 4095, comp);
236
237       if (g < 0)
238         putc(0, comp);
239       else
240         putc(255 * g / 4095, comp);
241
242       if (b < 0)
243         putc(0, comp);
244       else
245         putc(255 * b / 4095, comp);
246     }
247   }
248
249   for (i = 0; i < num_comps; i ++)
250     fclose(out[i]);
251
252   fclose(comp);
253   fclose(in);
254
255   cupsCMYKDelete(cmyk);
256 }
257
258
259 /*
260  * 'test_rgb()' - Test color separations...
261  */
262
263 void
264 test_rgb(int        num_comps,          /* I - Number of components */
265          const char *basename)          /* I - Base filename of output */
266 {
267   int                   i;              /* Looping var */
268   char                  filename[255];  /* Output filename */
269   char                  line[255];      /* Line from PPM file */
270   int                   width, height;  /* Width and height of test image */
271   int                   x, y;           /* Current coordinate in image */
272   int                   r, g, b;        /* Current RGB color */
273   unsigned char         input[7000];    /* Line to separate */
274   short                 output[48000],  /* Output separation data */
275                         *outptr;        /* Pointer in output */
276   FILE                  *in;            /* Input PPM file */
277   FILE                  *out[CUPS_MAX_CHAN];
278                                         /* Output PGM files */
279   FILE                  *comp;          /* Composite output */
280   cups_cmyk_t           *cmyk;          /* Color separation */
281
282
283  /*
284   * Open the test image...
285   */
286
287   in = fopen("image.ppm", "rb");
288   while (fgets(line, sizeof(line), in) != NULL)
289     if (isdigit(line[0]))
290       break;
291
292   sscanf(line, "%d%d", &width, &height);
293
294   fgets(line, sizeof(line), in);
295
296  /*
297   * Create the color separation...
298   */
299
300   cmyk = cupsCMYKNew(num_comps);
301
302   cupsCMYKSetBlack(cmyk, 0.5, 1.0);
303
304   switch (num_comps)
305   {
306     case 2 : /* Kk */
307         cupsCMYKSetLtDk(cmyk, 0, 0.5, 1.0);
308         break;
309     case 6 : /* CcMmYK */
310         cupsCMYKSetGamma(cmyk, 0, 1.0, 0.8);
311         cupsCMYKSetLtDk(cmyk, 0, 0.5, 1.0);
312         cupsCMYKSetGamma(cmyk, 2, 1.0, 0.8);
313         cupsCMYKSetLtDk(cmyk, 2, 0.5, 1.0);
314         break;
315     case 7 : /* CcMmYKk */
316         cupsCMYKSetGamma(cmyk, 0, 1.0, 0.8);
317         cupsCMYKSetLtDk(cmyk, 0, 0.5, 1.0);
318         cupsCMYKSetGamma(cmyk, 2, 1.0, 0.8);
319         cupsCMYKSetLtDk(cmyk, 2, 0.5, 1.0);
320         cupsCMYKSetLtDk(cmyk, 5, 0.5, 1.0);
321         break;
322   }
323
324  /*
325   * Open the color separation files...
326   */
327
328   for (i = 0; i < num_comps; i ++)
329   {
330     sprintf(filename, "%s%d.pgm", basename, i);
331     out[i] = fopen(filename, "wb");
332
333     fprintf(out[i], "P5\n%d %d 255\n", width, height);
334   }
335
336   sprintf(filename, "%s.ppm", basename);
337   comp = fopen(filename, "wb");
338
339   fprintf(comp, "P6\n%d %d 255\n", width, height);
340
341  /*
342   * Read the image and do the separations...
343   */
344
345   for (y = 0; y < height; y ++)
346   {
347     fread(input, width, 3, in);
348
349     cupsCMYKDoRGB(cmyk, input, output, width);
350
351     for (x = 0, outptr = output; x < width; x ++, outptr += num_comps)
352     {
353       for (i = 0; i < num_comps; i ++)
354         putc(255 - 255 * outptr[i] / 4095, out[i]);
355
356       r = 4095;
357       g = 4095;
358       b = 4095;
359
360       switch (num_comps)
361       {
362         case 1 :
363             r -= outptr[0];
364             g -= outptr[0];
365             b -= outptr[0];
366             break;
367         case 2 :
368             r -= outptr[0];
369             g -= outptr[0];
370             b -= outptr[0];
371
372             r -= outptr[1] / 2;
373             g -= outptr[1] / 2;
374             b -= outptr[1] / 2;
375             break;
376         case 3 :
377             r -= outptr[0];
378             g -= outptr[1];
379             b -= outptr[2];
380             break;
381         case 4 :
382             r -= outptr[0];
383             g -= outptr[1];
384             b -= outptr[2];
385
386             r -= outptr[3];
387             g -= outptr[3];
388             b -= outptr[3];
389             break;
390         case 6 :
391             r -= outptr[0] + outptr[1] / 2;
392             g -= outptr[2] + outptr[3] / 3;
393             b -= outptr[4];
394
395             r -= outptr[5];
396             g -= outptr[5];
397             b -= outptr[5];
398             break;
399         case 7 :
400             r -= outptr[0] + outptr[1] / 2;
401             g -= outptr[2] + outptr[3] / 3;
402             b -= outptr[4];
403
404             r -= outptr[5] + outptr[6] / 2;
405             g -= outptr[5] + outptr[6] / 2;
406             b -= outptr[5] + outptr[6] / 2;
407             break;
408       }
409
410       if (r < 0)
411         putc(0, comp);
412       else
413         putc(255 * r / 4095, comp);
414
415       if (g < 0)
416         putc(0, comp);
417       else
418         putc(255 * g / 4095, comp);
419
420       if (b < 0)
421         putc(0, comp);
422       else
423         putc(255 * b / 4095, comp);
424     }
425   }
426
427   for (i = 0; i < num_comps; i ++)
428     fclose(out[i]);
429
430   fclose(comp);
431   fclose(in);
432
433   cupsCMYKDelete(cmyk);
434 }
435
436
437 /*
438  * End of "$Id$".
439  */