Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@
"pako": "^2.1.0"
},
"devDependencies": {
"@types/node": "^24.0.1",
"@types/node": "^24.1.0",
"@types/pako": "^2.0.3",
"@vitest/coverage-v8": "^3.2.3",
"@zakodium/tsconfig": "^1.0.1",
"eslint": "^9.29.0",
"eslint-config-cheminfo-typescript": "^18.0.1",
"prettier": "^3.5.3",
"@vitest/coverage-v8": "^3.2.4",
"@zakodium/tsconfig": "^1.0.2",
"eslint": "^9.31.0",
"eslint-config-cheminfo-typescript": "^19.0.0",
"prettier": "^3.6.2",
"rimraf": "^6.0.1",
"typescript": "^5.8.3",
"vitest": "^3.2.3"
"vitest": "^3.2.4"
},
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/decode.lzw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('decode lzw', () => {
join(import.meta.dirname, '../../img/image.tif'),
);
const image = decode(buffer);

expect(
dataEqual(imageLzw[0].data as Uint8Array, image[0].data as Uint8Array),
).toBe(true);
Expand All @@ -32,6 +33,7 @@ describe('decode lzw', () => {
join(import.meta.dirname, '../../img/color8.tif'),
);
const image = decode(buffer);

expect(
dataEqual(imageLzw[0].data as Uint8Array, image[0].data as Uint8Array),
).toBe(true);
Expand Down
48 changes: 39 additions & 9 deletions src/__tests__/decode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,16 @@ test.each(cases)(
const result = decode(image);

expect(result).toHaveLength(file.pages ?? 1);

const { data, bitsPerSample, width, height, components, alpha } = result[0];

expect(width).toBe(file.width);
expect(height).toBe(file.height);
expect(components).toBe(file.components);
expect(bitsPerSample).toBe(file.bitsPerSample);

const size = file.width * file.height * file.components;

expect(data).toHaveLength(size);
expect(alpha).toBe(Boolean(file.alpha));
},
Expand All @@ -253,11 +257,13 @@ const expectedRgb8BitData = Uint8Array.from([

test('should decode RGB 8bit data', () => {
const [result] = decode(readImage('color-5x5.tif'));

expect(result.data).toStrictEqual(expectedRgb8BitData);
});

test('should decode RGB 8bit data with LZW compression', () => {
const [result] = decode(readImage('color-5x5-lzw.tif'));

expect(result.data).toStrictEqual(expectedRgb8BitData);
});

Expand All @@ -272,11 +278,13 @@ const expectedRgb8BitAlphaData = Uint8Array.from([

test('should decode RGB 8bit data with pre-multiplied alpha', () => {
const [result] = decode(readImage('color-alpha-5x5.tif'));

expect(result.data).toStrictEqual(expectedRgb8BitAlphaData);
});

test('should decode RGB 8bit data with pre-multiplied alpha and LZW compression', () => {
const [result] = decode(readImage('color-alpha-5x5-lzw.tif'));

expect(result.data).toStrictEqual(expectedRgb8BitAlphaData);
});

Expand All @@ -287,16 +295,19 @@ test('should decode RGB 8bit data with pre-multiplied alpha and lost precision',
128, 0, 0, 6, 128, 0, 0, 6,
]);
const [result] = decode(readImage('color-alpha-2x2.tif'));

expect(result.data).toStrictEqual(expectedData);
});

test('should decode with onlyFirst', () => {
const result = decode(readImage('grey8.tif'), { pages: [0] });

expect(result[0]).toHaveProperty('data');
});

test('should omit data', () => {
const result = decode(readImage('grey8.tif'), { ignoreImageData: true });

expect(result[0].data).toStrictEqual(new Uint8Array());
});

Expand All @@ -305,6 +316,7 @@ test('should read exif data', () => {
pages: [0],
ignoreImageData: true,
});

// @ts-expect-error We know exif is defined.
expect(result[0].exif.map).toStrictEqual({
ColorSpace: 65535,
Expand All @@ -315,7 +327,9 @@ test('should read exif data', () => {

test('should decode stacks', () => {
const decoded = decode(stack);

expect(decoded).toHaveLength(10);

for (const image of decoded) {
expect(image.width).toBe(128);
expect(image.height).toBe(128);
Expand All @@ -324,7 +338,9 @@ test('should decode stacks', () => {

test('specify pages to decode', () => {
const decoded = decode(stack, { pages: [0, 2, 4, 6, 8] });

expect(decoded).toHaveLength(5);

for (const image of decoded) {
expect(image.width).toBe(128);
expect(image.height).toBe(128);
Expand All @@ -345,15 +361,19 @@ test('should throw if pages invalid', () => {

test('should decode palette', () => {
const decoded = decode(readImage('palette.tif'));

expect(decoded).toHaveLength(1);

const { palette } = decoded[0];

expect(palette).toHaveLength(256);
// @ts-expect-error We know palette is defined.
expect(palette[0]).toStrictEqual([65535, 0, 0]);
});

test('should decode image compressed with deflate algorithm', () => {
const decoded = decode(readImage('tile_rgb_deflate.tif'));

expect(decoded).toHaveLength(1);
expect(decoded[0]).toMatchObject({
alpha: false,
Expand All @@ -364,8 +384,10 @@ test('should decode image compressed with deflate algorithm', () => {
height: 128,
});
});

test('should decode basic 2x2 1-bit image ', () => {
const decoded = decode(readImage('bw1bit.tif'));

expect(decoded).toHaveLength(1);
expect(decoded[0]).toMatchObject({
alpha: false,
Expand All @@ -375,10 +397,12 @@ test('should decode basic 2x2 1-bit image ', () => {
width: 2,
height: 2,
});
expect(decoded[0].data).toEqual(new Uint8Array([1, 0, 0, 1]));
expect(decoded[0].data).toStrictEqual(new Uint8Array([1, 0, 0, 1]));
});

test('should decode 10x10 1-bit image as a cross', () => {
const decoded = decode(readImage('bwCross.tif'));

expect(decoded).toHaveLength(1);
expect(decoded[0]).toMatchObject({
alpha: false,
Expand All @@ -388,7 +412,7 @@ test('should decode 10x10 1-bit image as a cross', () => {
width: 10,
height: 10,
});
expect(decoded[0].data).toEqual(
expect(decoded[0].data).toStrictEqual(
new Uint8Array(
[
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0],
Expand All @@ -405,8 +429,10 @@ test('should decode 10x10 1-bit image as a cross', () => {
),
);
});

test('should decode 15x15 image with tile data', () => {
const decoded = decode(readImage('crosshair_tiled.tif'));

expect(decoded).toHaveLength(1);
expect(decoded[0]).toMatchObject({
alpha: false,
Expand All @@ -416,7 +442,7 @@ test('should decode 15x15 image with tile data', () => {
width: 15,
height: 15,
});
expect(decoded[0].data).toEqual(
expect(decoded[0].data).toStrictEqual(
new Uint8Array(
[
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
Expand All @@ -441,6 +467,7 @@ test('should decode 15x15 image with tile data', () => {

test('should decode multiframe image', () => {
const decoded = decode(readImage('dog.tiff'));

expect(decoded).toHaveLength(8);
expect(decoded[0]).toMatchObject({
alpha: true,
Expand All @@ -451,22 +478,25 @@ test('should decode multiframe image', () => {
height: 16,
});

expect(decoded[0].imageWidth).toEqual(16);
expect(decoded[0].imageWidth).toBe(16);

// last row of the first frame
const firstFrameLastRow = new Uint8Array([
1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
]);
expect(decoded[1].imageLength).toEqual(16);
expect(decoded[1].imageWidth).toEqual(16);

expect(decoded[0].data.slice(960, 1024)).toEqual(firstFrameLastRow);
expect(decoded[1].imageLength).toBe(16);
expect(decoded[1].imageWidth).toBe(16);
expect(decoded[0].data.slice(960, 1024)).toStrictEqual(firstFrameLastRow);

// twelveth row of the second frame
const secondFrameTwelvethRow = new Uint8Array([
1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1, 0,
]);
expect(decoded[1].samplesPerPixel).toEqual(2);
expect(decoded[1].data.slice(352, 384)).toEqual(secondFrameTwelvethRow);

expect(decoded[1].samplesPerPixel).toBe(2);
expect(decoded[1].data.slice(352, 384)).toStrictEqual(secondFrameTwelvethRow);
});
1 change: 1 addition & 0 deletions src/__tests__/is_multi_page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const contents = files.map((file) =>
test('TIFF isMultiPage', () => {
for (let i = 0; i < contents.length; i++) {
const result = isMultiPage(contents[i]);

expect(result).toBe(files[i].pages > 1);
}
});
5 changes: 5 additions & 0 deletions src/__tests__/lzw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('lzw', () => {
const result = decompressLzw(
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength),
);

expect(result.byteLength).toBe(7770);
expect(
new Uint8Array(
Expand All @@ -20,11 +21,13 @@ describe('lzw', () => {
).reduce((sum, current) => sum + current, 0),
).toBe(675);
});

it('173', () => {
const buffer = readFileSync(join(import.meta.dirname, 'data/173.strip'));
const result = decompressLzw(
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength),
);

expect(result.byteLength).toBe(7770);
expect(
new Uint8Array(
Expand All @@ -34,11 +37,13 @@ describe('lzw', () => {
).reduce((sum, current) => sum + current, 0),
).toBe(38307);
});

it('174', () => {
const buffer = readFileSync(join(import.meta.dirname, 'data/174.strip'));
const result = decompressLzw(
new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength),
);

expect(result.byteLength).toBe(7770);
});
});
1 change: 1 addition & 0 deletions src/__tests__/page_count.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const contents = files.map((file) =>
test('TIFF pageCount', () => {
for (let i = 0; i < contents.length; i++) {
const result = pageCount(contents[i]);

expect(result).toBe(files[i].pages);
}
});
Loading