001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public static RenderedImage crop(
072                    RenderedImage renderedImage, int height, int width, int x, int y) {
073    
074                    return getImageTool().crop(renderedImage, height, width, x, y);
075            }
076    
077            /**
078             * Encodes the image using the GIF format.
079             *
080             * @param  renderedImage the image to encode
081             * @param  os the stream to write to
082             * @throws IOException if an IO exception occurred
083             */
084            public static void encodeGIF(RenderedImage renderedImage, OutputStream os)
085                    throws IOException {
086    
087                    getImageTool().encodeGIF(renderedImage, os);
088            }
089    
090            /**
091             * Encodes the image using the WBMP format.
092             *
093             * @param  renderedImage the image to encode
094             * @param  os the stream to write to
095             * @throws IOException if an IO exception occurred
096             */
097            public static void encodeWBMP(RenderedImage renderedImage, OutputStream os)
098                    throws IOException {
099    
100                    getImageTool().encodeWBMP(renderedImage, os);
101            }
102    
103            /**
104             * Returns the rendered image as a {@link java.awt.image.BufferedImage}.
105             *
106             * @param  renderedImage the original image
107             * @return the converted image
108             */
109            public static BufferedImage getBufferedImage(RenderedImage renderedImage) {
110                    return getImageTool().getBufferedImage(renderedImage);
111            }
112    
113            /**
114             * Returns the image as a <code>byte[]</code>.
115             *
116             * @param  renderedImage the image to read
117             * @param  contentType the content type (e.g., "image/jpeg") or image type
118             *         (e.g., "jpg") to use during encoding
119             * @return the encoded image
120             * @throws IOException if an IO exception occurred
121             */
122            public static byte[] getBytes(
123                            RenderedImage renderedImage, String contentType)
124                    throws IOException {
125    
126                    return getImageTool().getBytes(renderedImage, contentType);
127            }
128    
129            public static Image getDefaultCompanyLogo() {
130                    return getImageTool().getDefaultCompanyLogo();
131            }
132    
133            public static Image getDefaultOrganizationLogo() {
134                    return getImageTool().getDefaultOrganizationLogo();
135            }
136    
137            public static Image getDefaultSpacer() {
138                    return getImageTool().getDefaultSpacer();
139            }
140    
141            public static Image getDefaultUserFemalePortrait() {
142                    return getImageTool().getDefaultUserFemalePortrait();
143            }
144    
145            public static Image getDefaultUserMalePortrait() {
146                    return getImageTool().getDefaultUserMalePortrait();
147            }
148    
149            public static Image getImage(byte[] bytes) throws IOException {
150                    return getImageTool().getImage(bytes);
151            }
152    
153            public static Image getImage(File file) throws IOException {
154    
155                    return getImageTool().getImage(file);
156            }
157    
158            public static Image getImage(InputStream is) throws IOException {
159    
160                    return getImageTool().getImage(is);
161            }
162    
163            public static Image getImage(InputStream is, boolean cleanUpStream)
164                    throws IOException {
165    
166                    return getImageTool().getImage(is, cleanUpStream);
167            }
168    
169            public static ImageTool getImageTool() {
170                    PortalRuntimePermission.checkGetBeanProperty(ImageToolUtil.class);
171    
172                    return _imageTool;
173            }
174    
175            public static boolean isNullOrDefaultSpacer(byte[] bytes) {
176                    return getImageTool().isNullOrDefaultSpacer(bytes);
177            }
178    
179            /**
180             * Detects the image format and creates an {@link
181             * com.liferay.portal.kernel.image.ImageBag} containing the {@link
182             * java.awt.image.RenderedImage} and image type.
183             *
184             * @param  bytes the bytes to read
185             * @return the {@link com.liferay.portal.kernel.image.ImageBag}
186             * @throws IOException if an IO exception occurred
187             */
188            public static ImageBag read(byte[] bytes) throws IOException {
189                    return getImageTool().read(bytes);
190            }
191    
192            /**
193             * Detects the image format and creates an {@link
194             * com.liferay.portal.kernel.image.ImageBag} containing the {@link
195             * java.awt.image.RenderedImage} and image type.
196             *
197             * @param  file the file to read
198             * @return the {@link com.liferay.portal.kernel.image.ImageBag}
199             * @throws IOException if an IO exception occurred
200             */
201            public static ImageBag read(File file) throws IOException {
202                    return getImageTool().read(file);
203            }
204    
205            public static ImageBag read(InputStream inputStream) throws IOException {
206                    return getImageTool().read(inputStream);
207            }
208    
209            /**
210             * Returns the scaled image based on the given width with the height
211             * calculated to preserve aspect ratio.
212             *
213             * @param  renderedImage the image to scale
214             * @param  width the new width; also used to calculate the new height
215             * @return the scaled image
216             */
217            public static RenderedImage scale(RenderedImage renderedImage, int width) {
218                    return getImageTool().scale(renderedImage, width);
219            }
220    
221            /**
222             * Returns the scaled image based on the maximum height and width given
223             * while preserving the aspect ratio. If the image is already larger in both
224             * dimensions, the image will not be scaled.
225             *
226             * @param  renderedImage the image to scale
227             * @param  maxHeight the maximum height allowed for image
228             * @param  maxWidth the maximum width allowed for image
229             * @return the scaled image
230             */
231            public static RenderedImage scale(
232                    RenderedImage renderedImage, int maxHeight, int maxWidth) {
233    
234                    return getImageTool().scale(renderedImage, maxHeight, maxWidth);
235            }
236    
237            /**
238             * Encodes the image using the content or image type.
239             *
240             * @param  renderedImage the image to encode
241             * @param  contentType the content type (e.g., "image/jpeg") or image type
242             *         (e.g., "jpg") to use during encoding
243             * @param  os the stream to write to
244             * @throws IOException if an IO exception occurred
245             */
246            public static void write(
247                            RenderedImage renderedImage, String contentType, OutputStream os)
248                    throws IOException {
249    
250                    getImageTool().write(renderedImage, contentType, os);
251            }
252    
253            public void setImageTool(ImageTool imageTool) {
254                    PortalRuntimePermission.checkSetBeanProperty(getClass());
255    
256                    _imageTool = imageTool;
257            }
258    
259            private static ImageTool _imageTool;
260    
261    }