@@ -2,8 +2,20 @@ import { IOBuffer } from 'iobuffer';
2
2
import { Inflate as Inflator } from 'pako' ;
3
3
4
4
import { pngSignature , crc } from './common' ;
5
- import { IDecodedPNG , DecoderInputType , IPNGDecoderOptions , PNGDataArray , IndexedColors } from './types' ;
6
- import { ColorType , CompressionMethod , FilterMethod , InterlaceMethod } from './internalTypes' ;
5
+ import {
6
+ IDecodedPNG ,
7
+ DecoderInputType ,
8
+ IPNGDecoderOptions ,
9
+ PNGDataArray ,
10
+ IndexedColors ,
11
+ BitDepth ,
12
+ } from './types' ;
13
+ import {
14
+ ColorType ,
15
+ CompressionMethod ,
16
+ FilterMethod ,
17
+ InterlaceMethod ,
18
+ } from './internalTypes' ;
7
19
8
20
const empty = new Uint8Array ( 0 ) ;
9
21
const NULL = '\0' ;
@@ -28,9 +40,13 @@ export default class PNGDecoder extends IOBuffer {
28
40
const { checkCrc = false } = options ;
29
41
this . _checkCrc = checkCrc ;
30
42
this . _inflator = new Inflator ( ) ;
31
- // @ts -ignore
32
43
this . _png = {
33
- text : { }
44
+ width : - 1 ,
45
+ height : - 1 ,
46
+ channels : - 1 ,
47
+ data : new Uint8Array ( 0 ) ,
48
+ depth : 1 ,
49
+ text : { } ,
34
50
} ;
35
51
this . _end = false ;
36
52
this . _hasPalette = false ;
@@ -57,7 +73,7 @@ export default class PNGDecoder extends IOBuffer {
57
73
for ( let i = 0 ; i < pngSignature . length ; i ++ ) {
58
74
if ( this . readUint8 ( ) !== pngSignature [ i ] ) {
59
75
throw new Error (
60
- `wrong PNG signature. Byte at ${ i } should be ${ pngSignature [ i ] } .`
76
+ `wrong PNG signature. Byte at ${ i } should be ${ pngSignature [ i ] } .` ,
61
77
) ;
62
78
}
63
79
}
@@ -103,13 +119,13 @@ export default class PNGDecoder extends IOBuffer {
103
119
new Uint8Array (
104
120
this . buffer ,
105
121
this . byteOffset + this . offset - crcLength - 4 ,
106
- crcLength
122
+ crcLength ,
107
123
) ,
108
- crcLength
124
+ crcLength ,
109
125
) ; // "- 4" because we already advanced by reading the CRC
110
126
if ( actualCrc !== expectedCrc ) {
111
127
throw new Error (
112
- `CRC mismatch for chunk ${ type } . Expected ${ expectedCrc } , found ${ actualCrc } `
128
+ `CRC mismatch for chunk ${ type } . Expected ${ expectedCrc } , found ${ actualCrc } ` ,
113
129
) ;
114
130
}
115
131
} else {
@@ -122,8 +138,7 @@ export default class PNGDecoder extends IOBuffer {
122
138
const image = this . _png ;
123
139
image . width = this . readUint32 ( ) ;
124
140
image . height = this . readUint32 ( ) ;
125
- // @ts -ignore
126
- image . depth = this . readUint8 ( ) ;
141
+ image . depth = checkBitDepth ( this . readUint8 ( ) ) ;
127
142
128
143
const colorType : ColorType = this . readUint8 ( ) ;
129
144
let channels : number ;
@@ -151,7 +166,7 @@ export default class PNGDecoder extends IOBuffer {
151
166
this . _compressionMethod = this . readUint8 ( ) ;
152
167
if ( this . _compressionMethod !== CompressionMethod . DEFLATE ) {
153
168
throw new Error (
154
- `Unsupported compression method: ${ this . _compressionMethod } `
169
+ `Unsupported compression method: ${ this . _compressionMethod } ` ,
155
170
) ;
156
171
}
157
172
@@ -163,7 +178,7 @@ export default class PNGDecoder extends IOBuffer {
163
178
private decodePLTE ( length : number ) : void {
164
179
if ( length % 3 !== 0 ) {
165
180
throw new RangeError (
166
- `PLTE field length must be a multiple of 3. Got ${ length } `
181
+ `PLTE field length must be a multiple of 3. Got ${ length } ` ,
167
182
) ;
168
183
}
169
184
const l = length / 3 ;
@@ -179,7 +194,7 @@ export default class PNGDecoder extends IOBuffer {
179
194
private decodeIDAT ( length : number ) : void {
180
195
this . _inflator . push (
181
196
new Uint8Array ( this . buffer , this . offset + this . byteOffset , length ) ,
182
- false
197
+ false ,
183
198
) ;
184
199
this . skip ( length ) ;
185
200
}
@@ -206,10 +221,10 @@ export default class PNGDecoder extends IOBuffer {
206
221
this . _inflator . push ( empty , true ) ;
207
222
if ( this . _inflator . err ) {
208
223
throw new Error (
209
- `Error while decompressing the data: ${ this . _inflator . err } `
224
+ `Error while decompressing the data: ${ this . _inflator . err } ` ,
210
225
) ;
211
226
}
212
- var data = this . _inflator . result ;
227
+ const data = this . _inflator . result ;
213
228
214
229
if ( this . _filterMethod !== FilterMethod . ADAPTIVE ) {
215
230
throw new Error ( `Filter method ${ this . _filterMethod } not supported` ) ;
@@ -219,7 +234,7 @@ export default class PNGDecoder extends IOBuffer {
219
234
this . decodeInterlaceNull ( data as Uint8Array ) ;
220
235
} else {
221
236
throw new Error (
222
- `Interlace method ${ this . _interlaceMethod } not supported`
237
+ `Interlace method ${ this . _interlaceMethod } not supported` ,
223
238
) ;
224
239
}
225
240
}
@@ -254,7 +269,7 @@ export default class PNGDecoder extends IOBuffer {
254
269
newLine ,
255
270
prevLine ,
256
271
bytesPerLine ,
257
- bytesPerPixel
272
+ bytesPerPixel ,
258
273
) ;
259
274
break ;
260
275
case 4 :
@@ -263,7 +278,7 @@ export default class PNGDecoder extends IOBuffer {
263
278
newLine ,
264
279
prevLine ,
265
280
bytesPerLine ,
266
- bytesPerPixel
281
+ bytesPerPixel ,
267
282
) ;
268
283
break ;
269
284
default :
@@ -295,7 +310,7 @@ export default class PNGDecoder extends IOBuffer {
295
310
function unfilterNone (
296
311
currentLine : PNGDataArray ,
297
312
newLine : PNGDataArray ,
298
- bytesPerLine : number
313
+ bytesPerLine : number ,
299
314
) : void {
300
315
for ( let i = 0 ; i < bytesPerLine ; i ++ ) {
301
316
newLine [ i ] = currentLine [ i ] ;
@@ -306,7 +321,7 @@ function unfilterSub(
306
321
currentLine : PNGDataArray ,
307
322
newLine : PNGDataArray ,
308
323
bytesPerLine : number ,
309
- bytesPerPixel : number
324
+ bytesPerPixel : number ,
310
325
) : void {
311
326
let i = 0 ;
312
327
for ( ; i < bytesPerPixel ; i ++ ) {
@@ -322,7 +337,7 @@ function unfilterUp(
322
337
currentLine : PNGDataArray ,
323
338
newLine : PNGDataArray ,
324
339
prevLine : PNGDataArray ,
325
- bytesPerLine : number
340
+ bytesPerLine : number ,
326
341
) : void {
327
342
let i = 0 ;
328
343
if ( prevLine . length === 0 ) {
@@ -342,7 +357,7 @@ function unfilterAverage(
342
357
newLine : PNGDataArray ,
343
358
prevLine : PNGDataArray ,
344
359
bytesPerLine : number ,
345
- bytesPerPixel : number
360
+ bytesPerPixel : number ,
346
361
) : void {
347
362
let i = 0 ;
348
363
if ( prevLine . length === 0 ) {
@@ -369,7 +384,7 @@ function unfilterPaeth(
369
384
newLine : PNGDataArray ,
370
385
prevLine : PNGDataArray ,
371
386
bytesPerLine : number ,
372
- bytesPerPixel : number
387
+ bytesPerPixel : number ,
373
388
) : void {
374
389
let i = 0 ;
375
390
if ( prevLine . length === 0 ) {
@@ -389,7 +404,7 @@ function unfilterPaeth(
389
404
paethPredictor (
390
405
newLine [ i - bytesPerPixel ] ,
391
406
prevLine [ i ] ,
392
- prevLine [ i - bytesPerPixel ]
407
+ prevLine [ i - bytesPerPixel ] ,
393
408
) ) &
394
409
0xff ;
395
410
}
@@ -409,3 +424,16 @@ function paethPredictor(a: number, b: number, c: number): number {
409
424
function swap16 ( val : number ) : number {
410
425
return ( ( val & 0xff ) << 8 ) | ( ( val >> 8 ) & 0xff ) ;
411
426
}
427
+
428
+ function checkBitDepth ( value : number ) : BitDepth {
429
+ if (
430
+ value !== 1 &&
431
+ value !== 2 &&
432
+ value !== 4 &&
433
+ value !== 8 &&
434
+ value !== 16
435
+ ) {
436
+ throw new Error ( `invalid bit depth: ${ value } ` ) ;
437
+ }
438
+ return value ;
439
+ }
0 commit comments