Merge from mainline.
[official-gcc.git] / libjava / classpath / java / awt / image / LookupOp.java
blob46e72fe6183e72f37b334d7cfe52f000c160629e
1 /* LookupOp.java -- Filter that converts each pixel using a lookup table.
2 Copyright (C) 2004 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.image;
41 import java.awt.Graphics2D;
42 import java.awt.RenderingHints;
43 import java.awt.geom.Point2D;
44 import java.awt.geom.Rectangle2D;
46 /**
47 * LookupOp is a filter that converts each pixel using a lookup table.
49 * For filtering Rasters, the lookup table must have either one component
50 * that is applied to all bands, or one component for every band in the
51 * Rasters.
53 * For BufferedImages, the lookup table may apply to both color and alpha
54 * components. If the lookup table contains one component, or if there are
55 * the same number of components as color components in the source, the table
56 * applies to all color components. Otherwise the table applies to all
57 * components including alpha. Alpha premultiplication is ignored during the
58 * lookup filtering.
60 * After filtering, if color conversion is necessary, the conversion happens,
61 * taking alpha premultiplication into account.
63 * @author jlquinn
65 public class LookupOp implements BufferedImageOp, RasterOp
67 private LookupTable lut;
68 private RenderingHints hints;
70 /** Construct a new LookupOp.
72 * @param lookup LookupTable to use.
73 * @param hints Rendering hints (can be null).
75 public LookupOp(LookupTable lookup, RenderingHints hints)
77 lut = lookup;
78 this.hints = hints;
81 /* (non-Javadoc)
82 * @see java.awt.image.BufferedImageOp#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage)
84 public final BufferedImage filter(BufferedImage src, BufferedImage dst)
86 if (src.getColorModel() instanceof IndexColorModel)
87 throw new IllegalArgumentException("LookupOp.filter: IndexColorModel "
88 + "not allowed");
89 if (dst == null)
90 dst = createCompatibleDestImage(src, src.getColorModel());
92 // Set up for potential colormodel mismatch
93 BufferedImage tgt;
94 if (dst.getColorModel().equals(src.getColorModel()))
95 tgt = dst;
96 else
97 tgt = createCompatibleDestImage(src, src.getColorModel());
99 Raster sr = src.getRaster();
100 WritableRaster dr = tgt.getRaster();
102 if (src.getColorModel().hasAlpha() &&
103 (lut.getNumComponents() == 1 ||
104 lut.getNumComponents() == src.getColorModel().getNumColorComponents()))
106 // Need to ignore alpha for lookup
107 int[] dbuf = new int[src.getColorModel().getNumComponents()];
108 int tmpBands = src.getColorModel().getNumColorComponents();
109 int[] tmp = new int[tmpBands];
111 // Filter the pixels
112 for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
113 for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
115 // Filter only color components, but also copy alpha
116 sr.getPixel(x, y, dbuf);
117 System.arraycopy(dbuf, 0, tmp, 0, tmpBands);
118 dr.setPixel(x, y, lut.lookupPixel(tmp, dbuf));
121 else if (lut.getNumComponents() != 1
123 lut.getNumComponents() != src.getColorModel().getNumComponents())
124 throw new IllegalArgumentException("LookupOp.filter: "
125 + "Incompatible lookup "
126 + "table and source image");
128 // No alpha to ignore
129 int[] dbuf = new int[src.getColorModel().getNumComponents()];
131 // Filter the pixels
132 for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
133 for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
134 dr.setPixel(x, y, lut.lookupPixel(sr.getPixel(x, y, dbuf), dbuf));
136 if (tgt != dst)
138 // Convert between color models.
139 // TODO Check that premultiplied alpha is handled correctly here.
140 Graphics2D gg = dst.createGraphics();
141 gg.setRenderingHints(hints);
142 gg.drawImage(tgt, 0, 0, null);
143 gg.dispose();
146 return dst;
149 /* (non-Javadoc)
150 * @see java.awt.image.BufferedImageOp#getBounds2D(java.awt.image.BufferedImage)
152 public final Rectangle2D getBounds2D(BufferedImage src)
154 return src.getRaster().getBounds();
157 /* (non-Javadoc)
158 * @see java.awt.image.BufferedImageOp#createCompatibleDestImage(java.awt.image.BufferedImage, java.awt.image.ColorModel)
160 public BufferedImage createCompatibleDestImage(BufferedImage src,
161 ColorModel dstCM)
163 // FIXME: set properties to those in src
164 return new BufferedImage(dstCM,
165 src.getRaster().createCompatibleWritableRaster(),
166 src.isPremultiplied, null);
169 /** Return corresponding destination point for source point.
171 * LookupOp will return the value of src unchanged.
172 * @param src The source point.
173 * @param dst The destination point.
174 * @see java.awt.image.RasterOp#getPoint2D(java.awt.geom.Point2D, java.awt.geom.Point2D)
176 public final Point2D getPoint2D(Point2D src, Point2D dst)
178 if (dst == null)
179 return (Point2D) src.clone();
181 dst.setLocation(src);
182 return dst;
185 /** Return the LookupTable for this op. */
186 public final LookupTable getTable()
188 return lut;
191 /* (non-Javadoc)
192 * @see java.awt.image.RasterOp#getRenderingHints()
194 public final RenderingHints getRenderingHints()
196 return hints;
199 /** Filter a raster through a lookup table.
201 * Applies the lookup table for this Rasterop to each pixel of src and
202 * puts the results in dest. If dest is null, a new Raster is created and
203 * returned.
205 * @param src The source raster.
206 * @param dest The destination raster.
207 * @return The WritableRaster with the filtered pixels.
208 * @throws IllegalArgumentException if lookup table has more than one
209 * component but not the same as src and dest.
210 * @see java.awt.image.RasterOp#filter(java.awt.image.Raster, java.awt.image.WritableRaster)
212 public final WritableRaster filter(Raster src, WritableRaster dest)
214 if (dest == null)
215 // Allocate a raster if needed
216 dest = createCompatibleDestRaster(src);
217 else
218 if (src.getNumBands() != dest.getNumBands())
219 throw new IllegalArgumentException();
221 if (lut.getNumComponents() != 1
222 && lut.getNumComponents() != src.getNumBands())
223 throw new IllegalArgumentException();
226 // Allocate pixel storage.
227 int[] tmp = new int[src.getNumBands()];
229 // Filter the pixels
230 for (int y = src.getMinY(); y < src.getHeight() + src.getMinY(); y++)
231 for (int x = src.getMinX(); x < src.getWidth() + src.getMinX(); x++)
232 dest.setPixel(x, y, lut.lookupPixel(src.getPixel(x, y, tmp), tmp));
233 return dest;
236 /* (non-Javadoc)
237 * @see java.awt.image.RasterOp#getBounds2D(java.awt.image.Raster)
239 public final Rectangle2D getBounds2D(Raster src)
241 return src.getBounds();
244 /* (non-Javadoc)
245 * @see java.awt.image.RasterOp#createCompatibleDestRaster(java.awt.image.Raster)
247 public WritableRaster createCompatibleDestRaster(Raster src)
249 return src.createCompatibleWritableRaster();