Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 30b0b08

Browse files
authored
refactor: rename depth to bitDepth and improve some error messages (#298)
Closes: #280
1 parent 661e66b commit 30b0b08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+213
-211
lines changed

src/Image.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ import {
7676
import {
7777
convertColor,
7878
ConvertColorOptions,
79-
convertDepth,
79+
convertBitDepth,
8080
copyTo,
8181
CopyToOptions,
8282
crop,
@@ -106,7 +106,7 @@ export type ImageDataArray = Uint8Array | Uint16Array | Uint8ClampedArray;
106106
/**
107107
* Bit depth of the image (nb of bits that encode each value in the image).
108108
*/
109-
export type ColorDepth = 1 | 8 | 16;
109+
export type BitDepth = 1 | 8 | 16;
110110

111111
export const ImageCoordinates = {
112112
CENTER: 'center',
@@ -125,7 +125,7 @@ export interface ImageOptions {
125125
*
126126
* @default `8`.
127127
*/
128-
depth?: ColorDepth;
128+
bitDepth?: BitDepth;
129129

130130
/**
131131
* Typed array holding the image data.
@@ -173,7 +173,7 @@ export class Image {
173173
/**
174174
* The number of bits per value in each channel.
175175
*/
176-
public readonly depth: ColorDepth;
176+
public readonly bitDepth: BitDepth;
177177

178178
/**
179179
* The color model of the image.
@@ -222,7 +222,7 @@ export class Image {
222222
options: ImageOptions = {},
223223
) {
224224
const {
225-
depth = 8,
225+
bitDepth = 8,
226226
data,
227227
colorModel = 'RGB',
228228
origin = { row: 0, column: 0 },
@@ -243,7 +243,7 @@ export class Image {
243243
this.width = width;
244244
this.height = height;
245245
this.size = width * height;
246-
this.depth = depth;
246+
this.bitDepth = bitDepth;
247247
this.colorModel = colorModel;
248248
this.origin = origin;
249249

@@ -252,21 +252,21 @@ export class Image {
252252
this.alpha = colorModelDef.alpha;
253253
this.channels = colorModelDef.channels;
254254

255-
this.maxValue = 2 ** depth - 1;
255+
this.maxValue = 2 ** bitDepth - 1;
256256

257257
if (data === undefined) {
258258
this.data = createPixelArray(
259259
this.size,
260260
this.channels,
261261
this.alpha,
262-
this.depth,
262+
this.bitDepth,
263263
this.maxValue,
264264
);
265265
} else {
266-
if (depth === 8 && data instanceof Uint16Array) {
267-
throw new Error(`depth is ${depth} but data is Uint16Array`);
268-
} else if (depth === 16 && data instanceof Uint8Array) {
269-
throw new Error(`depth is ${depth} but data is Uint8Array`);
266+
if (bitDepth === 8 && data instanceof Uint16Array) {
267+
throw new Error(`bitDepth is ${bitDepth} but data is Uint16Array`);
268+
} else if (bitDepth === 16 && data instanceof Uint8Array) {
269+
throw new Error(`bitDepth is ${bitDepth} but data is Uint8Array`);
270270
}
271271
const expectedLength = this.size * this.channels;
272272
if (data.length !== expectedLength) {
@@ -290,14 +290,14 @@ export class Image {
290290
options: CreateFromOptions = {},
291291
): Image {
292292
const { width = other.width, height = other.height } = options;
293-
let depth: ColorDepth;
293+
let bitDepth: BitDepth;
294294
if (other instanceof Image) {
295-
depth = other.depth;
295+
bitDepth = other.bitDepth;
296296
} else {
297-
depth = 8;
297+
bitDepth = 8;
298298
}
299299
return new Image(width, height, {
300-
depth,
300+
bitDepth,
301301
colorModel: other.colorModel,
302302
origin: other.origin,
303303
...options,
@@ -458,7 +458,7 @@ export class Image {
458458
height: this.height,
459459
data: this.data,
460460
channels: this.channels,
461-
depth: this.depth,
461+
bitDepth: this.bitDepth,
462462
};
463463
}
464464

@@ -472,7 +472,7 @@ export class Image {
472472
return `Image {
473473
width: ${this.width}
474474
height: ${this.height}
475-
depth: ${this.depth}
475+
bitDepth: ${this.bitDepth}
476476
colorModel: ${this.colorModel}
477477
channels: ${this.channels}
478478
data: ${dataString}
@@ -577,7 +577,7 @@ export class Image {
577577
* Get the coordinates of a point in the image. The reference is the top-left corner.
578578
*
579579
* @param coordinates - The point for which you want the coordinates.
580-
* @param round - Should the coordinates be rounded? This is useful when you want the center of the image.
580+
* @param round - Whether the coordinates should be rounded. This is useful when you want the center of the image.
581581
* @returns Coordinates of the point in the format [column, row].
582582
*/
583583
public getCoordinates(coordinates: ImageCoordinates, round = false): Point {
@@ -766,8 +766,8 @@ export class Image {
766766
return convertColor(this, colorModel, options);
767767
}
768768

769-
public convertDepth(newDepth: ColorDepth): Image {
770-
return convertDepth(this, newDepth);
769+
public convertBitDepth(newDepth: BitDepth): Image {
770+
return convertBitDepth(this, newDepth);
771771
}
772772

773773
public grey(options?: GreyOptions): Image {
@@ -1054,28 +1054,28 @@ export class Image {
10541054
* @param size - Number of pixels.
10551055
* @param channels - Number of channels.
10561056
* @param alpha - Specify if there is alpha channel.
1057-
* @param depth - Number of bits per channel.
1057+
* @param bitDepth - Number of bits per channel.
10581058
* @param maxValue - Maximal acceptable value for the channels.
10591059
* @returns The new pixel array.
10601060
*/
10611061
function createPixelArray(
10621062
size: number,
10631063
channels: number,
10641064
alpha: boolean,
1065-
depth: ColorDepth,
1065+
bitDepth: BitDepth,
10661066
maxValue: number,
10671067
): ImageDataArray {
10681068
const length = channels * size;
10691069
let arr;
1070-
switch (depth) {
1070+
switch (bitDepth) {
10711071
case 8:
10721072
arr = new Uint8Array(length);
10731073
break;
10741074
case 16:
10751075
arr = new Uint16Array(length);
10761076
break;
10771077
default:
1078-
throw new Error(`unexpected color depth: ${depth}`);
1078+
throw new Error(`unexpected bitDepth: ${bitDepth}`);
10791079
}
10801080

10811081
// Alpha channel is 100% by default.
@@ -1096,7 +1096,7 @@ function createPixelArray(
10961096
*/
10971097
function printData(img: Image): string {
10981098
const result = [];
1099-
const padding = img.depth === 8 ? 3 : 5;
1099+
const padding = img.bitDepth === 8 ? 3 : 5;
11001100

11011101
for (let row = 0; row < img.height; row++) {
11021102
const currentRow = [];

src/Mask.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColorDepth, Image } from './Image';
1+
import { BitDepth, Image } from './Image';
22
import { subtract, SubtractImageOptions } from './compare';
33
import {
44
drawLineOnMask,
@@ -100,7 +100,7 @@ export class Mask {
100100
/**
101101
* The number of bits per value in each channel (always 1).
102102
*/
103-
public readonly depth: ColorDepth;
103+
public readonly bitDepth: BitDepth;
104104

105105
/**
106106
* The color model of the mask (always BINARY).
@@ -164,7 +164,7 @@ export class Mask {
164164
this.width = width;
165165
this.height = height;
166166
this.size = width * height;
167-
this.depth = 1;
167+
this.bitDepth = 1;
168168
this.colorModel = 'BINARY';
169169
this.origin = origin;
170170

src/__tests__/Image.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('create new images', () => {
99
width: 10,
1010
height: 20,
1111
size: 200,
12-
depth: 8,
12+
bitDepth: 8,
1313
colorModel: 'RGB',
1414
components: 3,
1515
channels: 3,
@@ -20,12 +20,12 @@ describe('create new images', () => {
2020
});
2121

2222
it('should create a 16-bit image', () => {
23-
const img = new Image(10, 20, { depth: 16 });
23+
const img = new Image(10, 20, { bitDepth: 16 });
2424
expect(img).toMatchObject({
2525
width: 10,
2626
height: 20,
2727
size: 200,
28-
depth: 16,
28+
bitDepth: 16,
2929
colorModel: 'RGB',
3030
components: 3,
3131
channels: 3,
@@ -38,7 +38,7 @@ describe('create new images', () => {
3838
it('should create a grey image with alpha', () => {
3939
const img = new Image(10, 20, { colorModel: 'GREYA' });
4040
expect(img).toMatchObject({
41-
depth: 8,
41+
bitDepth: 8,
4242
colorModel: 'GREYA',
4343
components: 1,
4444
channels: 2,
@@ -78,33 +78,33 @@ describe('create new images', () => {
7878

7979
it('should throw on wrong data size', () => {
8080
const data = new Uint16Array(2);
81-
expect(() => new Image(2, 2, { data, depth: 16 })).toThrow(
81+
expect(() => new Image(2, 2, { data, bitDepth: 16 })).toThrow(
8282
/incorrect data size: 2. Expected 12/,
8383
);
8484
});
8585

8686
it('should throw on wrong bit depth', () => {
8787
// @ts-expect-error we want to test the error.
88-
expect(() => new Image(1, 1, { depth: 20 })).toThrow(
89-
/unexpected color depth: 20/,
88+
expect(() => new Image(1, 1, { bitDepth: 20 })).toThrow(
89+
/unexpected bitDepth: 20/,
9090
);
9191
});
92-
it('should throw depth 8 but data 16', () => {
92+
it('should throw with bit depth 8 but data 16', () => {
9393
const data = new Uint16Array([1, 2, 3, 4]);
9494
expect(() => new Image(2, 2, { colorModel: 'GREY', data })).toThrow(
95-
'depth is 8 but data is Uint16Array',
95+
'bitDepth is 8 but data is Uint16Array',
9696
);
9797
});
98-
it('should throw depth 16 but data 8', () => {
98+
it('should throw with bit depth 16 but data 8', () => {
9999
const data = new Uint8Array([1, 2, 3, 4]);
100100
expect(
101101
() =>
102102
new Image(2, 2, {
103103
colorModel: 'GREY',
104-
depth: 16,
104+
bitDepth: 16,
105105
data,
106106
}),
107-
).toThrow('depth is 16 but data is Uint8Array');
107+
).toThrow('bitDepth is 16 but data is Uint8Array');
108108
});
109109
});
110110

src/__tests__/Mask.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('create new masks', () => {
1010
width: 10,
1111
height: 20,
1212
size: 200,
13-
depth: 1,
13+
bitDepth: 1,
1414
colorModel: 'BINARY',
1515
components: 1,
1616
channels: 1,

src/__tests__/__snapshots__/Image.test.ts.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`check custom inspect with image too large 1`] = `
44
"Image {
55
width: 25
66
height: 25
7-
depth: 8
7+
bitDepth: 8
88
colorModel: GREYA
99
channels: 2
1010
data: [...]
@@ -15,7 +15,7 @@ exports[`check custom inspect, GREYA image 1`] = `
1515
"Image {
1616
width: 2
1717
height: 2
18-
depth: 8
18+
bitDepth: 8
1919
colorModel: GREYA
2020
channels: 2
2121
data: {
@@ -31,7 +31,7 @@ exports[`check custom inspect, RGB image 1`] = `
3131
"Image {
3232
width: 1
3333
height: 2
34-
depth: 8
34+
bitDepth: 8
3535
colorModel: RGB
3636
channels: 3
3737
data: {

src/compare/__tests__/computeSsim.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ test('windowSize too large error', async () => {
112112
const other = image;
113113
expect(() => {
114114
computeSsim(image, other, { windowSize: 5 });
115-
}).toThrow('ssim: windowSize cannot exceed image dimensions');
115+
}).toThrow('windowSize cannot exceed image dimensions');
116116
expect(() => {
117117
computeSsim(image, other, { windowSize: 20 });
118-
}).toThrow('ssim: windowSize cannot exceed image dimensions');
118+
}).toThrow('windowSize cannot exceed image dimensions');
119119
});

src/compare/__tests__/subtract.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ test('different alpha should throw', async () => {
5555
const other = testUtils.createRgbImage([[1, 1, 1, 5, 5, 5, 10, 10, 10]]);
5656
expect(() => {
5757
image.subtract(other);
58-
}).toThrow(`subtract: both images must have the same alpha and depth`);
58+
}).toThrow(`subtract: both images must have the same alpha and bitDepth`);
5959
});
6060

6161
test('different number of channels should throw', async () => {

src/compare/computeSsim.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function computeSsim(
5353

5454
if (windowSize) {
5555
if (windowSize > image.width || windowSize > image.height) {
56-
throw new Error('ssim: windowSize cannot exceed image dimensions');
56+
throw new Error('windowSize cannot exceed image dimensions');
5757
}
5858
} else {
5959
windowSize = Math.min(11, image.height, image.width);

src/draw/__tests__/drawCircleOnImage.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ test('negative radius error', () => {
117117
image.drawCircle(center, radius, {
118118
color: [1],
119119
});
120-
}).toThrow('drawCircle: circle radius must be positive');
120+
}).toThrow('circle radius must be positive');
121121
});
122122
test('non-integer radius error', () => {
123123
const image = testUtils.createGreyImage([
@@ -131,7 +131,7 @@ test('non-integer radius error', () => {
131131
image.drawCircle(center, radius, {
132132
color: [1],
133133
});
134-
}).toThrow('drawCircle: radius must be an integer');
134+
}).toThrow('circle radius must be an integer');
135135
});
136136

137137
test('draw grey filled circle, radius=0', () => {

src/draw/drawCircleOnImage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ export function drawCircleOnImage(
5454
});
5555

5656
if (!Number.isInteger(radius)) {
57-
throw new Error('drawCircle: radius must be an integer');
57+
throw new Error('circle radius must be an integer');
5858
}
5959

6060
if (radius < 0) {
61-
throw new Error('drawCircle: circle radius must be positive');
61+
throw new Error('circle radius must be positive');
6262
}
63+
6364
if (radius === 0) {
6465
newImage.setVisiblePixel(center.column, center.row, color);
6566
return newImage;

0 commit comments

Comments
 (0)