qtree.c 5.7 KB

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