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

Commit 7a7933b

Browse files
committed
refactor: switch color depth from enum to plain numbers
Refs: #140
1 parent fd925d9 commit 7a7933b

30 files changed

+110
-120
lines changed

src/Image.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,11 @@ import {
104104
export { ImageColorModel, colorModels } from './utils/constants/colorModels';
105105

106106
export type ImageDataArray = Uint8Array | Uint16Array | Uint8ClampedArray;
107+
107108
/**
108109
* Bit depth of the image (nb of bits that encode each value in the image).
109110
*/
110-
export enum ColorDepth {
111-
UINT1 = 1,
112-
UINT8 = 8,
113-
UINT16 = 16,
114-
}
111+
export type ColorDepth = 1 | 8 | 16;
115112

116113
export enum ImageCoordinates {
117114
CENTER = 'CENTER',
@@ -125,7 +122,7 @@ export interface ImageOptions {
125122
/**
126123
* Number of bits per value in each channel.
127124
*
128-
* @default `ColorDepth.UINT8`.
125+
* @default `8`.
129126
*/
130127
depth?: ColorDepth;
131128

@@ -224,7 +221,7 @@ export class Image {
224221
options: ImageOptions = {},
225222
) {
226223
const {
227-
depth = ColorDepth.UINT8,
224+
depth = 8,
228225
data,
229226
colorModel = ImageColorModel.RGB,
230227
origin = { row: 0, column: 0 },
@@ -265,9 +262,9 @@ export class Image {
265262
this.maxValue,
266263
);
267264
} else {
268-
if (depth === ColorDepth.UINT8 && data instanceof Uint16Array) {
265+
if (depth === 8 && data instanceof Uint16Array) {
269266
throw new Error(`depth is ${depth} but data is Uint16Array`);
270-
} else if (depth === ColorDepth.UINT16 && data instanceof Uint8Array) {
267+
} else if (depth === 16 && data instanceof Uint8Array) {
271268
throw new Error(`depth is ${depth} but data is Uint8Array`);
272269
}
273270
const expectedLength = this.size * this.channels;
@@ -292,11 +289,11 @@ export class Image {
292289
options: CreateFromOptions = {},
293290
): Image {
294291
const { width = other.width, height = other.height } = options;
295-
let depth;
292+
let depth: ColorDepth;
296293
if (other instanceof Image) {
297294
depth = other.depth;
298295
} else {
299-
depth = ColorDepth.UINT8;
296+
depth = 8;
300297
}
301298
return new Image(width, height, {
302299
depth,
@@ -1070,10 +1067,10 @@ function createPixelArray(
10701067
const length = channels * size;
10711068
let arr;
10721069
switch (depth) {
1073-
case ColorDepth.UINT8:
1070+
case 8:
10741071
arr = new Uint8Array(length);
10751072
break;
1076-
case ColorDepth.UINT16:
1073+
case 16:
10771074
arr = new Uint16Array(length);
10781075
break;
10791076
default:

src/Mask.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class Mask {
163163
this.width = width;
164164
this.height = height;
165165
this.size = width * height;
166-
this.depth = ColorDepth.UINT1;
166+
this.depth = 1;
167167
this.colorModel = ImageColorModel.BINARY;
168168
this.origin = origin;
169169

src/__tests__/Image.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inspect } from 'node:util';
22

3-
import { Image, ColorDepth, ImageCoordinates } from '../Image';
3+
import { Image, ImageCoordinates } from '../Image';
44
import { ImageColorModel } from '../utils/constants/colorModels';
55

66
describe('create new images', () => {
@@ -10,7 +10,7 @@ describe('create new images', () => {
1010
width: 10,
1111
height: 20,
1212
size: 200,
13-
depth: ColorDepth.UINT8,
13+
depth: 8,
1414
colorModel: ImageColorModel.RGB,
1515
components: 3,
1616
channels: 3,
@@ -21,12 +21,12 @@ describe('create new images', () => {
2121
});
2222

2323
it('should create a 16-bit image', () => {
24-
const img = new Image(10, 20, { depth: ColorDepth.UINT16 });
24+
const img = new Image(10, 20, { depth: 16 });
2525
expect(img).toMatchObject({
2626
width: 10,
2727
height: 20,
2828
size: 200,
29-
depth: ColorDepth.UINT16,
29+
depth: 16,
3030
colorModel: ImageColorModel.RGB,
3131
components: 3,
3232
channels: 3,
@@ -39,7 +39,7 @@ describe('create new images', () => {
3939
it('should create a grey image with alpha', () => {
4040
const img = new Image(10, 20, { colorModel: ImageColorModel.GREYA });
4141
expect(img).toMatchObject({
42-
depth: ColorDepth.UINT8,
42+
depth: 8,
4343
colorModel: ImageColorModel.GREYA,
4444
components: 1,
4545
channels: 2,
@@ -79,7 +79,7 @@ describe('create new images', () => {
7979

8080
it('should throw on wrong data size', () => {
8181
const data = new Uint16Array(2);
82-
expect(() => new Image(2, 2, { data, depth: ColorDepth.UINT16 })).toThrow(
82+
expect(() => new Image(2, 2, { data, depth: 16 })).toThrow(
8383
/incorrect data size: 2. Expected 12/,
8484
);
8585
});
@@ -102,7 +102,7 @@ describe('create new images', () => {
102102
() =>
103103
new Image(2, 2, {
104104
colorModel: ImageColorModel.GREY,
105-
depth: ColorDepth.UINT16,
105+
depth: 16,
106106
data,
107107
}),
108108
).toThrow('depth is 16 but data is Uint8Array');

src/__tests__/Mask.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import util from 'node:util';
22

3-
import { ColorDepth, ImageColorModel } from '..';
3+
import { ImageColorModel } from '..';
44
import { Mask } from '../Mask';
55
import { Point } from '../utils/geometry/points';
66

@@ -11,7 +11,7 @@ describe('create new masks', () => {
1111
width: 10,
1212
height: 20,
1313
size: 200,
14-
depth: ColorDepth.UINT1,
14+
depth: 1,
1515
colorModel: ImageColorModel.BINARY,
1616
components: 1,
1717
channels: 1,

src/compare/computeSsim.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ssim as bufferSsim } from 'ssim.js';
22

3-
import { ColorDepth, Image, ImageColorModel } from '..';
3+
import { Image, ImageColorModel } from '..';
44
import checkProcessable from '../utils/checkProcessable';
55
import { validateForComparison } from '../utils/validators';
66

@@ -59,7 +59,7 @@ export function computeSsim(
5959
windowSize = Math.min(11, image.height, image.width);
6060
}
6161
checkProcessable(image, 'ssim', {
62-
bitDepth: [ColorDepth.UINT8],
62+
bitDepth: [8],
6363
channels: [1, 3, 4],
6464
});
6565

src/compare/subtract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColorDepth, Image, Mask } from '..';
1+
import { Image, Mask } from '..';
22
import checkProcessable from '../utils/checkProcessable';
33
import { validateForComparison } from '../utils/validators';
44

@@ -43,7 +43,7 @@ export function subtract(
4343

4444
if (image instanceof Image) {
4545
checkProcessable(image, 'subtract', {
46-
bitDepth: [ColorDepth.UINT1, ColorDepth.UINT8, ColorDepth.UINT16],
46+
bitDepth: [1, 8, 16],
4747
components: [1, 3],
4848
alpha: false,
4949
});

src/filters/hypotenuse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Image } from '..';
1+
import { ColorDepth, Image } from '..';
22
import checkProcessable from '../utils/checkProcessable';
33
import { validateChannels } from '../utils/validators';
44

@@ -8,7 +8,7 @@ export interface HypotenuseOptions {
88
*
99
* @default image.depth
1010
*/
11-
depth?: number;
11+
depth?: ColorDepth;
1212
/**
1313
* To which channels to apply the filter. By default all but alpha.
1414
*/

src/load/__tests__/decode.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import { decode } from '..';
2-
import { ColorDepth } from '../../Image';
32
import { ImageColorModel } from '../../utils/constants/colorModels';
43

54
test('auto decode png', async () => {
65
const buffer = testUtils.loadBuffer('formats/grey8.png');
76
expect(() => decode(buffer)).not.toThrow();
87
const decoded = decode(buffer);
9-
expect(decoded.depth).toStrictEqual(ColorDepth.UINT8);
8+
expect(decoded.depth).toStrictEqual(8);
109
expect(decoded.colorModel).toStrictEqual(ImageColorModel.GREY);
1110
});
1211

1312
test('auto decode jpeg', async () => {
1413
const buffer = testUtils.loadBuffer('formats/rgb12.jpg');
1514
expect(() => decode(buffer)).not.toThrow();
1615
const decoded = decode(buffer);
17-
expect(decoded.depth).toStrictEqual(ColorDepth.UINT8);
16+
expect(decoded.depth).toStrictEqual(8);
1817
expect(decoded.colorModel).toStrictEqual(ImageColorModel.RGBA);
1918
});
2019

2120
test('auto decode tiff', async () => {
2221
const buffer = testUtils.loadBuffer('formats/tif/grey8.tif');
2322
expect(() => decode(buffer)).not.toThrow();
2423
const decoded = decode(buffer);
25-
expect(decoded.depth).toStrictEqual(ColorDepth.UINT8);
24+
expect(decoded.depth).toStrictEqual(8);
2625
expect(decoded.colorModel).toStrictEqual(ImageColorModel.GREY);
2726
});
2827

src/load/__tests__/decodeJpeg.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { decodeJpeg } from '..';
2-
import { ColorDepth } from '../../Image';
32
import { ImageColorModel } from '../../utils/constants/colorModels';
43

54
const tests = [['grey6'], ['grey12'], ['rgb6'], ['rgb12']] as const;
@@ -8,5 +7,5 @@ test.each(tests)('should load from buffer %s', async (name) => {
87
const buffer = testUtils.loadBuffer(`formats/${name}.jpg`);
98
const img = decodeJpeg(buffer);
109
expect(img.colorModel).toBe(ImageColorModel.RGBA);
11-
expect(img.depth).toBe(ColorDepth.UINT8);
10+
expect(img.depth).toBe(8);
1211
});

src/load/__tests__/decodePng.test.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { decodePng } from '..';
2-
import { ColorDepth } from '../../Image';
32
import { ImageColorModel } from '../../utils/constants/colorModels';
43

54
const tests = [
65
// ['name', components, alpha, bitDepth]
7-
['grey8', ColorDepth.UINT8, ImageColorModel.GREY],
8-
['grey16', ColorDepth.UINT16, ImageColorModel.GREY],
9-
['greya16', ColorDepth.UINT8, ImageColorModel.GREYA],
10-
['greya32', ColorDepth.UINT16, ImageColorModel.GREYA],
11-
['rgb24', ColorDepth.UINT8, ImageColorModel.RGB],
12-
['rgb48', ColorDepth.UINT16, ImageColorModel.RGB],
13-
['rgba32', ColorDepth.UINT8, ImageColorModel.RGBA],
14-
['rgba64', ColorDepth.UINT16, ImageColorModel.RGBA],
15-
['plt-4bpp', ColorDepth.UINT8, ImageColorModel.RGB],
16-
['plt-8bpp-color', ColorDepth.UINT8, ImageColorModel.RGB],
6+
['grey8', 8, ImageColorModel.GREY],
7+
['grey16', 16, ImageColorModel.GREY],
8+
['greya16', 8, ImageColorModel.GREYA],
9+
['greya32', 16, ImageColorModel.GREYA],
10+
['rgb24', 8, ImageColorModel.RGB],
11+
['rgb48', 16, ImageColorModel.RGB],
12+
['rgba32', 8, ImageColorModel.RGBA],
13+
['rgba64', 16, ImageColorModel.RGBA],
14+
['plt-4bpp', 8, ImageColorModel.RGB],
15+
['plt-8bpp-color', 8, ImageColorModel.RGB],
1716
] as const;
1817

1918
test.each(tests)(

0 commit comments

Comments
 (0)