Skip to content

Commit df9aa5e

Browse files
committed
fix: revert breaking change in PNGDecoder
1 parent e7cf353 commit df9aa5e

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

‎src/PNGEncoder.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ function checkInteger(value: number, name: string): number {
140140
function getColourType(
141141
data: IImageData
142142
): { channels: number; bitDepth: number; colourType: number } {
143-
const { channels = 4, bitDepth = 8 } = data;
144-
if (channels !== 4 && channels !== 3 && channels !== 2 && channels !== 1) {
145-
throw new RangeError(`unsupported number of channels: ${channels}`);
143+
let { components = 3, bitDepth = 8 } = data;
144+
if (components !== 3 && components !== 1) {
145+
throw new RangeError(`unsupported number of components: ${components}`);
146146
}
147147
if (bitDepth !== 8 && bitDepth !== 16) {
148148
throw new RangeError(`unsupported bit depth: ${bitDepth}`);
@@ -156,6 +156,7 @@ function getColourType(
156156
throw new TypeError(`unsupported alpha: ${alpha}`);
157157
}
158158

159+
const channels = components + Number(alpha);
159160
const returnValue = { channels, bitDepth, colourType: -1 };
160161
switch (channels) {
161162
case 4:

‎src/__tests__/encode.test.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('encode', () => {
4848
width: 2,
4949
height: 3,
5050
data: dataArray,
51-
channels: 1,
51+
components: 1,
5252
alpha: false
5353
});
5454
expect(data).toBeInstanceOf(Uint8Array);
@@ -85,7 +85,7 @@ describe('encode', () => {
8585
height: 2,
8686
bitDepth: 16,
8787
data: dataArray,
88-
channels: 3,
88+
components: 3,
8989
alpha: false
9090
});
9191
expect(data).toBeInstanceOf(Uint8Array);
@@ -121,7 +121,7 @@ describe('encode', () => {
121121
width: 2,
122122
height: 3,
123123
data: dataArray,
124-
channels: 2,
124+
components: 1,
125125
alpha: true
126126
});
127127
expect(data).toBeInstanceOf(Uint8Array);
@@ -145,17 +145,17 @@ describe('encode', () => {
145145
height: 1,
146146
bitDepth: 8,
147147
data: new Uint8Array(),
148-
channels: 5,
148+
components: 5,
149149
alpha: true
150150
})
151-
).toThrow('unsupported number of channels: 5');
151+
).toThrow('unsupported number of components: 5');
152152
expect(() =>
153153
encode({
154154
width: 1,
155155
height: 1,
156156
bitDepth: 8,
157157
data: new Uint8Array(),
158-
channels: 3,
158+
components: 3,
159159
// @ts-ignore
160160
alpha: 2
161161
})
@@ -166,7 +166,7 @@ describe('encode', () => {
166166
height: 1,
167167
bitDepth: 8,
168168
data: new Uint8Array(),
169-
channels: 3,
169+
components: 3,
170170
alpha: false
171171
})
172172
).toThrow('width must be a positive integer');
@@ -177,7 +177,7 @@ describe('encode', () => {
177177
height: undefined,
178178
bitDepth: 8,
179179
data: new Uint8Array(),
180-
channels: 3,
180+
components: 3,
181181
alpha: false
182182
})
183183
).toThrow('height must be a positive integer');
@@ -187,7 +187,7 @@ describe('encode', () => {
187187
height: 1,
188188
bitDepth: 8,
189189
data: new Uint8Array(10),
190-
channels: 3,
190+
components: 3,
191191
alpha: false
192192
})
193193
).toThrow('wrong data size. Found 10, expected 3');
@@ -197,7 +197,7 @@ describe('encode', () => {
197197
height: 1,
198198
bitDepth: 1,
199199
data: new Uint8Array(10),
200-
channels: 3,
200+
components: 3,
201201
alpha: false
202202
})
203203
).toThrow('unsupported bit depth: 1');

‎src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface IImageData {
3131
width: number;
3232
height: number;
3333
data: PNGDataArray;
34-
channels?: number;
34+
components?: number;
3535
alpha?: boolean | 0 | 1;
3636
bitDepth?: number;
3737
}

0 commit comments

Comments
 (0)