1
1
import path from 'node:path' ;
2
2
3
- import { encodePng , write } from '../../save' ;
3
+ import { Image } from '../../Image' ;
4
+ import { write } from '../../save' ;
5
+
6
+ async function writeDebug ( resized : Image , type : string ) {
7
+ // @ts -expect-error Dynamic string.
8
+ const expected = testUtils . load ( `opencv/test_resize_${ type } .png` ) ;
9
+ await write ( path . join ( __dirname , `resize_${ type } _expected.png` ) , expected ) ;
10
+ await write ( path . join ( __dirname , `resize_${ type } _resized.png` ) , resized ) ;
11
+ const subtraction = expected . subtract ( resized ) ;
12
+ await write (
13
+ path . join ( __dirname , `resize_${ type } _subtraction.png` ) ,
14
+ subtraction ,
15
+ ) ;
16
+ }
4
17
5
- test ( 'compare result of resize with opencv (nearest)' , async ( ) => {
18
+ test ( 'compare with OpenCV (nearest, larger )' , async ( ) => {
6
19
const img = testUtils . load ( 'opencv/test.png' ) ;
7
20
8
21
const resized = img . resize ( {
@@ -11,62 +24,132 @@ test('compare result of resize with opencv (nearest)', async () => {
11
24
interpolationType : 'nearest' ,
12
25
} ) ;
13
26
14
- expect ( resized ) . toMatchImage ( 'opencv/testResizeNearest.png' ) ;
27
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_nearest_larger.png' ) ;
28
+ } ) ;
29
+
30
+ test ( 'compare with OpenCV (nearest, same size)' , async ( ) => {
31
+ const img = testUtils . load ( 'opencv/test.png' ) ;
32
+
33
+ const resized = img . resize ( {
34
+ xFactor : 1 ,
35
+ interpolationType : 'nearest' ,
36
+ } ) ;
37
+
38
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_nearest_same.png' ) ;
39
+ } ) ;
40
+
41
+ test ( 'compare with OpenCV (nearest, smaller)' , async ( ) => {
42
+ const img = testUtils . load ( 'opencv/test.png' ) ;
43
+
44
+ const resized = img . resize ( {
45
+ width : 5 ,
46
+ height : 6 ,
47
+ interpolationType : 'nearest' ,
48
+ } ) ;
49
+
50
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_nearest_smaller.png' ) ;
15
51
} ) ;
16
52
17
- test . skip ( 'compare result of resize with opencv (bilinear)' , async ( ) => {
53
+ test . skip ( 'compare with OpenCV (bilinear, larger )' , async ( ) => {
18
54
const img = testUtils . load ( 'opencv/test.png' ) ;
19
- const expectedImg = testUtils . load ( 'opencv/testResizeBilinear.png' ) ;
20
55
21
56
const resized = img . resize ( {
22
57
xFactor : 10 ,
23
58
yFactor : 10 ,
24
59
} ) ;
25
60
26
- const substraction = expectedImg . clone ( ) . subtract ( resized ) ;
27
- await write (
28
- path . join ( __dirname , 'resize_bilinear_substraction.png' ) ,
29
- substraction ,
30
- ) ;
31
- await write ( path . join ( __dirname , 'resize_bilinear.png' ) , resized ) ;
61
+ await writeDebug ( resized , 'bilinear_larger' ) ;
32
62
33
- expect ( resized ) . toMatchImage ( 'opencv/testResizeBilinear .png' ) ;
63
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_bilinear_larger .png' ) ;
34
64
} ) ;
35
65
36
- test ( 'result should have correct dimensions' , ( ) => {
66
+ test . skip ( 'compare with OpenCV (bilinear, same)' , async ( ) => {
67
+ const img = testUtils . load ( 'opencv/test.png' ) ;
68
+
69
+ const resized = img . resize ( {
70
+ xFactor : 1 ,
71
+ } ) ;
72
+
73
+ await writeDebug ( resized , 'bilinear_same' ) ;
74
+
75
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_bilinear_same.png' ) ;
76
+ } ) ;
77
+
78
+ test . skip ( 'compare with OpenCV (bilinear, smaller)' , async ( ) => {
79
+ const img = testUtils . load ( 'opencv/test.png' ) ;
80
+
81
+ const resized = img . resize ( {
82
+ width : 5 ,
83
+ height : 6 ,
84
+ } ) ;
85
+
86
+ await writeDebug ( resized , 'bilinear_smaller' ) ;
87
+
88
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_bilinear_smaller.png' ) ;
89
+ } ) ;
90
+
91
+ test . skip ( 'compare with OpenCV (bicubic, larger)' , async ( ) => {
37
92
const img = testUtils . load ( 'opencv/test.png' ) ;
38
93
39
94
const resized = img . resize ( {
40
95
xFactor : 10 ,
41
96
yFactor : 10 ,
97
+ interpolationType : 'bicubic' ,
42
98
} ) ;
43
- expect ( resized . width ) . toBe ( 10 * img . width ) ;
44
- expect ( resized . height ) . toBe ( 10 * img . height ) ;
99
+
100
+ await writeDebug ( resized , 'bicubic_larger' ) ;
101
+
102
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_bicubic_larger.png' ) ;
45
103
} ) ;
46
104
47
- test ( 'resize to given width and height' , ( ) => {
105
+ test . skip ( 'compare with OpenCV (bicubic, same)' , async ( ) => {
48
106
const img = testUtils . load ( 'opencv/test.png' ) ;
49
107
50
108
const resized = img . resize ( {
51
- width : 300 ,
52
- height : 100 ,
109
+ xFactor : 1 ,
110
+ interpolationType : 'bicubic' ,
53
111
} ) ;
54
112
55
- expect ( resized . width ) . toBe ( 300 ) ;
56
- expect ( resized . height ) . toBe ( 100 ) ;
113
+ await writeDebug ( resized , 'bicubic_same' ) ;
114
+
115
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_bicubic_same.png' ) ;
116
+ } ) ;
117
+
118
+ test . skip ( 'compare with OpenCV (bicubic, smaller)' , async ( ) => {
119
+ const img = testUtils . load ( 'opencv/test.png' ) ;
120
+
121
+ const resized = img . resize ( {
122
+ width : 5 ,
123
+ height : 6 ,
124
+ interpolationType : 'bicubic' ,
125
+ } ) ;
126
+
127
+ await writeDebug ( resized , 'bicubic_smaller' ) ;
128
+
129
+ expect ( resized ) . toMatchImage ( 'opencv/test_resize_bicubic_smaller.png' ) ;
57
130
} ) ;
58
131
59
- test ( 'has to match snapshot ' , ( ) => {
132
+ test ( 'result should have correct dimensions ' , ( ) => {
60
133
const img = testUtils . load ( 'opencv/test.png' ) ;
61
134
62
135
const resized = img . resize ( {
63
136
xFactor : 10 ,
64
137
yFactor : 10 ,
65
138
} ) ;
139
+ expect ( resized . width ) . toBe ( 10 * img . width ) ;
140
+ expect ( resized . height ) . toBe ( 10 * img . height ) ;
141
+ } ) ;
66
142
67
- const png = Buffer . from ( encodePng ( resized . convertColor ( 'GREY' ) ) ) ;
143
+ test ( 'resize to given width and height' , ( ) => {
144
+ const img = testUtils . load ( 'opencv/test.png' ) ;
145
+
146
+ const resized = img . resize ( {
147
+ width : 300 ,
148
+ height : 100 ,
149
+ } ) ;
68
150
69
- expect ( png ) . toMatchImageSnapshot ( ) ;
151
+ expect ( resized . width ) . toBe ( 300 ) ;
152
+ expect ( resized . height ) . toBe ( 100 ) ;
70
153
} ) ;
71
154
72
155
test ( 'aspect ratio not preserved' , ( ) => {
0 commit comments