2 * Rotate an image 180° in O(log Dx + log Dy)
3 * draw calls, using an extra buffer the same size
6 * The basic concept is that you can invert an array by
7 * inverting the top half, inverting the bottom half, and
10 * This is usually overkill, but it speeds up slow remote
11 * connections quite a bit.
29 static void reverse(Image*, Image*, int);
30 static void shuffle(Image*, Image*, int, int, Image*, int, int);
31 static void writefile(char *name, Image *im, int gran);
32 static void halvemaskdim(Image*);
33 static void swapranges(Image*, Image*, int, int, int, int);
36 * Rotate the image 180° by reflecting first
37 * along the X axis, and then along the Y axis.
44 tmp = xallocimage(display, img->r, img->chan, 0, DNofill);
48 reverse(img, tmp, Xaxis);
49 reverse(img, tmp, Yaxis);
57 reverse(Image *img, Image *tmp, int axis)
64 * We start by swapping large chunks at a time.
65 * The chunk size should be the largest power of
66 * two that fits in the dimension.
68 d = axis==Xaxis ? Dx(img) : Dy(img);
69 for(i = 1; i*2 <= d; i *= 2)
72 r = axis==Xaxis ? Rect(0,0, i,100) : Rect(0,0, 100,i);
73 mask = xallocimage(display, r, GREY1, 1, DTransparent);
74 mtmp = xallocimage(display, r, GREY1, 1, DTransparent);
77 * Now color the bottom (or left) half of the mask opaque.
84 draw(mask, r, display->opaque, nil, ZP);
85 writefile("mask", mask, i);
88 * Shuffle will recur, shuffling the pieces as necessary
89 * and making the mask a finer and finer grating.
91 shuffle(img, tmp, axis, d, mask, i, 0);
97 * Shuffle the image by swapping pieces of size maskdim.
100 shuffle(Image *img, Image *tmp, int axis, int imgdim, Image *mask, int maskdim)
108 * Figure out how much will be left over that needs to be
109 * shifted specially to the bottom.
111 slop = imgdim % maskdim;
114 * Swap adjacent grating lines as per mask.
116 swapadjacent(img, tmp, axis, imgdim - slop, mask, maskdim);
119 * Calculate the mask with gratings half as wide and recur.
121 halvemaskdim(mask, maskdim, axis);
122 writefile("mask", mask, maskdim/2);
124 shuffle(img, tmp, axis, imgdim, mask, maskdim/2);
127 * Move the slop down to the bottom of the image.
129 swapranges(img, tmp, 0, imgdim-slop, imgdim, axis);
130 moveup(im, tmp, lastnn, nn, n, axis);
134 * Halve the grating period in the mask.
135 * The grating currently looks like
136 * ####____####____####____####____
137 * where #### is opacity.
140 * ##__##__##__##__##__##__##__##__
141 * which is achieved by shifting the mask
142 * and drawing on itself through itself.
143 * Draw doesn't actually allow this, so
144 * we have to copy it first.
146 * ####____####____####____####____ (dst)
147 * + ____####____####____####____#### (src)
148 * in __####____####____####____####__ (mask)
149 * ===========================================
150 * ##__##__##__##__##__##__##__##__
153 halvemaskdim(Image *m, int maskdim, int axis)
157 δ = axis==Xaxis ? Pt(maskdim,0) : Pt(0,maskdim);
158 draw(mtmp, mtmp->r, mask, nil, mask->r.min);
159 gendraw(mask, mask->r, mtmp, δ, mtmp, divpt(δ,2));
160 writefile("mask", mask, maskdim/2);
164 * Swap the regions [a,b] and [b,c]
167 swapranges(Image *img, Image *tmp, int a, int b, int c, int axis)
175 writefile("swap", img, 0);
176 draw(tmp, tmp->r, im, nil, im->r.min);
178 /* [a,a+(c-b)] gets [b,c] */
182 r.min.x = img->r.min.x + a;
183 r.max.x = img->r.min.x + a + (c-b);
186 r.min.y = img->r.min.y + a;
187 r.max.y = img->r.min.y + a + (c-b);
189 draw(img, r, tmp, nil, addpt(tmp->r.min, mulpt(δ, b)));
191 /* [a+(c-b), c] gets [a,b] */
194 r.min.x = img->r.min.x + a + (c-b);
195 r.max.x = img->r.min.x + c;
197 r.min.y = img->r.min.y + a + (c-b);
198 r.max.y = img->r.min.y + c;
200 draw(img, r, tmp, nil, addpt(tmp->r.min, mulpt(δ, a)));
201 writefile("swap", img, 1);
205 * Swap adjacent regions as specified by the grating.
206 * We do this by copying the image through the mask twice,
207 * once aligned with the grading and once 180° out of phase.
210 swapadjacent(Image *img, Image *tmp, int axis, int imgdim, Image *mask, int maskdim)
215 δ = axis==Xaxis ? Pt(1,0) : Pt(0,1);
230 * r0 is the lower rectangle, while r1 is the upper one.
232 draw(tmp, tmp->r, img, nil
236 interlace(Image *im, Image *tmp, int axis, int n, Image *mask, int gran)
247 p0 = (Point){gran, 0};
248 p1 = (Point){-gran, 0};
253 p0 = (Point){0, gran};
254 p1 = (Point){0, -gran};
258 draw(tmp, im->r, im, display->black, im->r.min);
259 gendraw(im, r0, tmp, p0, mask, mask->r.min);
260 gendraw(im, r0, tmp, p1, mask, p1);
265 writefile(char *name, Image *im, int gran)
271 snprint(buf, sizeof buf, "%d%s%d", c++, name, gran);
272 fd = create(buf, OWRITE, 0666);
275 writeimage(fd, im, 0);