Skip to content

Commit 306b32b

Browse files
committed
refactor: rename bitDepth to depth
BREAKING CHANGE: The `bitDepth` field was renamed to `depth`.
1 parent b6348f5 commit 306b32b

File tree

6 files changed

+32
-36
lines changed

6 files changed

+32
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ PNG image decoder and encoder written entirely in JavaScript.
3232
- `width` - The width of the image
3333
- `height` - The height of the image
3434
- `data` - An array or TypedArray with the image data
35-
- `bitDepth` - A number indicating the bit depth (only 8 and 16 are supported now). Defaults to 8.
35+
- `depth` - A number indicating the color depth (only 8 and 16 are supported now). Defaults to 8.
3636
- `components` - Number of non-alpha channels (1 or 3 are supported). Defaults to 3.
3737
- `alpha` - Boolean indicating if an alpha channel is present. Defaults to true.
3838

src/PNGDecoder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export default class PNGDecoder extends IOBuffer {
114114
const image = this._png;
115115
image.width = this.readUint32();
116116
image.height = this.readUint32();
117-
image.bitDepth = this.readUint8();
117+
image.depth = this.readUint8();
118118
image.colourType = this.readUint8();
119119
image.compressionMethod = this.readUint8();
120120
image.filterMethod = this.readUint8();
@@ -216,7 +216,7 @@ export default class PNGDecoder extends IOBuffer {
216216
}
217217

218218
const height = this._png.height;
219-
const bytesPerPixel = (channels * this._png.bitDepth) / 8;
219+
const bytesPerPixel = (channels * this._png.depth) / 8;
220220
const bytesPerLine = this._png.width * bytesPerPixel;
221221
const newData = new Uint8Array(this._png.height * bytesPerLine);
222222

@@ -267,7 +267,7 @@ export default class PNGDecoder extends IOBuffer {
267267
this._png.palette = this._palette;
268268
}
269269

270-
if (this._png.bitDepth === 16) {
270+
if (this._png.depth === 16) {
271271
const uint16Data = new Uint16Array(newData.buffer);
272272
if (osIsLittleEndian) {
273273
for (let k = 0; k < uint16Data.length; k++) {

src/PNGEncoder.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export default class PNGEncoder extends IOBuffer {
4848

4949
this.writeUint32(this._png.width);
5050
this.writeUint32(this._png.height);
51-
this.writeByte(this._png.bitDepth);
51+
this.writeByte(this._png.depth);
5252
this.writeByte(this._png.colourType);
5353
this.writeByte(0); // Compression method
5454
this.writeByte(0); // Filter method
@@ -78,16 +78,16 @@ export default class PNGEncoder extends IOBuffer {
7878
}
7979

8080
private encodeData(): void {
81-
const { width, height, channels, bitDepth, data } = this._png;
81+
const { width, height, channels, depth, data } = this._png;
8282
const slotsPerLine = channels * width;
8383
const newData = new IOBuffer().setBigEndian();
8484
let offset = 0;
8585
for (let i = 0; i < height; i++) {
8686
newData.writeByte(0); // no filter
8787
/* istanbul ignore else */
88-
if (bitDepth === 8) {
88+
if (depth === 8) {
8989
offset = writeDataBytes(data, newData, slotsPerLine, offset);
90-
} else if (bitDepth === 16) {
90+
} else if (depth === 16) {
9191
offset = writeDataUint16(data, newData, slotsPerLine, offset);
9292
} else {
9393
throw new Error('unreachable');
@@ -102,10 +102,10 @@ export default class PNGEncoder extends IOBuffer {
102102
this._png.width = checkInteger(data.width, 'width');
103103
this._png.height = checkInteger(data.height, 'height');
104104
this._png.data = data.data;
105-
const { colourType, channels, bitDepth } = getColourType(data);
105+
const { colourType, channels, depth } = getColourType(data);
106106
this._png.colourType = colourType;
107107
this._png.channels = channels;
108-
this._png.bitDepth = bitDepth;
108+
this._png.depth = depth;
109109
const expectedSize = this._png.width * this._png.height * channels;
110110
if (this._png.data.length !== expectedSize) {
111111
throw new RangeError(
@@ -139,13 +139,13 @@ function checkInteger(value: number, name: string): number {
139139

140140
function getColourType(
141141
data: IImageData
142-
): { channels: number; bitDepth: number; colourType: number } {
143-
let { components = 3, bitDepth = 8 } = data;
142+
): { channels: number; depth: number; colourType: number } {
143+
let { components = 3, depth = 8 } = data;
144144
if (components !== 3 && components !== 1) {
145145
throw new RangeError(`unsupported number of components: ${components}`);
146146
}
147-
if (bitDepth !== 8 && bitDepth !== 16) {
148-
throw new RangeError(`unsupported bit depth: ${bitDepth}`);
147+
if (depth !== 8 && depth !== 16) {
148+
throw new RangeError(`unsupported bit depth: ${depth}`);
149149
}
150150

151151
let { alpha = true } = data;
@@ -157,7 +157,7 @@ function getColourType(
157157
}
158158

159159
const channels = components + Number(alpha);
160-
const returnValue = { channels, bitDepth, colourType: -1 };
160+
const returnValue = { channels, depth, colourType: -1 };
161161
switch (channels) {
162162
case 4:
163163
returnValue.colourType = 6;

src/__tests__/decode.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('decode', () => {
1111
check(img, {
1212
width: 2,
1313
height: 2,
14-
bitDepth: 8,
14+
depth: 8,
1515
colourType: 4
1616
});
1717
expect(img.data).toBeInstanceOf(Uint8Array);
@@ -25,7 +25,7 @@ describe('decode', () => {
2525
check(img, {
2626
width: 10,
2727
height: 10,
28-
bitDepth: 8,
28+
depth: 8,
2929
colourType: 6
3030
});
3131
expect(img.data).toBeInstanceOf(Uint8Array);
@@ -37,7 +37,7 @@ describe('decode', () => {
3737
check(img, {
3838
width: 150,
3939
height: 200,
40-
bitDepth: 8,
40+
depth: 8,
4141
colourType: 3
4242
});
4343
expect(img.palette).toBeInstanceOf(Array);

src/__tests__/encode.test.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('encode', () => {
3333
const expected = {
3434
width: 2,
3535
height: 2,
36-
bitDepth: 8,
36+
depth: 8,
3737
colourType: 6
3838
};
3939
check(decoded, expected);
@@ -56,7 +56,7 @@ describe('encode', () => {
5656
const expected = {
5757
width: 2,
5858
height: 3,
59-
bitDepth: 8,
59+
depth: 8,
6060
colourType: 0
6161
};
6262
check(decoded, expected);
@@ -83,7 +83,7 @@ describe('encode', () => {
8383
const data = encode({
8484
width: 2,
8585
height: 2,
86-
bitDepth: 16,
86+
depth: 16,
8787
data: dataArray,
8888
components: 3,
8989
alpha: false
@@ -93,7 +93,7 @@ describe('encode', () => {
9393
const expected = {
9494
width: 2,
9595
height: 2,
96-
bitDepth: 16,
96+
depth: 16,
9797
colourType: 2
9898
};
9999
check(decoded, expected);
@@ -129,7 +129,7 @@ describe('encode', () => {
129129
const expected = {
130130
width: 2,
131131
height: 3,
132-
bitDepth: 8,
132+
depth: 8,
133133
colourType: 4
134134
};
135135
check(decoded, expected);
@@ -143,7 +143,7 @@ describe('encode', () => {
143143
encode({
144144
width: 1,
145145
height: 1,
146-
bitDepth: 8,
146+
depth: 8,
147147
data: new Uint8Array(),
148148
components: 5,
149149
alpha: true
@@ -153,7 +153,7 @@ describe('encode', () => {
153153
encode({
154154
width: 1,
155155
height: 1,
156-
bitDepth: 8,
156+
depth: 8,
157157
data: new Uint8Array(),
158158
components: 3,
159159
// @ts-ignore
@@ -164,7 +164,7 @@ describe('encode', () => {
164164
encode({
165165
width: 1.1,
166166
height: 1,
167-
bitDepth: 8,
167+
depth: 8,
168168
data: new Uint8Array(),
169169
components: 3,
170170
alpha: false
@@ -175,7 +175,7 @@ describe('encode', () => {
175175
width: 1,
176176
// @ts-ignore
177177
height: undefined,
178-
bitDepth: 8,
178+
depth: 8,
179179
data: new Uint8Array(),
180180
components: 3,
181181
alpha: false
@@ -185,7 +185,7 @@ describe('encode', () => {
185185
encode({
186186
width: 1,
187187
height: 1,
188-
bitDepth: 8,
188+
depth: 8,
189189
data: new Uint8Array(10),
190190
components: 3,
191191
alpha: false
@@ -195,7 +195,7 @@ describe('encode', () => {
195195
encode({
196196
width: 1,
197197
height: 1,
198-
bitDepth: 1,
198+
depth: 1,
199199
data: new Uint8Array(10),
200200
components: 3,
201201
alpha: false
@@ -215,10 +215,6 @@ function check(img: any, values: any): void {
215215
function checkPngJs(data: any, values: any): void {
216216
const img = PNG.sync.read(Buffer.from(data, data.byteOffset, data.length));
217217
var newValues = Object.assign({}, values);
218-
if (newValues.bitDepth !== undefined) {
219-
newValues.depth = newValues.bitDepth;
220-
delete newValues.bitDepth;
221-
}
222218
if (newValues.colourType !== undefined) {
223219
newValues.colorType = newValues.colourType;
224220
delete newValues.colourType;

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export type PNGDataArray = Uint8Array | Uint16Array;
88
export type DecoderInputType = IOBuffer | ArrayBufferLike | ArrayBufferView;
99

1010
export interface IDecodedPNG {
11-
height: number;
1211
width: number;
12+
height: number;
1313
data: PNGDataArray;
14-
bitDepth: number;
14+
depth: number;
1515
colourType: number;
1616
channels: number;
1717
compressionMethod: number;
@@ -31,9 +31,9 @@ export interface IImageData {
3131
width: number;
3232
height: number;
3333
data: PNGDataArray;
34+
depth?: number;
3435
components?: number;
3536
alpha?: boolean | 0 | 1;
36-
bitDepth?: number;
3737
}
3838

3939
export interface IPNGEncoderOptions {

0 commit comments

Comments
 (0)