Skip to content

Commit 017706c

Browse files
committed
(#131) Updated usage of image-processor to exported functions
1 parent a4f0170 commit 017706c

File tree

3 files changed

+202
-202
lines changed

3 files changed

+202
-202
lines changed
+11-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as cv from "opencv4nodejs-prebuilt";
2-
import { Image } from "../../image.class";
3-
import { DataSink } from "./data-sink.interface";
4-
import { ImageProcessor } from "./image-processor.class";
2+
import {Image} from "../../image.class";
3+
import {DataSink} from "./data-sink.interface";
4+
import {fromImageWithAlphaChannel, fromImageWithoutAlphaChannel} from "./image-processor.class";
55

66
export class ImageWriter implements DataSink {
7-
public async store(data: Image, path: string): Promise<void> {
8-
let outputMat: cv.Mat;
9-
if (data.hasAlphaChannel) {
10-
outputMat = await ImageProcessor.fromImageWithAlphaChannel(data);
11-
} else {
12-
outputMat = await ImageProcessor.fromImageWithoutAlphaChannel(data);
7+
public async store(data: Image, path: string): Promise<void> {
8+
let outputMat: cv.Mat;
9+
if (data.hasAlphaChannel) {
10+
outputMat = await fromImageWithAlphaChannel(data);
11+
} else {
12+
outputMat = await fromImageWithoutAlphaChannel(data);
13+
}
14+
return cv.imwriteAsync(path, outputMat);
1315
}
14-
return cv.imwriteAsync(path, outputMat);
15-
}
1616
}
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
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";
2+
import {ImageReader} from "./image-reader.class";
3+
import {scaleImage} from "./scale-image.function";
4+
import {fromImageWithoutAlphaChannel} from "./image-processor.class";
55

66
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);
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 fromImageWithoutAlphaChannel(inputImage);
13+
const expectedWidth = Math.floor(inputMat.cols * scaleFactor);
14+
const expectedHeight = Math.floor(inputMat.rows * scaleFactor);
1515

16-
// WHEN
17-
const result = await scaleImage(inputMat, scaleFactor);
16+
// WHEN
17+
const result = await scaleImage(inputMat, scaleFactor);
1818

19-
// THEN
20-
expect(result.rows).toBe(expectedHeight);
21-
expect(result.cols).toBe(expectedWidth);
22-
});
19+
// THEN
20+
expect(result.rows).toBe(expectedHeight);
21+
expect(result.cols).toBe(expectedWidth);
22+
});
2323

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;
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 fromImageWithoutAlphaChannel(inputImage);
30+
const expectedWidth = inputMat.cols;
31+
const expectedHeight = inputMat.rows;
3232

33-
// WHEN
34-
const result = await scaleImage(inputMat, scaleFactor);
33+
// WHEN
34+
const result = await scaleImage(inputMat, scaleFactor);
3535

36-
// THEN
37-
expect(result.rows).toBe(expectedHeight);
38-
expect(result.cols).toBe(expectedWidth);
39-
});
36+
// THEN
37+
expect(result.rows).toBe(expectedHeight);
38+
expect(result.cols).toBe(expectedWidth);
39+
});
4040
});

0 commit comments

Comments
 (0)