@@ -34,6 +34,7 @@ export default class PNGDecoder extends IOBuffer {
34
34
private _compressionMethod : CompressionMethod ;
35
35
private _filterMethod : FilterMethod ;
36
36
private _interlaceMethod : InterlaceMethod ;
37
+ private _colorType : number ;
37
38
38
39
public constructor ( data : DecoderInputType , options : IPNGDecoderOptions = { } ) {
39
40
super ( data ) ;
@@ -54,6 +55,7 @@ export default class PNGDecoder extends IOBuffer {
54
55
this . _compressionMethod = CompressionMethod . UNKNOWN ;
55
56
this . _filterMethod = FilterMethod . UNKNOWN ;
56
57
this . _interlaceMethod = InterlaceMethod . UNKNOWN ;
58
+ this . _colorType = - 1 ;
57
59
// PNG is always big endian
58
60
// https://www.w3.org/TR/PNG/#7Integers-and-byte-order
59
61
this . setBigEndian ( ) ;
@@ -99,6 +101,9 @@ export default class PNGDecoder extends IOBuffer {
99
101
this . _end = true ;
100
102
break ;
101
103
// 11.3 Ancillary chunks
104
+ case 'tRNS' : // 11.3.2.1 tRNS Transparency
105
+ this . decodetRNS ( length ) ;
106
+ break ;
102
107
case 'tEXt' : // 11.3.4.3 tEXt Textual data
103
108
this . decodetEXt ( length ) ;
104
109
break ;
@@ -141,6 +146,7 @@ export default class PNGDecoder extends IOBuffer {
141
146
image . depth = checkBitDepth ( this . readUint8 ( ) ) ;
142
147
143
148
const colorType : ColorType = this . readUint8 ( ) ;
149
+ this . _colorType = colorType ;
144
150
let channels : number ;
145
151
switch ( colorType ) {
146
152
case ColorType . GREYSCALE :
@@ -199,6 +205,26 @@ export default class PNGDecoder extends IOBuffer {
199
205
this . skip ( length ) ;
200
206
}
201
207
208
+ // https://www.w3.org/TR/PNG/#11tRNS
209
+ private decodetRNS ( length : number ) : void {
210
+ // TODO: support other color types.
211
+ if ( this . _colorType === 3 ) {
212
+ if ( length > this . _palette . length ) {
213
+ throw new Error (
214
+ `tRNS chunk contains more alpha values than there are palette colors (${ length } vs ${ this . _palette . length } )` ,
215
+ ) ;
216
+ }
217
+ let i = 0 ;
218
+ for ( ; i < length ; i ++ ) {
219
+ const alpha = this . readByte ( ) ;
220
+ this . _palette [ i ] . push ( alpha ) ;
221
+ }
222
+ for ( ; i < this . _palette . length ; i ++ ) {
223
+ this . _palette [ i ] . push ( 255 ) ;
224
+ }
225
+ }
226
+ }
227
+
202
228
// https://www.w3.org/TR/PNG/#11tEXt
203
229
private decodetEXt ( length : number ) : void {
204
230
let keyword = '' ;
0 commit comments