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