1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
// work in progress... this version only good enough to read
// non-interleaved, 24bit RGB images
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <bio.h>
#include <flate.h>
#include <draw.h>
#include "imagefile.h"
int debug;
enum{ IDATSIZE=1000000,
/* filtering algorithms, supposedly increase compression */
FilterNone = 0, /* new[x][y] = buf[x][y] */
FilterSub = 1, /* new[x][y] = buf[x][y] + new[x-1][y] */
FilterUp = 2, /* new[x][y] = buf[x][y] + new[x][y-1] */
FilterAvg = 3, /* new[x][y] = buf[x][y] + (new[x-1][y]+new[x][y-1])/2 */
FilterPaeth= 4, /* new[x][y] = buf[x][y] + paeth(new[x-1][y],new[x][y-1],new[x-1][y-1]) */
FilterLast = 5,
PropertyBit = 1<<5,
};
typedef struct ZlibR{
Biobuf *bi;
uchar *buf;
uchar *b; // next byte to decompress
uchar *e; // past end of buf
} ZlibR;
typedef struct ZlibW{
uchar *r, *g, *b; // Rawimage channels
int chan; // next channel to write
int col; // column index of current pixel
// -1 = one-byte pseudo-column for filter spec
int row; // row index of current pixel
int ncol, nrow; // image width, height
int filter; // algorithm for current scanline
} ZlibW;
static ulong *crctab;
static uchar PNGmagic[] = {137,80,78,71,13,10,26,10};
static char memerr[] = "ReadPNG: malloc failed: %r";
static ulong
get4(uchar *a)
{
return (a[0]<<24) | (a[1]<<16) | (a[2]<<8) | a[3];
}
static
void
pnginit(void)
{
static int inited;
if(inited)
return;
inited = 1;
crctab = mkcrctab(0xedb88320);
if(crctab == nil)
sysfatal("mkcrctab error");
inflateinit();
}
static
void*
pngmalloc(ulong n, int clear)
{
void *p;
p = malloc(n);
if(p == nil)
sysfatal(memerr);
if(clear)
memset(p, 0, n);
return p;
}
static int
getchunk(Biobuf *b, char *type, uchar *d, int m)
{
uchar buf[8];
ulong crc = 0, crc2;
int n, nr;
if(Bread(b, buf, 8) != 8)
return -1;
n = get4(buf);
memmove(type, buf+4, 4);
type[4] = 0;
if(n > m)
sysfatal("getchunk needed %d, had %d", n, m);
nr = Bread(b, d, n);
if(nr != n)
sysfatal("getchunk read %d, expected %d", nr, n);
crc = blockcrc(crctab, crc, type, 4);
crc = blockcrc(crctab, crc, d, n);
if(Bread(b, buf, 4) != 4)
sysfatal("getchunk tlr failed");
crc2 = get4(buf);
if(crc != crc2)
sysfatal("getchunk crc failed");
return n;
}
static int
zread(void *va)
{
ZlibR *z = va;
char type[5];
int n;
if(z->b >= z->e){
refill_buffer:
z->b = z->buf;
n = getchunk(z->bi, type, z->b, IDATSIZE);
if(n < 0 || strcmp(type, "IEND") == 0)
return -1;
z->e = z->b + n;
if(type[0] & PropertyBit)
goto refill_buffer; /* skip auxiliary chunks for now */
if(strcmp(type,"IDAT") != 0)
sysfatal("unrecognized mandatory chunk %s", type);
}
return *z->b++;
}
static uchar
paeth(uchar a, uchar b, uchar c)
{
int p, pa, pb, pc;
p = (int)a + (int)b - (int)c;
pa = abs(p - (int)a);
pb = abs(p - (int)b);
pc = abs(p - (int)c);
if(pa <= pb && pa <= pc)
return a;
else if(pb <= pc)
return b;
return c;
}
static void
unfilter(int alg, uchar *buf, uchar *ebuf, int up)
{
switch(alg){
case FilterSub:
while (++buf < ebuf)
*buf += buf[-1];
break;
case FilterUp:
if (up != 0)
do
*buf += buf[up];
while (++buf < ebuf);
break;
case FilterAvg:
if (up == 0)
while (++buf < ebuf)
*buf += buf[-1]/2;
else{
*buf += buf[up]/2;
while (++buf < ebuf)
*buf += (buf[-1]+buf[up])/2;
}
break;
case FilterPaeth:
if (up == 0)
while (++buf < ebuf)
*buf += buf[-1];
else{
*buf += paeth(0, buf[up], 0);
while (++buf < ebuf)
*buf += paeth(buf[-1], buf[up], buf[up-1]);
}
break;
}
}
static int
zwrite(void *va, void *vb, int n)
{
ZlibW *z = va;
uchar *buf = vb;
int i, up;
for(i=0; i<n; i++){
if(z->col == -1){
// set filter byte
z->filter = *buf++;
if (z->filter >= FilterLast)
sysfatal("unknown filter algorithm %d for row %d", z->row, z->filter);
z->col++;
continue;
}
switch(z->chan){
case 0:
*z->r++ = *buf++;
z->chan = 1;
break;
case 1:
*z->g++ = *buf++;
z->chan = 2;
break;
case 2:
*z->b++ = *buf++;
z->chan = 0;
z->col++;
if(z->col == z->ncol){
if (z->filter){
if(z->row == 0)
up = 0;
else
up = -z->ncol;
unfilter(z->filter, z->r - z->col, z->r, up);
unfilter(z->filter, z->g - z->col, z->g, up);
unfilter(z->filter, z->b - z->col, z->b, up);
}
z->col = -1;
z->row++;
if((z->row >= z->nrow) && (i < n-1) )
sysfatal("header said %d rows; data goes further", z->nrow);
}
break;
}
}
return n;
}
static Rawimage*
readslave(Biobuf *b)
{
ZlibR zr;
ZlibW zw;
Rawimage *image;
char type[5];
uchar *buf, *h;
int k, n, nrow, ncol, err;
buf = pngmalloc(IDATSIZE, 0);
Bread(b, buf, sizeof PNGmagic);
if(memcmp(PNGmagic, buf, sizeof PNGmagic) != 0)
sysfatal("bad PNGmagic");
n = getchunk(b, type, buf, IDATSIZE);
if(n < 13 || strcmp(type,"IHDR") != 0)
sysfatal("missing IHDR chunk");
h = buf;
ncol = get4(h); h += 4;
nrow = get4(h); h += 4;
if(ncol <= 0 || nrow <= 0)
sysfatal("impossible image size nrow=%d ncol=%d", nrow, ncol);
if(debug)
fprint(2, "readpng nrow=%d ncol=%d\n", nrow, ncol);
if(*h++ != 8)
sysfatal("only 24 bit per pixel supported for now [%d]", h[-1]);
if(*h++ != 2)
sysfatal("only rgb supported for now [%d]", h[-1]);
if(*h++ != 0)
sysfatal("only deflate supported for now [%d]", h[-1]);
if(*h++ != FilterNone)
sysfatal("only FilterNone supported for now [%d]", h[-1]);
if(*h != 0)
sysfatal("only non-interlaced supported for now [%d]", h[-1]);
image = pngmalloc(sizeof(Rawimage), 1);
image->r = Rect(0, 0, ncol, nrow);
image->cmap = nil;
image->cmaplen = 0;
image->chanlen = ncol*nrow;
image->fields = 0;
image->gifflags = 0;
image->gifdelay = 0;
image->giftrindex = 0;
image->chandesc = CRGB;
image->nchans = 3;
for(k=0; k<3; k++)
image->chans[k] = pngmalloc(ncol*nrow, 0);
zr.bi = b;
zr.buf = buf;
zr.b = zr.e = buf + IDATSIZE;
zw.r = image->chans[0];
zw.g = image->chans[1];
zw.b = image->chans[2];
zw.chan = 0;
zw.col = -1;
zw.row = 0;
zw.ncol = ncol;
zw.nrow = nrow;
err = inflatezlib(&zw, zwrite, &zr, zread);
if(err)
sysfatal("inflatezlib %s\n", flateerr(err));
free(buf);
return image;
}
Rawimage**
Breadpng(Biobuf *b, int colorspace)
{
Rawimage *r, **array;
char buf[ERRMAX];
buf[0] = '\0';
if(colorspace != CRGB){
errstr(buf, sizeof buf); /* throw it away */
werrstr("ReadPNG: unknown color space %d", colorspace);
return nil;
}
pnginit();
array = malloc(2*sizeof(*array));
if(array==nil)
return nil;
errstr(buf, sizeof buf); /* throw it away */
r = readslave(b);
array[0] = r;
array[1] = nil;
return array;
}
Rawimage**
readpng(int fd, int colorspace)
{
Rawimage** a;
Biobuf b;
if(Binit(&b, fd, OREAD) < 0)
return nil;
a = Breadpng(&b, colorspace);
Bterm(&b);
return a;
}
|