001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/utils/ImageUtils.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstraße 19
030 53177 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042 ---------------------------------------------------------------------------*/
043
044 package org.deegree.ogcwebservices.wpvs.utils;
045
046 import java.awt.Color;
047 import java.awt.Graphics;
048 import java.awt.Image;
049 import java.awt.image.BufferedImage;
050 import java.awt.image.FilteredImageSource;
051 import java.awt.image.ImageFilter;
052 import java.awt.image.ImageProducer;
053 import java.awt.image.RGBImageFilter;
054
055 /**
056 * Little utility class responsible for filtering an image and making it
057 * transparent based on an array of colors considered to be transparent.
058 * Users of this class initalize an object with a non-null <code>Color</code> array
059 * that represents colors, which are supposed to be completely transparent.
060 * By calling <code>#filter( Image )</code>, the colors found in image are substituted
061 * by transparent pixels.
062 *
063 * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
064 * @author last edited by: $Author: apoth $
065 *
066 * $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
067 */
068 public class ImageUtils {
069
070
071 private ImageFilter filter;
072
073 /**
074 * Creates a new <code>ImageUtil</code> object.
075 * @param transparentColors the colors that will be substituted by a completely
076 * transparent color ('0x00FFFFFF'). transparentColors cannot be null.
077 */
078 public ImageUtils( Color[] transparentColors ){
079
080 if ( transparentColors == null ){
081 throw new NullPointerException( "transparentColors cannot be null!" );
082 }
083
084 int[] intColors = new int[ transparentColors.length ];
085 for ( int j = 0; j < intColors.length; j++ ) {
086 intColors[j] = transparentColors[j].getRGB();
087 }
088 filter = new ImageUtils.ColorsToTransparentFilter( intColors );
089
090 }
091
092 /**
093 * Creates a Imagefilter which makes an image transparent.
094 */
095 public ImageUtils( ){
096 filter = new ImageUtils.TransparentImageFilter();
097
098 }
099
100 /**
101 * Filters an image and return a new partially transparent image.
102 * @param image the image that is to be filtered.
103 * @return a new image whose colors are substituted accordign to the
104 * input traparent colors. The input image cannot be null.
105 */
106 public Image filterImage( BufferedImage image ){
107
108 if ( image == null ){
109 throw new NullPointerException( "Image cannot be null!" );
110 }
111 image = ensureRGBAImage( image );
112 ImageProducer imgProducer =
113 new FilteredImageSource( image.getSource(), filter );
114
115 return java.awt.Toolkit.getDefaultToolkit().createImage( imgProducer );
116 }
117
118 /**
119 * Checks if the type of <code>img</code> is <code>BufferedImage.TYPE_INT_ARGB</code>
120 * and if is not, create a new one, just like <code>img</code> but with transparency
121 * @param image the image to be checked. Cannot be null.
122 * @return the same image, if its type is <code>BufferedImage.TYPE_INT_ARGB</code>, or a
123 * new transparent one.
124 */
125 public BufferedImage ensureRGBAImage( BufferedImage image ) {
126
127 if ( image == null ){
128 throw new NullPointerException( "Image cannot be null!" );
129 }
130
131 if ( image.getType() != BufferedImage.TYPE_INT_ARGB ) {
132 BufferedImage tmp = new BufferedImage( image.getWidth(), image.getHeight(),
133 BufferedImage.TYPE_INT_ARGB );
134 Graphics g = tmp.getGraphics();
135 g.drawImage( image, 0, 0, null );
136 g.dispose();
137 image = tmp;
138 }
139 return image;
140 }
141
142 /**
143 * An <code>RGBImageFilter</code> to substitute all input colors by a completely
144 * transparent one.
145 *
146 * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
147 * @author last edited by: $Author: apoth $
148 *
149 * $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
150 */
151 public class ColorsToTransparentFilter extends RGBImageFilter {
152
153 private static final int TRANSPARENT_COLOR = 0x00FFFFFF;
154
155 private final int[] colors;
156
157 float alphaPercent = 0.975f;
158
159 /**
160 * @param colors the Colors which should be transparent
161 */
162 public ColorsToTransparentFilter( int[] colors ) {
163 if ( colors == null || colors.length == 0){
164 throw new NullPointerException( "colors cannot be null!" );
165 }
166 this.colors = colors;
167 canFilterIndexColorModel = true;
168 }
169
170 /**
171 * @see java.awt.image.RGBImageFilter
172 */
173 @Override
174 public int filterRGB(int x, int y, int argb) {
175 if( shouldBeTransparent( argb ) ) {
176 return TRANSPARENT_COLOR; // mask alpha bits to zero
177 // argb = TRANSPARENT_COLOR;
178 }
179 return argb;
180 /*int a = ( argb >> 24) & 0xff;
181 a *= alphaPercent;
182 return ( ( argb & 0x00ffffff) | (a << 24));*/
183 }
184
185 /**
186 * Compares <code>color</code> with TRANSPARENT_COLOR
187 * @param color color to be compared to TRANSPARENT_COLOR
188 * @return true if color = TRANSPARENT_COLOR
189 */
190 private boolean shouldBeTransparent( int color ) {
191 for ( int i = 0; i < colors.length; i++ ) {
192 if ( colors[i] == color ) {
193 return true;
194 }
195 }
196 return false;
197 }
198 }
199
200 /* from Java AWT reference, chap. 12*/
201 /**
202 * The <code>TransparentImageFilter</code> class filters an RGB-Pixel with a transparency.
203 *
204 * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
205 *
206 * @author last edited by: $Author: apoth $
207 *
208 * @version $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
209 *
210 */
211 class TransparentImageFilter extends RGBImageFilter {
212 float alphaPercent;
213 /**
214 * A TransparentImageFilter with no transparency
215 */
216 public TransparentImageFilter () {
217 this (1f);
218 }
219 /**
220 * @param aPercent of the transparency
221 */
222 public TransparentImageFilter (float aPercent) {
223 if ((aPercent < 0.0) || (aPercent > 1.0))
224 aPercent = 1;
225 alphaPercent = aPercent;
226 canFilterIndexColorModel = true;
227 }
228 @Override
229 public int filterRGB (int x, int y, int rgb) {
230 int a = (rgb >> 24) & 0xff;
231 a *= alphaPercent;
232
233 return ((rgb & 0x00ffffff) | (a << 24));
234 }
235 }
236 }
237