Skip to content

Commit e1287ca

Browse files
committed
Created data-sink interface + image-writer implementation
1 parent d37a149 commit e1287ca

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* A DataSink should provide methods to store data
3+
*
4+
* @interface DataSink
5+
*/
6+
export interface DataSink {
7+
/**
8+
* store will write data to disk
9+
* @param data Data to write
10+
* @param path Absolute output file path
11+
*/
12+
store(data: any, path: string): Promise<any>;
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { existsSync, unlinkSync } from "fs";
2+
import { resolve } from "path";
3+
import { ImageReader } from "./image-reader.class";
4+
import { ImageWriter } from "./image-writer.class";
5+
6+
const INPUT_PATH = resolve(__dirname, "./__mocks__/mouse.png");
7+
const OUTPUT_PATH_PNG = resolve(__dirname, "./__mocks__/output.png");
8+
const OUTPUT_PATH_JPG = resolve(__dirname, "./__mocks__/output.jpg");
9+
10+
beforeEach(() => {
11+
for (const file of [OUTPUT_PATH_JPG, OUTPUT_PATH_PNG]) {
12+
if (existsSync(file)) {
13+
unlinkSync(file);
14+
}
15+
}
16+
});
17+
18+
describe.each([[OUTPUT_PATH_PNG], [OUTPUT_PATH_JPG]])(
19+
"Image writer", (outputPath) => {
20+
test("should allow to store image data to disk", async () => {
21+
// GIVEN
22+
const imageReader = new ImageReader();
23+
const image = await imageReader.load(INPUT_PATH);
24+
const imageWriter = new ImageWriter();
25+
26+
// WHEN
27+
await imageWriter.store(image, outputPath);
28+
29+
// THEN
30+
expect(existsSync(outputPath)).toBeTruthy();
31+
});
32+
});
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 { DataSink } from "./data-sink.interface";
4+
import { ImageProcessor } from "./image-processor.class";
5+
6+
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);
13+
}
14+
return cv.imwriteAsync(path, outputMat);
15+
}
16+
}

0 commit comments

Comments
 (0)