1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.images

File ImageUtil.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

6
33
5
1
192
98
10
0.3
6.6
5
2

Classes

Class Line # Actions
ImageUtil 39 33 0% 10 44
0.00%
 

Contributing tests

No tests hitting this source file were found.

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 org.xwiki.rendering.wikimodel.images;
21   
22    import java.awt.Graphics2D;
23    import java.awt.RenderingHints;
24    import java.awt.image.BufferedImage;
25    import java.io.IOException;
26    import java.io.InputStream;
27    import java.io.OutputStream;
28   
29    import javax.imageio.ImageIO;
30    import javax.imageio.stream.ImageInputStream;
31   
32    /**
33    * Contains utility methods dealing with images like image size definition or
34    * creation of thumbnails (reduced copies of images).
35    *
36    * @version $Id: 2b0c0e29d583c58caa42a22a0353693cbb8f7e3f $
37    * @since 4.0M1
38    */
 
39    public class ImageUtil
40    {
41    /**
42    * Creates a smaller version of an image or returns the original image if it
43    * was in the specified boundaries. The returned image keeps the ratio of
44    * the original image.
45    *
46    * @param image the image to re-size
47    * @param thumbWidth the maximal width of the image
48    * @param thumbHeight the maximal height of the image
49    * @return a new re-sized image or the original image if it was in the
50    * specified boundaries
51    */
 
52  0 toggle private static BufferedImage createThumb(
53    BufferedImage image,
54    int thumbWidth,
55    int thumbHeight)
56    {
57  0 int imageWidth = image.getWidth(null);
58  0 int imageHeight = image.getHeight(null);
59  0 int[] size = getNewSize(
60    imageWidth,
61    imageHeight,
62    thumbWidth,
63    thumbHeight);
64  0 if (size[0] == imageWidth && size[1] == imageHeight) {
65  0 return image;
66    }
67   
68  0 BufferedImage thumbImage = new BufferedImage(
69    size[0],
70    size[1],
71    BufferedImage.TYPE_INT_RGB);
72  0 Graphics2D graphics2D = thumbImage.createGraphics();
73  0 graphics2D.setRenderingHint(
74    RenderingHints.KEY_INTERPOLATION,
75    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
76  0 graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
77  0 return thumbImage;
78    }
79   
80    /**
81    * Create a reduced image (thumb) of an image from the given input stream.
82    * Possible output formats are "jpg" or "png" (no "gif"; it is possible to
83    * read "gif" files, but not to write). The resulting thumb is written to
84    * the output stream. The both streams will be explicitly closed by this
85    * method. This method keeps the ratio width/height of the initial image. If
86    * both dimensions of the initial image is less than the specified
87    * boundaries it is not re-scaled; it is written to the output stream as is.
88    *
89    * @param input the input stream containing the image to reduce
90    * @param output the output stream where the resulting reduced image is
91    * written
92    * @param thumbWidth the maximal width of the reduced image
93    * @param thumbHeight the maximal height of the reduced image
94    * @param format the output format of the reduced image; it can be "jpg",
95    * "png", ...
96    */
 
97  0 toggle public static void createThumb(
98    InputStream input,
99    OutputStream output,
100    int thumbWidth,
101    int thumbHeight,
102    String format) throws IOException
103    {
104  0 try {
105  0 try {
106  0 ImageInputStream imageInput = ImageIO
107    .createImageInputStream(input);
108  0 BufferedImage image = ImageIO.read(imageInput);
109  0 BufferedImage thumbImage = createThumb(
110    image,
111    thumbWidth,
112    thumbHeight);
113  0 ImageIO.write(thumbImage, format, output);
114    } finally {
115  0 output.close();
116    }
117    } finally {
118  0 input.close();
119    }
120    }
121   
122    /**
123    * Returns the size (width and height) of an image from the given input
124    * stream. This method closes the given stream.
125    *
126    * @param input the input stream with an image
127    * @return the size (width and height) of an image from the given input
128    * stream
129    */
 
130  0 toggle public static int[] getImageSize(InputStream input) throws IOException
131    {
132  0 try {
133  0 ImageInputStream imageInput = ImageIO.createImageInputStream(input);
134  0 BufferedImage image = ImageIO.read(imageInput);
135  0 return new int[]{image.getWidth(), image.getHeight()};
136    } finally {
137  0 input.close();
138    }
139    }
140   
141    /**
142    * Returns the possible size of an image from the given input stream; the
143    * returned size does not overcome the specified maximal borders but keeps
144    * the ratio of the image. This method closes the given stream.
145    *
146    * @param input the input stream with an image
147    * @param maxWidth the maximal width
148    * @param maxHeight the maximal height
149    * @return the possible size of an image from the given input stream; the
150    * returned size does not overcome the specified maximal borders but
151    * keeps the ratio of the image
152    */
 
153  0 toggle public static int[] getImageSize(
154    InputStream input,
155    int maxWidth,
156    int maxHeight) throws IOException
157    {
158  0 int[] size = getImageSize(input);
159  0 return getNewSize(size[0], size[1], maxWidth, maxHeight);
160    }
161   
162    /**
163    * Calculates new size of an image with the specified max borders keeping
164    * the ratio between height and width of the image.
165    *
166    * @param width the initial width of an image
167    * @param height the initial height of an image
168    * @param maxWidth the maximal width of an image
169    * @param maxHeight the maximal height of an image
170    * @return a new size of an image where the height and width don't overcome
171    * the specified borders; the size keeps the initial image ratio
172    * between width and height
173    */
 
174  0 toggle public static int[] getNewSize(
175    int width,
176    int height,
177    int maxWidth,
178    int maxHeight)
179    {
180  0 if (width <= maxWidth && height <= maxHeight) {
181  0 return new int[]{width, height};
182    }
183  0 double thumbRatio = (double) maxWidth / (double) maxHeight;
184  0 double imageRatio = (double) width / (double) height;
185  0 if (thumbRatio < imageRatio) {
186  0 maxHeight = (int) (maxWidth / imageRatio);
187    } else {
188  0 maxWidth = (int) (maxHeight * imageRatio);
189    }
190  0 return new int[]{maxWidth, maxHeight};
191    }
192    }