readgif.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. return i;
  248. }
  249. static
  250. int
  251. readdata(Header *h, uchar *data)
  252. {
  253. int nbytes, n;
  254. nbytes = Bgetc(h->fd);
  255. if(nbytes < 0)
  256. giferror(h, "ReadGIF: can't read data: %r");
  257. if(nbytes == 0)
  258. return 0;
  259. n = Bread(h->fd, data, nbytes);
  260. if(n < 0)
  261. giferror(h, "ReadGIF: can't read data: %r");
  262. if(n != nbytes)
  263. fprint(2, "ReadGIF: short data subblock\n");
  264. return n;
  265. }
  266. static
  267. void
  268. graphiccontrol(Header *h)
  269. {
  270. if(Bread(h->fd, h->buf, 5+1) != 5+1)
  271. giferror(h, readerr);
  272. h->flags = h->buf[1];
  273. h->delay = h->buf[2]+(h->buf[3]<<8);
  274. h->trindex = h->buf[4];
  275. }
  276. static
  277. void
  278. skipextension(Header *h)
  279. {
  280. int type, hsize, hasdata, n;
  281. uchar data[256];
  282. hsize = 0;
  283. hasdata = 0;
  284. type = Bgetc(h->fd);
  285. switch(type){
  286. case Beof:
  287. giferror(h, extreaderr);
  288. break;
  289. case 0x01: /* Plain Text Extension */
  290. hsize = 13;
  291. hasdata = 1;
  292. break;
  293. case 0xF9: /* Graphic Control Extension */
  294. graphiccontrol(h);
  295. return;
  296. case 0xFE: /* Comment Extension */
  297. hasdata = 1;
  298. break;
  299. case 0xFF: /* Application Extension */
  300. hsize = Bgetc(h->fd);
  301. /* standard says this must be 11, but Adobe likes to put out 10-byte ones,
  302. * so we pay attention to the field. */
  303. hasdata = 1;
  304. break;
  305. default:
  306. giferror(h, "ReadGIF: unknown extension");
  307. }
  308. if(hsize>0 && Bread(h->fd, h->buf, hsize) != hsize)
  309. giferror(h, extreaderr);
  310. if(!hasdata){
  311. if(h->buf[hsize-1] != 0)
  312. giferror(h, "ReadGIF: bad extension format");
  313. return;
  314. }
  315. /* loop counter: Application Extension with NETSCAPE2.0 as string and 1 <loop.count> in data */
  316. if(type == 0xFF && hsize==11 && memcmp(h->buf, "NETSCAPE2.0", 11)==0){
  317. n = readdata(h, data);
  318. if(n == 0)
  319. return;
  320. if(n==3 && data[0]==1)
  321. h->loopcount = data[1] | (data[2]<<8);
  322. }
  323. while(readdata(h, data) != 0)
  324. ;
  325. }
  326. static
  327. uchar*
  328. decode(Header *h, Rawimage *i, Entry *tbl)
  329. {
  330. int c, incode, codesize, CTM, EOD, pici, datai, stacki, nbits, sreg, fc, code, piclen;
  331. int csize, nentry, maxentry, first, ocode, ndata, nb;
  332. uchar *pic;
  333. uchar stack[4096], data[256];
  334. if(Bread(h->fd, h->buf, 1) != 1)
  335. giferror(h, "ReadGIF: can't read data: %r");
  336. codesize = h->buf[0];
  337. if(codesize>8 || 0>codesize)
  338. giferror(h, "ReadGIF: can't handle codesize %d", codesize);
  339. if(i->cmap!=nil && i->cmaplen!=3*(1<<codesize)
  340. && (codesize!=2 || i->cmaplen!=3*2)) /* peculiar GIF bitmap files... */
  341. giferror(h, "ReadGIF: codesize %d doesn't match color map 3*%d", codesize, i->cmaplen/3);
  342. CTM =1<<codesize;
  343. EOD = CTM+1;
  344. piclen = (i->r.max.x-i->r.min.x)*(i->r.max.y-i->r.min.y);
  345. i->chanlen = piclen;
  346. pic = malloc(piclen);
  347. if(pic == nil)
  348. giferror(h, memerr);
  349. h->pic = pic;
  350. pici = 0;
  351. ndata = 0;
  352. datai = 0;
  353. nbits = 0;
  354. sreg = 0;
  355. fc = 0;
  356. Loop:
  357. for(;;){
  358. csize = codesize+1;
  359. nentry = EOD+1;
  360. maxentry = (1<<csize)-1;
  361. first = 1;
  362. ocode = -1;
  363. for(;; ocode = incode) {
  364. while(nbits < csize) {
  365. if(datai == ndata){
  366. ndata = readdata(h, data);
  367. if(ndata == 0)
  368. goto Return;
  369. datai = 0;
  370. }
  371. c = data[datai++];
  372. sreg |= c<<nbits;
  373. nbits += 8;
  374. }
  375. code = sreg & ((1<<csize) - 1);
  376. sreg >>= csize;
  377. nbits -= csize;
  378. if(code == EOD){
  379. ndata = readdata(h, data);
  380. if(ndata != 0)
  381. fprint(2, "ReadGIF: unexpected data past EOD");
  382. goto Return;
  383. }
  384. if(code == CTM)
  385. goto Loop;
  386. stacki = (sizeof stack)-1;
  387. incode = code;
  388. /* special case for KwKwK */
  389. if(code == nentry) {
  390. stack[stacki--] = fc;
  391. code = ocode;
  392. }
  393. if(code > nentry)
  394. giferror(h, "ReadGIF: bad code %x %x", code, nentry);
  395. for(c=code; c>=0; c=tbl[c].prefix)
  396. stack[stacki--] = tbl[c].exten;
  397. nb = (sizeof stack)-(stacki+1);
  398. if(pici+nb > piclen){
  399. /* this common error is harmless
  400. * we have to keep reading to keep the blocks in sync */
  401. ;
  402. }else{
  403. memmove(pic+pici, stack+stacki+1, sizeof stack - (stacki+1));
  404. pici += nb;
  405. }
  406. fc = stack[stacki+1];
  407. if(first){
  408. first = 0;
  409. continue;
  410. }
  411. #define early 0 /* peculiar tiff feature here for reference */
  412. if(nentry == maxentry-early) {
  413. if(csize >= 12)
  414. continue;
  415. csize++;
  416. maxentry = (1<<csize);
  417. if(csize < 12)
  418. maxentry--;
  419. }
  420. tbl[nentry].prefix = ocode;
  421. tbl[nentry].exten = fc;
  422. nentry++;
  423. }
  424. }
  425. Return:
  426. h->pic = nil;
  427. return pic;
  428. }
  429. static
  430. void
  431. interlace(Header *h, Rawimage *image)
  432. {
  433. uchar *pic;
  434. Rectangle r;
  435. int dx, yy, y;
  436. uchar *ipic;
  437. pic = image->chans[0];
  438. r = image->r;
  439. dx = r.max.x-r.min.x;
  440. ipic = malloc(dx*(r.max.y-r.min.y));
  441. if(ipic == nil)
  442. giferror(h, nil);
  443. /* Group 1: every 8th row, starting with row 0 */
  444. yy = 0;
  445. for(y=r.min.y; y<r.max.y; y+=8){
  446. memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
  447. yy++;
  448. }
  449. /* Group 2: every 8th row, starting with row 4 */
  450. for(y=r.min.y+4; y<r.max.y; y+=8){
  451. memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
  452. yy++;
  453. }
  454. /* Group 3: every 4th row, starting with row 2 */
  455. for(y=r.min.y+2; y<r.max.y; y+=4){
  456. memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
  457. yy++;
  458. }
  459. /* Group 4: every 2nd row, starting with row 1 */
  460. for(y=r.min.y+1; y<r.max.y; y+=2){
  461. memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
  462. yy++;
  463. }
  464. free(image->chans[0]);
  465. image->chans[0] = ipic;
  466. }