gxclbits.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /* Copyright (C) 1995, 1996, 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: gxclbits.c,v 1.4 2000/11/05 18:44:57 raph Exp $ */
  16. /* Halftone and bitmap writing for command lists */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gpcheck.h"
  20. #include "gserrors.h"
  21. #include "gsbitops.h"
  22. #include "gxdevice.h"
  23. #include "gxdevmem.h" /* must precede gxcldev.h */
  24. #include "gxcldev.h"
  25. #include "gxfmap.h"
  26. /*
  27. * Define when, if ever, to write character bitmaps in all bands.
  28. * Set this to:
  29. * 0 to always write in all bands;
  30. * N to write in all bands when the character has been seen in N+1
  31. * bands on a page;
  32. * max_ushort to never write in all bands.
  33. */
  34. #define CHAR_ALL_BANDS_COUNT max_ushort
  35. /* ------ Writing ------ */
  36. /*
  37. * Determine the (possibly unpadded) width in bytes for writing a bitmap,
  38. * per the algorithm in gxcldev.h. If compression_mask has any of the
  39. * cmd_mask_compress_any bits set, we assume the bitmap will be compressed.
  40. * Return the total size of the bitmap.
  41. */
  42. uint
  43. clist_bitmap_bytes(uint width_bits, uint height, int compression_mask,
  44. uint * width_bytes, uint * raster)
  45. {
  46. uint full_raster = *raster = bitmap_raster(width_bits);
  47. uint short_raster = (width_bits + 7) >> 3;
  48. uint width_bytes_last;
  49. if (compression_mask & cmd_mask_compress_any)
  50. *width_bytes = width_bytes_last = full_raster;
  51. else if (short_raster <= cmd_max_short_width_bytes ||
  52. height <= 1 ||
  53. (compression_mask & decompress_spread) != 0
  54. )
  55. *width_bytes = width_bytes_last = short_raster;
  56. else
  57. *width_bytes = full_raster, width_bytes_last = short_raster;
  58. return
  59. (height == 0 ? 0 : *width_bytes * (height - 1) + width_bytes_last);
  60. }
  61. /*
  62. * Compress a bitmap, skipping extra padding bytes at the end of each row if
  63. * necessary. We require height >= 1, raster >= bitmap_raster(width_bits).
  64. */
  65. private int
  66. cmd_compress_bitmap(stream_state * st, const byte * data, uint width_bits,
  67. uint raster, uint height, stream_cursor_write * pw)
  68. {
  69. uint width_bytes = bitmap_raster(width_bits);
  70. int status = 0;
  71. stream_cursor_read r;
  72. r.ptr = data - 1;
  73. if (raster == width_bytes) {
  74. r.limit = r.ptr + raster * height;
  75. status = (*st->template->process) (st, &r, pw, true);
  76. } else { /* Compress row-by-row. */
  77. uint y;
  78. for (y = 1; (r.limit = r.ptr + width_bytes), y < height; ++y) {
  79. status = (*st->template->process) (st, &r, pw, false);
  80. if (status)
  81. break;
  82. if (r.ptr != r.limit) { /* We don't attempt to handle compressors that */
  83. /* require >1 input byte to make progress. */
  84. status = -1;
  85. break;
  86. }
  87. r.ptr += raster - width_bytes;
  88. }
  89. if (status == 0)
  90. status = (*st->template->process) (st, &r, pw, true);
  91. }
  92. if (st->template->release)
  93. (*st->template->release) (st);
  94. return status;
  95. }
  96. /*
  97. * Put a bitmap in the buffer, compressing if appropriate.
  98. * pcls == 0 means put the bitmap in all bands.
  99. * Return <0 if error, otherwise the compression method.
  100. * A return value of gs_error_limitcheck means that the bitmap was too big
  101. * to fit in the command reading buffer.
  102. * Note that this leaves room for the command and initial arguments,
  103. * but doesn't fill them in.
  104. */
  105. int
  106. cmd_put_bits(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  107. const byte * data, uint width_bits, uint height, uint raster, int op_size,
  108. int compression_mask, byte ** pdp, uint * psize)
  109. {
  110. uint short_raster, full_raster;
  111. uint short_size =
  112. clist_bitmap_bytes(width_bits, height,
  113. compression_mask & ~cmd_mask_compress_any,
  114. &short_raster, &full_raster);
  115. uint uncompressed_raster;
  116. uint uncompressed_size =
  117. clist_bitmap_bytes(width_bits, height, compression_mask,
  118. &uncompressed_raster, &full_raster);
  119. uint max_size = cbuf_size - op_size;
  120. gs_memory_t *mem = (cldev->memory ? cldev->memory : &gs_memory_default);
  121. byte *dp;
  122. int compress = 0;
  123. /*
  124. * See if compressing the bits is possible and worthwhile.
  125. * Currently we can't compress if the compressed data won't fit in
  126. * the command reading buffer, or if the decompressed data won't fit
  127. * in the buffer and decompress_elsewhere isn't set.
  128. */
  129. if (short_size >= 50 &&
  130. (compression_mask & cmd_mask_compress_any) != 0 &&
  131. (uncompressed_size <= max_size ||
  132. (compression_mask & decompress_elsewhere) != 0)
  133. ) {
  134. union ss_ {
  135. stream_state ss;
  136. stream_CFE_state cf;
  137. stream_RLE_state rl;
  138. } sstate;
  139. int code;
  140. int try_size = op_size + min(uncompressed_size, max_size);
  141. *psize = try_size;
  142. code = (pcls != 0 ?
  143. set_cmd_put_op(dp, cldev, pcls, 0, try_size) :
  144. set_cmd_put_all_op(dp, cldev, 0, try_size));
  145. if (code < 0)
  146. return code;
  147. cmd_uncount_op(0, try_size);
  148. /*
  149. * Note that we currently keep all the padding if we are
  150. * compressing. This is ridiculous, but it's too hard to
  151. * change right now.
  152. */
  153. if (compression_mask & (1 << cmd_compress_cfe)) {
  154. /* Try CCITTFax compression. */
  155. clist_cfe_init(&sstate.cf,
  156. uncompressed_raster << 3 /*width_bits*/,
  157. mem);
  158. compress = cmd_compress_cfe;
  159. } else if (compression_mask & (1 << cmd_compress_rle)) {
  160. /* Try RLE compression. */
  161. clist_rle_init(&sstate.rl);
  162. compress = cmd_compress_rle;
  163. }
  164. if (compress) {
  165. byte *wbase = dp + (op_size - 1);
  166. stream_cursor_write w;
  167. /*
  168. * We can give up on compressing if we generate too much
  169. * output to fit in the command reading buffer, or too
  170. * much to make compression worthwhile.
  171. */
  172. uint wmax = min(uncompressed_size, max_size);
  173. int status;
  174. w.ptr = wbase;
  175. w.limit = w.ptr + min(wmax, short_size >> 1);
  176. status = cmd_compress_bitmap((stream_state *) & sstate, data,
  177. uncompressed_raster << 3 /*width_bits */ ,
  178. raster, height, &w);
  179. if (status == 0) { /* Use compressed representation. */
  180. uint wcount = w.ptr - wbase;
  181. cmd_shorten_list_op(cldev,
  182. (pcls ? &pcls->list : &cldev->band_range_list),
  183. try_size - (op_size + wcount));
  184. *psize = op_size + wcount;
  185. goto out;
  186. }
  187. }
  188. if (uncompressed_size > max_size) {
  189. /* Shorten to zero, erasing the operation altogether */
  190. if_debug1 ('L', "[L]Uncompressed bits %u too large for buffer\n",
  191. uncompressed_size);
  192. cmd_shorten_list_op(cldev,
  193. (pcls ? &pcls->list : &cldev->band_range_list),
  194. try_size);
  195. return_error(gs_error_limitcheck);
  196. }
  197. if (uncompressed_size != short_size) {
  198. if_debug2 ('L', "[L]Shortening bits from %u to %u\n",
  199. try_size, op_size + short_size);
  200. cmd_shorten_list_op(cldev,
  201. (pcls ? &pcls->list : &cldev->band_range_list),
  202. try_size - (op_size + short_size));
  203. *psize = op_size + short_size;
  204. }
  205. compress = 0;
  206. } else if (uncompressed_size > max_size)
  207. return_error(gs_error_limitcheck);
  208. else {
  209. int code;
  210. *psize = op_size + short_size;
  211. code = (pcls != 0 ?
  212. set_cmd_put_op(dp, cldev, pcls, 0, *psize) :
  213. set_cmd_put_all_op(dp, cldev, 0, *psize));
  214. if (code < 0)
  215. return code;
  216. cmd_uncount_op(0, *psize);
  217. }
  218. bytes_copy_rectangle(dp + op_size, short_raster, data, raster,
  219. short_raster, height);
  220. out:
  221. *pdp = dp;
  222. return compress;
  223. }
  224. /* Add a command to set the tile size and depth. */
  225. private uint
  226. cmd_size_tile_params(const gx_strip_bitmap * tile)
  227. {
  228. return 2 + cmd_size_w(tile->rep_width) + cmd_size_w(tile->rep_height) +
  229. (tile->rep_width == tile->size.x ? 0 :
  230. cmd_size_w(tile->size.x / tile->rep_width)) +
  231. (tile->rep_height == tile->size.y ? 0 :
  232. cmd_size_w(tile->size.y / tile->rep_height)) +
  233. (tile->rep_shift == 0 ? 0 : cmd_size_w(tile->rep_shift));
  234. }
  235. private void
  236. cmd_store_tile_params(byte * dp, const gx_strip_bitmap * tile, int depth,
  237. uint csize)
  238. {
  239. byte *p = dp + 2;
  240. byte bd = depth - 1;
  241. *dp = cmd_count_op(cmd_opv_set_tile_size, csize);
  242. p = cmd_put_w(tile->rep_width, p);
  243. p = cmd_put_w(tile->rep_height, p);
  244. if (tile->rep_width != tile->size.x) {
  245. p = cmd_put_w(tile->size.x / tile->rep_width, p);
  246. bd |= 0x20;
  247. }
  248. if (tile->rep_height != tile->size.y) {
  249. p = cmd_put_w(tile->size.y / tile->rep_height, p);
  250. bd |= 0x40;
  251. }
  252. if (tile->rep_shift != 0) {
  253. cmd_put_w(tile->rep_shift, p);
  254. bd |= 0x80;
  255. }
  256. dp[1] = bd;
  257. }
  258. /* Add a command to set the tile index. */
  259. /* This is a relatively high-frequency operation, so we declare it `inline'. */
  260. inline private int
  261. cmd_put_tile_index(gx_device_clist_writer *cldev, gx_clist_state *pcls,
  262. uint indx)
  263. {
  264. int idelta = indx - pcls->tile_index + 8;
  265. byte *dp;
  266. int code;
  267. if (!(idelta & ~15)) {
  268. code = set_cmd_put_op(dp, cldev, pcls,
  269. cmd_op_delta_tile_index + idelta, 1);
  270. if (code < 0)
  271. return code;
  272. } else {
  273. code = set_cmd_put_op(dp, cldev, pcls,
  274. cmd_op_set_tile_index + (indx >> 8), 2);
  275. if (code < 0)
  276. return code;
  277. dp[1] = indx & 0xff;
  278. }
  279. if_debug2('L', "[L]writing index=%u, offset=%lu\n",
  280. indx, cldev->tile_table[indx].offset);
  281. return 0;
  282. }
  283. /* If necessary, write out data for a single color map. */
  284. int
  285. cmd_put_color_map(gx_device_clist_writer * cldev, cmd_map_index map_index,
  286. const gx_transfer_map * map, gs_id * pid)
  287. {
  288. byte *dp;
  289. int code;
  290. if (map == 0) {
  291. if (pid && *pid == gs_no_id)
  292. return 0; /* no need to write */
  293. code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_misc, 2);
  294. if (code < 0)
  295. return code;
  296. dp[1] = cmd_set_misc_map + (cmd_map_none << 4) + map_index;
  297. if (pid)
  298. *pid = gs_no_id;
  299. } else {
  300. if (pid && map->id == *pid)
  301. return 0; /* no need to write */
  302. if (map->proc == gs_identity_transfer) {
  303. code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_misc, 2);
  304. if (code < 0)
  305. return code;
  306. dp[1] = cmd_set_misc_map + (cmd_map_identity << 4) + map_index;
  307. } else {
  308. code = set_cmd_put_all_op(dp, cldev, cmd_opv_set_misc,
  309. 2 + sizeof(map->values));
  310. if (code < 0)
  311. return code;
  312. dp[1] = cmd_set_misc_map + (cmd_map_other << 4) + map_index;
  313. memcpy(dp + 2, map->values, sizeof(map->values));
  314. }
  315. if (pid)
  316. *pid = map->id;
  317. }
  318. return 0;
  319. }
  320. /* ------ Tile cache management ------ */
  321. /* We want consecutive ids to map to consecutive hash slots if possible, */
  322. /* so we can use a delta representation when setting the index. */
  323. /* NB that we cannot emit 'delta' style tile indices if VM error recovery */
  324. /* is in effect, since reader & writer's tile indices may get out of phase */
  325. /* as a consequence of error recovery occurring. */
  326. #define tile_id_hash(id) (id)
  327. #define tile_hash_next(index) ((index) + 413) /* arbitrary large odd # */
  328. typedef struct tile_loc_s {
  329. uint index;
  330. tile_slot *tile;
  331. } tile_loc;
  332. /* Look up a tile or character in the cache. If found, set the index and */
  333. /* pointer; if not, set the index to the insertion point. */
  334. private bool
  335. clist_find_bits(gx_device_clist_writer * cldev, gx_bitmap_id id, tile_loc * ploc)
  336. {
  337. uint index = tile_id_hash(id);
  338. const tile_hash *table = cldev->tile_table;
  339. uint mask = cldev->tile_hash_mask;
  340. ulong offset;
  341. for (; (offset = table[index &= mask].offset) != 0;
  342. index = tile_hash_next(index)
  343. ) {
  344. tile_slot *tile = (tile_slot *) (cldev->data + offset);
  345. if (tile->id == id) {
  346. ploc->index = index;
  347. ploc->tile = tile;
  348. return true;
  349. }
  350. }
  351. ploc->index = index;
  352. return false;
  353. }
  354. /* Delete a tile from the cache. */
  355. private void
  356. clist_delete_tile(gx_device_clist_writer * cldev, tile_slot * slot)
  357. {
  358. tile_hash *table = cldev->tile_table;
  359. uint mask = cldev->tile_hash_mask;
  360. uint index = slot->index;
  361. ulong offset;
  362. if_debug2('L', "[L]deleting index=%u, offset=%lu\n",
  363. index, (ulong) ((byte *) slot - cldev->data));
  364. gx_bits_cache_free(&cldev->bits, (gx_cached_bits_head *) slot,
  365. &cldev->chunk);
  366. table[index].offset = 0;
  367. /* Delete the entry from the hash table. */
  368. /* We'd like to move up any later entries, so that we don't need */
  369. /* a deleted mark, but it's too difficult to note this in the */
  370. /* band list, so instead, we just delete any entries that */
  371. /* would need to be moved. */
  372. while ((offset = table[index = tile_hash_next(index) & mask].offset) != 0) {
  373. tile_slot *tile = (tile_slot *) (cldev->data + offset);
  374. tile_loc loc;
  375. if (!clist_find_bits(cldev, tile->id, &loc)) { /* We didn't find it, so it should be moved into a slot */
  376. /* that we just vacated; instead, delete it. */
  377. if_debug2('L', "[L]move-deleting index=%u, offset=%lu\n",
  378. index, offset);
  379. gx_bits_cache_free(&cldev->bits,
  380. (gx_cached_bits_head *) (cldev->data + offset),
  381. &cldev->chunk);
  382. table[index].offset = 0;
  383. }
  384. }
  385. }
  386. /* Add a tile to the cache. */
  387. /* tile->raster holds the raster for the replicated tile; */
  388. /* we pass the raster of the actual data separately. */
  389. private int
  390. clist_add_tile(gx_device_clist_writer * cldev, const gx_strip_bitmap * tiles,
  391. uint sraster, int depth)
  392. {
  393. uint raster = tiles->raster;
  394. uint size_bytes = raster * tiles->size.y;
  395. uint tsize =
  396. sizeof(tile_slot) + cldev->tile_band_mask_size + size_bytes;
  397. gx_cached_bits_head *slot_head;
  398. #define slot ((tile_slot *)slot_head)
  399. if (cldev->bits.csize == cldev->tile_max_count) { /* Don't let the hash table get too full: delete an entry. */
  400. /* Since gx_bits_cache_alloc returns an entry to delete when */
  401. /* it fails, just force it to fail. */
  402. gx_bits_cache_alloc(&cldev->bits, (ulong) cldev->chunk.size,
  403. &slot_head);
  404. if (slot_head == 0) { /* Wrap around and retry. */
  405. cldev->bits.cnext = 0;
  406. gx_bits_cache_alloc(&cldev->bits, (ulong) cldev->chunk.size,
  407. &slot_head);
  408. #ifdef DEBUG
  409. if (slot_head == 0) {
  410. lprintf("No entry to delete!\n");
  411. return_error(gs_error_Fatal);
  412. }
  413. #endif
  414. }
  415. clist_delete_tile(cldev, slot);
  416. }
  417. /* Allocate the space for the new entry, deleting entries as needed. */
  418. while (gx_bits_cache_alloc(&cldev->bits, (ulong) tsize, &slot_head) < 0) {
  419. if (slot_head == 0) { /* Wrap around. */
  420. if (cldev->bits.cnext == 0) { /* Too big to fit. We should probably detect this */
  421. /* sooner, since if we get here, we've cleared the */
  422. /* cache. */
  423. return_error(gs_error_limitcheck);
  424. }
  425. cldev->bits.cnext = 0;
  426. } else
  427. clist_delete_tile(cldev, slot);
  428. }
  429. /* Fill in the entry. */
  430. slot->cb_depth = depth;
  431. slot->cb_raster = raster;
  432. slot->width = tiles->rep_width;
  433. slot->height = tiles->rep_height;
  434. slot->shift = slot->rep_shift = tiles->rep_shift;
  435. slot->x_reps = slot->y_reps = 1;
  436. slot->id = tiles->id;
  437. memset(ts_mask(slot), 0, cldev->tile_band_mask_size);
  438. bytes_copy_rectangle(ts_bits(cldev, slot), raster,
  439. tiles->data, sraster,
  440. (tiles->rep_width * depth + 7) >> 3,
  441. tiles->rep_height);
  442. /* Make the hash table entry. */
  443. {
  444. tile_loc loc;
  445. #ifdef DEBUG
  446. if (clist_find_bits(cldev, tiles->id, &loc))
  447. lprintf1("clist_find_bits(0x%lx) should have failed!\n",
  448. (ulong) tiles->id);
  449. #else
  450. clist_find_bits(cldev, tiles->id, &loc); /* always fails */
  451. #endif
  452. slot->index = loc.index;
  453. cldev->tile_table[loc.index].offset =
  454. (byte *) slot_head - cldev->data;
  455. if_debug2('L', "[L]adding index=%u, offset=%lu\n",
  456. loc.index, cldev->tile_table[loc.index].offset);
  457. }
  458. slot->num_bands = 0;
  459. return 0;
  460. }
  461. /* ------ Driver procedure support ------ */
  462. /* Change the tile parameters (size and depth). */
  463. /* Currently we do this for all bands at once. */
  464. private void
  465. clist_new_tile_params(gx_strip_bitmap * new_tile, const gx_strip_bitmap * tiles,
  466. int depth, const gx_device_clist_writer * cldev)
  467. { /*
  468. * Adjust the replication factors. If we can, we replicate
  469. * the tile in X up to 32 bytes, and then in Y up to 4 copies,
  470. * as long as we don't exceed a total tile size of 256 bytes,
  471. * or more than 255 repetitions in X or Y, or make the tile so
  472. * large that not all possible tiles will fit in the cache.
  473. * Also, don't attempt Y replication if shifting is required.
  474. */
  475. #define max_tile_reps_x 255
  476. #define max_tile_bytes_x 32
  477. #define max_tile_reps_y 4
  478. #define max_tile_bytes 256
  479. uint rep_width = tiles->rep_width;
  480. uint rep_height = tiles->rep_height;
  481. uint rep_width_bits = rep_width * depth;
  482. uint tile_overhead =
  483. sizeof(tile_slot) + cldev->tile_band_mask_size;
  484. uint max_bytes = cldev->chunk.size / (rep_width_bits * rep_height);
  485. max_bytes -= min(max_bytes, tile_overhead);
  486. if (max_bytes > max_tile_bytes)
  487. max_bytes = max_tile_bytes;
  488. *new_tile = *tiles;
  489. {
  490. uint max_bits_x = max_bytes * 8 / rep_height;
  491. uint reps_x =
  492. min(max_bits_x, max_tile_bytes_x * 8) / rep_width_bits;
  493. uint reps_y;
  494. while (reps_x > max_tile_reps_x)
  495. reps_x >>= 1;
  496. new_tile->size.x = max(reps_x, 1) * rep_width;
  497. new_tile->raster = bitmap_raster(new_tile->size.x * depth);
  498. if (tiles->shift != 0)
  499. reps_y = 1;
  500. else {
  501. reps_y = max_bytes / (new_tile->raster * rep_height);
  502. if (reps_y > max_tile_reps_y)
  503. reps_y = max_tile_reps_y;
  504. else if (reps_y < 1)
  505. reps_y = 1;
  506. }
  507. new_tile->size.y = reps_y * rep_height;
  508. }
  509. #undef max_tile_reps_x
  510. #undef max_tile_bytes_x
  511. #undef max_tile_reps_y
  512. #undef max_tile_bytes
  513. }
  514. /* Change tile for clist_tile_rectangle. */
  515. int
  516. clist_change_tile(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  517. const gx_strip_bitmap * tiles, int depth)
  518. {
  519. tile_loc loc;
  520. int code;
  521. #define tile_params_differ(cldev, tiles, depth)\
  522. ((tiles)->rep_width != (cldev)->tile_params.rep_width ||\
  523. (tiles)->rep_height != (cldev)->tile_params.rep_height ||\
  524. (tiles)->rep_shift != (cldev)->tile_params.rep_shift ||\
  525. (depth) != (cldev)->tile_depth)
  526. top:if (clist_find_bits(cldev, tiles->id, &loc)) { /* The bitmap is in the cache. Check whether this band */
  527. /* knows about it. */
  528. int band_index = pcls - cldev->states;
  529. byte *bptr = ts_mask(loc.tile) + (band_index >> 3);
  530. byte bmask = 1 << (band_index & 7);
  531. if (*bptr & bmask) { /* Already known. Just set the index. */
  532. if (pcls->tile_index == loc.index)
  533. return 0;
  534. if ((code = cmd_put_tile_index(cldev, pcls, loc.index)) < 0)
  535. return code;
  536. } else {
  537. uint extra = 0;
  538. if tile_params_differ
  539. (cldev, tiles, depth) { /*
  540. * We have a cached tile whose parameters differ from
  541. * the current ones. Because of the way tile IDs are
  542. * managed, this is currently only possible when mixing
  543. * Patterns and halftones, but if we didn't generate new
  544. * IDs each time the main halftone cache needed to be
  545. * refreshed, this could also happen simply from
  546. * switching screens.
  547. */
  548. int band;
  549. clist_new_tile_params(&cldev->tile_params, tiles, depth,
  550. cldev);
  551. cldev->tile_depth = depth;
  552. /* No band knows about the new parameters. */
  553. for (band = cldev->tile_known_min;
  554. band <= cldev->tile_known_max;
  555. ++band
  556. )
  557. cldev->states[band].known &= ~tile_params_known;
  558. cldev->tile_known_min = cldev->nbands;
  559. cldev->tile_known_max = -1;
  560. }
  561. if (!(pcls->known & tile_params_known)) { /* We're going to have to write the tile parameters. */
  562. extra = cmd_size_tile_params(&cldev->tile_params);
  563. } { /*
  564. * This band doesn't know this tile yet, so output the
  565. * bits. Note that the offset we write is the one used by
  566. * the reading phase, not the writing phase. Note also
  567. * that the size of the cached and written tile may differ
  568. * from that of the client's tile. Finally, note that
  569. * this tile's size parameters are guaranteed to be
  570. * compatible with those stored in the device
  571. * (cldev->tile_params).
  572. */
  573. ulong offset = (byte *) loc.tile - cldev->chunk.data;
  574. uint rsize =
  575. extra + 1 + cmd_size_w(loc.index) + cmd_size_w(offset);
  576. byte *dp;
  577. uint csize;
  578. int code =
  579. cmd_put_bits(cldev, pcls, ts_bits(cldev, loc.tile),
  580. tiles->rep_width * depth, tiles->rep_height,
  581. loc.tile->cb_raster, rsize,
  582. (cldev->tile_params.size.x > tiles->rep_width ?
  583. decompress_elsewhere | decompress_spread :
  584. decompress_elsewhere),
  585. &dp, &csize);
  586. if (code < 0)
  587. return code;
  588. if (extra) { /* Write the tile parameters before writing the bits. */
  589. cmd_store_tile_params(dp, &cldev->tile_params, depth,
  590. extra);
  591. dp += extra;
  592. /* This band now knows the parameters. */
  593. pcls->known |= tile_params_known;
  594. if (band_index < cldev->tile_known_min)
  595. cldev->tile_known_min = band_index;
  596. if (band_index > cldev->tile_known_max)
  597. cldev->tile_known_max = band_index;
  598. }
  599. *dp = cmd_count_op(cmd_opv_set_tile_bits, csize - extra);
  600. dp++;
  601. dp = cmd_put_w(loc.index, dp);
  602. cmd_put_w(offset, dp);
  603. *bptr |= bmask;
  604. loc.tile->num_bands++;
  605. }
  606. }
  607. pcls->tile_index = loc.index;
  608. pcls->tile_id = loc.tile->id;
  609. return 0;
  610. }
  611. /* The tile is not in the cache, add it. */
  612. {
  613. gx_strip_bitmap new_tile;
  614. gx_strip_bitmap *ptile;
  615. /* Ensure that the tile size is compatible. */
  616. if (tile_params_differ(cldev, tiles, depth)) { /* We'll reset cldev->tile_params when we write the bits. */
  617. clist_new_tile_params(&new_tile, tiles, depth, cldev);
  618. ptile = &new_tile;
  619. } else {
  620. cldev->tile_params.id = tiles->id;
  621. cldev->tile_params.data = tiles->data;
  622. ptile = &cldev->tile_params;
  623. }
  624. code = clist_add_tile(cldev, ptile, tiles->raster, depth);
  625. if (code < 0)
  626. return code;
  627. }
  628. goto top;
  629. #undef tile_params_differ
  630. }
  631. /* Change "tile" for clist_copy_*. tiles->[rep_]shift must be zero. */
  632. int
  633. clist_change_bits(gx_device_clist_writer * cldev, gx_clist_state * pcls,
  634. const gx_strip_bitmap * tiles, int depth)
  635. {
  636. tile_loc loc;
  637. int code;
  638. top:if (clist_find_bits(cldev, tiles->id, &loc)) { /* The bitmap is in the cache. Check whether this band */
  639. /* knows about it. */
  640. uint band_index = pcls - cldev->states;
  641. byte *bptr = ts_mask(loc.tile) + (band_index >> 3);
  642. byte bmask = 1 << (band_index & 7);
  643. if (*bptr & bmask) { /* Already known. Just set the index. */
  644. if (pcls->tile_index == loc.index)
  645. return 0;
  646. cmd_put_tile_index(cldev, pcls, loc.index);
  647. } else { /* Not known yet. Output the bits. */
  648. /* Note that the offset we write is the one used by */
  649. /* the reading phase, not the writing phase. */
  650. ulong offset = (byte *) loc.tile - cldev->chunk.data;
  651. uint rsize = 2 + cmd_size_w(loc.tile->width) +
  652. cmd_size_w(loc.tile->height) + cmd_size_w(loc.index) +
  653. cmd_size_w(offset);
  654. byte *dp;
  655. uint csize;
  656. gx_clist_state *bit_pcls = pcls;
  657. int code;
  658. if (loc.tile->num_bands == CHAR_ALL_BANDS_COUNT)
  659. bit_pcls = NULL;
  660. code = cmd_put_bits(cldev, bit_pcls, ts_bits(cldev, loc.tile),
  661. loc.tile->width * depth,
  662. loc.tile->height, loc.tile->cb_raster,
  663. rsize,
  664. (1 << cmd_compress_cfe) | decompress_elsewhere,
  665. &dp, &csize);
  666. if (code < 0)
  667. return code;
  668. *dp = cmd_count_op(cmd_opv_set_bits, csize);
  669. dp[1] = (depth << 2) + code;
  670. dp += 2;
  671. dp = cmd_put_w(loc.tile->width, dp);
  672. dp = cmd_put_w(loc.tile->height, dp);
  673. dp = cmd_put_w(loc.index, dp);
  674. cmd_put_w(offset, dp);
  675. if (bit_pcls == NULL) {
  676. memset(ts_mask(loc.tile), 0xff,
  677. cldev->tile_band_mask_size);
  678. loc.tile->num_bands = cldev->nbands;
  679. } else {
  680. *bptr |= bmask;
  681. loc.tile->num_bands++;
  682. }
  683. }
  684. pcls->tile_index = loc.index;
  685. pcls->tile_id = loc.tile->id;
  686. return 0;
  687. }
  688. /* The tile is not in the cache. */
  689. code = clist_add_tile(cldev, tiles, tiles->raster, depth);
  690. if (code < 0)
  691. return code;
  692. goto top;
  693. }