gxclread.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* Copyright (C) 1991, 1996, 1997, 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: gxclread.c,v 1.13 2004/10/07 05:18:34 ray Exp $ */
  14. /* Command list reading for Ghostscript. */
  15. #include "memory_.h"
  16. #include "gx.h"
  17. #include "gp.h" /* for gp_fmode_rb */
  18. #include "gpcheck.h"
  19. #include "gserrors.h"
  20. #include "gxdevice.h"
  21. #include "gscoord.h" /* requires gsmatrix.h */
  22. #include "gsdevice.h" /* for gs_deviceinitialmatrix */
  23. #include "gxdevmem.h" /* must precede gxcldev.h */
  24. #include "gxcldev.h"
  25. #include "gxgetbit.h"
  26. #include "gxhttile.h"
  27. #include "gdevplnx.h"
  28. /*
  29. * We really don't like the fact that gdevprn.h is included here, since
  30. * command lists are supposed to be usable for purposes other than printer
  31. * devices; but gdev_prn_colors_used and gdev_create_buf_device are
  32. * currently only applicable to printer devices.
  33. */
  34. #include "gdevprn.h"
  35. #include "stream.h"
  36. #include "strimpl.h"
  37. /* ------ Band file reading stream ------ */
  38. /*
  39. * To separate banding per se from command list interpretation,
  40. * we make the command list interpreter simply read from a stream.
  41. * When we are actually doing banding, the stream filters the band file
  42. * and only passes through the commands for the current bands (or band
  43. * ranges that include a current band).
  44. */
  45. typedef struct stream_band_read_state_s {
  46. stream_state_common;
  47. gx_band_page_info_t page_info;
  48. int band_first, band_last;
  49. uint left; /* amount of data left in this run */
  50. cmd_block b_this;
  51. } stream_band_read_state;
  52. private int
  53. s_band_read_init(stream_state * st)
  54. {
  55. stream_band_read_state *const ss = (stream_band_read_state *) st;
  56. ss->left = 0;
  57. ss->b_this.band_min = 0;
  58. ss->b_this.band_max = 0;
  59. ss->b_this.pos = 0;
  60. clist_rewind(ss->page_bfile, false, ss->page_bfname);
  61. return 0;
  62. }
  63. private int
  64. s_band_read_process(stream_state * st, stream_cursor_read * ignore_pr,
  65. stream_cursor_write * pw, bool last)
  66. {
  67. stream_band_read_state *const ss = (stream_band_read_state *) st;
  68. register byte *q = pw->ptr;
  69. byte *wlimit = pw->limit;
  70. clist_file_ptr cfile = ss->page_cfile;
  71. clist_file_ptr bfile = ss->page_bfile;
  72. uint left = ss->left;
  73. int status = 1;
  74. uint count;
  75. while ((count = wlimit - q) != 0) {
  76. if (left) { /* Read more data for the current run. */
  77. if (count > left)
  78. count = left;
  79. clist_fread_chars(q + 1, count, cfile);
  80. if (clist_ferror_code(cfile) < 0) {
  81. status = ERRC;
  82. break;
  83. }
  84. q += count;
  85. left -= count;
  86. process_interrupts(st->memory);
  87. continue;
  88. }
  89. rb:
  90. /*
  91. * Scan for the next run for the current bands (or a band range
  92. * that includes a current band).
  93. */
  94. if (ss->b_this.band_min == cmd_band_end &&
  95. clist_ftell(bfile) == ss->page_bfile_end_pos
  96. ) {
  97. status = EOFC;
  98. break;
  99. } {
  100. int bmin = ss->b_this.band_min;
  101. int bmax = ss->b_this.band_max;
  102. long pos = ss->b_this.pos;
  103. clist_fread_chars(&ss->b_this, sizeof(ss->b_this), bfile);
  104. if (!(ss->band_last >= bmin && ss->band_first <= bmax))
  105. goto rb;
  106. clist_fseek(cfile, pos, SEEK_SET, ss->page_cfname);
  107. left = (uint) (ss->b_this.pos - pos);
  108. if_debug5('l', "[l]reading for bands (%d,%d) at bfile %ld, cfile %ld, length %u\n",
  109. bmin, bmax,
  110. clist_ftell(bfile) - 2 * sizeof(ss->b_this),
  111. pos, left);
  112. }
  113. }
  114. pw->ptr = q;
  115. ss->left = left;
  116. return status;
  117. }
  118. /* Stream template */
  119. private const stream_template s_band_read_template = {
  120. &st_stream_state, s_band_read_init, s_band_read_process, 1, cbuf_size
  121. };
  122. /* ------ Reading/rendering ------ */
  123. /* Forward references */
  124. private int clist_render_init(gx_device_clist *);
  125. private int clist_playback_file_bands(clist_playback_action action,
  126. gx_device_clist_reader *cdev,
  127. gx_band_page_info_t *page_info,
  128. gx_device *target,
  129. int band_first, int band_last,
  130. int x0, int y0);
  131. private int clist_rasterize_lines(gx_device *dev, int y, int lineCount,
  132. gx_device *bdev,
  133. const gx_render_plane_t *render_plane,
  134. int *pmy);
  135. /* Calculate the raster for a chunky or planar device. */
  136. private int
  137. clist_plane_raster(const gx_device *dev, const gx_render_plane_t *render_plane)
  138. {
  139. return bitmap_raster(dev->width *
  140. (render_plane && render_plane->index >= 0 ?
  141. render_plane->depth : dev->color_info.depth));
  142. }
  143. /* Select full-pixel rendering if required for RasterOp. */
  144. private void
  145. clist_select_render_plane(gx_device *dev, int y, int height,
  146. gx_render_plane_t *render_plane, int index)
  147. {
  148. if (index >= 0) {
  149. gx_colors_used_t colors_used;
  150. int ignore_start;
  151. gdev_prn_colors_used(dev, y, height, &colors_used, &ignore_start);
  152. if (colors_used.slow_rop)
  153. index = -1;
  154. }
  155. if (index < 0)
  156. render_plane->index = index;
  157. else
  158. gx_render_plane_init(render_plane, dev, index);
  159. }
  160. /*
  161. * Do device setup from params stored in command list. This is only for
  162. * async rendering & assumes that the first command in every command list
  163. * is a put_params command which sets all space-related parameters to the
  164. * value they will have for the duration of that command list.
  165. */
  166. int
  167. clist_setup_params(gx_device *dev)
  168. {
  169. gx_device_clist_reader * const crdev =
  170. &((gx_device_clist *)dev)->reader;
  171. int code = clist_render_init((gx_device_clist *)dev);
  172. if (code < 0)
  173. return code;
  174. code = clist_playback_file_bands(playback_action_setup,
  175. crdev, &crdev->page_info, 0, 0, 0, 0, 0);
  176. /* put_params may have reinitialized device into a writer */
  177. clist_render_init((gx_device_clist *)dev);
  178. return code;
  179. }
  180. /* Copy a rasterized rectangle to the client, rasterizing if needed. */
  181. int
  182. clist_get_bits_rectangle(gx_device *dev, const gs_int_rect * prect,
  183. gs_get_bits_params_t *params, gs_int_rect **unread)
  184. {
  185. gx_device_clist_common *cdev = (gx_device_clist_common *)dev;
  186. gs_get_bits_options_t options = params->options;
  187. int y = prect->p.y;
  188. int end_y = prect->q.y;
  189. int line_count = end_y - y;
  190. gs_int_rect band_rect;
  191. int lines_rasterized;
  192. gx_device *bdev;
  193. int num_planes =
  194. (options & GB_PACKING_CHUNKY ? 1 :
  195. options & GB_PACKING_PLANAR ? dev->color_info.num_components :
  196. options & GB_PACKING_BIT_PLANAR ? dev->color_info.depth :
  197. 0 /****** NOT POSSIBLE ******/);
  198. gx_render_plane_t render_plane;
  199. int plane_index;
  200. int my;
  201. int code;
  202. if (prect->p.x < 0 || prect->q.x > dev->width ||
  203. y < 0 || end_y > dev->height
  204. )
  205. return_error(gs_error_rangecheck);
  206. if (line_count <= 0 || prect->p.x >= prect->q.x)
  207. return 0;
  208. /*
  209. * Calculate the render_plane from the params. There are two cases:
  210. * full pixels, or a single plane.
  211. */
  212. plane_index = -1;
  213. if (options & GB_SELECT_PLANES) {
  214. /* Look for the one selected plane. */
  215. int i;
  216. for (i = 0; i < num_planes; ++i)
  217. if (params->data[i]) {
  218. if (plane_index >= 0) /* >1 plane requested */
  219. return gx_default_get_bits_rectangle(dev, prect, params,
  220. unread);
  221. plane_index = i;
  222. }
  223. }
  224. clist_select_render_plane(dev, y, line_count, &render_plane, plane_index);
  225. code = gdev_create_buf_device(cdev->buf_procs.create_buf_device,
  226. &bdev, cdev->target, &render_plane,
  227. dev->memory, true);
  228. if (code < 0)
  229. return code;
  230. code = clist_rasterize_lines(dev, y, line_count, bdev, &render_plane, &my);
  231. if (code < 0)
  232. return code;
  233. lines_rasterized = min(code, line_count);
  234. /* Return as much of the rectangle as falls within the rasterized lines. */
  235. band_rect = *prect;
  236. band_rect.p.y = my;
  237. band_rect.q.y = my + lines_rasterized;
  238. code = dev_proc(bdev, get_bits_rectangle)
  239. (bdev, &band_rect, params, unread);
  240. cdev->buf_procs.destroy_buf_device(bdev);
  241. if (code < 0 || lines_rasterized == line_count)
  242. return code;
  243. /*
  244. * We'll have to return the rectangle in pieces. Force GB_RETURN_COPY
  245. * rather than GB_RETURN_POINTER, and require all subsequent pieces to
  246. * use the same values as the first piece for all of the other format
  247. * options. If copying isn't allowed, or if there are any unread
  248. * rectangles, punt.
  249. */
  250. if (!(options & GB_RETURN_COPY) || code > 0)
  251. return gx_default_get_bits_rectangle(dev, prect, params, unread);
  252. options = params->options;
  253. if (!(options & GB_RETURN_COPY)) {
  254. /* Redo the first piece with copying. */
  255. params->options = options =
  256. (params->options & ~GB_RETURN_ALL) | GB_RETURN_COPY;
  257. lines_rasterized = 0;
  258. }
  259. {
  260. gs_get_bits_params_t band_params;
  261. uint raster = gx_device_raster(bdev, true);
  262. code = gdev_create_buf_device(cdev->buf_procs.create_buf_device,
  263. &bdev, cdev->target, &render_plane,
  264. dev->memory, true);
  265. if (code < 0)
  266. return code;
  267. band_params = *params;
  268. while ((y += lines_rasterized) < end_y) {
  269. int i;
  270. /* Increment data pointers by lines_rasterized. */
  271. for (i = 0; i < num_planes; ++i)
  272. if (band_params.data[i])
  273. band_params.data[i] += raster * lines_rasterized;
  274. line_count = end_y - y;
  275. code = clist_rasterize_lines(dev, y, line_count, bdev,
  276. &render_plane, &my);
  277. if (code < 0)
  278. break;
  279. lines_rasterized = min(code, line_count);
  280. band_rect.p.y = my;
  281. band_rect.q.y = my + lines_rasterized;
  282. code = dev_proc(bdev, get_bits_rectangle)
  283. (bdev, &band_rect, &band_params, unread);
  284. if (code < 0)
  285. break;
  286. params->options = options = band_params.options;
  287. if (lines_rasterized == line_count)
  288. break;
  289. }
  290. cdev->buf_procs.destroy_buf_device(bdev);
  291. }
  292. return code;
  293. }
  294. /* Copy scan lines to the client. This is where rendering gets done. */
  295. /* Processes min(requested # lines, # lines available thru end of band) */
  296. private int /* returns -ve error code, or # scan lines copied */
  297. clist_rasterize_lines(gx_device *dev, int y, int line_count,
  298. gx_device *bdev, const gx_render_plane_t *render_plane,
  299. int *pmy)
  300. {
  301. gx_device_clist * const cdev = (gx_device_clist *)dev;
  302. gx_device_clist_reader * const crdev = &cdev->reader;
  303. gx_device *target = crdev->target;
  304. uint raster = clist_plane_raster(target, render_plane);
  305. byte *mdata = crdev->data + crdev->page_tile_cache_size;
  306. int plane_index = (render_plane ? render_plane->index : -1);
  307. int code;
  308. /* Render a band if necessary, and copy it incrementally. */
  309. if (crdev->ymin < 0 || crdev->yplane.index != plane_index ||
  310. !(y >= crdev->ymin && y < crdev->ymax)
  311. ) {
  312. int band_height = crdev->page_band_height;
  313. int band = y / band_height;
  314. int band_begin_line = band * band_height;
  315. int band_end_line = band_begin_line + band_height;
  316. int band_num_lines;
  317. gs_int_rect band_rect;
  318. if (band_end_line > dev->height)
  319. band_end_line = dev->height;
  320. /* Clip line_count to current band */
  321. if (line_count > band_end_line - y)
  322. line_count = band_end_line - y;
  323. band_num_lines = band_end_line - band_begin_line;
  324. if (y < 0 || y > dev->height)
  325. return_error(gs_error_rangecheck);
  326. code = crdev->buf_procs.setup_buf_device
  327. (bdev, mdata, raster, NULL, 0, band_num_lines, band_num_lines);
  328. band_rect.p.x = 0;
  329. band_rect.p.y = band_begin_line;
  330. band_rect.q.x = dev->width;
  331. band_rect.q.y = band_end_line;
  332. if (code >= 0)
  333. code = clist_render_rectangle(cdev, &band_rect, bdev, render_plane,
  334. true);
  335. /* Reset the band boundaries now, so that we don't get */
  336. /* an infinite loop. */
  337. crdev->ymin = band_begin_line;
  338. crdev->ymax = band_end_line;
  339. if (code < 0)
  340. return code;
  341. }
  342. if (line_count > crdev->ymax - y)
  343. line_count = crdev->ymax - y;
  344. code = crdev->buf_procs.setup_buf_device
  345. (bdev, mdata, raster, NULL, y - crdev->ymin, line_count,
  346. crdev->ymax - crdev->ymin);
  347. if (code < 0)
  348. return code;
  349. *pmy = 0;
  350. return line_count;
  351. }
  352. /* Initialize for reading. */
  353. private int
  354. clist_render_init(gx_device_clist *dev)
  355. {
  356. gx_device_clist_reader * const crdev = &dev->reader;
  357. crdev->ymin = crdev->ymax = 0;
  358. crdev->yplane.index = -1;
  359. /* For normal rasterizing, pages and num_pages are zero. */
  360. crdev->pages = 0;
  361. crdev->num_pages = 0;
  362. return 0;
  363. }
  364. /*
  365. * Render a rectangle to a client-supplied device. There is no necessary
  366. * relationship between band boundaries and the region being rendered.
  367. */
  368. int
  369. clist_render_rectangle(gx_device_clist *cdev, const gs_int_rect *prect,
  370. gx_device *bdev,
  371. const gx_render_plane_t *render_plane, bool clear)
  372. {
  373. gx_device_clist_reader * const crdev = &cdev->reader;
  374. const gx_placed_page *ppages;
  375. int num_pages = crdev->num_pages;
  376. int band_height = crdev->page_band_height;
  377. int band_first = prect->p.y / band_height;
  378. int band_last = (prect->q.y - 1) / band_height;
  379. gx_saved_page current_page;
  380. gx_placed_page placed_page;
  381. int code = 0;
  382. int i;
  383. /* Initialize for rendering if we haven't done so yet. */
  384. if (crdev->ymin < 0) {
  385. code = clist_end_page(&cdev->writer);
  386. if (code < 0)
  387. return code;
  388. code = clist_render_init(cdev);
  389. if (code < 0)
  390. return code;
  391. }
  392. if (render_plane)
  393. crdev->yplane = *render_plane;
  394. else
  395. crdev->yplane.index = -1;
  396. if_debug2('l', "[l]rendering bands (%d,%d)\n", band_first, band_last);
  397. if (clear)
  398. dev_proc(bdev, fill_rectangle)
  399. (bdev, 0, 0, bdev->width, bdev->height, gx_device_white(bdev));
  400. /*
  401. * If we aren't rendering saved pages, do the current one.
  402. * Note that this is the only case in which we may encounter
  403. * a gx_saved_page with non-zero cfile or bfile.
  404. */
  405. ppages = crdev->pages;
  406. if (ppages == 0) {
  407. current_page.info = crdev->page_info;
  408. placed_page.page = &current_page;
  409. placed_page.offset.x = placed_page.offset.y = 0;
  410. ppages = &placed_page;
  411. num_pages = 1;
  412. }
  413. for (i = 0; i < num_pages && code >= 0; ++i) {
  414. const gx_placed_page *ppage = &ppages[i];
  415. code = clist_playback_file_bands(playback_action_render,
  416. crdev, &ppage->page->info,
  417. bdev, band_first, band_last,
  418. prect->p.x - ppage->offset.x,
  419. prect->p.y);
  420. }
  421. return code;
  422. }
  423. /* Playback the band file, taking the indicated action w/ its contents. */
  424. private int
  425. clist_playback_file_bands(clist_playback_action action,
  426. gx_device_clist_reader *cdev,
  427. gx_band_page_info_t *page_info, gx_device *target,
  428. int band_first, int band_last, int x0, int y0)
  429. {
  430. int code = 0;
  431. bool opened_bfile = false;
  432. bool opened_cfile = false;
  433. /* We have to pick some allocator for rendering.... */
  434. gs_memory_t *mem =cdev->memory;
  435. stream_band_read_state rs;
  436. /* setup stream */
  437. s_init_state((stream_state *)&rs, &s_band_read_template,
  438. (gs_memory_t *)0);
  439. rs.band_first = band_first;
  440. rs.band_last = band_last;
  441. rs.page_info = *page_info;
  442. /* If this is a saved page, open the files. */
  443. if (rs.page_cfile == 0) {
  444. code = clist_fopen(rs.page_cfname,
  445. gp_fmode_rb, &rs.page_cfile, cdev->bandlist_memory,
  446. cdev->bandlist_memory, true);
  447. opened_cfile = (code >= 0);
  448. }
  449. if (rs.page_bfile == 0 && code >= 0) {
  450. code = clist_fopen(rs.page_bfname,
  451. gp_fmode_rb, &rs.page_bfile, cdev->bandlist_memory,
  452. cdev->bandlist_memory, false);
  453. opened_bfile = (code >= 0);
  454. }
  455. if (rs.page_cfile != 0 && rs.page_bfile != 0) {
  456. stream s;
  457. byte sbuf[cbuf_size];
  458. static const stream_procs no_procs = {
  459. s_std_noavailable, s_std_noseek, s_std_read_reset,
  460. s_std_read_flush, s_std_close, s_band_read_process
  461. };
  462. s_band_read_init((stream_state *)&rs);
  463. /* The stream doesn't need a memory, but we'll need to access s.memory->gs_lib_ctx. */
  464. s_init(&s, mem);
  465. s_std_init(&s, sbuf, cbuf_size, &no_procs, s_mode_read);
  466. s.foreign = 1;
  467. s.state = (stream_state *)&rs;
  468. code = clist_playback_band(action, cdev, &s, target, x0, y0, mem);
  469. }
  470. /* Close the files if we just opened them. */
  471. if (opened_bfile && rs.page_bfile != 0)
  472. clist_fclose(rs.page_bfile, rs.page_bfname, false);
  473. if (opened_cfile && rs.page_cfile != 0)
  474. clist_fclose(rs.page_cfile, rs.page_cfname, false);
  475. return code;
  476. }