3 * http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
4 * http://www.fileformat.info/format/tiff/egff.htm
5 * http://www.fileformat.info/mirror/egff/ch09_05.htm
6 * http://www.itu.int/rec/T-REC-T.4-199904-S/en
7 * http://www.itu.int/rec/T-REC-T.6-198811-I/en
9 * many thanks to paul bourke for a simple description of tiff:
10 * http://paulbourke.net/dataformats/tiff/
12 * copy-pasted fax codes and lzw help:
13 * http://www.remotesensing.org/libtiff/
19 #include "imagefile.h"
22 II = 0x4949, /* little-endian */
23 MM = 0x4d4d, /* big-endian */
24 TIF = 0x002a /* tiff magic number */
76 typedef struct Tab Tab;
77 typedef struct Fax Fax;
78 typedef struct Code Code;
79 typedef struct Lzw Lzw;
80 typedef struct Fld Fld;
81 typedef struct Tif Tif;
86 int run; /* run length */
94 int ntab; /* position in tab */
102 ulong next; /* next strip offset in data */
113 int len; /* code length */
117 ulong next; /* next strip offset in data */
118 /* remaining allocated codes */
127 ulong off; /* value offset */
134 uint end; /* endianness */
138 int eof; /* reached end of image */
139 ulong n; /* offset in buf array */
143 ulong (*byte2)(uchar *);
144 ulong (*byte4)(uchar *);
151 uchar *(*uncompress)(Tif *);
154 int (*decode)(Tif *, Rawimage *, uchar *);
163 ulong *color; /* color map */
175 * imported from libdraw/arith.c to permit
176 * extern log2 function
178 static int log2[] = {
179 -1, 0, 1, -1, 2, -1, -1, -1, 3,
180 -1, -1, -1, -1, -1, -1, -1, 4,
181 -1, -1, -1, -1, -1, -1, -1, 4 /* BUG */,
182 -1, -1, -1, -1, -1, -1, -1, 5
185 static Tab faxwhite[Nfaxtab] = {
186 {4, 0x7, 2}, /* 0111 */
187 {4, 0x8, 3}, /* 1000 */
188 {4, 0xb, 4}, /* 1011 */
189 {4, 0xc, 5}, /* 1100 */
190 {4, 0xe, 6}, /* 1110 */
191 {4, 0xf, 7}, /* 1111 */
192 {5, 0x12, 128}, /* 1001 0 */
193 {5, 0x13, 8}, /* 1001 1 */
194 {5, 0x14, 9}, /* 1010 0 */
195 {5, 0x1b, 64}, /* 1101 1 */
196 {5, 0x7, 10}, /* 0011 1 */
197 {5, 0x8, 11}, /* 0100 0 */
198 {6, 0x17, 192}, /* 0101 11 */
199 {6, 0x18, 1664}, /* 0110 00 */
200 {6, 0x2a, 16}, /* 1010 10 */
201 {6, 0x2b, 17}, /* 1010 11 */
202 {6, 0x3, 13}, /* 0000 11 */
203 {6, 0x34, 14}, /* 1101 00 */
204 {6, 0x35, 15}, /* 1101 01 */
205 {6, 0x7, 1}, /* 0001 11 */
206 {6, 0x8, 12}, /* 0010 00 */
207 {7, 0x13, 26}, /* 0010 011 */
208 {7, 0x17, 21}, /* 0010 111 */
209 {7, 0x18, 28}, /* 0011 000 */
210 {7, 0x24, 27}, /* 0100 100 */
211 {7, 0x27, 18}, /* 0100 111 */
212 {7, 0x28, 24}, /* 0101 000 */
213 {7, 0x2b, 25}, /* 0101 011 */
214 {7, 0x3, 22}, /* 0000 011 */
215 {7, 0x37, 256}, /* 0110 111 */
216 {7, 0x4, 23}, /* 0000 100 */
217 {7, 0x8, 20}, /* 0001 000 */
218 {7, 0xc, 19}, /* 0001 100 */
219 {8, 0x12, 33}, /* 0001 0010 */
220 {8, 0x13, 34}, /* 0001 0011 */
221 {8, 0x14, 35}, /* 0001 0100 */
222 {8, 0x15, 36}, /* 0001 0101 */
223 {8, 0x16, 37}, /* 0001 0110 */
224 {8, 0x17, 38}, /* 0001 0111 */
225 {8, 0x1a, 31}, /* 0001 1010 */
226 {8, 0x1b, 32}, /* 0001 1011 */
227 {8, 0x2, 29}, /* 0000 0010 */
228 {8, 0x24, 53}, /* 0010 0100 */
229 {8, 0x25, 54}, /* 0010 0101 */
230 {8, 0x28, 39}, /* 0010 1000 */
231 {8, 0x29, 40}, /* 0010 1001 */
232 {8, 0x2a, 41}, /* 0010 1010 */
233 {8, 0x2b, 42}, /* 0010 1011 */
234 {8, 0x2c, 43}, /* 0010 1100 */
235 {8, 0x2d, 44}, /* 0010 1101 */
236 {8, 0x3, 30}, /* 0000 0011 */
237 {8, 0x32, 61}, /* 0011 0010 */
238 {8, 0x33, 62}, /* 0011 0011 */
239 {8, 0x34, 63}, /* 0011 0100 */
240 {8, 0x35, 0}, /* 0011 0101 */
241 {8, 0x36, 320}, /* 0011 0110 */
242 {8, 0x37, 384}, /* 0011 0111 */
243 {8, 0x4, 45}, /* 0000 0100 */
244 {8, 0x4a, 59}, /* 0100 1010 */
245 {8, 0x4b, 60}, /* 0100 1011 */
246 {8, 0x5, 46}, /* 0000 0101 */
247 {8, 0x52, 49}, /* 0101 0010 */
248 {8, 0x53, 50}, /* 0101 0011 */
249 {8, 0x54, 51}, /* 0101 0100 */
250 {8, 0x55, 52}, /* 0101 0101 */
251 {8, 0x58, 55}, /* 0101 1000 */
252 {8, 0x59, 56}, /* 0101 1001 */
253 {8, 0x5a, 57}, /* 0101 1010 */
254 {8, 0x5b, 58}, /* 0101 1011 */
255 {8, 0x64, 448}, /* 0110 0100 */
256 {8, 0x65, 512}, /* 0110 0101 */
257 {8, 0x67, 640}, /* 0110 0111 */
258 {8, 0x68, 576}, /* 0110 1000 */
259 {8, 0xa, 47}, /* 0000 1010 */
260 {8, 0xb, 48}, /* 0000 1011 */
261 {9, 0x98, 1472}, /* 0100 1100 0 */
262 {9, 0x99, 1536}, /* 0100 1100 1 */
263 {9, 0x9a, 1600}, /* 0100 1101 0 */
264 {9, 0x9b, 1728}, /* 0100 1101 1 */
265 {9, 0xcc, 704}, /* 0110 0110 0 */
266 {9, 0xcd, 768}, /* 0110 0110 1 */
267 {9, 0xd2, 832}, /* 0110 1001 0 */
268 {9, 0xd3, 896}, /* 0110 1001 1 */
269 {9, 0xd4, 960}, /* 0110 1010 0 */
270 {9, 0xd5, 1024}, /* 0110 1010 1 */
271 {9, 0xd6, 1088}, /* 0110 1011 0 */
272 {9, 0xd7, 1152}, /* 0110 1011 1 */
273 {9, 0xd8, 1216}, /* 0110 1100 0 */
274 {9, 0xd9, 1280}, /* 0110 1100 1 */
275 {9, 0xda, 1344}, /* 0110 1101 0 */
276 {9, 0xdb, 1408}, /* 0110 1101 1 */
277 {11, 0x8, 1792}, /* 0000 0001 000 */
278 {11, 0xc, 1856}, /* 0000 0001 100 */
279 {11, 0xd, 1920}, /* 0000 0001 101 */
280 {12, 0x1, -1}, /* 0000 0000 0001 */
281 {12, 0x12, 1984}, /* 0000 0001 0010 */
282 {12, 0x13, 2048}, /* 0000 0001 0011 */
283 {12, 0x14, 2112}, /* 0000 0001 0100 */
284 {12, 0x15, 2176}, /* 0000 0001 0101 */
285 {12, 0x16, 2240}, /* 0000 0001 0110 */
286 {12, 0x17, 2304}, /* 0000 0001 0111 */
287 {12, 0x1c, 2368}, /* 0000 0001 1100 */
288 {12, 0x1d, 2432}, /* 0000 0001 1101 */
289 {12, 0x1e, 2496}, /* 0000 0001 1110 */
290 {12, 0x1f, 2560} /* 0000 0001 1111 */
293 static Tab faxblack[Nfaxtab] = {
294 {2, 0x2, 3}, /* 10 */
295 {2, 0x3, 2}, /* 11 */
296 {3, 0x2, 1}, /* 010 */
297 {3, 0x3, 4}, /* 011 */
298 {4, 0x2, 6}, /* 0010 */
299 {4, 0x3, 5}, /* 0011 */
300 {5, 0x3, 7}, /* 0001 1 */
301 {6, 0x4, 9}, /* 0001 00 */
302 {6, 0x5, 8}, /* 0001 01 */
303 {7, 0x4, 10}, /* 0000 100 */
304 {7, 0x5, 11}, /* 0000 101 */
305 {7, 0x7, 12}, /* 0000 111 */
306 {8, 0x4, 13}, /* 0000 0100 */
307 {8, 0x7, 14}, /* 0000 0111 */
308 {9, 0x18, 15}, /* 0000 1100 0 */
309 {10, 0x17, 16}, /* 0000 0101 11 */
310 {10, 0x18, 17}, /* 0000 0110 00 */
311 {10, 0x37, 0}, /* 0000 1101 11 */
312 {10, 0x8, 18}, /* 0000 0010 00 */
313 {10, 0xf, 64}, /* 0000 0011 11 */
314 {11, 0x17, 24}, /* 0000 0010 111 */
315 {11, 0x18, 25}, /* 0000 0011 000 */
316 {11, 0x28, 23}, /* 0000 0101 000 */
317 {11, 0x37, 22}, /* 0000 0110 111 */
318 {11, 0x67, 19}, /* 0000 1100 111 */
319 {11, 0x68, 20}, /* 0000 1101 000 */
320 {11, 0x6c, 21}, /* 0000 1101 100 */
321 {11, 0x8, 1792}, /* 0000 0001 000 */
322 {11, 0xc, 1856}, /* 0000 0001 100 */
323 {11, 0xd, 1920}, /* 0000 0001 101 */
324 {12, 0x1, -1}, /* 0000 0000 0001 */
325 {12, 0x12, 1984}, /* 0000 0001 0010 */
326 {12, 0x13, 2048}, /* 0000 0001 0011 */
327 {12, 0x14, 2112}, /* 0000 0001 0100 */
328 {12, 0x15, 2176}, /* 0000 0001 0101 */
329 {12, 0x16, 2240}, /* 0000 0001 0110 */
330 {12, 0x17, 2304}, /* 0000 0001 0111 */
331 {12, 0x1c, 2368}, /* 0000 0001 1100 */
332 {12, 0x1d, 2432}, /* 0000 0001 1101 */
333 {12, 0x1e, 2496}, /* 0000 0001 1110 */
334 {12, 0x1f, 2560}, /* 0000 0001 1111 */
335 {12, 0x24, 52}, /* 0000 0010 0100 */
336 {12, 0x27, 55}, /* 0000 0010 0111 */
337 {12, 0x28, 56}, /* 0000 0010 1000 */
338 {12, 0x2b, 59}, /* 0000 0010 1011 */
339 {12, 0x2c, 60}, /* 0000 0010 1100 */
340 {12, 0x33, 320}, /* 0000 0011 0011 */
341 {12, 0x34, 384}, /* 0000 0011 0100 */
342 {12, 0x35, 448}, /* 0000 0011 0101 */
343 {12, 0x37, 53}, /* 0000 0011 0111 */
344 {12, 0x38, 54}, /* 0000 0011 1000 */
345 {12, 0x52, 50}, /* 0000 0101 0010 */
346 {12, 0x53, 51}, /* 0000 0101 0011 */
347 {12, 0x54, 44}, /* 0000 0101 0100 */
348 {12, 0x55, 45}, /* 0000 0101 0101 */
349 {12, 0x56, 46}, /* 0000 0101 0110 */
350 {12, 0x57, 47}, /* 0000 0101 0111 */
351 {12, 0x58, 57}, /* 0000 0101 1000 */
352 {12, 0x59, 58}, /* 0000 0101 1001 */
353 {12, 0x5a, 61}, /* 0000 0101 1010 */
354 {12, 0x5b, 256}, /* 0000 0101 1011 */
355 {12, 0x64, 48}, /* 0000 0110 0100 */
356 {12, 0x65, 49}, /* 0000 0110 0101 */
357 {12, 0x66, 62}, /* 0000 0110 0110 */
358 {12, 0x67, 63}, /* 0000 0110 0111 */
359 {12, 0x68, 30}, /* 0000 0110 1000 */
360 {12, 0x69, 31}, /* 0000 0110 1001 */
361 {12, 0x6a, 32}, /* 0000 0110 1010 */
362 {12, 0x6b, 33}, /* 0000 0110 1011 */
363 {12, 0x6c, 40}, /* 0000 0110 1100 */
364 {12, 0x6d, 41}, /* 0000 0110 1101 */
365 {12, 0xc8, 128}, /* 0000 1100 1000 */
366 {12, 0xc9, 192}, /* 0000 1100 1001 */
367 {12, 0xca, 26}, /* 0000 1100 1010 */
368 {12, 0xcb, 27}, /* 0000 1100 1011 */
369 {12, 0xcc, 28}, /* 0000 1100 1100 */
370 {12, 0xcd, 29}, /* 0000 1100 1101 */
371 {12, 0xd2, 34}, /* 0000 1101 0010 */
372 {12, 0xd3, 35}, /* 0000 1101 0011 */
373 {12, 0xd4, 36}, /* 0000 1101 0100 */
374 {12, 0xd5, 37}, /* 0000 1101 0101 */
375 {12, 0xd6, 38}, /* 0000 1101 0110 */
376 {12, 0xd7, 39}, /* 0000 1101 0111 */
377 {12, 0xda, 42}, /* 0000 1101 1010 */
378 {12, 0xdb, 43}, /* 0000 1101 1011 */
379 {13, 0x4a, 640}, /* 0000 0010 0101 0 */
380 {13, 0x4b, 704}, /* 0000 0010 0101 1 */
381 {13, 0x4c, 768}, /* 0000 0010 0110 0 */
382 {13, 0x4d, 832}, /* 0000 0010 0110 1 */
383 {13, 0x52, 1280}, /* 0000 0010 1001 0 */
384 {13, 0x53, 1344}, /* 0000 0010 1001 1 */
385 {13, 0x54, 1408}, /* 0000 0010 1010 0 */
386 {13, 0x55, 1472}, /* 0000 0010 1010 1 */
387 {13, 0x5a, 1536}, /* 0000 0010 1101 0 */
388 {13, 0x5b, 1600}, /* 0000 0010 1101 1 */
389 {13, 0x64, 1664}, /* 0000 0011 0010 0 */
390 {13, 0x65, 1728}, /* 0000 0011 0010 1 */
391 {13, 0x6c, 512}, /* 0000 0011 0110 0 */
392 {13, 0x6d, 576}, /* 0000 0011 0110 1 */
393 {13, 0x72, 896}, /* 0000 0011 1001 0 */
394 {13, 0x73, 960}, /* 0000 0011 1001 1 */
395 {13, 0x74, 1024}, /* 0000 0011 1010 0 */
396 {13, 0x75, 1088}, /* 0000 0011 1010 1 */
397 {13, 0x76, 1152}, /* 0000 0011 1011 0 */
398 {13, 0x77, 1216} /* 0000 0011 1011 1 */
401 static Tab faxcodes[Nfaxcodes] = {
403 {3, 0x1, 0}, /* 001 */
404 {3, 0x2, 0}, /* 010 */
405 {3, 0x3, 0}, /* 011 */
406 {4, 0x1, 0}, /* 0001 */
407 {6, 0x2, 0}, /* 0000 10 */
408 {6, 0x3, 0}, /* 0000 11 */
409 {7, 0x2, 0}, /* 0000 010 */
410 {7, 0x3, 0}, /* 0000 011 */
411 {12, 0x1, -1} /* 0000 0000 0001 */
414 static int typesizes[] = {0, 1, 0, 2, 4};
415 static int vcodeval[] = {0, 0, 0, 1, 0, 0, 2, 3};
417 static ulong byte2le(uchar *);
418 static ulong byte4le(uchar *);
419 static ulong byte2be(uchar *);
420 static ulong byte4be(uchar *);
421 static void readdata(Tif *, ulong);
422 static void readnbytes(Tif *, ulong);
423 static ulong readbyte(Tif *);
424 static ulong readshort(Tif *);
425 static ulong readlong(Tif *);
426 static int gototif(Tif *, ulong);
427 static int readheader(Tif *);
428 static uchar *nocomp(Tif *);
429 static int getbit1(Fax *);
430 static int getbit2(Fax *);
431 static Tab *findtab(Fax *, int, int, Tab *, int);
432 static Tab *gettab(Fax *, int);
433 static Tab *geteol(Fax *);
434 static int faxfill(Fax *, uchar *, ulong, ulong *, ulong *, ulong, int);
435 static void fillbits(Fax *);
436 static int faxalloclines(Fax *);
437 static Tab *getfax1d(Fax *, uchar *, ulong, ulong *, ulong *, ulong);
438 static Tab *getfax2d(Fax *, uchar *, ulong, ulong *, ulong *, ulong);
439 static int faxstrip(Tif *, Fax *, uchar *, ulong, ulong, ulong *);
440 static uchar *fax(Tif *);
441 static void tabinit(Lzw *);
442 static Code *newcode(Lzw *, Code *);
443 static void listadd(Lzw *, Code *);
444 static Code *tabadd(Lzw *, Code *, Code *);
445 static int getcode(Lzw *);
446 static int wstr(uchar *, ulong, ulong *, Code *, long *);
447 static void predict1(Tif *, uchar *);
448 static void predict8(Tif *, uchar *);
449 static int lzwstrip(Lzw *, uchar *, ulong, ulong *, long);
450 static uchar *lzw(Tif *);
451 static uchar *packbits(Tif *);
452 static int faxdecode(Tif *, Rawimage *, uchar *);
453 static int greydecode(Tif *, Rawimage *, uchar *);
454 static int rgbdecode(Tif *, Rawimage *, uchar *);
455 static int paldecode(Tif *, Rawimage *, uchar *);
456 static int parsefield(Tif *, Fld *);
457 static int readfield(Tif *, Fld *);
458 static int checkfields(Tif *);
459 static int readstrips(Tif *);
460 static Rawimage *decode(Tif *);
461 static void freefields(Tif *);
462 static Rawimage *readslave(Tif *);
467 return (buf[1] << 8) | buf[0];
473 return (byte2le(buf+2) << 16) | byte2le(buf);
479 return (buf[0] << 8) | buf[1];
485 return (byte2be(buf) << 16) | byte2be(buf+2);
489 readdata(Tif *t, ulong offset)
496 m = offset + 4096 - t->nbuf;
497 size = (m + t->nbuf) * sizeof *t->buf;
499 if((t->buf = malloc(size)) == nil)
500 sysfatal("malloc: %r");
502 if((t->buf = realloc(t->buf, size)) == nil)
503 sysfatal("realloc: %r");
505 if((n = Bread(t->fd, t->buf+t->nbuf, m)) < 0)
506 sysfatal("Bread: %r");
513 readnbytes(Tif *t, ulong n)
516 sysfatal("cannot read %lud bytes", n);
517 if(t->n+n > t->nbuf) {
519 sysfatal("reached end of file");
522 memmove(t->tmp, t->buf+t->n, n);
537 return (*t->byte2)(t->tmp);
544 return (*t->byte4)(t->tmp);
548 gototif(Tif *t, ulong n)
551 werrstr("offset pointing to header");
565 t->end = readshort(t);
574 werrstr("illegal byte order: %#.4x", t->end);
577 if((n = readshort(t)) != TIF) {
578 werrstr("illegal tiff magic: %#.4x", n);
581 t->off = readlong(t);
582 return gototif(t, t->off);
598 bit = (f->data[f->n] >> f->m) & 0x1;
614 bit = (f->data[f->n] >> f->m) & 0x1;
624 findtab(Fax *f, int code, int len, Tab *tab, int max)
628 while(f->ntab < max) {
632 if(p->code == code) {
642 gettab(Fax *f, int mode)
644 int i, n, maxlen, bit, code;
655 maxlen = tab[n-1].len;
656 for(i = 1; i <= maxlen; i++) {
657 if((bit = (*f->getbit)(f)) < 0) {
661 code = (code << 1) | bit;
662 if((p = findtab(f, code, i, tab, n)) != nil)
665 werrstr("code not found");
677 for(i = 0; i < 4; i++) {
678 if((*f->getbit)(f) < 0) {
684 if((p = gettab(f, 0)) == nil || p->run >= 0) {
685 werrstr("first eol");
691 for(i = 0; (bit = (*f->getbit)(f)) == 0; i++)
705 faxfill(Fax *f, uchar *data, ulong size, ulong *i, ulong *x, ulong dx,
709 werrstr("fax row overflow");
715 memset(data+*i-n, f->st, n);
722 if(f->getbit == getbit1) {
736 faxalloclines(Fax *f)
739 f->l1 = realloc(f->l1, f->nl*sizeof *f->l1);
742 f->l2 = realloc(f->l2, f->nl*sizeof *f->l2);
751 getfax1d(Fax *f, uchar *data, ulong size, ulong *i, ulong *x,
757 for(j = n = 0; *x < dx;) {
758 if((p = gettab(f, 0)) == nil)
760 if((n = p->run) < 0) {
764 if(faxfill(f, data, size, i, x, dx, n) < 0)
775 if((p = gettab(f, 0)) == nil)
780 werrstr("no terminating code");
788 getfax2d(Fax *f, uchar *data, ulong size, ulong *i, ulong *x,
791 int j, k, n, code, len, v;
796 for(j = 0; *x < dx;) {
799 if(b1 > a0 && f->st == k%2)
806 if((p = gettab(f, 1)) == nil)
817 if(code == 1 && len == 3) {
820 if((p = gettab(f, 0)) == nil)
822 if((n = p->run) < 0) {
826 if(faxfill(f, data, size, i, x,
835 } else if(code == 1 && len == 4) {
838 if(faxfill(f, data, size, i, x, dx, n) < 0)
858 if(faxfill(f, data, size, i, x, dx, n) < 0)
865 memmove(f->l1, f->l2, j*sizeof *f->l1);
870 faxstrip(Tif *t, Fax *f, uchar *data, ulong size, ulong rows,
877 d1 = t->comp != T6enc;
879 for(x = y = 0; x < t->dx || y < rows;) {
881 if(t->comp == T4enc) {
882 if(p == nil && geteol(f) == nil) {
893 d1 = (*f->getbit)(f);
901 p = getfax1d(f, data, size, i,
904 p = getfax2d(f, data, size, i,
907 if(t->comp == Huffman)
909 if(p == nil && x != t->dx) {
918 werrstr("fax data overflow");
924 /* i've encountered t4 images that did not have rtcs */
929 ulong i, j, datasz, r, dy;
933 datasz = t->dx * t->dy * sizeof *data;
934 data = mallocz(datasz, 1);
936 f.l1 = mallocz(f.nl*sizeof *f.l1, 1);
937 f.l2 = mallocz(f.nl*sizeof *f.l2, 1);
938 if(data == nil || f.l1 == nil || f.l2 == nil) {
959 if(t->comp == T4enc && t->t4 & (1<<1))
964 for(i = j = 0, dy = t->dy; i < t->nstrips; i++) {
969 f.next = t->strips[i+1];
972 r = dy < t->rows? dy: t->rows;
973 if(faxstrip(t, &f, data, datasz, r, &j) < 0)
990 l->ntab = Eoicode + 1;
995 newcode(Lzw *l, Code *p)
1001 if(l->first != nil) {
1003 if((l->first = l->first->next) == nil)
1005 } else if((q = malloc(sizeof *q)) == nil)
1013 listadd(Lzw *l, Code *p)
1020 l->first = l->last = p;
1021 while(l->last->next != nil)
1022 l->last = l->last->next;
1026 tabadd(Lzw *l, Code *p, Code *q)
1030 r = s = &l->tab[l->ntab];
1040 if(l->ntab++ >= Tabsz-3) {
1041 werrstr("lzw table full");
1045 while((p = p->next) != nil) {
1047 s->next->val = p->val;
1048 else if((s->next = newcode(l, p)) == nil)
1052 if(s->next != nil) {
1053 s->next->val = q->val;
1055 if(s->next != nil) {
1056 listadd(l, s->next);
1059 } else if((s->next = newcode(l, q)) == nil)
1069 if(l->n >= l->next) {
1075 for(i = l->len-1; i >= 0; i--) {
1076 c = (l->data[l->n] >> l->m) & 0x1;
1081 if(++l->n >= l->next && i > 0)
1089 wstr(uchar *data, ulong size, ulong *i, Code *p, long *striplen)
1091 for(; p != nil; p = p->next, ++*i, --*striplen) {
1092 if(*i >= size || *striplen < 0) {
1093 werrstr("lzw overflow");
1102 predict1(Tif *t, uchar *data)
1104 int bpl, pix, b[8], d, m, n, j;
1108 bpl = bytesperline(Rect(0, 0, t->dx, t->dy), d);
1111 for(y = 0; y < t->dy; y++) {
1112 for(x = 0; x < bpl; x++, data++) {
1114 b[n-1] = (pix >> d*(n-1)) & m;
1116 b[n-1] += *(data-1) & m;
1117 for(j = n-2; j >= 0; j--) {
1118 b[j] = (pix >> d*j) & m;
1121 for(j = pix = 0; j < n; j++)
1122 pix |= (b[j] & m) << d*j;
1129 predict8(Tif *t, uchar *data)
1134 for(y = 0; y < t->dy; y++) {
1135 for(x = 1, data += s; x < t->dx; x++) {
1136 for(j = 0; j < s; j++, data++)
1143 lzwstrip(Lzw *l, uchar *data, ulong size, ulong *i, long striplen)
1148 if((c = getcode(l)) != Clrcode) {
1149 werrstr("clear code");
1152 for(oc = -1; c != Eoicode;) {
1158 if((c = getcode(l)) == Eoicode)
1163 werrstr("table overflow");
1166 if(wstr(data, size, i, &l->tab[c],
1169 } else if(c < l->ntab) {
1171 if(wstr(data, size, i, p,
1175 if(tabadd(l, q, p) == nil)
1177 } else if(c == l->ntab) {
1179 if((p = tabadd(l, q, q)) == nil)
1181 if(wstr(data, size, i, p,
1185 werrstr("table overflow");
1199 ulong i, j, size, r, dy;
1205 size = ((t->dx*t->dy*t->depth + 7) / 8) * sizeof *data;
1206 if((data = malloc(size)) == nil) {
1210 for(i = 0; i < Tabsz; i++) {
1212 l.tab[i].next = nil;
1215 l.first = l.last = nil;
1216 for(i = j = 0, dy = t->dy; i < t->nstrips; i++) {
1220 if(i < t->nstrips-1)
1221 l.next = t->strips[i+1];
1224 r = dy < t->rows? dy: t->rows;
1225 striplen = (t->dx*r*t->depth + 7) / 8;
1226 if(lzwstrip(&l, data, size, &j, striplen) < 0)
1230 if(i < t->nstrips) {
1234 for(i = 0; i < Tabsz; i++) {
1235 for(p = l.tab[i].next; (q = p) != nil;) {
1240 for(p = l.first; (q = p) != nil;) {
1245 if(data != nil && t->predictor == 2) {
1258 ulong i, j, k, size;
1261 size = ((t->dx*t->dy*t->depth + 7) / 8) * sizeof *data;
1262 if((data = malloc(size)) == nil) {
1266 for(i = 0, j = 0; i < t->ndata;) {
1267 n = (char)t->data[i++];
1270 if((j += k) > size || (i += k) > t->ndata)
1272 memmove(data+j-k, t->data+i-k, k);
1273 } else if(n > -128 && n < 0) {
1275 if(k > size || i >= t->ndata)
1278 data[j] = t->data[i];
1283 werrstr("packbits overflow");
1292 faxdecode(Tif *t, Rawimage *im, uchar *data)
1296 for(n = 0; n < im->chanlen; n++) {
1297 if(t->photo == Whitezero)
1299 im->chans[0][n] = data[n] * 0xff;
1305 greydecode(Tif *t, Rawimage *im, uchar *data)
1307 int pix, pmask, xmask;
1310 pmask = (1 << t->depth) - 1;
1311 xmask = 7 >> log2[t->depth];
1312 for(y = 0, n = 0; y < t->dy; y++) {
1313 i = y * bytesperline(im->r, t->depth);
1314 for(x = 0; x < t->dx; x++, n++) {
1315 if(n >= im->chanlen) {
1316 werrstr("grey overflow");
1319 pix = (data[i] >> t->depth*((xmask -
1320 x) & xmask)) & pmask;
1321 if(((x + 1) & xmask) == 0)
1323 if(t->photo == Whitezero)
1325 pix = (pix * 0xff) / pmask;
1326 im->chans[0][n] = pix;
1333 rgbdecode(Tif *t, Rawimage *im, uchar *data)
1337 for(y = 0, n = 0; y < t->dy; y++) {
1338 for(x = 0; x < t->dx; x++, n += 3) {
1339 if(n >= im->chanlen) {
1340 werrstr("rgb overflow");
1343 i = (y*t->dx + x) * 3;
1344 im->chans[0][n] = data[i+2];
1345 im->chans[0][n+1] = data[i+1];
1346 im->chans[0][n+2] = data[i];
1353 paldecode(Tif *t, Rawimage *im, uchar *data)
1355 int pix, pmask, xmask;
1356 ulong i, n, x, y, *r, *g, *b, max;
1358 pmask = (1 << t->depth) - 1;
1359 xmask = 7 >> log2[t->depth];
1360 for(i = 0, max = 1; i < t->ncolor; i++) {
1361 if(t->color[i] > max)
1364 for(i = 0; i < t->ncolor; i++)
1365 t->color[i] = (t->color[i] * 0xff) / max;
1369 for(y = 0, n = 0; y < t->dy; y++) {
1370 i = y * bytesperline(im->r, t->depth);
1371 for(x = 0; x < t->dx; x++, n += 3) {
1372 if(n >= im->chanlen) {
1373 werrstr("palette overflow");
1376 pix = (data[i] >> t->depth*((xmask -
1377 x) & xmask)) & pmask;
1378 if(((x + 1) & xmask) == 0)
1380 im->chans[0][n] = b[pix];
1381 im->chans[0][n+1] = g[pix];
1382 im->chans[0][n+2] = r[pix];
1389 parsefield(Tif *t, Fld *f)
1404 t->depth += f->val[1] + f->val[2];
1414 t->decode = greydecode;
1417 t->decode = rgbdecode;
1420 t->decode = paldecode;
1428 t->nstrips = f->cnt;
1444 t->ncounts = f->cnt;
1463 werrstr("shouldn't reach");
1470 readfield(Tif *t, Fld *f)
1474 ulong (*readval)(Tif *);
1476 f->tag = readshort(t);
1477 f->typ = readshort(t);
1478 f->cnt = readlong(t);
1494 werrstr("field count");
1499 if(f->cnt != 1 && f->cnt != 3) {
1500 werrstr("field count");
1517 readval = readshort;
1523 werrstr("unsupported type\n");
1526 if((f->val = malloc(f->cnt*sizeof *f->val)) == nil)
1528 size = typesizes[f->typ];
1529 if((n = size*f->cnt) <= 4) {
1530 for(i = 0; i < f->cnt; i++)
1531 f->val[i] = (*readval)(t);
1534 for(j = n; j < 4; j += size)
1537 f->off = readlong(t);
1539 if(gototif(t, f->off) < 0)
1541 for(i = 0; i < f->cnt; i++)
1542 f->val[i] = (*readval)(t);
1544 if(gototif(t, off) < 0)
1547 return parsefield(t, f);
1556 werrstr("image width");
1560 werrstr("image length");
1570 werrstr("bits per sample");
1575 t->uncompress = nocomp;
1580 t->uncompress = fax;
1581 if(t->decode != nil)
1582 t->decode = faxdecode;
1583 if((t->comp == T4enc && t->t4 & (1<<1)) ||
1584 (t->comp == T6enc &&
1586 werrstr("uncompressed mode");
1591 t->uncompress = lzw;
1594 t->uncompress = packbits;
1597 werrstr("compression");
1600 if(t->decode == nil) {
1601 werrstr("photometric interpretation");
1604 if(t->depth > 1 && (t->comp == Huffman ||
1605 t->comp == T4enc || t->comp == T6enc)) {
1606 werrstr("compression");
1609 if(t->fill != 1 && t->fill != 2) {
1610 werrstr("fill order");
1613 if(t->fill == 2 && t->depth != 1) {
1614 werrstr("depth should be 1 with fill order 2");
1617 if(t->orientation != 1) {
1618 werrstr("orientation");
1622 werrstr("rows per strip");
1625 n = (t->dy + t->rows - 1) / t->rows;
1626 if(t->strips == nil || t->nstrips != n) {
1627 werrstr("strip offsets");
1630 if(t->samples == 1 && t->photo == Rgb) {
1631 werrstr("not enough samples per pixel");
1634 if(t->samples == 3 && t->photo != Rgb) {
1635 werrstr("too many samples per pixel");
1638 if(t->samples != 1 && t->samples != 3) {
1639 werrstr("samples per pixel");
1643 * strip byte counts should not be missing,
1644 * but we can guess correctly in this case
1646 size = sizeof *t->counts;
1647 if(t->counts == nil && t->comp == Nocomp &&
1649 (t->counts = malloc(size)) != nil) {
1650 t->counts[0] = (t->dx*t->dy*t->depth + 7) / 8;
1651 t->ncounts = t->nstrips;
1653 if(t->counts == nil || t->ncounts != t->nstrips) {
1654 werrstr("strip byte counts");
1657 if(t->planar != 1) {
1658 werrstr("planar configuration");
1661 if(t->photo == Palette && (t->color == nil ||
1662 t->ncolor != 3*(1<<t->depth))) {
1663 werrstr("color map");
1677 for(i = 0; i < t->nstrips; i++)
1678 t->ndata += t->counts[i];
1680 werrstr("no image data");
1683 if((t->data = malloc(t->ndata*sizeof *t->data)) == nil)
1686 for(i = n = 0; i < t->nstrips; i++) {
1687 if(gototif(t, t->strips[i]) < 0)
1690 * we store each strip's offset in t->data
1691 * in order to skip the final rtc or eofb
1692 * during fax decoding. t->strips is used
1693 * to save on memory allocation. these
1694 * offsets are also used in lzw as a
1695 * preventive measure.
1698 for(j = 0; j < t->counts[i]; j++, n++)
1699 t->data[n] = readbyte(t);
1701 return gototif(t, off);
1711 if((im = malloc(sizeof *im)) == nil)
1713 im->r = Rect(0, 0, t->dx, t->dy);
1716 im->chanlen = t->dx * t->dy;
1717 if(t->photo == Rgb || t->photo == Palette) {
1718 im->chandesc = CRGB24;
1723 size = im->chanlen * sizeof *im->chans[0];
1724 if((im->chans[0] = malloc(size)) == nil)
1726 /* unused members */
1731 im->gifloopcount = 1;
1732 if((data = (*t->uncompress)(t)) == nil)
1734 if((*t->decode)(t, im, data) < 0) {
1748 for(i = 0; i < t->nfld; i++) {
1749 if(t->fld[i].val != nil)
1750 free(t->fld[i].val);
1761 if(readheader(t) < 0)
1763 if((t->nfld = readshort(t)) <= 0) {
1764 werrstr("illegal field number: %#.4x", t->nfld);
1767 if((t->fld = malloc(t->nfld*sizeof *t->fld)) == nil)
1769 for(i = 0; i < t->nfld; i++) {
1770 if(readfield(t, &t->fld[i]) < 0) {
1771 if(t->fld[i].val != nil)
1772 free(t->fld[i].val);
1777 for(j = 0; j < i; j++) {
1778 if(t->fld[j].val != nil)
1779 free(t->fld[j].val);
1785 if(checkfields(t) < 0) {
1789 if(readstrips(t) < 0) {
1802 Breadtif(Biobuf *b, int colorspace)
1804 Rawimage **array, *r;
1807 if(colorspace != CRGB24) {
1808 werrstr("unknown color space: %d",
1812 if((t = malloc(sizeof *t)) == nil)
1814 if((array = malloc(2*sizeof *array)) == nil)
1818 t->nbuf = t->eof = t->n = 0;
1819 /* order doesn't matter for the first two bytes */
1827 t->uncompress = nil;
1835 t->rows = 0xffffffff; /* entire image is one strip */
1852 readtif(int fd, int colorspace)
1857 if(Binit(&b, fd, OREAD) < 0)
1859 a = Breadtif(&b, colorspace);