001    /**
002     * Copyright (c) 2000-2013 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 com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    import com.liferay.portal.model.Image;
019    
020    import java.awt.image.BufferedImage;
021    import java.awt.image.RenderedImage;
022    
023    import java.io.File;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.io.OutputStream;
027    
028    import java.util.concurrent.Future;
029    
030    /**
031     * The Image utility class.
032     *
033     * @author Brian Wing Shun Chan
034     * @author Alexander Chow
035     */
036    public class ImageToolUtil {
037    
038            /**
039             * Returns the CMYK image converted to RGB using ImageMagick. This must be
040             * run against the original <code>byte[]</code> and not one extracted from a
041             * {@link java.awt.image.RenderedImage}. The latter may potentially have
042             * been already been read incorrectly.
043             *
044             * @param  bytes the image to convert
045             * @param  type the image type (e.g., "gif", "jpg", etc.)
046             * @return the asynchronous process converting the image or <code>null
047             *         </code> if ImageMagick was disabled or if the conversion could
048             *         not be completed. The conversion may not complete if (1) the
049             *         image was not in the CMYK colorspace to begin with or (2) there
050             *         was an error in the conversion process.
051             */
052            public static Future<RenderedImage> convertCMYKtoRGB(
053                    byte[] bytes, String type) {
054    
055                    return getImageTool().convertCMYKtoRGB(bytes, type);
056            }
057    
058            /**
059             * Returns the image converted to the type.
060             *
061             * @param  sourceImage the image to convert
062             * @param  type the image type to convert to (e.g., "gif", "jpg", etc.)
063             * @return the converted image
064             */
065            public static BufferedImage convertImageType(
066                    BufferedImage sourceImage, int type) {
067    
068                    return getImageTool().convertImageType(sourceImage, type);
069            }
070    
071            /**
072             * Encodes the image using the GIF format.
073             *
074             * @param  renderedImage the image to encode
075             * @param  os the stream to write to
076             * @throws IOException if an IO exception occurred
077             */
078            public static void encodeGIF(RenderedImage renderedImage, OutputStream os)
079                    throws IOException {
080    
081                    getImageTool().encodeGIF(renderedImage, os);
082            }
083    
084            /**
085             * Encodes the image using the WBMP format.
086             *
087             * @param  renderedImage the image to encode
088             * @param  os the stream to write to
089             * @throws IOException if an IO exception occurred
090             */
091            public static void encodeWBMP(RenderedImage renderedImage, OutputStream os)
092                    throws IOException {
093    
094                    getImageTool().encodeWBMP(renderedImage, os);
095            }
096    
097            /**
098             * Returns the rendered image as a {@link java.awt.image.BufferedImage}.
099             *
100             * @param  renderedImage the original image
101             * @return the converted image
102             */
103            public static BufferedImage getBufferedImage(RenderedImage renderedImage) {
104                    return getImageTool().getBufferedImage(renderedImage);
105            }
106    
107            /**
108             * Returns the image as a <code>byte[]</code>.
109             *
110             * @param  renderedImage the image to read
111             * @param  contentType the content type (e.g., "image/jpeg") or image type
112             *         (e.g., "jpg") to use during encoding
113             * @return the encoded image
114             * @throws IOException if an IO exception occurred
115             */
116            public static byte[] getBytes(
117                            RenderedImage renderedImage, String contentType)
118                    throws IOException {
119    
120                    return getImageTool().getBytes(renderedImage, contentType);
121            }
122    
123            public static Image getDefaultCompanyLogo() {
124                    return getImageTool().getDefaultCompanyLogo();
125            }
126    
127            public static Image getDefaultOrganizationLogo() {
128                    return getImageTool().getDefaultOrganizationLogo();
129            }
130    
131            public static Image getDefaultSpacer() {
132                    return getImageTool().getDefaultSpacer();
133            }
134    
135            public static Image getDefaultUserFemalePortrait() {
136                    return getImageTool().getDefaultUserFemalePortrait();
137            }
138    
139            public static Image getDefaultUserMalePortrait() {
140                    return getImageTool().getDefaultUserMalePortrait();
141            }
142    
143            public static Image getImage(byte[] bytes) throws IOException {
144                    return getImageTool().getImage(bytes);
145            }
146    
147            public static Image getImage(File file) throws IOException {
148    
149                    return getImageTool().getImage(file);
150            }
151    
152            public static Image getImage(InputStream is) throws IOException {
153    
154                    return getImageTool().getImage(is);
155            }
156    
157            public static Image getImage(InputStream is, boolean cleanUpStream)
158                    throws IOException {
159    
160                    return getImageTool().getImage(is, cleanUpStream);
161            }
162    
163            public static ImageTool getImageTool() {
164                    PortalRuntimePermission.checkGetBeanProperty(ImageToolUtil.class);
165    
166                    return _imageTool;
167            }
168    
169            public static boolean isNullOrDefaultSpacer(byte[] bytes) {
170                    return getImageTool().isNullOrDefaultSpacer(bytes);
171            }
172    
173            /**
174             * Detects the image format and creates an {@link
175             * com.liferay.portal.kernel.image.ImageBag} containing the {@link
176             * java.awt.image.RenderedImage} and image type.
177             *
178             * @param  bytes the bytes to read
179             * @return the {@link com.liferay.portal.kernel.image.ImageBag}
180             * @throws IOException if an IO exception occurred
181             */
182            public static ImageBag read(byte[] bytes) throws IOException {
183                    return getImageTool().read(bytes);
184            }
185    
186            /**
187             * Detects the image format and creates an {@link
188             * com.liferay.portal.kernel.image.ImageBag} containing the {@link
189             * java.awt.image.RenderedImage} and image type.
190             *
191             * @param  file the file to read
192             * @return the {@link com.liferay.portal.kernel.image.ImageBag}
193             * @throws IOException if an IO exception occurred
194             */
195            public static ImageBag read(File file) throws IOException {
196                    return getImageTool().read(file);
197            }
198    
199            public static ImageBag read(InputStream inputStream) throws IOException {
200                    return getImageTool().read(inputStream);
201            }
202    
203            /**
204             * Returns the scaled image based on the given width with the height
205             * calculated to preserve aspect ratio.
206             *
207             * @param  renderedImage the image to scale
208             * @param  width the new width; also used to calculate the new height
209             * @return the scaled image
210             */
211            public static RenderedImage scale(RenderedImage renderedImage, int width) {
212                    return getImageTool().scale(renderedImage, width);
213            }
214    
215            /**
216             * Returns the scaled image based on the maximum height and width given
217             * while preserving the aspect ratio. If the image is already larger in both
218             * dimensions, the image will not be scaled.
219             *
220             * @param  renderedImage the image to scale
221             * @param  maxHeight the maximum height allowed for image
222             * @param  maxWidth the maximum width allowed for image
223             * @return the scaled image
224             */
225            public static RenderedImage scale(
226                    RenderedImage renderedImage, int maxHeight, int maxWidth) {
227    
228                    return getImageTool().scale(renderedImage, maxHeight, maxWidth);
229            }
230    
231            /**
232             * Encodes the image using the content or image type.
233             *
234             * @param  renderedImage the image to encode
235             * @param  contentType the content type (e.g., "image/jpeg") or image type
236             *         (e.g., "jpg") to use during encoding
237             * @param  os the stream to write to
238             * @throws IOException if an IO exception occurred
239             */
240            public static void write(
241                            RenderedImage renderedImage, String contentType, OutputStream os)
242                    throws IOException {
243    
244                    getImageTool().write(renderedImage, contentType, os);
245            }
246    
247            public void setImageTool(ImageTool imageTool) {
248                    PortalRuntimePermission.checkSetBeanProperty(getClass());
249    
250                    _imageTool = imageTool;
251            }
252    
253            private static ImageTool _imageTool;
254    
255    }