@@ -31,6 +31,8 @@ export default class PngDecoder extends IOBuffer {
31
31
private _end : boolean ;
32
32
private _hasPalette : boolean ;
33
33
private _palette : IndexedColors ;
34
+ private _hasTransparency : boolean ;
35
+ private _transparency : Uint16Array ;
34
36
private _compressionMethod : CompressionMethod ;
35
37
private _filterMethod : FilterMethod ;
36
38
private _interlaceMethod : InterlaceMethod ;
@@ -52,6 +54,8 @@ export default class PngDecoder extends IOBuffer {
52
54
this . _end = false ;
53
55
this . _hasPalette = false ;
54
56
this . _palette = [ ] ;
57
+ this . _hasTransparency = false ;
58
+ this . _transparency = new Uint16Array ( 0 ) ;
55
59
this . _compressionMethod = CompressionMethod . UNKNOWN ;
56
60
this . _filterMethod = FilterMethod . UNKNOWN ;
57
61
this . _interlaceMethod = InterlaceMethod . UNKNOWN ;
@@ -210,7 +214,27 @@ export default class PngDecoder extends IOBuffer {
210
214
// https://www.w3.org/TR/PNG/#11tRNS
211
215
private decodetRNS ( length : number ) : void {
212
216
switch ( this . _colorType ) {
213
- // TODO: support other color types.
217
+ case ColorType . GREYSCALE :
218
+ case ColorType . TRUECOLOUR : {
219
+ if ( length % 2 !== 0 ) {
220
+ throw new RangeError (
221
+ `tRNS chunk length must be a multiple of 2. Got ${ length } ` ,
222
+ ) ;
223
+ }
224
+ if ( length / 2 > this . _png . width * this . _png . height ) {
225
+ throw new Error (
226
+ `tRNS chunk contains more alpha values than there are pixels (${
227
+ length / 2
228
+ } vs ${ this . _png . width * this . _png . height } )`,
229
+ ) ;
230
+ }
231
+ this . _hasTransparency = true ;
232
+ this . _transparency = new Uint16Array ( length / 2 ) ;
233
+ for ( let i = 0 ; i < length / 2 ; i ++ ) {
234
+ this . _transparency [ i ] = this . readUint16 ( ) ;
235
+ }
236
+ break ;
237
+ }
214
238
case ColorType . INDEXED_COLOUR : {
215
239
if ( length > this . _palette . length ) {
216
240
throw new Error (
@@ -346,6 +370,9 @@ export default class PngDecoder extends IOBuffer {
346
370
if ( this . _hasPalette ) {
347
371
this . _png . palette = this . _palette ;
348
372
}
373
+ if ( this . _hasTransparency ) {
374
+ this . _png . transparency = this . _transparency ;
375
+ }
349
376
350
377
if ( this . _png . depth === 16 ) {
351
378
const uint16Data = new Uint16Array ( newData . buffer ) ;
0 commit comments