2 * Copyright (C) 2005 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with any library licensed under the Apache Software License.
22 * You must obey the GNU General Public License in all respects for all of the code used
23 * other than the above mentioned libraries. If you modify this file, you may extend this
24 * exception to your version of the file, but you are not obligated to do so.
25 * If you do not wish to do so, delete this exception statement from your version.
27 package mir.media.image;
29 import mir.config.MirPropertiesConfiguration;
30 import mir.log.LoggerWrapper;
31 import mir.media.MediaExc;
32 import mir.util.ShellRoutines;
33 import mir.util.StreamCopier;
35 import java.io.BufferedOutputStream;
37 import java.io.FileInputStream;
38 import java.io.FileNotFoundException;
39 import java.io.FileOutputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.OutputStream;
43 import java.util.StringTokenizer;
47 * Image processing by calling the ImageMagick command line progrmas
48 * "convert" and "identify". The main task of this class is to scale
49 * images. The path to ImageMagick commandline programs can be
50 * specified in the configuration file.
52 * @author <grok@no-log.org>, the Mir-coders group
54 public class ImageMagickImageProcessor implements ImageProcessor {
55 protected static MirPropertiesConfiguration configuration =
56 MirPropertiesConfiguration.instance();
57 static final LoggerWrapper logger =
58 new LoggerWrapper("media.image.imagemagick");
60 private ImageFile sourceImage;
61 private ImageFile scaledImage;
63 public ImageMagickImageProcessor(InputStream inputImageStream)
65 logger.debug("ImageMagickImageProcessor(stream)");
66 sourceImage = new ImageFile();
67 // copy stream into temporary file
69 FileOutputStream outputStream = new FileOutputStream(sourceImage.file);
71 StreamCopier.copy(inputImageStream, outputStream);
76 sourceImage.readInfo();
77 sourceImage.debugOutput();
81 public ImageMagickImageProcessor(File aFile) throws IOException {
82 logger.debug("ImageMagickImageProcessor(file)");
83 sourceImage = new ImageFile(aFile);
84 sourceImage.debugOutput();
88 * Deletes temporary files
90 public void cleanup() {
91 logger.debug("ImageMagickImageProcessor.cleanup()");
92 sourceImage.cleanup();
93 scaledImage.cleanup();
97 * Path to ImageMagick commandline programs
99 private static String getImageMagickPath() {
100 String result = configuration.getString("Producer.Image.ImageMagickPath");
101 // we want the path to finish by "/", so add it if it's missing
102 if (result.length() != 0 && !result.endsWith("/")) {
103 result = result.concat("/");
105 logger.debug("getImageMagickPath:" + result);
109 public void descaleImage(int aMaxSize) throws MediaExc {
110 descaleImage(aMaxSize, 0);
113 public void descaleImage(int aMaxSize, float aMinDescale) throws MediaExc {
114 descaleImage(aMaxSize, aMaxSize, aMinDescale, 0);
117 public void descaleImage(int aMaxSize, int aMinResize) throws MediaExc {
118 descaleImage(aMaxSize, aMaxSize, 0, aMinResize);
121 public void descaleImage(int aMaxSize, float aMinDescale, int aMinResize)
123 descaleImage(aMaxSize, aMaxSize, aMinDescale, aMinResize);
129 public void descaleImage(int aMaxWidth, int aMaxHeight,
130 float aMinDescale, int aMinResize) throws MediaExc {
132 logger.debug("descaleImage:" +
133 " aMaxWidth:" + aMaxWidth +
134 ", aMaxHeight:" + aMaxHeight +
135 ", aMinDescale:" + aMinDescale +
136 ", aMinResize:" + aMinResize);
137 if ((aMaxWidth > 0 && getWidth() > aMaxWidth + aMinResize - 1) ||
138 (aMaxHeight > 0 && getHeight() > aMaxHeight + aMinResize - 1)) {
139 logger.debug("descaleImage: image needs scaling");
143 if (aMaxWidth > 0 && getWidth() > aMaxWidth) {
144 scale = Math.min(scale, (float) aMaxWidth / (float) getWidth());
146 if (aMaxHeight > 0 && getHeight() > aMaxHeight) {
147 scale = Math.min(scale, (float) aMaxHeight / (float) getHeight());
150 if (1 - scale > aMinDescale) {
157 // the image didn't need to be scaled: scaledImage = original image
159 scaledImage = new ImageFile(sourceImage.file);
161 catch (IOException e) {
162 throw new MediaExc(e.toString());
169 * Scales image by a factor using the convert ImageMagick command
171 public void scaleImage(float aScalingFactor) throws MediaExc {
172 logger.debug("scaleImage:" + aScalingFactor);
174 // first cleanup previous temp scaledimage file if necesary
175 if (scaledImage != null) {
176 scaledImage.cleanup();
178 // now create temp file and execute convert
179 scaledImage = sourceImage.scale(aScalingFactor);
181 catch (Exception e) {
182 throw new MediaExc(e.toString());
184 logger.debug(" scaledImage:");
185 scaledImage.debugOutput();
188 public int getWidth() {
189 return sourceImage.width;
192 public int getHeight() {
193 return sourceImage.height;
196 public int getScaledWidth() {
197 return scaledImage.width;
200 public int getScaledHeight() {
201 return scaledImage.height;
204 public void writeScaledData(OutputStream aStream, String anImageType) throws MediaExc, IOException {
205 // we can't asume that requested "anImageType" is the same as the
206 // scaled image type, so we have to convert
207 // if image is an animation and target type doesn't support
208 // animations, then just use first frame
210 scaledImage.debugOutput();
212 if (scaledImage.isAnimation && !anImageType.equals("GIF")) {
215 // ImageMagick "convert" into temp file
216 File temp = File.createTempFile("mirimage", "");
217 String command = getImageMagickPath() + "convert " +
218 scaledImage.file.getAbsolutePath() + frame + " " +
219 anImageType + ":" + temp.getAbsolutePath();
220 logger.debug("writeScaledData command:" + command);
221 ShellRoutines.simpleExec(command);
222 // copy temp file into stream
223 StreamCopier.copy(new FileInputStream(temp), aStream);
227 public void writeScaledData(File aFile, String anImageType) throws MediaExc, IOException, FileNotFoundException {
228 OutputStream stream = new BufferedOutputStream(new FileOutputStream(aFile), 8192);
231 writeScaledData(stream, anImageType);
237 catch (Throwable t) {
243 * ImageFile is a thin wrapper around a file that contains an
244 * image. It uses ImageMagick to retreive information about the
245 * image. It can also scale images using ImageMagick. Intended for
246 * use in the ImageMagickImageProcessor class.
248 static class ImageFile {
250 * path to the file represented by this class
254 * whether the file must be deleted on cleanup
256 boolean fileIsTemp = false;
258 * image information is stored here to avoid multiple costly calls to
264 * Image type as returned by identify %m : "PNG", "GIF", ...
268 * number of scenes in image >1 (typically animated gif)
273 * Empty constructor automatically creates a temporary file
274 * that will later hold an image
276 ImageFile() throws IOException {
277 file = File.createTempFile("mirimage", "");
282 * if the file doesn't already have an image in it
283 * we don't want to read its information
285 ImageFile(File file, boolean doReadInfo) throws IOException {
292 ImageFile(File file) throws IOException {
297 * delete temporary files
299 public void cleanup() {
300 logger.debug("ImageFile.cleanup()");
302 logger.debug("deleting:" + file);
310 logger.debug(" filename:" + file +
313 " height:" + height +
315 " isAnimation:" + isAnimation);
318 private void checkFile() throws IOException {
319 if (file == null || !file.exists()) {
320 String message = "ImageFile.checkFile file \"" + file +
322 logger.error(message);
323 throw new IOException(message);
328 * Uses the imagemagick "identify" command to retreive image information
330 public void readInfo() throws IOException {
332 String infoString = ShellRoutines.execIntoString
333 (getImageMagickPath() +
334 "identify " + "-format \"%w %h %m %n \" " +
335 file.getAbsolutePath()); // extra space, for multiframe (animations)
336 StringTokenizer st = new StringTokenizer(infoString);
337 width = Integer.parseInt(st.nextToken());
338 height = Integer.parseInt(st.nextToken());
339 type = st.nextToken();
340 isAnimation = Integer.parseInt(st.nextToken()) > 1;
343 public ImageFile scale(float aScalingFactor) throws IOException {
344 logger.debug("ImageFile.scale");
347 ImageFile result = new ImageFile();
348 String command = getImageMagickPath() + "convert " +
349 file.getAbsolutePath() + " " +
351 Float.toString(aScalingFactor * 100) + "% " +
352 result.file.getAbsolutePath();
353 logger.debug("ImageFile.scale:command:" + command);
354 ShellRoutines.simpleExec(command);