gif.c 8.8 KB

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