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.
31 writefile(char *name, Image *im, int gran)
37 snprint(buf, sizeof buf, "%d%s%d", c++, name, gran);
38 fd = create(buf, OWRITE, 0666);
41 writeimage(fd, im, 0);
46 moveup(Image *im, Image *tmp, int a, int b, int c, int axis)
55 drawop(tmp, tmp->r, im, nil, im->r.min, S);
59 range = Rect(a, im->r.min.y, c, im->r.max.y);
61 dr0.max.x = dr0.min.x+(c-b);
62 p0 = Pt(b, im->r.min.y);
65 dr1.min.x = dr1.max.x-(b-a);
66 p1 = Pt(a, im->r.min.y);
70 range = Rect(im->r.min.x, a, im->r.max.x, c);
72 dr0.max.y = dr0.min.y+(c-b);
73 p0 = Pt(im->r.min.x, b);
76 dr1.min.y = dr1.max.y-(b-a);
77 p1 = Pt(im->r.min.x, a);
80 drawop(im, dr0, tmp, nil, p0, S);
81 drawop(im, dr1, tmp, nil, p1, S);
85 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 o = 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, o, mtmp, divpt(o,-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 /* rotates an image 270 degrees clockwise */
257 tmp = xallocimage(display, Rect(0, 0, dy, dx), im->chan, 0, DCyan);
259 fprint(2, "out of memory during rot270: %r\n");
263 for(i = 0; i < dy; i++) {
264 for(j = 0; j < dx; j++) {
265 drawop(tmp, Rect(i, j, i+1, j+1), im, nil, Pt(dx-(j+1), i), S);
273 /* from resample.c -- resize from → to using interpolation */
276 #define K2 7 /* from -.7 to +.7 inclusive, meaning .2 into each adjacent pixel */
292 * i0(x) is the modified Bessel function, Σ (x/2)^2L / (L!)²
293 * There are faster ways to calculate this, but we precompute
294 * into a table so let's keep it simple.
304 v += pow(x/2., 2*L)/pow(fac(L), 2);
309 kaiser(double x, double t, double a)
313 return i0(a*sqrt(1-(x*x/(t*t))))/i0(a);
318 resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
321 double X, xx, v, rat;
324 rat = (double)inx/(double)outx;
325 for(x=0; x<outx; x++){
327 /* don't resample if size unchanged */
328 out[off+x*d] = in[off+x*d];
333 for(k=-K2; k<=K2; k++){
340 v += in[off+i*d] * K[K2+k];
347 resampley(uchar **in, int off, int iny, uchar **out, int outy)
350 double Y, yy, v, rat;
352 rat = (double)iny/(double)outy;
353 for(y=0; y<outy; y++){
355 /* don't resample if size unchanged */
356 out[y][off] = in[y][off];
361 for(k=-K2; k<=K2; k++){
368 v += in[i][off] * K[K2+k];
376 resample(Image *from, Image *to)
378 int i, j, bpl, nchan;
379 uchar **oscan, **nscan;
386 for(i=-K2; i<=K2; i++){
387 K[K2+i] = kaiser(i/10., K2/10., 4.);
416 /* use library to convert to byte-per-chan form, then convert back */
417 t1 = xallocimage(display, Rect(0, 0, Dx(from->r), Dy(from->r)), tchan, 0, DNofill);
419 fprint(2, "out of memory for temp image 1 in resample: %r\n");
422 drawop(t1, t1->r, from, nil, ZP, S);
423 t2 = xallocimage(display, to->r, tchan, 0, DNofill);
425 fprint(2, "out of memory temp image 2 in resample: %r\n");
429 drawop(to, to->r, t2, nil, ZP, S);
435 sysfatal("can't handle channel type %s", chantostr(tmp, from->chan));
440 oscan = malloc(Dy(from->r)*sizeof(uchar*));
441 nscan = malloc(max(ysize, Dy(from->r))*sizeof(uchar*));
442 if(oscan == nil || nscan == nil)
443 sysfatal("can't allocate: %r");
445 /* unload original image into scan lines */
446 bpl = bytesperline(from->r, from->depth);
447 for(i=0; i<Dy(from->r); i++){
448 oscan[i] = malloc(bpl);
450 sysfatal("can't allocate: %r");
451 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);
453 sysfatal("unloadimage");
456 /* allocate scan lines for destination. we do y first, so need at least Dy(from->r) lines */
457 bpl = bytesperline(Rect(0, 0, xsize, Dy(from->r)), from->depth);
458 for(i=0; i<max(ysize, Dy(from->r)); i++){
459 nscan[i] = malloc(bpl);
461 sysfatal("can't allocate: %r");
465 nchan = from->depth/8;
466 for(i=0; i<Dy(from->r); i++){
467 for(j=0; j<nchan; j++){
468 if(j==0 && from->chan==XRGB32)
470 resamplex(oscan[i], j, nchan, Dx(from->r), nscan[i], xsize);
474 nscan[i] = malloc(bpl);
476 sysfatal("can't allocate: %r");
480 for(i=0; i<xsize; i++)
481 for(j=0; j<nchan; j++)
482 resampley(oscan, nchan*i+j, Dy(from->r), nscan, ysize);
484 /* pack data into destination */
485 bpl = bytesperline(to->r, from->depth);
486 for(i=0; i<ysize; i++){
487 j = loadimage(to, Rect(0, i, xsize, i+1), nscan[i], bpl);
489 sysfatal("loadimage: %r");
492 for(i=0; i<Dy(from->r); i++){