mug.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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. #include <cursor.h>
  14. #define initstate muginitstate
  15. typedef struct State State;
  16. struct State {
  17. double black;
  18. double white;
  19. double stretch;
  20. double gamma;
  21. int depth;
  22. int gtab[1001];
  23. Rectangle selr;
  24. };
  25. typedef struct Face Face;
  26. struct Face {
  27. Rectangle r;
  28. State state;
  29. Image *small;
  30. };
  31. double GAMMA = 1.0; /* theory tells me this should be 2.2, but 1.0 sure looks better */
  32. enum {
  33. Left=0,
  34. Right,
  35. Top,
  36. Bottom,
  37. RTopLeft=0,
  38. RTop,
  39. RTopRight,
  40. RLeft,
  41. RMiddle,
  42. RRight,
  43. RBotLeft,
  44. RBot,
  45. RBotRight,
  46. };
  47. void*
  48. emalloc(uint32_t sz)
  49. {
  50. void *v;
  51. v = malloc(sz);
  52. if(v == nil)
  53. sysfatal("malloc %lu fails", sz);
  54. memset(v, 0, sz);
  55. return v;
  56. }
  57. Face *face[8];
  58. int nface;
  59. uint8_t grey2cmap[256];
  60. Image *bkgd;
  61. Image *orig;
  62. Image *ramp, *small, *osmall, *tmp8, *red, *green, *blue;
  63. State state, ostate;
  64. uint8_t val2cmap[256];
  65. uint8_t clamp[3*256];
  66. Rectangle rbig, rramp, rface[nelem(face)], rsmall;
  67. double *rdata;
  68. int sdy, sdx;
  69. void
  70. geometry(Rectangle r)
  71. {
  72. int i;
  73. Rectangle fr[9];
  74. rramp.min = addpt(r.min, Pt(4,4));
  75. rramp.max = addpt(rramp.min, Pt(256,256));
  76. rbig.min = Pt(rramp.max.x+6, rramp.min.y);
  77. rbig.max = addpt(rbig.min, Pt(Dx(orig->r), Dy(orig->r)));
  78. for(i=0; i<9; i++)
  79. fr[i] = rectaddpt(Rect(0,0,48,48), Pt(rramp.min.x+48+56*(i%3), rramp.max.y+6+56*(i/3)));
  80. rsmall = fr[4];
  81. for(i=0; i<4; i++)
  82. rface[i] = fr[i];
  83. for(i=4; i<8; i++)
  84. rface[i] = fr[i+1];
  85. }
  86. double
  87. y2gamma(int y)
  88. {
  89. double g;
  90. g = (double)y / 128.0;
  91. return 0.5+g*g; /* gamma from 0.5 to 4.5, with 1.0 near the middle */
  92. }
  93. int
  94. gamma2y(double g)
  95. {
  96. g -= 0.5;
  97. return (int)(128.0*sqrt(g)+0.5);
  98. }
  99. void
  100. drawface(int i)
  101. {
  102. if(i==-1){
  103. border(screen, rsmall, -3, blue, ZP);
  104. draw(screen, rsmall, small, nil, ZP);
  105. return;
  106. }
  107. border(screen, rface[i], -1, display->black, ZP);
  108. if(face[i])
  109. draw(screen, rface[i], face[i]->small, nil, ZP);
  110. else
  111. draw(screen, rface[i], display->white, nil, ZP);
  112. }
  113. void
  114. drawrampbar(Image *color, State *s)
  115. {
  116. Rectangle liner, r;
  117. static Rectangle br;
  118. if(Dx(br))
  119. draw(screen, br, ramp, nil, subpt(br.min, rramp.min));
  120. r = rramp;
  121. r.max.x = r.min.x + (int)(s->white*255.0);
  122. r.min.x += (int)(s->black*255.0);
  123. r.min.y += gamma2y(s->gamma);
  124. r.max.y = r.min.y+1;
  125. rectclip(&r, rramp);
  126. draw(screen, r, color, nil, ZP);
  127. br = r;
  128. r.min.y -= 2;
  129. r.max.y += 2;
  130. liner = r;
  131. r.min.x += Dx(liner)/3;
  132. r.max.x -= Dx(liner)/3;
  133. rectclip(&r, rramp);
  134. draw(screen, r, color, nil, ZP);
  135. combinerect(&br, r);
  136. r = liner;
  137. r.max.x = r.min.x+3;
  138. rectclip(&r, rramp);
  139. draw(screen, r, color, nil, ZP);
  140. combinerect(&br, r);
  141. r = liner;
  142. r.min.x = r.max.x-3;
  143. rectclip(&r, rramp);
  144. draw(screen, r, color, nil, ZP);
  145. combinerect(&br, r);
  146. }
  147. void
  148. drawscreen(int clear)
  149. {
  150. int i;
  151. if(clear){
  152. geometry(screen->r);
  153. draw(screen, screen->r, bkgd, nil, ZP);
  154. }
  155. border(screen, rbig, -1, display->black, ZP);
  156. draw(screen, rbig, orig, nil, orig->r.min);
  157. border(screen, rramp, -1, display->black, ZP);
  158. draw(screen, rramp, ramp, nil, ramp->r.min);
  159. drawrampbar(red, &state);
  160. border(screen, rectaddpt(state.selr, subpt(rbig.min, orig->r.min)), -2, red, ZP);
  161. if(clear){
  162. drawface(-1);
  163. for(i=0; i<nelem(face); i++)
  164. drawface(i);
  165. }
  166. }
  167. void
  168. moveframe(Rectangle old, Rectangle new)
  169. {
  170. border(screen, rectaddpt(old, subpt(rbig.min, orig->r.min)), -2, orig, old.min);
  171. border(screen, rectaddpt(new, subpt(rbig.min, orig->r.min)), -2, red, ZP);
  172. }
  173. /*
  174. * Initialize gamma ramp; should dither for
  175. * benefit of non-true-color displays.
  176. */
  177. void
  178. initramp(void)
  179. {
  180. int k, x, y;
  181. uint8_t dat[256*256];
  182. double g;
  183. k = 0;
  184. for(y=0; y<256; y++) {
  185. g = y2gamma(y);
  186. for(x=0; x<256; x++)
  187. dat[k++] = 255.0 * pow(x/255.0, g);
  188. }
  189. assert(k == sizeof dat);
  190. ramp = allocimage(display, Rect(0,0,256,256), GREY8, 0, DNofill);
  191. if(ramp == nil)
  192. sysfatal("allocimage: %r");
  193. if(loadimage(ramp, ramp->r, dat, sizeof dat) != sizeof dat)
  194. sysfatal("loadimage: %r");
  195. }
  196. void
  197. initclamp(void)
  198. {
  199. int i;
  200. for(i=0; i<256; i++) {
  201. clamp[i] = 0;
  202. clamp[256+i] = i;
  203. clamp[512+i] = 255;
  204. }
  205. }
  206. void
  207. changestretch(double stretch)
  208. {
  209. state.stretch = stretch;
  210. }
  211. /*
  212. * There is greyscale data for the rectangle datar in data;
  213. * extract square r and write it into the 48x48 pixel image small.
  214. */
  215. void
  216. process(double *data, Rectangle datar, Rectangle r, Image *small)
  217. {
  218. double black, center, delta, *k, shrink, sum, *tmp[48], *tt, w, white, x;
  219. int datadx, dp, dx, dy, error, i, ii, j, jj;
  220. int ksize, ksizeby2, sdata[48*48], sd, sh, sm, sv, u, uu, uuu, v, vv;
  221. uint8_t bdata[48*48];
  222. datadx = Dx(datar);
  223. dx = Dx(r);
  224. dy = Dy(r);
  225. shrink = dx/48.0;
  226. ksize = 1+2*(int)(shrink/2.0);
  227. if(ksize <= 2)
  228. return;
  229. k = emalloc(ksize*sizeof(k[0]));
  230. /* center of box */
  231. for(i=1; i<ksize-1; i++)
  232. k[i] = 1.0;
  233. /* edges */
  234. x = shrink - floor(shrink);
  235. k[0] = x;
  236. k[ksize-1] = x;
  237. sum = 0.0;
  238. for(i=0; i<ksize; i++)
  239. sum += k[i];
  240. for(i=0; i<ksize; i++)
  241. k[i] /= sum;
  242. ksizeby2 = ksize/2;
  243. for(i=0; i<48; i++)
  244. tmp[i] = emalloc(datadx*sizeof(tmp[i][0]));
  245. /* squeeze vertically */
  246. for(i=0; i<48; i++) {
  247. ii = r.min.y+i*dy/48;
  248. tt = tmp[i];
  249. uu = ii - ksizeby2;
  250. for(j=r.min.x-ksize; j<r.max.x+ksize; j++) {
  251. if(j<datar.min.x || j>=datar.max.x)
  252. continue;
  253. w = 0.0;
  254. uuu = uu*datadx+j;
  255. if(uu>=datar.min.y && uu+ksize<datar.max.y)
  256. for(u=0; u<ksize; u++){
  257. w += k[u]*data[uuu];
  258. uuu += datadx;
  259. }
  260. else
  261. for(u=0; u<ksize; u++){
  262. if(uu+u>=datar.min.y && uu+u<datar.max.y)
  263. w += k[u]*data[uuu];
  264. uuu+=datadx;
  265. }
  266. tt[j-datar.min.x] = w;
  267. }
  268. }
  269. /* stretch value scale */
  270. center = (state.black+state.white)/2;
  271. delta = state.stretch*(state.white-state.black)/2;
  272. black = center - delta;
  273. white = center + delta;
  274. /* squeeze horizontally */
  275. for(i=0; i<48; i++) {
  276. tt = tmp[i];
  277. for(j=0; j<48; j++) {
  278. jj = r.min.x+j*dx/48;
  279. w = 0.0;
  280. for(v=0; v<ksize; v++) {
  281. vv = jj - ksizeby2 + v;
  282. if(vv<datar.min.x || vv>=datar.max.x) {
  283. w += k[v]; /* assume white surround */
  284. continue;
  285. }
  286. w += k[v]*tt[vv-datar.min.x];
  287. }
  288. if(w < black || black==white)
  289. w = 0.0;
  290. else if(w > white)
  291. w = 1.0;
  292. else
  293. w = (w-black)/(white-black);
  294. sdata[i*48+j] = state.gtab[(int)(1000.0*w)];
  295. }
  296. }
  297. /* dither to lower depth before copying into GREY8 version */
  298. if(small->chan != GREY8) {
  299. u = 0;
  300. dp = small->depth;
  301. for(i=0; i<48; i++) {
  302. sm = 0xFF ^ (0xFF>>dp);
  303. sh = 0;
  304. v = 0;
  305. for(j=0; j<48; j++) {
  306. ii = 48*i+j;
  307. sd = clamp[sdata[ii]+256];
  308. sv = sd&sm;
  309. v |= sv>>sh;
  310. sh += dp;
  311. if(sh == 8) {
  312. bdata[u++] = v;
  313. v = 0;
  314. sh = 0;
  315. }
  316. /* propagate error, with decay (sum errors < 1) */
  317. error = sd - sv;
  318. if(ii+49 < 48*48) { /* one test is enough, really */
  319. sdata[ii+1] = sdata[ii+1]+((3*error)>>4);
  320. sdata[ii+48] = sdata[ii+48]+((3*error)>>4);
  321. sdata[ii+49] = sdata[ii+49]+((3*error)>>3);
  322. }
  323. /* produce correct color map value by copying bits */
  324. switch(dp){
  325. case 1:
  326. sv |= sv>>1;
  327. case 2:
  328. sv |= sv>>2;
  329. case 4:
  330. sv |= sv>>4;
  331. }
  332. sdata[ii] = sv;
  333. }
  334. }
  335. for(i=0; i<nelem(bdata); i++)
  336. bdata[i] = sdata[i];
  337. if(loadimage(tmp8, tmp8->r, bdata, sizeof bdata) != sizeof bdata)
  338. sysfatal("loadimage: %r");
  339. draw(small, small->r, tmp8, nil, tmp8->r.min);
  340. } else {
  341. for(i=0; i<nelem(bdata); i++)
  342. bdata[i] = sdata[i];
  343. if(loadimage(small, small->r, bdata, sizeof bdata) != sizeof bdata)
  344. sysfatal("loadimage: %r");
  345. }
  346. free(k);
  347. for(i=0; i<48; i++)
  348. free(tmp[i]);
  349. }
  350. void
  351. initval2cmap(void)
  352. {
  353. int i;
  354. for(i=0; i<256; i++)
  355. val2cmap[i] = rgb2cmap(i, i, i);
  356. }
  357. void
  358. setgtab(State *s)
  359. {
  360. int i;
  361. for(i=0; i<=1000; i++)
  362. s->gtab[i] = val2cmap[(int)(255.0*pow((i/1000.0), 1.0/s->gamma))];
  363. }
  364. int
  365. section(int x)
  366. {
  367. int ib, iw;
  368. ib = state.black * 255.0;
  369. iw = state.white * 255.0;
  370. if(x<ib-5 || iw+5<x)
  371. return -1;
  372. iw -= ib;
  373. x -= ib;
  374. if(x < iw/3)
  375. return 0;
  376. if(x < 2*iw/3)
  377. return 1;
  378. return 2;
  379. }
  380. Image*
  381. copyimage(Image *i)
  382. {
  383. Image *n;
  384. if(i == nil)
  385. return nil;
  386. n = allocimage(display, i->r, i->chan, 0, DNofill);
  387. if(n == nil)
  388. sysfatal("allocimage: %r");
  389. draw(n, n->r, i, nil, i->r.min);
  390. return n;
  391. }
  392. Image*
  393. grey8image(Image *i)
  394. {
  395. Image *n;
  396. if(i->chan == GREY8)
  397. return i;
  398. n = allocimage(display, i->r, GREY8, 0, DNofill);
  399. if(n == nil)
  400. sysfatal("allocimage: %r");
  401. draw(n, n->r, i, nil, i->r.min);
  402. freeimage(i);
  403. return n;
  404. }
  405. void
  406. mark(void)
  407. {
  408. if(osmall != small){
  409. freeimage(osmall);
  410. osmall = small;
  411. }
  412. ostate = state;
  413. }
  414. void
  415. undo(void)
  416. {
  417. if(small != osmall){
  418. freeimage(small);
  419. small = osmall;
  420. }
  421. state = ostate;
  422. process(rdata, orig->r, state.selr, small);
  423. drawface(-1);
  424. drawscreen(0);
  425. }
  426. void
  427. saveface(Face *f, int slot)
  428. {
  429. if(slot == -1){
  430. mark();
  431. state = f->state;
  432. small = copyimage(f->small);
  433. drawface(-1);
  434. drawscreen(0);
  435. return;
  436. }
  437. if(face[slot]==nil)
  438. face[slot] = emalloc(sizeof(*face[slot]));
  439. else{
  440. freeimage(face[slot]->small);
  441. face[slot]->small = nil;
  442. }
  443. if(f == nil){
  444. face[slot]->small = copyimage(small);
  445. face[slot]->state = state;
  446. }else{
  447. face[slot]->small = copyimage(f->small);
  448. face[slot]->state = f->state;
  449. }
  450. drawface(slot);
  451. }
  452. int
  453. writeface(char *outfile, Image *image)
  454. {
  455. int i, fd, rv, y;
  456. uint8_t data[48*48/2];
  457. if(outfile == nil)
  458. fd = 1;
  459. else{
  460. if((fd = create(outfile, OWRITE, 0666)) < 0)
  461. return -1;
  462. }
  463. switch(image->chan) {
  464. default:
  465. rv = -1;
  466. break;
  467. case GREY1:
  468. if(unloadimage(image, image->r, data, 48*48/8) != 48*48/8)
  469. sysfatal("unloadimage: %r");
  470. for(y=0; y<48; y++) {
  471. for(i=0; i<3; i++)
  472. fprint(fd, "0x%.2x%.2x,", data[y*6+i*2+0], data[y*6+i*2+1]);
  473. fprint(fd, "\n");
  474. }
  475. rv = 0;
  476. break;
  477. case GREY2:
  478. if(unloadimage(image, image->r, data, 48*48/4) != 48*48/4)
  479. sysfatal("unloadimage: %r");
  480. for(y=0; y<48; y++) {
  481. for(i=0; i<3; i++)
  482. fprint(fd, "0x%.2x%.2x,%.2x%.2x,",
  483. data[y*12+i*4+0], data[y*12+i*4+1],
  484. data[y*12+i*4+2], data[y*12+i*4+3]);
  485. fprint(fd, "\n");
  486. }
  487. rv = 0;
  488. break;
  489. case GREY4:
  490. case GREY8:
  491. rv = writeimage(fd, image, 0); /* dolock? */
  492. break;
  493. }
  494. if(outfile)
  495. close(fd);
  496. return rv;
  497. }
  498. void
  499. room(Rectangle out, Rectangle in, int *a)
  500. {
  501. a[Left] = out.min.x - in.min.x;
  502. a[Right] = out.max.x - in.max.x;
  503. a[Top] = out.min.y - in.min.y;
  504. a[Bottom] = out.max.y - in.max.y;
  505. }
  506. int
  507. min(int a, int b)
  508. {
  509. if(a < b)
  510. return a;
  511. return b;
  512. }
  513. int
  514. max(int a, int b)
  515. {
  516. if(a > b)
  517. return a;
  518. return b;
  519. }
  520. int
  521. move(Rectangle r, Rectangle picr, Point d, int k, Rectangle *rp)
  522. {
  523. int a[4], i;
  524. Rectangle oldr;
  525. static int toggle;
  526. oldr = r;
  527. room(picr, r, a);
  528. switch(k){
  529. case RTopLeft:
  530. i = (d.x+d.y)/2;
  531. if(i>=Dx(r) || i>=Dy(r))
  532. break;
  533. i = max(i, a[Left]);
  534. i = max(i, a[Top]);
  535. r.min.x += i;
  536. r.min.y += i;
  537. break;
  538. case RTop:
  539. i = d.y;
  540. if(i < 0){
  541. /*
  542. * should really check i/2, but this is safe and feedback
  543. * makes the control feel right
  544. */
  545. i = -min(-i, a[Right]);
  546. i = max(i, a[Left]);
  547. }
  548. i = max(i, a[Top]);
  549. if(i >= Dy(r))
  550. break;
  551. r.min.y += i;
  552. /* divide the half bit equally */
  553. toggle = 1-toggle;
  554. if(toggle){
  555. r.min.x += i/2;
  556. r.max.x = r.min.x+Dy(r);
  557. }else{
  558. r.max.x -= i/2;
  559. r.min.x = r.max.x-Dy(r);
  560. }
  561. break;
  562. case RTopRight:
  563. i = (-d.x+d.y)/2;
  564. if(i>=Dx(r) || i>=Dy(r))
  565. break;
  566. i = -min(-i, a[Right]);
  567. i = max(i, a[Top]);
  568. r.max.x -= i;
  569. r.min.y += i;
  570. break;
  571. case RLeft:
  572. i = d.x;
  573. if(i < 0){
  574. i = -min(-i, a[Bottom]);
  575. i = max(i, a[Top]);
  576. }
  577. i = max(i, a[Left]);
  578. if(i >= Dx(r))
  579. break;
  580. r.min.x += i;
  581. /* divide the half bit equally */
  582. toggle = 1-toggle;
  583. if(toggle){
  584. r.min.y += i/2;
  585. r.max.y = r.min.y+Dx(r);
  586. }else{
  587. r.max.y -= i/2;
  588. r.min.y = r.max.y-Dx(r);
  589. }
  590. break;
  591. case RMiddle:
  592. if(d.x >= 0)
  593. d.x = min(d.x, a[Right]);
  594. else
  595. d.x = max(d.x, a[Left]);
  596. if(d.y >= 0)
  597. d.y = min(d.y, a[Bottom]);
  598. else
  599. d.y = max(d.y, a[Top]);
  600. r = rectaddpt(r, d);
  601. break;
  602. case RRight:
  603. i = d.x;
  604. if(i > 0){
  605. i = min(i, a[Bottom]);
  606. i = -max(-i, a[Top]);
  607. }
  608. i = min(i, a[Right]);
  609. if(-i >= Dx(r))
  610. break;
  611. r.max.x += i;
  612. /* divide the half bit equally */
  613. toggle = 1-toggle;
  614. if(toggle){
  615. r.min.y -= i/2;
  616. r.max.y = r.min.y+Dx(r);
  617. }else{
  618. r.max.y += i/2;
  619. r.min.y = r.max.y-Dx(r);
  620. }
  621. break;
  622. case RBotLeft:
  623. i = (d.x+-d.y)/2;
  624. if(i>=Dx(r) || i>=Dy(r))
  625. break;
  626. i = max(i, a[Left]);
  627. i = -min(-i, a[Bottom]);
  628. r.min.x += i;
  629. r.max.y -= i;
  630. break;
  631. case RBot:
  632. i = d.y;
  633. if(i > 0){
  634. i = min(i, a[Right]);
  635. i = -max(-i, a[Left]);
  636. }
  637. i = min(i, a[Bottom]);
  638. if(i >= Dy(r))
  639. break;
  640. r.max.y += i;
  641. /* divide the half bit equally */
  642. toggle = 1-toggle;
  643. if(toggle){
  644. r.min.x -= i/2;
  645. r.max.x = r.min.x+Dy(r);
  646. }else{
  647. r.max.x += i/2;
  648. r.min.x = r.max.x-Dy(r);
  649. }
  650. break;
  651. case RBotRight:
  652. i = (-d.x+-d.y)/2;
  653. if(i>=Dx(r) || i>=Dy(r))
  654. break;
  655. i = -min(-i, a[Right]);
  656. i = -min(-i, a[Bottom]);
  657. r.max.x -= i;
  658. r.max.y -= i;
  659. break;
  660. }
  661. if(Dx(r)<3 || Dy(r)<3){
  662. *rp = oldr;
  663. return 0;
  664. }
  665. *rp = r;
  666. return !eqrect(r, oldr);
  667. }
  668. void
  669. rlist(Rectangle r, Rectangle *ra)
  670. {
  671. Rectangle tr;
  672. tr = r;
  673. tr.max.y = r.min.y+Dy(r)/4;
  674. ra[0] = tr;
  675. ra[0].max.x = tr.min.x+Dx(tr)/4;
  676. ra[1] = tr;
  677. ra[1].min.x = ra[0].max.x;
  678. ra[1].max.x = tr.max.x-Dx(tr)/4;
  679. ra[2] = tr;
  680. ra[2].min.x = ra[1].max.x;
  681. tr.min.y = tr.max.y;
  682. tr.max.y = r.max.y-Dy(r)/4;
  683. ra[3] = tr;
  684. ra[3].max.x = tr.min.x+Dx(tr)/4;
  685. ra[4] = tr;
  686. ra[4].min.x = ra[3].max.x;
  687. ra[4].max.x = tr.max.x-Dx(tr)/4;
  688. ra[5] = tr;
  689. ra[5].min.x = ra[4].max.x;
  690. tr.min.y = tr.max.y;
  691. tr.max.y = r.max.y;
  692. ra[6] = tr;
  693. ra[6].max.x = tr.min.x+Dx(tr)/4;
  694. ra[7] = tr;
  695. ra[7].min.x = ra[6].max.x;
  696. ra[7].max.x = tr.max.x-Dx(tr)/4;
  697. ra[8] = tr;
  698. ra[8].min.x = ra[7].max.x;
  699. }
  700. int
  701. abs(int a)
  702. {
  703. if(a < 0)
  704. return -a;
  705. return a;
  706. }
  707. void
  708. usage(void)
  709. {
  710. fprint(2, "usage: mug [file.bit]\n");
  711. exits("usage");
  712. }
  713. void
  714. eresized(int new)
  715. {
  716. if(new && getwindow(display, Refmesg) < 0)
  717. fprint(2,"can't reattach to window");
  718. drawscreen(1);
  719. }
  720. /*
  721. interface notes
  722. cursor changes while in rbig to indicate region.
  723. only button 1 works for resizing region
  724. only button 1 works for moving thingy in ramp
  725. button-3 menu: Reset, Depth, Undo, Save, Write
  726. */
  727. Cursor tl = {
  728. {-4, -4},
  729. {0xfe, 0x00, 0x82, 0x00, 0x8c, 0x00, 0x87, 0xff,
  730. 0xa0, 0x01, 0xb0, 0x01, 0xd0, 0x01, 0x11, 0xff,
  731. 0x11, 0x00, 0x11, 0x00, 0x11, 0x00, 0x11, 0x00,
  732. 0x11, 0x00, 0x11, 0x00, 0x11, 0x00, 0x1f, 0x00, },
  733. {0x00, 0x00, 0x7c, 0x00, 0x70, 0x00, 0x78, 0x00,
  734. 0x5f, 0xfe, 0x4f, 0xfe, 0x0f, 0xfe, 0x0e, 0x00,
  735. 0x0e, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x0e, 0x00,
  736. 0x0e, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x00, 0x00, }
  737. };
  738. Cursor t = {
  739. {-7, -8},
  740. {0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x06, 0xc0,
  741. 0x1c, 0x70, 0x10, 0x10, 0x0c, 0x60, 0xfc, 0x7f,
  742. 0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0xff, 0xff,
  743. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
  744. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  745. 0x03, 0x80, 0x0f, 0xe0, 0x03, 0x80, 0x03, 0x80,
  746. 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe, 0x00, 0x00,
  747. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }
  748. };
  749. Cursor tr = {
  750. {-11, -4},
  751. {0x00, 0x7f, 0x00, 0x41, 0x00, 0x31, 0xff, 0xe1,
  752. 0x80, 0x05, 0x80, 0x0d, 0x80, 0x0b, 0xff, 0x88,
  753. 0x00, 0x88, 0x0, 0x88, 0x00, 0x88, 0x00, 0x88,
  754. 0x00, 0x88, 0x00, 0x88, 0x00, 0x88, 0x00, 0xf8, },
  755. {0x00, 0x00, 0x00, 0x3e, 0x00, 0x0e, 0x00, 0x1e,
  756. 0x7f, 0xfa, 0x7f, 0xf2, 0x7f, 0xf0, 0x00, 0x70,
  757. 0x00, 0x70, 0x00, 0x70, 0x00, 0x70, 0x00, 0x70,
  758. 0x00, 0x70, 0x00, 0x70, 0x00, 0x70, 0x00, 0x00, }
  759. };
  760. Cursor r = {
  761. {-8, -7},
  762. {0x07, 0xc0, 0x04, 0x40, 0x04, 0x40, 0x04, 0x58,
  763. 0x04, 0x68, 0x04, 0x6c, 0x04, 0x06, 0x04, 0x02,
  764. 0x04, 0x06, 0x04, 0x6c, 0x04, 0x68, 0x04, 0x58,
  765. 0x04, 0x40, 0x04, 0x40, 0x04, 0x40, 0x07, 0xc0, },
  766. {0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80,
  767. 0x03, 0x90, 0x03, 0x90, 0x03, 0xf8, 0x03, 0xfc,
  768. 0x03, 0xf8, 0x03, 0x90, 0x03, 0x90, 0x03, 0x80,
  769. 0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x00, 0x00, }
  770. };
  771. Cursor br = {
  772. {-11, -11},
  773. {0x00, 0xf8, 0x00, 0x88, 0x00, 0x88, 0x00, 0x88,
  774. 0x00, 0x88, 0x00, 0x88, 0x00, 0x88, 0x00, 0x88,
  775. 0xff, 0x88, 0x80, 0x0b, 0x80, 0x0d, 0x80, 0x05,
  776. 0xff, 0xe1, 0x00, 0x31, 0x00, 0x41, 0x00, 0x7f, },
  777. {0x00, 0x00, 0x00, 0x70, 0x00, 0x70, 0x00, 0x70,
  778. 0x0, 0x70, 0x00, 0x70, 0x00, 0x70, 0x00, 0x70,
  779. 0x00, 0x70, 0x7f, 0xf0, 0x7f, 0xf2, 0x7f, 0xfa,
  780. 0x00, 0x1e, 0x00, 0x0e, 0x00, 0x3e, 0x00, 0x00, }
  781. };
  782. Cursor b = {
  783. {-7, -7},
  784. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  785. 0xff, 0xff, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
  786. 0xfc, 0x7f, 0x0c, 0x60, 0x10, 0x10, 0x1c, 0x70,
  787. 0x06, 0xc0, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, },
  788. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  789. 0x00, 0x00, 0x7f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfe,
  790. 0x03, 0x80, 0x03, 0x80, 0x0f, 0xe0, 0x03, 0x80,
  791. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }
  792. };
  793. Cursor bl = {
  794. {-4, -11},
  795. {0x1f, 0x00, 0x11, 0x00, 0x11, 0x00, 0x11, 0x00,
  796. 0x11, 0x00, 0x11, 0x00, 0x11, 0x00, 0x11, 0x00,
  797. 0x11, 0xff, 0xd0, 0x01, 0xb0, 0x01, 0xa0, 0x01,
  798. 0x87, 0xff, 0x8c, 0x00, 0x82, 0x00, 0xfe, 0x00, },
  799. {0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x0e, 0x00,
  800. 0x0e, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x0e, 0x00,
  801. 0x0e, 0x00, 0x0f, 0xfe, 0x4f, 0xfe, 0x5f, 0xfe,
  802. 0x78, 0x00, 0x70, 0x00, 0x7c, 0x00, 0x00, 0x0, }
  803. };
  804. Cursor l = {
  805. {-7, -7},
  806. {0x03, 0xe0, 0x02, 0x20, 0x02, 0x20, 0x1a, 0x20,
  807. 0x16, 0x20, 0x36, 0x20, 0x60, 0x20, 0x40, 0x20,
  808. 0x60, 0x20, 0x36, 0x20, 0x16, 0x20, 0x1a, 0x20,
  809. 0x02, 0x20, 0x02, 0x20, 0x02, 0x20, 0x03, 0xe0, },
  810. {0x00, 0x00, 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0,
  811. 0x09, 0xc0, 0x09, 0xc0, 0x1f, 0xc0, 0x3f, 0xc0,
  812. 0x1f, 0xc0, 0x09, 0xc0, 0x09, 0xc0, 0x01, 0xc0,
  813. 0x01, 0xc0, 0x01, 0xc0, 0x01, 0xc0, 0x00, 0x00, }
  814. };
  815. Cursor boxcursor = {
  816. {-7, -7},
  817. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  818. 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
  819. 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
  820. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, },
  821. {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
  822. 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
  823. 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
  824. 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00, }
  825. };
  826. Cursor clearcursor;
  827. Cursor *corners[10] = {
  828. &tl, &t, &tr,
  829. &l, &boxcursor, &r,
  830. &bl, &b, &br,
  831. nil, /* default arrow */
  832. };
  833. char *item[] = {
  834. "Reset",
  835. "Depth",
  836. "Undo",
  837. "Write",
  838. "Exit",
  839. nil
  840. };
  841. Menu menu = {
  842. item,
  843. nil,
  844. 2
  845. };
  846. /*BUG make less flashy */
  847. void
  848. moveface(Image *back, Point lastp, Image *face, Point p, Point d)
  849. {
  850. draw(screen, rectaddpt(back->r, subpt(lastp, d)), back, nil, back->r.min);
  851. draw(back, back->r, screen, nil, addpt(back->r.min, subpt(p, d)));
  852. border(screen, rectaddpt(face->r, subpt(p, d)),
  853. -1, display->black, ZP);
  854. draw(screen, rectaddpt(face->r, subpt(p, d)),
  855. face, nil, face->r.min);
  856. }
  857. int
  858. dragface(Mouse *m, Image *im, Point d, int x)
  859. {
  860. int i;
  861. Point lastp;
  862. static Image *back;
  863. if(back == nil){
  864. back = allocimage(display, Rect(-1,-1,49,49), display->image->chan, 0, DNofill);
  865. if(back == nil)
  866. sysfatal("dragface backing store: %r");
  867. }
  868. lastp = m->xy;
  869. draw(back, back->r, screen, nil, addpt(back->r.min, subpt(lastp, d)));
  870. esetcursor(&clearcursor);
  871. do{
  872. moveface(back, lastp, im, m->xy, d);
  873. lastp = m->xy;
  874. }while(*m=emouse(), m->buttons==1);
  875. draw(screen, rectaddpt(back->r, subpt(lastp, d)), back, nil, back->r.min);
  876. esetcursor(nil);
  877. if(m->buttons==0){
  878. for(i=0; i<nelem(face); i++)
  879. if(ptinrect(m->xy, rface[i]))
  880. return i;
  881. if(ptinrect(m->xy, rsmall))
  882. return -1;
  883. return x;
  884. }
  885. while(*m=emouse(), m->buttons)
  886. ;
  887. return x;
  888. }
  889. void
  890. initstate(void)
  891. {
  892. state.black = 0.0;
  893. state.white = 1.0;
  894. state.stretch = 1.0;
  895. state.depth = 4;
  896. state.gamma = 1.0;
  897. setgtab(&state);
  898. state.selr = insetrect(orig->r, 5);
  899. sdx = Dx(state.selr);
  900. sdy = Dy(state.selr);
  901. if(sdx > sdy)
  902. state.selr.max.x = state.selr.min.x+sdy;
  903. else
  904. state.selr.max.y = state.selr.min.y+sdx;
  905. }
  906. void
  907. main(int argc, char **argv)
  908. {
  909. int ccursor, i, fd, k, n, y;
  910. unsigned char *data;
  911. double gammatab[256];
  912. Event e;
  913. Mouse m;
  914. Point lastp, p = {};
  915. Rectangle nselr, rbig9[9];
  916. ARGBEGIN{
  917. default:
  918. usage();
  919. }ARGEND
  920. if(argc > 1)
  921. usage();
  922. if(argc == 1){
  923. if((fd = open(argv[0], OREAD)) < 0)
  924. sysfatal("open %s: %r", argv[0]);
  925. }else
  926. fd = 0;
  927. if (initdraw(0, 0, "mug") < 0)
  928. sysfatal("initdraw failed");
  929. if((orig = readimage(display, fd, 0)) == nil)
  930. sysfatal("readimage: %r");
  931. orig = grey8image(orig);
  932. initramp();
  933. initclamp();
  934. initval2cmap();
  935. bkgd = allocimagemix(display, DPaleyellow, DWhite);
  936. small = allocimage(display, Rect(0,0,48,48), GREY4, 0, DWhite);
  937. tmp8 = allocimage(display, Rect(0,0,48,48), GREY8, 0, DWhite);
  938. red = allocimage(display, Rect(0,0,1,1), display->image->chan, 1, DRed);
  939. green = allocimage(display, Rect(0,0,1,1), display->image->chan, 1, DGreen);
  940. blue = allocimage(display, Rect(0,0,1,1), display->image->chan, 1, DBlue);
  941. if(bkgd==nil || small==nil || tmp8==nil || red==nil || green==nil || blue==nil)
  942. sysfatal("allocimage: %r");
  943. n = Dx(orig->r)*Dy(orig->r);
  944. data = emalloc(n*sizeof data[0]);
  945. rdata = emalloc(n*sizeof rdata[0]);
  946. if(unloadimage(orig, orig->r, data, n) != n)
  947. sysfatal("unloadimage: %r");
  948. for(i=0; i<256; i++)
  949. gammatab[i] = pow((255-i)/(double)255.0, GAMMA);
  950. for(i=0; i<n; i++)
  951. rdata[i] = gammatab[255-data[i]];
  952. initstate();
  953. process(rdata, orig->r, state.selr, small);
  954. drawscreen(1);
  955. flushimage(display, 1);
  956. einit(Emouse|Ekeyboard);
  957. ccursor = 9;
  958. for(;;){
  959. if((n=eread(Emouse|Ekeyboard, &e))==Ekeyboard)
  960. continue;
  961. if(n != Emouse)
  962. break;
  963. m = e.mouse;
  964. if(m.buttons&4){
  965. ccursor = 9;
  966. esetcursor(corners[ccursor]);
  967. switch(emenuhit(3, &m, &menu)){
  968. case -1:
  969. continue;
  970. case 0: /* Reset */
  971. mark();
  972. initstate();
  973. small = allocimage(display, Rect(0,0,48,48), CHAN1(CGrey, state.depth), 0, DWhite);
  974. if(small == nil)
  975. sysfatal("allocimage: %r");
  976. process(rdata, orig->r, state.selr, small);
  977. drawface(-1);
  978. drawscreen(0);
  979. break;
  980. case 1: /* Depth */
  981. mark();
  982. /* osmall = small, so no freeimage */
  983. state.depth /= 2;
  984. if(state.depth == 0)
  985. state.depth = 8;
  986. small = allocimage(display, Rect(0,0,48,48), CHAN1(CGrey, state.depth), 0, DWhite);
  987. if(small == nil)
  988. sysfatal("allocimage: %r");
  989. process(rdata, orig->r, state.selr, small);
  990. drawface(-1);
  991. break;
  992. case 2: /* Undo */
  993. undo();
  994. break;
  995. case 3: /* Write */
  996. writeface(nil, small);
  997. break;
  998. case 4: /* Exit */
  999. exits(nil);
  1000. break;
  1001. }
  1002. }
  1003. if(ptinrect(m.xy, rbig)){
  1004. rlist(rectaddpt(state.selr, subpt(rbig.min, orig->r.min)), rbig9);
  1005. for(i=0; i<9; i++)
  1006. if(ptinrect(m.xy, rbig9[i]))
  1007. break;
  1008. if(i != ccursor){
  1009. ccursor = i;
  1010. esetcursor(corners[ccursor]);
  1011. }
  1012. if(i==9)
  1013. continue;
  1014. if(m.buttons & 1){
  1015. mark();
  1016. lastp = m.xy;
  1017. while(m=emouse(), m.buttons&1){
  1018. if(move(state.selr, orig->r, subpt(m.xy, lastp), i, &nselr)){
  1019. moveframe(state.selr, nselr);
  1020. state.selr = nselr;
  1021. lastp = m.xy;
  1022. process(rdata, orig->r, state.selr, small);
  1023. drawface(-1);
  1024. }
  1025. }
  1026. }
  1027. continue;
  1028. }
  1029. if(ccursor != 9){ /* default cursor */
  1030. ccursor = 9;
  1031. esetcursor(corners[ccursor]);
  1032. }
  1033. if(ptinrect(m.xy, rramp)){
  1034. if(m.buttons != 1)
  1035. continue;
  1036. mark();
  1037. y = gamma2y(state.gamma);
  1038. if(abs(y-(m.xy.y-rramp.min.y)) > 5)
  1039. continue;
  1040. k = section(m.xy.x-rramp.min.x);
  1041. drawrampbar(green, &state);
  1042. lastp = m.xy;
  1043. while(m=emouse(), m.buttons&1){
  1044. if(!ptinrect(m.xy, rramp))
  1045. continue;
  1046. switch(k){
  1047. case -1:
  1048. continue;
  1049. case 0:
  1050. if((m.xy.x-rramp.min.x)/255.0 < state.white){
  1051. state.black = (m.xy.x-rramp.min.x)/255.0;
  1052. break;
  1053. }
  1054. continue;
  1055. case 1:
  1056. state.gamma = y2gamma(m.xy.y-rramp.min.y);
  1057. setgtab(&state);
  1058. break;
  1059. case 2:
  1060. if((m.xy.x-rramp.min.x)/255.0 > state.black){
  1061. state.white = (m.xy.x-rramp.min.x)/255.0;
  1062. break;
  1063. }
  1064. continue;
  1065. case 10:
  1066. state.black += (m.xy.x-lastp.x)/255.0;
  1067. state.white += (m.xy.x-lastp.x)/255.0;
  1068. state.gamma = y2gamma(p.y);
  1069. break;
  1070. }
  1071. process(rdata, orig->r, state.selr, small);
  1072. drawface(-1);
  1073. drawrampbar(green, &state);
  1074. }
  1075. if(m.buttons == 0){
  1076. process(rdata, orig->r, state.selr, small);
  1077. drawface(-1);
  1078. drawrampbar(red, &state);
  1079. }else
  1080. undo();
  1081. continue;
  1082. }
  1083. if(ptinrect(m.xy, rsmall)){
  1084. if(m.buttons != 1)
  1085. continue;
  1086. n=dragface(&m, small, subpt(m.xy, rsmall.min), -1);
  1087. if(n == -1)
  1088. continue;
  1089. saveface(nil, n);
  1090. }
  1091. for(i=0; i<nelem(face); i++)
  1092. if(ptinrect(m.xy, rface[i]))
  1093. break;
  1094. if(i<nelem(face) && face[i] != nil){
  1095. if(m.buttons != 1)
  1096. continue;
  1097. n=dragface(&m, face[i]->small, subpt(m.xy, rface[i].min), i);
  1098. if(n == i)
  1099. continue;
  1100. saveface(face[i], n);
  1101. continue;
  1102. }
  1103. do
  1104. m = emouse();
  1105. while(m.buttons==1);
  1106. }
  1107. exits(nil);
  1108. }