Skip to content

Commit

Permalink
Added more unit tests for ChannelPerceptualHash.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Jan 26, 2025
1 parent 01810d6 commit 5c6c2e5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/statistics/channel-perceptual-hash/hu-phash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

import { ColorSpace } from '@src/enums/color-space';
import { PixelChannel } from '@src/enums/pixel-channel';
import { TestFiles } from '@test/test-files';

describe('ChannelPerceptualHash#huPhash', () => {
it('should throw error for invalid colorspace', () => {
TestFiles.Images.fujiFilmFinePixS1ProJpg.use(image => {
const phash = image.perceptualHash();

let red = phash.getChannel(PixelChannel.Red);
red = expectToNotBeNull(red);

expect(() => red.huPhash(ColorSpace.HCL, 0)).toThrowError('Invalid color space specified');
});
});

it('should throw error for invalid channel index', () => {
TestFiles.Images.fujiFilmFinePixS1ProJpg.use(image => {
const phash = image.perceptualHash();

let red = phash.getChannel(PixelChannel.Red);
red = expectToNotBeNull(red);

expect(() => red.huPhash(ColorSpace.XyY, 7)).toThrowError('Invalid index specified');
});
});

it('should return the correct value', () => {
TestFiles.Images.fujiFilmFinePixS1ProJpg.use(image => {
const phash = image.perceptualHash();

let red = phash.getChannel(PixelChannel.Red);
red = expectToNotBeNull(red);

const huPhashXyY = red.huPhash(ColorSpace.XyY, 6);
expect(huPhashXyY).toBeCloseTo(9.1503);

const huPhashHSB = red.huPhash(ColorSpace.HSB, 6);
expect(huPhashHSB).toBeCloseTo(12.000);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

import { PixelChannel } from '@src/enums/pixel-channel';
import { TestFiles } from '@test/test-files';

describe('ChannelPerceptualHash#sumSquaredDistance', () => {
it('should return the distance', () => {
TestFiles.Images.fujiFilmFinePixS1ProJpg.use(image => {
const phash = image.perceptualHash();

let red = phash.getChannel(PixelChannel.Red);
let green = phash.getChannel(PixelChannel.Green);
red = expectToNotBeNull(red);
green = expectToNotBeNull(green);

const distance = red.sumSquaredDistance(green);
expect(distance).toBeCloseTo(32.1654);
});
});
});
21 changes: 21 additions & 0 deletions tests/statistics/channel-perceptual-hash/to-string.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

import { PixelChannel } from '@src/enums/pixel-channel';
import { TestFiles } from '@test/test-files';

describe('ChannelPerceptualHash#toString', () => {
it('should return the correct value', () => {
TestFiles.Images.fujiFilmFinePixS1ProJpg.use(image => {
const phash = image.perceptualHash();

let red = phash.getChannel(PixelChannel.Red);
red = expectToNotBeNull(red);

const value = red.toString();
expect(value).toEqual('a329882b94893dc8af62621c88f098623beac1c9846fd61b208e63962ee061ab562ee0');
});
});
});

0 comments on commit 5c6c2e5

Please sign in to comment.