rotate.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * rotate an image 180° in O(log Dx + log Dy) /dev/draw writes,
  3. * using an extra buffer same size as the image.
  4. *
  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.)
  9. *
  10. * This is usually overkill, but it speeds up slow remote
  11. * connections quite a bit.
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <bio.h>
  16. #include <draw.h>
  17. #include <event.h>
  18. #include "page.h"
  19. int ndraw = 0;
  20. enum {
  21. Xaxis = 0,
  22. Yaxis = 1,
  23. };
  24. Image *mtmp;
  25. void
  26. writefile(char *name, Image *im, int gran)
  27. {
  28. static int c = 100;
  29. int fd;
  30. char buf[200];
  31. snprint(buf, sizeof buf, "%d%s%d", c++, name, gran);
  32. fd = create(buf, OWRITE, 0666);
  33. if(fd < 0)
  34. return;
  35. writeimage(fd, im, 0);
  36. close(fd);
  37. }
  38. void
  39. moveup(Image *im, Image *tmp, int a, int b, int c, int axis)
  40. {
  41. Rectangle range;
  42. Rectangle dr0, dr1;
  43. Point p0, p1;
  44. if(a == b || b == c)
  45. return;
  46. drawop(tmp, tmp->r, im, nil, im->r.min, S);
  47. switch(axis){
  48. case Xaxis:
  49. range = Rect(a, im->r.min.y, c, im->r.max.y);
  50. dr0 = range;
  51. dr0.max.x = dr0.min.x+(c-b);
  52. p0 = Pt(b, im->r.min.y);
  53. dr1 = range;
  54. dr1.min.x = dr1.max.x-(b-a);
  55. p1 = Pt(a, im->r.min.y);
  56. break;
  57. case Yaxis:
  58. range = Rect(im->r.min.x, a, im->r.max.x, c);
  59. dr0 = range;
  60. dr0.max.y = dr0.min.y+(c-b);
  61. p0 = Pt(im->r.min.x, b);
  62. dr1 = range;
  63. dr1.min.y = dr1.max.y-(b-a);
  64. p1 = Pt(im->r.min.x, a);
  65. break;
  66. }
  67. drawop(im, dr0, tmp, nil, p0, S);
  68. drawop(im, dr1, tmp, nil, p1, S);
  69. }
  70. void
  71. interlace(Image *im, Image *tmp, int axis, int n, Image *mask, int gran)
  72. {
  73. Point p0, p1;
  74. Rectangle r0, r1;
  75. r0 = im->r;
  76. r1 = im->r;
  77. switch(axis) {
  78. case Xaxis:
  79. r0.max.x = n;
  80. r1.min.x = n;
  81. p0 = (Point){gran, 0};
  82. p1 = (Point){-gran, 0};
  83. break;
  84. case Yaxis:
  85. r0.max.y = n;
  86. r1.min.y = n;
  87. p0 = (Point){0, gran};
  88. p1 = (Point){0, -gran};
  89. break;
  90. }
  91. drawop(tmp, im->r, im, display->opaque, im->r.min, S);
  92. gendrawop(im, r0, tmp, p0, mask, mask->r.min, S);
  93. gendrawop(im, r0, tmp, p1, mask, p1, S);
  94. }
  95. /*
  96. * Halve the grating period in the mask.
  97. * The grating currently looks like
  98. * ####____####____####____####____
  99. * where #### is opacity.
  100. *
  101. * We want
  102. * ##__##__##__##__##__##__##__##__
  103. * which is achieved by shifting the mask
  104. * and drawing on itself through itself.
  105. * Draw doesn't actually allow this, so
  106. * we have to copy it first.
  107. *
  108. * ####____####____####____####____ (dst)
  109. * + ____####____####____####____#### (src)
  110. * in __####____####____####____####__ (mask)
  111. * ===========================================
  112. * ##__##__##__##__##__##__##__##__
  113. */
  114. int
  115. nextmask(Image *mask, int axis, int maskdim)
  116. {
  117. Point δ;
  118. δ = axis==Xaxis ? Pt(maskdim,0) : Pt(0,maskdim);
  119. drawop(mtmp, mtmp->r, mask, nil, mask->r.min, S);
  120. gendrawop(mask, mask->r, mtmp, δ, mtmp, divpt(δ,-2), S);
  121. // writefile("mask", mask, maskdim/2);
  122. return maskdim/2;
  123. }
  124. void
  125. shuffle(Image *im, Image *tmp, int axis, int n, Image *mask, int gran,
  126. int lastnn)
  127. {
  128. int nn, left;
  129. if(gran == 0)
  130. return;
  131. left = n%(2*gran);
  132. nn = n - left;
  133. interlace(im, tmp, axis, nn, mask, gran);
  134. // writefile("interlace", im, gran);
  135. gran = nextmask(mask, axis, gran);
  136. shuffle(im, tmp, axis, n, mask, gran, nn);
  137. // writefile("shuffle", im, gran);
  138. moveup(im, tmp, lastnn, nn, n, axis);
  139. // writefile("move", im, gran);
  140. }
  141. void
  142. rot180(Image *im)
  143. {
  144. Image *tmp, *tmp0;
  145. Image *mask;
  146. Rectangle rmask;
  147. int gran;
  148. if(chantodepth(im->chan) < 8){
  149. /* this speeds things up dramatically; draw is too slow on sub-byte pixel sizes */
  150. tmp0 = xallocimage(display, im->r, CMAP8, 0, DNofill);
  151. drawop(tmp0, tmp0->r, im, nil, im->r.min, S);
  152. }else
  153. tmp0 = im;
  154. tmp = xallocimage(display, tmp0->r, tmp0->chan, 0, DNofill);
  155. if(tmp == nil){
  156. if(tmp0 != im)
  157. freeimage(tmp0);
  158. return;
  159. }
  160. for(gran=1; gran<Dx(im->r); gran *= 2)
  161. ;
  162. gran /= 4;
  163. rmask.min = ZP;
  164. rmask.max = (Point){2*gran, 100};
  165. mask = xallocimage(display, rmask, GREY1, 1, DTransparent);
  166. mtmp = xallocimage(display, rmask, GREY1, 1, DTransparent);
  167. if(mask == nil || mtmp == nil) {
  168. fprint(2, "out of memory during rot180: %r\n");
  169. wexits("memory");
  170. }
  171. rmask.max.x = gran;
  172. drawop(mask, rmask, display->opaque, nil, ZP, S);
  173. // writefile("mask", mask, gran);
  174. shuffle(im, tmp, Xaxis, Dx(im->r), mask, gran, 0);
  175. freeimage(mask);
  176. freeimage(mtmp);
  177. for(gran=1; gran<Dy(im->r); gran *= 2)
  178. ;
  179. gran /= 4;
  180. rmask.max = (Point){100, 2*gran};
  181. mask = xallocimage(display, rmask, GREY1, 1, DTransparent);
  182. mtmp = xallocimage(display, rmask, GREY1, 1, DTransparent);
  183. if(mask == nil || mtmp == nil) {
  184. fprint(2, "out of memory during rot180: %r\n");
  185. wexits("memory");
  186. }
  187. rmask.max.y = gran;
  188. drawop(mask, rmask, display->opaque, nil, ZP, S);
  189. shuffle(im, tmp, Yaxis, Dy(im->r), mask, gran, 0);
  190. freeimage(mask);
  191. freeimage(mtmp);
  192. freeimage(tmp);
  193. if(tmp0 != im)
  194. freeimage(tmp0);
  195. }
  196. /* rotates an image 90 degrees clockwise */
  197. Image *
  198. rot90(Image *im)
  199. {
  200. Image *tmp;
  201. int i, j, dx, dy;
  202. dx = Dx(im->r);
  203. dy = Dy(im->r);
  204. tmp = xallocimage(display, Rect(0, 0, dy, dx), im->chan, 0, DCyan);
  205. if(tmp == nil) {
  206. fprint(2, "out of memory during rot90: %r\n");
  207. wexits("memory");
  208. }
  209. for(j = 0; j < dx; j++) {
  210. for(i = 0; i < dy; i++) {
  211. drawop(tmp, Rect(i, j, i+1, j+1), im, nil, Pt(j, dy-(i+1)), S);
  212. }
  213. }
  214. freeimage(im);
  215. return(tmp);
  216. }
  217. /* from resample.c -- resize from → to using interpolation */
  218. #define K2 7 /* from -.7 to +.7 inclusive, meaning .2 into each adjacent pixel */
  219. #define NK (2*K2+1)
  220. double K[NK];
  221. double
  222. fac(int L)
  223. {
  224. int i, f;
  225. f = 1;
  226. for(i=L; i>1; --i)
  227. f *= i;
  228. return f;
  229. }
  230. /*
  231. * i0(x) is the modified Bessel function, Σ (x/2)^2L / (L!)²
  232. * There are faster ways to calculate this, but we precompute
  233. * into a table so let's keep it simple.
  234. */
  235. double
  236. i0(double x)
  237. {
  238. double v;
  239. int L;
  240. v = 1.0;
  241. for(L=1; L<10; L++)
  242. v += pow(x/2., 2*L)/pow(fac(L), 2);
  243. return v;
  244. }
  245. double
  246. kaiser(double x, double τ, double α)
  247. {
  248. if(fabs(x) > τ)
  249. return 0.;
  250. return i0(α*sqrt(1-(x*x/(τ*τ))))/i0(α);
  251. }
  252. void
  253. resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
  254. {
  255. int i, x, k;
  256. double X, xx, v, rat;
  257. rat = (double)inx/(double)outx;
  258. for(x=0; x<outx; x++){
  259. if(inx == outx){
  260. /* don't resample if size unchanged */
  261. out[off+x*d] = in[off+x*d];
  262. continue;
  263. }
  264. v = 0.0;
  265. X = x*rat;
  266. for(k=-K2; k<=K2; k++){
  267. xx = X + rat*k/10.;
  268. i = xx;
  269. if(i < 0)
  270. i = 0;
  271. if(i >= inx)
  272. i = inx-1;
  273. v += in[off+i*d] * K[K2+k];
  274. }
  275. out[off+x*d] = v;
  276. }
  277. }
  278. void
  279. resampley(uchar **in, int off, int iny, uchar **out, int outy)
  280. {
  281. int y, i, k;
  282. double Y, yy, v, rat;
  283. rat = (double)iny/(double)outy;
  284. for(y=0; y<outy; y++){
  285. if(iny == outy){
  286. /* don't resample if size unchanged */
  287. out[y][off] = in[y][off];
  288. continue;
  289. }
  290. v = 0.0;
  291. Y = y*rat;
  292. for(k=-K2; k<=K2; k++){
  293. yy = Y + rat*k/10.;
  294. i = yy;
  295. if(i < 0)
  296. i = 0;
  297. if(i >= iny)
  298. i = iny-1;
  299. v += in[i][off] * K[K2+k];
  300. }
  301. out[y][off] = v;
  302. }
  303. }
  304. Image*
  305. resample(Image *from, Image *to)
  306. {
  307. int i, j, bpl, nchan;
  308. uchar **oscan, **nscan;
  309. char tmp[20];
  310. int xsize, ysize;
  311. double v;
  312. Image *t1, *t2;
  313. ulong tchan;
  314. for(i=-K2; i<=K2; i++){
  315. K[K2+i] = kaiser(i/10., K2/10., 4.);
  316. }
  317. /* normalize */
  318. v = 0.0;
  319. for(i=0; i<NK; i++)
  320. v += K[i];
  321. for(i=0; i<NK; i++)
  322. K[i] /= v;
  323. switch(from->chan){
  324. case GREY8:
  325. case RGB24:
  326. case RGBA32:
  327. case ARGB32:
  328. case XRGB32:
  329. break;
  330. case CMAP8:
  331. case RGB15:
  332. case RGB16:
  333. tchan = RGB24;
  334. goto Convert;
  335. case GREY1:
  336. case GREY2:
  337. case GREY4:
  338. tchan = GREY8;
  339. Convert:
  340. /* use library to convert to byte-per-chan form, then convert back */
  341. t1 = xallocimage(display, Rect(0, 0, Dx(from->r), Dy(from->r)), tchan, 0, DNofill);
  342. if(t1 == nil) {
  343. fprint(2, "out of memory for temp image 1 in resample: %r\n");
  344. wexits("memory");
  345. }
  346. drawop(t1, t1->r, from, nil, ZP, S);
  347. t2 = xallocimage(display, to->r, tchan, 0, DNofill);
  348. if(t2 == nil) {
  349. fprint(2, "out of memory temp image 2 in resample: %r\n");
  350. wexits("memory");
  351. }
  352. resample(t1, t2);
  353. drawop(to, to->r, t2, nil, ZP, S);
  354. freeimage(t1);
  355. freeimage(t2);
  356. return to;
  357. default:
  358. sysfatal("can't handle channel type %s", chantostr(tmp, from->chan));
  359. }
  360. xsize = Dx(to->r);
  361. ysize = Dy(to->r);
  362. oscan = malloc(Dy(from->r)*sizeof(uchar*));
  363. nscan = malloc(max(ysize, Dy(from->r))*sizeof(uchar*));
  364. if(oscan == nil || nscan == nil)
  365. sysfatal("can't allocate: %r");
  366. /* unload original image into scan lines */
  367. bpl = bytesperline(from->r, from->depth);
  368. for(i=0; i<Dy(from->r); i++){
  369. oscan[i] = malloc(bpl);
  370. if(oscan[i] == nil)
  371. sysfatal("can't allocate: %r");
  372. 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);
  373. if(j != bpl)
  374. sysfatal("unloadimage");
  375. }
  376. /* allocate scan lines for destination. we do y first, so need at least Dy(from->r) lines */
  377. bpl = bytesperline(Rect(0, 0, xsize, Dy(from->r)), from->depth);
  378. for(i=0; i<max(ysize, Dy(from->r)); i++){
  379. nscan[i] = malloc(bpl);
  380. if(nscan[i] == nil)
  381. sysfatal("can't allocate: %r");
  382. }
  383. /* resample in X */
  384. nchan = from->depth/8;
  385. for(i=0; i<Dy(from->r); i++){
  386. for(j=0; j<nchan; j++){
  387. if(j==0 && from->chan==XRGB32)
  388. continue;
  389. resamplex(oscan[i], j, nchan, Dx(from->r), nscan[i], xsize);
  390. }
  391. free(oscan[i]);
  392. oscan[i] = nscan[i];
  393. nscan[i] = malloc(bpl);
  394. if(nscan[i] == nil)
  395. sysfatal("can't allocate: %r");
  396. }
  397. /* resample in Y */
  398. for(i=0; i<xsize; i++)
  399. for(j=0; j<nchan; j++)
  400. resampley(oscan, nchan*i+j, Dy(from->r), nscan, ysize);
  401. /* pack data into destination */
  402. bpl = bytesperline(to->r, from->depth);
  403. for(i=0; i<ysize; i++){
  404. j = loadimage(to, Rect(0, i, xsize, i+1), nscan[i], bpl);
  405. if(j != bpl)
  406. sysfatal("loadimage: %r");
  407. }
  408. for(i=0; i<Dy(from->r); i++){
  409. free(oscan[i]);
  410. free(nscan[i]);
  411. }
  412. free(oscan);
  413. free(nscan);
  414. return to;
  415. }