qtree.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "sky.h"
  13. static void qtree_expand(Biobuf*, uint8_t*, int, int, uint8_t*);
  14. static void qtree_copy(uint8_t*, int, int, uint8_t*, int);
  15. static void qtree_bitins(uint8_t*, int, int, Pix*, int, int);
  16. static void read_bdirect(Biobuf*, Pix*, int, int, int, uint8_t*, int);
  17. void
  18. qtree_decode(Biobuf *infile, Pix *a, int n, int nqx, int nqy, int nbitplanes)
  19. {
  20. int log2n, k, bit, b, nqmax;
  21. int nx,ny,nfx,nfy,c;
  22. int nqx2, nqy2;
  23. unsigned char *scratch;
  24. /*
  25. * log2n is log2 of max(nqx,nqy) rounded up to next power of 2
  26. */
  27. nqmax = nqy;
  28. if(nqx > nqmax)
  29. nqmax = nqx;
  30. log2n = log(nqmax)/LN2+0.5;
  31. if (nqmax > (1<<log2n))
  32. log2n++;
  33. /*
  34. * allocate scratch array for working space
  35. */
  36. nqx2 = (nqx+1)/2;
  37. nqy2 = (nqy+1)/2;
  38. scratch = (uint8_t*)malloc(nqx2*nqy2);
  39. if(scratch == nil) {
  40. fprint(2, "qtree_decode: insufficient memory\n");
  41. exits("memory");
  42. }
  43. /*
  44. * now decode each bit plane, starting at the top
  45. * A is assumed to be initialized to zero
  46. */
  47. for(bit = nbitplanes-1; bit >= 0; bit--) {
  48. /*
  49. * Was bitplane was quadtree-coded or written directly?
  50. */
  51. b = input_nybble(infile);
  52. if(b == 0) {
  53. /*
  54. * bit map was written directly
  55. */
  56. read_bdirect(infile, a, n, nqx, nqy, scratch, bit);
  57. } else
  58. if(b != 0xf) {
  59. fprint(2, "qtree_decode: bad format code %x\n",b);
  60. exits("format");
  61. } else {
  62. /*
  63. * bitmap was quadtree-coded, do log2n expansions
  64. * read first code
  65. */
  66. scratch[0] = input_huffman(infile);
  67. /*
  68. * do log2n expansions, reading codes from file as necessary
  69. */
  70. nx = 1;
  71. ny = 1;
  72. nfx = nqx;
  73. nfy = nqy;
  74. c = 1<<log2n;
  75. for(k = 1; k<log2n; k++) {
  76. /*
  77. * this somewhat cryptic code generates the sequence
  78. * n[k-1] = (n[k]+1)/2 where n[log2n]=nqx or nqy
  79. */
  80. c = c>>1;
  81. nx = nx<<1;
  82. ny = ny<<1;
  83. if(nfx <= c)
  84. nx--;
  85. else
  86. nfx -= c;
  87. if(nfy <= c)
  88. ny--;
  89. else
  90. nfy -= c;
  91. qtree_expand(infile, scratch, nx, ny, scratch);
  92. }
  93. /*
  94. * copy last set of 4-bit codes to bitplane bit of array a
  95. */
  96. qtree_bitins(scratch, nqx, nqy, a, n, bit);
  97. }
  98. }
  99. free(scratch);
  100. }
  101. /*
  102. * do one quadtree expansion step on array a[(nqx+1)/2,(nqy+1)/2]
  103. * results put into b[nqx,nqy] (which may be the same as a)
  104. */
  105. static
  106. void
  107. qtree_expand(Biobuf *infile, uint8_t *a, int nx, int ny, uint8_t *b)
  108. {
  109. uint8_t *b1;
  110. /*
  111. * first copy a to b, expanding each 4-bit value
  112. */
  113. qtree_copy(a, nx, ny, b, ny);
  114. /*
  115. * now read new 4-bit values into b for each non-zero element
  116. */
  117. b1 = &b[nx*ny];
  118. while(b1 > b) {
  119. b1--;
  120. if(*b1 != 0)
  121. *b1 = input_huffman(infile);
  122. }
  123. }
  124. /*
  125. * copy 4-bit values from a[(nx+1)/2,(ny+1)/2] to b[nx,ny], expanding
  126. * each value to 2x2 pixels
  127. * a,b may be same array
  128. */
  129. static
  130. void
  131. qtree_copy(uint8_t *a, int nx, int ny, uint8_t *b, int n)
  132. {
  133. int i, j, k, nx2, ny2;
  134. int s00, s10;
  135. /*
  136. * first copy 4-bit values to b
  137. * start at end in case a,b are same array
  138. */
  139. nx2 = (nx+1)/2;
  140. ny2 = (ny+1)/2;
  141. k = ny2*(nx2-1) + ny2-1; /* k is index of a[i,j] */
  142. for (i = nx2-1; i >= 0; i--) {
  143. s00 = 2*(n*i+ny2-1); /* s00 is index of b[2*i,2*j] */
  144. for (j = ny2-1; j >= 0; j--) {
  145. b[s00] = a[k];
  146. k -= 1;
  147. s00 -= 2;
  148. }
  149. }
  150. /*
  151. * now expand each 2x2 block
  152. */
  153. for(i = 0; i<nx-1; i += 2) {
  154. s00 = n*i; /* s00 is index of b[i,j] */
  155. s10 = s00+n; /* s10 is index of b[i+1,j] */
  156. for(j = 0; j<ny-1; j += 2) {
  157. b[s10+1] = b[s00] & 1;
  158. b[s10 ] = (b[s00]>>1) & 1;
  159. b[s00+1] = (b[s00]>>2) & 1;
  160. b[s00 ] = (b[s00]>>3) & 1;
  161. s00 += 2;
  162. s10 += 2;
  163. }
  164. if(j < ny) {
  165. /*
  166. * row size is odd, do last element in row
  167. * s00+1, s10+1 are off edge
  168. */
  169. b[s10 ] = (b[s00]>>1) & 1;
  170. b[s00 ] = (b[s00]>>3) & 1;
  171. }
  172. }
  173. if(i < nx) {
  174. /*
  175. * column size is odd, do last row
  176. * s10, s10+1 are off edge
  177. */
  178. s00 = n*i;
  179. for (j = 0; j<ny-1; j += 2) {
  180. b[s00+1] = (b[s00]>>2) & 1;
  181. b[s00 ] = (b[s00]>>3) & 1;
  182. s00 += 2;
  183. }
  184. if(j < ny) {
  185. /*
  186. * both row and column size are odd, do corner element
  187. * s00+1, s10, s10+1 are off edge
  188. */
  189. b[s00 ] = (b[s00]>>3) & 1;
  190. }
  191. }
  192. }
  193. /*
  194. * Copy 4-bit values from a[(nx+1)/2,(ny+1)/2] to b[nx,ny], expanding
  195. * each value to 2x2 pixels and inserting into bitplane BIT of B.
  196. * A,B may NOT be same array (it wouldn't make sense to be inserting
  197. * bits into the same array anyway.)
  198. */
  199. static
  200. void
  201. qtree_bitins(uint8_t *a, int nx, int ny, Pix *b, int n, int bit)
  202. {
  203. int i, j;
  204. Pix *s00, *s10;
  205. Pix px;
  206. /*
  207. * expand each 2x2 block
  208. */
  209. for(i=0; i<nx-1; i+=2) {
  210. s00 = &b[n*i]; /* s00 is index of b[i,j] */
  211. s10 = s00+n; /* s10 is index of b[i+1,j] */
  212. for(j=0; j<ny-1; j+=2) {
  213. px = *a++;
  214. s10[1] |= ( px & 1) << bit;
  215. s10[0] |= ((px>>1) & 1) << bit;
  216. s00[1] |= ((px>>2) & 1) << bit;
  217. s00[0] |= ((px>>3) & 1) << bit;
  218. s00 += 2;
  219. s10 += 2;
  220. }
  221. if(j < ny) {
  222. /*
  223. * row size is odd, do last element in row
  224. * s00+1, s10+1 are off edge
  225. */
  226. px = *a++;
  227. s10[0] |= ((px>>1) & 1) << bit;
  228. s00[0] |= ((px>>3) & 1) << bit;
  229. }
  230. }
  231. if(i < nx) {
  232. /*
  233. * column size is odd, do last row
  234. * s10, s10+1 are off edge
  235. */
  236. s00 = &b[n*i];
  237. for(j=0; j<ny-1; j+=2) {
  238. px = *a++;
  239. s00[1] |= ((px>>2) & 1) << bit;
  240. s00[0] |= ((px>>3) & 1) << bit;
  241. s00 += 2;
  242. }
  243. if(j < ny) {
  244. /*
  245. * both row and column size are odd, do corner element
  246. * s00+1, s10, s10+1 are off edge
  247. */
  248. s00[0] |= ((*a>>3) & 1) << bit;
  249. }
  250. }
  251. }
  252. static
  253. void
  254. read_bdirect(Biobuf *infile, Pix *a, int n, int nqx, int nqy,
  255. uint8_t *scratch, int bit)
  256. {
  257. int i;
  258. /*
  259. * read bit image packed 4 pixels/nybble
  260. */
  261. for(i = 0; i < ((nqx+1)/2) * ((nqy+1)/2); i++) {
  262. scratch[i] = input_nybble(infile);
  263. }
  264. /*
  265. * insert in bitplane BIT of image A
  266. */
  267. qtree_bitins(scratch, nqx, nqy, a, n, bit);
  268. }