2 * rotate an image 180° in O(log Dx + log Dy) /dev/draw writes,
3 * using an extra buffer same size as the image.
5 * the basic concept is that you can invert an array by inverting
6 * the top half, inverting the bottom half, and then swapping them.
7 * the code does this slightly backwards to ensure O(log n) runtime.
8 * (If you do it wrong, you can get O(log² n) runtime.)
10 * This is usually overkill, but it speeds up slow remote
11 * connections quite a bit.
30 writefile(char *name, Image *im, int gran)
36 snprint(buf, sizeof buf, "%d%s%d", c++, name, gran);
37 fd = create(buf, OWRITE, 0666);
40 writeimage(fd, im, 0);
45 moveup(Image *im, Image *tmp, int a, int b, int c, int axis)
54 drawop(tmp, tmp->r, im, nil, im->r.min, S);
58 range = Rect(a, im->r.min.y, c, im->r.max.y);
60 dr0.max.x = dr0.min.x+(c-b);
61 p0 = Pt(b, im->r.min.y);
64 dr1.min.x = dr1.max.x-(b-a);
65 p1 = Pt(a, im->r.min.y);
68 range = Rect(im->r.min.x, a, im->r.max.x, c);
70 dr0.max.y = dr0.min.y+(c-b);
71 p0 = Pt(im->r.min.x, b);
74 dr1.min.y = dr1.max.y-(b-a);
75 p1 = Pt(im->r.min.x, a);
78 drawop(im, dr0, tmp, nil, p0, S);
79 drawop(im, dr1, tmp, nil, p1, S);
83 interlace(Image *im, Image *tmp, int axis, int n, Image *mask, int gran)
94 p0 = (Point){gran, 0};
95 p1 = (Point){-gran, 0};
100 p0 = (Point){0, gran};
101 p1 = (Point){0, -gran};
105 drawop(tmp, im->r, im, display->opaque, im->r.min, S);
106 gendrawop(im, r0, tmp, p0, mask, mask->r.min, S);
107 gendrawop(im, r0, tmp, p1, mask, p1, S);
111 * Halve the grating period in the mask.
112 * The grating currently looks like
113 * ####____####____####____####____
114 * where #### is opacity.
117 * ##__##__##__##__##__##__##__##__
118 * which is achieved by shifting the mask
119 * and drawing on itself through itself.
120 * Draw doesn't actually allow this, so
121 * we have to copy it first.
123 * ####____####____####____####____ (dst)
124 * + ____####____####____####____#### (src)
125 * in __####____####____####____####__ (mask)
126 * ===========================================
127 * ##__##__##__##__##__##__##__##__
130 nextmask(Image *mask, int axis, int maskdim)
134 delta = axis==Xaxis ? Pt(maskdim,0) : Pt(0,maskdim);
135 drawop(mtmp, mtmp->r, mask, nil, mask->r.min, S);
136 gendrawop(mask, mask->r, mtmp, delta, mtmp, divpt(delta,-2), S);
137 // writefile("mask", mask, maskdim/2);
142 shuffle(Image *im, Image *tmp, int axis, int n, Image *mask, int gran,
152 interlace(im, tmp, axis, nn, mask, gran);
153 // writefile("interlace", im, gran);
155 gran = nextmask(mask, axis, gran);
156 shuffle(im, tmp, axis, n, mask, gran, nn);
157 // writefile("shuffle", im, gran);
158 moveup(im, tmp, lastnn, nn, n, axis);
159 // writefile("move", im, gran);
170 if(chantodepth(im->chan) < 8){
171 /* this speeds things up dramatically; draw is too slow on sub-byte pixel sizes */
172 tmp0 = xallocimage(display, im->r, CMAP8, 0, DNofill);
173 drawop(tmp0, tmp0->r, im, nil, im->r.min, S);
177 tmp = xallocimage(display, tmp0->r, tmp0->chan, 0, DNofill);
183 for(gran=1; gran<Dx(im->r); gran *= 2)
188 rmask.max = (Point){2*gran, 100};
190 mask = xallocimage(display, rmask, GREY1, 1, DTransparent);
191 mtmp = xallocimage(display, rmask, GREY1, 1, DTransparent);
192 if(mask == nil || mtmp == nil) {
193 fprint(2, "out of memory during rot180: %r\n");
197 drawop(mask, rmask, display->opaque, nil, ZP, S);
198 // writefile("mask", mask, gran);
199 shuffle(im, tmp, Xaxis, Dx(im->r), mask, gran, 0);
203 for(gran=1; gran<Dy(im->r); gran *= 2)
206 rmask.max = (Point){100, 2*gran};
207 mask = xallocimage(display, rmask, GREY1, 1, DTransparent);
208 mtmp = xallocimage(display, rmask, GREY1, 1, DTransparent);
209 if(mask == nil || mtmp == nil) {
210 fprint(2, "out of memory during rot180: %r\n");
214 drawop(mask, rmask, display->opaque, nil, ZP, S);
215 shuffle(im, tmp, Yaxis, Dy(im->r), mask, gran, 0);
223 /* rotates an image 90 degrees clockwise */
232 tmp = xallocimage(display, Rect(0, 0, dy, dx), im->chan, 0, DCyan);
234 fprint(2, "out of memory during rot90: %r\n");
238 for(j = 0; j < dx; j++) {
239 for(i = 0; i < dy; i++) {
240 drawop(tmp, Rect(i, j, i+1, j+1), im, nil, Pt(j, dy-(i+1)), S);
248 /* from resample.c -- resize from → to using interpolation */
251 #define K2 7 /* from -.7 to +.7 inclusive, meaning .2 into each adjacent pixel */
267 * i0(x) is the modified Bessel function, Σ (x/2)^2L / (L!)²
268 * There are faster ways to calculate this, but we precompute
269 * into a table so let's keep it simple.
279 v += pow(x/2., 2*L)/pow(fac(L), 2);
284 kaiser(double x, double tau, double alpha)
288 return i0(alpha*sqrt(1-(x*x/(tau*tau))))/i0(alpha);
292 resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
295 double X, xx, v, rat;
298 rat = (double)inx/(double)outx;
299 for(x=0; x<outx; x++){
301 /* don't resample if size unchanged */
302 out[off+x*d] = in[off+x*d];
307 for(k=-K2; k<=K2; k++){
314 v += in[off+i*d] * K[K2+k];
321 resampley(uchar **in, int off, int iny, uchar **out, int outy)
324 double Y, yy, v, rat;
326 rat = (double)iny/(double)outy;
327 for(y=0; y<outy; y++){
329 /* don't resample if size unchanged */
330 out[y][off] = in[y][off];
335 for(k=-K2; k<=K2; k++){
342 v += in[i][off] * K[K2+k];
350 resample(Image *from, Image *to)
352 int i, j, bpl, nchan;
353 uchar **oscan, **nscan;
360 for(i=-K2; i<=K2; i++){
361 K[K2+i] = kaiser(i/10., K2/10., 4.);
390 /* use library to convert to byte-per-chan form, then convert back */
391 t1 = xallocimage(display, Rect(0, 0, Dx(from->r), Dy(from->r)), tchan, 0, DNofill);
393 fprint(2, "out of memory for temp image 1 in resample: %r\n");
396 drawop(t1, t1->r, from, nil, ZP, S);
397 t2 = xallocimage(display, to->r, tchan, 0, DNofill);
399 fprint(2, "out of memory temp image 2 in resample: %r\n");
403 drawop(to, to->r, t2, nil, ZP, S);
409 sysfatal("can't handle channel type %s", chantostr(tmp, from->chan));
414 oscan = malloc(Dy(from->r)*sizeof(uchar*));
415 nscan = malloc(max(ysize, Dy(from->r))*sizeof(uchar*));
416 if(oscan == nil || nscan == nil)
417 sysfatal("can't allocate: %r");
419 /* unload original image into scan lines */
420 bpl = bytesperline(from->r, from->depth);
421 for(i=0; i<Dy(from->r); i++){
422 oscan[i] = malloc(bpl);
424 sysfatal("can't allocate: %r");
425 j = unloadimage(from, Rect(from->r.min.x, from->r.min.y+i, from->r.max.x, from->r.min.y+i+1), oscan[i], bpl);
427 sysfatal("unloadimage");
430 /* allocate scan lines for destination. we do y first, so need at least Dy(from->r) lines */
431 bpl = bytesperline(Rect(0, 0, xsize, Dy(from->r)), from->depth);
432 for(i=0; i<max(ysize, Dy(from->r)); i++){
433 nscan[i] = malloc(bpl);
435 sysfatal("can't allocate: %r");
439 nchan = from->depth/8;
440 for(i=0; i<Dy(from->r); i++){
441 for(j=0; j<nchan; j++){
442 if(j==0 && from->chan==XRGB32)
444 resamplex(oscan[i], j, nchan, Dx(from->r), nscan[i], xsize);
448 nscan[i] = malloc(bpl);
450 sysfatal("can't allocate: %r");
454 for(i=0; i<xsize; i++)
455 for(j=0; j<nchan; j++)
456 resampley(oscan, nchan*i+j, Dy(from->r), nscan, ysize);
458 /* pack data into destination */
459 bpl = bytesperline(to->r, from->depth);
460 for(i=0; i<ysize; i++){
461 j = loadimage(to, Rect(0, i, xsize, i+1), nscan[i], bpl);
463 sysfatal("loadimage: %r");
466 for(i=0; i<Dy(from->r); i++){