1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/**************************************************************************
/* This class implements the processing of image-commands.
/*
/* Copyright (c) 2009 by Bernhard Bablok (mail@bablokb.de)
/*
/* This program is free software; you can redistribute it and/or modify
/* it under the terms of the GNU Library General Public License as published
/* by the Free Software Foundation; either version 2 of the License or
/* (at your option) any later version.
/*
/* This program is distributed in the hope that it will be useful, but
/* WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/* GNU Library General Public License for more details.
/*
/* You should have received a copy of the GNU Library General Public License
/* along with this program; see the file COPYING.LIB. If not, write to
/* the Free Software Foundation Inc., 59 Temple Place - Suite 330,
/* Boston, MA 02111-1307 USA
/**************************************************************************/
package org.im4java.core;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import javax.imageio.ImageIO;
import org.im4java.process.ErrorConsumer;
import org.im4java.process.ProcessStarter;
import org.im4java.process.StandardStream;
/**
This class implements the processing of image operations. It replaces
placeholders within the argument-stack and passes all arguments to the
generic run-method of ProcessStarter.
@version $Revision: 1.13 $
@author $Author: bablokb, Bruno Mascret (warnings) $
*/
public class ImageCommand extends ProcessStarter implements ErrorConsumer {
//////////////////////////////////////////////////////////////////////////////
/**
The command (plus initial arguments) to execute.
*/
private LinkedList<String> iCommands;
//////////////////////////////////////////////////////////////////////////////
/**
List of stderr-output.
*/
private ArrayList<String> iErrorText;
//////////////////////////////////////////////////////////////////////////////
/**
List of temporary files (input).
*/
private LinkedList<String> iTmpFiles;
//////////////////////////////////////////////////////////////////////////////
/**
Temporary output file.
*/
private String iTmpOutputFile;
//////////////////////////////////////////////////////////////////////////////
/**
* Constructor.
*/
public ImageCommand() {
super();
iCommands = new LinkedList<String>();
iTmpFiles = new LinkedList<String>();
setOutputConsumer(StandardStream.STDOUT);
setErrorConsumer(this);
}
//////////////////////////////////////////////////////////////////////////////
/**
* Constructor setting the commands.
*/
public ImageCommand(String... pCommands) {
this();
setCommand(pCommands);
}
//////////////////////////////////////////////////////////////////////////////
/**
* Set the command.
*/
public void setCommand(String... pCommands) {
for (String cmd:pCommands) {
iCommands.add(cmd);
}
}
//////////////////////////////////////////////////////////////////////////////
/**
Execute the command (replace given placeholders).
* @throws IM4JavaException
*/
public void run(Operation pOperation, Object... images)
throws IOException, InterruptedException, IM4JavaException {
// prepare list of arguments
LinkedList<String> args = new LinkedList<String>(pOperation.getCmdArgs());
args.addAll(0,iCommands);
resolveImages(args,images);
resolveDynamicOperations(pOperation,args,images);
//for(String s:args){System.out.println(s);}
int rc=run(args);
removeTmpFiles();
if (rc > 0) {
CommandException ce = new CommandException();
ce.setErrorText(iErrorText);
ce.printStackTrace();
for(String s:ce.getErrorText()){System.out.println(s);}
throw ce;
}
}
//////////////////////////////////////////////////////////////////////////////
/**
Resolve images passed as arguments.
*/
private void resolveImages(LinkedList<String> pArgs,Object... pImages)
throws IOException {
ListIterator<String> argIterator = pArgs.listIterator();
int i = 0;
for (Object obj:pImages) {
// find the next placeholder
while (argIterator.hasNext()) {
if (argIterator.next().equals(Operation.IMG_PLACEHOLDER)) {
break;
}
}
if (obj instanceof String) {
argIterator.set((String) obj);
} else if (obj instanceof BufferedImage) {
if (i<pImages.length) {
// write BufferedImage to temporary file
// and replace the placeholder with the temporary file
String tmpFile = convert2TmpFile((BufferedImage) obj);
argIterator.set(tmpFile);
iTmpFiles.add(tmpFile);
} else {
// special case: BufferedImage is last image, so just create name
iTmpOutputFile=getTmpFile();
argIterator.set(iTmpOutputFile);
}
} else {
throw new IllegalArgumentException(obj.getClass().getName() +
" is an unsupported image-type");
}
i++;
}
}
//////////////////////////////////////////////////////////////////////////////
/**
Resolve DynamicOperations.
@throws IM4JavaException
*/
private void resolveDynamicOperations(Operation pOp, LinkedList<String> pArgs,
Object... pImages) throws IM4JavaException {
ListIterator<String> argIterator = pArgs.listIterator();
ListIterator<DynamicOperation> dynOps =
pOp.getDynamicOperations().listIterator();
// iterate over all DynamicOperations
while (dynOps.hasNext()) {
DynamicOperation dynOp = dynOps.next();
Operation op = dynOp.resolveOperation(pImages);
// find the next placeholder
while (argIterator.hasNext()) {
if (argIterator.next().equals(Operation.DOP_PLACEHOLDER)) {
break;
}
}
if (op == null) {
// no operation
argIterator.remove();
} else {
List<String> args = dynOp.resolveOperation(pImages).getCmdArgs();
if (args == null) {
// empty operation, remove placeholder
argIterator.remove();
} else {
// remove placeholder and add replacement
argIterator.remove();
for (String arg:args) {
argIterator.add(arg);
}
}
}
} // while (dynOps.hasNext())
}
//////////////////////////////////////////////////////////////////////////////
/**
This method just saves the stderr-output into an internal field.
@see org.im4java.process.ErrorConsumer#consumeError(java.io.InputStream)
*/
public void consumeError(InputStream pInputStream) throws IOException {
InputStreamReader esr = new InputStreamReader(pInputStream);
BufferedReader reader = new BufferedReader(esr);
String line;
if (iErrorText == null) {
iErrorText= new ArrayList<String>();
}
while ((line=reader.readLine()) != null) {
iErrorText.add(line);
}
reader.close();
esr.close();
}
//////////////////////////////////////////////////////////////////////////////
/**
* Create a temporary file.
*/
private String getTmpFile() throws IOException {
File tmpFile = File.createTempFile("im4java-",".png");
tmpFile.deleteOnExit();
return tmpFile.getAbsolutePath();
}
//////////////////////////////////////////////////////////////////////////////
/**
* Write a BufferedImage to a temporary file.
*/
private String convert2TmpFile(BufferedImage pBufferedImage)
throws IOException {
String tmpFile = getTmpFile();
ImageIO.write(pBufferedImage,"PNG",new File(tmpFile));
return tmpFile;
}
//////////////////////////////////////////////////////////////////////////////
/**
* Remove all temporary files.
*/
private void removeTmpFiles() {
try {
for (String file:iTmpFiles) {
(new File(file)).delete();
}
} catch (Exception e) {
// ignore, since if we can't delete the file, we can't do anything about it
}
}
}
|