3 * http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
4 * http://paulbourke.net/dataformats/tiff/
5 * http://www.fileformat.info/format/tiff/egff.htm
6 * http://www.fileformat.info/mirror/egff/ch09_05.htm
7 * http://www.itu.int/rec/T-REC-T.4-199904-S/en
8 * http://www.itu.int/rec/T-REC-T.6-198811-I/en
11 * http://www.remotesensing.org/libtiff/
17 #include "imagefile.h"
20 II = 0x4949, /* little-endian */
21 MM = 0x4d4d, /* big-endian */
22 TIF = 0x002a /* tiff magic number */
74 typedef struct Tab Tab;
75 typedef struct Fax Fax;
76 typedef struct Code Code;
77 typedef struct Lzw Lzw;
78 typedef struct Fld Fld;
79 typedef struct Tif Tif;
84 int run; /* run length */
92 int ntab; /* position in tab */
99 ulong next; /* next strip offset in data */
110 int len; /* code length */
114 ulong next; /* next strip offset in data */
115 /* remaining allocated codes */
124 ulong off; /* value offset */
131 uint end; /* endianness */
135 int eof; /* reached end of image */
136 ulong n; /* offset in buf array */
140 ulong (*byte2)(uchar *);
141 ulong (*byte4)(uchar *);
148 uchar *(*uncompress)(Tif *);
151 int (*decode)(Tif *, Rawimage *, uchar *);
160 ulong *color; /* color map */
172 * imported from libdraw/arith.c to permit
173 * extern log2 function
175 static int log2[] = {
176 -1, 0, 1, -1, 2, -1, -1, -1, 3,
177 -1, -1, -1, -1, -1, -1, -1, 4,
178 -1, -1, -1, -1, -1, -1, -1, 4 /* BUG */,
179 -1, -1, -1, -1, -1, -1, -1, 5
182 static Tab faxwhite[Nfaxtab] = {
183 {4, 0x7, 2}, /* 0111 */
184 {4, 0x8, 3}, /* 1000 */
185 {4, 0xb, 4}, /* 1011 */
186 {4, 0xc, 5}, /* 1100 */
187 {4, 0xe, 6}, /* 1110 */
188 {4, 0xf, 7}, /* 1111 */
189 {5, 0x12, 128}, /* 1001 0 */
190 {5, 0x13, 8}, /* 1001 1 */
191 {5, 0x14, 9}, /* 1010 0 */
192 {5, 0x1b, 64}, /* 1101 1 */
193 {5, 0x7, 10}, /* 0011 1 */
194 {5, 0x8, 11}, /* 0100 0 */
195 {6, 0x17, 192}, /* 0101 11 */
196 {6, 0x18, 1664}, /* 0110 00 */
197 {6, 0x2a, 16}, /* 1010 10 */
198 {6, 0x2b, 17}, /* 1010 11 */
199 {6, 0x3, 13}, /* 0000 11 */
200 {6, 0x34, 14}, /* 1101 00 */
201 {6, 0x35, 15}, /* 1101 01 */
202 {6, 0x7, 1}, /* 0001 11 */
203 {6, 0x8, 12}, /* 0010 00 */
204 {7, 0x13, 26}, /* 0010 011 */
205 {7, 0x17, 21}, /* 0010 111 */
206 {7, 0x18, 28}, /* 0011 000 */
207 {7, 0x24, 27}, /* 0100 100 */
208 {7, 0x27, 18}, /* 0100 111 */
209 {7, 0x28, 24}, /* 0101 000 */
210 {7, 0x2b, 25}, /* 0101 011 */
211 {7, 0x3, 22}, /* 0000 011 */
212 {7, 0x37, 256}, /* 0110 111 */
213 {7, 0x4, 23}, /* 0000 100 */
214 {7, 0x8, 20}, /* 0001 000 */
215 {7, 0xc, 19}, /* 0001 100 */
216 {8, 0x12, 33}, /* 0001 0010 */
217 {8, 0x13, 34}, /* 0001 0011 */
218 {8, 0x14, 35}, /* 0001 0100 */
219 {8, 0x15, 36}, /* 0001 0101 */
220 {8, 0x16, 37}, /* 0001 0110 */
221 {8, 0x17, 38}, /* 0001 0111 */
222 {8, 0x1a, 31}, /* 0001 1010 */
223 {8, 0x1b, 32}, /* 0001 1011 */
224 {8, 0x2, 29}, /* 0000 0010 */
225 {8, 0x24, 53}, /* 0010 0100 */
226 {8, 0x25, 54}, /* 0010 0101 */
227 {8, 0x28, 39}, /* 0010 1000 */
228 {8, 0x29, 40}, /* 0010 1001 */
229 {8, 0x2a, 41}, /* 0010 1010 */
230 {8, 0x2b, 42}, /* 0010 1011 */
231 {8, 0x2c, 43}, /* 0010 1100 */
232 {8, 0x2d, 44}, /* 0010 1101 */
233 {8, 0x3, 30}, /* 0000 0011 */
234 {8, 0x32, 61}, /* 0011 0010 */
235 {8, 0x33, 62}, /* 0011 0011 */
236 {8, 0x34, 63}, /* 0011 0100 */
237 {8, 0x35, 0}, /* 0011 0101 */
238 {8, 0x36, 320}, /* 0011 0110 */
239 {8, 0x37, 384}, /* 0011 0111 */
240 {8, 0x4, 45}, /* 0000 0100 */
241 {8, 0x4a, 59}, /* 0100 1010 */
242 {8, 0x4b, 60}, /* 0100 1011 */
243 {8, 0x5, 46}, /* 0000 0101 */
244 {8, 0x52, 49}, /* 0101 0010 */
245 {8, 0x53, 50}, /* 0101 0011 */
246 {8, 0x54, 51}, /* 0101 0100 */
247 {8, 0x55, 52}, /* 0101 0101 */
248 {8, 0x58, 55}, /* 0101 1000 */
249 {8, 0x59, 56}, /* 0101 1001 */
250 {8, 0x5a, 57}, /* 0101 1010 */
251 {8, 0x5b, 58}, /* 0101 1011 */
252 {8, 0x64, 448}, /* 0110 0100 */
253 {8, 0x65, 512}, /* 0110 0101 */
254 {8, 0x67, 640}, /* 0110 0111 */
255 {8, 0x68, 576}, /* 0110 1000 */
256 {8, 0xa, 47}, /* 0000 1010 */
257 {8, 0xb, 48}, /* 0000 1011 */
258 {9, 0x98, 1472}, /* 0100 1100 0 */
259 {9, 0x99, 1536}, /* 0100 1100 1 */
260 {9, 0x9a, 1600}, /* 0100 1101 0 */
261 {9, 0x9b, 1728}, /* 0100 1101 1 */
262 {9, 0xcc, 704}, /* 0110 0110 0 */
263 {9, 0xcd, 768}, /* 0110 0110 1 */
264 {9, 0xd2, 832}, /* 0110 1001 0 */
265 {9, 0xd3, 896}, /* 0110 1001 1 */
266 {9, 0xd4, 960}, /* 0110 1010 0 */
267 {9, 0xd5, 1024}, /* 0110 1010 1 */
268 {9, 0xd6, 1088}, /* 0110 1011 0 */
269 {9, 0xd7, 1152}, /* 0110 1011 1 */
270 {9, 0xd8, 1216}, /* 0110 1100 0 */
271 {9, 0xd9, 1280}, /* 0110 1100 1 */
272 {9, 0xda, 1344}, /* 0110 1101 0 */
273 {9, 0xdb, 1408}, /* 0110 1101 1 */
274 {11, 0x8, 1792}, /* 0000 0001 000 */
275 {11, 0xc, 1856}, /* 0000 0001 100 */
276 {11, 0xd, 1920}, /* 0000 0001 101 */
277 {12, 0x1, -1}, /* 0000 0000 0001 */
278 {12, 0x12, 1984}, /* 0000 0001 0010 */
279 {12, 0x13, 2048}, /* 0000 0001 0011 */
280 {12, 0x14, 2112}, /* 0000 0001 0100 */
281 {12, 0x15, 2176}, /* 0000 0001 0101 */
282 {12, 0x16, 2240}, /* 0000 0001 0110 */
283 {12, 0x17, 2304}, /* 0000 0001 0111 */
284 {12, 0x1c, 2368}, /* 0000 0001 1100 */
285 {12, 0x1d, 2432}, /* 0000 0001 1101 */
286 {12, 0x1e, 2496}, /* 0000 0001 1110 */
287 {12, 0x1f, 2560} /* 0000 0001 1111 */
290 static Tab faxblack[Nfaxtab] = {
291 {2, 0x2, 3}, /* 10 */
292 {2, 0x3, 2}, /* 11 */
293 {3, 0x2, 1}, /* 010 */
294 {3, 0x3, 4}, /* 011 */
295 {4, 0x2, 6}, /* 0010 */
296 {4, 0x3, 5}, /* 0011 */
297 {5, 0x3, 7}, /* 0001 1 */
298 {6, 0x4, 9}, /* 0001 00 */
299 {6, 0x5, 8}, /* 0001 01 */
300 {7, 0x4, 10}, /* 0000 100 */
301 {7, 0x5, 11}, /* 0000 101 */
302 {7, 0x7, 12}, /* 0000 111 */
303 {8, 0x4, 13}, /* 0000 0100 */
304 {8, 0x7, 14}, /* 0000 0111 */
305 {9, 0x18, 15}, /* 0000 1100 0 */
306 {10, 0x17, 16}, /* 0000 0101 11 */
307 {10, 0x18, 17}, /* 0000 0110 00 */
308 {10, 0x37, 0}, /* 0000 1101 11 */
309 {10, 0x8, 18}, /* 0000 0010 00 */
310 {10, 0xf, 64}, /* 0000 0011 11 */
311 {11, 0x17, 24}, /* 0000 0010 111 */
312 {11, 0x18, 25}, /* 0000 0011 000 */
313 {11, 0x28, 23}, /* 0000 0101 000 */
314 {11, 0x37, 22}, /* 0000 0110 111 */
315 {11, 0x67, 19}, /* 0000 1100 111 */
316 {11, 0x68, 20}, /* 0000 1101 000 */
317 {11, 0x6c, 21}, /* 0000 1101 100 */
318 {11, 0x8, 1792}, /* 0000 0001 000 */
319 {11, 0xc, 1856}, /* 0000 0001 100 */
320 {11, 0xd, 1920}, /* 0000 0001 101 */
321 {12, 0x1, -1}, /* 0000 0000 0001 */
322 {12, 0x12, 1984}, /* 0000 0001 0010 */
323 {12, 0x13, 2048}, /* 0000 0001 0011 */
324 {12, 0x14, 2112}, /* 0000 0001 0100 */
325 {12, 0x15, 2176}, /* 0000 0001 0101 */
326 {12, 0x16, 2240}, /* 0000 0001 0110 */
327 {12, 0x17, 2304}, /* 0000 0001 0111 */
328 {12, 0x1c, 2368}, /* 0000 0001 1100 */
329 {12, 0x1d, 2432}, /* 0000 0001 1101 */
330 {12, 0x1e, 2496}, /* 0000 0001 1110 */
331 {12, 0x1f, 2560}, /* 0000 0001 1111 */
332 {12, 0x24, 52}, /* 0000 0010 0100 */
333 {12, 0x27, 55}, /* 0000 0010 0111 */
334 {12, 0x28, 56}, /* 0000 0010 1000 */
335 {12, 0x2b, 59}, /* 0000 0010 1011 */
336 {12, 0x2c, 60}, /* 0000 0010 1100 */
337 {12, 0x33, 320}, /* 0000 0011 0011 */
338 {12, 0x34, 384}, /* 0000 0011 0100 */
339 {12, 0x35, 448}, /* 0000 0011 0101 */
340 {12, 0x37, 53}, /* 0000 0011 0111 */
341 {12, 0x38, 54}, /* 0000 0011 1000 */
342 {12, 0x52, 50}, /* 0000 0101 0010 */
343 {12, 0x53, 51}, /* 0000 0101 0011 */
344 {12, 0x54, 44}, /* 0000 0101 0100 */
345 {12, 0x55, 45}, /* 0000 0101 0101 */
346 {12, 0x56, 46}, /* 0000 0101 0110 */
347 {12, 0x57, 47}, /* 0000 0101 0111 */
348 {12, 0x58, 57}, /* 0000 0101 1000 */
349 {12, 0x59, 58}, /* 0000 0101 1001 */
350 {12, 0x5a, 61}, /* 0000 0101 1010 */
351 {12, 0x5b, 256}, /* 0000 0101 1011 */
352 {12, 0x64, 48}, /* 0000 0110 0100 */
353 {12, 0x65, 49}, /* 0000 0110 0101 */
354 {12, 0x66, 62}, /* 0000 0110 0110 */
355 {12, 0x67, 63}, /* 0000 0110 0111 */
356 {12, 0x68, 30}, /* 0000 0110 1000 */
357 {12, 0x69, 31}, /* 0000 0110 1001 */
358 {12, 0x6a, 32}, /* 0000 0110 1010 */
359 {12, 0x6b, 33}, /* 0000 0110 1011 */
360 {12, 0x6c, 40}, /* 0000 0110 1100 */
361 {12, 0x6d, 41}, /* 0000 0110 1101 */
362 {12, 0xc8, 128}, /* 0000 1100 1000 */
363 {12, 0xc9, 192}, /* 0000 1100 1001 */
364 {12, 0xca, 26}, /* 0000 1100 1010 */
365 {12, 0xcb, 27}, /* 0000 1100 1011 */
366 {12, 0xcc, 28}, /* 0000 1100 1100 */
367 {12, 0xcd, 29}, /* 0000 1100 1101 */
368 {12, 0xd2, 34}, /* 0000 1101 0010 */
369 {12, 0xd3, 35}, /* 0000 1101 0011 */
370 {12, 0xd4, 36}, /* 0000 1101 0100 */
371 {12, 0xd5, 37}, /* 0000 1101 0101 */
372 {12, 0xd6, 38}, /* 0000 1101 0110 */
373 {12, 0xd7, 39}, /* 0000 1101 0111 */
374 {12, 0xda, 42}, /* 0000 1101 1010 */
375 {12, 0xdb, 43}, /* 0000 1101 1011 */
376 {13, 0x4a, 640}, /* 0000 0010 0101 0 */
377 {13, 0x4b, 704}, /* 0000 0010 0101 1 */
378 {13, 0x4c, 768}, /* 0000 0010 0110 0 */
379 {13, 0x4d, 832}, /* 0000 0010 0110 1 */
380 {13, 0x52, 1280}, /* 0000 0010 1001 0 */
381 {13, 0x53, 1344}, /* 0000 0010 1001 1 */
382 {13, 0x54, 1408}, /* 0000 0010 1010 0 */
383 {13, 0x55, 1472}, /* 0000 0010 1010 1 */
384 {13, 0x5a, 1536}, /* 0000 0010 1101 0 */
385 {13, 0x5b, 1600}, /* 0000 0010 1101 1 */
386 {13, 0x64, 1664}, /* 0000 0011 0010 0 */
387 {13, 0x65, 1728}, /* 0000 0011 0010 1 */
388 {13, 0x6c, 512}, /* 0000 0011 0110 0 */
389 {13, 0x6d, 576}, /* 0000 0011 0110 1 */
390 {13, 0x72, 896}, /* 0000 0011 1001 0 */
391 {13, 0x73, 960}, /* 0000 0011 1001 1 */
392 {13, 0x74, 1024}, /* 0000 0011 1010 0 */
393 {13, 0x75, 1088}, /* 0000 0011 1010 1 */
394 {13, 0x76, 1152}, /* 0000 0011 1011 0 */
395 {13, 0x77, 1216} /* 0000 0011 1011 1 */
398 static Tab faxcodes[Nfaxcodes] = {
400 {3, 0x1, 0}, /* 001 */
401 {3, 0x2, 0}, /* 010 */
402 {3, 0x3, 0}, /* 011 */
403 {4, 0x1, 0}, /* 0001 */
404 {6, 0x2, 0}, /* 0000 10 */
405 {6, 0x3, 0}, /* 0000 11 */
406 {7, 0x2, 0}, /* 0000 010 */
407 {7, 0x3, 0}, /* 0000 011 */
408 {12, 0x1, -1} /* 0000 0000 0001 */
411 static int typesizes[] = {0, 1, 0, 2, 4};
412 static int vcodeval[] = {0, 0, 0, 1, 0, 0, 2, 3};
414 static ulong byte2le(uchar *);
415 static ulong byte4le(uchar *);
416 static ulong byte2be(uchar *);
417 static ulong byte4be(uchar *);
418 static void readdata(Tif *, ulong);
419 static void readnbytes(Tif *, ulong);
420 static ulong readbyte(Tif *);
421 static ulong readshort(Tif *);
422 static ulong readlong(Tif *);
423 static int gototif(Tif *, ulong);
424 static int readheader(Tif *);
425 static uchar *nocomp(Tif *);
426 static int getbit1(Fax *);
427 static int getbit2(Fax *);
428 static Tab *findtab(Fax *, int, int, Tab *, int);
429 static Tab *gettab(Fax *, int);
430 static Tab *geteol(Fax *);
431 static int faxfill(Fax *, uchar *, ulong, ulong *, ulong *, ulong, int);
432 static void fillbits(Fax *);
433 static int faxalloclines(Fax *);
434 static Tab *getfax1d(Fax *, uchar *, ulong, ulong *, ulong *, ulong);
435 static Tab *getfax2d(Fax *, uchar *, ulong, ulong *, ulong *, ulong);
436 static int faxstrip(Tif *, Fax *, uchar *, ulong, ulong *);
437 static uchar *fax(Tif *);
438 static void tabinit(Lzw *);
439 static Code *newcode(Lzw *, Code *);
440 static void listadd(Lzw *, Code *);
441 static Code *tabadd(Lzw *, Code *, Code *);
442 static int getcode(Lzw *);
443 static int wstr(uchar *, ulong, ulong *, Code *, long *);
444 static void predict(Tif *, uchar *);
445 static int lzwstrip(Lzw *, uchar *, ulong, ulong *, long);
446 static uchar *lzw(Tif *);
447 static uchar *packbits(Tif *);
448 static int faxdecode(Tif *, Rawimage *, uchar *);
449 static int greydecode(Tif *, Rawimage *, uchar *);
450 static int rgbdecode(Tif *, Rawimage *, uchar *);
451 static int paldecode(Tif *, Rawimage *, uchar *);
452 static int parsefield(Tif *, Fld *);
453 static int readfield(Tif *, Fld *);
454 static int checkfields(Tif *);
455 static int readstrips(Tif *);
456 static Rawimage *decode(Tif *);
457 static void freefields(Tif *);
458 static Rawimage *readslave(Tif *);
463 return (buf[1] << 8) | buf[0];
469 return (byte2le(buf+2) << 16) | byte2le(buf);
475 return (buf[0] << 8) | buf[1];
481 return (byte2be(buf) << 16) | byte2be(buf+2);
485 readdata(Tif *t, ulong offset)
492 m = offset + 4096 - t->nbuf;
493 size = (m + t->nbuf) * sizeof *t->buf;
495 if((t->buf = malloc(size)) == nil)
496 sysfatal("malloc: %r");
498 if((t->buf = realloc(t->buf, size)) == nil)
499 sysfatal("realloc: %r");
501 if((n = Bread(t->fd, t->buf+t->nbuf, m)) < 0)
502 sysfatal("Bread: %r");
509 readnbytes(Tif *t, ulong n)
512 sysfatal("cannot read %lud bytes", n);
513 if(t->n+n > t->nbuf) {
515 sysfatal("reached end of file");
518 memmove(t->tmp, t->buf+t->n, n);
533 return (*t->byte2)(t->tmp);
540 return (*t->byte4)(t->tmp);
544 gototif(Tif *t, ulong n)
547 werrstr("offset pointing to header");
561 t->end = readshort(t);
570 werrstr("illegal byte order: %#.4x", t->end);
573 if((n = readshort(t)) != TIF) {
574 werrstr("illegal tiff magic: %#.4x", n);
577 t->off = readlong(t);
578 return gototif(t, t->off);
594 bit = (f->data[f->n] >> f->m) & 0x1;
610 bit = (f->data[f->n] >> f->m) & 0x1;
620 findtab(Fax *f, int code, int len, Tab *tab, int max)
624 while(f->ntab < max) {
628 if(p->code == code) {
638 gettab(Fax *f, int mode)
640 int i, n, maxlen, bit, code;
651 maxlen = tab[n-1].len;
652 for(i = 1; i <= maxlen; i++) {
653 if((bit = (*f->getbit)(f)) < 0) {
657 code = (code << 1) | bit;
658 if((p = findtab(f, code, i, tab, n)) != nil)
661 werrstr("code not found");
672 if((p = gettab(f, 0)) == nil || p->run >= 0) {
673 werrstr("first eol");
679 for(i = 0; (bit = (*f->getbit)(f)) == 0; i++)
693 faxfill(Fax *f, uchar *data, ulong size, ulong *i, ulong *x, ulong dx,
697 werrstr("fax row overflow");
700 if((*i += n) >= size) {
701 werrstr("fax data overflow");
705 memset(data+*i-n, f->st, n);
712 if(f->getbit == getbit1) {
726 faxalloclines(Fax *f)
729 f->l1 = realloc(f->l1, f->nl*sizeof *f->l1);
732 f->l2 = realloc(f->l2, f->nl*sizeof *f->l2);
741 getfax1d(Fax *f, uchar *data, ulong size, ulong *i, ulong *x,
747 for(j = 0; *x < dx;) {
748 if((p = gettab(f, 0)) == nil)
750 if((n = p->run) < 0) {
754 if(faxfill(f, data, size, i, x, dx, n) < 0)
767 getfax2d(Fax *f, uchar *data, ulong size, ulong *i, ulong *x,
770 int j, k, n, code, len, a0, a1, b1, b2, v;
774 for(j = 0; *x < dx;) {
777 if(b1 > a0 && f->st == k%2)
784 if((p = gettab(f, 1)) == nil)
793 if(code == 1 && len == 3) {
796 if((p = gettab(f, 0)) == nil)
799 if(faxfill(f, data, size, i, x,
808 } else if(code == 1 && len == 4) {
811 if(faxfill(f, data, size, i, x, dx, n) < 0)
831 if(faxfill(f, data, size, i, x, dx, n) < 0)
840 memmove(f->l1, f->l2, j*sizeof *f->l1);
845 faxstrip(Tif *t, Fax *f, uchar *data, ulong size, ulong *i)
851 d1 = t->comp != T6enc;
853 for(x = y = 0; x < t->dx || y < t->rows;) {
855 if(t->comp == T4enc) {
856 if(p == nil && geteol(f) == nil) {
864 d1 = (*f->getbit)(f);
872 p = getfax1d(f, data, size, i,
875 p = getfax2d(f, data, size, i,
878 if(t->comp == Huffman)
880 if(p == nil && x != t->dx) {
881 if(f->st >= 0 || x > t->dx)
890 * the t4 fax test images i decoded did not follow the
891 * spec. in particular, they did not have rtcs.
897 ulong i, j, datasz, linesz;
901 datasz = t->dx * t->dy * sizeof *data;
902 data = malloc(datasz);
904 linesz = f.nl * sizeof *f.l1;
905 f.l1 = malloc(linesz);
906 f.l2 = malloc(linesz);
907 if(data == nil || f.l1 == nil || f.l2 == nil) {
917 memset(data, 0, datasz);
918 memset(f.l1, 0, linesz);
919 memset(f.l2, 0, linesz);
932 for(i = j = 0; i < t->nstrips; i++) {
937 f.next = t->strips[i+1];
940 if(faxstrip(t, &f, data, datasz, &j) < 0)
956 l->ntab = Eoicode + 1;
961 newcode(Lzw *l, Code *p)
967 if(l->first != nil) {
969 if((l->first = l->first->next) == nil)
971 } else if((q = malloc(sizeof *q)) == nil)
979 listadd(Lzw *l, Code *p)
986 l->first = l->last = p;
987 while(l->last->next != nil)
988 l->last = l->last->next;
992 tabadd(Lzw *l, Code *p, Code *q)
996 if(l->ntab >= Tabsz) {
997 werrstr("lzw table full");
1000 r = s = &l->tab[l->ntab++];
1011 while((p = p->next) != nil) {
1013 s->next->val = p->val;
1014 else if((s->next = newcode(l, p)) == nil)
1018 if(s->next != nil) {
1019 s->next->val = q->val;
1021 if(s->next != nil) {
1022 listadd(l, s->next);
1025 } else if((s->next = newcode(l, q)) == nil)
1035 if(l->n >= l->next) {
1040 for(i = l->len-1; i >= 0; i--) {
1041 c = (l->data[l->n] >> l->m) & 0x1;
1053 wstr(uchar *data, ulong size, ulong *i, Code *p, long *striplen)
1055 for(; p != nil; p = p->next, ++*i, --*striplen) {
1056 if(*i >= size || *striplen < 0) {
1057 werrstr("lzw overflow");
1066 predict(Tif *t, uchar *data)
1069 ulong y, x, i, j, k, s;
1072 for(y = 0; y < t->dy; y++) {
1073 for(x = 1; x < t->dx; x++) {
1075 for(j = 0; j < s; j++) {
1078 b = (char)data[k-s];
1079 data[k] = (uchar)(a + b);
1086 lzwstrip(Lzw *l, uchar *data, ulong size, ulong *i, long striplen)
1091 if((c = getcode(l)) != Clrcode) {
1092 werrstr("clear code");
1095 for(oc = -1; c != Eoicode;) {
1101 if((c = getcode(l)) == Eoicode)
1105 if(wstr(data, size, i, &l->tab[c],
1108 } else if(c < l->ntab) {
1110 if(wstr(data, size, i, p,
1114 if(tabadd(l, q, p) == nil)
1118 if((p = tabadd(l, q, q)) == nil)
1120 if(wstr(data, size, i, p,
1141 i = t->dx * t->rows * t->depth;
1142 striplen = i%8 == 0? i/8: i/8+1;
1143 size = t->nstrips * striplen * sizeof *data;
1144 if((data = malloc(size)) == nil) {
1148 for(i = 0; i < Tabsz; i++) {
1150 l.tab[i].next = nil;
1153 l.first = l.last = nil;
1154 for(i = j = 0; i < t->nstrips; i++) {
1158 if(i < t->nstrips-1)
1159 l.next = t->strips[i+1];
1162 if(lzwstrip(&l, data, size, &j, striplen) < 0)
1165 if(i < t->nstrips) {
1169 for(i = 0; i < Tabsz; i++) {
1170 for(p = l.tab[i].next; (q = p) != nil;) {
1175 for(p = l.first; (q = p) != nil;) {
1180 if(data != nil && t->predictor == 2)
1189 ulong i, j, k, size;
1192 size = t->dx * t->dy * t->samples * sizeof *data;
1193 if((data = malloc(size)) == nil) {
1197 for(i = 0, j = 0; i < t->ndata;) {
1198 n = (char)t->data[i++];
1201 if(j+k >= size || i+k >= t->ndata)
1203 memmove(data+j, t->data+i, k);
1206 } else if(n > -128 && n < 0) {
1208 if(k > size || i >= t->ndata)
1211 data[j] = t->data[i];
1216 werrstr("packbits overflow");
1225 faxdecode(Tif *t, Rawimage *im, uchar *data)
1229 for(n = 0; n < im->chanlen; n++) {
1230 if(t->photo == Whitezero)
1232 im->chans[0][n] = data[n] * 0xff;
1238 greydecode(Tif *t, Rawimage *im, uchar *data)
1240 int pix, pmask, xmask;
1243 pmask = (1 << t->depth) - 1;
1244 xmask = 7 >> log2[t->depth];
1245 for(y = 0, n = 0; y < t->dy; y++) {
1246 i = y * bytesperline(im->r, t->depth);
1247 for(x = 0; x < t->dx; x++, n++) {
1248 if(n >= im->chanlen) {
1249 werrstr("grey overflow");
1252 pix = (data[i] >> t->depth*((xmask -
1253 x) & xmask)) & pmask;
1254 if(((x + 1) & xmask) == 0)
1256 if(t->photo == Whitezero)
1258 pix = (pix * 0xff) / pmask;
1259 im->chans[0][n] = pix;
1266 rgbdecode(Tif *t, Rawimage *im, uchar *data)
1270 for(y = 0, n = 0; y < t->dy; y++) {
1271 for(x = 0; x < t->dx; x++, n += 3) {
1272 if(n >= im->chanlen) {
1273 werrstr("rgb overflow");
1276 i = (y*t->dx + x) * 3;
1277 im->chans[0][n] = data[i+2];
1278 im->chans[0][n+1] = data[i+1];
1279 im->chans[0][n+2] = data[i];
1286 paldecode(Tif *t, Rawimage *im, uchar *data)
1288 int pix, pmask, xmask;
1289 ulong i, n, x, y, *r, *g, *b, max;
1291 pmask = (1 << t->depth) - 1;
1292 xmask = 7 >> log2[t->depth];
1293 for(i = 0, max = 1; i < t->ncolor; i++) {
1294 if(t->color[i] > max)
1297 for(i = 0; i < t->ncolor; i++)
1298 t->color[i] = (t->color[i] * 0xff) / max;
1302 for(y = 0, n = 0; y < t->dy; y++) {
1303 i = y * bytesperline(im->r, t->depth);
1304 for(x = 0; x < t->dx; x++, n += 3) {
1305 if(n >= im->chanlen) {
1306 werrstr("palette overflow");
1309 pix = (data[i] >> t->depth*((xmask -
1310 x) & xmask)) & pmask;
1311 if(((x + 1) & xmask) == 0)
1313 im->chans[0][n] = b[pix];
1314 im->chans[0][n+1] = g[pix];
1315 im->chans[0][n+2] = r[pix];
1322 parsefield(Tif *t, Fld *f)
1337 t->depth += f->val[1] + f->val[2];
1347 t->decode = greydecode;
1350 t->decode = rgbdecode;
1353 t->decode = paldecode;
1361 t->nstrips = f->cnt;
1377 t->ncounts = f->cnt;
1396 werrstr("shouldn't reach");
1403 readfield(Tif *t, Fld *f)
1407 ulong (*readval)(Tif *);
1409 f->tag = readshort(t);
1410 f->typ = readshort(t);
1411 f->cnt = readlong(t);
1427 werrstr("field count");
1432 if(f->cnt != 1 && f->cnt != 3) {
1433 werrstr("field count");
1450 readval = readshort;
1456 werrstr("unsupported type\n");
1459 if((f->val = malloc(f->cnt*sizeof *f->val)) == nil)
1461 size = typesizes[f->typ];
1462 if((n = size*f->cnt) <= 4) {
1463 for(i = 0; i < f->cnt; i++)
1464 f->val[i] = readval(t);
1467 for(j = n; j < 4; j += size)
1470 f->off = readlong(t);
1472 if(gototif(t, f->off) < 0)
1474 for(i = 0; i < f->cnt; i++)
1475 f->val[i] = readval(t);
1477 if(gototif(t, off) < 0)
1480 return parsefield(t, f);
1490 werrstr("image width");
1494 werrstr("image length");
1504 werrstr("bits per sample");
1509 t->uncompress = nocomp;
1514 t->uncompress = fax;
1515 if(t->decode != nil)
1516 t->decode = faxdecode;
1517 if((t->comp == T4enc && t->t4 & (1<<1)) ||
1518 (t->comp == T6enc &&
1520 werrstr("uncompressed mode");
1525 t->uncompress = lzw;
1528 t->uncompress = packbits;
1531 werrstr("compression");
1534 if(t->decode == nil) {
1535 werrstr("photometric interpretation");
1538 if(t->depth > 1 && (t->comp == Huffman ||
1539 t->comp == T4enc || t->comp == T6enc)) {
1540 werrstr("compression");
1543 if(t->fill != 1 && t->fill != 2) {
1544 werrstr("fill order");
1547 if(t->fill == 2 && t->depth != 1) {
1548 werrstr("depth should be 1 with fill order 2");
1551 if(t->orientation != 1) {
1552 werrstr("orientation");
1556 werrstr("rows per strip");
1560 b = (double)t->rows;
1561 n = (ulong)floor((a+b-1)/b);
1562 if(t->strips == nil || t->nstrips != n) {
1563 werrstr("strip offsets");
1566 if(t->samples == 1 && t->photo == Rgb) {
1567 werrstr("not enough samples per pixel");
1570 if(t->samples == 3 && t->photo != Rgb) {
1571 werrstr("too many samples per pixel");
1574 if(t->samples != 1 && t->samples != 3) {
1575 werrstr("samples per pixel");
1579 * strip byte counts should not be missing,
1580 * but we can guess correctly in this case
1582 size = sizeof *t->counts;
1583 if(t->counts == nil && t->comp == Nocomp &&
1585 (t->counts = malloc(size)) != nil) {
1586 n = t->dx * t->dy * t->depth;
1587 t->counts[0] = n%8 == 0? n/8: n/8+1;
1588 t->ncounts = t->nstrips;
1590 if(t->counts == nil || t->ncounts != t->nstrips) {
1591 werrstr("strip byte counts");
1594 if(t->planar != 1) {
1595 werrstr("planar configuration");
1598 if(t->photo == Palette && (t->color == nil ||
1599 t->ncolor != 3*(1<<t->depth))) {
1600 werrstr("color map");
1603 if(t->predictor == 2 && t->depth == 1) {
1604 werrstr("depth too low for predictor 2");
1617 for(i = 0; i < t->nstrips; i++)
1618 t->ndata += t->counts[i];
1619 if((t->data = malloc(t->ndata*sizeof *t->data)) == nil)
1622 for(i = n = 0; i < t->nstrips; i++) {
1623 if(gototif(t, t->strips[i]) < 0)
1626 * we store each strip's offset in t->data
1627 * in order to skip the final rtc or eofb
1628 * during fax decoding. t->strips is used
1629 * to save on memory allocation. these
1630 * offsets are also used in lzw as a
1631 * preventive measure.
1634 for(j = 0; j < t->counts[i]; j++, n++)
1635 t->data[n] = readbyte(t);
1637 return gototif(t, off);
1647 if((im = malloc(sizeof *im)) == nil)
1649 im->r = Rect(0, 0, t->dx, t->dy);
1652 im->chanlen = t->dx * t->dy;
1653 if(t->photo == Rgb || t->photo == Palette) {
1654 im->chandesc = CRGB24;
1659 size = im->chanlen * sizeof *im->chans[0];
1660 if((im->chans[0] = malloc(size)) == nil)
1662 /* unused members */
1667 im->gifloopcount = 1;
1668 if((data = (*t->uncompress)(t)) == nil)
1670 if((*t->decode)(t, im, data) < 0) {
1684 for(i = 0; i < t->nfld; i++) {
1685 if(t->fld[i].val != nil)
1686 free(t->fld[i].val);
1697 if(readheader(t) < 0)
1699 if((t->nfld = readshort(t)) <= 0) {
1700 werrstr("illegal field number: %#.4x", t->nfld);
1703 if((t->fld = malloc(t->nfld*sizeof *t->fld)) == nil)
1705 for(i = 0; i < t->nfld; i++) {
1706 if(readfield(t, &t->fld[i]) < 0) {
1707 if(t->fld[i].val != nil)
1708 free(t->fld[i].val);
1713 for(j = 0; j < i; j++) {
1714 if(t->fld[j].val != nil)
1715 free(t->fld[j].val);
1721 if(checkfields(t) < 0) {
1725 if(readstrips(t) < 0) {
1740 Rawimage **array, *r;
1743 if((t = malloc(sizeof *t)) == nil)
1745 if((array = malloc(2*sizeof *array)) == nil)
1749 t->nbuf = t->eof = t->n = 0;
1750 /* order doesn't matter for the first two bytes */
1758 t->uncompress = nil;
1766 t->rows = 0xffffffff; /* entire image is one strip */
1788 if(Binit(&b, fd, OREAD) < 0)