gxdither.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* Copyright (C) 1989, 1995, 1996, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gxdither.c,v 1.2 2000/09/19 19:00:36 lpd Exp $ */
  16. #include "gx.h"
  17. #include "gsstruct.h"
  18. #include "gsdcolor.h"
  19. #include "gxdevice.h"
  20. #include "gxlum.h"
  21. #include "gxcmap.h"
  22. #include "gxdither.h"
  23. #include "gzht.h"
  24. /*
  25. * Binary halftoning algorithms.
  26. *
  27. * The procedures in this file use halftoning (if necessary)
  28. * to implement a given device color that has already gone through
  29. * the transfer function. There are two major cases: gray and color.
  30. * Gray halftoning always uses a binary screen. Color halftoning
  31. * uses either a fast algorithm with a binary screen that produces
  32. * relatively poor approximations, or a very slow algorithm with a
  33. * general colored screen (or screens) that faithfully implements
  34. * the Adobe specifications.
  35. */
  36. /* Tables for fast computation of fractional color levels. */
  37. /* We have to put the table before any uses of it because of a bug */
  38. /* in the VAX C compiler. */
  39. /* We have to split up the definition of the table itself because of a bug */
  40. /* in the IBM AIX 3.2 C compiler. */
  41. private const gx_color_value q0[] = {
  42. 0
  43. };
  44. private const gx_color_value q1[] = {
  45. 0, frac_color_(1, 1)
  46. };
  47. private const gx_color_value q2[] = {
  48. 0, frac_color_(1, 2), frac_color_(2, 2)
  49. };
  50. private const gx_color_value q3[] = {
  51. 0, frac_color_(1, 3), frac_color_(2, 3), frac_color_(3, 3)
  52. };
  53. private const gx_color_value q4[] = {
  54. 0, frac_color_(1, 4), frac_color_(2, 4), frac_color_(3, 4),
  55. frac_color_(4, 4)
  56. };
  57. private const gx_color_value q5[] = {
  58. 0, frac_color_(1, 5), frac_color_(2, 5), frac_color_(3, 5),
  59. frac_color_(4, 5), frac_color_(5, 5)
  60. };
  61. private const gx_color_value q6[] = {
  62. 0, frac_color_(1, 6), frac_color_(2, 6), frac_color_(3, 6),
  63. frac_color_(4, 6), frac_color_(5, 6), frac_color_(6, 6)
  64. };
  65. private const gx_color_value q7[] = {
  66. 0, frac_color_(1, 7), frac_color_(2, 7), frac_color_(3, 7),
  67. frac_color_(4, 7), frac_color_(5, 7), frac_color_(6, 7), frac_color_(7, 7)
  68. };
  69. /* We export fc_color_quo for the fractional_color macro in gzht.h. */
  70. const gx_color_value *const fc_color_quo[8] = {
  71. q0, q1, q2, q3, q4, q5, q6, q7
  72. };
  73. /* Render a gray, possibly by halftoning. */
  74. int
  75. gx_render_device_gray(frac gray, gx_color_value alpha, gx_device_color * pdevc,
  76. gx_device * dev, gx_device_halftone * pdht,
  77. const gs_int_point * ht_phase)
  78. {
  79. bool cmyk = dev->color_info.num_components == 4;
  80. /* Make a special check for black and white. */
  81. if (alpha == gx_max_color_value) {
  82. gx_color_value lum;
  83. switch (gray) {
  84. case frac_0:
  85. lum = 0;
  86. goto bw;
  87. case frac_1:
  88. lum = gx_max_color_value;
  89. bw:color_set_pure(pdevc,
  90. (cmyk ?
  91. gx_map_cmyk_color(dev, 0, 0, 0,
  92. gx_max_color_value - lum) :
  93. gx_map_rgb_color(dev, lum, lum, lum)));
  94. return 0;
  95. default:
  96. ;
  97. }
  98. }
  99. /* get a few handy values */
  100. {
  101. uint max_value = dev->color_info.dither_grays - 1;
  102. unsigned long hsize = (unsigned)pdht->order.num_levels;
  103. unsigned long nshades = hsize * max_value + 1;
  104. unsigned long lx = (nshades * gray) / (frac_1_long + 1);
  105. uint v = lx / hsize;
  106. gx_color_value lum = fractional_color(v, max_value);
  107. gx_color_index color1;
  108. int level = lx % hsize;
  109. /* The following should be a conditional expression, */
  110. /* but the DECStation 3100 Ultrix 4.3 compiler */
  111. /* generates bad code for it. */
  112. #define SET_COLOR_LUM(col, lum)\
  113. if ( cmyk )\
  114. col = gx_map_cmyk_color(dev, 0, 0, 0,\
  115. gx_max_color_value - lum);\
  116. else if ( alpha == gx_max_color_value )\
  117. col = gx_map_rgb_color(dev, lum, lum, lum);\
  118. else\
  119. col = gx_map_rgb_alpha_color(dev, lum, lum, lum, alpha)
  120. SET_COLOR_LUM(color1, lum);
  121. if_debug5('c', "[c]gray=0x%x --> (%d+%d/%lu)/%d\n",
  122. (unsigned)gray, v, level, hsize, max_value + 1);
  123. if (level == 0) { /* Close enough to a pure color, */
  124. /* no dithering needed. */
  125. color_set_pure(pdevc, color1);
  126. return 0;
  127. } else {
  128. gx_color_index color2;
  129. v++;
  130. lum = fractional_color(v, max_value);
  131. SET_COLOR_LUM(color2, lum);
  132. color_set_binary_halftone(pdevc, pdht,
  133. color1, color2, level);
  134. color_set_phase_mod(pdevc, ht_phase->x, ht_phase->y,
  135. pdht->order.width,
  136. pdht->order.full_height);
  137. return 1;
  138. }
  139. #undef SET_COLOR_LUM
  140. }
  141. }
  142. /*
  143. * Color dithering for Ghostscript. The underlying device imaging model
  144. * supports dithering between two colors to generate intermediate shades.
  145. *
  146. * If the device has high quality colors (at least 32 values
  147. * per axis), we ask it to map the color directly.
  148. *
  149. * Otherwise, things are a bit more complicated. If the device
  150. * supports N shades of each R, G and B independently, there are a total
  151. * of N*N*N colors. These colors form a 3-D grid in a cubical color
  152. * space. The following dithering technique works by locating the
  153. * color we want in this 3-D color grid and finding the eight colors
  154. * that surround it. In the case of dithering into 8 colors with 1
  155. * bit for each red, green and blue, these eight colors will always
  156. * be the same.
  157. *
  158. * Now we consider all possible diagonal paths between the eight colors
  159. * and chose the path that runs closest to our desired color in 3-D
  160. * color space. There are 28 such paths. Then we find the position
  161. * on the path that is closest to our color.
  162. *
  163. * The search is made faster by always reflecting our color into
  164. * the bottom octant of the cube and comparing it to 7 paths.
  165. * After the best path and the best position on that path are found,
  166. * the results are reflected back into the original color space.
  167. *
  168. * NOTE: This code has been tested for B/W and Color imaging with
  169. * 1, 2, 3 and 8 bits per component.
  170. *
  171. * --- original code by Paul Haeberli @ Silicon Graphics - 1990
  172. * --- extensively revised by L. Peter Deutsch, Aladdin Enterprises
  173. *
  174. * lpd 3/14/94: added support for CMYK.
  175. */
  176. /*
  177. * The weights are arbitrary, as long as their ratios are correct
  178. * and they will fit into the difference between a ulong and a frac
  179. * with room to spare. By making WEIGHT1 and WEIGHT4 powers of 2,
  180. * we can turn some multiplies into shifts.
  181. */
  182. #define WNUM 128000
  183. #define WEIGHT1 (ulong)(WNUM/1000) /* 1.0 */
  184. #define WEIGHT2 (ulong)(WNUM/1414) /* 1/sqrt(2.0) */
  185. #define WEIGHT3 (ulong)(WNUM/1732) /* 1/sqrt(3.0) */
  186. #define WEIGHT4 (ulong)(WNUM/2000) /* 1/sqrt(4.0) */
  187. #define DIAG_R (0x1)
  188. #define DIAG_G (0x2)
  189. #define DIAG_B (0x4)
  190. #define DIAG_W (0x8)
  191. #define DIAG_RG (0x3)
  192. #define DIAG_GB (0x6)
  193. #define DIAG_BR (0x5)
  194. #define DIAG_RGB (0x7)
  195. #define DIAG_RGBW (0xf)
  196. /* What should we do about the W/K component? For the moment, */
  197. /* we ignore it in the luminance computation. */
  198. #define lum_white_weight 0
  199. private const unsigned short lum_w[16] =
  200. {
  201. (0 * lum_blue_weight + 0 * lum_green_weight + 0 * lum_red_weight + 0 * lum_white_weight),
  202. (0 * lum_blue_weight + 0 * lum_green_weight + 1 * lum_red_weight + 0 * lum_white_weight),
  203. (0 * lum_blue_weight + 1 * lum_green_weight + 0 * lum_red_weight + 0 * lum_white_weight),
  204. (0 * lum_blue_weight + 1 * lum_green_weight + 1 * lum_red_weight + 0 * lum_white_weight),
  205. (1 * lum_blue_weight + 0 * lum_green_weight + 0 * lum_red_weight + 0 * lum_white_weight),
  206. (1 * lum_blue_weight + 0 * lum_green_weight + 1 * lum_red_weight + 0 * lum_white_weight),
  207. (1 * lum_blue_weight + 1 * lum_green_weight + 0 * lum_red_weight + 0 * lum_white_weight),
  208. (1 * lum_blue_weight + 1 * lum_green_weight + 1 * lum_red_weight + 0 * lum_white_weight),
  209. (0 * lum_blue_weight + 0 * lum_green_weight + 0 * lum_red_weight + 1 * lum_white_weight),
  210. (0 * lum_blue_weight + 0 * lum_green_weight + 1 * lum_red_weight + 1 * lum_white_weight),
  211. (0 * lum_blue_weight + 1 * lum_green_weight + 0 * lum_red_weight + 1 * lum_white_weight),
  212. (0 * lum_blue_weight + 1 * lum_green_weight + 1 * lum_red_weight + 1 * lum_white_weight),
  213. (1 * lum_blue_weight + 0 * lum_green_weight + 0 * lum_red_weight + 1 * lum_white_weight),
  214. (1 * lum_blue_weight + 0 * lum_green_weight + 1 * lum_red_weight + 1 * lum_white_weight),
  215. (1 * lum_blue_weight + 1 * lum_green_weight + 0 * lum_red_weight + 1 * lum_white_weight),
  216. (1 * lum_blue_weight + 1 * lum_green_weight + 1 * lum_red_weight + 1 * lum_white_weight)
  217. };
  218. /*
  219. * Render RGB or CMYK, possibly by halftoning. If we are rendering RGB,
  220. * white is ignored. If we are rendering CMYK, red/green/blue/white are
  221. * actually cyan/magenta/yellow/black.
  222. */
  223. int
  224. gx_render_device_color(frac red, frac green, frac blue, frac white, bool cmyk,
  225. gx_color_value alpha, gx_device_color * pdevc, gx_device * dev,
  226. gx_device_halftone * pdht, const gs_int_point * ht_phase)
  227. {
  228. uint max_value = dev->color_info.dither_colors - 1;
  229. uint num_levels = pdht->order.num_levels;
  230. frac rem_r, rem_g, rem_b, rem_w;
  231. uint r, g, b, w;
  232. gx_color_value vr, vg, vb, vw;
  233. #define MAP_COLOR_RGB()\
  234. (alpha == gx_max_color_value ?\
  235. gx_map_rgb_color(dev, vr, vg, vb) :\
  236. gx_map_rgb_alpha_color(dev, vr, vg, vb, alpha))
  237. #define MAP_COLOR_CMYK()\
  238. gx_map_cmyk_color(dev, vr, vg, vb, vw)
  239. #define MAP_COLOR()\
  240. (cmyk ? MAP_COLOR_CMYK() : MAP_COLOR_RGB())
  241. /* Compute the quotient and remainder of each color component */
  242. /* with the actual number of available colors. */
  243. switch (max_value) {
  244. case 1: /* 8 or 16 colors */
  245. if (red == frac_1)
  246. rem_r = 0, r = 1;
  247. else
  248. rem_r = red, r = 0;
  249. if (green == frac_1)
  250. rem_g = 0, g = 1;
  251. else
  252. rem_g = green, g = 0;
  253. if (blue == frac_1)
  254. rem_b = 0, b = 1;
  255. else
  256. rem_b = blue, b = 0;
  257. if (white == frac_1)
  258. rem_w = 0, w = 1;
  259. else
  260. rem_w = white, w = 0;
  261. break;
  262. default:
  263. {
  264. ulong want_r, want_g, want_b, want_w;
  265. want_r = (ulong) max_value * red;
  266. r = frac_1_quo(want_r);
  267. rem_r = frac_1_rem(want_r, r);
  268. want_g = (ulong) max_value * green;
  269. g = frac_1_quo(want_g);
  270. rem_g = frac_1_rem(want_g, g);
  271. want_b = (ulong) max_value * blue;
  272. b = frac_1_quo(want_b);
  273. rem_b = frac_1_rem(want_b, b);
  274. want_w = (ulong) max_value * white;
  275. w = frac_1_quo(want_w);
  276. rem_w = frac_1_rem(want_w, w);
  277. }
  278. }
  279. /* Check for no dithering required */
  280. if (!(rem_r | rem_g | rem_b | rem_w)) {
  281. vr = fractional_color(r, max_value);
  282. vg = fractional_color(g, max_value);
  283. vb = fractional_color(b, max_value);
  284. vw = fractional_color(w, max_value);
  285. color_set_pure(pdevc, MAP_COLOR());
  286. return 0;
  287. }
  288. if_debug12('c', "[c]rgbw=0x%x,0x%x,0x%x,0x%x -->\n %x+0x%x,%x+0x%x,%x+0x%x,%x+0x%x -->\n",
  289. (unsigned)red, (unsigned)green, (unsigned)blue, (unsigned)white,
  290. (unsigned)r, (unsigned)rem_r, (unsigned)g, (unsigned)rem_g,
  291. (unsigned)b, (unsigned)rem_b, (unsigned)w, (unsigned)rem_w);
  292. /* Dithering is required. Choose between two algorithms. */
  293. if (dev->color_info.num_components >= 4) {
  294. /* This is a CMYK device. */
  295. /* Use the slow, general colored halftone algorithm. */
  296. #define RGB_REM(rem_v, i)\
  297. (rem_v * (ulong)(pdht->components ? pdht->components[pdht->color_indices[i]].corder.num_levels : num_levels) / frac_1)
  298. uint lr = RGB_REM(rem_r, 0), lg = RGB_REM(rem_g, 1),
  299. lb = RGB_REM(rem_b, 2);
  300. if (cmyk)
  301. color_set_cmyk_halftone(pdevc, pdht, r, lr, g, lg, b, lb,
  302. w, RGB_REM(rem_w, 3));
  303. else
  304. color_set_rgb_halftone(pdevc, pdht, r, lr, g, lg, b, lb, alpha);
  305. #undef RGB_REM
  306. color_set_phase_mod(pdevc, ht_phase->x, ht_phase->y,
  307. pdht->lcm_width, pdht->lcm_height);
  308. if (!(pdevc->colors.colored.plane_mask &
  309. (pdevc->colors.colored.plane_mask - 1))) {
  310. /* We can reduce this color to a binary halftone or pure color. */
  311. return gx_reduce_colored_halftone(pdevc, dev, cmyk);
  312. }
  313. return 1;
  314. }
  315. /* Fast, approximate binary halftone algorithm. */
  316. {
  317. ulong hsize = num_levels;
  318. int adjust_r, adjust_b, adjust_g, adjust_w;
  319. gx_color_index color1;
  320. frac amax, amin;
  321. ulong fmax, cmax;
  322. int axisc, facec, cubec, diagc;
  323. unsigned short lum_invert;
  324. ulong dot1, dot2, dot3, dot4;
  325. int level;
  326. int code;
  327. /* Flip the remainder color into the 0, 0, 0 octant. */
  328. lum_invert = 0;
  329. #define half (frac_1/2)
  330. if (rem_r > half)
  331. rem_r = frac_1 - rem_r,
  332. adjust_r = -1, r++, lum_invert += lum_red_weight * 2;
  333. else
  334. adjust_r = 1;
  335. if (rem_g > half)
  336. rem_g = frac_1 - rem_g,
  337. adjust_g = -1, g++, lum_invert += lum_green_weight * 2;
  338. else
  339. adjust_g = 1;
  340. if (rem_b > half)
  341. rem_b = frac_1 - rem_b,
  342. adjust_b = -1, b++, lum_invert += lum_blue_weight * 2;
  343. else
  344. adjust_b = 1;
  345. vr = fractional_color(r, max_value);
  346. vg = fractional_color(g, max_value);
  347. vb = fractional_color(b, max_value);
  348. if (cmyk) {
  349. if (rem_w > half)
  350. rem_w = frac_1 - rem_w,
  351. adjust_w = -1, w++, lum_invert += lum_white_weight * 2;
  352. else
  353. adjust_w = 1;
  354. vw = fractional_color(w, max_value);
  355. color1 = MAP_COLOR_CMYK();
  356. } else
  357. color1 = MAP_COLOR_RGB();
  358. /*
  359. * Dot the color with each axis to find the best one of 15;
  360. * find the color at the end of the axis chosen.
  361. */
  362. cmax = (ulong) rem_r + rem_g + rem_b;
  363. dot4 = cmax + rem_w;
  364. if (rem_g > rem_r) {
  365. if (rem_b > rem_g)
  366. amax = rem_b, axisc = DIAG_B;
  367. else
  368. amax = rem_g, axisc = DIAG_G;
  369. if (rem_b > rem_r)
  370. amin = rem_r, fmax = (ulong) rem_g + rem_b, facec = DIAG_GB;
  371. else
  372. amin = rem_b, fmax = (ulong) rem_r + rem_g, facec = DIAG_RG;
  373. } else {
  374. if (rem_b > rem_r)
  375. amax = rem_b, axisc = DIAG_B;
  376. else
  377. amax = rem_r, axisc = DIAG_R;
  378. if (rem_b > rem_g)
  379. amin = rem_g, fmax = (ulong) rem_b + rem_r, facec = DIAG_BR;
  380. else
  381. amin = rem_b, fmax = (ulong) rem_r + rem_g, facec = DIAG_RG;
  382. }
  383. if (rem_w > amin) {
  384. cmax = fmax + rem_w, cubec = facec + DIAG_W;
  385. if (rem_w > amax)
  386. fmax = (ulong) amax + rem_w, facec = axisc + DIAG_W,
  387. amax = rem_w, axisc = DIAG_W;
  388. else if (rem_w > fmax - amax)
  389. fmax = (ulong) amax + rem_w, facec = axisc + DIAG_W;
  390. } else
  391. cubec = DIAG_RGB;
  392. dot1 = amax * WEIGHT1;
  393. dot2 = fmax * WEIGHT2;
  394. dot3 = cmax * WEIGHT3;
  395. /*dot4 see above */
  396. #define use_axis()\
  397. diagc = axisc, level = (hsize * amax + (frac_1_long / 2)) / frac_1_long
  398. #define use_face()\
  399. diagc = facec, level = (hsize * fmax + frac_1_long) / (2 * frac_1_long)
  400. #define use_cube()\
  401. diagc = cubec, level = (hsize * cmax + (3 * frac_1_long / 2)) / (3 * frac_1_long)
  402. #define use_tesseract()\
  403. diagc = DIAG_RGBW, level = (hsize * dot4 + (2 * frac_1_long)) / (4 * frac_1_long)
  404. if (dot1 > dot2) {
  405. if (dot3 > dot1) {
  406. if (dot4 * WEIGHT4 > dot3)
  407. use_tesseract();
  408. else
  409. use_cube();
  410. } else {
  411. if (dot4 * WEIGHT4 > dot1)
  412. use_tesseract();
  413. else
  414. use_axis();
  415. }
  416. } else {
  417. if (dot3 > dot2) {
  418. if (dot4 * WEIGHT4 > dot3)
  419. use_tesseract();
  420. else
  421. use_cube();
  422. } else {
  423. if (dot4 * WEIGHT4 > dot2)
  424. use_tesseract();
  425. else
  426. use_face();
  427. }
  428. };
  429. if_debug12('c', " %x+0x%x,%x+0x%x,%x+0x%x,%x+0x%x; adjust=%d,%d,%d,%d\n",
  430. (unsigned)r, (unsigned)rem_r, (unsigned)g, (unsigned)rem_g,
  431. (unsigned)b, (unsigned)rem_b, (unsigned)w, (unsigned)rem_w,
  432. adjust_r, adjust_g, adjust_b, adjust_w);
  433. if (level == 0) {
  434. color_set_pure(pdevc, color1);
  435. code = 0;
  436. } else {
  437. gx_color_index color2;
  438. /* construct the second color, inverting back to original space if needed */
  439. if (diagc & DIAG_R)
  440. r += adjust_r;
  441. if (diagc & DIAG_G)
  442. g += adjust_g;
  443. if (diagc & DIAG_B)
  444. b += adjust_b;
  445. /* get the second device color, sorting by luminance */
  446. vr = fractional_color(r, max_value);
  447. vg = fractional_color(g, max_value);
  448. vb = fractional_color(b, max_value);
  449. if (cmyk) {
  450. if (diagc & DIAG_W)
  451. w += adjust_w;
  452. vw = fractional_color(w, max_value);
  453. color2 = MAP_COLOR_CMYK();
  454. } else
  455. color2 = MAP_COLOR_RGB();
  456. if (level == num_levels) { /* This can only happen through rounding.... */
  457. color_set_pure(pdevc, color2);
  458. code = 0;
  459. } else {
  460. if (lum_w[diagc] < lum_invert)
  461. color_set_binary_halftone(pdevc, pdht, color2, color1, hsize - level);
  462. else
  463. color_set_binary_halftone(pdevc, pdht, color1, color2, level);
  464. color_set_phase_mod(pdevc, ht_phase->x, ht_phase->y,
  465. pdht->order.width,
  466. pdht->order.full_height);
  467. code = 1;
  468. }
  469. }
  470. if_debug7('c', "[c]diagc=%d; colors=0x%lx,0x%lx; level=%d/%d; lum=%d,diag=%d\n",
  471. diagc, (ulong) pdevc->colors.binary.color[0],
  472. (ulong) pdevc->colors.binary.color[1],
  473. level, (unsigned)hsize, lum_invert, lum_w[diagc]);
  474. return code;
  475. }
  476. }
  477. /* Reduce a colored halftone to a binary halftone or pure color. */
  478. int
  479. gx_reduce_colored_halftone(gx_device_color *pdevc, gx_device *dev, bool cmyk)
  480. {
  481. int planes = pdevc->colors.colored.plane_mask;
  482. gx_color_value max_color = dev->color_info.dither_colors - 1;
  483. uint b[4];
  484. gx_color_value v[4];
  485. gx_color_index c0, c1;
  486. b[0] = pdevc->colors.colored.c_base[0];
  487. v[0] = fractional_color(b[0], max_color);
  488. b[1] = pdevc->colors.colored.c_base[1];
  489. v[1] = fractional_color(b[1], max_color);
  490. b[2] = pdevc->colors.colored.c_base[2];
  491. v[2] = fractional_color(b[2], max_color);
  492. if (cmyk) {
  493. b[3] = pdevc->colors.colored.c_base[3];
  494. v[3] = fractional_color(b[3], max_color);
  495. c0 = dev_proc(dev, map_cmyk_color)(dev, v[0], v[1], v[2], v[3]);
  496. } else
  497. c0 = dev_proc(dev, map_rgb_color)(dev, v[0], v[1], v[2]);
  498. if (planes == 0) {
  499. /*
  500. * Use a pure color. This case is unlikely, but it can occur if
  501. * (and only if) the difference of each component from the nearest
  502. * device color is less than one halftone level.
  503. */
  504. color_set_pure(pdevc, c0);
  505. return 0;
  506. } else {
  507. /* Use a binary color. */
  508. int i = (planes >> 1) - (planes >> 3); /* log2 for 1,2,4,8 */
  509. uint bi = b[i] + 1;
  510. const gx_device_halftone *pdht = pdevc->colors.colored.c_ht;
  511. int index =
  512. (pdht->components == 0 ? -1 : pdht->color_indices[i]);
  513. /*
  514. * NB: the halftone orders are all set up for an additive color
  515. * space. To use these work with a cmyk color space, it is
  516. * necessary to invert both the color level and the color
  517. * pair. Note that if the original color was provided an
  518. * additive space, this will reverse (in an approximate sense)
  519. * the color conversion performed to express the color in cmyk
  520. * space.
  521. */
  522. bool invert = dev->color_info.num_components == 4; /****** HACK ******/
  523. uint level = pdevc->colors.colored.c_level[i];
  524. v[i] = fractional_color(bi, max_color);
  525. c1 = (cmyk ?
  526. dev_proc(dev, map_cmyk_color)(dev, v[0], v[1], v[2], v[3]) :
  527. dev_proc(dev, map_rgb_color)(dev, v[0], v[1], v[2]));
  528. if (invert) {
  529. level =
  530. (index < 0 ? &pdht->order : &pdht->components[index].corder)
  531. ->num_levels - level;
  532. color_set_binary_halftone_component(pdevc, pdht, index, c1, c0,
  533. level);
  534. } else
  535. color_set_binary_halftone_component(pdevc, pdht, index, c0, c1, level);
  536. return 1;
  537. }
  538. }