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