lens.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <event.h>
  13. enum {
  14. Edge = 5,
  15. Maxmag = 16
  16. };
  17. enum {
  18. Mzoom,
  19. Munzoom,
  20. Mgrid,
  21. Mredraw,
  22. Mexit
  23. };
  24. char *menustr[] = {
  25. "zoom",
  26. "unzoom",
  27. "grid",
  28. "redraw",
  29. "exit",
  30. nil
  31. };
  32. Menu menu = {
  33. menustr,
  34. nil,
  35. -1
  36. };
  37. Point lastp;
  38. Image *red;
  39. Image *tmp;
  40. Image *grid;
  41. Image *chequer;
  42. int screenfd;
  43. int mag = 4;
  44. int showgrid = 0;
  45. Rectangle screenr;
  46. uint8_t *screenbuf;
  47. void magnify(void);
  48. void makegrid(void);
  49. void
  50. drawit(void)
  51. {
  52. Rectangle r;
  53. border(screen, screen->r, Edge, red, ZP);
  54. magnify();
  55. r = insetrect(screen->r, Edge);
  56. draw(screen, r, tmp, nil, tmp->r.min);
  57. flushimage(display, 1);
  58. }
  59. int bypp;
  60. void
  61. main(int argc, char *argv[])
  62. {
  63. Event e;
  64. char buf[5*12];
  65. uint32_t chan;
  66. int d;
  67. USED(argc); USED(argv);
  68. if(initdraw(nil, nil, "lens") < 0){
  69. fprint(2, "lens: initdraw failed: %r\n");
  70. exits("initdraw");
  71. }
  72. einit(Emouse|Ekeyboard);
  73. red = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, DRed);
  74. chequer = allocimage(display, Rect(0, 0, 2, 2), GREY1, 1, DBlack);
  75. draw(chequer, Rect(0, 0, 1, 1), display->white, nil, ZP);
  76. draw(chequer, Rect(1, 1, 2, 2), display->white, nil, ZP);
  77. lastp = divpt(addpt(screen->r.min, screen->r.max), 2);
  78. screenfd = open("/dev/screen", OREAD);
  79. if(screenfd < 0){
  80. fprint(2, "lens: can't open /dev/screen: %r\n");
  81. exits("screen");
  82. }
  83. if(read(screenfd, buf, sizeof buf) != sizeof buf){
  84. fprint(2, "lens: can't read /dev/screen: %r\n");
  85. exits("screen");
  86. }
  87. chan = strtochan(buf);
  88. d = chantodepth(chan);
  89. if(d < 8){
  90. fprint(2, "lens: can't handle screen format %11.11s\n", buf);
  91. exits("screen");
  92. }
  93. bypp = d/8;
  94. screenr.min.x = atoi(buf+1*12);
  95. screenr.min.y = atoi(buf+2*12);
  96. screenr.max.x = atoi(buf+3*12);
  97. screenr.max.y = atoi(buf+4*12);
  98. screenbuf = malloc(bypp*Dx(screenr)*Dy(screenr));
  99. if(screenbuf == nil){
  100. fprint(2, "lens: buffer malloc failed: %r\n");
  101. exits("malloc");
  102. }
  103. eresized(0);
  104. for(;;)
  105. switch(event(&e)){
  106. case Ekeyboard:
  107. switch(e.kbdc){
  108. case 'q':
  109. case 0x7f:
  110. case '\04':
  111. caseexit:
  112. exits(nil);
  113. case '=':
  114. case '+':
  115. casezoom:
  116. if(mag < Maxmag){
  117. mag++;
  118. makegrid();
  119. drawit();
  120. }
  121. break;
  122. case 'g':
  123. casegrid:
  124. showgrid = !showgrid;
  125. makegrid();
  126. drawit();
  127. break;
  128. case '-':
  129. case '_':
  130. caseunzoom:
  131. if(mag > 1){
  132. mag--;
  133. makegrid();
  134. drawit();
  135. }
  136. break;
  137. case '.':
  138. case ' ':
  139. caseredraw:
  140. drawit();
  141. break;
  142. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case'0':
  143. mag = e.kbdc-'0';
  144. if(mag == 0)
  145. mag = 10;
  146. makegrid();
  147. drawit();
  148. break;
  149. }
  150. break;
  151. case Emouse:
  152. if(e.mouse.buttons & 1){
  153. lastp = e.mouse.xy;
  154. drawit();
  155. }
  156. if(e.mouse.buttons & 4)
  157. switch(emenuhit(3, &e.mouse, &menu)){
  158. case Mzoom:
  159. goto casezoom;
  160. case Munzoom:
  161. goto caseunzoom;
  162. case Mgrid:
  163. goto casegrid;
  164. case Mredraw:
  165. goto caseredraw;
  166. case Mexit:
  167. goto caseexit;
  168. }
  169. break;
  170. }
  171. }
  172. void
  173. makegrid(void)
  174. {
  175. int m;
  176. if (grid != nil) {
  177. freeimage(grid);
  178. grid = nil;
  179. }
  180. if (showgrid) {
  181. m = mag;
  182. if (m < 5)
  183. m *= 10;
  184. grid = allocimage(display, Rect(0, 0, m, m),
  185. CHAN2(CGrey, 8, CAlpha, 8), 1, DTransparent);
  186. if (grid != nil){
  187. draw(grid, Rect(0, 0, m, 1), chequer, nil, ZP);
  188. draw(grid, Rect(0, 1, 1, m), chequer, nil, ZP);
  189. }
  190. }
  191. }
  192. void
  193. eresized(int new)
  194. {
  195. if(new && getwindow(display, Refnone) < 0){
  196. fprint(2, "lens: can't reattach to window: %r\n");
  197. exits("attach");
  198. }
  199. freeimage(tmp);
  200. tmp = allocimage(display, Rect(0, 0, Dx(screen->r)-Edge, Dy(screen->r)-Edge+Maxmag), screen->chan, 0, DNofill);
  201. if(tmp == nil){
  202. fprint(2, "lens: allocimage failed: %r\n");
  203. exits("allocimage");
  204. }
  205. drawit();
  206. }
  207. void
  208. magnify(void)
  209. {
  210. int x, y, xx, yy, dd, i;
  211. int dx, dy;
  212. int xoff, yoff;
  213. uint8_t out[8192];
  214. uint8_t sp[4];
  215. dx = (Dx(tmp->r)+mag-1)/mag;
  216. dy = (Dy(tmp->r)+mag-1)/mag;
  217. xoff = lastp.x-Dx(tmp->r)/(mag*2);
  218. yoff = lastp.y-Dy(tmp->r)/(mag*2);
  219. yy = yoff;
  220. dd = dy;
  221. if(yy < 0){
  222. dd += dy;
  223. yy = 0;
  224. }
  225. if(yy+dd > Dy(screenr))
  226. dd = Dy(screenr)-yy;
  227. seek(screenfd, 5*12+bypp*yy*Dx(screenr), 0);
  228. if(readn(screenfd, screenbuf+bypp*yy*Dx(screenr), bypp*Dx(screenr)*dd) != bypp*Dx(screenr)*dd){
  229. fprint(2, "lens: can't read screen: %r\n");
  230. return;
  231. }
  232. for(y=0; y<dy; y++){
  233. yy = yoff+y;
  234. if(yy>=0 && yy<Dy(screenr))
  235. for(x=0; x<dx; x++){
  236. xx = xoff+x;
  237. if(xx>=0 && xx<Dx(screenr)) /* snarf pixel at xx, yy */
  238. for(i=0; i<bypp; i++)
  239. sp[i] = screenbuf[bypp*(yy*Dx(screenr)+xx)+i];
  240. else
  241. sp[0] = sp[1] = sp[2] = sp[3] = 0;
  242. for(xx=0; xx<mag; xx++)
  243. if(x*mag+xx < tmp->r.max.x)
  244. for(i=0; i<bypp; i++)
  245. out[(x*mag+xx)*bypp+i] = sp[i];
  246. }
  247. else
  248. memset(out, 0, bypp*Dx(tmp->r));
  249. for(yy=0; yy<mag && y*mag+yy<Dy(tmp->r); yy++){
  250. werrstr("no error");
  251. if(loadimage(tmp, Rect(0, y*mag+yy, Dx(tmp->r), y*mag+yy+1), out, bypp*Dx(tmp->r)) != bypp*Dx(tmp->r)){
  252. exits("load");
  253. }
  254. }
  255. }
  256. if (showgrid && mag && grid)
  257. draw(tmp, tmp->r, grid, nil, mulpt(Pt(xoff, yoff), mag));
  258. }