Skip to content

Commit 4f62a6a

Browse files
committed
DataSource interface, ImageLoader class and test
1 parent 7f75cdf commit 4f62a6a

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* A DataSource should provide methods to load data
3+
*
4+
* @interface DataSource
5+
*/
6+
export interface DataSource {
7+
/**
8+
* load will load image data from disk
9+
* @param path Absolute path to output file
10+
*/
11+
load(path: string): Promise<any>;
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as path from "path";
2+
import { ImageLoader } from "./image-loader.class";
3+
4+
describe("Image loader", () => {
5+
it("should resolve to a non-empty Mat on successful load", async () => {
6+
// GIVEN
7+
const SUT = new ImageLoader();
8+
const imagePath = path.resolve(__dirname, "./__mocks__/mouse.png");
9+
10+
// WHEN
11+
const result = await SUT.load(imagePath);
12+
13+
// THEN
14+
expect(result.height).toBeGreaterThan(0);
15+
expect(result.width).toBeGreaterThan(0);
16+
});
17+
18+
it("loadImage should reject on unsuccessful load", async () => {
19+
// GIVEN
20+
const SUT = new ImageLoader();
21+
const imagePath = "./__mocks__/foo.png";
22+
23+
// WHEN
24+
const call = SUT.load;
25+
26+
// THEN
27+
await expect(call(imagePath)).rejects.toEqual(`Failed to load image from '${imagePath}'`);
28+
});
29+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as cv from "opencv4nodejs";
2+
import { Image } from "../../image.class";
3+
import { DataSource } from "./data-source.interface";
4+
5+
export class ImageLoader implements DataSource {
6+
public async load(path: string): Promise<Image> {
7+
return new Promise<Image>(async (resolve, reject) => {
8+
try {
9+
const image = await cv.imreadAsync(path);
10+
resolve(new Image(image.cols, image.rows, image.getData(), image.channels));
11+
} catch (e) {
12+
reject(`Failed to load image from '${path}'`);
13+
}
14+
});
15+
}
16+
}

0 commit comments

Comments
 (0)