File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments