Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.util

File ImageUtil.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
65% of files have more coverage

Code metrics

12
41
5
1
134
75
14
0.34
8.2
5
2.8

Classes

Class Line # Actions
ImageUtil 53 41 0% 14 17
0.7068965470.7%
 

Contributing tests

This file is covered by 32 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2015 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.util;
35   
36    import info.magnolia.imaging.OutputFormat;
37   
38    import java.awt.Color;
39    import java.awt.Graphics2D;
40    import java.awt.Transparency;
41    import java.awt.image.BufferedImage;
42    import java.awt.image.DirectColorModel;
43    import java.awt.image.WritableRaster;
44    import java.io.BufferedInputStream;
45    import java.io.InputStream;
46   
47    import org.slf4j.Logger;
48    import org.slf4j.LoggerFactory;
49   
50    /**
51    * Keeps utility functions for imaging.
52    */
 
53    public class ImageUtil {
54   
55    private static final Logger log = LoggerFactory.getLogger(ImageUtil.class);
56   
57    /**
58    * @see info.magnolia.imaging.util.ImageUtilTest#testJpegOddity()
59    */
 
60  27 toggle public static BufferedImage flattenTransparentImageForOpaqueFormat(final BufferedImage img, OutputFormat outputFormat) {
61  27 if (img == null || outputFormat == null) {
62  0 throw new IllegalArgumentException("No image or output format passed: [img:" + img + ", outputFormat:" + outputFormat + "]");
63    }
64    // this is not entirely sufficient; for instance, a gif with no transparent pixels loaded through ImageIO.read() will not be considered opaque by the following.
65  27 final boolean isOpaque = img.getTransparency() == Transparency.OPAQUE;
66   
67  27 if (!isOpaque && !outputFormat.supportsTransparency()) {
68  12 final WritableRaster raster = img.getRaster();
69  12 final WritableRaster newRaster = raster.createWritableChild(0, 0, img.getWidth(), img.getHeight(), 0, 0, new int[]{0, 1, 2});
70   
71    // test for non-operable file
72  12 if (img.getColorModel() instanceof java.awt.image.ComponentColorModel) {
73  1 log.warn("flattenTransparentImageForOpaqueFormat cannot run on image with ComponentColorModel. Consider replacing image. Falling back to slower fillTransparantPixels method to flatten image. [img:" + img + "]");
74  1 return fillTransparentPixels(img, Color.black);
75    }
76   
77    // create a ColorModel that represents the one of the ARGB except the alpha channel
78  11 final DirectColorModel cm = (DirectColorModel) img.getColorModel();
79  11 final DirectColorModel newCM = new DirectColorModel(cm.getPixelSize(), cm.getRedMask(), cm.getGreenMask(), cm.getBlueMask());
80   
81    // now create the new buffer that we'll use to write the image
82  11 return new BufferedImage(newCM, newRaster, false, null);
83   
84    } else {
85  15 return img;
86    }
87    }
88   
89    // this is much slower than flattenTransparentImageForOpaqueFormat; but more robust.
90   
 
91  3 toggle static BufferedImage fillTransparentPixels(final BufferedImage image, Color fillColor) {
92  3 int w = image.getWidth();
93  3 int h = image.getHeight();
94  3 final BufferedImage image2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
95  3 final Graphics2D g = image2.createGraphics();
96  3 g.setColor(fillColor);
97  3 g.fillRect(0, 0, w, h);
98  3 g.drawRenderedImage(image, null);
99  3 g.dispose();
100  3 return image2;
101    }
102   
 
103  0 toggle public static BufferedInputStream newBufferedInputStream(InputStream in) {
104  0 return new BufferedInputStream(in) {
 
105  0 toggle @Override
106    public String toString() {
107  0 final StringBuilder sb = new StringBuilder();
108  0 sb.append("BufferedInputStream");
109  0 sb.append("{pos='").append(pos).append('\'');
110  0 sb.append(", count='").append(count).append('\'');
111  0 sb.append(", markpos='").append(markpos).append('\'');
112  0 sb.append(", marklimit='").append(marklimit).append('\'');
113  0 sb.append(", buf.length='").append(buf.length).append('\'');
114  0 sb.append(", wrapping ").append(in.toString());
115  0 sb.append('}');
116  0 return sb.toString();
117    }
118    };
119    }
120   
 
121  35 toggle public static int getImageType(BufferedImage img) {
122  35 int imageType = img.getType();
123  35 if (imageType == BufferedImage.TYPE_CUSTOM) { // if the source image type is not set...
124  1 if (img.getAlphaRaster() != null) { // with alpha channel
125  1 imageType = BufferedImage.TYPE_INT_ARGB_PRE;
126    } else {
127  0 imageType = BufferedImage.TYPE_INT_RGB;
128    }
129  34 } else if (imageType == BufferedImage.TYPE_BYTE_INDEXED && img.getColorModel().hasAlpha()) {
130  4 imageType = BufferedImage.TYPE_INT_ARGB_PRE;
131    }
132  35 return imageType;
133    }
134    }