nrotate.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Rotate an image 180° in O(log Dx + log Dy)
  3. * draw calls, using an extra buffer the same size
  4. * as the image.
  5. *
  6. * The basic concept is that you can invert an array by
  7. * inverting the top half, inverting the bottom half, and
  8. * then swapping them.
  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,
  22. Yaxis,
  23. };
  24. static void reverse(Image*, Image*, int);
  25. static void shuffle(Image*, Image*, int, int, Image*, int, int);
  26. static void writefile(char *name, Image *im, int gran);
  27. static void halvemaskdim(Image*);
  28. static void swapranges(Image*, Image*, int, int, int, int);
  29. /*
  30. * Rotate the image 180° by reflecting first
  31. * along the X axis, and then along the Y axis.
  32. */
  33. void
  34. rot180(Image *img)
  35. {
  36. Image *tmp;
  37. tmp = xallocimage(display, img->r, img->chan, 0, DNofill);
  38. if(tmp == nil)
  39. return;
  40. reverse(img, tmp, Xaxis);
  41. reverse(img, tmp, Yaxis);
  42. freeimage(tmp);
  43. }
  44. Image *mtmp;
  45. static void
  46. reverse(Image *img, Image *tmp, int axis)
  47. {
  48. Image *mask;
  49. Rectangle r;
  50. int i, d;
  51. /*
  52. * We start by swapping large chunks at a time.
  53. * The chunk size should be the largest power of
  54. * two that fits in the dimension.
  55. */
  56. d = axis==Xaxis ? Dx(img) : Dy(img);
  57. for(i = 1; i*2 <= d; i *= 2)
  58. ;
  59. r = axis==Xaxis ? Rect(0,0, i,100) : Rect(0,0, 100,i);
  60. mask = xallocimage(display, r, GREY1, 1, DTransparent);
  61. mtmp = xallocimage(display, r, GREY1, 1, DTransparent);
  62. /*
  63. * Now color the bottom (or left) half of the mask opaque.
  64. */
  65. if(axis==Xaxis)
  66. r.max.x /= 2;
  67. else
  68. r.max.y /= 2;
  69. draw(mask, r, display->opaque, nil, ZP);
  70. writefile("mask", mask, i);
  71. /*
  72. * Shuffle will recur, shuffling the pieces as necessary
  73. * and making the mask a finer and finer grating.
  74. */
  75. shuffle(img, tmp, axis, d, mask, i, 0);
  76. freeimage(mask);
  77. }
  78. /*
  79. * Shuffle the image by swapping pieces of size maskdim.
  80. */
  81. static void
  82. shuffle(Image *img, Image *tmp, int axis, int imgdim, Image *mask, int maskdim)
  83. {
  84. int slop;
  85. if(maskdim == 0)
  86. return;
  87. /*
  88. * Figure out how much will be left over that needs to be
  89. * shifted specially to the bottom.
  90. */
  91. slop = imgdim % maskdim;
  92. /*
  93. * Swap adjacent grating lines as per mask.
  94. */
  95. swapadjacent(img, tmp, axis, imgdim - slop, mask, maskdim);
  96. /*
  97. * Calculate the mask with gratings half as wide and recur.
  98. */
  99. halvemaskdim(mask, maskdim, axis);
  100. writefile("mask", mask, maskdim/2);
  101. shuffle(img, tmp, axis, imgdim, mask, maskdim/2);
  102. /*
  103. * Move the slop down to the bottom of the image.
  104. */
  105. swapranges(img, tmp, 0, imgdim-slop, imgdim, axis);
  106. moveup(im, tmp, lastnn, nn, n, axis);
  107. }
  108. /*
  109. * Halve the grating period in the mask.
  110. * The grating currently looks like
  111. * ####____####____####____####____
  112. * where #### is opacity.
  113. *
  114. * We want
  115. * ##__##__##__##__##__##__##__##__
  116. * which is achieved by shifting the mask
  117. * and drawing on itself through itself.
  118. * Draw doesn't actually allow this, so
  119. * we have to copy it first.
  120. *
  121. * ####____####____####____####____ (dst)
  122. * + ____####____####____####____#### (src)
  123. * in __####____####____####____####__ (mask)
  124. * ===========================================
  125. * ##__##__##__##__##__##__##__##__
  126. */
  127. static void
  128. halvemaskdim(Image *m, int maskdim, int axis)
  129. {
  130. Point δ;
  131. δ = axis==Xaxis ? Pt(maskdim,0) : Pt(0,maskdim);
  132. draw(mtmp, mtmp->r, mask, nil, mask->r.min);
  133. gendraw(mask, mask->r, mtmp, δ, mtmp, divpt(δ,2));
  134. writefile("mask", mask, maskdim/2);
  135. }
  136. /*
  137. * Swap the regions [a,b] and [b,c]
  138. */
  139. static void
  140. swapranges(Image *img, Image *tmp, int a, int b, int c, int axis)
  141. {
  142. Rectangle r;
  143. Point δ;
  144. if(a == b || b == c)
  145. return;
  146. writefile("swap", img, 0);
  147. draw(tmp, tmp->r, im, nil, im->r.min);
  148. /* [a,a+(c-b)] gets [b,c] */
  149. r = img->r;
  150. if(axis==Xaxis){
  151. δ = Pt(1,0);
  152. r.min.x = img->r.min.x + a;
  153. r.max.x = img->r.min.x + a + (c-b);
  154. }else{
  155. δ = Pt(0,1);
  156. r.min.y = img->r.min.y + a;
  157. r.max.y = img->r.min.y + a + (c-b);
  158. }
  159. draw(img, r, tmp, nil, addpt(tmp->r.min, mulpt(δ, b)));
  160. /* [a+(c-b), c] gets [a,b] */
  161. r = img->r;
  162. if(axis==Xaxis){
  163. r.min.x = img->r.min.x + a + (c-b);
  164. r.max.x = img->r.min.x + c;
  165. }else{
  166. r.min.y = img->r.min.y + a + (c-b);
  167. r.max.y = img->r.min.y + c;
  168. }
  169. draw(img, r, tmp, nil, addpt(tmp->r.min, mulpt(δ, a)));
  170. writefile("swap", img, 1);
  171. }
  172. /*
  173. * Swap adjacent regions as specified by the grating.
  174. * We do this by copying the image through the mask twice,
  175. * once aligned with the grading and once 180° out of phase.
  176. */
  177. static void
  178. swapadjacent(Image *img, Image *tmp, int axis, int imgdim, Image *mask, int maskdim)
  179. {
  180. Point δ;
  181. Rectangle r0, r1;
  182. δ = axis==Xaxis ? Pt(1,0) : Pt(0,1);
  183. r0 = img->r;
  184. r1 = img->r;
  185. switch(axis){
  186. case Xaxis:
  187. r0.max.x = imgdim;
  188. r1.min.x = imgdim;
  189. break;
  190. case Yaxis:
  191. r0.max.y = imgdim;
  192. r1.min.y = imgdim;
  193. }
  194. /*
  195. * r0 is the lower rectangle, while r1 is the upper one.
  196. */
  197. draw(tmp, tmp->r, img, nil,
  198. }
  199. void
  200. interlace(Image *im, Image *tmp, int axis, int n, Image *mask, int gran)
  201. {
  202. Point p0, p1;
  203. Rectangle r0, r1;
  204. r0 = im->r;
  205. r1 = im->r;
  206. switch(axis) {
  207. case Xaxis:
  208. r0.max.x = n;
  209. r1.min.x = n;
  210. p0 = (Point){gran, 0};
  211. p1 = (Point){-gran, 0};
  212. break;
  213. case Yaxis:
  214. r0.max.y = n;
  215. r1.min.y = n;
  216. p0 = (Point){0, gran};
  217. p1 = (Point){0, -gran};
  218. break;
  219. }
  220. draw(tmp, im->r, im, display->black, im->r.min);
  221. gendraw(im, r0, tmp, p0, mask, mask->r.min);
  222. gendraw(im, r0, tmp, p1, mask, p1);
  223. }
  224. static void
  225. writefile(char *name, Image *im, int gran)
  226. {
  227. static int c = 100;
  228. int fd;
  229. char buf[200];
  230. snprint(buf, sizeof buf, "%d%s%d", c++, name, gran);
  231. fd = create(buf, OWRITE, 0666);
  232. if(fd < 0)
  233. return;
  234. writeimage(fd, im, 0);
  235. close(fd);
  236. }