Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging.operations.load

File AbstractLoaderTest.java

 

Code metrics

20
159
21
2
417
295
39
0.25
7.57
10.5
1.86
1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
AbstractLoaderTest 63 158 0% 38 26
0.8686868586.9%
AbstractLoaderTest.Loader 315 1 50% 1 0
1.0100%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /**
2    * This file Copyright (c) 2012-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging.operations.load;
35   
36    import static info.magnolia.imaging.AbstractImagingTest.newOutputFile;
37    import static org.junit.Assert.*;
38    import static org.mockito.Mockito.mock;
39   
40    import info.magnolia.imaging.AbstractRepositoryTestCase;
41    import info.magnolia.imaging.ImagingException;
42    import info.magnolia.imaging.ParameterProvider;
43   
44    import java.awt.Color;
45    import java.awt.image.BufferedImage;
46    import java.awt.image.WritableRaster;
47    import java.io.File;
48    import java.io.IOException;
49    import java.lang.reflect.Field;
50    import java.lang.reflect.Modifier;
51    import java.util.HashMap;
52    import java.util.Map;
53    import java.util.Set;
54   
55    import javax.imageio.ImageIO;
56   
57    import org.junit.Before;
58    import org.junit.Test;
59    import org.reflections.ReflectionUtils;
60    import org.slf4j.Logger;
61    import org.slf4j.LoggerFactory;
62   
 
63    public class AbstractLoaderTest extends AbstractRepositoryTestCase {
64    private static final Logger log = LoggerFactory.getLogger(AbstractLoaderTest.class);
65   
66    private Loader loader;
67    private final ParameterProvider provider = mock(ParameterProvider.class);
68   
69    private BufferedImage originalJpgImage;
70    private BufferedImage originalPngImage;
71    private BufferedImage originalGifImage;
72    private BufferedImage original4BitGifImage;
73    private BufferedImage originalBmpImage;
74    private BufferedImage originalHiResImage;
75    private BufferedImage originalPngWithAlphaImage;
76    private BufferedImage originalPngWithAlphaImageBlackBackground;
77   
78    private BufferedImage png2gifOriginalImage;
79    private BufferedImage png2jpgOriginalImage;
80    private BufferedImage jpg2jpgOriginalImage;
81    private BufferedImage gif2gifOriginalImage;
82    private BufferedImage gif2pngOriginalImage;
83    private BufferedImage gif2jpgOriginalImage;
84    private BufferedImage gif2gifOriginal4BitImage;
85    private BufferedImage gif2pngOriginal4BitImage;
86    private BufferedImage gif2jpgOriginal4BitImage;
87   
88    private final String pathWithOriginals = "src/test/resources/AbstractLoaderTest/originals";
89   
 
90  10 toggle @Before
91    @Override
92    public void setUp() throws Exception {
93  10 super.setUp();
94  10 loader = new Loader();
95   
96  10 originalJpgImage = ImageIO.read(new File(pathWithOriginals, "originalJpg.jpg"));
97  10 originalPngImage = ImageIO.read(new File(pathWithOriginals, "originalPng.png"));
98  10 originalGifImage = ImageIO.read(new File(pathWithOriginals, "originalGif.gif"));
99  10 original4BitGifImage = ImageIO.read(new File(pathWithOriginals, "original4BitGif.gif"));
100  10 originalBmpImage = ImageIO.read(new File(pathWithOriginals, "originalBmp.bmp"));
101  10 originalPngWithAlphaImage = ImageIO.read(new File(pathWithOriginals, "originalPngWithAlpha.png"));
102  10 originalHiResImage = ImageIO.read(new File(pathWithOriginals, "originalHiRes.jpg"));
103  10 originalPngWithAlphaImageBlackBackground = ImageIO.read(new File(pathWithOriginals, "originalPngWithAlphaBlackBackground.png"));
104   
105  10 png2gifOriginalImage = ImageIO.read(new File(pathWithOriginals, "png2gifOriginal.gif"));
106  10 png2jpgOriginalImage = ImageIO.read(new File(pathWithOriginals, "png2jpgOriginal.jpg"));
107  10 jpg2jpgOriginalImage = ImageIO.read(new File(pathWithOriginals, "jpg2jpgOriginal.jpg"));
108  10 gif2gifOriginalImage = ImageIO.read(new File(pathWithOriginals, "gif2gifOriginal.gif"));
109  10 gif2pngOriginalImage = ImageIO.read(new File(pathWithOriginals, "gif2pngOriginal.png"));
110  10 gif2jpgOriginalImage = ImageIO.read(new File(pathWithOriginals, "gif2jpgOriginal.jpg"));
111  10 gif2gifOriginal4BitImage = ImageIO.read(new File(pathWithOriginals, "gif2gifOriginal4Bit.gif"));
112  10 gif2pngOriginal4BitImage = ImageIO.read(new File(pathWithOriginals, "gif2pngOriginal4Bit.png"));
113  10 gif2jpgOriginal4BitImage = ImageIO.read(new File(pathWithOriginals, "gif2jpgOriginal4Bit.jpg"));
114    }
115   
 
116  1 toggle @Test
117    public void backgroundColor() throws Exception {
118    // GIVEN
119  1 loader.setSource(originalPngWithAlphaImage);
120  1 loader.setBackgroundColor(Color.black);
121    // WHEN
122  1 BufferedImage bufferedImage = loader.apply(null, provider);
123  1 log.info("\nTest BufferedImage from png with alpha image: source image type: {}, target image type: {}",
124    imageTypeToString(originalPngWithAlphaImage.getType()), imageTypeToString(bufferedImage.getType()));
125    // THEN
126  1 assertEquals(originalPngWithAlphaImage.getType(), bufferedImage.getType());
127  1 assertTrue(equalsPictures(originalPngWithAlphaImageBlackBackground, bufferedImage));
128  1 assertTrue(equalsAlpha(originalPngWithAlphaImageBlackBackground, bufferedImage));
129   
130    // for visual checkout
131  1 ImageIO.write(bufferedImage, "png", newOutputFile("loadertest", "pngWithAlpha2PngBlackBackground", "png"));
132    }
133   
134    // PNG
 
135  1 toggle @Test
136    public void bufferedImageFromPng() throws Exception {
137    // GIVEN
138  1 loader.setSource(originalPngImage);
139    // WHEN
140  1 BufferedImage bufferedImage = loader.apply(null, provider);
141  1 log.info("\nTest BufferedImage from png image: source image type: {}, target image type: {}",
142    imageTypeToString(originalPngImage.getType()), imageTypeToString(bufferedImage.getType()));
143    // THEN
144  1 assertEquals(originalPngImage.getType(), bufferedImage.getType());
145  1 assertTrue(equalsPictures(originalPngImage, bufferedImage));
146  1 assertTrue(equalsAlpha(originalPngImage, bufferedImage));
147   
148  1 final File png2jpg = newOutputFile("loadertest", "png2jpg", "jpg");
149  1 final File png2png = newOutputFile("loadertest", "png2png", "png");
150  1 final File png2gif = newOutputFile("loadertest", "png2gif", "gif");
151   
152  1 ImageIO.write(bufferedImage, "gif", png2gif);
153  1 ImageIO.write(bufferedImage, "png", png2png);
154  1 ImageIO.write(bufferedImage, "jpg", png2jpg);
155   
156  1 assertImage("png to png", png2png, originalPngImage, originalPngImage);
157  1 assertImage("png to jpg", png2jpg, originalPngImage, png2jpgOriginalImage);
158  1 assertImage("png to gif", png2gif, originalPngImage, png2gifOriginalImage);
159    }
160   
161    // JPEG
 
162  1 toggle @Test
163    public void bufferedImageFromJpg() throws Exception {
164    // GIVEN
165  1 loader.setSource(originalJpgImage);
166    // WHEN
167  1 BufferedImage bufferedImage = loader.apply(null, provider);
168  1 log.info("\nTest BufferedImage from jpg image: source image type: {}, target image type: {}",
169    imageTypeToString(originalJpgImage.getType()), imageTypeToString(bufferedImage.getType()));
170    // THEN
171  1 assertEquals(originalJpgImage.getType(), bufferedImage.getType());
172  1 assertTrue(equalsPictures(originalJpgImage, bufferedImage));
173  1 assertTrue(equalsAlpha(originalJpgImage, bufferedImage));
174   
175  1 final File jpg2jpg = newOutputFile("loadertest", "jpg2jpg", "jpg");
176  1 final File jpg2png = newOutputFile("loadertest", "jpg2png", "png");
177  1 final File jpg2gif = newOutputFile("loadertest", "jpg2gif", "gif");
178  1 ImageIO.write(bufferedImage, "gif", jpg2gif);
179  1 ImageIO.write(bufferedImage, "png", jpg2png);
180  1 ImageIO.write(bufferedImage, "jpg", jpg2jpg);
181   
182  1 assertImage("jpg to gif", jpg2gif, originalJpgImage, originalGifImage);
183  1 assertImage("jpg to png", jpg2png, originalJpgImage, originalJpgImage);
184  1 assertImage("jpg to jpg", jpg2jpg, originalJpgImage, jpg2jpgOriginalImage);
185    }
186   
 
187  1 toggle @Test
188    public void gifToGifCase4BitImage() throws Exception {
189    // GIVEN and WHEN
190  1 BufferedImage bufferedImage = loadOriginal4BitImage();
191    // THEN
192  1 assertValid4BitGifImage(bufferedImage);
193  1 final File gif2gif = newOutputFile("loadertest", "gif2gif4Bit", "gif");
194  1 ImageIO.write(bufferedImage, "gif", gif2gif);
195  1 assertImage("gif to gif (4 bit)", gif2gif, original4BitGifImage, gif2gifOriginal4BitImage);
196    }
197   
 
198  1 toggle @Test
199    public void gifToPngCase4BitImage() throws Exception {
200    // GIVEN and WHEN
201  1 BufferedImage bufferedImage = loadOriginal4BitImage();
202    // THEN
203  1 assertValid4BitGifImage(bufferedImage);
204  1 final File gif2png = newOutputFile("loadertest", "gif2png4Bit", "png");
205  1 ImageIO.write(bufferedImage, "png", gif2png);
206  1 assertImage("gif to png (4 bit)", gif2png, original4BitGifImage, gif2pngOriginal4BitImage);
207    }
208   
 
209  1 toggle @Test
210    public void gifToJpgCase4BitImage() throws Exception {
211    // GIVEN and WHEN
212  1 BufferedImage bufferedImage = loadOriginal4BitImage();
213    // THEN
214  1 assertValid4BitGifImage(bufferedImage);
215  1 final File gif2jpg = newOutputFile("loadertest", "gif2jpg4Bit", "jpg");
216  1 ImageIO.write(bufferedImage, "jpg", gif2jpg);
217  1 assertImage("gif to jpg (4 bit)", gif2jpg, original4BitGifImage, gif2jpgOriginal4BitImage);
218    }
219   
 
220  3 toggle private BufferedImage loadOriginal4BitImage() throws Exception {
221  3 loader.setSource(original4BitGifImage);
222  3 BufferedImage bufferedImage = loader.apply(null, provider);
223  3 return bufferedImage;
224    }
225   
 
226  3 toggle private void assertValid4BitGifImage(BufferedImage bufferedImage) {
227  3 assertEquals(BufferedImage.TYPE_INT_RGB, bufferedImage.getType());
228  3 assertTrue(equalsPictures(original4BitGifImage, bufferedImage));
229  3 assertTrue(equalsAlpha(original4BitGifImage, bufferedImage));
230    }
231   
 
232  9 toggle private void assertImage(String desc, File src, BufferedImage original, BufferedImage expectedImg) throws IOException {
233    // WHEN
234  9 BufferedImage asRead = ImageIO.read(src);
235  9 log.info("\nTest {} image : {}, target image type: {}", desc, imageTypeToString(original.getType()), imageTypeToString(asRead.getType()));
236    // THEN
237  9 assertTrue(equalsPictures(expectedImg, asRead));
238  9 assertTrue(equalsAlpha(original, asRead));
239    }
240   
 
241  1 toggle @Test
242    public void bufferedImageFromBmp() throws Exception {
243    // GIVEN
244  1 loader.setSource(originalBmpImage);
245    // WHEN
246  1 BufferedImage bufferedImage = loader.apply(null, provider);
247  1 log.info("\nTest BufferedImage from bmp image: source image type: {}, target image type: {}",
248    imageTypeToString(originalBmpImage.getType()), imageTypeToString(bufferedImage.getType()));
249    // THEN
250  1 assertEquals(originalBmpImage.getType(), bufferedImage.getType());
251  1 assertTrue(equalsPictures(originalBmpImage, bufferedImage));
252  1 assertTrue(equalsAlpha(originalBmpImage, bufferedImage));
253    }
254   
 
255  1 toggle @Test
256    public void bufferedImageFromPngWithAlpha() throws Exception {
257    // GIVEN
258  1 loader.setSource(originalPngWithAlphaImage);
259    // WHEN
260  1 BufferedImage bufferedImage = loader.apply(null, provider);
261  1 log.info("\nTest BufferedImage from png with alpha image: source image type: {}, target image type: {}",
262    imageTypeToString(originalPngWithAlphaImage.getType()), imageTypeToString(bufferedImage.getType()));
263    // THEN
264  1 assertEquals(originalPngWithAlphaImage.getType(), bufferedImage.getType());
265  1 assertTrue(equalsPictures(originalPngWithAlphaImage, bufferedImage));
266  1 assertTrue(equalsAlpha(originalPngWithAlphaImage, bufferedImage));
267   
268    // for visual checkout
269  1 ImageIO.write(bufferedImage, "png", newOutputFile("loadertest", "pngWithAlpha2Png", "png"));
270    }
271   
 
272  1 toggle @Test
273    public void transparencyOfIndexedImageIsNotLost() throws Exception {
274    // GIVEN
275  1 final String fileName = "transparentIndexed";
276  1 File originalFile = new File(pathWithOriginals, fileName + ".png");
277  1 File outputFile = newOutputFile("loader-test", "Loaded", "png");
278  1 BufferedImage originalImage = ImageIO.read(originalFile);
279  1 loader.setSource(originalImage);
280    // WHEN
281  1 BufferedImage bufferedImage = loader.apply(null, provider);
282  1 log.info("\nTest BufferedImage from indexed image: source image type: {}, target image type: {}",
283    imageTypeToString(originalBmpImage.getType()), imageTypeToString(bufferedImage.getType()));
284    // THEN
285    // for visual checkout
286    // this.checkVisually(bufferedImage, originalFile, outputFile);
287  1 assertTrue(bufferedImage.getColorModel().hasAlpha());
288    }
289   
 
290  1 toggle @Test
291    public void memoryUse() throws Exception {
292    // GIVEN
293  1 loader.setSource(originalHiResImage);
294  1 Runtime runtime = Runtime.getRuntime();
295  1 runtime.gc();
296  1 long tm = runtime.totalMemory();
297  1 long fm = runtime.freeMemory();
298  1 long um = tm-fm;
299    // WHEN
300  1 BufferedImage bufferedImage = loader.apply(null, provider);
301    //THEN
302  1 runtime.gc();
303  1 tm = runtime.totalMemory();
304  1 fm = runtime.freeMemory();
305    // 10MB tolerance
306  1 assertTrue((um+10000000>(tm-fm)) && (um-10000000<(tm-fm)));
307    }
308   
309   
 
310  0 toggle private void checkVisually(BufferedImage bufferedImage, File originalFile, File outputFile) throws IOException {
311  0 ImageIO.write(bufferedImage, "png", outputFile);
312  0 Runtime.getRuntime().exec(String.format("open %s %s", originalFile.getAbsolutePath(), outputFile.getAbsolutePath()));
313    }
314   
 
315    final class Loader extends AbstractLoader {
316   
317    private BufferedImage source;
318   
 
319    toggle public void setSource(BufferedImage originalImage) {
320    source = originalImage;
321    }
322   
 
323  10 toggle @Override
324    protected BufferedImage loadSource(ParameterProvider filterParams) throws ImagingException {
325  10 return source;
326    }
327    }
328   
 
329  0 toggle private void showSupportedWriterFormats() {
330  0 String writerNames[] = ImageIO.getWriterFormatNames();
331  0 String msg = "\nSupported writer formats: ";
332  0 for (String name : writerNames) {
333  0 msg += name + ",";
334    }
335  0 log.info(msg);
336    }
337   
 
338  17 toggle private boolean equalsAlpha(BufferedImage bi1, BufferedImage bi2) {
339  17 if (bi1.getWidth() != bi2.getWidth() || bi1.getHeight() != bi2.getHeight()) {
340  0 log.error(String.format("Different size: '%dx%d' vs. '%dx%d'",
341    bi1.getWidth(), bi1.getHeight(), bi2.getWidth(), bi2.getHeight()));
342  0 return false;
343    }
344  17 WritableRaster wr1 = bi1.getAlphaRaster();
345  17 WritableRaster wr2 = bi2.getAlphaRaster();
346   
347  17 if (wr1 == null && wr2 == null) {
348  15 return true;
349  2 } else if (wr1 == null && wr2 != null || wr1 != null && wr2 == null) {
350  0 return false;
351    }
352   
353  2 int size = bi1.getWidth() * bi1.getHeight();
354  2 int dArray1[] = wr1.getPixel(0, 0, new int[size]);
355  2 int dArray2[] = wr2.getPixel(0, 0, new int[size]);
356   
357  131074 for (int x = 0; x < size; x++) {
358  131072 if (dArray1[x] != dArray2[x]) {
359  0 log.error(String.format("Different alpha value at position: %d': '%d' vs. '%d'.",
360    x, dArray1[x], dArray2[x]));
361  0 return false;
362    }
363    }
364  2 return true;
365    }
366   
 
367  17 toggle private boolean equalsPictures(BufferedImage bi1, BufferedImage bi2) {
368   
369  17 if (bi1.getWidth() != bi2.getWidth() || bi1.getHeight() != bi2.getHeight()) {
370  0 log.error(String.format("Different size: '%dx%d' vs. '%dx%d'",
371    bi1.getWidth(), bi1.getHeight(), bi2.getWidth(), bi2.getHeight()));
372  0 return false;
373    }
374   
375  4747 for (int x = 0; x < bi1.getWidth(); x++) {
376  982276 for (int y = 0; y < bi1.getHeight(); y++) {
377  977546 if (bi1.getRGB(x, y) != bi2.getRGB(x, y)) {
378  0 log.error(String.format("Different pixel at position: '%d,%d': '%d' vs. '%d'.",
379    x, y, bi1.getRGB(x, y), bi2.getRGB(x, y)));
380  0 return false;
381    }
382    }
383    }
384  17 return true;
385    }
386   
 
387  30 toggle private String imageTypeToString(int imageType) {
388  30 final String str = imageTypeToString.get(imageType);
389  30 if (imageType == 0 || str == null) {
390  0 throw new IllegalArgumentException("Unknown image type " + imageType + "(" + str + ")");
391    }
392  30 return str;
393    }
394   
395    static final Map<Integer, String> imageTypeToString = new HashMap<>();
396   
 
397  1 toggle static {
398  1 final Set<Field> typeFields = ReflectionUtils.getAllFields(BufferedImage.class, field -> {
399  44 int mod = field.getModifiers();
400  44 return Modifier.isStatic(mod) &&
401    Modifier.isPublic(mod) &&
402    Modifier.isFinal(mod) &&
403    Integer.TYPE.equals(field.getType()) &&
404    field.getName().startsWith("TYPE_");
405   
406    });
407  1 try {
408  1 for (Field field : typeFields) {
409  14 imageTypeToString.put((Integer) field.get(null), field.getName());
410    }
411    } catch (IllegalAccessException e) {
412  0 throw new IllegalStateException(e);
413    }
414   
415    }
416   
417    }