gsalphac.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /* Copyright (C) 1997, 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: gsalphac.c,v 1.2 2000/09/19 19:00:25 lpd Exp $ */
  16. /* Alpha-compositing implementation */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gsalphac.h"
  21. #include "gsiparam.h" /* for gs_image_alpha_t */
  22. #include "gsutil.h" /* for gs_next_ids */
  23. #include "gxalpha.h"
  24. #include "gxcomp.h"
  25. #include "gxdevice.h"
  26. #include "gxgetbit.h"
  27. #include "gxlum.h"
  28. /* ---------------- Internal definitions ---------------- */
  29. /* Define the parameters for a compositing operation. */
  30. typedef struct gs_composite_params_s {
  31. gs_composite_op_t cop;
  32. float delta; /* only for dissolve */
  33. uint source_alpha; /* only if !psource->alpha */
  34. uint source_values[4]; /* only if !psource->data */
  35. } gs_composite_params_t;
  36. /* Define the source or destination for a compositing operation. */
  37. #define pixel_row_fields(elt_type)\
  38. elt_type *data;\
  39. int bits_per_value; /* 1, 2, 4, 8, 12, 16 */\
  40. int initial_x;\
  41. gs_image_alpha_t alpha
  42. typedef struct pixel_row_s {
  43. pixel_row_fields(byte);
  44. } pixel_row_t;
  45. typedef struct const_pixel_row_s {
  46. pixel_row_fields(const byte);
  47. } const_pixel_row_t;
  48. /*
  49. * Composite two arrays of (premultiplied) pixel values. Legal values of
  50. * values_per_pixel are 1-4, not including alpha. Note that if pdest->alpha
  51. * is "none", the alpha value for all destination pixels will be taken as
  52. * unity, and any operation that could generate alpha values other than
  53. * unity will return an error. "Could generate" means that there are
  54. * possible values of the source and destination alpha values for which the
  55. * result has non-unity alpha: the error check does not scan the actual
  56. * alpha data to test whether there are any actual values that would
  57. * generate a non-unity alpha result.
  58. */
  59. int composite_values(P5(const pixel_row_t * pdest,
  60. const const_pixel_row_t * psource,
  61. int values_per_pixel, uint num_pixels,
  62. const gs_composite_params_t * pcp));
  63. /* ---------------- Alpha-compositing objects ---------------- */
  64. /*
  65. * Define which operations can generate non-unity alpha values in 3 of the 4
  66. * cases of source and destination not having unity alphas. (This is always
  67. * possible in the fourth case, both S & D non-unity, except for CLEAR.) We
  68. * do this with a bit mask indexed by the operation, counting from the LSB.
  69. * The name indicates whether S and/or D has non-unity alphas.
  70. */
  71. #define alpha_out_notS_notD\
  72. (1<<composite_Dissolve)
  73. #define _alpha_out_either\
  74. (alpha_out_notS_notD|(1<<composite_Satop)|(1<<composite_Datop)|\
  75. (1<<composite_Xor)|(1<<composite_PlusD)|(1<<composite_PlusL))
  76. #define alpha_out_S_notD\
  77. (_alpha_out_either|(1<<composite_Copy)|(1<<composite_Sover)|\
  78. (1<<composite_Din)|(1<<composite_Dout))
  79. #define alpha_out_notS_D\
  80. (_alpha_out_either|(1<<composite_Sin)|(1<<composite_Sout)|\
  81. (1<<composite_Dover)|(1<<composite_Highlight))
  82. /* ------ Object definition and creation ------ */
  83. /* Define alpha-compositing objects. */
  84. private composite_create_default_compositor_proc(c_alpha_create_default_compositor);
  85. private composite_equal_proc(c_alpha_equal);
  86. private composite_write_proc(c_alpha_write);
  87. private composite_read_proc(c_alpha_read);
  88. private const gs_composite_type_t gs_composite_alpha_type =
  89. {
  90. {
  91. c_alpha_create_default_compositor,
  92. c_alpha_equal,
  93. c_alpha_write,
  94. c_alpha_read
  95. }
  96. };
  97. typedef struct gs_composite_alpha_s {
  98. gs_composite_common;
  99. gs_composite_alpha_params_t params;
  100. } gs_composite_alpha_t;
  101. gs_private_st_simple(st_composite_alpha, gs_composite_alpha_t,
  102. "gs_composite_alpha_t");
  103. /* Create an alpha-compositing object. */
  104. int
  105. gs_create_composite_alpha(gs_composite_t ** ppcte,
  106. const gs_composite_alpha_params_t * params, gs_memory_t * mem)
  107. {
  108. gs_composite_alpha_t *pcte;
  109. rc_alloc_struct_0(pcte, gs_composite_alpha_t, &st_composite_alpha,
  110. mem, return_error(gs_error_VMerror),
  111. "gs_create_composite_alpha");
  112. pcte->type = &gs_composite_alpha_type;
  113. pcte->id = gs_next_ids(1);
  114. pcte->params = *params;
  115. *ppcte = (gs_composite_t *) pcte;
  116. return 0;
  117. }
  118. /* ------ Object implementation ------ */
  119. #define pacte ((const gs_composite_alpha_t *)pcte)
  120. private bool
  121. c_alpha_equal(const gs_composite_t * pcte, const gs_composite_t * pcte2)
  122. {
  123. return (pcte2->type == pcte->type &&
  124. #define pacte2 ((const gs_composite_alpha_t *)pcte2)
  125. pacte2->params.op == pacte->params.op &&
  126. (pacte->params.op != composite_Dissolve ||
  127. pacte2->params.delta == pacte->params.delta));
  128. #undef pacte2
  129. }
  130. private int
  131. c_alpha_write(const gs_composite_t * pcte, byte * data, uint * psize)
  132. {
  133. uint size = *psize;
  134. uint used;
  135. if (pacte->params.op == composite_Dissolve) {
  136. used = 1 + sizeof(pacte->params.delta);
  137. if (size < used) {
  138. *psize = used;
  139. return_error(gs_error_rangecheck);
  140. }
  141. memcpy(data + 1, &pacte->params.delta, sizeof(pacte->params.delta));
  142. } else {
  143. used = 1;
  144. if (size < used) {
  145. *psize = used;
  146. return_error(gs_error_rangecheck);
  147. }
  148. }
  149. *data = (byte) pacte->params.op;
  150. *psize = used;
  151. return 0;
  152. }
  153. private int
  154. c_alpha_read(gs_composite_t ** ppcte, const byte * data, uint size,
  155. gs_memory_t * mem)
  156. {
  157. gs_composite_alpha_params_t params;
  158. if (size < 1 || *data > composite_op_last)
  159. return_error(gs_error_rangecheck);
  160. params.op = *data;
  161. if (params.op == composite_Dissolve) {
  162. if (size != 1 + sizeof(params.delta))
  163. return_error(gs_error_rangecheck);
  164. memcpy(&params.delta, data + 1, sizeof(params.delta));
  165. } else {
  166. if (size != 1)
  167. return_error(gs_error_rangecheck);
  168. }
  169. return gs_create_composite_alpha(ppcte, &params, mem);
  170. }
  171. /* ---------------- Alpha-compositing device ---------------- */
  172. /* Define the default alpha-compositing device. */
  173. typedef struct gx_device_composite_alpha_s {
  174. gx_device_forward_common;
  175. gs_composite_alpha_params_t params;
  176. } gx_device_composite_alpha;
  177. gs_private_st_suffix_add0_final(st_device_composite_alpha,
  178. gx_device_composite_alpha, "gx_device_composite_alpha",
  179. device_c_alpha_enum_ptrs, device_c_alpha_reloc_ptrs, gx_device_finalize,
  180. st_device_forward);
  181. /* The device descriptor. */
  182. private dev_proc_close_device(dca_close);
  183. private dev_proc_fill_rectangle(dca_fill_rectangle);
  184. private dev_proc_map_rgb_color(dca_map_rgb_color);
  185. private dev_proc_map_color_rgb(dca_map_color_rgb);
  186. private dev_proc_copy_mono(dca_copy_mono);
  187. private dev_proc_copy_color(dca_copy_color);
  188. private dev_proc_map_rgb_alpha_color(dca_map_rgb_alpha_color);
  189. private dev_proc_map_color_rgb_alpha(dca_map_color_rgb_alpha);
  190. private dev_proc_copy_alpha(dca_copy_alpha);
  191. private const gx_device_composite_alpha gs_composite_alpha_device =
  192. {std_device_std_body_open(gx_device_composite_alpha, 0,
  193. "alpha compositor", 0, 0, 1, 1),
  194. {gx_default_open_device,
  195. gx_forward_get_initial_matrix,
  196. gx_default_sync_output,
  197. gx_default_output_page,
  198. dca_close,
  199. dca_map_rgb_color,
  200. dca_map_color_rgb,
  201. dca_fill_rectangle,
  202. gx_default_tile_rectangle,
  203. dca_copy_mono,
  204. dca_copy_color,
  205. gx_default_draw_line,
  206. gx_default_get_bits,
  207. gx_forward_get_params,
  208. gx_forward_put_params,
  209. gx_default_cmyk_map_cmyk_color, /* only called for CMYK */
  210. gx_forward_get_xfont_procs,
  211. gx_forward_get_xfont_device,
  212. dca_map_rgb_alpha_color,
  213. gx_forward_get_page_device,
  214. gx_forward_get_alpha_bits,
  215. dca_copy_alpha,
  216. gx_forward_get_band,
  217. gx_default_copy_rop,
  218. gx_default_fill_path,
  219. gx_default_stroke_path,
  220. gx_default_fill_mask,
  221. gx_default_fill_trapezoid,
  222. gx_default_fill_parallelogram,
  223. gx_default_fill_triangle,
  224. gx_default_draw_thin_line,
  225. gx_default_begin_image,
  226. gx_default_image_data,
  227. gx_default_end_image,
  228. gx_default_strip_tile_rectangle,
  229. gx_default_strip_copy_rop,
  230. gx_forward_get_clipping_box,
  231. gx_default_begin_typed_image,
  232. gx_forward_get_bits_rectangle,
  233. dca_map_color_rgb_alpha,
  234. gx_no_create_compositor
  235. }
  236. };
  237. /* Create an alpha compositor. */
  238. private int
  239. c_alpha_create_default_compositor(const gs_composite_t * pcte,
  240. gx_device ** pcdev, gx_device * dev, const gs_imager_state * pis,
  241. gs_memory_t * mem)
  242. {
  243. gx_device_composite_alpha *cdev;
  244. if (pacte->params.op == composite_Copy) {
  245. /* Just use the original device. */
  246. *pcdev = dev;
  247. return 0;
  248. }
  249. cdev =
  250. gs_alloc_struct_immovable(mem, gx_device_composite_alpha,
  251. &st_device_composite_alpha,
  252. "create default alpha compositor");
  253. *pcdev = (gx_device *)cdev;
  254. if (cdev == 0)
  255. return_error(gs_error_VMerror);
  256. gx_device_init((gx_device *)cdev,
  257. (const gx_device *)&gs_composite_alpha_device, mem, true);
  258. gx_device_copy_params((gx_device *)cdev, dev);
  259. /*
  260. * Set the color_info and depth to be compatible with the target,
  261. * but using standard chunky color storage, including alpha.
  262. ****** CURRENTLY ALWAYS USE 8-BIT COLOR ******
  263. */
  264. cdev->color_info.depth =
  265. (dev->color_info.num_components == 4 ? 32 /* CMYK, no alpha */ :
  266. (dev->color_info.num_components + 1) * 8);
  267. cdev->color_info.max_gray = cdev->color_info.max_color = 255;
  268. /* No halftoning will occur, but we fill these in anyway.... */
  269. cdev->color_info.dither_grays = cdev->color_info.dither_colors = 256;
  270. /*
  271. * We could speed things up a little by tailoring the procedures in
  272. * the device to the specific num_components, but for simplicity,
  273. * we'll defer considering that until there is a demonstrated need.
  274. */
  275. gx_device_set_target((gx_device_forward *)cdev, dev);
  276. cdev->params = pacte->params;
  277. return 0;
  278. }
  279. /* Close the device and free its storage. */
  280. private int
  281. dca_close(gx_device * dev)
  282. { /*
  283. * Finalization will call close again: avoid a recursion loop.
  284. */
  285. set_dev_proc(dev, close_device, gx_default_close_device);
  286. gs_free_object(dev->memory, dev, "dca_close");
  287. return 0;
  288. }
  289. /* ------ (RGB) color mapping ------ */
  290. private gx_color_index
  291. dca_map_rgb_color(gx_device * dev,
  292. gx_color_value r, gx_color_value g, gx_color_value b)
  293. {
  294. return dca_map_rgb_alpha_color(dev, r, g, b, gx_max_color_value);
  295. }
  296. private gx_color_index
  297. dca_map_rgb_alpha_color(gx_device * dev,
  298. gx_color_value red, gx_color_value green, gx_color_value blue,
  299. gx_color_value alpha)
  300. { /*
  301. * We work exclusively with premultiplied color values, so we
  302. * have to premultiply the color components by alpha here.
  303. */
  304. byte a = gx_color_value_to_byte(alpha);
  305. #define premult_(c)\
  306. (((c) * a + gx_max_color_value / 2) / gx_max_color_value)
  307. #ifdef PREMULTIPLY_TOWARDS_WHITE
  308. byte bias = ~a;
  309. # define premult(c) (premult_(c) + bias)
  310. #else
  311. # define premult(c) premult_(c)
  312. #endif
  313. gx_color_index color;
  314. if (dev->color_info.num_components == 1) {
  315. uint lum =
  316. (red * lum_red_weight + green * lum_green_weight +
  317. blue * lum_blue_weight + lum_all_weights / 2) /
  318. lum_all_weights;
  319. if (a == 0xff)
  320. color = gx_color_value_to_byte(lum);
  321. else /* Premultiplication is necessary. */
  322. color = premult(lum);
  323. } else {
  324. if (a == 0xff)
  325. color =
  326. ((uint) gx_color_value_to_byte(red) << 16) +
  327. ((uint) gx_color_value_to_byte(green) << 8) +
  328. gx_color_value_to_byte(blue);
  329. else /* Premultiplication is necessary. */
  330. color =
  331. (premult(red) << 16) + (premult(green) << 8) + premult(blue);
  332. }
  333. #undef premult
  334. return (color << 8) + a;
  335. }
  336. private int
  337. dca_map_color_rgb(gx_device * dev, gx_color_index color,
  338. gx_color_value prgb[3])
  339. {
  340. gx_color_value red = gx_color_value_from_byte((byte) (color >> 24));
  341. byte a = (byte) color;
  342. #define postdiv_(c)\
  343. (((c) * 0xff + a / 2) / a)
  344. #ifdef PREMULTIPLY_TOWARDS_WHITE
  345. byte bias = ~a;
  346. # define postdiv(c) postdiv_(c - bias)
  347. #else
  348. # define postdiv(c) postdiv_(c)
  349. #endif
  350. if (dev->color_info.num_components == 1) {
  351. if (a != 0xff) {
  352. /* Undo premultiplication. */
  353. if (a == 0)
  354. red = 0;
  355. else
  356. red = postdiv(red);
  357. }
  358. prgb[0] = prgb[1] = prgb[2] = red;
  359. } else {
  360. gx_color_value
  361. green = gx_color_value_from_byte((byte) (color >> 16)),
  362. blue = gx_color_value_from_byte((byte) (color >> 8));
  363. if (a != 0xff) {
  364. /* Undo premultiplication. */
  365. /****** WHAT TO DO ABOUT BIG LOSS OF PRECISION? ******/
  366. if (a == 0)
  367. red = green = blue = 0;
  368. else {
  369. red = postdiv(red);
  370. green = postdiv(green);
  371. blue = postdiv(blue);
  372. }
  373. }
  374. prgb[0] = red, prgb[1] = green, prgb[2] = blue;
  375. }
  376. #undef postdiv
  377. return 0;
  378. }
  379. private int
  380. dca_map_color_rgb_alpha(gx_device * dev, gx_color_index color,
  381. gx_color_value prgba[4])
  382. {
  383. prgba[3] = gx_color_value_from_byte((byte) color);
  384. return dca_map_color_rgb(dev, color, prgba);
  385. }
  386. /* ------ Imaging ------ */
  387. private int
  388. dca_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  389. gx_color_index color)
  390. { /* This is where all the real work gets done! */
  391. gx_device_composite_alpha *adev = (gx_device_composite_alpha *) dev;
  392. gx_device *target = adev->target;
  393. byte *std_row;
  394. byte *native_row;
  395. gs_int_rect rect;
  396. gs_get_bits_params_t std_params, native_params;
  397. int code = 0;
  398. int yi;
  399. gs_composite_params_t cp;
  400. const_pixel_row_t source;
  401. pixel_row_t dest;
  402. fit_fill(dev, x, y, w, h);
  403. std_row = gs_alloc_bytes(dev->memory,
  404. (dev->color_info.depth * w + 7) >> 3,
  405. "dca_fill_rectangle(std)");
  406. native_row = gs_alloc_bytes(dev->memory,
  407. (target->color_info.depth * w + 7) >> 3,
  408. "dca_fill_rectangle(native)");
  409. if (std_row == 0 || native_row == 0) {
  410. code = gs_note_error(gs_error_VMerror);
  411. goto out;
  412. }
  413. rect.p.x = x, rect.q.x = x + w;
  414. std_params.options =
  415. gb_colors_for_device(dev) |
  416. (GB_ALPHA_LAST | GB_DEPTH_8 | GB_PACKING_CHUNKY |
  417. GB_RETURN_COPY | GB_RETURN_POINTER | GB_ALIGN_ANY |
  418. GB_OFFSET_0 | GB_OFFSET_ANY | GB_RASTER_STANDARD |
  419. GB_RASTER_ANY);
  420. cp.cop = adev->params.op;
  421. if (cp.cop == composite_Dissolve)
  422. cp.delta = adev->params.delta;
  423. {
  424. gx_color_value rgba[4];
  425. /****** DOESN'T HANDLE CMYK ******/
  426. (*dev_proc(dev, map_color_rgb_alpha)) (dev, color, rgba);
  427. cp.source_values[0] = gx_color_value_to_byte(rgba[0]);
  428. cp.source_values[1] = gx_color_value_to_byte(rgba[1]);
  429. cp.source_values[2] = gx_color_value_to_byte(rgba[2]);
  430. cp.source_alpha = gx_color_value_to_byte(rgba[3]);
  431. }
  432. source.data = 0;
  433. source.bits_per_value = 8;
  434. source.alpha = gs_image_alpha_none;
  435. for (yi = y; yi < y + h; ++yi) {
  436. /* Read a row in standard representation. */
  437. rect.p.y = yi, rect.q.y = yi + 1;
  438. std_params.data[0] = std_row;
  439. code = (*dev_proc(target, get_bits_rectangle))
  440. (target, &rect, &std_params, NULL);
  441. if (code < 0)
  442. break;
  443. /* Do the work. */
  444. dest.data = std_params.data[0];
  445. dest.bits_per_value = 8;
  446. dest.initial_x =
  447. (std_params.options & GB_OFFSET_ANY ? std_params.x_offset : 0);
  448. dest.alpha =
  449. (std_params.options & GB_ALPHA_FIRST ? gs_image_alpha_first :
  450. std_params.options & GB_ALPHA_LAST ? gs_image_alpha_last :
  451. gs_image_alpha_none);
  452. code = composite_values(&dest, &source,
  453. dev->color_info.num_components, w, &cp);
  454. if (code < 0)
  455. break;
  456. if (std_params.data[0] == std_row) {
  457. /* Convert the row back to native representation. */
  458. /* (Otherwise, we had a direct pointer to device data.) */
  459. native_params.options =
  460. (GB_COLORS_NATIVE | GB_PACKING_CHUNKY | GB_RETURN_COPY |
  461. GB_OFFSET_0 | GB_RASTER_ALL | GB_ALIGN_STANDARD);
  462. native_params.data[0] = native_row;
  463. code = gx_get_bits_copy(target, 0, w, 1, &native_params,
  464. &std_params, std_row,
  465. 0 /* raster is irrelevant */ );
  466. if (code < 0)
  467. break;
  468. code = (*dev_proc(target, copy_color))
  469. (target, native_row, 0, 0 /* raster is irrelevant */ ,
  470. gx_no_bitmap_id, x, yi, w, 1);
  471. if (code < 0)
  472. break;
  473. }
  474. }
  475. out:gs_free_object(dev->memory, native_row, "dca_fill_rectangle(native)");
  476. gs_free_object(dev->memory, std_row, "dca_fill_rectangle(std)");
  477. return code;
  478. }
  479. private int
  480. dca_copy_mono(gx_device * dev, const byte * data,
  481. int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  482. gx_color_index zero, gx_color_index one)
  483. {
  484. /****** TEMPORARY ******/
  485. return gx_default_copy_mono(dev, data, dx, raster, id, x, y, w, h,
  486. zero, one);
  487. }
  488. private int
  489. dca_copy_color(gx_device * dev, const byte * data,
  490. int dx, int raster, gx_bitmap_id id,
  491. int x, int y, int w, int h)
  492. {
  493. /****** TEMPORARY ******/
  494. return gx_default_copy_color(dev, data, dx, raster, id, x, y, w, h);
  495. }
  496. private int
  497. dca_copy_alpha(gx_device * dev, const byte * data, int data_x,
  498. int raster, gx_bitmap_id id, int x, int y, int width, int height,
  499. gx_color_index color, int depth)
  500. {
  501. /****** TEMPORARY ******/
  502. return gx_default_copy_alpha(dev, data, data_x, raster, id, x, y,
  503. width, height, color, depth);
  504. }
  505. /*
  506. * Composite two arrays of (premultiplied) pixel values.
  507. * See gsdpnext.h for the specification.
  508. *
  509. * The current implementation is simple but inefficient. We'll speed it up
  510. * later if necessary.
  511. */
  512. int
  513. composite_values(const pixel_row_t * pdest, const const_pixel_row_t * psource,
  514. int values_per_pixel, uint num_pixels, const gs_composite_params_t * pcp)
  515. {
  516. int dest_bpv = pdest->bits_per_value;
  517. int source_bpv = psource->bits_per_value;
  518. /*
  519. * source_alpha_j gives the source component index for the alpha value,
  520. * if the source has alpha.
  521. */
  522. int source_alpha_j =
  523. (psource->alpha == gs_image_alpha_last ? values_per_pixel :
  524. psource->alpha == gs_image_alpha_first ? 0 : -1);
  525. /* dest_alpha_j does the same for the destination. */
  526. int dest_alpha_j =
  527. (pdest->alpha == gs_image_alpha_last ? values_per_pixel :
  528. pdest->alpha == gs_image_alpha_first ? 0 : -1);
  529. /* dest_vpp is the number of stored destination values. */
  530. int dest_vpp = values_per_pixel + (dest_alpha_j >= 0);
  531. /* source_vpp is the number of stored source values. */
  532. int source_vpp = values_per_pixel + (source_alpha_j >= 0);
  533. bool constant_colors = psource->data == 0;
  534. uint highlight_value = (1 << dest_bpv) - 1;
  535. sample_load_declare(sptr, sbit);
  536. sample_store_declare(dptr, dbit, dbyte);
  537. {
  538. uint xbit = pdest->initial_x * dest_bpv * dest_vpp;
  539. sample_store_setup(dbit, xbit & 7, dest_bpv);
  540. dptr = pdest->data + (xbit >> 3);
  541. }
  542. {
  543. uint xbit = psource->initial_x * source_bpv * source_vpp;
  544. sbit = xbit & 7;
  545. sptr = psource->data + (xbit >> 3);
  546. }
  547. {
  548. uint source_max = (1 << source_bpv) - 1;
  549. uint dest_max = (1 << dest_bpv) - 1;
  550. /*
  551. * We could save a little work by only setting up source_delta
  552. * and dest_delta if the operation is Dissolve.
  553. */
  554. float source_delta = pcp->delta * dest_max / source_max;
  555. float dest_delta = 1.0 - pcp->delta;
  556. uint source_alpha = pcp->source_alpha;
  557. uint dest_alpha = dest_max;
  558. #ifdef PREMULTIPLY_TOWARDS_WHITE
  559. uint source_bias = source_max - source_alpha;
  560. uint dest_bias = 0;
  561. uint result_bias = 0;
  562. #endif
  563. uint x;
  564. if (!pdest->alpha) {
  565. uint mask =
  566. (psource->alpha || source_alpha != source_max ?
  567. alpha_out_S_notD : alpha_out_notS_notD);
  568. if ((mask >> pcp->cop) & 1) {
  569. /*
  570. * The operation could produce non-unity alpha values, but
  571. * the destination can't store them. Return an error.
  572. */
  573. return_error(gs_error_rangecheck);
  574. }
  575. }
  576. /* Preload the output byte buffer if necessary. */
  577. sample_store_preload(dbyte, dptr, dbit, dest_bpv);
  578. for (x = 0; x < num_pixels; ++x) {
  579. int j;
  580. uint result_alpha = dest_alpha;
  581. /* get_value does not increment the source pointer. */
  582. #define get_value(v, ptr, bit, bpv, vmax)\
  583. sample_load16(v, ptr, bit, bpv)
  584. /* put_value increments the destination pointer. */
  585. #define put_value(v, ptr, bit, bpv, bbyte)\
  586. sample_store_next16(v, ptr, bit, bpv, bbyte)
  587. #define advance(ptr, bit, bpv)\
  588. sample_next(ptr, bit, bpv)
  589. /* Get destination alpha value. */
  590. if (dest_alpha_j >= 0) {
  591. int dabit = dbit + dest_bpv * dest_alpha_j;
  592. const byte *daptr = dptr + (dabit >> 3);
  593. get_value(dest_alpha, daptr, dabit & 7, dest_bpv, dest_max);
  594. #ifdef PREMULTIPLY_TOWARDS_WHITE
  595. dest_bias = dest_max - dest_alpha;
  596. #endif
  597. }
  598. /* Get source alpha value. */
  599. if (source_alpha_j >= 0) {
  600. int sabit = sbit;
  601. const byte *saptr = sptr;
  602. if (source_alpha_j == 0)
  603. advance(sptr, sbit, source_bpv);
  604. else
  605. advance(saptr, sabit, source_bpv * source_alpha_j);
  606. get_value(source_alpha, saptr, sabit, source_bpv, source_max);
  607. #ifdef PREMULTIPLY_TOWARDS_WHITE
  608. source_bias = source_max - source_alpha;
  609. #endif
  610. }
  611. /*
  612. * We are always multiplying a dest value by a source value to compute a
  613. * dest value, so the denominator is always source_max. (Dissolve is the
  614. * one exception.)
  615. */
  616. #define fr(v, a) ((v) * (a) / source_max)
  617. #define nfr(v, a, maxv) ((v) * (maxv - (a)) / source_max)
  618. /*
  619. * Iterate over the components of a single pixel.
  620. * j = 0 for alpha, 1 .. values_per_pixel for color
  621. * components, regardless of the actual storage order;
  622. * we arrange things so that sptr/sbit and dptr/dbit
  623. * always point to the right place.
  624. */
  625. for (j = 0; j <= values_per_pixel; ++j) {
  626. uint dest_v, source_v, result;
  627. #define set_clamped(r, v)\
  628. BEGIN if ( (r = (v)) > dest_max ) r = dest_max; END
  629. if (j == 0) {
  630. source_v = source_alpha;
  631. dest_v = dest_alpha;
  632. } else {
  633. if (constant_colors)
  634. source_v = pcp->source_values[j - 1];
  635. else {
  636. get_value(source_v, sptr, sbit, source_bpv, source_max);
  637. advance(sptr, sbit, source_bpv);
  638. }
  639. get_value(dest_v, dptr, dbit, dest_bpv, dest_max);
  640. #ifdef PREMULTIPLY_TOWARDS_WHITE
  641. source_v -= source_bias;
  642. dest_v -= dest_bias;
  643. #endif
  644. }
  645. switch (pcp->cop) {
  646. case composite_Clear:
  647. /*
  648. * The NeXT documentation doesn't say this, but the CLEAR
  649. * operation sets not only alpha but also all the color
  650. * values to 0.
  651. */
  652. result = 0;
  653. break;
  654. case composite_Copy:
  655. result = source_v;
  656. break;
  657. case composite_PlusD:
  658. /*
  659. * This is the only case where we have to worry about
  660. * clamping a possibly negative result.
  661. */
  662. result = source_v + dest_v;
  663. result = (result < dest_max ? 0 : result - dest_max);
  664. break;
  665. case composite_PlusL:
  666. set_clamped(result, source_v + dest_v);
  667. break;
  668. case composite_Sover:
  669. set_clamped(result, source_v + nfr(dest_v, source_alpha, source_max));
  670. break;
  671. case composite_Dover:
  672. set_clamped(result, nfr(source_v, dest_alpha, dest_max) + dest_v);
  673. break;
  674. case composite_Sin:
  675. result = fr(source_v, dest_alpha);
  676. break;
  677. case composite_Din:
  678. result = fr(dest_v, source_alpha);
  679. break;
  680. case composite_Sout:
  681. result = nfr(source_v, dest_alpha, dest_max);
  682. break;
  683. case composite_Dout:
  684. result = nfr(dest_v, source_alpha, source_max);
  685. break;
  686. case composite_Satop:
  687. set_clamped(result, fr(source_v, dest_alpha) +
  688. nfr(dest_v, source_alpha, source_max));
  689. break;
  690. case composite_Datop:
  691. set_clamped(result, nfr(source_v, dest_alpha, dest_max) +
  692. fr(dest_v, source_alpha));
  693. break;
  694. case composite_Xor:
  695. set_clamped(result, nfr(source_v, dest_alpha, dest_max) +
  696. nfr(dest_v, source_alpha, source_max));
  697. break;
  698. case composite_Highlight:
  699. /*
  700. * Bizarre but true: this operation converts white and
  701. * light gray into each other, and leaves all other values
  702. * unchanged. We only implement it properly for gray-scale
  703. * devices.
  704. */
  705. if (j != 0 && !((source_v ^ highlight_value) & ~1))
  706. result = source_v ^ 1;
  707. else
  708. result = source_v;
  709. break;
  710. case composite_Dissolve:
  711. /*
  712. * In this case, and only this case, we need to worry about
  713. * source and dest having different bpv values. For the
  714. * moment, we wimp out and do everything in floating point.
  715. */
  716. result = (uint) (source_v * source_delta + dest_v * dest_delta);
  717. break;
  718. default:
  719. return_error(gs_error_rangecheck);
  720. }
  721. /*
  722. * Store the result. We don't have to worry about
  723. * destinations that don't store alpha, because we don't
  724. * even compute an alpha value in that case.
  725. */
  726. #ifdef PREMULTIPLY_TOWARDS_WHITE
  727. if (j == 0) {
  728. result_alpha = result;
  729. result_bias = dest_max - result_alpha;
  730. if (dest_alpha_j != 0)
  731. continue;
  732. } else {
  733. result += result_bias;
  734. }
  735. #else
  736. if (j == 0 && dest_alpha_j != 0) {
  737. result_alpha = result;
  738. continue;
  739. }
  740. #endif
  741. put_value(result, dptr, dbit, dest_bpv, dbyte);
  742. }
  743. /* Skip a trailing source alpha value. */
  744. if (source_alpha_j > 0)
  745. advance(sptr, sbit, source_bpv);
  746. /* Store a trailing destination alpha value. */
  747. if (dest_alpha_j > 0)
  748. put_value(result_alpha, dptr, dbit, dest_bpv, dbyte);
  749. #undef get_value
  750. #undef put_value
  751. #undef advance
  752. }
  753. /* Store any partial output byte. */
  754. sample_store_flush(dptr, dbit, dest_bpv, dbyte);
  755. }
  756. return 0;
  757. }