rotate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. /* rotates an image 270 degrees clockwise */
  218. Image *
  219. rot270(Image *im)
  220. {
  221. Image *tmp;
  222. int i, j, dx, dy;
  223. dx = Dx(im->r);
  224. dy = Dy(im->r);
  225. tmp = xallocimage(display, Rect(0, 0, dy, dx), im->chan, 0, DCyan);
  226. if(tmp == nil) {
  227. fprint(2, "out of memory during rot270: %r\n");
  228. wexits("memory");
  229. }
  230. for(i = 0; i < dy; i++) {
  231. for(j = 0; j < dx; j++) {
  232. drawop(tmp, Rect(i, j, i+1, j+1), im, nil, Pt(dx-(j+1), i), S);
  233. }
  234. }
  235. freeimage(im);
  236. return(tmp);
  237. }
  238. /* from resample.c -- resize from → to using interpolation */
  239. #define K2 7 /* from -.7 to +.7 inclusive, meaning .2 into each adjacent pixel */
  240. #define NK (2*K2+1)
  241. double K[NK];
  242. double
  243. fac(int L)
  244. {
  245. int i, f;
  246. f = 1;
  247. for(i=L; i>1; --i)
  248. f *= i;
  249. return f;
  250. }
  251. /*
  252. * i0(x) is the modified Bessel function, Σ (x/2)^2L / (L!)²
  253. * There are faster ways to calculate this, but we precompute
  254. * into a table so let's keep it simple.
  255. */
  256. double
  257. i0(double x)
  258. {
  259. double v;
  260. int L;
  261. v = 1.0;
  262. for(L=1; L<10; L++)
  263. v += pow(x/2., 2*L)/pow(fac(L), 2);
  264. return v;
  265. }
  266. double
  267. kaiser(double x, double τ, double α)
  268. {
  269. if(fabs(x) > τ)
  270. return 0.;
  271. return i0(α*sqrt(1-(x*x/(τ*τ))))/i0(α);
  272. }
  273. void
  274. resamplex(uchar *in, int off, int d, int inx, uchar *out, int outx)
  275. {
  276. int i, x, k;
  277. double X, xx, v, rat;
  278. rat = (double)inx/(double)outx;
  279. for(x=0; x<outx; x++){
  280. if(inx == outx){
  281. /* don't resample if size unchanged */
  282. out[off+x*d] = in[off+x*d];
  283. continue;
  284. }
  285. v = 0.0;
  286. X = x*rat;
  287. for(k=-K2; k<=K2; k++){
  288. xx = X + rat*k/10.;
  289. i = xx;
  290. if(i < 0)
  291. i = 0;
  292. if(i >= inx)
  293. i = inx-1;
  294. v += in[off+i*d] * K[K2+k];
  295. }
  296. out[off+x*d] = v;
  297. }
  298. }
  299. void
  300. resampley(uchar **in, int off, int iny, uchar **out, int outy)
  301. {
  302. int y, i, k;
  303. double Y, yy, v, rat;
  304. rat = (double)iny/(double)outy;
  305. for(y=0; y<outy; y++){
  306. if(iny == outy){
  307. /* don't resample if size unchanged */
  308. out[y][off] = in[y][off];
  309. continue;
  310. }
  311. v = 0.0;
  312. Y = y*rat;
  313. for(k=-K2; k<=K2; k++){
  314. yy = Y + rat*k/10.;
  315. i = yy;
  316. if(i < 0)
  317. i = 0;
  318. if(i >= iny)
  319. i = iny-1;
  320. v += in[i][off] * K[K2+k];
  321. }
  322. out[y][off] = v;
  323. }
  324. }
  325. Image*
  326. resample(Image *from, Image *to)
  327. {
  328. int i, j, bpl, nchan;
  329. uchar **oscan, **nscan;
  330. char tmp[20];
  331. int xsize, ysize;
  332. double v;
  333. Image *t1, *t2;
  334. ulong tchan;
  335. for(i=-K2; i<=K2; i++){
  336. K[K2+i] = kaiser(i/10., K2/10., 4.);
  337. }
  338. /* normalize */
  339. v = 0.0;
  340. for(i=0; i<NK; i++)
  341. v += K[i];
  342. for(i=0; i<NK; i++)
  343. K[i] /= v;
  344. switch(from->chan){
  345. case GREY8:
  346. case RGB24:
  347. case RGBA32:
  348. case ARGB32:
  349. case XRGB32:
  350. break;
  351. case CMAP8:
  352. case RGB15:
  353. case RGB16:
  354. tchan = RGB24;
  355. goto Convert;
  356. case GREY1:
  357. case GREY2:
  358. case GREY4:
  359. tchan = GREY8;
  360. Convert:
  361. /* use library to convert to byte-per-chan form, then convert back */
  362. t1 = xallocimage(display, Rect(0, 0, Dx(from->r), Dy(from->r)), tchan, 0, DNofill);
  363. if(t1 == nil) {
  364. fprint(2, "out of memory for temp image 1 in resample: %r\n");
  365. wexits("memory");
  366. }
  367. drawop(t1, t1->r, from, nil, ZP, S);
  368. t2 = xallocimage(display, to->r, tchan, 0, DNofill);
  369. if(t2 == nil) {
  370. fprint(2, "out of memory temp image 2 in resample: %r\n");
  371. wexits("memory");
  372. }
  373. resample(t1, t2);
  374. drawop(to, to->r, t2, nil, ZP, S);
  375. freeimage(t1);
  376. freeimage(t2);
  377. return to;
  378. default:
  379. sysfatal("can't handle channel type %s", chantostr(tmp, from->chan));
  380. }
  381. xsize = Dx(to->r);
  382. ysize = Dy(to->r);
  383. oscan = malloc(Dy(from->r)*sizeof(uchar*));
  384. nscan = malloc(max(ysize, Dy(from->r))*sizeof(uchar*));
  385. if(oscan == nil || nscan == nil)
  386. sysfatal("can't allocate: %r");
  387. /* unload original image into scan lines */
  388. bpl = bytesperline(from->r, from->depth);
  389. for(i=0; i<Dy(from->r); i++){
  390. oscan[i] = malloc(bpl);
  391. if(oscan[i] == nil)
  392. sysfatal("can't allocate: %r");
  393. 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);
  394. if(j != bpl)
  395. sysfatal("unloadimage");
  396. }
  397. /* allocate scan lines for destination. we do y first, so need at least Dy(from->r) lines */
  398. bpl = bytesperline(Rect(0, 0, xsize, Dy(from->r)), from->depth);
  399. for(i=0; i<max(ysize, Dy(from->r)); i++){
  400. nscan[i] = malloc(bpl);
  401. if(nscan[i] == nil)
  402. sysfatal("can't allocate: %r");
  403. }
  404. /* resample in X */
  405. nchan = from->depth/8;
  406. for(i=0; i<Dy(from->r); i++){
  407. for(j=0; j<nchan; j++){
  408. if(j==0 && from->chan==XRGB32)
  409. continue;
  410. resamplex(oscan[i], j, nchan, Dx(from->r), nscan[i], xsize);
  411. }
  412. free(oscan[i]);
  413. oscan[i] = nscan[i];
  414. nscan[i] = malloc(bpl);
  415. if(nscan[i] == nil)
  416. sysfatal("can't allocate: %r");
  417. }
  418. /* resample in Y */
  419. for(i=0; i<xsize; i++)
  420. for(j=0; j<nchan; j++)
  421. resampley(oscan, nchan*i+j, Dy(from->r), nscan, ysize);
  422. /* pack data into destination */
  423. bpl = bytesperline(to->r, from->depth);
  424. for(i=0; i<ysize; i++){
  425. j = loadimage(to, Rect(0, i, xsize, i+1), nscan[i], bpl);
  426. if(j != bpl)
  427. sysfatal("loadimage: %r");
  428. }
  429. for(i=0; i<Dy(from->r); i++){
  430. free(oscan[i]);
  431. free(nscan[i]);
  432. }
  433. free(oscan);
  434. free(nscan);
  435. return to;
  436. }