001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.image;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.exception.ImageResolutionException;
020    import com.liferay.portal.kernel.model.Image;
021    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
022    
023    import java.awt.image.BufferedImage;
024    import java.awt.image.RenderedImage;
025    
026    import java.io.File;
027    import java.io.IOException;
028    import java.io.InputStream;
029    import java.io.OutputStream;
030    
031    import java.util.concurrent.Future;
032    
033    /**
034     * The Image utility class.
035     *
036     * @author Brian Wing Shun Chan
037     * @author Alexander Chow
038     */
039    @ProviderType
040    public class ImageToolUtil {
041    
042            /**
043             * Returns the CMYK image converted to RGB using ImageMagick. This must be
044             * run against the original <code>byte[]</code> and not one extracted from a
045             * {@link RenderedImage}. The latter may potentially have been already been
046             * read incorrectly.
047             *
048             * @param  bytes the image to convert
049             * @param  type the image type (e.g., "gif", "jpg", etc.)
050             * @return the asynchronous process converting the image or <code>null
051             *         </code> if ImageMagick was disabled or if the conversion could
052             *         not be completed. The conversion may not complete if (1) the
053             *         image was not in the CMYK colorspace to begin with or (2) there
054             *         was an error in the conversion process.
055             */
056            public static Future<RenderedImage> convertCMYKtoRGB(
057                    byte[] bytes, String type) {
058    
059                    return getImageTool().convertCMYKtoRGB(bytes, type);
060            }
061    
062            /**
063             * Returns the image converted to the type.
064             *
065             * @param  sourceImage the image to convert
066             * @param  type the image type to convert to (e.g., "gif", "jpg", etc.)
067             * @return the converted image
068             */
069            public static BufferedImage convertImageType(
070                    BufferedImage sourceImage, int type) {
071    
072                    return getImageTool().convertImageType(sourceImage, type);
073            }
074    
075            public static RenderedImage crop(
076                    RenderedImage renderedImage, int height, int width, int x, int y) {
077    
078                    return getImageTool().crop(renderedImage, height, width, x, y);
079            }
080    
081            /**
082             * Encodes the image using the GIF format.
083             *
084             * @param  renderedImage the image to encode
085             * @param  os the stream to write to
086             * @throws IOException if an IO exception occurred
087             */
088            public static void encodeGIF(RenderedImage renderedImage, OutputStream os)
089                    throws IOException {
090    
091                    getImageTool().encodeGIF(renderedImage, os);
092            }
093    
094            /**
095             * Encodes the image using the WBMP format.
096             *
097             * @param  renderedImage the image to encode
098             * @param  os the stream to write to
099             * @throws IOException if an IO exception occurred
100             */
101            public static void encodeWBMP(RenderedImage renderedImage, OutputStream os)
102                    throws IOException {
103    
104                    getImageTool().encodeWBMP(renderedImage, os);
105            }
106    
107            /**
108             * Returns the rendered image as a {@link BufferedImage}.
109             *
110             * @param  renderedImage the original image
111             * @return the converted image
112             */
113            public static BufferedImage getBufferedImage(RenderedImage renderedImage) {
114                    return getImageTool().getBufferedImage(renderedImage);
115            }
116    
117            /**
118             * Returns the image as a <code>byte[]</code>.
119             *
120             * @param  renderedImage the image to read
121             * @param  contentType the content type (e.g., "image/jpeg") or image type
122             *         (e.g., "jpg") to use during encoding
123             * @return the encoded image
124             * @throws IOException if an IO exception occurred
125             */
126            public static byte[] getBytes(
127                            RenderedImage renderedImage, String contentType)
128                    throws IOException {
129    
130                    return getImageTool().getBytes(renderedImage, contentType);
131            }
132    
133            public static Image getDefaultCompanyLogo() {
134                    return getImageTool().getDefaultCompanyLogo();
135            }
136    
137            public static Image getDefaultOrganizationLogo() {
138                    return getImageTool().getDefaultOrganizationLogo();
139            }
140    
141            public static Image getDefaultSpacer() {
142                    return getImageTool().getDefaultSpacer();
143            }
144    
145            public static Image getDefaultUserFemalePortrait() {
146                    return getImageTool().getDefaultUserFemalePortrait();
147            }
148    
149            public static Image getDefaultUserMalePortrait() {
150                    return getImageTool().getDefaultUserMalePortrait();
151            }
152    
153            public static Image getImage(byte[] bytes)
154                    throws ImageResolutionException, IOException {
155    
156                    return getImageTool().getImage(bytes);
157            }
158    
159            public static Image getImage(File file)
160                    throws ImageResolutionException, IOException {
161    
162                    return getImageTool().getImage(file);
163            }
164    
165            public static Image getImage(InputStream is)
166                    throws ImageResolutionException, IOException {
167    
168                    return getImageTool().getImage(is);
169            }
170    
171            public static Image getImage(InputStream is, boolean cleanUpStream)
172                    throws ImageResolutionException, IOException {
173    
174                    return getImageTool().getImage(is, cleanUpStream);
175            }
176    
177            public static ImageTool getImageTool() {
178                    PortalRuntimePermission.checkGetBeanProperty(ImageToolUtil.class);
179    
180                    return _imageTool;
181            }
182    
183            public static boolean isNullOrDefaultSpacer(byte[] bytes) {
184                    return getImageTool().isNullOrDefaultSpacer(bytes);
185            }
186    
187            /**
188             * Detects the image format and creates an {@link
189             * ImageBag} containing the {@link
190             * RenderedImage} and image type.
191             *
192             * @param  bytes the bytes to read
193             * @return the {@link ImageBag}
194             * @throws ImageResolutionException if the image's dimensions were larger
195             *         than those specified by portal properties
196             *         <code>image.tool.image.max.height</code> and
197             *         <code>image.tool.image.max.width</code>
198             * @throws IOException if an IO exception occurred
199             */
200            public static ImageBag read(byte[] bytes)
201                    throws ImageResolutionException, IOException {
202    
203                    return getImageTool().read(bytes);
204            }
205    
206            /**
207             * Detects the image format and creates an {@link
208             * ImageBag} containing the {@link
209             * RenderedImage} and image type.
210             *
211             * @param  file the file to read
212             * @return the {@link ImageBag}
213             * @throws ImageResolutionException if the image's dimensions were larger
214             *         than those specified by portal properties
215             *         <code>image.tool.image.max.height</code> and
216             *         <code>image.tool.image.max.width</code>
217             * @throws IOException if an IO exception occurred
218             */
219            public static ImageBag read(File file)
220                    throws ImageResolutionException, IOException {
221    
222                    return getImageTool().read(file);
223            }
224    
225            public static ImageBag read(InputStream inputStream)
226                    throws ImageResolutionException, IOException {
227    
228                    return getImageTool().read(inputStream);
229            }
230    
231            /**
232             * Returns the scaled image based on the given width with the height
233             * calculated to preserve aspect ratio.
234             *
235             * @param  renderedImage the image to scale
236             * @param  width the new width; also used to calculate the new height
237             * @return the scaled image
238             */
239            public static RenderedImage scale(RenderedImage renderedImage, int width) {
240                    return getImageTool().scale(renderedImage, width);
241            }
242    
243            /**
244             * Returns the scaled image based on the maximum height and width given
245             * while preserving the aspect ratio. If the image is already larger in both
246             * dimensions, the image will not be scaled.
247             *
248             * @param  renderedImage the image to scale
249             * @param  maxHeight the maximum height allowed for image
250             * @param  maxWidth the maximum width allowed for image
251             * @return the scaled image
252             */
253            public static RenderedImage scale(
254                    RenderedImage renderedImage, int maxHeight, int maxWidth) {
255    
256                    return getImageTool().scale(renderedImage, maxHeight, maxWidth);
257            }
258    
259            /**
260             * Encodes the image using the content or image type.
261             *
262             * @param  renderedImage the image to encode
263             * @param  contentType the content type (e.g., "image/jpeg") or image type
264             *         (e.g., "jpg") to use during encoding
265             * @param  os the stream to write to
266             * @throws IOException if an IO exception occurred
267             */
268            public static void write(
269                            RenderedImage renderedImage, String contentType, OutputStream os)
270                    throws IOException {
271    
272                    getImageTool().write(renderedImage, contentType, os);
273            }
274    
275            public void setImageTool(ImageTool imageTool) {
276                    PortalRuntimePermission.checkSetBeanProperty(getClass());
277    
278                    _imageTool = imageTool;
279            }
280    
281            private static ImageTool _imageTool;
282    
283    }