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
/* * @(#)FilteredImageSource.java 1.29 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.awt.image; import java.awt.Image; import java.awt.image.ImageFilter; import java.awt.image.ImageConsumer; import java.awt.image.ImageProducer; import java.util.Hashtable; import java.awt.image.ColorModel; /** * This class is an implementation of the ImageProducer interface which * takes an existing image and a filter object and uses them to produce * image data for a new filtered version of the original image. * Here is an example which filters an image by swapping the red and * blue compents: * <pre> * * Image src = getImage("doc:///demo/images/duke/T1.gif"); * ImageFilter colorfilter = new RedBlueSwapFilter(); * Image img = createImage(new FilteredImageSource(src.getSource(), * colorfilter)); * * </pre> * * @see ImageProducer * * @version 1.29 11/17/05 * @author Jim Graham */ public class FilteredImageSource implements ImageProducer { ImageProducer src; ImageFilter filter; /** * Constructs an ImageProducer object from an existing ImageProducer * and a filter object. * @param orig the specified <code>ImageProducer</code> * @param imgf the specified <code>ImageFilter</code> * @see ImageFilter * @see java.awt.Component#createImage */ public FilteredImageSource(ImageProducer orig, ImageFilter imgf) { src = orig; filter = imgf; } private Hashtable proxies; /** * Adds the specified <code>ImageConsumer</code> * to the list of consumers interested in data for the filtered image. * An instance of the original <code>ImageFilter</code> * is created * (using the filter's <code>getFilterInstance</code> method) * to manipulate the image data * for the specified <code>ImageConsumer</code>. * The newly created filter instance * is then passed to the <code>addConsumer</code> method * of the original <code>ImageProducer</code>. * * <p> * This method is public as a side effect * of this class implementing * the <code>ImageProducer</code> interface. * It should not be called from user code, * and its behavior if called from user code is unspecified. * * @param ic the consumer for the filtered image * @see ImageConsumer */ public synchronized void addConsumer(ImageConsumer ic) { if (proxies == null) { proxies = new Hashtable(); } if (!proxies.containsKey(ic)) { ImageFilter imgf = filter.getFilterInstance(ic); proxies.put(ic, imgf); src.addConsumer(imgf); } } /** * Determines whether an ImageConsumer is on the list of consumers * currently interested in data for this image. * * <p> * This method is public as a side effect * of this class implementing * the <code>ImageProducer</code> interface. * It should not be called from user code, * and its behavior if called from user code is unspecified. * * @param ic the specified <code>ImageConsumer</code> * @return true if the ImageConsumer is on the list; false otherwise * @see ImageConsumer */ public synchronized boolean isConsumer(ImageConsumer ic) { return (proxies != null && proxies.containsKey(ic)); } /** * Removes an ImageConsumer from the list of consumers interested in * data for this image. * * <p> * This method is public as a side effect * of this class implementing * the <code>ImageProducer</code> interface. * It should not be called from user code, * and its behavior if called from user code is unspecified. * * @see ImageConsumer */ public synchronized void removeConsumer(ImageConsumer ic) { if (proxies != null) { ImageFilter imgf = (ImageFilter) proxies.get(ic); if (imgf != null) { src.removeConsumer(imgf); proxies.remove(ic); if (proxies.isEmpty()) { proxies = null; } } } } /** * Starts production of the filtered image. * If the specified <code>ImageConsumer</code> * isn't already a consumer of the filtered image, * an instance of the original <code>ImageFilter</code> * is created * (using the filter's <code>getFilterInstance</code> method) * to manipulate the image data * for the <code>ImageConsumer</code>. * The filter instance for the <code>ImageConsumer</code> * is then passed to the <code>startProduction</code> method * of the original <code>ImageProducer</code>. * * <p> * This method is public as a side effect * of this class implementing * the <code>ImageProducer</code> interface. * It should not be called from user code, * and its behavior if called from user code is unspecified. * * @param ic the consumer for the filtered image * @see ImageConsumer */ public void startProduction(ImageConsumer ic) { if (proxies == null) { proxies = new Hashtable(); } ImageFilter imgf = (ImageFilter) proxies.get(ic); if (imgf == null) { imgf = filter.getFilterInstance(ic); proxies.put(ic, imgf); } src.startProduction(imgf); } /** * Requests that a given ImageConsumer have the image data delivered * one more time in top-down, left-right order. The request is * handed to the ImageFilter for further processing, since the * ability to preserve the pixel ordering depends on the filter. * * <p> * This method is public as a side effect * of this class implementing * the <code>ImageProducer</code> interface. * It should not be called from user code, * and its behavior if called from user code is unspecified. * * @see ImageConsumer */ public void requestTopDownLeftRightResend(ImageConsumer ic) { if (proxies != null) { ImageFilter imgf = (ImageFilter) proxies.get(ic); if (imgf != null) { imgf.resendTopDownLeftRight(src); } } } }