lens.c 4.8 KB

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