Skip to content

Commit 9749e1c

Browse files
committed
fix: remove misleading kind option
Replaced by separate "components" and "alpha" options. BREAKING CHANGE: The "kind" option was misleading and has been replaced by separate "components" and "alpha" options.
1 parent 29d41ef commit 9749e1c

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ __Arguments__
3434
* `height` - The height of the image
3535
* `data` - An array or TypedArray with the image data
3636
* `bitDepth` - A number indicating the bit depth (only 8 and 16 are supported now). Defaults to 8.
37-
* `kind` - One of `'RGBA'`, `'RGB'`, `'GREYA'`, `'GREY'`. This will determine the number of channels and the colour type of the image. Defaults to `'RGBA'`.
37+
* `components` - Number of non-alpha channels (1 or 3 are supported). Defaults to 3.
38+
* `alpha` - Boolean indicating if an alpha channel is present. Defaults to true.
3839

3940
## PNG standard
4041

‎__tests__/encode.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ describe('encode', () => {
3030
width: 2,
3131
height: 3,
3232
data: dataArray,
33-
kind: 'GREY'
33+
components: 1,
34+
alpha: false
3435
});
3536
expect(data).toBeInstanceOf(Uint8Array);
3637
const decoded = decode(data);
@@ -53,7 +54,8 @@ describe('encode', () => {
5354
height: 2,
5455
bitDepth: 16,
5556
data: dataArray,
56-
kind: 'RGB'
57+
components: 3,
58+
alpha: false
5759
});
5860
expect(data).toBeInstanceOf(Uint8Array);
5961
const decoded = decode(data);
@@ -68,6 +70,17 @@ describe('encode', () => {
6870
expect(decoded.data).toBeInstanceOf(Uint16Array);
6971
expect(decoded.data).toEqual(dataArray);
7072
});
73+
74+
it('errors', () => {
75+
expect(() => encode({width: 1, height: 1, bitDepth: 8, data: [], components: 2, alpha: true}))
76+
.toThrow('unsupported number of components: 2');
77+
expect(() => encode({width: 1, height: 1, bitDepth: 8, data: [], components: 3, alpha: 2}))
78+
.toThrow('unsupported number of channels: 5');
79+
expect(() => encode({width: 1.1, height: 1, bitDepth: 8, data: [], components: 3, alpha: false}))
80+
.toThrow('width must be a positive integer');
81+
expect(() => encode({width: 1, height: undefined, bitDepth: 8, data: [], components: 3, alpha: false}))
82+
.toThrow('height must be a positive integer');
83+
});
7184
});
7285

7386
function check(img, values) {

‎src/PNGEncoder.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default class PNGDecoder extends IOBuffer {
8989
height: checkInteger(data.height, 'height'),
9090
data: data.data
9191
};
92-
const {colourType, channels} = getColourType(data.kind);
92+
const {colourType, channels} = getColourType(data);
9393
this._png.colourType = colourType;
9494
this._png.channels = channels;
9595
this._png.bitDepth = data.bitDepth || 8;
@@ -111,18 +111,26 @@ function checkInteger(value, name) {
111111
throw new TypeError(`${name} must be a positive integer`);
112112
}
113113

114-
function getColourType(kind = 'RGBA') {
115-
switch (kind) {
116-
case 'RGBA':
114+
function getColourType(data) {
115+
const {
116+
components = 3,
117+
alpha = true
118+
} = data;
119+
if (components !== 3 && components !== 1) {
120+
throw new RangeError(`unsupported number of components: ${components}`);
121+
}
122+
const channels = components + Number(alpha);
123+
switch (channels) {
124+
case 4:
117125
return {colourType: 6, channels: 4};
118-
case 'RGB':
126+
case 3:
119127
return {colourType: 2, channels: 3};
120-
case 'GREY':
128+
case 1:
121129
return {colourType: 0, channels: 1};
122-
case 'GREYA':
130+
case 2:
123131
return {colourType: 4, channels: 2};
124132
default:
125-
throw new Error(`unknown kind: ${kind}`);
133+
throw new Error(`unsupported number of channels: ${channels}`);
126134
}
127135
}
128136

0 commit comments

Comments
 (0)