lens.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. #include <event.h>
  5. enum {
  6. Edge = 5,
  7. Maxmag = 16
  8. };
  9. Point lastp;
  10. Image *red;
  11. Image *tmp;
  12. Image *grid;
  13. Image *chequer;
  14. int screenfd;
  15. int mag = 4;
  16. int showgrid = 0;
  17. Rectangle screenr;
  18. uchar *screenbuf;
  19. void magnify(void);
  20. void makegrid(void);
  21. void
  22. drawit(void)
  23. {
  24. Rectangle r;
  25. border(screen, screen->r, Edge, red, ZP);
  26. magnify();
  27. r = insetrect(screen->r, Edge);
  28. draw(screen, r, tmp, nil, tmp->r.min);
  29. flushimage(display, 1);
  30. }
  31. int bypp;
  32. void
  33. main(int argc, char *argv[])
  34. {
  35. Event e;
  36. char buf[5*12];
  37. ulong chan;
  38. int d;
  39. USED(argc, argv);
  40. if(initdraw(nil, nil, "lens") < 0){
  41. fprint(2, "lens: initdraw failed: %r\n");
  42. exits("initdraw");
  43. }
  44. einit(Emouse|Ekeyboard);
  45. red = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, DRed);
  46. chequer = allocimage(display, Rect(0, 0, 2, 2), GREY1, 1, DBlack);
  47. draw(chequer, Rect(0, 0, 1, 1), display->white, nil, ZP);
  48. draw(chequer, Rect(1, 1, 2, 2), display->white, nil, ZP);
  49. lastp = divpt(addpt(screen->r.min, screen->r.max), 2);
  50. screenfd = open("/dev/screen", OREAD);
  51. if(screenfd < 0){
  52. fprint(2, "lens: can't open /dev/screen: %r\n");
  53. exits("screen");
  54. }
  55. if(read(screenfd, buf, sizeof buf) != sizeof buf){
  56. fprint(2, "lens: can't read /dev/screen: %r\n");
  57. exits("screen");
  58. }
  59. chan = strtochan(buf);
  60. d = chantodepth(chan);
  61. if(d < 8){
  62. fprint(2, "lens: can't handle screen format %11.11s\n", buf);
  63. exits("screen");
  64. }
  65. bypp = d/8;
  66. screenr.min.x = atoi(buf+1*12);
  67. screenr.min.y = atoi(buf+2*12);
  68. screenr.max.x = atoi(buf+3*12);
  69. screenr.max.y = atoi(buf+4*12);
  70. screenbuf = malloc(bypp*Dx(screenr)*Dy(screenr));
  71. if(screenbuf == nil){
  72. fprint(2, "lens: buffer malloc failed: %r\n");
  73. exits("malloc");
  74. }
  75. eresized(0);
  76. for(;;)
  77. switch(event(&e)){
  78. case Ekeyboard:
  79. switch(e.kbdc){
  80. case 'q':
  81. case '\04':
  82. exits(nil);
  83. case '=':
  84. case '+':
  85. if(mag < Maxmag){
  86. mag++;
  87. makegrid();
  88. drawit();
  89. }
  90. break;
  91. case 'g':
  92. showgrid = !showgrid;
  93. makegrid();
  94. drawit();
  95. break;
  96. case '-':
  97. case '_':
  98. if(mag > 1){
  99. mag--;
  100. makegrid();
  101. drawit();
  102. }
  103. break;
  104. case '.':
  105. case ' ':
  106. drawit();
  107. break;
  108. case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case'0':
  109. mag = e.kbdc-'0';
  110. if(mag == 0)
  111. mag = 10;
  112. makegrid();
  113. drawit();
  114. break;
  115. }
  116. if(e.kbdc == 'q' || e.kbdc == '\04')
  117. exits(nil);
  118. break;
  119. case Emouse:
  120. if(e.mouse.buttons){
  121. lastp = e.mouse.xy;
  122. drawit();
  123. }
  124. break;
  125. }
  126. }
  127. void
  128. makegrid(void)
  129. {
  130. int m;
  131. if (grid != nil) {
  132. freeimage(grid);
  133. grid = nil;
  134. }
  135. if (showgrid) {
  136. m = mag;
  137. if (m < 5)
  138. m *= 10;
  139. grid = allocimage(display, Rect(0, 0, m, m),
  140. CHAN2(CGrey, 8, CAlpha, 8), 1, DTransparent);
  141. if (grid != nil){
  142. draw(grid, Rect(0, 0, m, 1), chequer, nil, ZP);
  143. draw(grid, Rect(0, 1, 1, m), chequer, nil, ZP);
  144. }
  145. }
  146. }
  147. void
  148. eresized(int new)
  149. {
  150. if(new && getwindow(display, Refnone) < 0){
  151. fprint(2, "lens: can't reattach to window: %r\n");
  152. exits("attach");
  153. }
  154. freeimage(tmp);
  155. tmp = allocimage(display, Rect(0, 0, Dx(screen->r)-Edge, Dy(screen->r)-Edge+Maxmag), screen->chan, 0, DNofill);
  156. if(tmp == nil){
  157. fprint(2, "lens: allocimage failed: %r\n");
  158. exits("allocimage");
  159. }
  160. drawit();
  161. }
  162. void
  163. magnify(void)
  164. {
  165. int x, y, xx, yy, dd, i;
  166. int dx, dy;
  167. int xoff, yoff;
  168. uchar out[8192];
  169. uchar sp[4];
  170. dx = (Dx(tmp->r)+mag-1)/mag;
  171. dy = (Dy(tmp->r)+mag-1)/mag;
  172. xoff = lastp.x-Dx(tmp->r)/(mag*2);
  173. yoff = lastp.y-Dy(tmp->r)/(mag*2);
  174. yy = yoff;
  175. dd = dy;
  176. if(yy < 0){
  177. dd += dy;
  178. yy = 0;
  179. }
  180. if(yy+dd > Dy(screenr))
  181. dd = Dy(screenr)-yy;
  182. seek(screenfd, 5*12+bypp*yy*Dx(screenr), 0);
  183. if(readn(screenfd, screenbuf+bypp*yy*Dx(screenr), bypp*Dx(screenr)*dd) != bypp*Dx(screenr)*dd){
  184. fprint(2, "lens: can't read screen: %r\n");
  185. return;
  186. }
  187. for(y=0; y<dy; y++){
  188. yy = yoff+y;
  189. if(yy>=0 && yy<Dy(screenr))
  190. for(x=0; x<dx; x++){
  191. xx = xoff+x;
  192. if(xx>=0 && xx<Dx(screenr)) /* snarf pixel at xx, yy */
  193. for(i=0; i<bypp; i++)
  194. sp[i] = screenbuf[bypp*(yy*Dx(screenr)+xx)+i];
  195. else
  196. sp[0] = sp[1] = sp[2] = sp[3] = 0;
  197. for(xx=0; xx<mag; xx++)
  198. if(x*mag+xx < tmp->r.max.x)
  199. for(i=0; i<bypp; i++)
  200. out[(x*mag+xx)*bypp+i] = sp[i];
  201. }
  202. else
  203. memset(out, 0, bypp*Dx(tmp->r));
  204. for(yy=0; yy<mag && y*mag+yy<Dy(tmp->r); yy++){
  205. werrstr("no error");
  206. if(loadimage(tmp, Rect(0, y*mag+yy, Dx(tmp->r), y*mag+yy+1), out, bypp*Dx(tmp->r)) != bypp*Dx(tmp->r)){
  207. exits("load");
  208. }
  209. }
  210. }
  211. if (showgrid && mag && grid)
  212. draw(tmp, tmp->r, grid, nil, mulpt(Pt(xoff, yoff), mag));
  213. }