gxpcmap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /* Copyright (C) 1993, 2000 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: gxpcmap.c,v 1.3 2000/09/19 19:00:39 lpd Exp $ */
  16. /* Pattern color mapping for Ghostscript library */
  17. #include "math_.h"
  18. #include "memory_.h"
  19. #include "gx.h"
  20. #include "gserrors.h"
  21. #include "gsstruct.h"
  22. #include "gsutil.h" /* for gs_next_ids */
  23. #include "gxfixed.h"
  24. #include "gxmatrix.h"
  25. #include "gxcspace.h" /* for gscolor2.h */
  26. #include "gxcolor2.h"
  27. #include "gxdcolor.h"
  28. #include "gxdevice.h"
  29. #include "gxdevmem.h"
  30. #include "gxpcolor.h"
  31. #include "gzstate.h"
  32. /* Define the default size of the Pattern cache. */
  33. #define max_cached_patterns_LARGE 50
  34. #define max_pattern_bits_LARGE 100000
  35. #define max_cached_patterns_SMALL 5
  36. #define max_pattern_bits_SMALL 1000
  37. uint
  38. gx_pat_cache_default_tiles(void)
  39. {
  40. #if arch_small_memory
  41. return max_cached_patterns_SMALL;
  42. #else
  43. return (gs_debug_c('.') ? max_cached_patterns_SMALL :
  44. max_cached_patterns_LARGE);
  45. #endif
  46. }
  47. ulong
  48. gx_pat_cache_default_bits(void)
  49. {
  50. #if arch_small_memory
  51. return max_pattern_bits_SMALL;
  52. #else
  53. return (gs_debug_c('.') ? max_pattern_bits_SMALL :
  54. max_pattern_bits_LARGE);
  55. #endif
  56. }
  57. /* Define the structures for Pattern rendering and caching. */
  58. private_st_color_tile();
  59. private_st_color_tile_element();
  60. private_st_pattern_cache();
  61. private_st_device_pattern_accum();
  62. /* ------ Pattern rendering ------ */
  63. /* Device procedures */
  64. private dev_proc_open_device(pattern_accum_open);
  65. private dev_proc_close_device(pattern_accum_close);
  66. private dev_proc_fill_rectangle(pattern_accum_fill_rectangle);
  67. private dev_proc_copy_mono(pattern_accum_copy_mono);
  68. private dev_proc_copy_color(pattern_accum_copy_color);
  69. private dev_proc_get_bits_rectangle(pattern_accum_get_bits_rectangle);
  70. /* The device descriptor */
  71. private const gx_device_pattern_accum gs_pattern_accum_device =
  72. {std_device_std_body_open(gx_device_pattern_accum, 0,
  73. "pattern accumulator",
  74. 0, 0, 72, 72),
  75. {
  76. /* NOTE: all drawing procedures must be defaulted, not forwarded. */
  77. pattern_accum_open,
  78. NULL,
  79. NULL,
  80. NULL,
  81. pattern_accum_close,
  82. NULL,
  83. NULL,
  84. pattern_accum_fill_rectangle,
  85. gx_default_tile_rectangle,
  86. pattern_accum_copy_mono,
  87. pattern_accum_copy_color,
  88. NULL,
  89. gx_default_get_bits,
  90. NULL,
  91. NULL,
  92. NULL,
  93. NULL,
  94. NULL,
  95. NULL,
  96. NULL,
  97. NULL,
  98. gx_default_copy_alpha,
  99. NULL,
  100. gx_default_copy_rop,
  101. gx_default_fill_path,
  102. gx_default_stroke_path,
  103. gx_default_fill_mask,
  104. gx_default_fill_trapezoid,
  105. gx_default_fill_parallelogram,
  106. gx_default_fill_triangle,
  107. gx_default_draw_thin_line,
  108. gx_default_begin_image,
  109. gx_default_image_data,
  110. gx_default_end_image,
  111. gx_default_strip_tile_rectangle,
  112. gx_default_strip_copy_rop,
  113. gx_get_largest_clipping_box,
  114. gx_default_begin_typed_image,
  115. pattern_accum_get_bits_rectangle,
  116. NULL,
  117. NULL,
  118. NULL,
  119. gx_default_text_begin,
  120. gx_default_finish_copydevice
  121. },
  122. 0, /* target */
  123. 0, 0, 0, 0 /* bitmap_memory, bits, mask, instance */
  124. };
  125. /* Allocate a pattern accumulator, with an initial refct of 0. */
  126. gx_device_pattern_accum *
  127. gx_pattern_accum_alloc(gs_memory_t * mem, client_name_t cname)
  128. {
  129. gx_device_pattern_accum *adev =
  130. gs_alloc_struct(mem, gx_device_pattern_accum,
  131. &st_device_pattern_accum, cname);
  132. if (adev == 0)
  133. return 0;
  134. gx_device_init((gx_device *)adev,
  135. (const gx_device *)&gs_pattern_accum_device,
  136. mem, true);
  137. gx_device_forward_fill_in_procs((gx_device_forward *)adev);
  138. return adev;
  139. }
  140. /*
  141. * Initialize a pattern accumulator.
  142. * Client must already have set instance and bitmap_memory.
  143. *
  144. * Note that mask and bits accumulators are only created if necessary.
  145. */
  146. private int
  147. pattern_accum_open(gx_device * dev)
  148. {
  149. gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  150. const gs_pattern1_instance_t *pinst = padev->instance;
  151. gs_memory_t *mem = padev->bitmap_memory;
  152. gx_device_memory *mask = 0;
  153. gx_device_memory *bits = 0;
  154. /*
  155. * The client should preset the target, because the device for which the
  156. * pattern is being rendered may not (in general, will not) be the same
  157. * as the one that was current when the pattern was instantiated.
  158. */
  159. gx_device *target =
  160. (padev->target == 0 ? gs_currentdevice(pinst->saved) :
  161. padev->target);
  162. int width = pinst->size.x;
  163. int height = pinst->size.y;
  164. int code = 0;
  165. bool mask_open = false;
  166. /*
  167. * C's bizarre coercion rules force us to copy HWResolution in pieces
  168. * rather than using a single assignment.
  169. */
  170. #define PDSET(dev)\
  171. ((dev)->width = width, (dev)->height = height,\
  172. /*(dev)->HWResolution = target->HWResolution*/\
  173. (dev)->HWResolution[0] = target->HWResolution[0],\
  174. (dev)->HWResolution[1] = target->HWResolution[1])
  175. PDSET(padev);
  176. padev->color_info = target->color_info;
  177. if (pinst->uses_mask) {
  178. mask = gs_alloc_struct( mem,
  179. gx_device_memory,
  180. &st_device_memory,
  181. "pattern_accum_open(mask)"
  182. );
  183. if (mask == 0)
  184. return_error(gs_error_VMerror);
  185. gs_make_mem_mono_device(mask, mem, 0);
  186. PDSET(mask);
  187. mask->bitmap_memory = mem;
  188. mask->base = 0;
  189. code = (*dev_proc(mask, open_device)) ((gx_device *) mask);
  190. if (code >= 0) {
  191. mask_open = true;
  192. memset(mask->base, 0, mask->raster * mask->height);
  193. }
  194. }
  195. if (code >= 0) {
  196. switch (pinst->template.PaintType) {
  197. case 2: /* uncolored */
  198. gx_device_set_target((gx_device_forward *)padev, target);
  199. break;
  200. case 1: /* colored */
  201. bits = gs_alloc_struct(mem, gx_device_memory,
  202. &st_device_memory,
  203. "pattern_accum_open(bits)");
  204. if (bits == 0)
  205. code = gs_note_error(gs_error_VMerror);
  206. else {
  207. gs_make_mem_device(bits,
  208. gdev_mem_device_for_bits(target->color_info.depth),
  209. mem, -1, target);
  210. PDSET(bits);
  211. #undef PDSET
  212. bits->color_info = target->color_info;
  213. bits->bitmap_memory = mem;
  214. code = (*dev_proc(bits, open_device)) ((gx_device *) bits);
  215. gx_device_set_target((gx_device_forward *)padev,
  216. (gx_device *)bits);
  217. }
  218. }
  219. }
  220. if (code < 0) {
  221. if (bits != 0)
  222. gs_free_object(mem, bits, "pattern_accum_open(bits)");
  223. if (mask != 0) {
  224. if (mask_open)
  225. (*dev_proc(mask, close_device)) ((gx_device *) mask);
  226. gs_free_object(mem, mask, "pattern_accum_open(mask)");
  227. }
  228. return code;
  229. }
  230. padev->mask = mask;
  231. padev->bits = bits;
  232. /* Retain the device, so it will survive anomalous grestores. */
  233. gx_device_retain(dev, true);
  234. return code;
  235. }
  236. /* Close an accumulator and free the bits. */
  237. private int
  238. pattern_accum_close(gx_device * dev)
  239. {
  240. gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  241. gs_memory_t *mem = padev->bitmap_memory;
  242. /*
  243. * If bits != 0, it is the target of the device; reference counting
  244. * will close and free it.
  245. */
  246. gx_device_set_target((gx_device_forward *)padev, NULL);
  247. padev->bits = 0;
  248. if (padev->mask != 0) {
  249. (*dev_proc(padev->mask, close_device)) ((gx_device *) padev->mask);
  250. gs_free_object(mem, padev->mask, "pattern_accum_close(mask)");
  251. padev->mask = 0;
  252. }
  253. /* Un-retain the device now, so reference counting will free it. */
  254. gx_device_retain(dev, false);
  255. return 0;
  256. }
  257. /* Fill a rectangle */
  258. private int
  259. pattern_accum_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  260. gx_color_index color)
  261. {
  262. gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  263. if (padev->bits)
  264. (*dev_proc(padev->target, fill_rectangle))
  265. (padev->target, x, y, w, h, color);
  266. if (padev->mask)
  267. return (*dev_proc(padev->mask, fill_rectangle))
  268. ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
  269. else
  270. return 0;
  271. }
  272. /* Copy a monochrome bitmap. */
  273. private int
  274. pattern_accum_copy_mono(gx_device * dev, const byte * data, int data_x,
  275. int raster, gx_bitmap_id id, int x, int y, int w, int h,
  276. gx_color_index color0, gx_color_index color1)
  277. {
  278. gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  279. if (padev->bits)
  280. (*dev_proc(padev->target, copy_mono))
  281. (padev->target, data, data_x, raster, id, x, y, w, h,
  282. color0, color1);
  283. if (padev->mask) {
  284. if (color0 != gx_no_color_index)
  285. color0 = 1;
  286. if (color1 != gx_no_color_index)
  287. color1 = 1;
  288. if (color0 == 1 && color1 == 1)
  289. return (*dev_proc(padev->mask, fill_rectangle))
  290. ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
  291. else
  292. return (*dev_proc(padev->mask, copy_mono))
  293. ((gx_device *) padev->mask, data, data_x, raster, id, x, y, w, h,
  294. color0, color1);
  295. } else
  296. return 0;
  297. }
  298. /* Copy a color bitmap. */
  299. private int
  300. pattern_accum_copy_color(gx_device * dev, const byte * data, int data_x,
  301. int raster, gx_bitmap_id id, int x, int y, int w, int h)
  302. {
  303. gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  304. if (padev->bits)
  305. (*dev_proc(padev->target, copy_color))
  306. (padev->target, data, data_x, raster, id, x, y, w, h);
  307. if (padev->mask)
  308. return (*dev_proc(padev->mask, fill_rectangle))
  309. ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
  310. else
  311. return 0;
  312. }
  313. /* Read back a rectangle of bits. */
  314. /****** SHOULD USE MASK TO DEFINE UNREAD AREA *****/
  315. private int
  316. pattern_accum_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
  317. gs_get_bits_params_t * params, gs_int_rect ** unread)
  318. {
  319. gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  320. if (padev->bits)
  321. return (*dev_proc(padev->target, get_bits_rectangle))
  322. (padev->target, prect, params, unread);
  323. return_error(gs_error_Fatal); /* can't happen */
  324. }
  325. /* ------ Color space implementation ------ */
  326. /* Free all entries in a pattern cache. */
  327. private bool
  328. pattern_cache_choose_all(gx_color_tile * ctile, void *proc_data)
  329. {
  330. return true;
  331. }
  332. private void
  333. pattern_cache_free_all(gx_pattern_cache * pcache)
  334. {
  335. gx_pattern_cache_winnow(pcache, pattern_cache_choose_all, NULL);
  336. }
  337. /* Allocate a Pattern cache. */
  338. gx_pattern_cache *
  339. gx_pattern_alloc_cache(gs_memory_t * mem, uint num_tiles, ulong max_bits)
  340. {
  341. gx_pattern_cache *pcache =
  342. gs_alloc_struct(mem, gx_pattern_cache, &st_pattern_cache,
  343. "pattern_cache_alloc(struct)");
  344. gx_color_tile *tiles =
  345. gs_alloc_struct_array(mem, num_tiles, gx_color_tile,
  346. &st_color_tile_element,
  347. "pattern_cache_alloc(tiles)");
  348. uint i;
  349. if (pcache == 0 || tiles == 0) {
  350. gs_free_object(mem, tiles, "pattern_cache_alloc(tiles)");
  351. gs_free_object(mem, pcache, "pattern_cache_alloc(struct)");
  352. return 0;
  353. }
  354. pcache->memory = mem;
  355. pcache->tiles = tiles;
  356. pcache->num_tiles = num_tiles;
  357. pcache->tiles_used = 0;
  358. pcache->next = 0;
  359. pcache->bits_used = 0;
  360. pcache->max_bits = max_bits;
  361. pcache->free_all = pattern_cache_free_all;
  362. for (i = 0; i < num_tiles; tiles++, i++) {
  363. tiles->id = gx_no_bitmap_id;
  364. /* Clear the pointers to pacify the GC. */
  365. uid_set_invalid(&tiles->uid);
  366. tiles->tbits.data = 0;
  367. tiles->tmask.data = 0;
  368. tiles->index = i;
  369. }
  370. return pcache;
  371. }
  372. /* Ensure that an imager has a Pattern cache. */
  373. private int
  374. ensure_pattern_cache(gs_imager_state * pis)
  375. {
  376. if (pis->pattern_cache == 0) {
  377. gx_pattern_cache *pcache =
  378. gx_pattern_alloc_cache(pis->memory,
  379. gx_pat_cache_default_tiles(),
  380. gx_pat_cache_default_bits());
  381. if (pcache == 0)
  382. return_error(gs_error_VMerror);
  383. pis->pattern_cache = pcache;
  384. }
  385. return 0;
  386. }
  387. /* Get and set the Pattern cache in a gstate. */
  388. gx_pattern_cache *
  389. gstate_pattern_cache(gs_state * pgs)
  390. {
  391. return pgs->pattern_cache;
  392. }
  393. void
  394. gstate_set_pattern_cache(gs_state * pgs, gx_pattern_cache * pcache)
  395. {
  396. pgs->pattern_cache = pcache;
  397. }
  398. /* Free a Pattern cache entry. */
  399. private void
  400. gx_pattern_cache_free_entry(gx_pattern_cache * pcache, gx_color_tile * ctile)
  401. {
  402. if (ctile->id != gx_no_bitmap_id) {
  403. gs_memory_t *mem = pcache->memory;
  404. gx_device_memory mdev;
  405. /*
  406. * We must initialize the memory device properly, even though
  407. * we aren't using it for drawing.
  408. */
  409. gs_make_mem_mono_device(&mdev, mem, NULL);
  410. if (ctile->tmask.data != 0) {
  411. mdev.width = ctile->tmask.size.x;
  412. mdev.height = ctile->tmask.size.y;
  413. /*mdev.color_info.depth = 1;*/
  414. pcache->bits_used -= gdev_mem_bitmap_size(&mdev);
  415. gs_free_object(mem, ctile->tmask.data,
  416. "free_pattern_cache_entry(mask data)");
  417. ctile->tmask.data = 0; /* for GC */
  418. }
  419. if (ctile->tbits.data != 0) {
  420. mdev.width = ctile->tbits.size.x;
  421. mdev.height = ctile->tbits.size.y;
  422. mdev.color_info.depth = ctile->depth;
  423. pcache->bits_used -= gdev_mem_bitmap_size(&mdev);
  424. gs_free_object(mem, ctile->tbits.data,
  425. "free_pattern_cache_entry(bits data)");
  426. ctile->tbits.data = 0; /* for GC */
  427. }
  428. ctile->id = gx_no_bitmap_id;
  429. pcache->tiles_used--;
  430. }
  431. }
  432. /*
  433. * Add a Pattern cache entry. This is exported for the interpreter.
  434. * Note that this does not free any of the data in the accumulator
  435. * device, but it may zero out the bitmap_memory pointers to prevent
  436. * the accumulated bitmaps from being freed when the device is closed.
  437. */
  438. private void make_bitmap(P3(gx_strip_bitmap *, const gx_device_memory *, gx_bitmap_id));
  439. int
  440. gx_pattern_cache_add_entry(gs_imager_state * pis,
  441. gx_device_pattern_accum * padev, gx_color_tile ** pctile)
  442. {
  443. gx_device_memory *mbits = padev->bits;
  444. gx_device_memory *mmask = padev->mask;
  445. const gs_pattern1_instance_t *pinst = padev->instance;
  446. gx_pattern_cache *pcache;
  447. ulong used = 0;
  448. gx_bitmap_id id = pinst->id;
  449. gx_color_tile *ctile;
  450. int code = ensure_pattern_cache(pis);
  451. if (code < 0)
  452. return code;
  453. pcache = pis->pattern_cache;
  454. /*
  455. * Check whether the pattern completely fills its box.
  456. * If so, we can avoid the expensive masking operations
  457. * when using the pattern.
  458. */
  459. if (mmask != 0) {
  460. int y;
  461. for (y = 0; y < mmask->height; y++) {
  462. const byte *row = scan_line_base(mmask, y);
  463. int w;
  464. for (w = mmask->width; w > 8; w -= 8)
  465. if (*row++ != 0xff)
  466. goto keep;
  467. if ((*row | (0xff >> w)) != 0xff)
  468. goto keep;
  469. }
  470. /* We don't need a mask. */
  471. mmask = 0;
  472. keep:;
  473. }
  474. if (mbits != 0)
  475. used += gdev_mem_bitmap_size(mbits);
  476. if (mmask != 0)
  477. used += gdev_mem_bitmap_size(mmask);
  478. ctile = &pcache->tiles[id % pcache->num_tiles];
  479. gx_pattern_cache_free_entry(pcache, ctile);
  480. while (pcache->bits_used + used > pcache->max_bits &&
  481. pcache->bits_used != 0 /* allow 1 oversized entry (?) */
  482. ) {
  483. pcache->next = (pcache->next + 1) % pcache->num_tiles;
  484. gx_pattern_cache_free_entry(pcache, &pcache->tiles[pcache->next]);
  485. }
  486. ctile->id = id;
  487. ctile->depth = padev->color_info.depth;
  488. ctile->uid = pinst->template.uid;
  489. ctile->tiling_type = pinst->template.TilingType;
  490. ctile->step_matrix = pinst->step_matrix;
  491. ctile->bbox = pinst->bbox;
  492. ctile->is_simple = pinst->is_simple;
  493. if (mbits != 0) {
  494. make_bitmap(&ctile->tbits, mbits, gs_next_ids(1));
  495. mbits->bitmap_memory = 0; /* don't free the bits */
  496. } else
  497. ctile->tbits.data = 0;
  498. if (mmask != 0) {
  499. make_bitmap(&ctile->tmask, mmask, id);
  500. mmask->bitmap_memory = 0; /* don't free the bits */
  501. } else
  502. ctile->tmask.data = 0;
  503. pcache->bits_used += used;
  504. pcache->tiles_used++;
  505. *pctile = ctile;
  506. return 0;
  507. }
  508. private void
  509. make_bitmap(register gx_strip_bitmap * pbm, const gx_device_memory * mdev,
  510. gx_bitmap_id id)
  511. {
  512. pbm->data = mdev->base;
  513. pbm->raster = mdev->raster;
  514. pbm->rep_width = pbm->size.x = mdev->width;
  515. pbm->rep_height = pbm->size.y = mdev->height;
  516. pbm->id = id;
  517. pbm->rep_shift = pbm->shift = 0;
  518. }
  519. /* Purge selected entries from the pattern cache. */
  520. void
  521. gx_pattern_cache_winnow(gx_pattern_cache * pcache,
  522. bool(*proc) (P2(gx_color_tile * ctile, void *proc_data)), void *proc_data)
  523. {
  524. uint i;
  525. if (pcache == 0) /* no cache created yet */
  526. return;
  527. for (i = 0; i < pcache->num_tiles; ++i) {
  528. gx_color_tile *ctile = &pcache->tiles[i];
  529. if (ctile->id != gx_no_bitmap_id && (*proc) (ctile, proc_data))
  530. gx_pattern_cache_free_entry(pcache, ctile);
  531. }
  532. }
  533. /* Reload a (non-null) Pattern color into the cache. */
  534. /* *pdc is already set, except for colors.pattern.p_tile and mask.m_tile. */
  535. int
  536. gx_pattern_load(gx_device_color * pdc, const gs_imager_state * pis,
  537. gx_device * dev, gs_color_select_t select)
  538. {
  539. gx_device_pattern_accum *adev;
  540. gs_pattern1_instance_t *pinst =
  541. (gs_pattern1_instance_t *)pdc->ccolor.pattern;
  542. gs_state *saved;
  543. gx_color_tile *ctile;
  544. gs_memory_t *mem = pis->memory;
  545. int code;
  546. if (gx_pattern_cache_lookup(pdc, pis, dev, select))
  547. return 0;
  548. /* We REALLY don't like the following cast.... */
  549. code = ensure_pattern_cache((gs_imager_state *) pis);
  550. if (code < 0)
  551. return code;
  552. /*
  553. * Note that adev is an internal device, so it will be freed when the
  554. * last reference to it from a graphics state is deleted.
  555. */
  556. adev = gx_pattern_accum_alloc(mem, "gx_pattern_load");
  557. if (adev == 0)
  558. return_error(gs_error_VMerror);
  559. gx_device_set_target((gx_device_forward *)adev, dev);
  560. adev->instance = pinst;
  561. adev->bitmap_memory = mem;
  562. code = dev_proc(adev, open_device)((gx_device *)adev);
  563. if (code < 0)
  564. goto fail;
  565. saved = gs_gstate(pinst->saved);
  566. if (saved == 0) {
  567. code = gs_note_error(gs_error_VMerror);
  568. goto fail;
  569. }
  570. if (saved->pattern_cache == 0)
  571. saved->pattern_cache = pis->pattern_cache;
  572. gs_setdevice_no_init(saved, (gx_device *)adev);
  573. code = (*pinst->template.PaintProc)(&pdc->ccolor, saved);
  574. if (code < 0) {
  575. dev_proc(adev, close_device)((gx_device *)adev);
  576. /* Freeing the state will free the device. */
  577. gs_state_free(saved);
  578. return code;
  579. }
  580. /* We REALLY don't like the following cast.... */
  581. code = gx_pattern_cache_add_entry((gs_imager_state *)pis, adev, &ctile);
  582. if (code >= 0) {
  583. if (!gx_pattern_cache_lookup(pdc, pis, dev, select)) {
  584. lprintf("Pattern cache lookup failed after insertion!\n");
  585. code = gs_note_error(gs_error_Fatal);
  586. }
  587. }
  588. #ifdef DEBUG
  589. if (gs_debug_c('B')) {
  590. if (adev->mask)
  591. debug_dump_bitmap(adev->mask->base, adev->mask->raster,
  592. adev->mask->height, "[B]Pattern mask");
  593. if (adev->bits)
  594. debug_dump_bitmap(((gx_device_memory *) adev->target)->base,
  595. ((gx_device_memory *) adev->target)->raster,
  596. adev->target->height, "[B]Pattern bits");
  597. }
  598. #endif
  599. /* Free the bookkeeping structures, except for the bits and mask */
  600. /* data iff they are still needed. */
  601. dev_proc(adev, close_device)((gx_device *)adev);
  602. /* Freeing the state will free the device. */
  603. gs_state_free(saved);
  604. return code;
  605. fail:
  606. gs_free_object(mem, adev, "gx_pattern_load");
  607. return code;
  608. }
  609. /* Remap a PatternType 1 color. */
  610. cs_proc_remap_color(gx_remap_Pattern); /* check the prototype */
  611. int
  612. gs_pattern1_remap_color(const gs_client_color * pc, const gs_color_space * pcs,
  613. gx_device_color * pdc, const gs_imager_state * pis,
  614. gx_device * dev, gs_color_select_t select)
  615. {
  616. gs_pattern1_instance_t *pinst = (gs_pattern1_instance_t *)pc->pattern;
  617. int code;
  618. pdc->ccolor = *pc;
  619. if (pinst == 0) {
  620. /* Null pattern */
  621. color_set_null_pattern(pdc);
  622. return 0;
  623. }
  624. if (pinst->template.PaintType == 2) { /* uncolored */
  625. code = (*pcs->params.pattern.base_space.type->remap_color)
  626. (pc, (const gs_color_space *)&pcs->params.pattern.base_space,
  627. pdc, pis, dev, select);
  628. if (code < 0)
  629. return code;
  630. if (pdc->type == gx_dc_type_pure)
  631. pdc->type = &gx_dc_pure_masked;
  632. else if (pdc->type == gx_dc_type_ht_binary)
  633. pdc->type = &gx_dc_binary_masked;
  634. else if (pdc->type == gx_dc_type_ht_colored)
  635. pdc->type = &gx_dc_colored_masked;
  636. else
  637. return_error(gs_error_unregistered);
  638. } else
  639. color_set_null_pattern(pdc);
  640. pdc->mask.id = pinst->id;
  641. pdc->mask.m_tile = 0;
  642. return gx_pattern_load(pdc, pis, dev, select);
  643. }