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.util

File ImageUtil.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

12
46
5
1
139
79
16
0.35
9.2
5
3.2

Classes

Class Line # Actions
ImageUtil 53 46 0% 16 17
0.7301587573%
 

Contributing tests

This file is covered by 38 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-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.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  28 toggle public static BufferedImage flattenTransparentImageForOpaqueFormat(final BufferedImage img, OutputFormat outputFormat) {
61  28 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  28 final boolean isOpaque = img.getTransparency() == Transparency.OPAQUE;
66   
67  28 if (!isOpaque && !outputFormat.supportsTransparency()) {
68  13 final WritableRaster raster = img.getRaster();
69  13 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  13 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  12 final DirectColorModel cm = (DirectColorModel) img.getColorModel();
79  12 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  12 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  40 toggle public static int getImageType(BufferedImage img) {
122  40 int imageType = img.getType();
123  40 switch (imageType) {
124  1 case BufferedImage.TYPE_CUSTOM: // If the source image type is not set...
125  1 imageType = img.getAlphaRaster() != null ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_RGB;
126  1 break;
127    // Handle both TYPE_BYTE_BINARY (1-4 bit/pixel) and TYPE_BYTE_INDEXED (8 bit/pixel).
128  6 case BufferedImage.TYPE_BYTE_BINARY:
129  5 case BufferedImage.TYPE_BYTE_INDEXED:
130  11 if (img.getColorModel() != null) {
131  11 imageType = img.getColorModel().hasAlpha() ? BufferedImage.TYPE_INT_ARGB_PRE : BufferedImage.TYPE_INT_RGB;
132    }
133  11 break;
134  28 default:
135  28 break;
136    }
137  40 return imageType;
138    }
139    }