Skip to content

Commit

Permalink
Added custom method to compare images.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Aug 22, 2024
1 parent c52909e commit 666bc2f
Show file tree
Hide file tree
Showing 26 changed files with 120 additions and 143 deletions.
25 changes: 25 additions & 0 deletions tests/custom-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { IMagickImage } from '@src/magick-image';
import { IMagickColor } from '@src/magick-color';
import { MagickFormat } from '@src/enums/magick-format';
Expand All @@ -15,11 +16,35 @@ interface MatcherResult {
}

export interface ICustomMatchers {
toEqualImage: (other: IMagickImage, expectedDistortion?: number) => void;
toHavePixelWithColor: (x: number, y: number, colorOrString: IMagickColor | string) => void;
toNotBeUnknown: (message: string) => void;
}

export const CustomMatchers = {
toEqualImage: ((image: IMagickImage, other: IMagickImage, expectedDistortion: number = 0) => {
const distortion = image.compare(other, ErrorMetric.RootMeanSquared);
if (expectedDistortion === 0) {
if (distortion !== 0) {
return {
pass: false,
message: () => 'Excepted images to be equal.'
};
}
} else {
const expectedDistortionString = expectedDistortion.toFixed(5).toString();
const distortionString = distortion.toFixed(5);
if (distortionString !== expectedDistortionString) {
return {
pass: false,
message: () => `Excepted ${distortionString} to be ${expectedDistortionString}.`
};
}
}

return { pass: true, message: () => '' }
}) as () => MatcherResult,

toHavePixelWithColor: ((image: IMagickImage, x: number, y: number, colorOrString: IMagickColor | string) => {
const actualColor = pixelColor(image, x, y);
const expectedColor = colorOrString.toString();
Expand Down
3 changes: 1 addition & 2 deletions tests/magick-image-collection/complex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import { ComplexOperator } from '@src/enums/complex-operator';
import { ErrorMetric } from '@src/enums/error-metric';
import { ComplexSettings } from '@src/settings/complex-settings';
import { TestImages } from '@test/test-images';

Expand All @@ -25,7 +24,7 @@ describe('MagickImageCollection#complex', () => {

TestImages.roseSparkleGif.use(images => {
images.complex(settings, (image) => {
expect(image.compare(images[0], ErrorMetric.RootMeanSquared)).toBeCloseTo(0.56843);
expect(image).toEqualImage(images[0], 0.56843);
});
});
});
Expand Down
4 changes: 1 addition & 3 deletions tests/magick-image-collection/flatten.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { MagickFormat } from '@src/enums/magick-format';
import { TestImages } from '@test/test-images';

Expand All @@ -23,8 +22,7 @@ describe('MagickImageCollection#flatten', () => {
expect(image.width).toBe(70);
expect(image.height).toBe(46);

const difference = images[0].compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.11919);
expect(image).toEqualImage(images[0], 0.11920);
});
});
});
Expand Down
4 changes: 1 addition & 3 deletions tests/magick-image-collection/merge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { TestImages } from '@test/test-images';

describe('MagickImageCollection#merge', () => {
Expand All @@ -25,8 +24,7 @@ describe('MagickImageCollection#merge', () => {
expect(image.width).toBe(imageMagickJpg.width);
expect(image.height).toBe(imageMagickJpg.height);

const difference = roses[0].compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.27456);
expect(image).toEqualImage(roses[0], 0.23778);
});
});
});
Expand Down
4 changes: 1 addition & 3 deletions tests/magick-image-collection/montage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { MagickGeometry } from '@src/types/magick-geometry';
import { MontageSettings } from '@src/settings/montage-settings';
import { TestImages } from '@test/test-images';
Expand Down Expand Up @@ -31,8 +30,7 @@ describe('MagickImageCollection#montage', () => {
expect(image.width).toBe(504);
expect(image.height).toBe(166);

const difference = images[0].compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.56318);
expect(image).toEqualImage(images[0], 0.56319);
});
});
});
Expand Down
4 changes: 1 addition & 3 deletions tests/magick-image-collection/mosaic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { MagickFormat } from '@src/enums/magick-format';
import { MagickGeometry } from '@src/types/magick-geometry';
import { TestImages } from '@test/test-images';
Expand All @@ -26,8 +25,7 @@ describe('MagickImageCollection#mosaic', () => {
expect(image.width).toBe(170);
expect(image.height).toBe(146);

const difference = images[0].compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.28644);
expect(image).toEqualImage(images[0], 0.28644);
});
});
});
Expand Down
7 changes: 3 additions & 4 deletions tests/magick-image-collection/quantize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { QuantizeSettings } from '@src/settings/quantize-settings';
import { TestImages } from '@test/test-images';

Expand Down Expand Up @@ -37,9 +36,9 @@ describe('MagickImageCollection#flatten', () => {
expect(result).not.toBeNull();

TestImages.roseSparkleGif.use(original => {
expect(images[0].compare(original[0], ErrorMetric.RootMeanSquared)).toBeCloseTo(0.18152);
expect(images[1].compare(original[1], ErrorMetric.RootMeanSquared)).toBeCloseTo(0.14315);
expect(images[2].compare(original[2], ErrorMetric.RootMeanSquared)).toBeCloseTo(0.14822);
expect(images[0]).toEqualImage(original[0], 0.18152);
expect(images[1]).toEqualImage(original[1], 0.14315);
expect(images[2]).toEqualImage(original[2], 0.14823);
});
});
});
Expand Down
3 changes: 1 addition & 2 deletions tests/magick-image/adaptive-resize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { MagickGeometry } from '@src/types/magick-geometry';
import { TestImages } from '@test/test-images';

Expand Down Expand Up @@ -43,7 +42,7 @@ describe('MagickImage#adaptiveResize', () => {
expect(image.height).toBe(300);

TestImages.Builtin.logo.use((original) => {
expect(image.compare(original, ErrorMetric.RootMeanSquared)).toBeCloseTo(0.31492);
expect(image).toEqualImage(original, 0.31492);
});
});
});
Expand Down
10 changes: 3 additions & 7 deletions tests/magick-image/brightness-contrast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { Percentage } from '@src/types/percentage';
import { TestImages } from '@test/test-images';

Expand All @@ -13,8 +12,7 @@ describe('MagickImage#brightnessContrast', () => {
image.clone(other => {
other.brightnessContrast(new Percentage(0), new Percentage(0));

const difference = other.compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBe(0);
expect(other).toEqualImage(image);
});
});
});
Expand All @@ -24,8 +22,7 @@ describe('MagickImage#brightnessContrast', () => {
image.clone(other => {
other.brightnessContrast(new Percentage(50), new Percentage(0));

const difference = other.compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.18056);
expect(other).toEqualImage(image, 0.18096);
});
});
});
Expand All @@ -35,8 +32,7 @@ describe('MagickImage#brightnessContrast', () => {
image.clone(other => {
other.brightnessContrast(new Percentage(50), new Percentage(100));

const difference = other.compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.278);
expect(other).toEqualImage(image, 0.27885);
});
});
});
Expand Down
7 changes: 2 additions & 5 deletions tests/magick-image/charcoal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { TestImages } from '@test/test-images';

describe('MagickImage#charcoal', () => {
Expand All @@ -13,8 +12,7 @@ describe('MagickImage#charcoal', () => {
image.charcoal();
other.charcoal(0.0, 1.0);

const difference = other.compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBe(0);
expect(other).toEqualImage(image);
});
});
});
Expand All @@ -24,8 +22,7 @@ describe('MagickImage#charcoal', () => {
image.clone(other => {
other.charcoal(4, 2);

const difference = other.compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.31);
expect(other).toEqualImage(image, 0.31179);
});
});
});
Expand Down
24 changes: 12 additions & 12 deletions tests/magick-image/chromaticity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ describe('MagickImage#chromaBluePrimary', () => {
TestImages.redPng.use(image => {
const chromaticity = image.chromaticity;

expect(chromaticity.red.x).toBeCloseTo(0.64);
expect(chromaticity.red.y).toBeCloseTo(0.33);
expect(chromaticity.red.z).toBeCloseTo(0.03);
expect(chromaticity.green.x).toBeCloseTo(0.30);
expect(chromaticity.green.y).toBeCloseTo(0.60);
expect(chromaticity.green.z).toBeCloseTo(0.1);
expect(chromaticity.blue.x).toBeCloseTo(0.15);
expect(chromaticity.blue.y).toBeCloseTo(0.06);
expect(chromaticity.blue.z).toBeCloseTo(0.79);
expect(chromaticity.white.x).toBeCloseTo(0.31);
expect(chromaticity.white.y).toBeCloseTo(0.33);
expect(chromaticity.white.z).toBeCloseTo(0.36);
expect(chromaticity.red.x).toBeCloseTo(0.64000, 4);
expect(chromaticity.red.y).toBeCloseTo(0.33000, 4);
expect(chromaticity.red.z).toBeCloseTo(0.030000, 4);
expect(chromaticity.green.x).toBeCloseTo(0.30000, 4);
expect(chromaticity.green.y).toBeCloseTo(0.60000, 4);
expect(chromaticity.green.z).toBeCloseTo(0.10000, 4);
expect(chromaticity.blue.x).toBeCloseTo(0.15000, 4);
expect(chromaticity.blue.y).toBeCloseTo(0.06000, 4);
expect(chromaticity.blue.z).toBeCloseTo(0.79000, 4);
expect(chromaticity.white.x).toBeCloseTo(0.31270, 4);
expect(chromaticity.white.y).toBeCloseTo(0.32900, 4);
expect(chromaticity.white.z).toBeCloseTo(0.35830, 4);
});
});

Expand Down
9 changes: 4 additions & 5 deletions tests/magick-image/clahe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { Percentage } from '@src/types/percentage';
import { TestImages } from '@test/test-images';

Expand All @@ -12,8 +11,8 @@ describe('MagickImage#clache', () => {
TestImages.Builtin.logo.use(image => {
image.clone(clone => {
clone.clahe(50, 100, 128, 3);
const difference = clone.compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.017);

expect(image).toEqualImage(clone, 0.01756);
});
});
});
Expand All @@ -22,8 +21,8 @@ describe('MagickImage#clache', () => {
TestImages.Builtin.logo.use(image => {
image.clone(clone => {
clone.clahe(new Percentage(50), new Percentage(10), 128, 3);
const difference = clone.compare(image, ErrorMetric.RootMeanSquared);
expect(difference).toBeCloseTo(0.012);

expect(image).toEqualImage(clone, 0.01242);
});
});
});
Expand Down
Loading

0 comments on commit 666bc2f

Please sign in to comment.