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)
96 p0 = (Point){gran, 0};
97 p1 = (Point){-gran, 0};
103 p0 = (Point){0, gran};
104 p1 = (Point){0, -gran};
108 drawop(tmp, im->r, im, display->opaque, im->r.min, S);
109 gendrawop(im, r0, tmp, p0, mask, mask->r.min, S);
110 gendrawop(im, r0, tmp, p1, mask, p1, S);
114 * Halve the grating period in the mask.
115 * The grating currently looks like
116 * ####____####____####____####____
117 * where #### is opacity.
120 * ##__##__##__##__##__##__##__##__
121 * which is achieved by shifting the mask
122 * and drawing on itself through itself.
123 * Draw doesn't actually allow this, so
124 * we have to copy it first.
126 * ####____####____####____####____ (dst)
127 * + ____####____####____####____#### (src)
128 * in __####____####____####____####__ (mask)
129 * ===========================================
130 * ##__##__##__##__##__##__##__##__
133 nextmask(Image *mask, int axis, int maskdim)
137 o = axis==Xaxis ? Pt(maskdim,0) : Pt(0,maskdim);
138 drawop(mtmp, mtmp->r, mask, nil, mask->r.min, S);
139 gendrawop(mask, mask->r, mtmp, o, mtmp, divpt(o,-2), S);
140 // writefile("mask", mask, maskdim/2);
145 shuffle(Image *im, Image *tmp, int axis, int n, Image *mask, int gran,
155 interlace(im, tmp, axis, nn, mask, gran);
156 // writefile("interlace", im, gran);
158 gran = nextmask(mask, axis, gran);
159 shuffle(im, tmp, axis, n, mask, gran, nn);
160 // writefile("shuffle", im, gran);
161 moveup(im, tmp, lastnn, nn, n, axis);
162 // writefile("move", im, gran);
173 if(chantodepth(im->chan) < 8){
174 /* this speeds things up dramatically; draw is too slow on sub-byte pixel sizes */
175 tmp0 = xallocimage(display, im->r, CMAP8, 0, DNofill);
176 drawop(tmp0, tmp0->r, im, nil, im->r.min, S);
180 tmp = xallocimage(display, tmp0->r, tmp0->chan, 0, DNofill);
186 for(gran=1; gran<Dx(im->r); gran *= 2)
191 rmask.max = (Point){2*gran, 100};
193 mask = xallocimage(display, rmask, GREY1, 1, DTransparent);
194 mtmp = xallocimage(display, rmask, GREY1, 1, DTransparent);
195 if(mask == nil || mtmp == nil) {
196 fprint(2, "out of memory during rot180: %r\n");
200 drawop(mask, rmask, display->opaque, nil, ZP, S);
201 // writefile("mask", mask, gran);
202 shuffle(im, tmp, Xaxis, Dx(im->r), mask, gran, 0);
206 for(gran=1; gran<Dy(im->r); gran *= 2)
209 rmask.max = (Point){100, 2*gran};
210 mask = xallocimage(display, rmask, GREY1, 1, DTransparent);
211 mtmp = xallocimage(display, rmask, GREY1, 1, DTransparent);
212 if(mask == nil || mtmp == nil) {
213 fprint(2, "out of memory during rot180: %r\n");
217 drawop(mask, rmask, display->opaque, nil, ZP, S);
218 shuffle(im, tmp, Yaxis, Dy(im->r), mask, gran, 0);
226 /* rotates an image 90 degrees clockwise */
235 tmp = xallocimage(display, Rect(0, 0, dy, dx), im->chan, 0, DCyan);
237 fprint(2, "out of memory during rot90: %r\n");
241 for(j = 0; j < dx; j++) {
242 for(i = 0; i < dy; i++) {
243 drawop(tmp, Rect(i, j, i+1, j+1), im, nil, Pt(j, dy-(i+1)), S);
251 /* rotates an image 270 degrees clockwise */
260 tmp = xallocimage(display, Rect(0, 0, dy, dx), im->chan, 0, DCyan);
262 fprint(2, "out of memory during rot270: %r\n");
266 for(i = 0; i < dy; i++) {
267 for(j = 0; j < dx; j++) {
268 drawop(tmp, Rect(i, j, i+1, j+1), im, nil, Pt(dx-(j+1), i), S);
276 /* from resample.c -- resize from → to using interpolation */
279 #define K2 7 /* from -.7 to +.7 inclusive, meaning .2 into each adjacent pixel */
295 * i0(x) is the modified Bessel function, Σ (x/2)^2L / (L!)²
296 * There are faster ways to calculate this, but we precompute
297 * into a table so let's keep it simple.
307 v += pow(x/2., 2*L)/pow(fac(L), 2);
312 kaiser(double x, double t, double a)
316 return i0(a*sqrt(1-(x*x/(t*t))))/i0(a);
321 resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
324 double X, xx, v, rat;
327 rat = (double)inx/(double)outx;
328 for(x=0; x<outx; x++){
330 /* don't resample if size unchanged */
331 out[off+x*d] = in[off+x*d];
336 for(k=-K2; k<=K2; k++){
343 v += in[off+i*d] * K[K2+k];
350 resampley(uchar **in, int off, int iny, uchar **out, int outy)
353 double Y, yy, v, rat;
355 rat = (double)iny/(double)outy;
356 for(y=0; y<outy; y++){
358 /* don't resample if size unchanged */
359 out[y][off] = in[y][off];
364 for(k=-K2; k<=K2; k++){
371 v += in[i][off] * K[K2+k];
379 resample(Image *from, Image *to)
381 int i, j, bpl, nchan;
382 uchar **oscan, **nscan;
389 for(i=-K2; i<=K2; i++){
390 K[K2+i] = kaiser(i/10., K2/10., 4.);
419 /* use library to convert to byte-per-chan form, then convert back */
420 t1 = xallocimage(display, Rect(0, 0, Dx(from->r), Dy(from->r)), tchan, 0, DNofill);
422 fprint(2, "out of memory for temp image 1 in resample: %r\n");
425 drawop(t1, t1->r, from, nil, ZP, S);
426 t2 = xallocimage(display, to->r, tchan, 0, DNofill);
428 fprint(2, "out of memory temp image 2 in resample: %r\n");
432 drawop(to, to->r, t2, nil, ZP, S);
438 sysfatal("can't handle channel type %s", chantostr(tmp, from->chan));
443 oscan = malloc(Dy(from->r)*sizeof(uchar*));
444 nscan = malloc(max(ysize, Dy(from->r))*sizeof(uchar*));
445 if(oscan == nil || nscan == nil)
446 sysfatal("can't allocate: %r");
448 /* unload original image into scan lines */
449 bpl = bytesperline(from->r, from->depth);
450 for(i=0; i<Dy(from->r); i++){
451 oscan[i] = malloc(bpl);
453 sysfatal("can't allocate: %r");
454 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);
456 sysfatal("unloadimage");
459 /* allocate scan lines for destination. we do y first, so need at least Dy(from->r) lines */
460 bpl = bytesperline(Rect(0, 0, xsize, Dy(from->r)), from->depth);
461 for(i=0; i<max(ysize, Dy(from->r)); i++){
462 nscan[i] = malloc(bpl);
464 sysfatal("can't allocate: %r");
468 nchan = from->depth/8;
469 for(i=0; i<Dy(from->r); i++){
470 for(j=0; j<nchan; j++){
471 if(j==0 && from->chan==XRGB32)
473 resamplex(oscan[i], j, nchan, Dx(from->r), nscan[i], xsize);
477 nscan[i] = malloc(bpl);
479 sysfatal("can't allocate: %r");
483 for(i=0; i<xsize; i++)
484 for(j=0; j<nchan; j++)
485 resampley(oscan, nchan*i+j, Dy(from->r), nscan, ysize);
487 /* pack data into destination */
488 bpl = bytesperline(to->r, from->depth);
489 for(i=0; i<ysize; i++){
490 j = loadimage(to, Rect(0, i, xsize, i+1), nscan[i], bpl);
492 sysfatal("loadimage: %r");
495 for(i=0; i<Dy(from->r); i++){