Blob


1 // based on PNG 1.2 specification, July 1999 (see also rfc2083)
2 // Alpha is not supported yet because of lack of industry acceptance and
3 // because Plan9 Image uses premultiplied alpha, so png can't be lossless.
4 // Only 24bit color supported, because 8bit may as well use GIF.
6 #include <u.h>
7 #include <libc.h>
8 #include <draw.h>
9 #include <memdraw.h>
10 #include <ctype.h>
11 #include <bio.h>
12 #include <flate.h>
13 #include "imagefile.h"
15 enum{ IDATSIZE = 20000,
16 FilterNone = 0,
17 };
19 typedef struct ZlibR{
20 uchar *data;
21 int width;
22 int nrow, ncol;
23 int row, col; // next pixel to send
24 } ZlibR;
26 typedef struct ZlibW{
27 Biobuf *bo;
28 uchar *buf;
29 uchar *b; // next place to write
30 uchar *e; // past end of buf
31 } ZlibW;
33 static ulong *crctab;
34 static uchar PNGmagic[] = {137,80,78,71,13,10,26,10};
36 static void
37 put4(uchar *a, ulong v)
38 {
39 a[0] = v>>24;
40 a[1] = v>>16;
41 a[2] = v>>8;
42 a[3] = v;
43 }
45 static void
46 chunk(Biobuf *bo, char *type, uchar *d, int n)
47 {
48 uchar buf[4];
49 ulong crc = 0;
51 if(strlen(type) != 4)
52 return;
53 put4(buf, n);
54 Bwrite(bo, buf, 4);
55 Bwrite(bo, type, 4);
56 Bwrite(bo, d, n);
57 crc = blockcrc(crctab, crc, type, 4);
58 crc = blockcrc(crctab, crc, d, n);
59 put4(buf, crc);
60 Bwrite(bo, buf, 4);
61 }
63 static int
64 zread(void *va, void *buf, int n)
65 {
66 ZlibR *z = va;
67 int nrow = z->nrow;
68 int ncol = z->ncol;
69 uchar *b = buf, *e = b+n, *img;
70 int i, pixels; // number of pixels in row that can be sent now
72 while(b+3 <= e){ // loop over image rows
73 if(z->row >= nrow)
74 break;
75 if(z->col==0)
76 *b++ = FilterNone;
77 pixels = (e-b)/3;
78 if(pixels > ncol - z->col)
79 pixels = ncol - z->col;
80 img = z->data + z->width * z->row + 3 * z->col;
82 // Plan 9 image format is BGR?!!!
83 // memmove(b, img, 3*pixels);
84 // b += 3*pixels;
85 for(i=0; i<pixels; i++, img += 3){
86 *b++ = img[2];
87 *b++ = img[1];
88 *b++ = img[0];
89 }
91 z->col += pixels;
92 if(z->col >= ncol){
93 z->col = 0;
94 z->row++;
95 }
96 }
97 return b - (uchar*)buf;
98 }
100 static void
101 IDAT(ZlibW *z)
103 chunk(z->bo, "IDAT", z->buf, z->b - z->buf);
104 z->b = z->buf;
107 static int
108 zwrite(void *va, void *buf, int n)
110 ZlibW *z = va;
111 uchar *b = buf, *e = b+n;
112 int m;
114 while(b < e){ // loop over IDAT chunks
115 m = z->e - z->b;
116 if(m > e - b)
117 m = e - b;
118 memmove(z->b, b, m);
119 z->b += m;
120 b += m;
121 if(z->b >= z->e)
122 IDAT(z);
124 return n;
127 static Memimage*
128 memRGB(Memimage *i)
130 Memimage *ni;
132 if(i->chan == RGB24)
133 return i;
135 ni = allocmemimage(i->r, RGB24);
136 if(ni == nil)
137 return ni;
138 memimagedraw(ni, ni->r, i, i->r.min, nil, i->r.min, S);
139 return ni;
142 char*
143 memwritepng(Biobuf *bo, Memimage *r, ImageInfo *II)
145 uchar buf[200], *h;
146 ulong vgamma;
147 int err, n;
148 ZlibR zr;
149 ZlibW zw;
150 int nrow = r->r.max.y - r->r.min.y;
151 int ncol = r->r.max.x - r->r.min.x;
152 Tm *tm;
153 Memimage *rgb;
155 rgb = memRGB(r);
156 if(rgb == nil)
157 return "allocmemimage nil";
158 crctab = mkcrctab(0xedb88320);
159 if(crctab == nil)
160 sysfatal("mkcrctab error");
161 deflateinit();
163 Bwrite(bo, PNGmagic, sizeof PNGmagic);
164 // IHDR chunk
165 h = buf;
166 put4(h, ncol); h += 4;
167 put4(h, nrow); h += 4;
168 *h++ = 8; // bit depth = 24 bit per pixel
169 *h++ = 2; // color type = rgb
170 *h++ = 0; // compression method = deflate
171 *h++ = 0; // filter method
172 *h++ = 0; // interlace method = no interlace
173 chunk(bo, "IHDR", buf, h-buf);
175 tm = gmtime(time(0));
176 h = buf;
177 *h++ = (tm->year + 1900)>>8;
178 *h++ = (tm->year + 1900)&0xff;
179 *h++ = tm->mon + 1;
180 *h++ = tm->mday;
181 *h++ = tm->hour;
182 *h++ = tm->min;
183 *h++ = tm->sec;
184 chunk(bo, "tIME", buf, h-buf);
186 if(II->fields_set & II_GAMMA){
187 vgamma = II->gamma*100000;
188 put4(buf, vgamma);
189 chunk(bo, "gAMA", buf, 4);
192 if(II->fields_set & II_COMMENT){
193 strncpy((char*)buf, "Comment", sizeof buf);
194 n = strlen((char*)buf)+1; // leave null between Comment and text
195 strncpy((char*)(buf+n), II->comment, sizeof buf - n);
196 chunk(bo, "tEXt", buf, n+strlen((char*)buf+n));
199 // image chunks
200 zr.nrow = nrow;
201 zr.ncol = ncol;
202 zr.width = rgb->width * sizeof(ulong);
203 zr.data = rgb->data->bdata;
204 zr.row = zr.col = 0;
205 zw.bo = bo;
206 zw.buf = malloc(IDATSIZE);
207 zw.b = zw.buf;
208 zw.e = zw.b + IDATSIZE;
209 err = deflatezlib(&zw, zwrite, &zr, zread, 6, 0);
210 if(zw.b > zw.buf)
211 IDAT(&zw);
212 free(zw.buf);
213 if(err)
214 sysfatal("deflatezlib %s\n", flateerr(err));
215 chunk(bo, "IEND", nil, 0);
217 if(r != rgb)
218 freememimage(rgb);
219 return nil;