svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.expressionevaluator / org.gvsig.expressionevaluator.lib / org.gvsig.expressionevaluator.lib.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / image / ImageWriteFunction.java @ 44924
History | View | Annotate | Download (1.9 KB)
1 |
package org.gvsig.expressionevaluator.impl.function.image; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.io.OutputStream; |
6 |
import java.util.Objects; |
7 |
import java.util.logging.Level; |
8 |
import java.util.logging.Logger; |
9 |
import org.apache.commons.lang3.Range; |
10 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
11 |
import org.gvsig.expressionevaluator.Interpreter; |
12 |
import org.gvsig.expressionevaluator.impl.function.programming.FileFunction; |
13 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
14 |
|
15 |
public class ImageWriteFunction extends AbstractFunction { |
16 |
|
17 |
public ImageWriteFunction() {
|
18 |
super(
|
19 |
"Image", "IMAGEWRITE", Range.is(2), |
20 |
"Read an image from a data source and return it as a 'Java BufferedImage' object.",
|
21 |
"IMAGEWRITE({{simpleimage}}, target, format)",
|
22 |
new String[]{ |
23 |
"Image to write",
|
24 |
"Value to save the image in",
|
25 |
"Format name, PNG, JPEG,...",
|
26 |
}, |
27 |
"SimpleImage",
|
28 |
false
|
29 |
); |
30 |
} |
31 |
|
32 |
@Override
|
33 |
public boolean allowConstantFolding() { |
34 |
return true; |
35 |
} |
36 |
|
37 |
@Override
|
38 |
public Object call(Interpreter interpreter, Object[] args) { |
39 |
String formatName = this.getStr(args, 2); |
40 |
Object arg0 = this.getObject(args, 0); |
41 |
SimpleImage image = ImageReadFunction.createSimpleImage(arg0); |
42 |
Object target = this.getObject(args, 1); |
43 |
try {
|
44 |
if( target instanceof OutputStream ) { |
45 |
image.save((OutputStream) target, formatName);
|
46 |
} else {
|
47 |
File f = FileFunction.toFile(target);
|
48 |
image.save(f, formatName); |
49 |
} |
50 |
} catch (IOException ex) { |
51 |
throw new ExpressionRuntimeException("Can't write image to '"+Objects.toString(target)+" as format '"+formatName+".", ex); |
52 |
} |
53 |
return image;
|
54 |
} |
55 |
} |