|
| 1 | +import * as path from "path"; |
| 2 | +import { ImageProcessor } from "./image-processor.class"; |
| 3 | +import { ImageReader } from "./image-reader.class"; |
| 4 | +import { scaleImage } from "./scale-image.function"; |
| 5 | + |
| 6 | +describe("scaleImage", () => { |
| 7 | + it.each([[0.5], [1.5]])("should scale an image correctly by factor %f", async (scaleFactor) => { |
| 8 | + // GIVEN |
| 9 | + const imageLoader = new ImageReader(); |
| 10 | + const pathToinput = path.resolve(__dirname, "./__mocks__/mouse.png"); |
| 11 | + const inputImage = await imageLoader.load(pathToinput); |
| 12 | + const inputMat = await ImageProcessor.fromImageWithoutAlphaChannel(inputImage); |
| 13 | + const expectedWidth = Math.floor(inputMat.cols * scaleFactor); |
| 14 | + const expectedHeight = Math.floor(inputMat.rows * scaleFactor); |
| 15 | + |
| 16 | + // WHEN |
| 17 | + const result = await scaleImage(inputMat, scaleFactor); |
| 18 | + |
| 19 | + // THEN |
| 20 | + expect(result.rows).toBe(expectedHeight); |
| 21 | + expect(result.cols).toBe(expectedWidth); |
| 22 | + }); |
| 23 | + |
| 24 | + it.each([[0], [-0.25]])("should keep scale if factor <= 0: Scale %f", async (scaleFactor) => { |
| 25 | + // GIVEN |
| 26 | + const imageLoader = new ImageReader(); |
| 27 | + const pathToinput = path.resolve(__dirname, "./__mocks__/mouse.png"); |
| 28 | + const inputImage = await imageLoader.load(pathToinput); |
| 29 | + const inputMat = await ImageProcessor.fromImageWithoutAlphaChannel(inputImage); |
| 30 | + const expectedWidth = inputMat.cols; |
| 31 | + const expectedHeight = inputMat.rows; |
| 32 | + |
| 33 | + // WHEN |
| 34 | + const result = await scaleImage(inputMat, scaleFactor); |
| 35 | + |
| 36 | + // THEN |
| 37 | + expect(result.rows).toBe(expectedHeight); |
| 38 | + expect(result.cols).toBe(expectedWidth); |
| 39 | + }); |
| 40 | +}); |
0 commit comments