gxifast.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /* Copyright (C) 1989, 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: gxifast.c,v 1.4 2001/10/06 03:27:57 rayjj Exp $ */
  16. /* Fast monochrome image rendering */
  17. #include "gx.h"
  18. #include "memory_.h"
  19. #include "gpcheck.h"
  20. #include "gsbittab.h"
  21. #include "gserrors.h"
  22. #include "gxfixed.h"
  23. #include "gxarith.h"
  24. #include "gxmatrix.h"
  25. #include "gsccolor.h"
  26. #include "gspaint.h"
  27. #include "gsutil.h"
  28. #include "gxdevice.h"
  29. #include "gxcmap.h"
  30. #include "gxdcolor.h"
  31. #include "gxistate.h"
  32. #include "gxdevmem.h"
  33. #include "gdevmem.h" /* for mem_mono_device */
  34. #include "gxcpath.h"
  35. #include "gximage.h"
  36. #include "gzht.h"
  37. /* Conditionally include statistics code. */
  38. #ifdef DEBUG
  39. # define STATS
  40. #endif
  41. /* ------ Strategy procedure ------ */
  42. /* Check the prototype. */
  43. iclass_proc(gs_image_class_1_simple);
  44. /* Use special fast logic for portrait or landscape black-and-white images. */
  45. private irender_proc(image_render_skip);
  46. private irender_proc(image_render_simple);
  47. private irender_proc(image_render_landscape);
  48. irender_proc_t
  49. gs_image_class_1_simple(gx_image_enum * penum)
  50. {
  51. irender_proc_t rproc;
  52. fixed ox = dda_current(penum->dda.pixel0.x);
  53. fixed oy = dda_current(penum->dda.pixel0.y);
  54. if (penum->use_rop || penum->spp != 1 || penum->bps != 1)
  55. return 0;
  56. switch (penum->posture) {
  57. case image_portrait:
  58. { /* Use fast portrait algorithm. */
  59. long dev_width =
  60. fixed2long_pixround(ox + penum->x_extent.x) -
  61. fixed2long_pixround(ox);
  62. if (dev_width != penum->rect.w) {
  63. /*
  64. * Add an extra align_bitmap_mod of padding so that
  65. * we can align scaled rows with the device.
  66. */
  67. long line_size =
  68. bitmap_raster(any_abs(dev_width)) + align_bitmap_mod;
  69. if (penum->adjust != 0 || line_size > max_uint)
  70. return 0;
  71. /* Must buffer a scan line. */
  72. penum->line_width = any_abs(dev_width);
  73. penum->line_size = (uint) line_size;
  74. penum->line = gs_alloc_bytes(penum->memory,
  75. penum->line_size, "image line");
  76. if (penum->line == 0) {
  77. gx_default_end_image(penum->dev,
  78. (gx_image_enum_common_t *)penum,
  79. false);
  80. return 0;
  81. }
  82. }
  83. if_debug2('b', "[b]render=simple, unpack=copy; rect.w=%d, dev_width=%ld\n",
  84. penum->rect.w, dev_width);
  85. rproc = image_render_simple;
  86. break;
  87. }
  88. case image_landscape:
  89. { /* Use fast landscape algorithm. */
  90. long dev_width =
  91. fixed2long_pixround(oy + penum->x_extent.y) -
  92. fixed2long_pixround(oy);
  93. long line_size =
  94. (dev_width = any_abs(dev_width),
  95. bitmap_raster(dev_width) * 8 +
  96. ROUND_UP(dev_width, 8) * align_bitmap_mod);
  97. if ((dev_width != penum->rect.w && penum->adjust != 0) ||
  98. line_size > max_uint
  99. )
  100. return 0;
  101. /* Must buffer a group of 8N scan lines. */
  102. penum->line_width = dev_width;
  103. penum->line_size = (uint) line_size;
  104. penum->line = gs_alloc_bytes(penum->memory,
  105. penum->line_size, "image line");
  106. if (penum->line == 0) {
  107. gx_default_end_image(penum->dev,
  108. (gx_image_enum_common_t *) penum,
  109. false);
  110. return 0;
  111. }
  112. penum->xi_next = penum->line_xy = fixed2int_var_rounded(ox);
  113. if_debug3('b', "[b]render=landscape, unpack=copy; rect.w=%d, dev_width=%ld, line_size=%ld\n",
  114. penum->rect.w, dev_width, line_size);
  115. rproc = image_render_landscape;
  116. /* Precompute values needed for rasterizing. */
  117. penum->dxy =
  118. float2fixed(penum->matrix.xy +
  119. fixed2float(fixed_epsilon) / 2);
  120. break;
  121. }
  122. default:
  123. return 0;
  124. }
  125. /* Precompute values needed for rasterizing. */
  126. penum->dxx =
  127. float2fixed(penum->matrix.xx + fixed2float(fixed_epsilon) / 2);
  128. /*
  129. * We don't want to spread the samples, but we have to reset unpack_bps
  130. * to prevent the buffer pointer from being incremented by 8 bytes per
  131. * input byte.
  132. */
  133. penum->unpack = sample_unpack_copy;
  134. penum->unpack_bps = 8;
  135. if (penum->use_mask_color) {
  136. /*
  137. * Set the masked color as 'no_color' to make it transparent
  138. * according to the mask color range and the decoding.
  139. */
  140. penum->masked = true;
  141. if (penum->mask_color.values[0] == 1) {
  142. /* if v0 == 1, 1 is transparent since v1 must be == 1 to be a valid range */
  143. color_set_pure(penum->map[0].inverted ? &penum->icolor0 : &penum->icolor1,
  144. gx_no_color_index);
  145. } else if (penum->mask_color.values[1] == 0) {
  146. /* if v1 == 0, 0 is transparent since v0 must be == 0 to be a valid range */
  147. color_set_pure(penum->map[0].inverted ? &penum->icolor1 : &penum->icolor0,
  148. gx_no_color_index);
  149. } else {
  150. /*
  151. * The only other possible in-range value is v0 = 0, v1 = 1.
  152. * The image is completely transparent!
  153. */
  154. rproc = image_render_skip;
  155. }
  156. penum->map[0].decoding = sd_none;
  157. }
  158. return rproc;
  159. }
  160. /* ------ Rendering procedures ------ */
  161. #define DC_IS_NULL(pdc)\
  162. (gx_dc_is_pure(pdc) && (pdc)->colors.pure == gx_no_color_index)
  163. /* Skip over a completely transparent image. */
  164. private int
  165. image_render_skip(gx_image_enum * penum, const byte * buffer, int data_x,
  166. uint w, int h, gx_device * dev)
  167. {
  168. return h;
  169. }
  170. /*
  171. * Scale (and possibly reverse) one scan line of a monobit image.
  172. * This is used for both portrait and landscape image processing.
  173. * We pass in an x offset (0 <= line_x < align_bitmap_mod * 8) so that
  174. * we can align the result with the eventual device X.
  175. *
  176. * To be precise, the input to this routine is the w bits starting at
  177. * bit data_x in buffer. These w bits expand to abs(x_extent) bits,
  178. * either inverted (zero = 0xff) or not (zero = 0), starting at bit
  179. * line_x in line which corresponds to coordinate
  180. * fixed2int_pixround(xcur + min(x_extent, 0)). Note that the entire
  181. * bytes containing the first and last output bits are affected: the
  182. * other bits in those bytes are set to zero (i.e., the value of the
  183. * 'zero' argument).
  184. */
  185. #ifdef STATS
  186. struct stats_image_fast_s {
  187. long
  188. calls, all0s, all1s, runs, lbit0, byte00, byte01, byte02, byte03,
  189. byte04, rbit0, lbit1, byte1, rbit1, thin, thin2, nwide, bwide,
  190. nfill, bfill;
  191. } stats_image_fast;
  192. # define INCS(stat) ++stats_image_fast.stat
  193. # define ADDS(stat, n) stats_image_fast.stat += n
  194. #else
  195. # define INCS(stat) DO_NOTHING
  196. # define ADDS(stat, n) DO_NOTHING
  197. #endif
  198. inline private void
  199. fill_row(byte *line, int line_x, uint raster, int value)
  200. {
  201. memset(line + (line_x >> 3), value, raster - (line_x >> 3));
  202. }
  203. private void
  204. image_simple_expand(byte * line, int line_x, uint raster,
  205. const byte * buffer, int data_x, uint w,
  206. fixed xcur, fixed x_extent, byte zero /* 0 or 0xff */ )
  207. {
  208. int dbitx = data_x & 7;
  209. byte sbit = 0x80 >> dbitx;
  210. byte sbitmask = 0xff >> dbitx;
  211. uint wx = dbitx + w;
  212. gx_dda_fixed xl;
  213. gx_dda_step_fixed dxx4, dxx8, dxx16, dxx24, dxx32;
  214. register const byte *psrc = buffer + (data_x >> 3);
  215. /*
  216. * The following 3 variables define the end of the input data row.
  217. * We would put them in a struct, except that no compiler that we
  218. * know of will optimize individual struct members as though they
  219. * were simple variables (e.g., by putting them in registers).
  220. *
  221. * endp points to the byte that contains the bit just beyond the
  222. * end of the row. endx gives the bit number of this bit within
  223. * the byte, with 0 being the *least* significant bit. endbit is
  224. * a mask for this bit.
  225. */
  226. const byte *endp = psrc + (wx >> 3);
  227. int endx = ~wx & 7;
  228. byte endbit = 1 << endx;
  229. /*
  230. * The following 3 variables do the same for start of the last run
  231. * of the input row (think of it as a pointer to just beyond the
  232. * end of the next-to-last run).
  233. */
  234. const byte *stop = endp;
  235. int stopx;
  236. byte stopbit = endbit;
  237. byte data;
  238. byte one = ~zero;
  239. fixed xl0;
  240. if (w == 0)
  241. return;
  242. INCS(calls);
  243. /* Scan backward for the last transition. */
  244. if (stopbit == 0x80)
  245. --stop, stopbit = 1;
  246. else
  247. stopbit <<= 1;
  248. /* Now (stop, stopbit) give the last bit of the row. */
  249. {
  250. byte stopmask = -stopbit << 1;
  251. byte last = *stop;
  252. if (stop == psrc) /* only 1 input byte */
  253. stopmask &= sbitmask;
  254. if (last & stopbit) {
  255. /* The last bit is a 1: look for a 0-to-1 transition. */
  256. if (~last & stopmask) { /* Transition in last byte. */
  257. last |= stopbit - 1;
  258. } else { /* No transition in the last byte. */
  259. while (stop > psrc && stop[-1] == 0xff)
  260. --stop;
  261. if (stop == psrc ||
  262. (stop == psrc + 1 && !(~*psrc & sbitmask))
  263. ) {
  264. /* The input is all 1s. Clear the row and exit. */
  265. INCS(all1s);
  266. fill_row(line, line_x, raster, one);
  267. return;
  268. }
  269. last = *--stop;
  270. }
  271. stopx = byte_bit_run_length_0[byte_reverse_bits[last]] - 1;
  272. } else {
  273. /* The last bit is a 0: look for a 1-to-0 transition. */
  274. if (last & stopmask) { /* Transition in last byte. */
  275. last &= -stopbit;
  276. } else { /* No transition in the last byte. */
  277. while (stop > psrc && stop[-1] == 0)
  278. --stop;
  279. if (stop == psrc ||
  280. (stop == psrc + 1 && !(*psrc & sbitmask))
  281. ) {
  282. /* The input is all 0s. Clear the row and exit. */
  283. INCS(all0s);
  284. fill_row(line, line_x, raster, zero);
  285. return;
  286. }
  287. last = *--stop;
  288. }
  289. stopx = byte_bit_run_length_0[byte_reverse_bits[last ^ 0xff]] - 1;
  290. }
  291. if (stopx < 0)
  292. stopx = 7, ++stop;
  293. stopbit = 1 << stopx;
  294. }
  295. /* Pre-clear the row. */
  296. fill_row(line, line_x, raster, zero);
  297. /* Set up the DDAs. */
  298. xl0 =
  299. (x_extent >= 0 ?
  300. fixed_fraction(fixed_pre_pixround(xcur)) :
  301. fixed_fraction(fixed_pre_pixround(xcur + x_extent)) - x_extent);
  302. xl0 += int2fixed(line_x);
  303. dda_init(xl, xl0, x_extent, w);
  304. dxx4 = xl.step;
  305. dda_step_add(dxx4, xl.step);
  306. /* egcc - 2.91.66 generates incorrect code for
  307. * dda_step_add(dxx4, dxx4);
  308. * Using the temp variable.
  309. */
  310. dxx8 = dxx4;
  311. dda_step_add(dxx4, dxx8);
  312. dxx8 = dxx4;
  313. dda_step_add(dxx8, dxx4);
  314. dxx16 = dxx8;
  315. dda_step_add(dxx16, dxx8);
  316. dxx24 = dxx16;
  317. dda_step_add(dxx24, dxx8);
  318. dxx32 = dxx24;
  319. dda_step_add(dxx32, dxx8);
  320. /*
  321. * Loop invariants:
  322. * data = *psrc;
  323. * sbit = 1 << n, 0<=n<=7.
  324. */
  325. for (data = *psrc;;) {
  326. int x0, n, bit;
  327. byte *bp;
  328. static const byte lmasks[9] = {
  329. 0xff, 0x7f, 0x3f, 0x1f, 0xf, 7, 3, 1, 0
  330. };
  331. static const byte rmasks[9] = {
  332. 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
  333. };
  334. INCS(runs);
  335. /* Scan a run of zeros. */
  336. data ^= 0xff; /* invert */
  337. while (data & sbit) {
  338. dda_next(xl);
  339. sbit >>= 1;
  340. INCS(lbit0);
  341. }
  342. if (!sbit) { /* Scan a run of zero bytes. */
  343. sw: if ((data = psrc[1]) != 0) {
  344. psrc++;
  345. INCS(byte00);
  346. } else if ((data = psrc[2]) != 0) {
  347. dda_state_next(xl.state, dxx8);
  348. psrc += 2;
  349. INCS(byte01);
  350. } else if ((data = psrc[3]) != 0) {
  351. dda_state_next(xl.state, dxx16);
  352. psrc += 3;
  353. INCS(byte02);
  354. } else if ((data = psrc[4]) != 0) {
  355. dda_state_next(xl.state, dxx24);
  356. psrc += 4;
  357. INCS(byte03);
  358. } else {
  359. dda_state_next(xl.state, dxx32);
  360. psrc += 4;
  361. INCS(byte04);
  362. goto sw;
  363. }
  364. if (data > 0xf)
  365. sbit = 0x80;
  366. else {
  367. sbit = 0x08;
  368. dda_state_next(xl.state, dxx4);
  369. }
  370. data ^= 0xff; /* invert */
  371. while (data & sbit) {
  372. dda_next(xl);
  373. sbit >>= 1;
  374. INCS(rbit0);
  375. }
  376. }
  377. x0 = dda_current_fixed2int(xl);
  378. if (psrc >= stop && sbit == stopbit) {
  379. /*
  380. * We've scanned the last run of 0s.
  381. * Prepare to fill the final run of 1s.
  382. */
  383. n = fixed2int(xl0 + x_extent) - x0;
  384. } else { /* Scan a run of ones. */
  385. /* We know the current bit is a one. */
  386. data ^= 0xff; /* un-invert */
  387. do {
  388. dda_next(xl);
  389. sbit >>= 1;
  390. INCS(lbit1);
  391. }
  392. while (data & sbit);
  393. if (!sbit) { /* Scan a run of 0xff bytes. */
  394. while ((data = *++psrc) == 0xff) {
  395. dda_state_next(xl.state, dxx8);
  396. INCS(byte1);
  397. }
  398. if (data < 0xf0)
  399. sbit = 0x80;
  400. else {
  401. sbit = 0x08;
  402. dda_state_next(xl.state, dxx4);
  403. }
  404. while (data & sbit) {
  405. dda_next(xl);
  406. sbit >>= 1;
  407. INCS(rbit1);
  408. }
  409. }
  410. n = dda_current_fixed2int(xl) - x0;
  411. }
  412. /* Fill the run in the scan line. */
  413. if (n < 0)
  414. x0 += n, n = -n;
  415. bp = line + (x0 >> 3);
  416. bit = x0 & 7;
  417. if ((n += bit) <= 8) {
  418. *bp ^= lmasks[bit] - lmasks[n];
  419. INCS(thin);
  420. } else if ((n -= 8) <= 8) {
  421. *bp ^= lmasks[bit];
  422. bp[1] ^= rmasks[n];
  423. INCS(thin2);
  424. } else {
  425. *bp++ ^= lmasks[bit];
  426. if (n >= 56) {
  427. int nb = n >> 3;
  428. memset(bp, one, nb);
  429. bp += nb;
  430. INCS(nwide);
  431. ADDS(bwide, nb);
  432. } else {
  433. ADDS(bfill, n >> 3);
  434. while ((n -= 8) >= 0)
  435. *bp++ = one;
  436. INCS(nfill);
  437. }
  438. *bp ^= rmasks[n & 7];
  439. }
  440. if (psrc >= stop && sbit == stopbit)
  441. break;
  442. }
  443. }
  444. /* Copy one rendered scan line to the device. */
  445. private int
  446. copy_portrait(gx_image_enum * penum, const byte * data, int dx, int raster,
  447. int x, int y, int w, int h, gx_device * dev)
  448. {
  449. const gx_device_color *pdc0;
  450. const gx_device_color *pdc1;
  451. uint align = ALIGNMENT_MOD(data, align_bitmap_mod);
  452. /*
  453. * We know that the lookup table maps 1 bit to 1 bit,
  454. * so it can only have 2 states: straight-through or invert.
  455. */
  456. if (penum->map[0].table.lookup4x1to32[0])
  457. pdc0 = &penum->icolor1, pdc1 = &penum->icolor0;
  458. else
  459. pdc0 = &penum->icolor0, pdc1 = &penum->icolor1;
  460. data -= align;
  461. dx += align << 3;
  462. if (gx_dc_is_pure(pdc0) && gx_dc_is_pure(pdc1)) {
  463. /* Just use copy_mono. */
  464. dev_proc_copy_mono((*copy_mono)) =
  465. (h == 1 || (raster & (align_bitmap_mod - 1)) == 0 ?
  466. dev_proc(dev, copy_mono) : gx_copy_mono_unaligned);
  467. return (*copy_mono)
  468. (dev, data, dx, raster, gx_no_bitmap_id,
  469. x, y, w, h, pdc0->colors.pure, pdc1->colors.pure);
  470. }
  471. /*
  472. * At least one color isn't pure: if the other one is transparent, use
  473. * the opaque color's fill_masked procedure. Note that we use a
  474. * slightly unusual representation for transparent here (per
  475. * gx_begin_image1): a pure color with pixel value gx_no_color_index.
  476. */
  477. {
  478. const gx_device_color *pdc;
  479. bool invert;
  480. if (DC_IS_NULL(pdc1)) {
  481. pdc = pdc0;
  482. invert = true;
  483. } else {
  484. if (!DC_IS_NULL(pdc0)) {
  485. int code = gx_device_color_fill_rectangle
  486. (pdc0, x, y, w, h, dev, lop_default, NULL);
  487. if (code < 0)
  488. return code;
  489. }
  490. pdc = pdc1;
  491. invert = false;
  492. }
  493. return (*pdc->type->fill_masked)
  494. (pdc, data, dx, raster, gx_no_bitmap_id, x, y, w, h,
  495. dev, lop_default, invert);
  496. }
  497. }
  498. /* Rendering procedure for a monobit image with no */
  499. /* skew or rotation and pure colors. */
  500. private int
  501. image_render_simple(gx_image_enum * penum, const byte * buffer, int data_x,
  502. uint w, int h, gx_device * dev)
  503. {
  504. dev_proc_copy_mono((*copy_mono)) = dev_proc(dev, copy_mono);
  505. const fixed dxx = penum->dxx;
  506. const byte *line;
  507. uint line_width, line_size;
  508. int line_x;
  509. fixed xcur = dda_current(penum->dda.pixel0.x);
  510. int ix = fixed2int_pixround(xcur);
  511. int ixr;
  512. const int iy = penum->yci, ih = penum->hci;
  513. gx_device_color * const pdc0 = &penum->icolor0;
  514. gx_device_color * const pdc1 = &penum->icolor1;
  515. int dy;
  516. int code;
  517. if (h == 0)
  518. return 0;
  519. if ((!DC_IS_NULL(pdc0) &&
  520. (code = gx_color_load(pdc0, penum->pis, dev)) < 0) ||
  521. (!DC_IS_NULL(pdc1) &&
  522. (code = gx_color_load(pdc1, penum->pis, dev)) < 0)
  523. )
  524. return code;
  525. if (penum->line == 0) { /* A direct BitBlt is possible. */
  526. line = buffer;
  527. line_size = (w + 7) >> 3;
  528. line_width = w;
  529. line_x = 0;
  530. } else if (copy_mono == dev_proc(&mem_mono_device, copy_mono) &&
  531. dxx > 0 && gx_dc_is_pure(pdc1) && gx_dc_is_pure(pdc0) &&
  532. /* We know the colors must be (0,1) or (1,0). */
  533. (pdc0->colors.pure ^ pdc1->colors.pure) == 1 &&
  534. !penum->clip_image &&
  535. /*
  536. * Even if clip_image is false, the clipping rectangle
  537. * might lie partly outside the device coordinate space
  538. * if the Margins values are non-zero.
  539. */
  540. ix >= 0 &&
  541. (ixr = fixed2int_pixround(xcur + penum->x_extent.x) - 1) <
  542. dev->width &&
  543. iy >= 0 && iy + ih <= dev->height
  544. ) {
  545. /* Do the operation directly into the memory device bitmap. */
  546. int line_ix;
  547. int ib_left = ix >> 3, ib_right = ixr >> 3;
  548. byte *scan_line = scan_line_base((gx_device_memory *) dev, iy);
  549. byte save_left, save_right, mask;
  550. line_x = ix & (align_bitmap_mod * 8 - 1);
  551. line_ix = ix - line_x;
  552. line_size = (ixr >> 3) + 1 - (line_ix >> 3);
  553. line_width = ixr + 1 - ix;
  554. /* We must save and restore any unmodified bits in */
  555. /* the two edge bytes. */
  556. save_left = scan_line[ib_left];
  557. save_right = scan_line[ib_right];
  558. image_simple_expand(scan_line + (line_ix >> 3), line_x,
  559. line_size, buffer, data_x, w, xcur,
  560. penum->x_extent.x,
  561. ((pdc0->colors.pure == 0) !=
  562. (penum->map[0].table.lookup4x1to32[0] == 0) ?
  563. 0xff : 0));
  564. if (ix & 7)
  565. mask = (byte) (0xff00 >> (ix & 7)),
  566. scan_line[ib_left] =
  567. (save_left & mask) + (scan_line[ib_left] & ~mask);
  568. if ((ixr + 1) & 7)
  569. mask = (byte) (0xff00 >> ((ixr + 1) & 7)),
  570. scan_line[ib_right] =
  571. (scan_line[ib_right] & mask) + (save_right & ~mask);
  572. if (ih <= 1)
  573. return 1;
  574. /****** MAY BE UNALIGNED ******/
  575. line = scan_line + (line_ix >> 3);
  576. if (dxx < 0)
  577. ix -= line_width;
  578. for (dy = 1; dy < ih; dy++) {
  579. int code = (*copy_mono)
  580. (dev, line, line_x, line_size, gx_no_bitmap_id,
  581. ix, iy + dy, line_width, 1,
  582. (gx_color_index)0, (gx_color_index)1);
  583. if (code < 0)
  584. return code;
  585. }
  586. return 0;
  587. } else {
  588. line = penum->line;
  589. line_size = penum->line_size;
  590. line_width = penum->line_width;
  591. line_x = ix & (align_bitmap_mod * 8 - 1);
  592. image_simple_expand(penum->line, line_x, line_size,
  593. buffer, data_x, w, xcur,
  594. penum->x_extent.x, 0);
  595. }
  596. /* Finally, transfer the scan line to the device. */
  597. if (dxx < 0)
  598. ix -= line_width;
  599. for (dy = 0; dy < ih; dy++) {
  600. int code = copy_portrait(penum, line, line_x, line_size,
  601. ix, iy + dy, line_width, 1, dev);
  602. if (code < 0)
  603. return code;
  604. }
  605. return 1;
  606. }
  607. /* Rendering procedure for a 90 degree rotated monobit image */
  608. /* with pure colors. We buffer and then flip 8 scan lines at a time. */
  609. private int copy_landscape(P5(gx_image_enum *, int, int, bool, gx_device *));
  610. private int
  611. image_render_landscape(gx_image_enum * penum, const byte * buffer, int data_x,
  612. uint w, int h, gx_device * dev)
  613. {
  614. byte *line = penum->line;
  615. uint raster = bitmap_raster(penum->line_width);
  616. int ix = penum->xci, iw = penum->wci;
  617. int xinc, xmod;
  618. byte *row;
  619. const byte *orig_row = 0;
  620. bool y_neg = penum->dxy < 0;
  621. if (is_fneg(penum->matrix.yx))
  622. ix += iw, iw = -iw, xinc = -1;
  623. else
  624. xinc = 1;
  625. /*
  626. * Because of clipping, there may be discontinuous jumps in the values
  627. * of ix (xci). If this happens, or if we are at the end of the data or
  628. * a client has requested flushing, flush the flipping buffer.
  629. */
  630. if (ix != penum->xi_next || h == 0) {
  631. int xi = penum->xi_next;
  632. int code =
  633. (xinc > 0 ?
  634. copy_landscape(penum, penum->line_xy, xi, y_neg, dev) :
  635. copy_landscape(penum, xi, penum->line_xy, y_neg, dev));
  636. if (code < 0)
  637. return code;
  638. penum->line_xy = penum->xi_next = ix;
  639. if (h == 0)
  640. return code;
  641. }
  642. for (; iw != 0; iw -= xinc) {
  643. if (xinc < 0)
  644. --ix;
  645. xmod = ix & 7;
  646. row = line + xmod * raster;
  647. if (orig_row == 0) {
  648. image_simple_expand(row, 0, raster,
  649. buffer, data_x, w,
  650. dda_current(penum->dda.pixel0.y),
  651. penum->x_extent.y, 0);
  652. orig_row = row;
  653. } else
  654. memcpy(row, orig_row, raster);
  655. if (xinc > 0) {
  656. ++ix;
  657. if (xmod == 7) {
  658. int code =
  659. copy_landscape(penum, penum->line_xy, ix, y_neg, dev);
  660. if (code < 0)
  661. return code;
  662. orig_row = 0;
  663. penum->line_xy = ix;
  664. }
  665. } else {
  666. if (xmod == 0) {
  667. int code =
  668. copy_landscape(penum, ix, penum->line_xy, y_neg, dev);
  669. if (code < 0)
  670. return code;
  671. orig_row = 0;
  672. penum->line_xy = ix;
  673. }
  674. }
  675. }
  676. penum->xi_next = ix;
  677. return 0;
  678. }
  679. /* Flip and copy one group of scan lines. */
  680. private int
  681. copy_landscape(gx_image_enum * penum, int x0, int x1, bool y_neg,
  682. gx_device * dev)
  683. {
  684. byte *line = penum->line;
  685. uint line_width = penum->line_width;
  686. uint raster = bitmap_raster(line_width);
  687. byte *flipped = line + raster * 8;
  688. int w = x1 - x0;
  689. int y = fixed2int_pixround(dda_current(penum->dda.pixel0.y));
  690. if (w == 0 || line_width == 0)
  691. return 0;
  692. /* Flip the buffered data from raster x 8 to align_bitmap_mod x */
  693. /* line_width. */
  694. if (line_width > 0) {
  695. int i;
  696. for (i = (line_width - 1) >> 3; i >= 0; --i)
  697. memflip8x8(line + i, raster,
  698. flipped + (i << (log2_align_bitmap_mod + 3)),
  699. align_bitmap_mod);
  700. }
  701. /* Transfer the scan lines to the device. */
  702. if (w < 0)
  703. x0 = x1, w = -w;
  704. if (y_neg)
  705. y -= line_width;
  706. return copy_portrait(penum, flipped, x0 & 7, align_bitmap_mod,
  707. x0, y, w, line_width, dev);
  708. }