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

Commit cca4975

Browse files
feat: convert image if needed when encoding (#466)
And do not allow to convert bit depth of image to 1 because only Mask are supposed to have this bit depth
1 parent 1225b4d commit cca4975

File tree

8 files changed

+46
-22
lines changed

8 files changed

+46
-22
lines changed

src/operations/__tests__/convertBitDepth.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,13 @@ test('throw if converting to same bit depth', () => {
6565
img.convertBitDepth(8);
6666
}).toThrow('cannot convert image to same bitDepth');
6767
});
68+
test('throw if converting to unsupported bit depth', () => {
69+
const img = testUtils.createRgbaImage([
70+
[256, 256, 256, 256, 512, 512, 512, 512],
71+
[768, 768, 768, 768, 1024, 1024, 1024, 1024],
72+
]);
73+
74+
expect(() => {
75+
img.convertBitDepth(1);
76+
}).toThrow('This image bit depth is not supported: 1');
77+
});

src/operations/convertBitDepth.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ export function convertBitDepth(image: Image, newBitDepth: BitDepth): Image {
1313

1414
if (newBitDepth === 16) {
1515
return convertToUint16(image);
16-
} else {
16+
} else if (newBitDepth === 8) {
1717
return convertToUint8(image);
18+
} else {
19+
throw new RangeError(
20+
`This image bit depth is not supported: ${newBitDepth}`,
21+
);
1822
}
1923
}
2024

src/save/__tests__/encodeBmp.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ test('encode 10x2 mask', () => {
3535
const buffer = encodeBmp(mask).buffer;
3636
expect(buffer).toEqual(result.buffer);
3737
});
38+
test('should throw error', () => {
39+
const image = testUtils.createGreyImage([
40+
[255, 255, 255, 0, 0, 255, 0, 255, 0, 255],
41+
[255, 0, 255, 0, 255, 0, 0, 255, 255, 255],
42+
]);
43+
44+
expect(() => encodeBmp(image)).toThrow(
45+
'Image bmp encoding is not implemented.',
46+
);
47+
});

src/save/__tests__/encodePng.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ test('should encode a 16bits image correctly', () => {
1717
expect(imgDup.bitDepth).toBe(16);
1818
expect(imgDup).toMatchImage(img);
1919
});
20+
21+
test('add mask test', () => {
22+
const img = testUtils.load('formats/tif/grey16.tif');
23+
const mask = img.threshold();
24+
const imgDup = decode(encodePng(mask));
25+
expect(imgDup.colorModel).toBe('GREY');
26+
expect(imgDup.bitDepth).toBe(8);
27+
});

src/save/encodeBmp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Mask } from '../Mask';
99
* @param mask - The mask instance.
1010
* @returns The buffer.
1111
*/
12-
export function encodeBmp(mask: Mask | Image) {
12+
export function encodeBmp(mask: Image | Mask) {
1313
if (!(mask instanceof Mask)) {
1414
throw new TypeError('Image bmp encoding is not implemented.');
1515
}

src/save/encodeJpeg.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ export function encodeJpeg(
2222
options: EncodeJpegOptions = {},
2323
): Uint8Array {
2424
const { quality = 50 } = options;
25-
if (!(image instanceof Image)) {
26-
throw new TypeError('Mask JPG/JPEG encoding is not supported.');
27-
}
28-
if (image.colorModel !== 'RGBA') {
25+
if (image.colorModel !== 'RGBA' || image instanceof Mask) {
2926
image = image.convertColor('RGBA');
3027
}
3128
if (image.bitDepth !== 8) {

src/save/encodePng.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ export function encodePng(
1515
image: Image | Mask,
1616
options?: EncodePngOptions,
1717
): Uint8Array {
18-
if (!(image instanceof Image)) {
19-
throw new TypeError('Mask PNG encoding is not supported.');
20-
}
21-
if (image.bitDepth !== 8 && image.bitDepth !== 16) {
22-
image = image.convertBitDepth(8);
23-
}
2418
if (
25-
image.colorModel !== 'RGB' &&
26-
image.colorModel !== 'RGBA' &&
27-
image.colorModel !== 'GREY' &&
28-
image.colorModel !== 'GREYA'
19+
(image.colorModel !== 'RGB' &&
20+
image.colorModel !== 'RGBA' &&
21+
image.colorModel !== 'GREY' &&
22+
image.colorModel !== 'GREYA') ||
23+
image instanceof Mask
2924
) {
3025
image = image.convertColor('GREY');
3126
}

src/save/write.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,12 @@ function getDataToWrite(
137137
): Uint8Array {
138138
if (!options || !('format' in options)) {
139139
const extension = nodePath.extname(destinationPath).slice(1).toLowerCase();
140-
if (extension === 'png' || extension === 'jpg' || extension === 'jpeg') {
141-
if (image instanceof Mask) {
142-
image = image.convertColor('GREY');
143-
}
144-
return encode(image, { ...options, format: extension });
145-
} else if (extension === 'bmp') {
140+
if (
141+
extension === 'png' ||
142+
extension === 'jpg' ||
143+
extension === 'jpeg' ||
144+
extension === 'bmp'
145+
) {
146146
return encode(image, { ...options, format: extension });
147147
} else {
148148
throw new RangeError(

0 commit comments

Comments
 (0)