gif.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include <u.h>
  2. #include <lib9.h>
  3. #include <bio.h>
  4. #include <draw.h>
  5. #include <event.h>
  6. #include <keyboard.h>
  7. #include "imagefile.h"
  8. int cflag = 0;
  9. int dflag = 0;
  10. int eflag = 0;
  11. int nineflag = 0;
  12. int threeflag = 0;
  13. int output = 0;
  14. uint32_t outchan = CMAP8;
  15. Image **allims;
  16. int which;
  17. int defaultcolor = 1;
  18. enum{
  19. Border = 2,
  20. Edge = 5
  21. };
  22. char *show(int, char*);
  23. Rectangle
  24. imager(void)
  25. {
  26. Point p1, p2;
  27. if(allims==nil || allims[0]==nil)
  28. return screen->r;
  29. p1 = addpt(divpt(subpt(allims[0]->r.max, allims[0]->r.min), 2), allims[0]->r.min);
  30. p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
  31. return rectaddpt(allims[0]->r, subpt(p2, p1));
  32. }
  33. void
  34. eresized(int new)
  35. {
  36. Rectangle r;
  37. if(new && getwindow(display, Refnone) < 0){
  38. fprint(2, "gif: can't reattach to window\n");
  39. exits("resize");
  40. }
  41. if(allims==nil || allims[which]==nil)
  42. return;
  43. r = imager();
  44. border(screen, r, -Border, nil, ZP);
  45. draw(screen, r, allims[which], nil, allims[which]->r.min);
  46. flushimage(display, 1);
  47. }
  48. void
  49. main(int argc, char *argv[])
  50. {
  51. int fd, i;
  52. char *err;
  53. ARGBEGIN{
  54. case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
  55. threeflag++;
  56. /* fall through */
  57. case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
  58. cflag++;
  59. dflag++;
  60. output++;
  61. defaultcolor = 0;
  62. outchan = RGB24;
  63. break;
  64. case 'c': /* produce encoded, compressed, bitmap file; no display by default */
  65. cflag++;
  66. dflag++;
  67. output++;
  68. if(defaultcolor)
  69. outchan = CMAP8;
  70. break;
  71. case 'd': /* suppress display of image */
  72. dflag++;
  73. break;
  74. case 'e': /* disable floyd-steinberg error diffusion */
  75. eflag++;
  76. break;
  77. case 'k': /* force black and white */
  78. defaultcolor = 0;
  79. outchan = GREY8;
  80. break;
  81. case 'v': /* force RGBV */
  82. defaultcolor = 0;
  83. outchan = CMAP8;
  84. break;
  85. case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
  86. nineflag++;
  87. dflag++;
  88. output++;
  89. if(defaultcolor)
  90. outchan = CMAP8;
  91. break;
  92. default:
  93. fprint(2, "usage: gif -39cdektv [file.gif ...]\n");
  94. exits("usage");
  95. }ARGEND;
  96. err = nil;
  97. if(argc == 0)
  98. err = show(0, "<stdin>");
  99. else{
  100. for(i=0; i<argc; i++){
  101. fd = sys_open(argv[i], OREAD);
  102. if(fd < 0){
  103. fprint(2, "gif: can't open %s: %r\n", argv[i]);
  104. err = "open";
  105. }else{
  106. err = show(fd, argv[i]);
  107. sys_close(fd);
  108. }
  109. if(output && argc>1 && err==nil){
  110. fprint(2, "gif: exiting after one file\n");
  111. break;
  112. }
  113. }
  114. }
  115. exits(err);
  116. }
  117. Image*
  118. transparency(Rawimage *r, char *name)
  119. {
  120. Image *i;
  121. int j, index;
  122. unsigned char *pic, *mpic, *mask;
  123. if((r->gifflags&TRANSP) == 0)
  124. return nil;
  125. i = allocimage(display, r->r, GREY8, 0, 0);
  126. if(i == nil){
  127. fprint(2, "gif: allocimage for mask of %s failed: %r\n", name);
  128. return nil;
  129. }
  130. pic = r->chans[0];
  131. mask = malloc(r->chanlen);
  132. if(mask == nil){
  133. fprint(2, "gif: malloc for mask of %s failed: %r\n", name);
  134. freeimage(i);
  135. return nil;
  136. }
  137. index = r->giftrindex;
  138. mpic = mask;
  139. for(j=0; j<r->chanlen; j++)
  140. if(*pic++ == index)
  141. *mpic++ = 0;
  142. else
  143. *mpic++ = 0xFF;
  144. if(loadimage(i, i->r, mask, r->chanlen) < 0){
  145. fprint(2, "gif: loadimage for mask of %s failed: %r\n", name);
  146. free(mask);
  147. freeimage(i);
  148. return nil;
  149. }
  150. free(mask);
  151. return i;
  152. }
  153. /* interleave alpha values of 0xFF in data stream. alpha value comes first, then b g r */
  154. unsigned char*
  155. expand(unsigned char *u, int chanlen, int nchan)
  156. {
  157. int j, k;
  158. unsigned char *v, *up, *vp;
  159. v = malloc(chanlen*(nchan+1));
  160. if(v == nil){
  161. fprint(2, "gif: malloc fails: %r\n");
  162. exits("malloc");
  163. }
  164. up = u;
  165. vp = v;
  166. for(j=0; j<chanlen; j++){
  167. *vp++ = 0xFF;
  168. for(k=0; k<nchan; k++)
  169. *vp++ = *up++;
  170. }
  171. return v;
  172. }
  173. void
  174. addalpha(Rawimage *i)
  175. {
  176. char buf[32];
  177. switch(outchan){
  178. case CMAP8:
  179. i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
  180. i->chanlen = 2*(i->chanlen/1);
  181. i->chandesc = CRGBVA16;
  182. outchan = CHAN2(CMap, 8, CAlpha, 8);
  183. break;
  184. case GREY8:
  185. i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
  186. i->chanlen = 2*(i->chanlen/1);
  187. i->chandesc = CYA16;
  188. outchan = CHAN2(CGrey, 8, CAlpha, 8);
  189. break;
  190. case RGB24:
  191. i->chans[0] = expand(i->chans[0], i->chanlen/3, 3);
  192. i->chanlen = 4*(i->chanlen/3);
  193. i->chandesc = CRGBA32;
  194. outchan = RGBA32;
  195. break;
  196. default:
  197. chantostr(buf, outchan);
  198. fprint(2, "gif: can't add alpha to type %s\n", buf);
  199. exits("err");
  200. }
  201. }
  202. /*
  203. * Called only when writing output. If the output is RGBA32,
  204. * we must write four bytes per pixel instead of two.
  205. * There's always at least two: data plus alpha.
  206. * r is used only for reference; the image is already in c.
  207. */
  208. void
  209. blackout(Rawimage *r, Rawimage *c)
  210. {
  211. int i, trindex;
  212. unsigned char *rp, *cp;
  213. rp = r->chans[0];
  214. cp = c->chans[0];
  215. trindex = r->giftrindex;
  216. if(outchan == RGBA32)
  217. for(i=0; i<r->chanlen; i++){
  218. if(*rp == trindex){
  219. *cp++ = 0x00;
  220. *cp++ = 0x00;
  221. *cp++ = 0x00;
  222. *cp++ = 0x00;
  223. }else{
  224. *cp++ = 0xFF;
  225. cp += 3;
  226. }
  227. rp++;
  228. }
  229. else
  230. for(i=0; i<r->chanlen; i++){
  231. if(*rp == trindex){
  232. *cp++ = 0x00;
  233. *cp++ = 0x00;
  234. }else{
  235. *cp++ = 0xFF;
  236. cp++;
  237. }
  238. rp++;
  239. }
  240. }
  241. int
  242. init(void)
  243. {
  244. static int inited;
  245. if(inited == 0){
  246. if(initdraw(0, 0, 0) < 0){
  247. fprint(2, "gif: initdraw failed: %r\n");
  248. return -1;
  249. }
  250. einit(Ekeyboard|Emouse);
  251. inited++;
  252. }
  253. return 1;
  254. }
  255. char*
  256. show(int fd, char *name)
  257. {
  258. Rawimage **images, **rgbv;
  259. Image *tmp, *msk, *img, *dst, **ims;
  260. Rectangle r;
  261. int j, k, n, ch, nloop, loopcount, dt;
  262. char *err;
  263. char buf[32];
  264. err = nil;
  265. images = readgif(fd, CRGB, dflag);
  266. if(images == nil){
  267. fprint(2, "gif: decode %s failed: %r\n", name);
  268. return "decode";
  269. }
  270. for(n=0; images[n]; n++)
  271. if(n == 0)
  272. r = images[n]->r;
  273. else
  274. combinerect(&r, images[n]->r);
  275. tmp = nil;
  276. ims = malloc((n+1)*sizeof(Image*));
  277. rgbv = malloc((n+1)*sizeof(Rawimage*));
  278. if(rgbv==nil || ims==nil){
  279. fprint(2, "gif: malloc of masks for %s failed: %r\n", name);
  280. err = "malloc";
  281. goto Return;
  282. }
  283. memset(ims, 0, (n+1)*sizeof(Image*));
  284. memset(rgbv, 0, (n+1)*sizeof(Rawimage*));
  285. if(!dflag){
  286. if(init() < 0){
  287. err = "initdraw";
  288. goto Return;
  289. }
  290. if(defaultcolor && screen->depth>8)
  291. outchan = RGB24;
  292. }
  293. for(k=0; k<n; k++){
  294. if(outchan == CMAP8)
  295. rgbv[k] = torgbv(images[k], !eflag);
  296. else{
  297. if(outchan==GREY8 || (images[k]->chandesc==CY && threeflag==0))
  298. rgbv[k] = totruecolor(images[k], CY);
  299. else
  300. rgbv[k] = totruecolor(images[k], CRGB24);
  301. }
  302. if(rgbv[k] == nil){
  303. fprint(2, "gif: converting %s to local format failed: %r\n", name);
  304. err = "torgbv";
  305. goto Return;
  306. }
  307. if(!dflag){
  308. msk = transparency(images[k], name);
  309. if(rgbv[k]->chandesc == CY)
  310. img = allocimage(display, rgbv[k]->r, GREY8, 0, 0);
  311. else
  312. img = allocimage(display, rgbv[k]->r, outchan, 0, 0);
  313. if(tmp == nil)
  314. tmp = allocimage(display, r, img->chan, 0, DWhite);
  315. ims[k]= dst = allocimage(display, r, tmp->chan, 0, DWhite);
  316. if(tmp == nil || img == nil || dst == nil){
  317. fprint(2, "gif: allocimage %s failed: %r\n", name);
  318. err = "allocimage";
  319. goto Return;
  320. }
  321. if(loadimage(img, img->r, rgbv[k]->chans[0], rgbv[k]->chanlen) < 0){
  322. fprint(2, "gif: loadimage %s failed: %r\n", name);
  323. err = "loadimage";
  324. goto Return;
  325. }
  326. switch((images[k]->gifflags>>2)&7){
  327. default:
  328. draw(tmp, img->r, img, msk, img->r.min);
  329. draw(dst, tmp->r, tmp, nil, tmp->r.min);
  330. break;
  331. case 2:
  332. draw(tmp, img->r, display->white, msk, img->r.min);
  333. /* no break */
  334. case 3:
  335. draw(dst, tmp->r, tmp, nil, tmp->r.min);
  336. draw(dst, img->r, img, msk, img->r.min);
  337. break;
  338. }
  339. freeimage(msk);
  340. freeimage(img);
  341. }
  342. }
  343. if(tmp)
  344. freeimage(tmp);
  345. allims = ims;
  346. loopcount = images[0]->gifloopcount;
  347. if(!dflag){
  348. nloop = 0;
  349. do{
  350. for(k=0; k<n; k++){
  351. which = k;
  352. eresized(0);
  353. dt = images[k]->gifdelay*10;
  354. if(dt < 50)
  355. dt = 50;
  356. while(n==1 || ecankbd()){
  357. /* an odd, democratic list */
  358. if((ch=ekbd())=='q' || ch==Kdel || ch==Keof)
  359. exits(nil);
  360. if(ch == '\n')
  361. goto Out;
  362. }sleep(dt);
  363. }
  364. /* loopcount -1 means no loop (this code's rule), loopcount 0 means loop forever (netscape's rule)*/
  365. }while(loopcount==0 || ++nloop<loopcount);
  366. /* loop count has run out */
  367. ekbd();
  368. Out:
  369. draw(screen, screen->clipr, display->white, nil, ZP);
  370. }
  371. if(nineflag){
  372. if(images[0]->gifflags&TRANSP){
  373. addalpha(rgbv[0]);
  374. blackout(images[0], rgbv[0]);
  375. }
  376. chantostr(buf, outchan);
  377. print("%11s %11d %11d %11d %11d ", buf,
  378. rgbv[0]->r.min.x, rgbv[0]->r.min.y, rgbv[0]->r.max.x, rgbv[0]->r.max.y);
  379. if(jehanne_write(1, rgbv[0]->chans[0], rgbv[0]->chanlen) != rgbv[0]->chanlen){
  380. fprint(2, "gif: %s: write error %r\n", name);
  381. return "write";
  382. }
  383. }else if(cflag){
  384. if(images[0]->gifflags&TRANSP){
  385. addalpha(rgbv[0]);
  386. blackout(images[0], rgbv[0]);
  387. }
  388. if(writerawimage(1, rgbv[0]) < 0){
  389. fprint(2, "gif: %s: write error: %r\n", name);
  390. return "write";
  391. }
  392. }
  393. Return:
  394. allims = nil;
  395. for(k=0; images[k]; k++){
  396. for(j=0; j<images[k]->nchans; j++)
  397. free(images[k]->chans[j]);
  398. free(images[k]->cmap);
  399. if(rgbv[k])
  400. free(rgbv[k]->chans[0]);
  401. freeimage(ims[k]);
  402. free(images[k]);
  403. free(rgbv[k]);
  404. }
  405. free(images);
  406. free(ims);
  407. return err;
  408. }