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.util; |
35 |
|
|
36 |
|
import static org.junit.Assert.*; |
37 |
|
import static org.mockito.Mockito.*; |
38 |
|
|
39 |
|
import info.magnolia.imaging.AbstractImagingTest; |
40 |
|
import info.magnolia.imaging.operations.ImageOperationChain; |
41 |
|
import info.magnolia.imaging.operations.cropresize.AutoCropAndResize; |
42 |
|
import info.magnolia.imaging.operations.load.Blank; |
43 |
|
import info.magnolia.imaging.operations.load.ClasspathImageLoader; |
44 |
|
import info.magnolia.imaging.operations.load.DefaultImageIOImageDecoder; |
45 |
|
import info.magnolia.imaging.operations.load.ImageDecoder; |
46 |
|
import info.magnolia.imaging.operations.text.FixedText; |
47 |
|
import info.magnolia.imaging.operations.text.TextStyle; |
48 |
|
|
49 |
|
import java.awt.Color; |
50 |
|
import java.awt.Graphics2D; |
51 |
|
import java.awt.Rectangle; |
52 |
|
import java.awt.image.BufferedImage; |
53 |
|
import java.awt.image.ColorModel; |
54 |
|
import java.awt.image.IndexColorModel; |
55 |
|
import java.io.IOException; |
56 |
|
import java.net.URL; |
57 |
|
|
58 |
|
import javax.imageio.ImageIO; |
59 |
|
|
60 |
|
import org.junit.Test; |
61 |
|
|
62 |
|
|
63 |
|
@link |
64 |
|
|
|
|
| 97.4% |
Uncovered Elements: 4 (155) |
Complexity: 28 |
Complexity Density: 0.24 |
|
65 |
|
public class ImageUtilTest extends AbstractImagingTest { |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
70 |
1 |
@Test... |
71 |
|
public void testDecodingHuffmanImage() throws Exception { |
72 |
1 |
final ImageOperationChain chain = chainWithLoaderTextAndResize(new DefaultImageIOImageDecoder(), "/huffman.jpg"); |
73 |
1 |
final BufferedImage res = chain.generate(null); |
74 |
1 |
final BufferedImage flat = ImageUtil.flattenTransparentImageForOpaqueFormat(res, BASIC_JPEG); |
75 |
1 |
write("huffman_flat", flat, BASIC_JPEG); |
76 |
|
} |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 1 |
Complexity Density: 0.06 |
|
78 |
1 |
private ImageOperationChain chainWithLoaderTextAndResize(ImageDecoder imageDecoder, String src) {... |
79 |
1 |
final ClasspathImageLoader loader = new ClasspathImageLoader(src); |
80 |
1 |
loader.setImageDecoder(imageDecoder); |
81 |
|
|
82 |
1 |
final ImageOperationChain chain = new ImageOperationChain(); |
83 |
1 |
chain.addOperation(loader); |
84 |
1 |
final FixedText text = new FixedText(); |
85 |
1 |
text.setText(imageDecoder.getClass().getName()); |
86 |
1 |
text.setTextStyle(new TextStyle()); |
87 |
1 |
text.getTextStyle().setColor(Color.RED); |
88 |
1 |
text.getTextStyle().setFontName("Arial"); |
89 |
1 |
text.getTextStyle().setFontSize(20); |
90 |
1 |
chain.addOperation(text); |
91 |
|
|
92 |
1 |
final AutoCropAndResize resize = new AutoCropAndResize(); |
93 |
1 |
resize.setTargetHeight(200); |
94 |
1 |
resize.setTargetWidth(200); |
95 |
1 |
chain.addOperation(resize); |
96 |
|
|
97 |
1 |
return chain; |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
100 |
1 |
@Test... |
101 |
|
public void testCanHandleImageCreatedByBlankOperationWithoutBackgroundColor() throws Exception { |
102 |
1 |
final Blank blank = new Blank(200, 200); |
103 |
1 |
final BufferedImage img = blank.apply(null, null); |
104 |
1 |
final BufferedImage res = ImageUtil.flattenTransparentImageForOpaqueFormat(img, BASIC_JPEG); |
105 |
|
|
106 |
1 |
write(res, BASIC_JPEG); |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
public void testCanHandleImageCreatedByBlankOperationWithBackgroundColor() throws Exception { |
111 |
1 |
final Blank blank = new Blank(Color.orange, 200, 200); |
112 |
1 |
final BufferedImage img = blank.apply(null, null); |
113 |
1 |
final BufferedImage res = ImageUtil.flattenTransparentImageForOpaqueFormat(img, BASIC_JPEG); |
114 |
|
|
115 |
1 |
write(res, BASIC_JPEG); |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
118 |
1 |
@Test... |
119 |
|
public void testCanHandleOpaqueGIFSourceWhenFlatteningForJPEG() throws Exception { |
120 |
1 |
doTestFlattenTransparentImageForOpaqueFormat("/some_opaque.gif"); |
121 |
|
} |
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
123 |
1 |
@Test... |
124 |
|
public void testCanHandleTransparentGIFSourceWhenFlatteningForJPEG() throws Exception { |
125 |
1 |
doTestFlattenTransparentImageForOpaqueFormat("/cookies.gif"); |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
128 |
1 |
@Test... |
129 |
|
public void testCanHandleOpaquePNGSourceWhenFlatteningForJPEG() throws Exception { |
130 |
1 |
doTestFlattenTransparentImageForOpaqueFormat("/random_screenshot.png"); |
131 |
|
} |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
133 |
1 |
@Test... |
134 |
|
public void testCanHandleTransparentPNGSourceWhenFlatteningForJPEG() throws Exception { |
135 |
1 |
doTestFlattenTransparentImageForOpaqueFormat("/some_transparent.png"); |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
138 |
1 |
@Test... |
139 |
|
public void testCanHandleTranslucentPNGSourceWhenFlatteningForJPEG() throws Exception { |
140 |
1 |
doTestFlattenTransparentImageForOpaqueFormat("/pngtrans/rgba16.png"); |
141 |
|
} |
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
143 |
1 |
@Test... |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
@throws |
148 |
|
|
149 |
|
public void testCanHandleProblematicLogoPNGSourceWhenFlatteningForJPEG() throws Exception { |
150 |
1 |
doTestFlattenTransparentImageForOpaqueFormat("/magnolia-logo.png"); |
151 |
|
} |
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
153 |
6 |
private void doTestFlattenTransparentImageForOpaqueFormat(final String source) throws Exception {... |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
6 |
final BufferedImage img = loadFromResource(source); |
164 |
6 |
final BufferedImage res = ImageUtil.flattenTransparentImageForOpaqueFormat(img, BASIC_JPEG); |
165 |
6 |
write(res, BASIC_JPEG); |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
171 |
1 |
@Test... |
172 |
|
public void testJpegOddity() throws IOException { |
173 |
|
|
174 |
1 |
final BufferedImage img = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB); |
175 |
|
|
176 |
|
|
177 |
1 |
final Graphics2D g = img.createGraphics(); |
178 |
1 |
g.setColor(Color.red); |
179 |
1 |
g.fill(new Rectangle(10, 10, 30, 30)); |
180 |
|
|
181 |
|
|
182 |
1 |
write("withoutHack", img, BASIC_JPEG); |
183 |
|
|
184 |
|
|
185 |
1 |
final BufferedImage flattened = ImageUtil.flattenTransparentImageForOpaqueFormat(img, BASIC_JPEG); |
186 |
1 |
write("flattenTransparentImageForOpaqueFormat", flattened, BASIC_JPEG); |
187 |
|
|
188 |
|
|
189 |
1 |
final BufferedImage filled = ImageUtil.fillTransparentPixels(img, Color.green); |
190 |
1 |
write("fillTransparentPixels", filled, BASIC_JPEG); |
191 |
|
} |
192 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
1PASS
|
|
193 |
1 |
@Test... |
194 |
|
public void testPerformance() throws Exception { |
195 |
1 |
final Blank blank = new Blank(200, 200); |
196 |
1 |
final BufferedImage src = blank.apply(null, null); |
197 |
1 |
final int iterations = 1; |
198 |
|
{ |
199 |
1 |
final long start = System.currentTimeMillis(); |
200 |
2 |
for (int i = 0; i < iterations; i++) { |
201 |
1 |
ImageUtil.flattenTransparentImageForOpaqueFormat(src, BASIC_JPEG); |
202 |
|
} |
203 |
1 |
System.out.println("flattenTransparentImageForOpaqueFormat: " + (System.currentTimeMillis() - start) + "ms."); |
204 |
|
} |
205 |
|
{ |
206 |
1 |
final long start = System.currentTimeMillis(); |
207 |
2 |
for (int i = 0; i < iterations; i++) { |
208 |
1 |
ImageUtil.fillTransparentPixels(src, Color.black); |
209 |
|
} |
210 |
1 |
System.out.println("fillTransparentPixels : " + (System.currentTimeMillis() - start) + "ms."); |
211 |
|
} |
212 |
|
} |
213 |
|
|
|
|
| 93.3% |
Uncovered Elements: 4 (60) |
Complexity: 10 |
Complexity Density: 0.24 |
1PASS
|
|
214 |
1 |
@Test... |
215 |
|
public void testLoadingPerformance() throws Exception { |
216 |
1 |
final URL smallPng = getClass().getResource("/pngtrans/rgba16.png"); |
217 |
1 |
final URL largeJpeg = getClass().getResource("/IMG_1937.JPG"); |
218 |
1 |
final int iterations = 1; |
219 |
|
{ |
220 |
1 |
final long start = System.currentTimeMillis(); |
221 |
2 |
for (int i = 0; i < iterations; i++) { |
222 |
1 |
ImageIO.read(smallPng); |
223 |
|
} |
224 |
1 |
System.out.println("warm up : " + (System.currentTimeMillis() - start) + "ms."); |
225 |
|
} |
226 |
|
{ |
227 |
1 |
final long start = System.currentTimeMillis(); |
228 |
2 |
for (int i = 0; i < iterations; i++) { |
229 |
1 |
ImageIO.read(largeJpeg); |
230 |
|
} |
231 |
1 |
System.out.println("warm up 2 : " + (System.currentTimeMillis() - start) + "ms."); |
232 |
|
} |
233 |
|
{ |
234 |
1 |
final long start = System.currentTimeMillis(); |
235 |
2 |
for (int i = 0; i < iterations; i++) { |
236 |
1 |
ImageIO.read(smallPng); |
237 |
|
} |
238 |
1 |
System.out.println("just reading small png : " + (System.currentTimeMillis() - start) + "ms."); |
239 |
|
} |
240 |
|
{ |
241 |
1 |
final long start = System.currentTimeMillis(); |
242 |
2 |
for (int i = 0; i < iterations; i++) { |
243 |
1 |
ImageIO.read(largeJpeg); |
244 |
|
} |
245 |
1 |
System.out.println("just reading large jpeg : " + (System.currentTimeMillis() - start) + "ms."); |
246 |
|
} |
247 |
|
{ |
248 |
1 |
final long start = System.currentTimeMillis(); |
249 |
2 |
for (int i = 0; i < iterations; i++) { |
250 |
1 |
final BufferedImage src = ImageIO.read(smallPng); |
251 |
|
|
252 |
1 |
final BufferedImage img = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE); |
253 |
1 |
final Graphics2D g = img.createGraphics(); |
254 |
1 |
final boolean b = g.drawImage(src, null, null); |
255 |
1 |
if (!b) { |
256 |
0 |
throw new IllegalStateException("wtf?"); |
257 |
|
} |
258 |
|
} |
259 |
1 |
System.out.println("with drawing on new image - small png : " + (System.currentTimeMillis() - start) + "ms."); |
260 |
|
} |
261 |
|
{ |
262 |
1 |
final long start = System.currentTimeMillis(); |
263 |
2 |
for (int i = 0; i < iterations; i++) { |
264 |
1 |
final BufferedImage src = ImageIO.read(largeJpeg); |
265 |
|
|
266 |
1 |
final BufferedImage img = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE); |
267 |
1 |
final Graphics2D g = img.createGraphics(); |
268 |
1 |
final boolean b = g.drawImage(src, null, null); |
269 |
1 |
if (!b) { |
270 |
0 |
throw new IllegalStateException("wtf?"); |
271 |
|
} |
272 |
|
} |
273 |
1 |
System.out.println("with drawing on new image - large jpeg: " + (System.currentTimeMillis() - start) + "ms."); |
274 |
|
} |
275 |
|
{ |
276 |
1 |
final long start = System.currentTimeMillis(); |
277 |
2 |
for (int i = 0; i < iterations; i++) { |
278 |
1 |
final ClasspathImageLoader loader = new ClasspathImageLoader("/IMG_1937.JPG"); |
279 |
1 |
loader.apply(null, null); |
280 |
|
} |
281 |
1 |
System.out.println("with ClasspathImageLoader - large jpeg: " + (System.currentTimeMillis() - start) + "ms."); |
282 |
|
} |
283 |
|
} |
284 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
285 |
1 |
@Test... |
286 |
|
public void testGetOriginalImageType() throws Exception { |
287 |
|
|
288 |
1 |
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_3BYTE_BGR); |
289 |
|
|
290 |
|
|
291 |
1 |
int imageType = ImageUtil.getImageType(img); |
292 |
|
|
293 |
|
|
294 |
1 |
assertEquals(BufferedImage.TYPE_3BYTE_BGR, imageType); |
295 |
|
} |
296 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
297 |
1 |
@Test... |
298 |
|
public void testGetImageTypeIndexedTypeWithoutAlpha() throws Exception { |
299 |
|
|
300 |
1 |
ColorModel colorModel = new IndexColorModel(1, 1, new byte[8], 0, false); |
301 |
1 |
assertFalse(colorModel.hasAlpha()); |
302 |
1 |
BufferedImage img = mock(BufferedImage.class); |
303 |
1 |
when(img.getType()).thenReturn(BufferedImage.TYPE_BYTE_INDEXED); |
304 |
1 |
when(img.getColorModel()).thenReturn(colorModel); |
305 |
|
|
306 |
|
|
307 |
1 |
int imageType = ImageUtil.getImageType(img); |
308 |
|
|
309 |
|
|
310 |
1 |
assertEquals(BufferedImage.TYPE_BYTE_INDEXED, imageType); |
311 |
|
} |
312 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
313 |
1 |
@Test... |
314 |
|
public void testGetImageTypeIndexedTypeWithAlpha() throws Exception { |
315 |
|
|
316 |
1 |
ColorModel colorModel = new IndexColorModel(1, 1, new byte[8], 0, true); |
317 |
1 |
assertTrue(colorModel.hasAlpha()); |
318 |
1 |
BufferedImage img = mock(BufferedImage.class); |
319 |
1 |
when(img.getType()).thenReturn(BufferedImage.TYPE_BYTE_INDEXED); |
320 |
1 |
when(img.getColorModel()).thenReturn(colorModel); |
321 |
|
|
322 |
|
|
323 |
1 |
int imageType = ImageUtil.getImageType(img); |
324 |
|
|
325 |
|
|
326 |
1 |
assertEquals(BufferedImage.TYPE_INT_ARGB_PRE, imageType); |
327 |
|
} |
328 |
|
|
329 |
|
} |