View Javadoc

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          //filter through internal Request dispatcher
34          this.filter(image, new Properties());
35  
36          //return the original image if someone wants to use the results of super()
37          return image;
38      }
39  
40      public PlanarImage filter(PlanarImage image, Properties additionalProperties) throws FilterException {
41          //check what configure() was passed into.
42          filterProperties.putAll(additionalProperties);
43          ImageRequest internalRequest = this.mapInternalRequest(image);
44  
45          //dispatch for internal processing
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          //set the resource image for the subclass to use.
55          this.resourceImage = internalRequest.getImage();
56  
57          //return the original image if someone wants to use the results of super()
58          return image;
59      }
60  
61      protected ImageRequest mapInternalRequest(PlanarImage image) {
62          ImageRequest internalRequest = new ImageRequest();
63  
64          //clone the actual image and put into internal request.
65          internalRequest.setImage(this.cloneImage(image));
66  
67          //TODO ugly: sets chain uri by using the inherited image URI which is really a chain URI
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                  //this may overwrite the inherited image
74                  internalRequest.setImageURI(URI.create(imageUri));
75              }
76          }
77  
78          // don't pass anything on.
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  }