gsbitcom.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* Copyright (C) 2000 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: gsbitcom.c,v 1.3 2002/02/21 22:24:52 giles Exp $ */
  14. /* Oversampled bitmap compression */
  15. #include "std.h"
  16. #include "gstypes.h"
  17. #include "gdebug.h"
  18. #include "gsbitops.h" /* for prototype */
  19. /*
  20. * Define a compile-time option to reverse nibble order in alpha maps.
  21. * Note that this does not reverse bit order within nibbles.
  22. * This option is here for a very specialized purpose and does not
  23. * interact well with the rest of the code.
  24. */
  25. #ifndef ALPHA_LSB_FIRST
  26. # define ALPHA_LSB_FIRST 0
  27. #endif
  28. /* Count the number of 1-bits in a half-byte. */
  29. static const byte half_byte_1s[16] = {
  30. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
  31. };
  32. /* Count the number of trailing 1s in an up-to-5-bit value, -1. */
  33. static const byte bits5_trailing_1s[32] = {
  34. 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3,
  35. 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 4
  36. };
  37. /* Count the number of leading 1s in an up-to-5-bit value, -1. */
  38. static const byte bits5_leading_1s[32] = {
  39. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  40. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 4
  41. };
  42. /*
  43. * Compress a value between 0 and 2^M to a value between 0 and 2^N-1.
  44. * Possible values of M are 1, 2, 3, or 4; of N are 1, 2, and 4.
  45. * The name of the table is compress_count_M_N.
  46. * As noted below, we require that N <= M.
  47. */
  48. static const byte compress_1_1[3] = {
  49. 0, 1, 1
  50. };
  51. static const byte compress_2_1[5] = {
  52. 0, 0, 1, 1, 1
  53. };
  54. static const byte compress_2_2[5] = {
  55. 0, 1, 2, 2, 3
  56. };
  57. static const byte compress_3_1[9] = {
  58. 0, 0, 0, 0, 1, 1, 1, 1, 1
  59. };
  60. static const byte compress_3_2[9] = {
  61. 0, 0, 1, 1, 2, 2, 2, 3, 3
  62. };
  63. static const byte compress_4_1[17] = {
  64. 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  65. };
  66. static const byte compress_4_2[17] = {
  67. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3
  68. };
  69. static const byte compress_4_4[17] = {
  70. 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15
  71. };
  72. /* The table of tables is indexed by log2(N) and then by M-1. */
  73. static const byte *const compress_tables[4][4] = {
  74. {compress_1_1, compress_2_1, compress_3_1, compress_4_1},
  75. {0, compress_2_2, compress_3_2, compress_4_2},
  76. {0, 0, 0, compress_4_4}
  77. };
  78. /*
  79. * Compress an XxY-oversampled bitmap to Nx1 by counting 1-bits. The X and
  80. * Y oversampling factors are 1, 2, or 4, but may be different. N, the
  81. * resulting number of (alpha) bits per pixel, may be 1, 2, or 4; we allow
  82. * compression in place, in which case N must not exceed the X oversampling
  83. * factor. Width and height are the source dimensions, and hence reflect
  84. * the oversampling; both are multiples of the relevant scale factor. The
  85. * same is true for srcx.
  86. */
  87. void
  88. bits_compress_scaled(const byte * src, int srcx, uint width, uint height,
  89. uint sraster, byte * dest, uint draster,
  90. const gs_log2_scale_point *plog2_scale, int log2_out_bits)
  91. {
  92. int log2_x = plog2_scale->x, log2_y = plog2_scale->y;
  93. int xscale = 1 << log2_x;
  94. int yscale = 1 << log2_y;
  95. int out_bits = 1 << log2_out_bits;
  96. /*
  97. * The following two initializations are only needed (and the variables
  98. * are only used) if out_bits <= xscale. We do them in all cases only
  99. * to suppress bogus "possibly uninitialized variable" warnings from
  100. * certain compilers.
  101. */
  102. int input_byte_out_bits = out_bits << (3 - log2_x);
  103. byte input_byte_out_mask = (1 << input_byte_out_bits) - 1;
  104. const byte *table =
  105. compress_tables[log2_out_bits][log2_x + log2_y - 1];
  106. uint sskip = sraster << log2_y;
  107. uint dwidth = (width >> log2_x) << log2_out_bits;
  108. uint dskip = draster - ((dwidth + 7) >> 3);
  109. uint mask = (1 << xscale) - 1;
  110. uint count_max = 1 << (log2_x + log2_y);
  111. /*
  112. * For the moment, we don't attempt to take advantage of the fact
  113. * that the input is aligned.
  114. */
  115. const byte *srow = src + (srcx >> 3);
  116. int in_shift_initial = 8 - xscale - (srcx & 7);
  117. int in_shift_check = (out_bits <= xscale ? 8 - xscale : -1);
  118. byte *d = dest;
  119. uint h;
  120. for (h = height; h; srow += sskip, h -= yscale) {
  121. const byte *s = srow;
  122. #if ALPHA_LSB_FIRST
  123. # define out_shift_initial 0
  124. # define out_shift_update(out_shift, nbits) ((out_shift += (nbits)) >= 8)
  125. #else
  126. # define out_shift_initial (8 - out_bits)
  127. # define out_shift_update(out_shift, nbits) ((out_shift -= (nbits)) < 0)
  128. #endif
  129. int out_shift = out_shift_initial;
  130. byte out = 0;
  131. int in_shift = in_shift_initial;
  132. int dw = 8 - (srcx & 7);
  133. int w;
  134. /* Loop over source bytes. */
  135. for (w = width; w > 0; w -= dw, dw = 8) {
  136. int index;
  137. int in_shift_final = (w >= dw ? 0 : dw - w);
  138. /*
  139. * Check quickly for all-0s or all-1s, but only if each
  140. * input byte generates no more than one output byte,
  141. * we're at an input byte boundary, and we're processing
  142. * an entire input byte (i.e., this isn't a final
  143. * partial byte.)
  144. */
  145. if (in_shift == in_shift_check && in_shift_final == 0)
  146. switch (*s) {
  147. case 0:
  148. for (index = sraster; index != sskip; index += sraster)
  149. if (s[index] != 0)
  150. goto p;
  151. if (out_shift_update(out_shift, input_byte_out_bits))
  152. *d++ = out, out_shift &= 7, out = 0;
  153. s++;
  154. continue;
  155. #if !ALPHA_LSB_FIRST /* too messy to make it work */
  156. case 0xff:
  157. for (index = sraster; index != sskip; index += sraster)
  158. if (s[index] != 0xff)
  159. goto p;
  160. {
  161. int shift =
  162. (out_shift -= input_byte_out_bits) + out_bits;
  163. if (shift > 0)
  164. out |= input_byte_out_mask << shift;
  165. else {
  166. out |= input_byte_out_mask >> -shift;
  167. *d++ = out;
  168. out_shift += 8;
  169. out = input_byte_out_mask << (8 + shift);
  170. }
  171. }
  172. s++;
  173. continue;
  174. #endif
  175. default:
  176. ;
  177. }
  178. p: /* Loop over source pixels within a byte. */
  179. do {
  180. uint count;
  181. for (index = 0, count = 0; index != sskip;
  182. index += sraster
  183. )
  184. count += half_byte_1s[(s[index] >> in_shift) & mask];
  185. if (count != 0 && table[count] == 0) { /* Look at adjacent cells to help prevent */
  186. /* dropouts. */
  187. uint orig_count = count;
  188. uint shifted_mask = mask << in_shift;
  189. byte in;
  190. if_debug3('B', "[B]count(%d,%d)=%d\n",
  191. (width - w) / xscale,
  192. (height - h) / yscale, count);
  193. if (yscale > 1) { /* Look at the next "lower" cell. */
  194. if (h < height && (in = s[0] & shifted_mask) != 0) {
  195. uint lower;
  196. for (index = 0, lower = 0;
  197. -(index -= sraster) <= sskip &&
  198. (in &= s[index]) != 0;
  199. )
  200. lower += half_byte_1s[in >> in_shift];
  201. if_debug1('B', "[B] lower adds %d\n",
  202. lower);
  203. if (lower <= orig_count)
  204. count += lower;
  205. }
  206. /* Look at the next "higher" cell. */
  207. if (h > yscale && (in = s[sskip - sraster] & shifted_mask) != 0) {
  208. uint upper;
  209. for (index = sskip, upper = 0;
  210. index < sskip << 1 &&
  211. (in &= s[index]) != 0;
  212. index += sraster
  213. )
  214. upper += half_byte_1s[in >> in_shift];
  215. if_debug1('B', "[B] upper adds %d\n",
  216. upper);
  217. if (upper < orig_count)
  218. count += upper;
  219. }
  220. }
  221. if (xscale > 1) {
  222. uint mask1 = (mask << 1) + 1;
  223. /* Look at the next cell to the left. */
  224. if (w < width) {
  225. int lshift = in_shift + xscale - 1;
  226. uint left;
  227. for (index = 0, left = 0;
  228. index < sskip; index += sraster
  229. ) {
  230. uint bits =
  231. ((s[index - 1] << 8) +
  232. s[index]) >> lshift;
  233. left += bits5_trailing_1s[bits & mask1];
  234. }
  235. if_debug1('B', "[B] left adds %d\n",
  236. left);
  237. if (left < orig_count)
  238. count += left;
  239. }
  240. /* Look at the next cell to the right. */
  241. if (w > xscale) {
  242. int rshift = in_shift - xscale + 8;
  243. uint right;
  244. for (index = 0, right = 0;
  245. index < sskip; index += sraster
  246. ) {
  247. uint bits =
  248. ((s[index] << 8) +
  249. s[index + 1]) >> rshift;
  250. right += bits5_leading_1s[(bits & mask1) << (4 - xscale)];
  251. }
  252. if_debug1('B', "[B] right adds %d\n",
  253. right);
  254. if (right <= orig_count)
  255. count += right;
  256. }
  257. }
  258. if (count > count_max)
  259. count = count_max;
  260. }
  261. out += table[count] << out_shift;
  262. if (out_shift_update(out_shift, out_bits))
  263. *d++ = out, out_shift &= 7, out = 0;
  264. }
  265. while ((in_shift -= xscale) >= in_shift_final);
  266. s++, in_shift += 8;
  267. }
  268. if (out_shift != out_shift_initial)
  269. *d++ = out;
  270. for (w = dskip; w != 0; w--)
  271. *d++ = 0;
  272. #undef out_shift_initial
  273. #undef out_shift_update
  274. }
  275. }