001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.resources.content.utils;
020:
021: import java.awt.image.*;
022: import java.awt.image.renderable.ParameterBlock;
023: import java.io.File;
024: import java.util.logging.*;
025: import java.util.logging.Level;
026:
027: import javax.imageio.ImageIO;
028: import javax.media.jai.*;
029: import javax.media.jai.operator.OverlayDescriptor;
030:
031: /**
032: * Utility class which uses the java JAI extensions to perform image
033: * manipulations.
034: *
035: * @author Michael Bell
036: * @version $Revision: 1.2 $
037: *
038: */
039: public class ImageConverter {
040:
041: /**
042: * Logger for this class.
043: */
044: private static final Logger m_logger = Logger
045: .getLogger(ImageConverter.class.getName());
046:
047: /**
048: * Returns the height of the specified image.
049: *
050: * @param file the image file
051: * @return the height of the specified image
052: */
053: static public int getImageHeight(File file) {
054: int nHeight = -1;
055:
056: PlanarImage image = readImage(file.getAbsolutePath());
057:
058: if (image != null) {
059: nHeight = image.getHeight();
060: }
061:
062: return nHeight;
063:
064: }
065:
066: /**
067: * Returns the width of the specified image.
068: *
069: * @param file the image file
070: * @return the width of the specified image
071: */
072: static public int getImageWidth(File file) {
073: int nHeight = -1;
074:
075: PlanarImage image = readImage(file.getAbsolutePath());
076:
077: if (image != null) {
078: nHeight = image.getWidth();
079: }
080:
081: return nHeight;
082:
083: }
084:
085: /**
086: * Returns a copy of the given image in the specified image format, name
087: * and height.
088: *
089: * @param file the image file
090: * @param sEndType the required image format
091: * @param sNewName the required image name
092: * @param nHeight the required image height
093: * @return a copy of the given image in the specified image format and
094: * height
095: * @throws Exception if any errors occur
096: */
097: public static File convertImage(File file, String sEndType,
098: String sNewName, int nHeight) throws Exception {
099:
100: return convertImage(file.getAbsolutePath(), sEndType, sNewName,
101: nHeight);
102: }
103:
104: /**
105: * Returns a copy of the image at the given file location in the
106: * specified image format, name and height.
107: *
108: * @param sFilename the image file location
109: * @param sEndType the required image format
110: * @param sNewName the required image name
111: * @param nHeight the requried image height
112: * @return a copy of the image at the given file location in the
113: * specified image format and height
114: * @throws Exception if any errors occur
115: */
116: public static File convertImage(String sFilename, String sEndType,
117: String sNewName, int nHeight) throws Exception {
118:
119: PlanarImage croppedImage = cropImage(readImage(sFilename));
120: RenderedOp image = scaleImage(croppedImage, nHeight);
121:
122: if (sNewName.equals(sFilename)) {
123: sNewName = sNewName + "_copy";
124: }
125: sNewName = sNewName + "." + sEndType;
126:
127: File newFile = saveImage(image, sEndType, sNewName);
128:
129: return newFile;
130: }
131:
132: /**
133: * Returns a new version of the given image in the specified image format
134: * and name.
135: *
136: * @param file the image file
137: * @param sEndType the required image format
138: * @param sNewName the required image name
139: * @return a new version of the given image of the specified image format
140: * @throws Exception if any errors occur
141: */
142: public static File convertImage(File file, String sEndType,
143: String sNewName) throws Exception {
144: return convertImage(file.getAbsolutePath(), sEndType, sNewName);
145: }
146:
147: /**
148: * Returns a new version of the given image in the specified image format
149: * and name.
150: *
151: * @param sFilename the image file location
152: * @param sEndType the required image format
153: * @param sNewName the required image name
154: * @return a new version of the given image in the specified image format
155: * @throws Exception if any errors occur
156: */
157: public static File convertImage(String sFilename, String sEndType,
158: String sNewName) throws Exception {
159:
160: PlanarImage croppedImage = cropImage(readImage(sFilename));
161: RenderedOp image = scaleImage(croppedImage);
162:
163: if (sNewName.equals(sFilename)) {
164: sNewName = sNewName + "_copy";
165: }
166: sNewName = sNewName + "." + sEndType;
167:
168: File newFile = saveImage(image, sEndType, sNewName);
169:
170: return newFile;
171: }
172:
173: /**
174: * Saves the given <code>RenderedImage</code> as a <code>File</code>
175: * with the specified location and image format.
176: *
177: * @param image the image
178: * @param sType the required image format
179: * @param sAddress the file location to save the image to
180: * @return the resultant image <code>File</code>
181: * @throws Exception if any errors occur
182: */
183: protected static File saveImage(RenderedImage image, String sType,
184: String sAddress) throws Exception {
185: File file = new File(sAddress);
186: ImageIO.write(image, sType, file);
187:
188: return file;
189: }
190:
191: /**
192: * Scales the given <code>PlanerImage</code> such that the resultant
193: * <code>RendererOp</code> image has the specified height.
194: *
195: * @param im the image
196: * @param nHeight the required image height
197: * @return the resultant scaled image
198: */
199: protected static RenderedOp scaleImage(PlanarImage im, int nHeight) {
200:
201: int nCurrentHeight = im.getHeight();
202: float fScale = (float) nHeight / (float) nCurrentHeight;
203:
204: return scaleImage(im, fScale);
205: }
206:
207: /**
208: * Returns the <code>RenderedOp</code> image representation of the
209: * given <code>PlanarImage</code>.
210: *
211: * @param im the <code>PlanarImage</code>
212: * @return the <code>RenderedOp</code> image representation
213: */
214: protected static RenderedOp scaleImage(PlanarImage im) {
215: return scaleImage(im, 1F);
216: }
217:
218: /**
219: * Scales the given image by the specified scaling factor.
220: *
221: * @param im the image
222: * @param fScale the scaling factor
223: * @return the scaled image
224: */
225: protected static RenderedOp scaleImage(PlanarImage im, float fScale) {
226: ParameterBlock pb = new ParameterBlock();
227:
228: pb.addSource(im);
229: pb.add(fScale); //x scale factor
230: pb.add(fScale); //y scale factor
231: pb.add(0.0F); //x trans
232: pb.add(0.0F); //y trans
233: Interpolation interp = Interpolation
234: .getInstance(Interpolation.INTERP_NEAREST);
235: pb.add(interp);
236:
237: RenderedOp rop = JAI.create("scale", pb, null);
238: return rop;
239: }
240:
241: /**
242: * Returns the <code>PlanarImage</code> representation of the image file
243: * at the specified file location.
244: *
245: * @param filename the image file location
246: * @return the <code>PlanarImage</code> representation of the image file
247: */
248: protected static PlanarImage readImage(String filename) {
249: PlanarImage image = null;
250:
251: image = JAI.create("fileload", filename);
252:
253: // If the source image is colormapped, convert it to 3-band RGB.
254: if (image.getColorModel() instanceof IndexColorModel) {
255: // Retrieve the IndexColorModel
256: IndexColorModel icm = (IndexColorModel) image
257: .getColorModel();
258:
259: // Cache the number of elements in each band of the colormap.
260: int mapSize = icm.getMapSize();
261:
262: // Allocate an array for the lookup table data.
263: byte[][] lutData = new byte[3][mapSize];
264:
265: // Load the lookup table data from the IndexColorModel.
266: icm.getReds(lutData[0]);
267: icm.getGreens(lutData[1]);
268: icm.getBlues(lutData[2]);
269:
270: // Create the lookup table object.
271: LookupTableJAI lut = new LookupTableJAI(lutData);
272:
273: // Replace the original image with the 3-band RGB image.
274: image = JAI.create("lookup", image, lut);
275: }
276:
277: return image;
278: }
279:
280: /**
281: * Returns the resultant image file of squaring the given image file such
282: * that the side of the resultant square has the specified size.
283: *
284: * @param file the orginal image file
285: * @param nSize the required height/width of the square
286: * @return the resultant square image file
287: */
288: protected static File getSquareImage(File file, int nSize) {
289: File sqrImage = null;
290:
291: PlanarImage oldImage = readImage(file.getAbsolutePath());
292:
293: if (oldImage.getHeight() != nSize
294: && oldImage.getWidth() != nSize) {
295:
296: ParameterBlock params = new ParameterBlock();
297: params.addSource(oldImage);
298:
299: PlanarImage whiteImage = readImage("C:\\Temp\\white.jpg");
300:
301: int nHeight = whiteImage.getHeight();
302:
303: RenderedOp scaledWhile = scaleImage(whiteImage, nSize);
304:
305: RenderedOp op = OverlayDescriptor.create(scaledWhile,
306: oldImage, null);
307:
308: try {
309: sqrImage = saveImage(op, "jpg", "C:\\Temp\\square.jpg");
310: } catch (Exception e) {
311: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
312: }
313:
314: }
315:
316: return sqrImage;
317: }
318:
319: protected static PlanarImage cropImage(PlanarImage image) {
320: int width = image.getWidth();
321: int height = image.getHeight();
322: if (width != height) {
323: float size = height;
324: if (width < height) {
325: size = width;
326: }
327: ParameterBlock pb = new ParameterBlock();
328: pb.addSource(image);
329: pb.add(0.0F); //x trans
330: pb.add(0.0F); //y trans
331: pb.add(size); //width
332: pb.add(size); //height
333: // Create the output image by cropping the input image.
334: return JAI.create("crop", pb, null);
335: } else {
336: return image;
337: }
338: }
339:
340: /**
341: * Main method to enable running class as an application in order
342: * to test methods.
343: *
344: * @param args command line arguments
345: */
346: public static void main(String[] args) {
347:
348: PlanarImage image = cropImage(readImage("C:\\projects\\sim site\\new html\\simulacra_html\\images\\content\\info_architecture_image.jpg"));
349:
350: try {
351: saveImage(image, "jpg", "C:\\Temp\\test.jpg");
352: } catch (Exception e) {
353: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
354: }
355: }
356:
357: }
|