KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > content > utils > ImageConverter


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.resources.content.utils;
20
21 import java.awt.image.*;
22 import java.awt.image.renderable.ParameterBlock JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.logging.*;
25 import java.util.logging.Level JavaDoc;
26
27 import javax.imageio.ImageIO JavaDoc;
28 import javax.media.jai.*;
29 import javax.media.jai.operator.OverlayDescriptor;
30
31 /**
32  * Utility class which uses the java JAI extensions to perform image
33  * manipulations.
34  *
35  * @author Michael Bell
36  * @version $Revision: 1.2 $
37  *
38  */

39 public class ImageConverter {
40
41     /**
42      * Logger for this class.
43      */

44     private static final Logger m_logger = Logger.getLogger(ImageConverter.class.getName());
45     
46     /**
47      * Returns the height of the specified image.
48      *
49      * @param file the image file
50      * @return the height of the specified image
51      */

52     static public int getImageHeight(File JavaDoc file) {
53         int nHeight = -1;
54
55         PlanarImage image = readImage(file.getAbsolutePath());
56
57         if (image != null) {
58             nHeight = image.getHeight();
59         }
60
61         return nHeight;
62
63     }
64
65     /**
66      * Returns the width of the specified image.
67      *
68      * @param file the image file
69      * @return the width of the specified image
70      */

71     static public int getImageWidth(File JavaDoc file) {
72         int nHeight = -1;
73
74         PlanarImage image = readImage(file.getAbsolutePath());
75
76         if (image != null) {
77             nHeight = image.getWidth();
78         }
79
80         return nHeight;
81
82     }
83
84     /**
85      * Returns a copy of the given image in the specified image format, name
86      * and height.
87      *
88      * @param file the image file
89      * @param sEndType the required image format
90      * @param sNewName the required image name
91      * @param nHeight the required image height
92      * @return a copy of the given image in the specified image format and
93      * height
94      * @throws Exception if any errors occur
95      */

96     public static File JavaDoc convertImage(File JavaDoc file, String JavaDoc sEndType, String JavaDoc sNewName, int nHeight)
97         throws Exception JavaDoc {
98
99         return convertImage(file.getAbsolutePath(), sEndType, sNewName, nHeight);
100     }
101
102     /**
103      * Returns a copy of the image at the given file location in the
104      * specified image format, name and height.
105      *
106      * @param sFilename the image file location
107      * @param sEndType the required image format
108      * @param sNewName the required image name
109      * @param nHeight the requried image height
110      * @return a copy of the image at the given file location in the
111      * specified image format and height
112      * @throws Exception if any errors occur
113      */

114     public static File JavaDoc convertImage(
115         String JavaDoc sFilename,
116         String JavaDoc sEndType,
117         String JavaDoc sNewName,
118         int nHeight)
119         throws Exception JavaDoc {
120
121         PlanarImage croppedImage = cropImage(readImage(sFilename));
122         RenderedOp image = scaleImage(croppedImage, nHeight);
123
124         if(sNewName.equals(sFilename)){
125             sNewName = sNewName + "_copy";
126         }
127         sNewName = sNewName + "." + sEndType;
128
129         File JavaDoc newFile = saveImage(image, sEndType, sNewName);
130
131         return newFile;
132     }
133
134     /**
135      * Returns a new version of the given image in the specified image format
136      * and name.
137      *
138      * @param file the image file
139      * @param sEndType the required image format
140      * @param sNewName the required image name
141      * @return a new version of the given image of the specified image format
142      * @throws Exception if any errors occur
143      */

144     public static File JavaDoc convertImage(File JavaDoc file, String JavaDoc sEndType, String JavaDoc sNewName)
145         throws Exception JavaDoc {
146         return convertImage(file.getAbsolutePath(), sEndType, sNewName);
147     }
148
149     /**
150      * Returns a new version of the given image in the specified image format
151      * and name.
152      *
153      * @param sFilename the image file location
154      * @param sEndType the required image format
155      * @param sNewName the required image name
156      * @return a new version of the given image in the specified image format
157      * @throws Exception if any errors occur
158      */

159     public static File JavaDoc convertImage(String JavaDoc sFilename, String JavaDoc sEndType, String JavaDoc sNewName)
160         throws Exception JavaDoc {
161
162          PlanarImage croppedImage = cropImage(readImage(sFilename));
163          RenderedOp image = scaleImage(croppedImage);
164
165          if(sNewName.equals(sFilename)){
166             sNewName = sNewName + "_copy";
167         }
168         sNewName = sNewName + "." + sEndType;
169
170         File JavaDoc newFile = saveImage(image, sEndType, sNewName);
171
172         return newFile;
173     }
174
175     /**
176      * Saves the given <code>RenderedImage</code> as a <code>File</code>
177      * with the specified location and image format.
178      *
179      * @param image the image
180      * @param sType the required image format
181      * @param sAddress the file location to save the image to
182      * @return the resultant image <code>File</code>
183      * @throws Exception if any errors occur
184      */

185     protected static File JavaDoc saveImage(
186         RenderedImage image,
187         String JavaDoc sType,
188         String JavaDoc sAddress)
189         throws Exception JavaDoc {
190         File JavaDoc file = new File JavaDoc(sAddress);
191         ImageIO.write(image, sType, file);
192
193         return file;
194     }
195
196     /**
197      * Scales the given <code>PlanerImage</code> such that the resultant
198      * <code>RendererOp</code> image has the specified height.
199      *
200      * @param im the image
201      * @param nHeight the required image height
202      * @return the resultant scaled image
203      */

204     protected static RenderedOp scaleImage(PlanarImage im, int nHeight) {
205
206         int nCurrentHeight = im.getHeight();
207         float fScale = (float) nHeight / (float) nCurrentHeight;
208
209         return scaleImage(im, fScale);
210     }
211
212     /**
213      * Returns the <code>RenderedOp</code> image representation of the
214      * given <code>PlanarImage</code>.
215      *
216      * @param im the <code>PlanarImage</code>
217      * @return the <code>RenderedOp</code> image representation
218      */

219     protected static RenderedOp scaleImage(PlanarImage im) {
220         return scaleImage(im, 1F);
221     }
222
223     /**
224      * Scales the given image by the specified scaling factor.
225      *
226      * @param im the image
227      * @param fScale the scaling factor
228      * @return the scaled image
229      */

230     protected static RenderedOp scaleImage(PlanarImage im, float fScale) {
231         ParameterBlock JavaDoc pb = new ParameterBlock JavaDoc();
232
233         pb.addSource(im);
234         pb.add(fScale); //x scale factor
235
pb.add(fScale); //y scale factor
236
pb.add(0.0F); //x trans
237
pb.add(0.0F); //y trans
238
Interpolation interp =
239             Interpolation.getInstance(Interpolation.INTERP_NEAREST);
240         pb.add(interp);
241
242         RenderedOp rop = JAI.create("scale", pb, null);
243         return rop;
244     }
245
246     /**
247      * Returns the <code>PlanarImage</code> representation of the image file
248      * at the specified file location.
249      *
250      * @param filename the image file location
251      * @return the <code>PlanarImage</code> representation of the image file
252      */

253     protected static PlanarImage readImage(String JavaDoc filename) {
254         PlanarImage image = null;
255
256         image = JAI.create("fileload", filename);
257
258         // If the source image is colormapped, convert it to 3-band RGB.
259
if (image.getColorModel() instanceof IndexColorModel) {
260             // Retrieve the IndexColorModel
261
IndexColorModel icm = (IndexColorModel) image.getColorModel();
262
263             // Cache the number of elements in each band of the colormap.
264
int mapSize = icm.getMapSize();
265
266             // Allocate an array for the lookup table data.
267
byte[][] lutData = new byte[3][mapSize];
268
269             // Load the lookup table data from the IndexColorModel.
270
icm.getReds(lutData[0]);
271             icm.getGreens(lutData[1]);
272             icm.getBlues(lutData[2]);
273
274             // Create the lookup table object.
275
LookupTableJAI lut = new LookupTableJAI(lutData);
276
277             // Replace the original image with the 3-band RGB image.
278
image = JAI.create("lookup", image, lut);
279         }
280
281         return image;
282     }
283
284     /**
285      * Returns the resultant image file of squaring the given image file such
286      * that the side of the resultant square has the specified size.
287      *
288      * @param file the orginal image file
289      * @param nSize the required height/width of the square
290      * @return the resultant square image file
291      */

292     protected static File JavaDoc getSquareImage(File JavaDoc file, int nSize) {
293         File JavaDoc sqrImage = null;
294
295         PlanarImage oldImage = readImage(file.getAbsolutePath());
296
297         if (oldImage.getHeight() != nSize && oldImage.getWidth() != nSize) {
298             
299             ParameterBlock JavaDoc params = new ParameterBlock JavaDoc();
300             params.addSource(oldImage);
301
302             PlanarImage whiteImage = readImage("C:\\Temp\\white.jpg");
303             
304             int nHeight = whiteImage.getHeight();
305             
306             RenderedOp scaledWhile = scaleImage(whiteImage, nSize);
307             
308             RenderedOp op = OverlayDescriptor.create(scaledWhile,oldImage, null);
309             
310             try {
311                 sqrImage = saveImage(op, "jpg", "C:\\Temp\\square.jpg");
312             } catch (Exception JavaDoc e) {
313                 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
314             }
315
316         }
317
318         return sqrImage;
319     }
320     
321     protected static PlanarImage cropImage(PlanarImage image){
322         int width = image.getWidth();
323         int height = image.getHeight();
324         if(width != height){
325             float size = height;
326             if(width < height){
327                 size = width;
328             }
329             ParameterBlock JavaDoc pb = new ParameterBlock JavaDoc();
330             pb.addSource(image);
331             pb.add(0.0F); //x trans
332
pb.add(0.0F); //y trans
333
pb.add(size); //width
334
pb.add(size); //height
335
// Create the output image by cropping the input image.
336
return JAI.create("crop",pb,null);
337         } else {
338             return image;
339         }
340     }
341
342     /**
343      * Main method to enable running class as an application in order
344      * to test methods.
345      *
346      * @param args command line arguments
347      */

348     public static void main(String JavaDoc[] args) {
349
350         PlanarImage image = cropImage(readImage("C:\\projects\\sim site\\new html\\simulacra_html\\images\\content\\info_architecture_image.jpg"));
351
352         try {
353             saveImage(image, "jpg", "C:\\Temp\\test.jpg");
354         }
355         catch (Exception JavaDoc e) {
356             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
357         }
358     }
359
360 }
361
Popular Tags