readgif.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <draw.h>
  5. #include "imagefile.h"
  6. typedef struct Entry Entry;
  7. typedef struct Header Header;
  8. struct Entry{
  9. int prefix;
  10. int exten;
  11. };
  12. struct Header{
  13. Biobuf *fd;
  14. char err[256];
  15. jmp_buf errlab;
  16. uchar buf[3*256];
  17. char vers[8];
  18. uchar *globalcmap;
  19. int screenw;
  20. int screenh;
  21. int fields;
  22. int bgrnd;
  23. int aspect;
  24. int flags;
  25. int delay;
  26. int trindex;
  27. int loopcount;
  28. Entry tbl[4096];
  29. Rawimage **array;
  30. Rawimage *new;
  31. uchar *pic;
  32. };
  33. static char readerr[] = "ReadGIF: read error: %r";
  34. static char extreaderr[] = "ReadGIF: can't read extension: %r";
  35. static char memerr[] = "ReadGIF: malloc failed: %r";
  36. static Rawimage** readarray(Header*);
  37. static Rawimage* readone(Header*);
  38. static void readheader(Header*);
  39. static void skipextension(Header*);
  40. static uchar* readcmap(Header*, int);
  41. static uchar* decode(Header*, Rawimage*, Entry*);
  42. static void interlace(Header*, Rawimage*);
  43. static
  44. void
  45. clear(void **p)
  46. {
  47. if(*p){
  48. free(*p);
  49. *p = nil;
  50. }
  51. }
  52. static
  53. void
  54. giffreeall(Header *h, int freeimage)
  55. {
  56. int i;
  57. if(h->fd){
  58. Bterm(h->fd);
  59. h->fd = nil;
  60. }
  61. clear(&h->pic);
  62. if(h->new){
  63. clear(&h->new->cmap);
  64. clear(&h->new->chans[0]);
  65. clear(&h->new);
  66. }
  67. clear(&h->globalcmap);
  68. if(freeimage && h->array!=nil){
  69. for(i=0; h->array[i]; i++){
  70. clear(&h->array[i]->cmap);
  71. clear(&h->array[i]->chans[0]);
  72. }
  73. clear(&h->array);
  74. }
  75. }
  76. static
  77. void
  78. giferror(Header *h, char *fmt, ...)
  79. {
  80. va_list arg;
  81. va_start(arg, fmt);
  82. vseprint(h->err, h->err+sizeof h->err, fmt, arg);
  83. va_end(arg);
  84. werrstr(h->err);
  85. giffreeall(h, 1);
  86. longjmp(h->errlab, 1);
  87. }
  88. Rawimage**
  89. readgif(int fd, int colorspace)
  90. {
  91. Rawimage **a;
  92. Biobuf b;
  93. Header *h;
  94. char buf[ERRMAX];
  95. buf[0] = '\0';
  96. USED(colorspace);
  97. if(Binit(&b, fd, OREAD) < 0)
  98. return nil;
  99. h = malloc(sizeof(Header));
  100. if(h == nil){
  101. Bterm(&b);
  102. return nil;
  103. }
  104. memset(h, 0, sizeof(Header));
  105. h->fd = &b;
  106. errstr(buf, sizeof buf); /* throw it away */
  107. if(setjmp(h->errlab))
  108. a = nil;
  109. else
  110. a = readarray(h);
  111. giffreeall(h, 0);
  112. free(h);
  113. return a;
  114. }
  115. static
  116. void
  117. inittbl(Header *h)
  118. {
  119. int i;
  120. Entry *tbl;
  121. tbl = h->tbl;
  122. for(i=0; i<258; i++) {
  123. tbl[i].prefix = -1;
  124. tbl[i].exten = i;
  125. }
  126. }
  127. static
  128. Rawimage**
  129. readarray(Header *h)
  130. {
  131. Entry *tbl;
  132. Rawimage *new, **array;
  133. int c, nimages;
  134. tbl = h->tbl;
  135. readheader(h);
  136. if(h->fields & 0x80)
  137. h->globalcmap = readcmap(h, (h->fields&7)+1);
  138. array = malloc(sizeof(Rawimage**));
  139. if(array == nil)
  140. giferror(h, memerr);
  141. nimages = 0;
  142. array[0] = nil;
  143. h->array = array;
  144. for(;;){
  145. switch(c = Bgetc(h->fd)){
  146. case Beof:
  147. goto Return;
  148. case 0x21: /* Extension (ignored) */
  149. skipextension(h);
  150. break;
  151. case 0x2C: /* Image Descriptor */
  152. inittbl(h);
  153. new = readone(h);
  154. if(new->fields & 0x80){
  155. new->cmaplen = 3*(1<<((new->fields&7)+1));
  156. new->cmap = readcmap(h, (new->fields&7)+1);
  157. }else{
  158. new->cmaplen = 3*(1<<((h->fields&7)+1));
  159. new->cmap = malloc(new->cmaplen);
  160. memmove(new->cmap, h->globalcmap, new->cmaplen);
  161. }
  162. h->new = new;
  163. new->chans[0] = decode(h, new, tbl);
  164. if(new->fields & 0x40)
  165. interlace(h, new);
  166. new->gifflags = h->flags;
  167. new->gifdelay = h->delay;
  168. new->giftrindex = h->trindex;
  169. new->gifloopcount = h->loopcount;
  170. array = realloc(h->array, (nimages+2)*sizeof(Rawimage*));
  171. if(array == nil)
  172. giferror(h, memerr);
  173. array[nimages++] = new;
  174. array[nimages] = nil;
  175. h->array = array;
  176. h->new = nil;
  177. break;
  178. case 0x3B: /* Trailer */
  179. goto Return;
  180. default:
  181. fprint(2, "ReadGIF: unknown block type: 0x%.2x\n", c);
  182. goto Return;
  183. }
  184. }
  185. Return:
  186. if(array[0]==nil || array[0]->chans[0] == nil)
  187. giferror(h, "ReadGIF: no picture in file");
  188. return array;
  189. }
  190. static
  191. void
  192. readheader(Header *h)
  193. {
  194. if(Bread(h->fd, h->buf, 13) != 13)
  195. giferror(h, "ReadGIF: can't read header: %r");
  196. memmove(h->vers, h->buf, 6);
  197. if(strcmp(h->vers, "GIF87a")!=0 && strcmp(h->vers, "GIF89a")!=0)
  198. giferror(h, "ReadGIF: can't recognize format %s", h->vers);
  199. h->screenw = h->buf[6]+(h->buf[7]<<8);
  200. h->screenh = h->buf[8]+(h->buf[9]<<8);
  201. h->fields = h->buf[10];
  202. h->bgrnd = h->buf[11];
  203. h->aspect = h->buf[12];
  204. h->flags = 0;
  205. h->delay = 0;
  206. h->trindex = 0;
  207. h->loopcount = -1;
  208. }
  209. static
  210. uchar*
  211. readcmap(Header *h, int size)
  212. {
  213. uchar *map;
  214. if(size > 8)
  215. giferror(h, "ReadGIF: can't handles %d bits per pixel", size);
  216. size = 3*(1<<size);
  217. if(Bread(h->fd, h->buf, size) != size)
  218. giferror(h, "ReadGIF: short read on color map");
  219. map = malloc(size);
  220. if(map == nil)
  221. giferror(h, memerr);
  222. memmove(map, h->buf, size);
  223. return map;
  224. }
  225. static
  226. Rawimage*
  227. readone(Header *h)
  228. {
  229. Rawimage *i;
  230. int left, top, width, height;
  231. if(Bread(h->fd, h->buf, 9) != 9)
  232. giferror(h, "ReadGIF: can't read image descriptor: %r");
  233. i = malloc(sizeof(Rawimage));
  234. if(i == nil)
  235. giferror(h, memerr);
  236. left = h->buf[0]+(h->buf[1]<<8);
  237. top = h->buf[2]+(h->buf[3]<<8);
  238. width = h->buf[4]+(h->buf[5]<<8);
  239. height = h->buf[6]+(h->buf[7]<<8);
  240. i->fields = h->buf[8];
  241. i->r.min.x = left;
  242. i->r.min.y = top;
  243. i->r.max.x = left+width;
  244. i->r.max.y = top+height;
  245. i->nchans = 1;
  246. i->chandesc = CRGB1;
  247. memset(i->chans, 0, sizeof(i->chans));
  248. return i;
  249. }
  250. static
  251. int
  252. readdata(Header *h, uchar *data)
  253. {
  254. int nbytes, n;
  255. nbytes = Bgetc(h->fd);
  256. if(nbytes < 0)
  257. giferror(h, "ReadGIF: can't read data: %r");
  258. if(nbytes == 0)
  259. return 0;
  260. n = Bread(h->fd, data, nbytes);
  261. if(n < 0)
  262. giferror(h, "ReadGIF: can't read data: %r");
  263. if(n != nbytes)
  264. fprint(2, "ReadGIF: short data subblock\n");
  265. return n;
  266. }
  267. static
  268. void
  269. graphiccontrol(Header *h)
  270. {
  271. if(Bread(h->fd, h->buf, 5+1) != 5+1)
  272. giferror(h, readerr);
  273. h->flags = h->buf[1];
  274. h->delay = h->buf[2]+(h->buf[3]<<8);
  275. h->trindex = h->buf[4];
  276. }
  277. static
  278. void
  279. skipextension(Header *h)
  280. {
  281. int type, hsize, hasdata, n;
  282. uchar data[256];
  283. hsize = 0;
  284. hasdata = 0;
  285. type = Bgetc(h->fd);
  286. switch(type){
  287. case Beof:
  288. giferror(h, extreaderr);
  289. break;
  290. case 0x01: /* Plain Text Extension */
  291. hsize = 13;
  292. hasdata = 1;
  293. break;
  294. case 0xF9: /* Graphic Control Extension */
  295. graphiccontrol(h);
  296. return;
  297. case 0xFE: /* Comment Extension */
  298. hasdata = 1;
  299. break;
  300. case 0xFF: /* Application Extension */
  301. hsize = Bgetc(h->fd);
  302. /* standard says this must be 11, but Adobe likes to put out 10-byte ones,
  303. * so we pay attention to the field. */
  304. hasdata = 1;
  305. break;
  306. default:
  307. giferror(h, "ReadGIF: unknown extension");
  308. }
  309. if(hsize>0 && Bread(h->fd, h->buf, hsize) != hsize)
  310. giferror(h, extreaderr);
  311. if(!hasdata){
  312. /*
  313. * This code used to check h->buf[hsize-1] != 0
  314. * and giferror if so, but if !hasdata, hsize == 0.
  315. */
  316. return;
  317. }
  318. /* loop counter: Application Extension with NETSCAPE2.0 as string and 1 <loop.count> in data */
  319. if(type == 0xFF && hsize==11 && memcmp(h->buf, "NETSCAPE2.0", 11)==0){
  320. n = readdata(h, data);
  321. if(n == 0)
  322. return;
  323. if(n==3 && data[0]==1)
  324. h->loopcount = data[1] | (data[2]<<8);
  325. }
  326. while(readdata(h, data) != 0)
  327. ;
  328. }
  329. static
  330. uchar*
  331. decode(Header *h, Rawimage *i, Entry *tbl)
  332. {
  333. int c, doclip, incode, codesize, CTM, EOD, pici, datai, stacki, nbits, sreg, fc, code, piclen;
  334. int csize, nentry, maxentry, first, ocode, ndata, nb;
  335. uchar clip, *p, *pic;
  336. uchar stack[4096], data[256];
  337. if(Bread(h->fd, h->buf, 1) != 1)
  338. giferror(h, "ReadGIF: can't read data: %r");
  339. codesize = h->buf[0];
  340. if(codesize>8 || 0>codesize)
  341. giferror(h, "ReadGIF: can't handle codesize %d", codesize);
  342. doclip = 0;
  343. if(i->cmap!=nil && i->cmaplen!=3*(1<<codesize)
  344. && (codesize!=2 || i->cmaplen!=3*2)) /* peculiar GIF bitmap files... */
  345. doclip = 1;
  346. CTM =1<<codesize;
  347. EOD = CTM+1;
  348. piclen = (i->r.max.x-i->r.min.x)*(i->r.max.y-i->r.min.y);
  349. i->chanlen = piclen;
  350. pic = malloc(piclen);
  351. if(pic == nil)
  352. giferror(h, memerr);
  353. h->pic = pic;
  354. pici = 0;
  355. ndata = 0;
  356. datai = 0;
  357. nbits = 0;
  358. sreg = 0;
  359. fc = 0;
  360. Loop:
  361. for(;;){
  362. csize = codesize+1;
  363. nentry = EOD+1;
  364. maxentry = (1<<csize)-1;
  365. first = 1;
  366. ocode = -1;
  367. for(;; ocode = incode) {
  368. while(nbits < csize) {
  369. if(datai == ndata){
  370. ndata = readdata(h, data);
  371. if(ndata == 0)
  372. goto Return;
  373. datai = 0;
  374. }
  375. c = data[datai++];
  376. sreg |= c<<nbits;
  377. nbits += 8;
  378. }
  379. code = sreg & ((1<<csize) - 1);
  380. sreg >>= csize;
  381. nbits -= csize;
  382. if(code == EOD){
  383. ndata = readdata(h, data);
  384. if(ndata != 0)
  385. fprint(2, "ReadGIF: unexpected data past EOD\n");
  386. goto Return;
  387. }
  388. if(code == CTM)
  389. goto Loop;
  390. stacki = (sizeof stack)-1;
  391. incode = code;
  392. /* special case for KwKwK */
  393. if(code == nentry) {
  394. stack[stacki--] = fc;
  395. code = ocode;
  396. }
  397. if(code > nentry){
  398. fprint(2, "ReadGIF: GIF invalid, code out of range, %x > %x\n", code, nentry);
  399. code = nentry;
  400. }
  401. for(c=code; stacki>0 && c>=0; c=tbl[c].prefix)
  402. stack[stacki--] = tbl[c].exten;
  403. nb = (sizeof stack)-(stacki+1);
  404. if(pici+nb > piclen){
  405. /* this common error is harmless
  406. * we have to keep reading to keep the blocks in sync */
  407. ;
  408. }else{
  409. memmove(pic+pici, stack+stacki+1, sizeof stack - (stacki+1));
  410. pici += nb;
  411. }
  412. fc = stack[stacki+1];
  413. if(first){
  414. first = 0;
  415. continue;
  416. }
  417. #define early 0 /* peculiar tiff feature here for reference */
  418. if(nentry == maxentry-early) {
  419. if(csize >= 12)
  420. continue;
  421. csize++;
  422. maxentry = (1<<csize);
  423. if(csize < 12)
  424. maxentry--;
  425. }
  426. tbl[nentry].prefix = ocode;
  427. tbl[nentry].exten = fc;
  428. nentry++;
  429. }
  430. }
  431. Return:
  432. if(doclip){
  433. clip = i->cmaplen/3;
  434. for(p = pic; p < pic+piclen; p++)
  435. if(*p >= clip)
  436. *p = clip;
  437. }
  438. h->pic = nil;
  439. return pic;
  440. }
  441. static
  442. void
  443. interlace(Header *h, Rawimage *image)
  444. {
  445. uchar *pic;
  446. Rectangle r;
  447. int dx, yy, y;
  448. uchar *ipic;
  449. pic = image->chans[0];
  450. r = image->r;
  451. dx = r.max.x-r.min.x;
  452. ipic = malloc(dx*(r.max.y-r.min.y));
  453. if(ipic == nil)
  454. giferror(h, nil);
  455. /* Group 1: every 8th row, starting with row 0 */
  456. yy = 0;
  457. for(y=r.min.y; y<r.max.y; y+=8){
  458. memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
  459. yy++;
  460. }
  461. /* Group 2: every 8th row, starting with row 4 */
  462. for(y=r.min.y+4; y<r.max.y; y+=8){
  463. memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
  464. yy++;
  465. }
  466. /* Group 3: every 4th row, starting with row 2 */
  467. for(y=r.min.y+2; y<r.max.y; y+=4){
  468. memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
  469. yy++;
  470. }
  471. /* Group 4: every 2nd row, starting with row 1 */
  472. for(y=r.min.y+1; y<r.max.y; y+=2){
  473. memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
  474. yy++;
  475. }
  476. free(image->chans[0]);
  477. image->chans[0] = ipic;
  478. }