1 package org.jmage.filter;
2
3 import org.jmage.ApplicationContext;
4 import org.jmage.ImageRequest;
5 import org.jmage.JmageException;
6 import org.jmage.dispatcher.InternalRequestDispatcher;
7
8 import javax.media.jai.JAI;
9 import javax.media.jai.PlanarImage;
10 import java.awt.*;
11 import java.net.URI;
12 import java.util.Properties;
13
14 /***
15 * TwinResourceImageFilter introduces a second image into the filter, used as an additional resource for filter
16 * operations.
17 */
18 public abstract class TwinResourceImageFilter extends ConfigurableImageFilter {
19 public static final String IMAGE_URI = "IMAGE_URI";
20 public static final String CHAIN_URI = "CHAIN_URI";
21
22 protected PlanarImage resourceImage;
23
24 private static final String DISPATCH_ERROR = "error while internally dispatching request, cause: ";
25 private static final String CHAIN = "chain:";
26
27 public void initialize(Properties filterProperties) throws FilterException {
28 this.filterProperties = filterProperties;
29 }
30
31 public PlanarImage filter(PlanarImage image) throws FilterException {
32
33
34 this.filter(image, new Properties());
35
36
37 return image;
38 }
39
40 public PlanarImage filter(PlanarImage image, Properties additionalProperties) throws FilterException {
41
42 filterProperties.putAll(additionalProperties);
43 ImageRequest internalRequest = this.mapInternalRequest(image);
44
45
46 try {
47 InternalRequestDispatcher internalRequestDispatcher = new InternalRequestDispatcher();
48 internalRequestDispatcher.configure(ApplicationContext.getContext());
49 internalRequestDispatcher.dispatch(internalRequest);
50 } catch (JmageException e) {
51 throw new FilterException(DISPATCH_ERROR + e.getMessage());
52 }
53
54
55 this.resourceImage = internalRequest.getImage();
56
57
58 return image;
59 }
60
61 protected ImageRequest mapInternalRequest(PlanarImage image) {
62 ImageRequest internalRequest = new ImageRequest();
63
64
65 internalRequest.setImage(this.cloneImage(image));
66
67
68 String imageUri = filterProperties.getProperty(IMAGE_URI);
69 if (imageUri != null) {
70 if (imageUri.startsWith(CHAIN)) {
71 internalRequest.setFilterChainURI(new URI[] {URI.create(imageUri)});
72 } else {
73
74 internalRequest.setImageURI(URI.create(imageUri));
75 }
76 }
77
78
79 internalRequest.setFilterChainProperties(new Properties());
80 return internalRequest;
81 }
82
83 protected PlanarImage cloneImage(PlanarImage image) {
84 return (PlanarImage) JAI.create("AWTImage", (Image) image.getAsBufferedImage());
85 }
86 }