aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/page/nrotate.c
blob: 00d065ad8d6dda3d45c3a4131d9dc68805e60c49 (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
/*
 * Rotate an image 180° in O(log Dx + log Dy)
 * draw calls, using an extra buffer the same size
 * as the image.
 *
 * The basic concept is that you can invert an array by
 * inverting the top half, inverting the bottom half, and
 * then swapping them.
 *
 * This is usually overkill, but it speeds up slow remote
 * connections quite a bit.
 */

#include <u.h>
#include <libc.h>
#include <bio.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
#include "page.h"

int ndraw = 0;

enum {
	Xaxis,
	Yaxis,
};

static void reverse(Image*, Image*, int);
static void shuffle(Image*, Image*, int, int, Image*, int, int);
static void writefile(char *name, Image *im, int gran);
static void halvemaskdim(Image*);
static void swapranges(Image*, Image*, int, int, int, int);

/*
 * Rotate the image 180° by reflecting first
 * along the X axis, and then along the Y axis.
 */
void
rot180(Image *img)
{
	Image *tmp;

	tmp = xallocimage(display, img->r, img->chan, 0, DNofill);
	if(tmp == nil)
		return;

	reverse(img, tmp, Xaxis);
	reverse(img, tmp, Yaxis);

	freeimage(tmp);
}

Image *mtmp;

static void
reverse(Image *img, Image *tmp, int axis)
{
	Image *mask;
	Rectangle r;
	int i, d;

	/*
	 * We start by swapping large chunks at a time.
	 * The chunk size should be the largest power of
	 * two that fits in the dimension.
	 */
	d = axis==Xaxis ? Dx(img) : Dy(img);
	for(i = 1; i*2 <= d; i *= 2)
		;

	r = axis==Xaxis ? Rect(0,0, i,100) : Rect(0,0, 100,i);
	mask = xallocimage(display, r, GREY1, 1, DTransparent);
	mtmp = xallocimage(display, r, GREY1, 1, DTransparent);

	/*
	 * Now color the bottom (or left) half of the mask opaque.
	 */
	if(axis==Xaxis)
		r.max.x /= 2;
	else
		r.max.y /= 2;

	draw(mask, r, display->opaque, nil, ZP);
	writefile("mask", mask, i);

	/*
	 * Shuffle will recur, shuffling the pieces as necessary
	 * and making the mask a finer and finer grating.
	 */
	shuffle(img, tmp, axis, d, mask, i, 0);

	freeimage(mask);
}

/*
 * Shuffle the image by swapping pieces of size maskdim.
 */
static void
shuffle(Image *img, Image *tmp, int axis, int imgdim, Image *mask, int maskdim)
{
	int slop;

	if(maskdim == 0)
		return;

	/*
	 * Figure out how much will be left over that needs to be
	 * shifted specially to the bottom.
	 */
	slop = imgdim % maskdim;

	/*
	 * Swap adjacent grating lines as per mask.
	 */
	swapadjacent(img, tmp, axis, imgdim - slop, mask, maskdim);

	/*
	 * Calculate the mask with gratings half as wide and recur.
	 */
	halvemaskdim(mask, maskdim, axis);
	writefile("mask", mask, maskdim/2);

	shuffle(img, tmp, axis, imgdim, mask, maskdim/2);

	/*
	 * Move the slop down to the bottom of the image.
	 */
	swapranges(img, tmp, 0, imgdim-slop, imgdim, axis);
	moveup(im, tmp, lastnn, nn, n, axis);
}

/*
 * Halve the grating period in the mask.
 * The grating currently looks like
 * ####____####____####____####____
 * where #### is opacity.
 *
 * We want
 * ##__##__##__##__##__##__##__##__
 * which is achieved by shifting the mask
 * and drawing on itself through itself.
 * Draw doesn't actually allow this, so
 * we have to copy it first.
 *
 *     ####____####____####____####____ (dst)
 * +   ____####____####____####____#### (src)
 * in  __####____####____####____####__ (mask)
 * ===========================================
 *     ##__##__##__##__##__##__##__##__
 */
static void
halvemaskdim(Image *m, int maskdim, int axis)
{
	Point δ;

	δ = axis==Xaxis ? Pt(maskdim,0) : Pt(0,maskdim);
	draw(mtmp, mtmp->r, mask, nil, mask->r.min);
	gendraw(mask, mask->r, mtmp, δ, mtmp, divpt(δ,2));
	writefile("mask", mask, maskdim/2);
}

/*
 * Swap the regions [a,b] and [b,c]
 */
static void
swapranges(Image *img, Image *tmp, int a, int b, int c, int axis)
{
	Rectangle r;
	Point δ;

	if(a == b || b == c)
		return;

	writefile("swap", img, 0);
	draw(tmp, tmp->r, im, nil, im->r.min);

	/* [a,a+(c-b)] gets [b,c] */
	r = img->r;
	if(axis==Xaxis){
		δ = Pt(1,0);
		r.min.x = img->r.min.x + a;
		r.max.x = img->r.min.x + a + (c-b);
	}else{
		δ = Pt(0,1);
		r.min.y = img->r.min.y + a;
		r.max.y = img->r.min.y + a + (c-b);
	}
	draw(img, r, tmp, nil, addpt(tmp->r.min, mulpt(δ, b)));

	/* [a+(c-b), c] gets [a,b] */
	r = img->r;
	if(axis==Xaxis){
		r.min.x = img->r.min.x + a + (c-b);
		r.max.x = img->r.min.x + c;
	}else{
		r.min.y = img->r.min.y + a + (c-b);
		r.max.y = img->r.min.y + c;
	}
	draw(img, r, tmp, nil, addpt(tmp->r.min, mulpt(δ, a)));
	writefile("swap", img, 1);
}

/*
 * Swap adjacent regions as specified by the grating.
 * We do this by copying the image through the mask twice,
 * once aligned with the grading and once 180° out of phase.
 */
static void
swapadjacent(Image *img, Image *tmp, int axis, int imgdim, Image *mask, int maskdim)
{
	Point δ;
	Rectangle r0, r1;

	δ = axis==Xaxis ? Pt(1,0) : Pt(0,1);

	r0 = img->r;
	r1 = img->r;
	switch(axis){
	case Xaxis:
		r0.max.x = imgdim;
		r1.min.x = imgdim;
		break;
	case Yaxis:
		r0.max.y = imgdim;
		r1.min.y = imgdim;
	}

	/*
	 * r0 is the lower rectangle, while r1 is the upper one.
	 */
	draw(tmp, tmp->r, img, nil,
}

void
interlace(Image *im, Image *tmp, int axis, int n, Image *mask, int gran)
{
	Point p0, p1;
	Rectangle r0, r1;

	r0 = im->r;
	r1 = im->r;
	switch(axis) {
	case Xaxis:
		r0.max.x = n;
		r1.min.x = n;
		p0 = (Point){gran, 0};
		p1 = (Point){-gran, 0};
		break;
	case Yaxis:
		r0.max.y = n;
		r1.min.y = n;
		p0 = (Point){0, gran};
		p1 = (Point){0, -gran};
		break;
	}

	draw(tmp, im->r, im, display->black, im->r.min);
	gendraw(im, r0, tmp, p0, mask, mask->r.min);
	gendraw(im, r0, tmp, p1, mask, p1);
}


static void
writefile(char *name, Image *im, int gran)
{
	static int c = 100;
	int fd;
	char buf[200];

	snprint(buf, sizeof buf, "%d%s%d", c++, name, gran);
	fd = create(buf, OWRITE, 0666);
	if(fd < 0)
		return;
	writeimage(fd, im, 0);
	close(fd);
}