1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.plugin.image

File DefaultImageProcessor.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

10
30
4
1
126
80
12
0.4
7.5
4
3

Classes

Class Line # Actions
DefaultImageProcessor 53 30 0% 12 20
0.5454545654.5%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.internal.plugin.image;
21   
22    import java.awt.Graphics2D;
23    import java.awt.Image;
24    import java.awt.RenderingHints;
25    import java.awt.image.BufferedImage;
26    import java.awt.image.RenderedImage;
27    import java.io.IOException;
28    import java.io.InputStream;
29    import java.io.OutputStream;
30    import java.util.Arrays;
31    import java.util.Iterator;
32   
33    import javax.imageio.IIOImage;
34    import javax.imageio.ImageIO;
35    import javax.imageio.ImageWriteParam;
36    import javax.imageio.ImageWriter;
37    import javax.imageio.plugins.jpeg.JPEGImageWriteParam;
38    import javax.imageio.stream.ImageOutputStream;
39    import javax.inject.Singleton;
40   
41    import org.xwiki.component.annotation.Component;
42   
43    import com.xpn.xwiki.plugin.image.ImageProcessor;
44   
45    /**
46    * Default {@link ImageProcessor} implementation.
47    *
48    * @version $Id: b5f5cb098e18928d4b0fa2b400d6f0816b3f29ac $
49    * @since 2.5M2
50    */
51    @Component
52    @Singleton
 
53    public class DefaultImageProcessor implements ImageProcessor
54    {
 
55  3 toggle @Override
56    public Image readImage(InputStream inputStream) throws IOException
57    {
58  3 return ImageIO.read(inputStream);
59    }
60   
 
61  2 toggle @Override
62    public void writeImage(RenderedImage image, String mimeType, float quality, OutputStream out) throws IOException
63    {
64  2 if ("image/jpeg".equals(mimeType)) {
65    // Find a JPEG writer.
66  0 ImageWriter writer = null;
67  0 Iterator<ImageWriter> iter = ImageIO.getImageWritersByMIMEType(mimeType);
68  0 if (iter.hasNext()) {
69  0 writer = iter.next();
70    }
71  0 JPEGImageWriteParam iwp = new JPEGImageWriteParam(null);
72  0 iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
73  0 iwp.setCompressionQuality(quality);
74   
75    // Prepare output file.
76  0 ImageOutputStream ios = ImageIO.createImageOutputStream(out);
77  0 writer.setOutput(ios);
78   
79    // Write the image.
80  0 writer.write(null, new IIOImage(image, null, null), iwp);
81   
82    // Cleanup.
83  0 ios.flush();
84  0 writer.dispose();
85  0 ios.close();
86    } else {
87  2 ImageIO.write(image, "png", out);
88    }
89    }
90   
 
91  2 toggle @Override
92    public RenderedImage scaleImage(Image image, int width, int height)
93    {
94    // Draw the given image to a buffered image object and scale it to the new size on-the-fly.
95  2 int imageType = BufferedImage.TYPE_4BYTE_ABGR;
96  2 if (image instanceof BufferedImage) {
97  2 imageType = ((BufferedImage) image).getType();
98  2 if (imageType == BufferedImage.TYPE_BYTE_INDEXED || imageType == BufferedImage.TYPE_BYTE_BINARY
99    || imageType == BufferedImage.TYPE_CUSTOM) {
100    // INDEXED and BINARY: GIFs or indexed PNGs may lose their transparent bits, for safety revert to ABGR.
101    // CUSTOM: Unknown image type, fall back on ABGR.
102  1 imageType = BufferedImage.TYPE_4BYTE_ABGR;
103    }
104    }
105  2 BufferedImage bufferedImage = new BufferedImage(width, height, imageType);
106  2 Graphics2D graphics2D = bufferedImage.createGraphics();
107  2 graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
108    // We should test the return code here because an exception can be throw but caught.
109  2 if (!graphics2D.drawImage(image, 0, 0, width, height, null)) {
110    // Conversion failed.
111  0 throw new RuntimeException("Failed to resize image.");
112    }
113  2 return bufferedImage;
114    }
115   
 
116  3 toggle @Override
117    public boolean isMimeTypeSupported(String mimeType)
118    {
119  3 try {
120  3 return Arrays.asList(ImageIO.getReaderMIMETypes()).contains(mimeType);
121    } catch (NoClassDefFoundError e) {
122    // Happens on certain systems where the javax.imageio package is not available.
123  0 return false;
124    }
125    }
126    }