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.
28 static void reverse(Image*, Image*, int);
29 static void shuffle(Image*, Image*, int, int, Image*, int, int);
30 static void writefile(char *name, Image *im, int gran);
31 static void halvemaskdim(Image*);
32 static void swapranges(Image*, Image*, int, int, int, int);
35 * Rotate the image 180° by reflecting first
36 * along the X axis, and then along the Y axis.
43 tmp = xallocimage(display, img->r, img->chan, 0, DNofill);
47 reverse(img, tmp, Xaxis);
48 reverse(img, tmp, Yaxis);
56 reverse(Image *img, Image *tmp, int axis)
63 * We start by swapping large chunks at a time.
64 * The chunk size should be the largest power of
65 * two that fits in the dimension.
67 d = axis==Xaxis ? Dx(img) : Dy(img);
68 for(i = 1; i*2 <= d; i *= 2)
71 r = axis==Xaxis ? Rect(0,0, i,100) : Rect(0,0, 100,i);
72 mask = xallocimage(display, r, GREY1, 1, DTransparent);
73 mtmp = xallocimage(display, r, GREY1, 1, DTransparent);
76 * Now color the bottom (or left) half of the mask opaque.
83 draw(mask, r, display->opaque, nil, ZP);
84 writefile("mask", mask, i);
87 * Shuffle will recur, shuffling the pieces as necessary
88 * and making the mask a finer and finer grating.
90 shuffle(img, tmp, axis, d, mask, i, 0);
96 * Shuffle the image by swapping pieces of size maskdim.
99 shuffle(Image *img, Image *tmp, int axis, int imgdim, Image *mask, int maskdim)
107 * Figure out how much will be left over that needs to be
108 * shifted specially to the bottom.
110 slop = imgdim % maskdim;
113 * Swap adjacent grating lines as per mask.
115 swapadjacent(img, tmp, axis, imgdim - slop, mask, maskdim);
118 * Calculate the mask with gratings half as wide and recur.
120 halvemaskdim(mask, maskdim, axis);
121 writefile("mask", mask, maskdim/2);
123 shuffle(img, tmp, axis, imgdim, mask, maskdim/2);
126 * Move the slop down to the bottom of the image.
128 swapranges(img, tmp, 0, imgdim-slop, imgdim, axis);
129 moveup(im, tmp, lastnn, nn, n, axis);
133 * Halve the grating period in the mask.
134 * The grating currently looks like
135 * ####____####____####____####____
136 * where #### is opacity.
139 * ##__##__##__##__##__##__##__##__
140 * which is achieved by shifting the mask
141 * and drawing on itself through itself.
142 * Draw doesn't actually allow this, so
143 * we have to copy it first.
145 * ####____####____####____####____ (dst)
146 * + ____####____####____####____#### (src)
147 * in __####____####____####____####__ (mask)
148 * ===========================================
149 * ##__##__##__##__##__##__##__##__
152 halvemaskdim(Image *m, int maskdim, int axis)
156 δ = axis==Xaxis ? Pt(maskdim,0) : Pt(0,maskdim);
157 draw(mtmp, mtmp->r, mask, nil, mask->r.min);
158 gendraw(mask, mask->r, mtmp, δ, mtmp, divpt(δ,2));
159 writefile("mask", mask, maskdim/2);
163 * Swap the regions [a,b] and [b,c]
166 swapranges(Image *img, Image *tmp, int a, int b, int c, int axis)
174 writefile("swap", img, 0);
175 draw(tmp, tmp->r, im, nil, im->r.min);
177 /* [a,a+(c-b)] gets [b,c] */
181 r.min.x = img->r.min.x + a;
182 r.max.x = img->r.min.x + a + (c-b);
185 r.min.y = img->r.min.y + a;
186 r.max.y = img->r.min.y + a + (c-b);
188 draw(img, r, tmp, nil, addpt(tmp->r.min, mulpt(δ, b)));
190 /* [a+(c-b), c] gets [a,b] */
193 r.min.x = img->r.min.x + a + (c-b);
194 r.max.x = img->r.min.x + c;
196 r.min.y = img->r.min.y + a + (c-b);
197 r.max.y = img->r.min.y + c;
199 draw(img, r, tmp, nil, addpt(tmp->r.min, mulpt(δ, a)));
200 writefile("swap", img, 1);
204 * Swap adjacent regions as specified by the grating.
205 * We do this by copying the image through the mask twice,
206 * once aligned with the grading and once 180° out of phase.
209 swapadjacent(Image *img, Image *tmp, int axis, int imgdim, Image *mask, int maskdim)
214 δ = axis==Xaxis ? Pt(1,0) : Pt(0,1);
229 * r0 is the lower rectangle, while r1 is the upper one.
231 draw(tmp, tmp->r, img, nil,
235 interlace(Image *im, Image *tmp, int axis, int n, Image *mask, int gran)
246 p0 = (Point){gran, 0};
247 p1 = (Point){-gran, 0};
252 p0 = (Point){0, gran};
253 p1 = (Point){0, -gran};
257 draw(tmp, im->r, im, display->black, im->r.min);
258 gendraw(im, r0, tmp, p0, mask, mask->r.min);
259 gendraw(im, r0, tmp, p1, mask, p1);
264 writefile(char *name, Image *im, int gran)
270 snprint(buf, sizeof buf, "%d%s%d", c++, name, gran);
271 fd = create(buf, OWRITE, 0666);
274 writeimage(fd, im, 0);