@@ -19,7 +19,6 @@ export default class PNGDecoder extends IOBuffer {
19
19
private _end : boolean ;
20
20
private _hasPalette : boolean ;
21
21
private _palette : IndexedColors ;
22
- private _colorType : ColorType ;
23
22
private _compressionMethod : CompressionMethod ;
24
23
private _filterMethod : FilterMethod ;
25
24
private _interlaceMethod : InterlaceMethod ;
@@ -36,7 +35,6 @@ export default class PNGDecoder extends IOBuffer {
36
35
this . _end = false ;
37
36
this . _hasPalette = false ;
38
37
this . _palette = [ ] ;
39
- this . _colorType = ColorType . UNKNOWN ;
40
38
this . _compressionMethod = CompressionMethod . UNKNOWN ;
41
39
this . _filterMethod = FilterMethod . UNKNOWN ;
42
40
this . _interlaceMethod = InterlaceMethod . UNKNOWN ;
@@ -126,15 +124,39 @@ export default class PNGDecoder extends IOBuffer {
126
124
image . height = this . readUint32 ( ) ;
127
125
// @ts -ignore
128
126
image . depth = this . readUint8 ( ) ;
129
- this . _colorType = this . readUint8 ( ) ;
127
+
128
+ const colorType : ColorType = this . readUint8 ( ) ;
129
+ let channels : number ;
130
+ switch ( colorType ) {
131
+ case ColorType . GREYSCALE :
132
+ channels = 1 ;
133
+ break ;
134
+ case ColorType . TRUECOLOUR :
135
+ channels = 3 ;
136
+ break ;
137
+ case ColorType . INDEXED_COLOUR :
138
+ channels = 1 ;
139
+ break ;
140
+ case ColorType . GREYSCALE_ALPHA :
141
+ channels = 2 ;
142
+ break ;
143
+ case ColorType . TRUECOLOUR_ALPHA :
144
+ channels = 4 ;
145
+ break ;
146
+ default :
147
+ throw new Error ( `Unknown color type: ${ colorType } ` ) ;
148
+ }
149
+ this . _png . channels = channels ;
150
+
130
151
this . _compressionMethod = this . readUint8 ( ) ;
131
- this . _filterMethod = this . readUint8 ( ) ;
132
- this . _interlaceMethod = this . readUint8 ( ) ;
133
152
if ( this . _compressionMethod !== CompressionMethod . DEFLATE ) {
134
153
throw new Error (
135
154
`Unsupported compression method: ${ this . _compressionMethod } `
136
155
) ;
137
156
}
157
+
158
+ this . _filterMethod = this . readUint8 ( ) ;
159
+ this . _interlaceMethod = this . readUint8 ( ) ;
138
160
}
139
161
140
162
// https://www.w3.org/TR/PNG/#11PLTE
@@ -203,30 +225,8 @@ export default class PNGDecoder extends IOBuffer {
203
225
}
204
226
205
227
private decodeInterlaceNull ( data : PNGDataArray ) : void {
206
- let channels : number ;
207
- switch ( this . _colorType ) {
208
- case ColorType . GREYSCALE :
209
- channels = 1 ;
210
- break ;
211
- case ColorType . TRUECOLOUR :
212
- channels = 3 ;
213
- break ;
214
- case ColorType . INDEXED_COLOUR :
215
- if ( ! this . _hasPalette ) throw new Error ( 'Missing palette' ) ;
216
- channels = 1 ;
217
- break ;
218
- case ColorType . GREYSCALE_ALPHA :
219
- channels = 2 ;
220
- break ;
221
- case ColorType . TRUECOLOUR_ALPHA :
222
- channels = 4 ;
223
- break ;
224
- default :
225
- throw new Error ( `Unknown color type: ${ this . _colorType } ` ) ;
226
- }
227
-
228
228
const height = this . _png . height ;
229
- const bytesPerPixel = ( channels * this . _png . depth ) / 8 ;
229
+ const bytesPerPixel = ( this . _png . channels * this . _png . depth ) / 8 ;
230
230
const bytesPerLine = this . _png . width * bytesPerPixel ;
231
231
const newData = new Uint8Array ( this . _png . height * bytesPerLine ) ;
232
232
0 commit comments