13 IDATSIZE = 8*1024*1024,
15 /* filtering algorithms */
16 FilterNone = 0, /* new[x][y] = buf[x][y] */
17 FilterSub = 1, /* new[x][y] = buf[x][y] + new[x-1][y] */
18 FilterUp = 2, /* new[x][y] = buf[x][y] + new[x][y-1] */
19 FilterAvg = 3, /* new[x][y] = buf[x][y] + (new[x-1][y]+new[x][y-1])/2 */
20 FilterPaeth = 4, /* new[x][y] = buf[x][y] + paeth(new[x-1][y],new[x][y-1],new[x-1][y-1]) */
26 typedef struct ZlibR ZlibR;
27 typedef struct ZlibW ZlibW;
31 uchar *data; /* Rawimage data */
37 uchar *scan; /* new scanline */
38 uchar *lastscan; /* previous scan line */
39 int scanlen; /* scan line length */
40 int scanpos; /* scan position */
42 int dx; /* width of image */
43 int dy; /* height of image */
44 int bpc; /* bits per channel (per pixel) */
45 int y; /* current scan line */
46 int pass; /* adam7 pass#; 0 means no adam7 */
47 uchar palette[3*256]; /* color palette */
48 int palsize; /* number of palette entries */
53 Biobuf *io; /* input buffer */
54 uchar *buf; /* malloc'ed staging buffer */
55 uchar *p; /* next byte to decompress */
56 uchar *e; /* end of buffer */
61 static uchar PNGmagic[] = { 137, 'P', 'N', 'G', '\r', '\n', 26, '\n'};
66 return (a[0]<<24) | (a[1]<<16) | (a[2]<<8) | a[3];
78 crctab = mkcrctab(0xedb88320);
80 sysfatal("mkcrctab error");
86 pngmalloc(ulong n, int clear)
90 p = mallocz(n, clear);
92 sysfatal("malloc: %r");
97 getchunk(Biobuf *b, char *type, uchar *d, int m)
103 if(Breadn(b, buf, 8) != 8)
106 memmove(type, buf+4, 4);
109 sysfatal("getchunk needed %d, had %d", n, m);
110 nr = Breadn(b, d, n);
112 sysfatal("getchunk read %d, expected %d", nr, n);
113 crc = blockcrc(crctab, crc, type, 4);
114 crc = blockcrc(crctab, crc, d, n);
115 if(Breadn(b, buf, 4) != 4)
116 sysfatal("getchunk tlr failed");
119 sysfatal("getchunk crc failed");
134 n = getchunk(z->io, type, z->p, IDATSIZE);
135 if(n < 0 || strcmp(type, "IEND") == 0)
138 if(!strcmp(type,"PLTE")){
139 if(n < 3 || n > 3*256 || n%3)
140 sysfatal("invalid PLTE chunk len %d", n);
141 memcpy(z->w->palette, z->p, n);
145 if(type[0] & PropertyBit)
146 goto Again; /* skip auxiliary chunks fornow */
147 if(strcmp(type,"IDAT")){
148 sysfatal("unrecognized mandatory chunk %s", type);
156 paeth(uchar a, uchar b, uchar c)
165 if(pa <= pb && pa <= pc)
173 unfilter(int alg, uchar *buf, uchar *up, int len, int bypp)
182 for(i = bypp; i < len; ++i)
183 buf[i] += buf[i-bypp];
187 for(i = 0; i < len; ++i)
192 for(i = 0; i < bypp; ++i)
193 buf[i] += (0+up[i])/2;
195 buf[i] += (buf[i-bypp]+up[i])/2;
199 for(i = 0; i < bypp; ++i)
200 buf[i] += paeth(0, up[i], 0);
202 buf[i] += paeth(buf[i-bypp], up[i], up[i-bypp]);
206 sysfatal("unknown filtering scheme %d\n", alg);
216 {0,0,1,1}, /* eve alone */
217 {0,0,8,8}, /* pass 1 */
218 {4,0,8,8}, /* pass 2 */
219 {0,4,4,8}, /* pass 3 */
220 {2,0,4,4}, /* pass 4 */
221 {0,2,2,4}, /* pass 5 */
222 {1,0,2,2}, /* pass 6 */
223 {0,1,1,2}, /* pass 7 */
227 scan(int len, ZlibW *z)
229 int chan, i, j, nbit, off, val;
230 uchar pixel[4], *p, *w;
232 unfilter(z->scan[0], z->scan+1, z->lastscan+1, len-1, (z->nchan*z->bpc+7)/8);
235 * loop over raw bits extracting pixel values and converting to 8-bit
240 off = z->y*z->dx + adam7[z->pass].x;
241 w = z->data + z->noutchan*off;
242 p = z->scan+1; /* skip alg byte */
244 for(i=0; i<len*8; i++){
246 if(p[i>>3] & (1<<(7-(i&7))))
248 if(++nbit == z->bpc){
249 /* finished the value */
250 pixel[chan++] = (val*255)/((1<<z->bpc)-1);
253 if(chan == z->nchan){
254 /* finished the pixel */
255 if(off < z->dx*z->dy){
256 if(z->nchan < 3 && z->palsize){
261 sysfatal("index %d >= palette size %d", j, z->palsize);
262 pixel[3] = pixel[1]; /* alpha */
263 pixel[0] = z->palette[3*j];
264 pixel[1] = z->palette[3*j+1];
265 pixel[2] = z->palette[3*j+2];
269 // print("%.2x%.2x ", pixel[0], pixel[1]);
271 *w++ += (pixel[0]*pixel[1])/255;
274 // print("%.2x%.2x%.2x%.2x ", pixel[0], pixel[1], pixel[2], pixel[3]);
276 *w++ += (pixel[2]*pixel[3])/255;
277 *w++ += (pixel[1]*pixel[3])/255;
278 *w++ += (pixel[0]*pixel[3])/255;
287 w += (adam7[z->pass].dx-1)*z->noutchan;
289 off += adam7[z->pass].dx;
290 if(off >= (z->y+1)*z->dx){
291 /* finished the line */
298 sysfatal("scan line too short");
304 int bits, n, adx, dx;
306 if(adam7[z->pass].y >= z->dy || adam7[z->pass].x >= z->dx)
308 adx = adam7[z->pass].dx;
309 dx = z->dx - adam7[z->pass].x;
314 if(n != 1 + (z->dx - (adam7[z->pass].x+1)) / adam7[z->pass].dx){
315 print("%d/%d != 1+(%d-1)/%d = %d\n",
316 z->dx - adam7[z->pass].x - 1 + adx, adx,
317 z->dx - (adam7[z->pass].x+1), adam7[z->pass].dx,
318 1 + (z->dx - (adam7[z->pass].x+1)) / adam7[z->pass].dx);
320 bits = n*z->bpc*z->nchan;
321 return 1 + (bits+7)/8;
329 memset(z->lastscan, 0, z->scanlen);
331 z->pass = (z->pass+1)%8;
332 z->y = adam7[z->pass].y;
339 zwrite(void *vz, void *vbuf, int n)
354 m = len - z->scanpos;
356 /* save final partial line */
357 memmove(z->scan+z->scanpos, buf, n);
363 memmove(z->scan+z->scanpos, buf, m);
370 z->scan = z->lastscan;
374 z->y += adam7[z->pass].dy;
385 int bpc, colorfmt, dx, dy, err, n, nchan, nout, useadam7;
391 buf = pngmalloc(IDATSIZE, 0);
392 Breadn(b, buf, sizeof PNGmagic);
393 if(memcmp(PNGmagic, buf, sizeof PNGmagic) != 0)
394 sysfatal("bad PNGmagic");
396 n = getchunk(b, type, buf, IDATSIZE);
397 if(n < 13 || strcmp(type,"IHDR") != 0)
398 sysfatal("missing IHDR chunk");
404 if(dx <= 0 || dy <= 0)
405 sysfatal("impossible image size %dx%d", dx, dy);
407 fprint(2, "readpng %dx%d\n", dx, dy);
413 sysfatal("only deflate supported for now [%d]", h[-1]);
414 if(*h++ != FilterNone)
415 sysfatal("only FilterNone supported for now [%d]", h[-1]);
419 image = pngmalloc(sizeof(Rawimage), 1);
420 image->r = Rect(0, 0, dx, dy);
425 image->chandesc = CY;
431 image->chandesc = CRGB24;
435 case 3: /* indexed rgb with PLTE */
437 image->chandesc = CRGB24;
441 case 4: /* grey+alpha */
443 image->chandesc = CYA16;
447 case 6: /* rgb+alpha */
449 image->chandesc = CRGBA32;
454 sysfatal("unsupported color scheme %d", h[-1]);
456 image->chanlen = dx*dy*nout;
457 image->chans[0] = pngmalloc(image->chanlen, 0);
458 memset(image->chans[0], 0, image->chanlen);
460 memset(&zr, 0, sizeof zr);
465 memset(&zw, 0, sizeof zw);
468 zw.data = image->chans[0];
469 zw.ndata = image->chanlen;
470 zw.chandesc = image->chandesc;
475 zw.scanlen = (nchan*dx*bpc+7)/8+1;
476 zw.scan = pngmalloc(zw.scanlen, 1);
477 zw.lastscan = pngmalloc(zw.scanlen, 1);
481 err = inflatezlib(&zw, zwrite, &zr, zread);
484 sysfatal("inflatezlib %s\n", flateerr(err));
493 Breadpng(Biobuf *b, int colorspace)
495 Rawimage **array, *r;
497 if(colorspace != CRGB){
498 werrstr("ReadPNG: unknown color space %d", colorspace);
502 array = malloc(2*sizeof(*array));
512 readpng(int fd, int colorspace)
517 if(Binit(&b, fd, OREAD) < 0)
519 a = Breadpng(&b, colorspace);