Skip to content

Commit 683d61a

Browse files
committed
fix(decode): set channels property of result
1 parent 998c162 commit 683d61a

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

‎src/PNGDecoder.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default class PNGDecoder extends IOBuffer {
1919
private _end: boolean;
2020
private _hasPalette: boolean;
2121
private _palette: IndexedColors;
22-
private _colorType: ColorType;
2322
private _compressionMethod: CompressionMethod;
2423
private _filterMethod: FilterMethod;
2524
private _interlaceMethod: InterlaceMethod;
@@ -36,7 +35,6 @@ export default class PNGDecoder extends IOBuffer {
3635
this._end = false;
3736
this._hasPalette = false;
3837
this._palette = [];
39-
this._colorType = ColorType.UNKNOWN;
4038
this._compressionMethod = CompressionMethod.UNKNOWN;
4139
this._filterMethod = FilterMethod.UNKNOWN;
4240
this._interlaceMethod = InterlaceMethod.UNKNOWN;
@@ -126,15 +124,39 @@ export default class PNGDecoder extends IOBuffer {
126124
image.height = this.readUint32();
127125
// @ts-ignore
128126
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+
130151
this._compressionMethod = this.readUint8();
131-
this._filterMethod = this.readUint8();
132-
this._interlaceMethod = this.readUint8();
133152
if (this._compressionMethod !== CompressionMethod.DEFLATE) {
134153
throw new Error(
135154
`Unsupported compression method: ${this._compressionMethod}`
136155
);
137156
}
157+
158+
this._filterMethod = this.readUint8();
159+
this._interlaceMethod = this.readUint8();
138160
}
139161

140162
// https://www.w3.org/TR/PNG/#11PLTE
@@ -203,30 +225,8 @@ export default class PNGDecoder extends IOBuffer {
203225
}
204226

205227
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-
228228
const height = this._png.height;
229-
const bytesPerPixel = (channels * this._png.depth) / 8;
229+
const bytesPerPixel = (this._png.channels * this._png.depth) / 8;
230230
const bytesPerLine = this._png.width * bytesPerPixel;
231231
const newData = new Uint8Array(this._png.height * bytesPerLine);
232232

‎src/__tests__/decode.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('decode', () => {
1111
check(img, {
1212
width: 2,
1313
height: 2,
14-
depth: 8
14+
depth: 8,
15+
channels: 2
1516
});
1617
expect(img.data).toBeInstanceOf(Uint8Array);
1718
expect(img.data).toStrictEqual(
@@ -24,7 +25,8 @@ describe('decode', () => {
2425
check(img, {
2526
width: 10,
2627
height: 10,
27-
depth: 8
28+
depth: 8,
29+
channels: 4
2830
});
2931
expect(img.data).toBeInstanceOf(Uint8Array);
3032
expect(img.data).toHaveLength(10 * 10 * 4);
@@ -35,7 +37,8 @@ describe('decode', () => {
3537
check(img, {
3638
width: 150,
3739
height: 200,
38-
depth: 8
40+
depth: 8,
41+
channels: 1
3942
});
4043
expect(img.palette).toBeInstanceOf(Array);
4144
expect(img.palette).toHaveLength(256);

0 commit comments

Comments
 (0)