gxshade4.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright (C) 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gxshade4.c,v 1.30 2005/04/19 12:22:08 igor Exp $ */
  14. /* Rendering for Gouraud triangle shadings */
  15. #include "math_.h"
  16. #include "memory_.h"
  17. #include "gx.h"
  18. #include "gserrors.h"
  19. #include "gsmatrix.h" /* for gscoord.h */
  20. #include "gscoord.h"
  21. #include "gsptype2.h"
  22. #include "gxcspace.h"
  23. #include "gxdcolor.h"
  24. #include "gxdevcli.h"
  25. #include "gxistate.h"
  26. #include "gxpath.h"
  27. #include "gxshade.h"
  28. #include "gxshade4.h"
  29. #include "vdtrace.h"
  30. #define VD_TRACE_TRIANGLE_PATCH 1
  31. /* Initialize the fill state for triangle shading. */
  32. int
  33. mesh_init_fill_state(mesh_fill_state_t * pfs, const gs_shading_mesh_t * psh,
  34. const gs_fixed_rect * rect_clip, gx_device * dev,
  35. gs_imager_state * pis)
  36. {
  37. shade_init_fill_state((shading_fill_state_t *) pfs,
  38. (const gs_shading_t *)psh, dev, pis);
  39. pfs->pshm = psh;
  40. pfs->rect = *rect_clip;
  41. return 0;
  42. }
  43. /* ---------------- Gouraud triangle shadings ---------------- */
  44. private int
  45. Gt_next_vertex(const gs_shading_mesh_t * psh, shade_coord_stream_t * cs,
  46. shading_vertex_t * vertex)
  47. {
  48. int code = shade_next_vertex(cs, vertex);
  49. if (code >= 0 && psh->params.Function) {
  50. vertex->c.t[0] = vertex->c.cc.paint.values[0];
  51. vertex->c.t[1] = 0;
  52. /* Decode the color with the function. */
  53. code = gs_function_evaluate(psh->params.Function, vertex->c.t,
  54. vertex->c.cc.paint.values);
  55. }
  56. return code;
  57. }
  58. inline private int
  59. Gt_fill_triangle(mesh_fill_state_t * pfs, const shading_vertex_t * va,
  60. const shading_vertex_t * vb, const shading_vertex_t * vc)
  61. {
  62. patch_fill_state_t pfs1;
  63. int code = 0;
  64. memcpy(&pfs1, (shading_fill_state_t *)pfs, sizeof(shading_fill_state_t));
  65. pfs1.Function = pfs->pshm->params.Function;
  66. pfs1.rect = pfs->rect;
  67. code = init_patch_fill_state(&pfs1);
  68. if (code < 0)
  69. return code;
  70. if (INTERPATCH_PADDING) {
  71. code = mesh_padding(&pfs1, &va->p, &vb->p, &va->c, &vb->c);
  72. if (code >= 0)
  73. code = mesh_padding(&pfs1, &vb->p, &vc->p, &vb->c, &vc->c);
  74. if (code >= 0)
  75. code = mesh_padding(&pfs1, &vc->p, &va->p, &vc->c, &va->c);
  76. }
  77. if (code >= 0)
  78. code = mesh_triangle(&pfs1, va, vb, vc);
  79. term_patch_fill_state(&pfs1);
  80. return code;
  81. }
  82. int
  83. gs_shading_FfGt_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
  84. const gs_fixed_rect * rect_clip,
  85. gx_device * dev, gs_imager_state * pis)
  86. {
  87. const gs_shading_FfGt_t * const psh = (const gs_shading_FfGt_t *)psh0;
  88. mesh_fill_state_t state;
  89. shade_coord_stream_t cs;
  90. int num_bits = psh->params.BitsPerFlag;
  91. int flag;
  92. shading_vertex_t va, vb, vc;
  93. int code;
  94. if (VD_TRACE_TRIANGLE_PATCH && vd_allowed('s')) {
  95. vd_get_dc('s');
  96. vd_set_shift(0, 0);
  97. vd_set_scale(0.01);
  98. vd_set_origin(0, 0);
  99. }
  100. code = mesh_init_fill_state(&state, (const gs_shading_mesh_t *)psh, rect_clip,
  101. dev, pis);
  102. if (code < 0)
  103. return code;
  104. shade_next_init(&cs, (const gs_shading_mesh_params_t *)&psh->params,
  105. pis);
  106. while ((flag = shade_next_flag(&cs, num_bits)) >= 0) {
  107. switch (flag) {
  108. default:
  109. return_error(gs_error_rangecheck);
  110. case 0:
  111. if ((code = Gt_next_vertex(state.pshm, &cs, &va)) < 0 ||
  112. (code = shade_next_flag(&cs, num_bits)) < 0 ||
  113. (code = Gt_next_vertex(state.pshm, &cs, &vb)) < 0 ||
  114. (code = shade_next_flag(&cs, num_bits)) < 0
  115. )
  116. return code;
  117. goto v2;
  118. case 1:
  119. va = vb;
  120. case 2:
  121. vb = vc;
  122. v2: if ((code = Gt_next_vertex(state.pshm, &cs, &vc)) < 0)
  123. return code;
  124. if ((code = Gt_fill_triangle(&state, &va, &vb, &vc)) < 0)
  125. return code;
  126. }
  127. }
  128. if (VD_TRACE_TRIANGLE_PATCH && vd_allowed('s'))
  129. vd_release_dc;
  130. if (!cs.is_eod(&cs))
  131. return_error(gs_error_rangecheck);
  132. return 0;
  133. }
  134. int
  135. gs_shading_LfGt_fill_rectangle(const gs_shading_t * psh0, const gs_rect * rect,
  136. const gs_fixed_rect * rect_clip,
  137. gx_device * dev, gs_imager_state * pis)
  138. {
  139. const gs_shading_LfGt_t * const psh = (const gs_shading_LfGt_t *)psh0;
  140. mesh_fill_state_t state;
  141. shade_coord_stream_t cs;
  142. shading_vertex_t *vertex;
  143. shading_vertex_t next;
  144. int per_row = psh->params.VerticesPerRow;
  145. int i, code;
  146. if (VD_TRACE_TRIANGLE_PATCH && vd_allowed('s')) {
  147. vd_get_dc('s');
  148. vd_set_shift(0, 0);
  149. vd_set_scale(0.01);
  150. vd_set_origin(0, 0);
  151. }
  152. code = mesh_init_fill_state(&state, (const gs_shading_mesh_t *)psh, rect_clip,
  153. dev, pis);
  154. if (code < 0)
  155. return code;
  156. shade_next_init(&cs, (const gs_shading_mesh_params_t *)&psh->params,
  157. pis);
  158. vertex = (shading_vertex_t *)
  159. gs_alloc_byte_array(pis->memory, per_row, sizeof(*vertex),
  160. "gs_shading_LfGt_render");
  161. if (vertex == 0)
  162. return_error(gs_error_VMerror);
  163. for (i = 0; i < per_row; ++i)
  164. if ((code = Gt_next_vertex(state.pshm, &cs, &vertex[i])) < 0)
  165. goto out;
  166. while (!seofp(cs.s)) {
  167. code = Gt_next_vertex(state.pshm, &cs, &next);
  168. if (code < 0)
  169. goto out;
  170. for (i = 1; i < per_row; ++i) {
  171. code = Gt_fill_triangle(&state, &vertex[i - 1], &vertex[i], &next);
  172. if (code < 0)
  173. goto out;
  174. vertex[i - 1] = next;
  175. code = Gt_next_vertex(state.pshm, &cs, &next);
  176. if (code < 0)
  177. goto out;
  178. code = Gt_fill_triangle(&state, &vertex[i], &vertex[i - 1], &next);
  179. if (code < 0)
  180. goto out;
  181. }
  182. vertex[per_row - 1] = next;
  183. }
  184. out:
  185. if (VD_TRACE_TRIANGLE_PATCH && vd_allowed('s'))
  186. vd_release_dc;
  187. gs_free_object(pis->memory, vertex, "gs_shading_LfGt_render");
  188. return code;
  189. }