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.resizers; |
35 |
|
|
36 |
|
import info.magnolia.imaging.operations.cropresize.Coords; |
37 |
|
import info.magnolia.imaging.operations.cropresize.Size; |
38 |
|
import info.magnolia.imaging.util.ImageUtil; |
39 |
|
|
40 |
|
import java.awt.Graphics2D; |
41 |
|
import java.awt.RenderingHints; |
42 |
|
import java.awt.image.BufferedImage; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
|
|
| 83.3% |
Uncovered Elements: 10 (60) |
Complexity: 13 |
Complexity Density: 0.35 |
|
51 |
|
public class MultiStepResizer extends BasicResizer { |
|
|
| 83.1% |
Uncovered Elements: 10 (59) |
Complexity: 13 |
Complexity Density: 0.35 |
|
52 |
6 |
@Override... |
53 |
|
public BufferedImage resize(BufferedImage src, Coords srcCoords, Size targetSize) { |
54 |
6 |
final int targetWidth = targetSize.getWidth(); |
55 |
6 |
final int targetHeight = targetSize.getHeight(); |
56 |
6 |
final int type = ImageUtil.getImageType(src); |
57 |
6 |
BufferedImage ret = src; |
58 |
6 |
int w, h; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
6 |
w = srcCoords.getWidth(); |
64 |
6 |
h = srcCoords.getHeight(); |
65 |
|
|
66 |
6 |
boolean firstStep = true; |
67 |
6 |
do { |
68 |
6 |
if (w > targetWidth) { |
69 |
3 |
w /= 2; |
70 |
3 |
if (w < targetWidth) { |
71 |
3 |
w = targetWidth; |
72 |
|
} |
73 |
3 |
} else if (w < targetWidth) { |
74 |
3 |
w = targetWidth * 2; |
75 |
3 |
if (w > targetWidth) { |
76 |
3 |
w = targetWidth; |
77 |
|
} |
78 |
|
} |
79 |
|
|
80 |
6 |
if (h > targetHeight) { |
81 |
3 |
h /= 2; |
82 |
3 |
if (h < targetHeight) { |
83 |
3 |
h = targetHeight; |
84 |
|
} |
85 |
3 |
} else if (h < targetHeight) { |
86 |
3 |
h = targetHeight * 2; |
87 |
3 |
if (h > targetHeight) { |
88 |
3 |
h = targetHeight; |
89 |
|
} |
90 |
|
} |
91 |
|
|
92 |
6 |
final BufferedImage tmp = new BufferedImage(w, h, type); |
93 |
6 |
final Graphics2D g2 = tmp.createGraphics(); |
94 |
6 |
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, getInterpolationHint()); |
95 |
6 |
if (firstStep) { |
96 |
6 |
g2.drawImage(ret, 0, 0, w, h, srcCoords.getX1(), srcCoords.getY1(), srcCoords.getX2(), srcCoords.getY2(), null); |
97 |
|
} else { |
98 |
0 |
g2.drawImage(ret, 0, 0, w, h, null); |
99 |
|
} |
100 |
6 |
g2.dispose(); |
101 |
6 |
if (ret != null) { |
102 |
|
|
103 |
6 |
ret.flush(); |
104 |
|
} |
105 |
6 |
ret = tmp; |
106 |
6 |
firstStep = false; |
107 |
6 |
} while (w != targetWidth || h != targetHeight); |
108 |
|
|
109 |
6 |
return ret; |
110 |
|
} |
111 |
|
} |