aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/scat/qtree.c
blob: a3549c65ef45be1266a682310c5dfbc47ea8866b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include	<u.h>
#include	<libc.h>
#include	<bio.h>
#include	"sky.h"

static void	qtree_expand(Biobuf*, uchar*, int, int, uchar*);
static void	qtree_copy(uchar*, int, int, uchar*, int);
static void	qtree_bitins(uchar*, int, int, Pix*, int, int);
static void	read_bdirect(Biobuf*, Pix*, int, int, int, uchar*, int);

void
qtree_decode(Biobuf *infile, Pix *a, int n, int nqx, int nqy, int nbitplanes)
{
	int log2n, k, bit, b, nqmax;
	int nx,ny,nfx,nfy,c;
	int nqx2, nqy2;
	unsigned char *scratch;

	/*
	 * log2n is log2 of max(nqx,nqy) rounded up to next power of 2
	 */
	nqmax = nqy;
	if(nqx > nqmax)
		nqmax = nqx;
	log2n = log(nqmax)/LN2+0.5;
	if (nqmax > (1<<log2n))
		log2n++;

	/*
	 * allocate scratch array for working space
	 */
	nqx2 = (nqx+1)/2;
	nqy2 = (nqy+1)/2;
	scratch = (uchar*)malloc(nqx2*nqy2);
	if(scratch == nil) {
		fprint(2, "qtree_decode: insufficient memory\n");
		exits("memory");
	}

	/*
	 * now decode each bit plane, starting at the top
	 * A is assumed to be initialized to zero
	 */
	for(bit = nbitplanes-1; bit >= 0; bit--) {
		/*
		 * Was bitplane was quadtree-coded or written directly?
		 */
		b = input_nybble(infile);
		if(b == 0) {
			/*
			 * bit map was written directly
			 */
			read_bdirect(infile, a, n, nqx, nqy, scratch, bit);
		} else
		if(b != 0xf) {
			fprint(2, "qtree_decode: bad format code %x\n",b);
			exits("format");
		} else {
			/*
			 * bitmap was quadtree-coded, do log2n expansions
			 * read first code
			 */

			scratch[0] = input_huffman(infile);

			/*
			 * do log2n expansions, reading codes from file as necessary
			 */
			nx = 1;
			ny = 1;
			nfx = nqx;
			nfy = nqy;
			c = 1<<log2n;
			for(k = 1; k<log2n; k++) {
				/*
				 * this somewhat cryptic code generates the sequence
				 * n[k-1] = (n[k]+1)/2 where n[log2n]=nqx or nqy
				 */
				c = c>>1;
				nx = nx<<1;
				ny = ny<<1;
				if(nfx <= c)
					nx--;
				else
					nfx -= c;
				if(nfy <= c)
					ny--;
				else
					nfy -= c;
				qtree_expand(infile, scratch, nx, ny, scratch);
			}

			/*
			 * copy last set of 4-bit codes to bitplane bit of array a
			 */
			qtree_bitins(scratch, nqx, nqy, a, n, bit);
		}
	}
	free(scratch);
}

/*
 * do one quadtree expansion step on array a[(nqx+1)/2,(nqy+1)/2]
 * results put into b[nqx,nqy] (which may be the same as a)
 */
static
void
qtree_expand(Biobuf *infile, uchar *a, int nx, int ny, uchar *b)
{
	uchar *b1;

	/*
	 * first copy a to b, expanding each 4-bit value
	 */
	qtree_copy(a, nx, ny, b, ny);

	/*
	 * now read new 4-bit values into b for each non-zero element
	 */
	b1 = &b[nx*ny];
	while(b1 > b) {
		b1--;
		if(*b1 != 0)
			*b1 = input_huffman(infile);
	}
}

/*
 * copy 4-bit values from a[(nx+1)/2,(ny+1)/2] to b[nx,ny], expanding
 * each value to 2x2 pixels
 * a,b may be same array
 */
static
void
qtree_copy(uchar *a, int nx, int ny, uchar *b, int n)
{
	int i, j, k, nx2, ny2;
	int s00, s10;

	/*
	 * first copy 4-bit values to b
	 * start at end in case a,b are same array
	 */
	nx2 = (nx+1)/2;
	ny2 = (ny+1)/2;
	k = ny2*(nx2-1) + ny2-1;	/* k   is index of a[i,j] */
	for (i = nx2-1; i >= 0; i--) {
		s00 = 2*(n*i+ny2-1);	/* s00 is index of b[2*i,2*j] */
		for (j = ny2-1; j >= 0; j--) {
			b[s00] = a[k];
			k -= 1;
			s00 -= 2;
		}
	}

	/*
	 * now expand each 2x2 block
	 */
	for(i = 0; i<nx-1; i += 2) {
		s00 = n*i;				/* s00 is index of b[i,j] */
		s10 = s00+n;				/* s10 is index of b[i+1,j] */
		for(j = 0; j<ny-1; j += 2) {
			b[s10+1] =  b[s00]     & 1;
			b[s10  ] = (b[s00]>>1) & 1;
			b[s00+1] = (b[s00]>>2) & 1;
			b[s00  ] = (b[s00]>>3) & 1;
			s00 += 2;
			s10 += 2;
		}
		if(j < ny) {
			/*
			 * row size is odd, do last element in row
			 * s00+1, s10+1 are off edge
			 */
			b[s10  ] = (b[s00]>>1) & 1;
			b[s00  ] = (b[s00]>>3) & 1;
		}
	}
	if(i < nx) {
		/*
		 * column size is odd, do last row
		 * s10, s10+1 are off edge
		 */
		s00 = n*i;
		for (j = 0; j<ny-1; j += 2) {
			b[s00+1] = (b[s00]>>2) & 1;
			b[s00  ] = (b[s00]>>3) & 1;
			s00 += 2;
		}
		if(j < ny) {
			/*
			 * both row and column size are odd, do corner element
			 * s00+1, s10, s10+1 are off edge
			 */
			b[s00  ] = (b[s00]>>3) & 1;
		}
	}
}

/*
 * Copy 4-bit values from a[(nx+1)/2,(ny+1)/2] to b[nx,ny], expanding
 * each value to 2x2 pixels and inserting into bitplane BIT of B.
 * A,B may NOT be same array (it wouldn't make sense to be inserting
 * bits into the same array anyway.)
 */
static
void
qtree_bitins(uchar *a, int nx, int ny, Pix *b, int n, int bit)
{
	int i, j;
	Pix *s00, *s10;
	Pix px;

	/*
	 * expand each 2x2 block
	 */
	for(i=0; i<nx-1; i+=2) {
		s00 = &b[n*i];			/* s00 is index of b[i,j] */
		s10 = s00+n;			/* s10 is index of b[i+1,j] */
		for(j=0; j<ny-1; j+=2) {
			px = *a++;
			s10[1] |= ( px     & 1) << bit;
			s10[0] |= ((px>>1) & 1) << bit;
			s00[1] |= ((px>>2) & 1) << bit;
			s00[0] |= ((px>>3) & 1) << bit;
			s00 += 2;
			s10 += 2;
		}
		if(j < ny) {
			/*
			 * row size is odd, do last element in row
			 * s00+1, s10+1 are off edge
			 */
			px = *a++;
			s10[0] |= ((px>>1) & 1) << bit;
			s00[0] |= ((px>>3) & 1) << bit;
		}
	}
	if(i < nx) {
		/*
		 * column size is odd, do last row
		 * s10, s10+1 are off edge
		 */
		s00 = &b[n*i];
		for(j=0; j<ny-1; j+=2) {
			px = *a++;
			s00[1] |= ((px>>2) & 1) << bit;
			s00[0] |= ((px>>3) & 1) << bit;
			s00 += 2;
		}
		if(j < ny) {
			/*
			 * both row and column size are odd, do corner element
			 * s00+1, s10, s10+1 are off edge
			 */
			s00[0] |= ((*a>>3) & 1) << bit;
		}
	}
}

static
void
read_bdirect(Biobuf *infile, Pix *a, int n, int nqx, int nqy, uchar *scratch, int bit)
{
	int i;

	/*
	 * read bit image packed 4 pixels/nybble
	 */
	for(i = 0; i < ((nqx+1)/2) * ((nqy+1)/2); i++) {
		scratch[i] = input_nybble(infile);
	}

	/*
	 * insert in bitplane BIT of image A
	 */
	qtree_bitins(scratch, nqx, nqy, a, n, bit);
}