Skip to content

Commit 35393fa

Browse files
committed
refactor: replace components with channels and remove alpha
BREAKING CHANGE: * The components property has been replaced with channels, that includes alpha. * The alpha property has been removed. It was unused after the previous change.
1 parent 306b32b commit 35393fa

File tree

4 files changed

+14
-44
lines changed

4 files changed

+14
-44
lines changed

‎README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ PNG image decoder and encoder written entirely in JavaScript.
3333
- `height` - The height of the image
3434
- `data` - An array or TypedArray with the image data
3535
- `depth` - A number indicating the color depth (only 8 and 16 are supported now). Defaults to 8.
36-
- `components` - Number of non-alpha channels (1 or 3 are supported). Defaults to 3.
37-
- `alpha` - Boolean indicating if an alpha channel is present. Defaults to true.
36+
- `channels` - Number of channels, including alpha (1, 2, 3 and 4 are supported). Defaults to 4.
3837

3938
## PNG standard
4039

‎src/PNGEncoder.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,14 @@ function checkInteger(value: number, name: string): number {
140140
function getColourType(
141141
data: IImageData
142142
): { channels: number; depth: number; colourType: number } {
143-
let { components = 3, depth = 8 } = data;
144-
if (components !== 3 && components !== 1) {
145-
throw new RangeError(`unsupported number of components: ${components}`);
143+
const { channels = 4, depth = 8 } = data;
144+
if (channels !== 4 && channels !== 3 && channels !== 2 && channels !== 1) {
145+
throw new RangeError(`unsupported number of channels: ${channels}`);
146146
}
147147
if (depth !== 8 && depth !== 16) {
148148
throw new RangeError(`unsupported bit depth: ${depth}`);
149149
}
150150

151-
let { alpha = true } = data;
152-
if (alpha === 0 || alpha === 1) {
153-
alpha = Boolean(alpha);
154-
}
155-
if (typeof alpha !== 'boolean') {
156-
throw new TypeError(`unsupported alpha: ${alpha}`);
157-
}
158-
159-
const channels = components + Number(alpha);
160151
const returnValue = { channels, depth, colourType: -1 };
161152
switch (channels) {
162153
case 4:

‎src/__tests__/encode.test.ts

+9-28
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ describe('encode', () => {
4848
width: 2,
4949
height: 3,
5050
data: dataArray,
51-
components: 1,
52-
alpha: false
51+
channels: 1
5352
});
5453
expect(data).toBeInstanceOf(Uint8Array);
5554
const decoded = decode(data);
@@ -85,8 +84,7 @@ describe('encode', () => {
8584
height: 2,
8685
depth: 16,
8786
data: dataArray,
88-
components: 3,
89-
alpha: false
87+
channels: 3
9088
});
9189
expect(data).toBeInstanceOf(Uint8Array);
9290
const decoded = decode(data);
@@ -121,8 +119,7 @@ describe('encode', () => {
121119
width: 2,
122120
height: 3,
123121
data: dataArray,
124-
components: 1,
125-
alpha: true
122+
channels: 2
126123
});
127124
expect(data).toBeInstanceOf(Uint8Array);
128125
const decoded = decode(data);
@@ -145,29 +142,16 @@ describe('encode', () => {
145142
height: 1,
146143
depth: 8,
147144
data: new Uint8Array(),
148-
components: 5,
149-
alpha: true
145+
channels: 5
150146
})
151-
).toThrow('unsupported number of components: 5');
152-
expect(() =>
153-
encode({
154-
width: 1,
155-
height: 1,
156-
depth: 8,
157-
data: new Uint8Array(),
158-
components: 3,
159-
// @ts-ignore
160-
alpha: 2
161-
})
162-
).toThrow('unsupported alpha: 2');
147+
).toThrow('unsupported number of channels: 5');
163148
expect(() =>
164149
encode({
165150
width: 1.1,
166151
height: 1,
167152
depth: 8,
168153
data: new Uint8Array(),
169-
components: 3,
170-
alpha: false
154+
channels: 3
171155
})
172156
).toThrow('width must be a positive integer');
173157
expect(() =>
@@ -177,8 +161,7 @@ describe('encode', () => {
177161
height: undefined,
178162
depth: 8,
179163
data: new Uint8Array(),
180-
components: 3,
181-
alpha: false
164+
channels: 3
182165
})
183166
).toThrow('height must be a positive integer');
184167
expect(() =>
@@ -187,8 +170,7 @@ describe('encode', () => {
187170
height: 1,
188171
depth: 8,
189172
data: new Uint8Array(10),
190-
components: 3,
191-
alpha: false
173+
channels: 3
192174
})
193175
).toThrow('wrong data size. Found 10, expected 3');
194176
expect(() =>
@@ -197,8 +179,7 @@ describe('encode', () => {
197179
height: 1,
198180
depth: 1,
199181
data: new Uint8Array(10),
200-
components: 3,
201-
alpha: false
182+
channels: 3
202183
})
203184
).toThrow('unsupported bit depth: 1');
204185
});

‎src/types.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export interface IImageData {
3232
height: number;
3333
data: PNGDataArray;
3434
depth?: number;
35-
components?: number;
36-
alpha?: boolean | 0 | 1;
35+
channels?: number;
3736
}
3837

3938
export interface IPNGEncoderOptions {

0 commit comments

Comments
 (0)