gdevmrun.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /* Copyright (C) 1995, 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: gdevmrun.c,v 1.2 2000/09/19 19:00:14 lpd Exp $ */
  16. /* Run-length encoded memory device */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gxdevice.h"
  21. #include "gdevmrun.h"
  22. /*
  23. * NOTE: THIS CODE HAS NOT BEEN TESTED. IF YOU WANT TO USE IT, PLEASE
  24. * TEST IT CAREFULLY AND REPORT ANY PROBLEMS.
  25. */
  26. /*
  27. * Define the representation of each run. We store runs in a doubly-linked
  28. * list. Run 0 is a dummy end-of-line run; run 1 is a dummy start-of-line
  29. * run. The dummy runs have length MAX_RUN_LENGTH to prevent merging.
  30. *
  31. * We limit the number of runs per line for two reasons: if there are many
  32. * runs, the run-length representation probably isn't buying us much; and
  33. * we need to allocate temporary space on the stack for the runs when we
  34. * expand a line to uncompressed form.
  35. */
  36. typedef gx_color_index run_value;
  37. typedef uint run_index;
  38. #define RUN_INDEX_BITS 10 /* see above */
  39. #define MAX_RUNS (1 << RUN_INDEX_BITS)
  40. #define MAX_RUN_INDEX (MAX_RUNS - 1)
  41. typedef uint run_length;
  42. #define RUN_LENGTH_BITS (32 - 2 * RUN_INDEX_BITS)
  43. #define MAX_RUN_LENGTH ((1 << RUN_LENGTH_BITS) - 1)
  44. typedef struct run_s {
  45. run_value value;
  46. run_length length : RUN_LENGTH_BITS;
  47. run_index next : RUN_INDEX_BITS;
  48. run_index prev : RUN_INDEX_BITS; /* 0 iff free run */
  49. } run;
  50. /*
  51. * Define a pointer into a run list.
  52. * For speed, we keep both the index of and the pointer to the current run.
  53. */
  54. typedef struct run_ptr_s {
  55. run *ptr;
  56. run_index index; /* index of current run */
  57. } run_ptr;
  58. typedef struct const_run_ptr_s {
  59. const run *ptr;
  60. run_index index; /* index of current run */
  61. } const_run_ptr;
  62. /* Accessors */
  63. #define RP_LENGTH(rp) ((rp).ptr->length)
  64. #define RP_VALUE(rp) ((rp).ptr->value)
  65. #define RP_NEXT(rp) ((rp).ptr->next)
  66. #define RP_PREV(rp) ((rp).ptr->prev)
  67. #define RL_DATA(line) ((run *)((line) + 1))
  68. #define CONST_RL_DATA(line) ((const run *)((line) + 1))
  69. #define RDEV_LINE(rdev, y) ((run_line *)scan_line_base(&(rdev)->md, y))
  70. /* Traversers */
  71. #define RP_AT_START(rp) ((rp).index == 1)
  72. #define RP_AT_END(rp) ((rp).index == 0)
  73. #define RP_TO_START(rp, data)\
  74. ((rp).index = (data)[1].next,\
  75. (rp).ptr = (data) + (rp).index)
  76. /* Note that RP_TO_NEXT and RP_TO_PREV allow rpn == rpc. */
  77. #define RP_TO_NEXT(rpc, data, rpn)\
  78. ((rpn).ptr = (data) + ((rpn).index = RP_NEXT(rpc)))
  79. #define RP_TO_PREV(rpc, data, rpp)\
  80. ((rpp).ptr = (data) + ((rpp).index = RP_PREV(rpc)))
  81. /*
  82. * Define the state of a single scan line.
  83. *
  84. * We maintain the following invariant: if two adjacent runs have the
  85. * same value, the sum of their lengths is greater than MAX_RUN_LENGTH.
  86. * This may miss optimality by nearly a factor of 2, but it's far easier
  87. * to maintain than a true optimal representation.
  88. *
  89. * For speed in the common case where nothing other than white is ever stored,
  90. * we initially don't bother to construct the runs (or the free run list)
  91. * for a line at all.
  92. */
  93. typedef struct run_line_s {
  94. gx_color_index zero; /* device white if line not initialized, */
  95. /* gx_no_color_index if initialized */
  96. uint xcur; /* x value at cursor position */
  97. run_ptr rpcur; /* cursor */
  98. run_index free; /* head of free list */
  99. } run_line;
  100. /* Insert/delete */
  101. private void
  102. rp_delete_next(run_ptr *prpc, run *data, run_line *line)
  103. {
  104. run_ptr rpn, rpn2;
  105. RP_TO_NEXT(*prpc, data, rpn);
  106. RP_TO_NEXT(rpn, data, rpn2);
  107. RP_NEXT(*prpc) = rpn2.index;
  108. RP_PREV(rpn2) = prpc->index;
  109. RP_NEXT(rpn) = line->free;
  110. RP_PREV(rpn) = 0;
  111. line->free = rpn.index;
  112. }
  113. private int
  114. rp_insert_next(run_ptr *prpc, run *data, run_line *line, run_ptr *prpn)
  115. {
  116. run_index new = line->free;
  117. run *prnew = data + new;
  118. if (new == 0)
  119. return -1;
  120. RP_TO_NEXT(*prpc, data, *prpn);
  121. RP_NEXT(*prpc) = new;
  122. RP_PREV(*prpn) = new;
  123. line->free = prnew->next;
  124. prnew->prev = prpc->index;
  125. prnew->next = prpn->index;
  126. prpn->index = new;
  127. prpn->ptr = prnew;
  128. return 0;
  129. }
  130. private int
  131. rp_insert_prev(run_ptr *prpc, run *data, run_line *line, run_ptr *prpp)
  132. {
  133. run_index new = line->free;
  134. run *prnew = data + new;
  135. if (new == 0)
  136. return -1;
  137. RP_TO_PREV(*prpc, data, *prpp);
  138. RP_NEXT(*prpp) = new;
  139. RP_PREV(*prpc) = new;
  140. line->free = prnew->next;
  141. prnew->prev = prpp->index;
  142. prnew->next = prpc->index;
  143. prpp->index = new;
  144. prpp->ptr = prnew;
  145. return 0;
  146. }
  147. /* Define the run-oriented device procedures. */
  148. private dev_proc_copy_mono(run_copy_mono);
  149. private dev_proc_copy_color(run_copy_color);
  150. private dev_proc_fill_rectangle(run_fill_rectangle);
  151. private dev_proc_copy_alpha(run_copy_alpha);
  152. private dev_proc_strip_tile_rectangle(run_strip_tile_rectangle);
  153. private dev_proc_strip_copy_rop(run_strip_copy_rop);
  154. private dev_proc_get_bits_rectangle(run_get_bits_rectangle);
  155. /*
  156. * Convert a memory device to run-length form. The mdev argument should be
  157. * const, but it isn't because we need to call gx_device_white.
  158. */
  159. int
  160. gdev_run_from_mem(gx_device_run *rdev, gx_device_memory *mdev)
  161. {
  162. int runs_per_line =
  163. (bitmap_raster(mdev->width * mdev->color_info.depth) -
  164. sizeof(run_line)) / sizeof(run);
  165. /*
  166. * We use the scan lines of the memory device for storing runs. We need
  167. * ceil(width / MAX_RUN_LENGTH) runs to represent a line where all
  168. * elements have the same value, +2 for the start and end runs.
  169. */
  170. int min_runs = (mdev->width + (MAX_RUN_LENGTH - 1)) / MAX_RUN_LENGTH + 2;
  171. int i;
  172. gx_color_index white = gx_device_white((gx_device *)mdev);
  173. rdev->md = *mdev;
  174. if (runs_per_line > MAX_RUNS)
  175. runs_per_line = MAX_RUNS;
  176. if (runs_per_line < min_runs)
  177. return 0; /* just use the memory device as-is */
  178. for (i = 0; i < mdev->height; ++i) {
  179. run_line *line = RDEV_LINE(rdev, i);
  180. line->zero = white;
  181. }
  182. rdev->runs_per_line = runs_per_line;
  183. rdev->umin = 0;
  184. rdev->umax1 = mdev->height;
  185. rdev->smin = mdev->height;
  186. rdev->smax1 = 0;
  187. /* Save and replace the representation-aware rendering procedures. */
  188. #define REPLACE(proc, rproc)\
  189. (rdev->save_procs.proc = dev_proc(&rdev->md, proc),\
  190. set_dev_proc(&rdev->md, proc, rproc))
  191. REPLACE(copy_mono, run_copy_mono);
  192. REPLACE(copy_color, run_copy_color);
  193. REPLACE(fill_rectangle, run_fill_rectangle);
  194. REPLACE(copy_alpha, run_copy_alpha);
  195. REPLACE(strip_tile_rectangle, run_strip_tile_rectangle);
  196. REPLACE(strip_copy_rop, run_strip_copy_rop);
  197. REPLACE(get_bits_rectangle, run_get_bits_rectangle);
  198. #undef REPLACE
  199. return 0;
  200. }
  201. /* Convert a scan line to expanded form in place. */
  202. private int
  203. run_expand(gx_device_run *rdev, int y)
  204. {
  205. const run_line *line = RDEV_LINE(rdev, y);
  206. const run *const data = CONST_RL_DATA(line);
  207. const_run_ptr rp;
  208. int n, x, w;
  209. #if RUN_LENGTH_BITS <= 8
  210. byte length[MAX_RUNS];
  211. #else
  212. # if RUN_LENGTH_BITS <= 16
  213. ushort length[MAX_RUNS];
  214. # else
  215. uint length[MAX_RUNS];
  216. # endif
  217. #endif
  218. gx_color_index value[MAX_RUNS];
  219. if (line->zero != gx_no_color_index) {
  220. rdev->save_procs.fill_rectangle((gx_device *)&rdev->md,
  221. 0, y, rdev->md.width, 1, line->zero);
  222. return 0;
  223. }
  224. /* Copy the runs into local storage to avoid stepping on our own toes. */
  225. for (n = 0, RP_TO_START(rp, data); !RP_AT_END(rp);
  226. ++n, RP_TO_NEXT(rp, data, rp)
  227. ) {
  228. length[n] = RP_LENGTH(rp);
  229. value[n] = RP_VALUE(rp);
  230. }
  231. for (x = 0, n = 0; x < rdev->md.width; x += w, ++n) {
  232. w = length[n];
  233. rdev->save_procs.fill_rectangle((gx_device *)&rdev->md,
  234. x, y, w, 1, value[n]);
  235. }
  236. return 0;
  237. }
  238. /*
  239. * Convert a range of scan lines to standard form.
  240. */
  241. private int
  242. run_standardize(gx_device_run *rdev, int y, int h)
  243. {
  244. int ye, iy;
  245. fit_fill_y(&rdev->md, y, h);
  246. fit_fill_h(&rdev->md, y, h);
  247. ye = y + h;
  248. if (y < rdev->smin) {
  249. if (ye > rdev->smax1)
  250. run_standardize(rdev, rdev->smax1, ye - rdev->smax1);
  251. if (ye < rdev->smin)
  252. ye = rdev->smin;
  253. rdev->smin = y;
  254. } else if (ye > rdev->smax1) {
  255. if (y > rdev->smax1)
  256. y = rdev->smax1;
  257. rdev->smax1 = ye;
  258. } else
  259. return 0;
  260. for (iy = y; iy < ye; ++iy)
  261. run_expand(rdev, iy);
  262. return 0;
  263. }
  264. /* Trampoline rendering procedures */
  265. private int
  266. run_copy_mono(gx_device * dev, const byte * data, int dx, int raster,
  267. gx_bitmap_id id, int x, int y, int w, int h,
  268. gx_color_index zero, gx_color_index one)
  269. {
  270. gx_device_run *const rdev = (gx_device_run *)dev;
  271. run_standardize(rdev, y, h);
  272. return rdev->save_procs.copy_mono((gx_device *)&rdev->md,
  273. data, dx, raster, id,
  274. x, y, w, h, zero, one);
  275. }
  276. private int
  277. run_copy_color(gx_device * dev, const byte * data,
  278. int data_x, int raster, gx_bitmap_id id,
  279. int x, int y, int w, int h)
  280. {
  281. gx_device_run *const rdev = (gx_device_run *)dev;
  282. run_standardize(rdev, y, h);
  283. return rdev->save_procs.copy_color((gx_device *)&rdev->md,
  284. data, data_x, raster, id,
  285. x, y, w, h);
  286. }
  287. private int
  288. run_copy_alpha(gx_device * dev, const byte * data, int data_x, int raster,
  289. gx_bitmap_id id, int x, int y, int w, int h,
  290. gx_color_index color, int depth)
  291. {
  292. gx_device_run *const rdev = (gx_device_run *)dev;
  293. run_standardize(rdev, y, h);
  294. return rdev->save_procs.copy_alpha((gx_device *)&rdev->md,
  295. data, data_x, raster, id,
  296. x, y, w, h, color, depth);
  297. }
  298. private int
  299. run_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
  300. int x, int y, int w, int h, gx_color_index color0, gx_color_index color1,
  301. int px, int py)
  302. {
  303. gx_device_run *const rdev = (gx_device_run *)dev;
  304. run_standardize(rdev, y, h);
  305. return rdev->save_procs.strip_tile_rectangle((gx_device *)&rdev->md,
  306. tiles, x, y, w, h,
  307. color0, color1, px, py);
  308. }
  309. private int
  310. run_strip_copy_rop(gx_device * dev, const byte * sdata, int sourcex,
  311. uint sraster, gx_bitmap_id id,
  312. const gx_color_index * scolors,
  313. const gx_strip_bitmap * textures,
  314. const gx_color_index * tcolors,
  315. int x, int y, int w, int h, int px, int py,
  316. gs_logical_operation_t lop)
  317. {
  318. gx_device_run *const rdev = (gx_device_run *)dev;
  319. run_standardize(rdev, y, h);
  320. return rdev->save_procs.strip_copy_rop((gx_device *)&rdev->md,
  321. sdata, sourcex, sraster,
  322. id, scolors, textures, tcolors,
  323. x, y, w, h, px, py, lop);
  324. }
  325. private int
  326. run_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
  327. gs_get_bits_params_t * params, gs_int_rect **unread)
  328. {
  329. gx_device_run *const rdev = (gx_device_run *)dev;
  330. run_standardize(rdev, prect->p.y, prect->q.y - prect->p.y);
  331. return rdev->save_procs.get_bits_rectangle((gx_device *)&rdev->md,
  332. prect, params, unread);
  333. }
  334. /* Finish initializing a line. This is a separate procedure only */
  335. /* for readability. */
  336. private void
  337. run_line_initialize(gx_device_run *rdev, int y)
  338. {
  339. run_line *line = RDEV_LINE(rdev, y);
  340. run *data = RL_DATA(line);
  341. int left = rdev->md.width;
  342. run_index index = 2;
  343. run *rcur;
  344. line->zero = gx_no_color_index;
  345. data[0].length = MAX_RUN_LENGTH; /* see above */
  346. data[0].value = gx_no_color_index; /* shouldn't matter */
  347. data[1].length = MAX_RUN_LENGTH;
  348. data[1].value = gx_no_color_index;
  349. data[1].next = 2;
  350. rcur = data + index;
  351. for (; left > 0; index++, rcur++, left -= MAX_RUN_LENGTH) {
  352. rcur->length = min(left, MAX_RUN_LENGTH);
  353. rcur->value = 0;
  354. rcur->prev = index - 1;
  355. rcur->next = index + 1;
  356. }
  357. rcur->next = 0;
  358. data[0].prev = index - 1;
  359. line->xcur = 0;
  360. line->rpcur.ptr = data + 2;
  361. line->rpcur.index = 2;
  362. line->free = index;
  363. for (; index < rdev->runs_per_line; ++index)
  364. data[index].next = index + 1;
  365. data[index - 1].next = 0;
  366. if (y >= rdev->umin && y < rdev->umax1) {
  367. if (y > (rdev->umin + rdev->umax1) >> 1)
  368. rdev->umax1 = y;
  369. else
  370. rdev->umin = y + 1;
  371. }
  372. }
  373. /*
  374. * Replace an interval of a line with a new value. This is the procedure
  375. * that does all the interesting work. We assume the line has been
  376. * initialized, and that 0 <= xo < xe <= dev->width.
  377. */
  378. private int
  379. run_fill_interval(run_line *line, int xo, int xe, run_value new)
  380. {
  381. run *data = RL_DATA(line);
  382. int xc = line->xcur;
  383. run_ptr rpc;
  384. int x0, x1;
  385. run_ptr rp0;
  386. int code;
  387. rpc = line->rpcur;
  388. /* Find the run that contains xo. */
  389. if (xo < xc) {
  390. while (xo < xc)
  391. RP_TO_PREV(rpc, data, rpc), xc -= RP_LENGTH(rpc);
  392. } else {
  393. while (xo >= xc + RP_LENGTH(rpc))
  394. xc += RP_LENGTH(rpc), RP_TO_NEXT(rpc, data, rpc);
  395. }
  396. /*
  397. * Skip runs above xo that already contain the new value.
  398. * If the entire interval already has the correct value, exit.
  399. * If we skip any such runs, set xo to just above them.
  400. */
  401. for (; !RP_AT_END(rpc) && RP_VALUE(rpc) == new;
  402. RP_TO_NEXT(rpc, data, rpc)
  403. )
  404. if ((xo = xc += RP_LENGTH(rpc)) >= xe)
  405. return 0;
  406. x0 = xc, rp0 = rpc;
  407. /* Find the run that contains xe-1. */
  408. while (xe > xc + RP_LENGTH(rpc))
  409. xc += RP_LENGTH(rpc), RP_TO_NEXT(rpc, data, rpc);
  410. /*
  411. * Skip runs below xe that already contain the new value.
  412. * (We know that some run between xo and xe doesn't.)
  413. * If we skip any such runs, set xe to just below them.
  414. */
  415. while (RP_TO_PREV(rpc, data, rpc), RP_VALUE(rpc) == new)
  416. xe = xc -= RP_LENGTH(rpc);
  417. RP_TO_NEXT(rpc, data, rpc);
  418. /*
  419. * At this point, we know the following:
  420. * x0 <= xo < x0 + RP_LENGTH(rp0).
  421. * RP_VALUE(rp0) != new.
  422. * xc <= xe-1 < xc + RP_LENGTH(rpc).
  423. * RP_VALUE(rpc) != new.
  424. * Note that rp0 and rpc may point to the same run.
  425. */
  426. /* Split off any unaffected prefix of the run at rp0. */
  427. if (x0 < xo) {
  428. uint diff = xo - x0;
  429. run_value v0 = RP_VALUE(rp0);
  430. run_ptr rpp;
  431. RP_TO_PREV(rp0, data, rpp);
  432. if (RP_VALUE(rpp) == v0 && RP_LENGTH(rpp) + diff <= MAX_RUN_LENGTH)
  433. RP_LENGTH(rpp) += diff;
  434. else {
  435. code = rp_insert_prev(&rp0, data, line, &rpp);
  436. if (code < 0)
  437. return code;
  438. RP_LENGTH(rpp) = diff;
  439. RP_VALUE(rpp) = v0;
  440. }
  441. RP_LENGTH(rp0) -= diff;
  442. }
  443. /* Split off any unaffected suffix of the run at rpc. */
  444. x1 = xc + RP_LENGTH(rpc);
  445. if (x1 > xe) {
  446. uint diff = x1 - xe;
  447. run_value vc = RP_VALUE(rpc);
  448. run_ptr rpn;
  449. RP_TO_NEXT(rpc, data, rpn);
  450. if (RP_VALUE(rpn) == vc && RP_LENGTH(rpn) + diff <= MAX_RUN_LENGTH)
  451. RP_LENGTH(rpn) += diff;
  452. else {
  453. code = rp_insert_next(&rpc, data, line, &rpn);
  454. if (code < 0)
  455. return code;
  456. RP_LENGTH(rpn) = diff;
  457. RP_VALUE(rpn) = vc;
  458. }
  459. RP_LENGTH(rpc) -= diff;
  460. }
  461. /* Delete all runs from rp0 through rpc. */
  462. RP_TO_PREV(rp0, data, rp0);
  463. while (RP_NEXT(rp0) != RP_NEXT(rpc))
  464. rp_delete_next(&rp0, data, line);
  465. /*
  466. * Finally, insert new runs with the new value.
  467. * We need to check for one boundary case, namely,
  468. * xo == x0 and the next lower run has the new value.
  469. * (There's probably a way to structure the code just slightly
  470. * differently to avoid this test.)
  471. */
  472. {
  473. uint left = xe - xo;
  474. if (xo == x0 && RP_VALUE(rp0) == new &&
  475. RP_LENGTH(rp0) + left <= MAX_RUN_LENGTH
  476. )
  477. RP_LENGTH(rp0) += left;
  478. else {
  479. /*
  480. * If we need more than one run, we divide up the length to
  481. * create more runs with length less than MAX_RUN_LENGTH in
  482. * order to improve the chances of a later merge. However,
  483. * we still guarantee that we won't create more runs than
  484. * the minimum number required to represent the length.
  485. */
  486. run_length len;
  487. if (left <= MAX_RUN_LENGTH)
  488. len = left;
  489. else {
  490. /*len = ceil(left / ceil(left / MAX_RUN_LENGTH))*/
  491. int pieces = left + (MAX_RUN_LENGTH - 1) / MAX_RUN_LENGTH;
  492. len = (left + pieces - 1) / pieces;
  493. }
  494. do {
  495. run_ptr rpn;
  496. /*
  497. * The allocation in rp_insert_next can't fail, because
  498. * we just deleted at least as many runs as we're going
  499. * to insert.
  500. */
  501. rp_insert_next(&rp0, data, line, &rpn);
  502. RP_LENGTH(rpn) = min(left, len);
  503. RP_VALUE(rpn) = new;
  504. }
  505. while ((left -= len) > 0);
  506. }
  507. }
  508. return 0;
  509. }
  510. /* Replace a rectangle with a new value. */
  511. private int
  512. run_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  513. gx_color_index color)
  514. {
  515. gx_device_run *const rdev = (gx_device_run *)dev;
  516. int xe, ye;
  517. int iy;
  518. fit_fill(dev, x, y, w, h);
  519. ye = y + h;
  520. /*
  521. * If the new value is white and the rectangle falls entirely within
  522. * the uninitialized region that we're keeping track of,
  523. * we can skip the entire operation.
  524. */
  525. if (y >= rdev->umin && ye <= rdev->umax1 &&
  526. color == RDEV_LINE(rdev, y)->zero
  527. )
  528. return 0;
  529. /*
  530. * Hand off any parts of the operation that fall within the area
  531. * already in standard form.
  532. */
  533. if (y < rdev->smax1 && ye > rdev->smin) {
  534. /* Some part of the operation must be handed off. */
  535. if (y < rdev->smin) {
  536. run_fill_rectangle(dev, x, y, w, rdev->smin - y, color);
  537. y = rdev->smin;
  538. }
  539. /* Now rdev->smin <= y < ye. */
  540. rdev->save_procs.fill_rectangle((gx_device *)&rdev->md,
  541. x, y, w, min(ye, rdev->smax1) - y,
  542. color);
  543. if (ye <= rdev->smax1)
  544. return 0;
  545. y = rdev->smax1;
  546. }
  547. xe = x + w;
  548. for (iy = y; iy < ye; ++iy) {
  549. run_line *line = RDEV_LINE(rdev, iy);
  550. if (color != line->zero) {
  551. if (line->zero != gx_no_color_index)
  552. run_line_initialize(rdev, iy);
  553. if (run_fill_interval(line, x, xe, color) < 0) {
  554. /* We ran out of runs. Convert to expanded form. */
  555. run_standardize(rdev, iy, 1);
  556. rdev->save_procs.fill_rectangle((gx_device *)&rdev->md,
  557. x, iy, w, 1, color);
  558. }
  559. }
  560. }
  561. return 0;
  562. }
  563. /* Debugging code */
  564. #ifdef DEBUG
  565. void
  566. debug_print_run(const run *data, run_index index, const char *prefix)
  567. {
  568. const run *pr = data + index;
  569. dlprintf6("%s%5d: length = %3d, value = 0x%lx, prev = %5u, next = %5u\n",
  570. prefix, index, pr->length, (ulong)pr->value, pr->prev, pr->next);
  571. }
  572. void
  573. debug_print_run_line(const run_line *line, const char *prefix)
  574. {
  575. const run *data = CONST_RL_DATA(line);
  576. dlprintf5("%sruns at 0x%lx: zero = 0x%lx, free = %u, xcur = %u,\n",
  577. prefix, (ulong)data, (ulong)line->zero, line->free, line->xcur);
  578. dlprintf3("%s rpcur = {ptr = 0x%lx, index = %u}\n",
  579. prefix, (ulong)line->rpcur.ptr, line->rpcur.index);
  580. {
  581. const_run_ptr rpc;
  582. RP_TO_START(rpc, data);
  583. while (!RP_AT_END(rpc)) {
  584. debug_print_run(data, rpc.index, prefix);
  585. RP_TO_NEXT(rpc, data, rpc);
  586. }
  587. }
  588. }
  589. #endif /* DEBUG */