1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.imaging.operations.cropresize; |
35 |
|
|
36 |
|
import static org.junit.Assert.assertEquals; |
37 |
|
|
38 |
|
import info.magnolia.imaging.AbstractImagingTest; |
39 |
|
|
40 |
|
import java.awt.image.BufferedImage; |
41 |
|
import java.io.IOException; |
42 |
|
|
43 |
|
import javax.imageio.ImageIO; |
44 |
|
|
45 |
|
import org.junit.Test; |
46 |
|
|
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
|
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (191) |
Complexity: 17 |
Complexity Density: 0.1 |
|
52 |
|
public class AutoCropAndResizeTest extends AbstractImagingTest { |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
54 |
1 |
@Test... |
55 |
|
public void testJustResizeIfTargetRatioIsEquivalentToSourceRatio() throws Exception { |
56 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
57 |
1 |
op.setTargetWidth(400); |
58 |
1 |
op.setTargetHeight(300); |
59 |
|
|
60 |
1 |
final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null); |
61 |
1 |
assertEquals(1600, coords.getWidth()); |
62 |
1 |
assertEquals(1200, coords.getHeight()); |
63 |
1 |
assertEquals(0, coords.getX1()); |
64 |
1 |
assertEquals(1600, coords.getX2()); |
65 |
1 |
assertEquals(0, coords.getY1()); |
66 |
1 |
assertEquals(1200, coords.getY2()); |
67 |
|
|
68 |
1 |
final BufferedImage res = op.apply(getHorizontalTestImage(), null); |
69 |
1 |
write(res); |
70 |
1 |
assertEquals(400, res.getWidth()); |
71 |
1 |
assertEquals(300, res.getHeight()); |
72 |
|
} |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
74 |
1 |
@Test... |
75 |
|
public void testRespectsRatioOfTargetSizeIfCroppingIsNeededAndCropFromTheCenter() throws Exception { |
76 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
77 |
1 |
op.setTargetWidth(400); |
78 |
1 |
op.setTargetHeight(225); |
79 |
|
|
80 |
1 |
final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null); |
81 |
1 |
assertEquals(1600, coords.getWidth()); |
82 |
1 |
assertEquals(900, coords.getHeight()); |
83 |
1 |
assertEquals(0, coords.getX1()); |
84 |
1 |
assertEquals(1600, coords.getX2()); |
85 |
1 |
assertEquals(150, coords.getY1()); |
86 |
1 |
assertEquals(1050, coords.getY2()); |
87 |
|
|
88 |
1 |
final BufferedImage res = op.apply(getHorizontalTestImage(), null); |
89 |
1 |
write(res); |
90 |
1 |
assertEquals(400, res.getWidth()); |
91 |
1 |
assertEquals(225, res.getHeight()); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
94 |
1 |
@Test... |
95 |
|
public void testVerticalRatiosAlsoWork() throws Exception { |
96 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
97 |
1 |
op.setTargetWidth(200); |
98 |
1 |
op.setTargetHeight(600); |
99 |
|
|
100 |
1 |
final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null); |
101 |
1 |
assertEquals(400, coords.getWidth()); |
102 |
1 |
assertEquals(1200, coords.getHeight()); |
103 |
1 |
assertEquals(600, coords.getX1()); |
104 |
1 |
assertEquals(1000, coords.getX2()); |
105 |
1 |
assertEquals(0, coords.getY1()); |
106 |
1 |
assertEquals(1200, coords.getY2()); |
107 |
|
|
108 |
1 |
final BufferedImage res = op.apply(getHorizontalTestImage(), null); |
109 |
1 |
write(res); |
110 |
1 |
assertEquals(200, res.getWidth()); |
111 |
1 |
assertEquals(600, res.getHeight()); |
112 |
|
} |
113 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
114 |
1 |
@Test... |
115 |
|
public void testDoesNotCropIfOnlyTargetHeightIsSpecifiedAndRespectsOriginalRatio() throws Exception { |
116 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
117 |
1 |
op.setTargetWidth(0); |
118 |
1 |
op.setTargetHeight(300); |
119 |
|
|
120 |
1 |
final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null); |
121 |
1 |
assertEquals(1600, coords.getWidth()); |
122 |
1 |
assertEquals(1200, coords.getHeight()); |
123 |
1 |
assertEquals(0, coords.getX1()); |
124 |
1 |
assertEquals(1600, coords.getX2()); |
125 |
1 |
assertEquals(0, coords.getY1()); |
126 |
1 |
assertEquals(1200, coords.getY2()); |
127 |
|
|
128 |
1 |
final BufferedImage res = op.apply(getHorizontalTestImage(), null); |
129 |
1 |
write(res); |
130 |
1 |
assertEquals(400, res.getWidth()); |
131 |
1 |
assertEquals(300, res.getHeight()); |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
134 |
1 |
@Test... |
135 |
|
public void testDoesNotCropIfOnlyTargetWidthIsSpecifiedAndRespectsOriginalRatio() throws Exception { |
136 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
137 |
1 |
op.setTargetWidth(400); |
138 |
1 |
op.setTargetHeight(0); |
139 |
|
|
140 |
1 |
final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null); |
141 |
1 |
assertEquals(1600, coords.getWidth()); |
142 |
1 |
assertEquals(1200, coords.getHeight()); |
143 |
1 |
assertEquals(0, coords.getX1()); |
144 |
1 |
assertEquals(1600, coords.getX2()); |
145 |
1 |
assertEquals(0, coords.getY1()); |
146 |
1 |
assertEquals(1200, coords.getY2()); |
147 |
|
|
148 |
1 |
final BufferedImage res = op.apply(getHorizontalTestImage(), null); |
149 |
1 |
write(res); |
150 |
1 |
assertEquals(400, res.getWidth()); |
151 |
1 |
assertEquals(300, res.getHeight()); |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
157 |
1 |
@Test... |
158 |
|
public void testSquareTargetUsesLargestPossibleZoneForHorizontalSource() throws Exception { |
159 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
160 |
1 |
op.setTargetWidth(400); |
161 |
1 |
op.setTargetHeight(400); |
162 |
|
|
163 |
1 |
final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null); |
164 |
1 |
assertEquals(1200, coords.getWidth()); |
165 |
1 |
assertEquals(1200, coords.getHeight()); |
166 |
1 |
assertEquals(200, coords.getX1()); |
167 |
1 |
assertEquals(1400, coords.getX2()); |
168 |
1 |
assertEquals(0, coords.getY1()); |
169 |
1 |
assertEquals(1200, coords.getY2()); |
170 |
|
|
171 |
1 |
final BufferedImage res = op.apply(getHorizontalTestImage(), null); |
172 |
1 |
write(res); |
173 |
1 |
assertEquals(400, res.getWidth()); |
174 |
1 |
assertEquals(400, res.getHeight()); |
175 |
|
} |
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
177 |
1 |
@Test... |
178 |
|
public void testSquareTargetUsesLargestPossibleZoneForVerticalSourceToo() throws Exception { |
179 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
180 |
1 |
op.setTargetWidth(400); |
181 |
1 |
op.setTargetHeight(400); |
182 |
|
|
183 |
1 |
final Coords coords = op.getCroopCoords(getVerticalTestImage(), null); |
184 |
1 |
assertEquals(1200, coords.getWidth()); |
185 |
1 |
assertEquals(1200, coords.getHeight()); |
186 |
1 |
assertEquals(0, coords.getX1()); |
187 |
1 |
assertEquals(1200, coords.getX2()); |
188 |
1 |
assertEquals(200, coords.getY1()); |
189 |
1 |
assertEquals(1400, coords.getY2()); |
190 |
|
|
191 |
1 |
final BufferedImage res = op.apply(getVerticalTestImage(), null); |
192 |
1 |
write(res); |
193 |
1 |
assertEquals(400, res.getWidth()); |
194 |
1 |
assertEquals(400, res.getHeight()); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
200 |
1 |
@Test... |
201 |
|
public void testVerticalSourceIsHandledJustAsWell() throws Exception { |
202 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
203 |
1 |
op.setTargetWidth(400); |
204 |
1 |
op.setTargetHeight(300); |
205 |
|
|
206 |
1 |
final Coords coords = op.getCroopCoords(getVerticalTestImage(), null); |
207 |
1 |
assertEquals(1200, coords.getWidth()); |
208 |
1 |
assertEquals(900, coords.getHeight()); |
209 |
1 |
assertEquals(0, coords.getX1()); |
210 |
1 |
assertEquals(1200, coords.getX2()); |
211 |
1 |
assertEquals(350, coords.getY1()); |
212 |
1 |
assertEquals(1250, coords.getY2()); |
213 |
|
|
214 |
1 |
final BufferedImage res = op.apply(getVerticalTestImage(), null); |
215 |
1 |
write(res); |
216 |
1 |
assertEquals(400, res.getWidth()); |
217 |
1 |
assertEquals(300, res.getHeight()); |
218 |
|
} |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
220 |
1 |
@Test... |
221 |
|
public void testVerticalSourceWithVerticalRatioIsAlsoSwell() throws Exception { |
222 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
223 |
1 |
op.setTargetWidth(150); |
224 |
1 |
op.setTargetHeight(600); |
225 |
|
|
226 |
1 |
final Coords coords = op.getCroopCoords(getVerticalTestImage(), null); |
227 |
1 |
assertEquals(400, coords.getWidth()); |
228 |
1 |
assertEquals(1600, coords.getHeight()); |
229 |
1 |
assertEquals(400, coords.getX1()); |
230 |
1 |
assertEquals(800, coords.getX2()); |
231 |
1 |
assertEquals(0, coords.getY1()); |
232 |
1 |
assertEquals(1600, coords.getY2()); |
233 |
|
|
234 |
1 |
final BufferedImage res = op.apply(getVerticalTestImage(), null); |
235 |
1 |
write(res); |
236 |
1 |
assertEquals(150, res.getWidth()); |
237 |
1 |
assertEquals(600, res.getHeight()); |
238 |
|
} |
239 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
240 |
1 |
@Test... |
241 |
|
public void testPhilippSawBlackBorders() throws Exception { |
242 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
243 |
1 |
op.setTargetWidth(150); |
244 |
1 |
op.setTargetHeight(100); |
245 |
|
|
246 |
1 |
final BufferedImage src = generateArbitraryTestImage(400, 200); |
247 |
|
|
248 |
1 |
final Coords coords = op.getCroopCoords(src, null); |
249 |
1 |
assertEquals(300, coords.getWidth()); |
250 |
1 |
assertEquals(200, coords.getHeight()); |
251 |
1 |
assertEquals(50, coords.getX1()); |
252 |
1 |
assertEquals(350, coords.getX2()); |
253 |
1 |
assertEquals(0, coords.getY1()); |
254 |
1 |
assertEquals(200, coords.getY2()); |
255 |
|
|
256 |
1 |
final BufferedImage res = op.apply(src, null); |
257 |
1 |
write(res); |
258 |
1 |
assertEquals(150, res.getWidth()); |
259 |
1 |
assertEquals(100, res.getHeight()); |
260 |
|
} |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
262 |
1 |
@Test... |
263 |
|
public void testThereWereVerticalBlackBordersToo() throws Exception { |
264 |
1 |
final AutoCropAndResize op = new AutoCropAndResize(); |
265 |
1 |
op.setTargetWidth(100); |
266 |
1 |
op.setTargetHeight(150); |
267 |
|
|
268 |
1 |
final BufferedImage src = generateArbitraryTestImage(200, 400); |
269 |
|
|
270 |
1 |
final Coords coords = op.getCroopCoords(src, null); |
271 |
1 |
assertEquals(200, coords.getWidth()); |
272 |
1 |
assertEquals(300, coords.getHeight()); |
273 |
1 |
assertEquals(0, coords.getX1()); |
274 |
1 |
assertEquals(200, coords.getX2()); |
275 |
1 |
assertEquals(50, coords.getY1()); |
276 |
1 |
assertEquals(350, coords.getY2()); |
277 |
|
|
278 |
1 |
final BufferedImage res = op.apply(src, null); |
279 |
1 |
write(res); |
280 |
1 |
assertEquals(100, res.getWidth()); |
281 |
1 |
assertEquals(150, res.getHeight()); |
282 |
|
} |
283 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
284 |
1 |
@Test... |
285 |
|
public void testNoResizeIfWidthAndHeightAreNotSpecified() throws IOException { |
286 |
1 |
doResizeTest(-1, -1, 16, 8); |
287 |
1 |
doResizeTest(0, 0, 16, 8); |
288 |
1 |
doResizeTest(0, 0, new Coords(5, 6, 10, 18), 5, 12); |
289 |
|
} |
290 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
291 |
1 |
@Test... |
292 |
|
public void testResizesProportionallyIfOnlyWidthIsSpecified() throws IOException { |
293 |
1 |
doResizeTest(50, -1, 50, 25); |
294 |
1 |
doResizeTest(8, -1, 8, 4); |
295 |
|
} |
296 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
297 |
1 |
@Test... |
298 |
|
public void testResizesProportionallyIfOnlyHeightIsSpecified() throws IOException { |
299 |
1 |
doResizeTest(-1, 50, 100, 50); |
300 |
1 |
doResizeTest(-1, 6, 12, 6); |
301 |
|
} |
302 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
303 |
1 |
@Test... |
304 |
|
public void testResizesUsingBothWidthAndHeightIfSpecified() throws IOException { |
305 |
1 |
doResizeTest(30, 30, 30, 30); |
306 |
1 |
doResizeTest(20, 50, 20, 50); |
307 |
|
} |
308 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
309 |
8 |
private void doResizeTest(int targetWidth, int targetHeight, int expectedWidth, int expectedHeight) throws IOException {... |
310 |
8 |
doResizeTest(targetWidth, targetHeight, new Coords(0, 0, 16, 8), expectedWidth, expectedHeight); |
311 |
|
} |
312 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
313 |
9 |
private void doResizeTest(int targetWidth, int targetHeight, Coords cropCoords, int expectedWidth, int expectedHeight) throws IOException {... |
314 |
9 |
final BufferedImage dummyImg = ImageIO.read(getClass().getResourceAsStream("/funnel.gif")); |
315 |
9 |
final AutoCropAndResize op = new AutoCropAndResize(); |
316 |
9 |
op.setTargetWidth(targetWidth); |
317 |
9 |
op.setTargetHeight(targetHeight); |
318 |
9 |
final Size effectiveTargetSize = op.getEffectiveTargetSize(dummyImg, cropCoords, null); |
319 |
9 |
final BufferedImage result = op.resize(dummyImg, cropCoords, effectiveTargetSize); |
320 |
9 |
assertEquals(expectedWidth, result.getWidth()); |
321 |
9 |
assertEquals(expectedHeight, result.getHeight()); |
322 |
|
} |
323 |
|
|
324 |
|
} |