/
print-util.c
2553 lines (2307 loc) · 62.1 KB
/
print-util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* "$Id: print-util.c,v 1.1.1.2 2000/01/13 02:58:07 rlk Exp $"
*
* Print plug-in driver utility functions for the GIMP.
*
* Copyright 1997-1999 Michael Sweet (mike@easysw.com) and
* Robert Krawitz (rlk@alum.mit.edu)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Contents:
*
* dither_black() - Dither grayscale pixels to black.
* dither_cmyk() - Dither RGB pixels to cyan, magenta, yellow, and
* black.
* dither_black4() - Dither grayscale pixels to 4 levels of black.
* dither_cmyk4() - Dither RGB pixels to 4 levels of cyan, magenta,
* yellow, and black.
* gray_to_gray() - Convert grayscale image data to grayscale.
* indexed_to_gray() - Convert indexed image data to grayscale.
* indexed_to_rgb() - Convert indexed image data to RGB.
* rgb_to_gray() - Convert RGB image data to grayscale.
* rgb_to_rgb() - Convert RGB image data to RGB.
* default_media_size() - Return the size of a default page size.
*
* Revision History:
*
* $Log: print-util.c,v $
* Revision 1.1.1.2 2000/01/13 02:58:07 rlk
* Update from the Gimp
*
* Revision 1.11 1999/12/16 19:44:01 olofk
* Thu Dec 16 20:15:25 CET 1999 Olof S Kylande <olof@gimp.org>
*
* Fix of KDE/Kwm selection add/sub/inter problem
* NOTE: This is a workaround, not a real fix.
* Many Thanks to Matthias Ettrich
*
* * app/disp_callbacks.c
*
* Updated unsharp-mask to version 0.10
*
* * plug-ins/unsharp/dialog_f.c
* * plug-ins/unsharp/dialog_f.h
* * plug-ins/unsharp/dialog_i.c
* * plug-ins/unsharp/dialog_i.h
* * plug-ins/unsharp/unsharp.c
*
* Updated print plug-in to version 3.0.1
*
* * plug-ins/print/README (new file)
* * plug-ins/print/print-escp2.c
* * plug-ins/print/print-pcl.c
* * plug-ins/print/print-ps.c
* * plug-ins/print/print-util.c
* * plug-ins/print/print.c
* * plug-ins/print/print.h
*
* Updated all files in the help/C/dialogs dir. This is
* a first alpha glimpse of the help system. Please give
* me feedback of the content. However since it's in alpha
* stage it means that there is spell, grammatical, etc errors.
* There is may also be pure errors which I hope "you" will
* report to either olof@gimp.org or karin@gimp.org. Please
* don't report spell, grammatical, etc error at this stage in dev.
*
* If you have any plans to commit to the help system please write
* to olof@gimp.org. (This is mandatory not a please ;-).
*
* * help/C/welcome.html
* * help/C/dialogs/about.html ..............
*
* Revision 1.37 1999/12/05 23:24:08 rlk
* don't want PRINT_LUT in release
*
* Revision 1.36 1999/12/05 04:33:34 rlk
* Good results for the night.
*
* Revision 1.35 1999/12/04 19:01:05 rlk
* better use of light colors
*
* Revision 1.34 1999/12/02 02:09:45 rlk
* .
*
* Revision 1.33 1999/11/25 00:02:03 rlk
* Revamped many controls
*
* Revision 1.32 1999/11/23 02:11:37 rlk
* Rationalize variables, pass 3
*
* Revision 1.31 1999/11/23 01:33:37 rlk
* First stage of simplifying the variable stuff
*
* Revision 1.30 1999/11/16 00:59:00 rlk
* More fine tuning
*
* Revision 1.29 1999/11/14 21:37:13 rlk
* Revamped contrast
*
* Revision 1.28 1999/11/14 18:59:22 rlk
* Final preparations for release to Olof
*
* Revision 1.27 1999/11/14 00:57:11 rlk
* Mix black in sooner gives better density.
*
* Revision 1.26 1999/11/13 02:31:29 rlk
* Finally! Good settings!
*
* Revision 1.25 1999/11/12 03:34:40 rlk
* More tweaking
*
* Revision 1.24 1999/11/12 02:18:32 rlk
* Stubs for dynamic memory allocation
*
* Revision 1.23 1999/11/12 01:53:37 rlk
* Remove silly spurious stuff
*
* Revision 1.22 1999/11/12 01:51:47 rlk
* Much better black
*
* Revision 1.21 1999/11/10 01:13:06 rlk
* Support up to 2880 dpi
*
* Revision 1.20 1999/11/07 22:16:42 rlk
* Bug fixes; try to improve dithering slightly
*
* Revision 1.19 1999/10/29 01:01:16 rlk
* Smoother rendering of darker colors
*
* Revision 1.18 1999/10/28 02:01:15 rlk
* One bug, two effects:
*
* 1) Handle 4-color correctly (it was treating the 4-color too much like the
* 6-color).
*
* 2) An attempt to handle both cases with the same code path led to a
* discontinuity that depending upon the orientation of a color gradient would
* lead to either white or dark lines at the point that the dark version of
* the color would kick in.
*
* Revision 1.17 1999/10/26 23:58:31 rlk
* indentation
*
* Revision 1.16 1999/10/26 23:36:51 rlk
* Comment out all remaining 16-bit code, and rename 16-bit functions to "standard" names
*
* Revision 1.15 1999/10/26 02:10:30 rlk
* Mostly fix save/load
*
* Move all gimp, glib, gtk stuff into print.c (take it out of everything else).
* This should help port it to more general purposes later.
*
* Revision 1.14 1999/10/25 23:31:59 rlk
* 16-bit clean
*
* Revision 1.13 1999/10/25 00:14:46 rlk
* Remove more of the 8-bit code, now that it is tested
*
* Revision 1.12 1999/10/23 20:26:48 rlk
* Move LUT calculation to print-util
*
* Revision 1.11 1999/10/21 01:27:37 rlk
* More progress toward full 16-bit rendering
*
* Revision 1.10 1999/10/19 02:04:59 rlk
* Merge all of the single-level print_cmyk functions
*
* Revision 1.9 1999/10/18 01:37:02 rlk
* Remove spurious stuff
*
* Revision 1.8 1999/10/17 23:44:07 rlk
* 16-bit everything (untested)
*
* Revision 1.7 1999/10/17 23:01:01 rlk
* Move various dither functions into print-utils.c
*
* Revision 1.6 1999/10/14 01:59:59 rlk
* Saturation
*
* Revision 1.5 1999/10/03 23:57:20 rlk
* Various improvements
*
* Revision 1.4 1999/09/18 15:18:47 rlk
* A bit more random
*
* Revision 1.3 1999/09/14 21:43:43 rlk
* Some hoped-for improvements
*
* Revision 1.2 1999/09/12 00:12:24 rlk
* Current best stuff
*
* Revision 1.10 1998/05/17 07:16:49 yosh
* 0.99.31 fun
*
* updated print plugin
*
* -Yosh
*
* Revision 1.14 1998/05/16 18:27:59 mike
* Cleaned up dithering functions - unnecessary extra data in dither buffer.
*
* Revision 1.13 1998/05/13 17:00:36 mike
* Minor change to CMYK generation code - now cube black difference value
* for better colors.
*
* Revision 1.12 1998/05/08 19:20:50 mike
* Updated CMYK generation code to use new method.
* Updated dithering algorithm (slightly more uniform now, less speckling)
* Added default media size function.
*
* Revision 1.11 1998/03/01 18:03:27 mike
* Whoops - need to add 255 - alpha to the output values (transparent to white
* and not transparent to black...)
*
* Revision 1.10 1998/03/01 17:20:48 mike
* Updated alpha code to do alpha computation before gamma/brightness lut.
*
* Revision 1.9 1998/03/01 17:13:46 mike
* Updated CMY/CMYK conversion code for dynamic BG and hue adjustment.
* Added alpha channel support to color conversion functions.
*
* Revision 1.8 1998/01/21 21:33:47 mike
* Replaced Burkes dither with stochastic (random) dither.
*
* Revision 1.7 1997/10/02 17:57:26 mike
* Replaced ordered dither with Burkes dither (error-diffusion).
* Now dither K separate from CMY.
*
* Revision 1.6 1997/07/30 20:33:05 mike
* Final changes for 1.1 release.
*
* Revision 1.5 1997/07/26 18:43:04 mike
* Fixed dither_black and dither_cmyk - wasn't clearing extra bits
* (caused problems with A3/A4 size output).
*
* Revision 1.5 1997/07/26 18:43:04 mike
* Fixed dither_black and dither_cmyk - wasn't clearing extra bits
* (caused problems with A3/A4 size output).
*
* Revision 1.4 1997/07/02 18:46:26 mike
* Fixed stupid bug in dither_black() - wasn't comparing against gray
* pixels (comparing against the first black byte - d'oh!)
* Changed 255 in dither matrix to 254 to shade correctly.
*
* Revision 1.4 1997/07/02 18:46:26 mike
* Fixed stupid bug in dither_black() - wasn't comparing against gray
* pixels (comparing against the first black byte - d'oh!)
* Changed 255 in dither matrix to 254 to shade correctly.
*
* Revision 1.3 1997/07/02 13:51:53 mike
* Added rgb_to_rgb and gray_to_gray conversion functions.
* Standardized calling args to conversion functions.
*
* Revision 1.2 1997/07/01 19:28:44 mike
* Updated dither matrix.
* Fixed scaling bugs in dither_*() functions.
*
* Revision 1.1 1997/06/19 02:18:15 mike
* Initial revision
*/
/* #define PRINT_DEBUG */
#include "print.h"
#include <math.h>
/*
* RGB to grayscale luminance constants...
*/
#define LUM_RED 31
#define LUM_GREEN 61
#define LUM_BLUE 8
/*
* Error buffer for dither functions. This needs to be at least 14xMAXDPI
* (currently 720) to avoid problems...
*
* Want to dynamically allocate this so we can save memory!
*/
#define ERROR_ROWS 2
typedef union error
{
struct
{
int c[ERROR_ROWS];
int m[ERROR_ROWS];
int y[ERROR_ROWS];
int k[ERROR_ROWS];
} c;
int v[4][ERROR_ROWS];
} error_t;
error_t *nerror = 0;
int error[2][4][14*2880+1];
/*
* 'dither_black()' - Dither grayscale pixels to black.
*/
void
dither_black(unsigned short *gray, /* I - Grayscale pixels */
int row, /* I - Current Y coordinate */
int src_width, /* I - Width of input row */
int dst_width, /* I - Width of output row */
unsigned char *black) /* O - Black bitmap pixels */
{
int x, /* Current X coordinate */
xerror, /* X error count */
xstep, /* X step */
xmod, /* X error modulus */
length; /* Length of output bitmap in bytes */
unsigned char bit, /* Current bit */
*kptr; /* Current black pixel */
int k, /* Current black error */
ditherk, /* Next error value in buffer */
*kerror0, /* Pointer to current error row */
*kerror1; /* Pointer to next error row */
int ditherbit; /* Random dithering bitmask */
xstep = src_width / dst_width;
xmod = src_width % dst_width;
length = (dst_width) / 8;
kerror0 = error[row & 1][3];
kerror1 = error[1 - (row & 1)][3];
memset(black, 0, length);
for (x = 0, bit = 128, kptr = black, xerror = 0,
ditherbit = rand(), ditherk = *kerror0;
x < dst_width;
x ++, kerror0 ++, kerror1 ++)
{
k = 65535 - *gray + ditherk / 8;
if (k > 32767)
{
*kptr |= bit;
k -= 65535;
}
if (ditherbit & bit)
{
kerror1[0] = 5 * k;
ditherk = kerror0[1] + 3 * k;
}
else
{
kerror1[0] = 3 * k;
ditherk = kerror0[1] + 5 * k;
}
if (bit == 1)
{
kptr ++;
bit = 128;
ditherbit = rand();
}
else
bit >>= 1;
gray += xstep;
xerror += xmod;
if (xerror >= dst_width)
{
xerror -= dst_width;
gray ++;
}
}
}
/*
* 'dither_cmyk6()' - Dither RGB pixels to cyan, magenta, light cyan,
* light magenta, yellow, and black.
*
* Added by Robert Krawitz <rlk@alum.mit.edu> August 30, 1999.
*
* Let's be really aggressive and use a single routine for ALL cmyk dithering,
* including 6 and 7 color.
*
* Note that this is heavily tuned for Epson Stylus Photo printers.
* This should be generalized for other CMYK and CcMmYK printers. All
* of these constants were empirically determined, and are subject to review.
*/
/*
* Ratios of dark to light inks. The darker ink should be DE / NU darker
* than the light ink.
*
* It is essential to be very careful about use of parentheses with these
* macros!
*
* Increasing the denominators results in use of more dark ink. This creates
* more saturated colors.
*/
#define NU_C 1
#define DE_C 3
#define NU_M 1
#define DE_M 3
#define NU_Y 1
#define DE_Y 3
#define I_RATIO_C NU_C / DE_C
#define I_RATIO_C1 NU_C / (DE_C + NU_C)
#define RATIO_C DE_C / NU_C
#define RATIO_C1 (DE_C + NU_C) / NU_C
#define I_RATIO_M NU_M / DE_M
#define I_RATIO_M1 NU_M / (DE_M + NU_M)
#define RATIO_M DE_M / NU_M
#define RATIO_M1 (DE_M + NU_M) / NU_M
#define I_RATIO_Y NU_Y / DE_Y
#define I_RATIO_Y1 NU_Y / (DE_Y + NU_Y)
#define RATIO_Y DE_Y / NU_Y
#define RATIO_Y1 (DE_Y + NU_Y) / NU_Y
/*
* Lower and upper bounds for mixing CMY with K to produce gray scale.
* Reducing KDARKNESS_LOWER results in more black being used with relatively
* light grays, which causes speckling. Increasing KDARKNESS_UPPER results
* in more CMY being used in dark tones, which results in less pure black.
* Decreasing the gap too much results in sharp crossover and stairstepping.
*/
#define KDARKNESS_LOWER (32 * 256)
#define KDARKNESS_UPPER (96 * 256)
/*
* Randomizing values for deciding when to output a bit. Normally with the
* error diffusion algorithm a bit is not output until the accumulated value
* of the pixel crosses a threshold. This randomizes the threshold, which
* results in fewer obnoxious diagonal jaggies in pale regions. Smaller values
* result in greater randomizing. We use less randomness for black output
* to avoid production of black speckles in light regions.
*/
#define C_RANDOMIZER 2
#define M_RANDOMIZER 2
#define Y_RANDOMIZER 2
#define K_RANDOMIZER 8
#ifdef PRINT_DEBUG
#define UPDATE_COLOR_DBG(r) \
do { \
od##r = dither##r; \
d##r = r; \
} while (0)
#define PRINT_D1(r, R, d1, d2) \
do { \
fprintf(dbg, "Case 4: o" #r " %lld " #r \
" %lld ditherbit" #d1 " %d ditherbit" #d2 " %d " \
"num %lld den %lld test1 %lld test2 %lld\n", \
o##r, r, ditherbit##d1, ditherbit##d2, \
o##r, 65536ll, \
((32767 + (((long long) ditherbit##d2 / 1) - 32768)) * o##r \
/ 65536), \
((o##r - (65536 * I_RATIO_##R##1 * 3 / 4)) * 65536 / \
(65536 - (65536 * I_RATIO_##R##1 * 3 / 4)))); \
} while (0)
#define PRINT_D2(r, R, d1, d2) \
do { \
fprintf(dbg, "Case 1: o" #r " %lld " #r " %lld test %lld\n", o##r, r, \
(32767 + (((long long) ditherbit##d2 / 1) - 32768)) * \
I_RATIO_##R##1); \
} while (0)
#define PRINT_D3(r, R, d1, d2) \
do { \
fprintf(dbg, "Case 2: o" #r " %lld " #r \
" %lld ditherbit" #d1 " %d ditherbit" #d2 " %d " \
"num %lld den %lld test1 %lld test2 %lld\n", \
o##r, r, ditherbit##d1, ditherbit##d2, \
o##r, 65536ll, \
((32767 + (((long long) ditherbit##d2 / 1) - 32768)) * o##r / \
65536), cutoff); \
} while (0)
#define PRINT_D4(r, R, d1, d2) \
do { \
fprintf(dbg, "Case 3: o" #r " %lld " #r \
" %lld ditherbit" #d1 " %d ditherbit" #d2 " %d " \
"num %lld den %lld test1 %lld test2 %lld\n", \
o##r, r, ditherbit##d1, ditherbit##d2, \
o##r, 65536ll, \
((32767 + (((long long) ditherbit##d2 / 1) - 32768)) * o##r / \
65536), cutoff); \
} while (0)
#else /* !PRINT_DEBUG */
#define UPDATE_COLOR_DBG(r) do {} while (0)
#define PRINT_D1(r, R, d1, d2) do {} while (0)
#define PRINT_D2(r, R, d1, d2) do {} while (0)
#define PRINT_D3(r, R, d1, d2) do {} while (0)
#define PRINT_D4(r, R, d1, d2) do {} while (0)
#endif
#define UPDATE_COLOR(r) \
do { \
o##r = r; \
r += dither##r / 8; \
UPDATE_COLOR_DBG(r); \
} while (0)
#define PRINT_COLOR(color, r, R, d1, d2) \
do { \
if (!l##color) \
{ \
if (r > (32767 + (((long long) ditherbit##d2 / R##_RANDOMIZER) - \
(32768 / R##_RANDOMIZER)))) \
{ \
PRINT_D1(r, R, d1, d2); \
if (r##bits++ % horizontal_overdensity == 0) \
if (! (*kptr & bit)) \
*r##ptr |= bit; \
r -= 65535; \
} \
} \
else \
{ \
if (r <= (65536 * I_RATIO_##R##1 * 2 / 3)) \
{ \
if (r > (32767 + (((long long) ditherbit##d2 / R##_RANDOMIZER) - \
(32768 / R##_RANDOMIZER))) * I_RATIO_##R##1) \
{ \
PRINT_D2(r, R, d1, d2); \
if (l##r##bits++ % horizontal_overdensity == 0) \
if (! (*kptr & bit)) \
*l##r##ptr |= bit; \
r -= 65535 * I_RATIO_##R##1; \
} \
} \
else if (r > (32767 + (((long long) ditherbit##d2 / R##_RANDOMIZER) - \
(32768 / R##_RANDOMIZER))) * I_RATIO_##R##1) \
{ \
int cutoff = ((density - (65536 * I_RATIO_##R##1 * 2 / 3)) * \
65536 / (65536 - (65536 * I_RATIO_##R##1 * 2 / 3))); \
long long sub = (65535ll * I_RATIO_##R##1) + \
((65535ll - (65535ll * I_RATIO_##R##1)) * cutoff / 65536); \
if (ditherbit##d1 > cutoff) \
{ \
PRINT_D3(r, R, d1, d2); \
if (l##r##bits++ % horizontal_overdensity == 0) \
if (! (*kptr & bit)) \
*l##r##ptr |= bit; \
} \
else \
{ \
PRINT_D4(r, R, d1, d2); \
if (r##bits++ % horizontal_overdensity == 0) \
if (! (*kptr & bit)) \
*r##ptr |= bit; \
} \
if (sub < (65535 * I_RATIO_##R##1)) \
r -= (65535 * I_RATIO_##R##1); \
else if (sub > 65535) \
r -= 65535; \
else \
r -= sub; \
} \
} \
} while (0)
#if 1
#define UPDATE_DITHER(r, d2, x, width) \
do { \
if (ditherbit##d2 & bit) \
{ \
if (x > 0) \
r##error1[-1] += r; \
else \
r##error1[0] = r; \
r##error1[0] += 3 * r; \
r##error1[1] = r; \
dither##r = r##error0[1] + 3 * r; \
} \
else \
{ \
if (x > 0) \
r##error1[-1] += r * 3 / 4; \
else \
r##error1[0] = r * 3 / 4; \
r##error1[0] += r * 3 / 2; \
r##error1[1] = r * 3 / 4; \
dither##r = r##error0[1] + 5 * r; \
} \
} while (0)
#else
#define UPDATE_DITHER(r, d2, x, width) \
do { \
if (ditherbit##d2 & bit) \
{ \
r##error1[0] = 5 * r; \
dither##r = r##error0[1] + 3 * r; \
} \
else \
{ \
r##error1[0] = 3 * r; \
dither##r = r##error0[1] + 5 * r; \
} \
} while (0)
#endif
void
dither_cmyk(unsigned short *rgb, /* I - RGB pixels */
int row, /* I - Current Y coordinate */
int src_width, /* I - Width of input row */
int dst_width, /* I - Width of output rows */
unsigned char *cyan, /* O - Cyan bitmap pixels */
unsigned char *lcyan, /* O - Light cyan bitmap pixels */
unsigned char *magenta, /* O - Magenta bitmap pixels */
unsigned char *lmagenta, /* O - Light magenta bitmap pixels */
unsigned char *yellow, /* O - Yellow bitmap pixels */
unsigned char *lyellow, /* O - Light yellow bitmap pixels */
unsigned char *black, /* O - Black bitmap pixels */
int horizontal_overdensity)
{
int x, /* Current X coordinate */
xerror, /* X error count */
xstep, /* X step */
xmod, /* X error modulus */
length; /* Length of output bitmap in bytes */
long long c, m, y, k, /* CMYK values */
oc, om, ok, oy,
divk; /* Inverse of K */
long long diff; /* Average color difference */
unsigned char bit, /* Current bit */
*cptr, /* Current cyan pixel */
*mptr, /* Current magenta pixel */
*yptr, /* Current yellow pixel */
*lmptr, /* Current light magenta pixel */
*lcptr, /* Current light cyan pixel */
*lyptr, /* Current light yellow pixel */
*kptr; /* Current black pixel */
int ditherc, /* Next error value in buffer */
*cerror0, /* Pointer to current error row */
*cerror1; /* Pointer to next error row */
int dithery, /* Next error value in buffer */
*yerror0, /* Pointer to current error row */
*yerror1; /* Pointer to next error row */
int ditherm, /* Next error value in buffer */
*merror0, /* Pointer to current error row */
*merror1; /* Pointer to next error row */
int ditherk, /* Next error value in buffer */
*kerror0, /* Pointer to current error row */
*kerror1; /* Pointer to next error row */
int ditherbit; /* Random dither bitmask */
int nk;
int ck;
int bk;
int ub, lb;
int ditherbit0, ditherbit1, ditherbit2, ditherbit3;
long long density;
/*
* If horizontal_overdensity is > 1, we want to output a bit only so many
* times that a bit would be generated. These serve as counters for making
* that decision. We make these variable static rather than reinitializing
* at zero each line to avoid having a line of bits near the edge of the
* image.
*/
static int cbits = 0;
static int mbits = 0;
static int ybits = 0;
static int kbits = 0;
static int lcbits = 0;
static int lmbits = 0;
static int lybits = 0;
#ifdef PRINT_DEBUG
long long odk, odc, odm, ody, dk, dc, dm, dy, xk, xc, xm, xy, yc, ym, yy;
FILE *dbg;
#endif
xstep = 3 * (src_width / dst_width);
xmod = src_width % dst_width;
length = (dst_width + 7) / 8;
cerror0 = error[row & 1][0];
cerror1 = error[1 - (row & 1)][0];
merror0 = error[row & 1][1];
merror1 = error[1 - (row & 1)][1];
yerror0 = error[row & 1][2];
yerror1 = error[1 - (row & 1)][2];
kerror0 = error[row & 1][3];
kerror1 = error[1 - (row & 1)][3];
memset(cyan, 0, length);
if (lcyan)
memset(lcyan, 0, length);
memset(magenta, 0, length);
if (lmagenta)
memset(lmagenta, 0, length);
memset(yellow, 0, length);
if (lyellow)
memset(lyellow, 0, length);
if (black)
memset(black, 0, length);
#ifdef PRINT_DEBUG
dbg = fopen("/mnt1/dbg", "a");
#endif
/*
* Main loop starts here!
*/
for (x = 0, bit = 128,
cptr = cyan, mptr = magenta, yptr = yellow, lcptr = lcyan,
lmptr = lmagenta, lyptr = lyellow, kptr = black, xerror = 0,
ditherbit = rand(),
ditherc = cerror0[0], ditherm = merror0[0], dithery = yerror0[0],
ditherk = kerror0[0],
ditherbit0 = ditherbit & 0xffff,
ditherbit1 = ((ditherbit >> 8) & 0xffff),
ditherbit2 = (((ditherbit >> 16) & 0x7fff) +
((ditherbit & 0x100) << 7)),
ditherbit3 = (((ditherbit >> 24) & 0x7f) + ((ditherbit & 1) << 7) +
((ditherbit >> 8) & 0xff00));
x < dst_width;
x ++, cerror0 ++, cerror1 ++, merror0 ++, merror1 ++, yerror0 ++,
yerror1 ++, kerror0 ++, kerror1 ++)
{
/*
* First compute the standard CMYK separation color values...
*/
int maxlevel;
int ak;
int kdarkness;
c = 65535 - (unsigned) rgb[0];
m = 65535 - (unsigned) rgb[1];
y = 65535 - (unsigned) rgb[2];
oc = c;
om = m;
oy = y;
k = MIN(c, MIN(m, y));
#ifdef PRINT_DEBUG
xc = c;
xm = m;
xy = y;
xk = k;
yc = c;
ym = m;
yy = y;
#endif
maxlevel = MAX(c, MAX(m, y));
if (black != NULL)
{
/*
* Since we're printing black, adjust the black level based upon
* the amount of color in the pixel (colorful pixels get less black)...
*/
long long xdiff = (abs(c - m) + abs(c - y) + abs(m - y)) / 3;
diff = 65536 - xdiff;
diff = diff * diff * diff / (65536ll * 65536ll); /* diff = diff^3 */
diff--;
if (diff < 0)
diff = 0;
k = diff * k / 65535ll;
ak = k;
divk = 65535 - k;
if (divk == 0)
c = m = y = 0; /* Grayscale */
else
{
/*
* Full color; update the CMY values for the black value and reduce
* CMY as necessary to give better blues, greens, and reds... :)
*/
c = (65535 - (unsigned) rgb[1] / 4) * (c - k) / divk;
m = (65535 - (unsigned) rgb[2] / 4) * (m - k) / divk;
y = (65535 - (unsigned) rgb[0] / 4) * (y - k) / divk;
}
#ifdef PRINT_DEBUG
yc = c;
ym = m;
yy = y;
#endif
/*
* kdarkness is an artificially computed darkness value for deciding
* how much black vs. CMY to use for the k component. This is
* empirically determined.
*/
ok = k;
nk = k + (ditherk) / 8;
kdarkness = MAX((c + c / 3 + m + 2 * y / 3) / 4, ak);
/*
kdarkness = ak;
*/
if (kdarkness < KDARKNESS_UPPER)
{
int rb;
/*
ub = KDARKNESS_UPPER - kdarkness;
lb = ub * KDARKNESS_LOWER / KDARKNESS_UPPER;
*/
ub = KDARKNESS_UPPER;
lb = KDARKNESS_LOWER;
rb = ub - lb;
#ifdef PRINT_DEBUG
fprintf(dbg, "Black: kd %d ub %d lb %d rb %d test %d range %d\n",
kdarkness, ub, lb, rb, ditherbit % rb, kdarkness - lb);
#endif
if (kdarkness <= lb)
{
bk = 0;
ub = 0;
lb = 1;
}
else if (kdarkness < ub)
{
if (rb == 0 || (ditherbit % rb) < (kdarkness - lb))
bk = nk;
else
bk = 0;
}
else
{
ub = 1;
lb = 1;
bk = nk;
}
}
else
{
#ifdef PRINT_DEBUG
fprintf(dbg, "Black real\n");
#endif
bk = nk;
}
ck = nk - bk;
/*
* These constants are empirically determined to produce a CMY value
* that looks reasonably gray and is reasonably well balanced tonally
* with black. As usual, this is very ad hoc and needs to be
* generalized.
*/
if (lmagenta)
{
c += ck * 10 / 8;
m += ck * 19 / 16;
y += ck * 3 / 2;
}
else
{
c += ck;
m += ck;
y += ck;
}
/*
* Don't allow cmy to grow without bound.
*/
if (c > 65535)
c = 65535;
if (m > 65535)
m = 65535;
if (y > 65535)
y = 65535;
k = bk;
#ifdef PRINT_DEBUG
odk = ditherk;
dk = k;
#endif
if (k > (32767 + ((ditherbit0 / K_RANDOMIZER) - (32768 / K_RANDOMIZER))))
{
if (kbits++ % horizontal_overdensity == 0)
*kptr |= bit;
k -= 65535;
}
UPDATE_DITHER(k, 1, x, src_width);
#if 0
if (ditherbit0 & bit)
{
if (x > 0)
kerror1[-1] += k;
else
kerror1[0] = k;
kerror1[0] += 2 * k;
kerror1[1] = k;
ditherk = kerror0[1] + 3 * k;
}
else
{
if (x > 0)
kerror1[-1] += k / 2;
else
kerror1[0] = k / 2;
kerror1[0] += k;
kerror1[1] = k / 2;
ditherk = kerror0[1] + 5 * k;
}
#endif
}
else
{
/*
* We're not printing black, but let's adjust the CMY levels to produce
* better reds, greens, and blues...
*/
ok = 0;
c = (65535 - rgb[1] / 4) * (c - k) / 65535 + k;
m = (65535 - rgb[2] / 4) * (m - k) / 65535 + k;
y = (65535 - rgb[0] / 4) * (y - k) / 65535 + k;
}
UPDATE_COLOR(c);
UPDATE_COLOR(m);
UPDATE_COLOR(y);
density = (c + m + y) / horizontal_overdensity;
/*****************************************************************
* Cyan
*****************************************************************/
if (! (*kptr & bit))
PRINT_COLOR(cyan, c, C, 1, 2);
UPDATE_DITHER(c, 2, x, dst_width);
/*****************************************************************
* Magenta
*****************************************************************/
if (! (*kptr & bit))
PRINT_COLOR(magenta, m, M, 2, 3);
UPDATE_DITHER(m, 3, x, dst_width);
/*****************************************************************
* Yellow
*****************************************************************/
if (! (*kptr & bit))
PRINT_COLOR(yellow, y, Y, 3, 0);
UPDATE_DITHER(y, 0, x, dst_width);
/*****************************************************************
* Advance the loop
*****************************************************************/
#ifdef PRINT_DEBUG
fprintf(dbg, " x %d y %d r %d g %d b %d xc %lld xm %lld xy %lld yc "
"%lld ym %lld yy %lld xk %lld diff %lld divk %lld oc %lld om "
"%lld oy %lld ok %lld c %lld m %lld y %lld k %lld %c%c%c%c%c%c%c"
" dk %lld dc %lld dm %lld dy %lld kd %d ck %d bk %d nk %d ub %d "
"lb %d\n",
x, row,
rgb[0], rgb[1], rgb[2],
xc, xm, xy, yc, ym, yy, xk, diff, divk,
oc, om, oy, ok,
dc, dm, dy, dk,
(*cptr & bit) ? 'c' : ' ',
(lcyan && (*lcptr & bit)) ? 'C' : ' ',
(*mptr & bit) ? 'm' : ' ',
(lmagenta && (*lmptr & bit)) ? 'M' : ' ',
(*yptr & bit) ? 'y' : ' ',
(lyellow && (*lyptr & bit)) ? 'Y' : ' ',
(black && (*kptr & bit)) ? 'k' : ' ',
odk, odc, odm, ody,
kdarkness, ck, bk, nk, ub, lb);
#endif
if (bit == 1)
{
cptr ++;
if (lcptr)
lcptr ++;
mptr ++;
if (lmptr)
lmptr ++;
yptr ++;