forked from arian/pngjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PNGReader.js
426 lines (359 loc) · 10.5 KB
/
PNGReader.js
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*global Uint8Array:true ArrayBuffer:true */
"use strict";
var zlib = require('zlib');
var PNG = require('./PNG');
var inflate = function(data, callback){
return zlib.inflate(new Buffer(data), callback);
};
var slice = Array.prototype.slice;
var toString = Object.prototype.toString;
function equalBytes(a, b){
if (a.length != b.length) return false;
for (var l = a.length; l--;) if (a[l] != b[l]) return false;
return true;
}
function readUInt32(buffer, offset){
return (buffer[offset] << 24) +
(buffer[offset + 1] << 16) +
(buffer[offset + 2] << 8) +
(buffer[offset + 3] << 0);
}
function readUInt16(buffer, offset){
return (buffer[offset + 1] << 8) + (buffer[offset] << 0);
}
function readUInt8(buffer, offset){
return buffer[offset] << 0;
}
function bufferToString(buffer){
var str = '';
for (var i = 0; i < buffer.length; i++){
str += String.fromCharCode(buffer[i]);
}
return str;
}
var PNGReader = function(bytes){
if (typeof bytes == 'string'){
var bts = bytes;
bytes = new Array(bts.length);
for (var i = 0, l = bts.length; i < l; i++){
bytes[i] = bts[i].charCodeAt(0);
}
} else {
var type = toString.call(bytes).slice(8, -1);
if (type == 'ArrayBuffer') bytes = new Uint8Array(bytes);
}
// current pointer
this.i = 0;
// bytes buffer
this.bytes = bytes;
// Output object
this.png = new PNG();
this.dataChunks = [];
};
PNGReader.prototype.readBytes = function(length){
var end = this.i + length;
if (end > this.bytes.length){
throw new Error('Unexpectedly reached end of file');
}
var bytes = slice.call(this.bytes, this.i, end);
this.i = end;
return bytes;
};
/**
* http://www.w3.org/TR/2003/REC-PNG-20031110/#5PNG-file-signature
*/
PNGReader.prototype.decodeHeader = function(){
if (this.i !== 0){
throw new Error('file pointer should be at 0 to read the header');
}
var header = this.readBytes(8);
if (!equalBytes(header, [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A])){
throw new Error('invalid PNGReader file (bad signature)');
}
this.header = header;
};
/**
* http://www.w3.org/TR/2003/REC-PNG-20031110/#5Chunk-layout
*
* length = 4 bytes
* type = 4 bytes (IHDR, PLTE, IDAT, IEND or others)
* chunk = length bytes
* crc = 4 bytes
*/
PNGReader.prototype.decodeChunk = function(){
var length = readUInt32(this.readBytes(4), 0);
if (length < 0){
throw new Error('Bad chunk length ' + (0xFFFFFFFF & length));
}
var type = bufferToString(this.readBytes(4));
var chunk = this.readBytes(length);
var crc = this.readBytes(4);
switch (type){
case 'IHDR': this.decodeIHDR(chunk); break;
case 'PLTE': this.decodePLTE(chunk); break;
case 'IDAT': this.decodeIDAT(chunk); break;
case 'tRNS': this.decodeTRNS(chunk); break;
case 'IEND': this.decodeIEND(chunk); break;
}
return type;
};
/**
* http://www.w3.org/TR/2003/REC-PNG-20031110/#11IHDR
* http://www.libpng.org/pub/png/spec/1.2/png-1.2-pdg.html#C.IHDR
*
* Width 4 bytes
* Height 4 bytes
* Bit depth 1 byte
* Colour type 1 byte
* Compression method 1 byte
* Filter method 1 byte
* Interlace method 1 byte
*/
PNGReader.prototype.decodeIHDR = function(chunk){
var png = this.png;
png.setWidth( readUInt32(chunk, 0));
png.setHeight( readUInt32(chunk, 4));
png.setBitDepth( readUInt8(chunk, 8));
png.setColorType( readUInt8(chunk, 9));
png.setCompressionMethod( readUInt8(chunk, 10));
png.setFilterMethod( readUInt8(chunk, 11));
png.setInterlaceMethod( readUInt8(chunk, 12));
};
/**
*
* http://www.w3.org/TR/PNG/#11PLTE
*/
PNGReader.prototype.decodePLTE = function(chunk){
this.png.setPalette(chunk);
};
/**
* http://www.w3.org/TR/2003/REC-PNG-20031110/#11IDAT
*/
PNGReader.prototype.decodeIDAT = function(chunk){
// multiple IDAT chunks will concatenated
this.dataChunks.push(chunk);
};
/**
* https://www.w3.org/TR/PNG/#11tRNS
*/
PNGReader.prototype.decodeTRNS = function(chunk) {
this.png.setTRNS(chunk);
};
/**
* http://www.w3.org/TR/2003/REC-PNG-20031110/#11IEND
*/
PNGReader.prototype.decodeIEND = function(){
};
/**
* Uncompress IDAT chunks
*/
PNGReader.prototype.decodePixels = function(callback){
var png = this.png;
var reader = this;
var length = 0;
var i, j, k, l;
for (l = this.dataChunks.length; l--;) length += this.dataChunks[l].length;
var data = new Buffer(length);
for (i = 0, k = 0, l = this.dataChunks.length; i < l; i++){
var chunk = this.dataChunks[i];
for (j = 0; j < chunk.length; j++) data[k++] = chunk[j];
}
inflate(data, function(err, data){
if (err) return callback(err);
try {
if (png.getInterlaceMethod() === 0){
reader.interlaceNone(data);
} else {
reader.interlaceAdam7(data);
}
} catch (e){
return callback(e);
}
callback();
});
};
// Different interlace methods
PNGReader.prototype.interlaceNone = function(data){
var png = this.png;
// bytes per pixel
var bpp = Math.max(1, png.colors * png.bitDepth / 8);
// color bytes per row
var cpr = bpp * png.width;
var pixels = new Buffer(bpp * png.width * png.height);
var scanline;
var offset = 0;
for (var i = 0; i < data.length; i += cpr + 1){
scanline = slice.call(data, i + 1, i + cpr + 1);
switch (readUInt8(data, i)){
case 0: this.unFilterNone( scanline, pixels, bpp, offset, cpr); break;
case 1: this.unFilterSub( scanline, pixels, bpp, offset, cpr); break;
case 2: this.unFilterUp( scanline, pixels, bpp, offset, cpr); break;
case 3: this.unFilterAverage(scanline, pixels, bpp, offset, cpr); break;
case 4: this.unFilterPaeth( scanline, pixels, bpp, offset, cpr); break;
default: throw new Error("unkown filtered scanline");
}
offset += cpr;
}
png.pixels = pixels;
};
PNGReader.prototype.interlaceAdam7 = function(data){
throw new Error("Adam7 interlacing is not implemented yet");
};
// Unfiltering
/**
* No filtering, direct copy
*/
PNGReader.prototype.unFilterNone = function(scanline, pixels, bpp, of, length){
for (var i = 0, to = length; i < to; i++){
pixels[of + i] = scanline[i];
}
};
/**
* The Sub() filter transmits the difference between each byte and the value
* of the corresponding byte of the prior pixel.
* Sub(x) = Raw(x) + Raw(x - bpp)
*/
PNGReader.prototype.unFilterSub = function(scanline, pixels, bpp, of, length){
var i = 0;
for (; i < bpp; i++) pixels[of + i] = scanline[i];
for (; i < length; i++){
// Raw(x) + Raw(x - bpp)
pixels[of + i] = (scanline[i] + pixels[of + i - bpp]) & 0xFF;
}
};
/**
* The Up() filter is just like the Sub() filter except that the pixel
* immediately above the current pixel, rather than just to its left, is used
* as the predictor.
* Up(x) = Raw(x) + Prior(x)
*/
PNGReader.prototype.unFilterUp = function(scanline, pixels, bpp, of, length){
var i = 0, byte, prev;
// Prior(x) is 0 for all x on the first scanline
if ((of - length) < 0) for (; i < length; i++){
pixels[of + i] = scanline[i];
} else for (; i < length; i++){
// Raw(x)
byte = scanline[i];
// Prior(x)
prev = pixels[of + i - length];
pixels[of + i] = (byte + prev) & 0xFF;
}
};
/**
* The Average() filter uses the average of the two neighboring pixels (left
* and above) to predict the value of a pixel.
* Average(x) = Raw(x) + floor((Raw(x-bpp)+Prior(x))/2)
*/
PNGReader.prototype.unFilterAverage = function(scanline, pixels, bpp, of, length){
var i = 0, byte, prev, prior;
if ((of - length) < 0){
// Prior(x) == 0 && Raw(x - bpp) == 0
for (; i < bpp; i++){
pixels[of + i] = scanline[i];
}
// Prior(x) == 0 && Raw(x - bpp) != 0 (right shift, prevent doubles)
for (; i < length; i++){
pixels[of + i] = (scanline[i] + (pixels[of + i - bpp] >> 1)) & 0xFF;
}
} else {
// Prior(x) != 0 && Raw(x - bpp) == 0
for (; i < bpp; i++){
pixels[of + i] = (scanline[i] + (pixels[of - length + i] >> 1)) & 0xFF;
}
// Prior(x) != 0 && Raw(x - bpp) != 0
for (; i < length; i++){
byte = scanline[i];
prev = pixels[of + i - bpp];
prior = pixels[of + i - length];
pixels[of + i] = (byte + (prev + prior >> 1)) & 0xFF;
}
}
};
/**
* The Paeth() filter computes a simple linear function of the three
* neighboring pixels (left, above, upper left), then chooses as predictor
* the neighboring pixel closest to the computed value. This technique is due
* to Alan W. Paeth.
* Paeth(x) = Raw(x) +
* PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
* function PaethPredictor (a, b, c)
* begin
* ; a = left, b = above, c = upper left
* p := a + b - c ; initial estimate
* pa := abs(p - a) ; distances to a, b, c
* pb := abs(p - b)
* pc := abs(p - c)
* ; return nearest of a,b,c,
* ; breaking ties in order a,b,c.
* if pa <= pb AND pa <= pc then return a
* else if pb <= pc then return b
* else return c
* end
*/
PNGReader.prototype.unFilterPaeth = function(scanline, pixels, bpp, of, length){
var i = 0, raw, a, b, c, p, pa, pb, pc, pr;
if ((of - length) < 0){
// Prior(x) == 0 && Raw(x - bpp) == 0
for (; i < bpp; i++){
pixels[of + i] = scanline[i];
}
// Prior(x) == 0 && Raw(x - bpp) != 0
// paethPredictor(x, 0, 0) is always x
for (; i < length; i++){
pixels[of + i] = (scanline[i] + pixels[of + i - bpp]) & 0xFF;
}
} else {
// Prior(x) != 0 && Raw(x - bpp) == 0
// paethPredictor(x, 0, 0) is always x
for (; i < bpp; i++){
pixels[of + i] = (scanline[i] + pixels[of + i - length]) & 0xFF;
}
// Prior(x) != 0 && Raw(x - bpp) != 0
for (; i < length; i++){
raw = scanline[i];
a = pixels[of + i - bpp];
b = pixels[of + i - length];
c = pixels[of + i - length - bpp];
p = a + b - c;
pa = Math.abs(p - a);
pb = Math.abs(p - b);
pc = Math.abs(p - c);
if (pa <= pb && pa <= pc) pr = a;
else if (pb <= pc) pr = b;
else pr = c;
pixels[of + i] = (raw + pr) & 0xFF;
}
}
};
/**
* Parse the PNG file
*
* reader.parse(options, callback)
* OR
* reader.parse(callback)
*
* OPTIONS:
* option | type | default
* ----------------------------
* data boolean true should it read the pixel data
*/
PNGReader.prototype.parse = function(options, callback){
if (typeof options == 'function') callback = options;
if (typeof options != 'object') options = {};
try {
this.decodeHeader();
while (this.i < this.bytes.length){
var type = this.decodeChunk();
// stop after IHDR chunk, or after IEND
if (type == 'IHDR' && options.data === false || type == 'IEND') break;
}
var png = this.png;
this.decodePixels(function(err){
callback(err, png);
});
} catch (e){
callback(e);
}
};
module.exports = PNGReader;