1
/* Copyright (C) 2004, 2006 Free Software Foundation
3
This file is part of GNU Classpath.
5
GNU Classpath is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2, or (at your option)
10
GNU Classpath is distributed in the hope that it will be useful, but
11
WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with GNU Classpath; see the file COPYING. If not, write to the
17
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
Linking this library statically or dynamically with other modules is
21
making a combined work based on this library. Thus, the terms and
22
conditions of the GNU General Public License cover the whole
25
As a special exception, the copyright holders of this library give you
26
permission to link this library with independent modules to produce an
27
executable, regardless of the license terms of these independent
28
modules, and to copy and distribute the resulting executable under
29
terms of your choice, provided that you also meet, for each linked
30
independent module, the terms and conditions of the license of that
31
module. An independent module is a module which is not derived from
32
or based on this library. If you modify this library, you may extend
33
this exception to your version of the library, but you are not
34
obligated to do so. If you do not wish to do so, delete this
35
exception statement from your version. */
38
package java.awt.image;
40
import java.awt.RenderingHints;
41
import java.awt.geom.Point2D;
42
import java.awt.geom.Rectangle2D;
43
import java.util.Arrays;
46
* RescaleOp is a filter that changes each pixel by a scaling factor and offset.
48
* For filtering Rasters, either one scaling factor and offset can be specified,
49
* which will be applied to all bands; or a scaling factor and offset can be
50
* specified for each band.
52
* For BufferedImages, the scaling may apply to both color and alpha components.
53
* If only one scaling factor is provided, or if the number of factors provided
54
* equals the number of color components, the scaling is performed on all color
55
* components. Otherwise, the scaling is performed on all components including
56
* alpha. Alpha premultiplication is ignored.
58
* After filtering, if color conversion is necessary, the conversion happens,
59
* taking alpha premultiplication into account.
61
* @author Jerry Quinn (jlquinn@optonline.net)
62
* @author Francis Kung (fkung@redhat.com)
64
public class RescaleOp implements BufferedImageOp, RasterOp
66
private float[] scale;
67
private float[] offsets;
68
private RenderingHints hints = null;
71
* Create a new RescaleOp object using the given scale factors and offsets.
73
* The length of the arrays must be equal to the number of bands (or number of
74
* data or color components) of the raster/image that this Op will be used on,
75
* otherwise an IllegalArgumentException will be thrown when calling the
78
* @param scaleFactors an array of scale factors.
79
* @param offsets an array of offsets.
80
* @param hints any rendering hints to use (can be null).
81
* @throws NullPointerException if the scaleFactors or offsets array is null.
83
public RescaleOp(float[] scaleFactors,
87
int length = Math.min(scaleFactors.length, offsets.length);
89
scale = new float[length];
90
System.arraycopy(scaleFactors, 0, this.scale, 0, length);
92
this.offsets = new float[length];
93
System.arraycopy(offsets, 0, this.offsets, 0, length);
99
* Create a new RescaleOp object using the given scale factor and offset.
101
* The same scale factor and offset will be used on all bands/components.
103
* @param scaleFactor the scale factor to use.
104
* @param offset the offset to use.
105
* @param hints any rendering hints to use (can be null).
107
public RescaleOp(float scaleFactor,
109
RenderingHints hints)
111
scale = new float[]{ scaleFactor };
112
offsets = new float[]{offset};
117
* Returns the scaling factors. This method accepts an optional array, which
118
* will be used to store the factors if not null (this avoids allocating a
119
* new array). If this array is too small to hold all the scaling factors,
120
* the array will be filled and the remaining factors discarded.
122
* @param scaleFactors array to store the scaling factors in (can be null).
123
* @return an array of scaling factors.
125
public final float[] getScaleFactors(float[] scaleFactors)
127
if (scaleFactors == null)
128
scaleFactors = new float[scale.length];
129
System.arraycopy(scale, 0, scaleFactors, 0, Math.min(scale.length,
130
scaleFactors.length));
135
* Returns the offsets. This method accepts an optional array, which
136
* will be used to store the offsets if not null (this avoids allocating a
137
* new array). If this array is too small to hold all the offsets, the array
138
* will be filled and the remaining factors discarded.
140
* @param offsets array to store the offsets in (can be null).
141
* @return an array of offsets.
143
public final float[] getOffsets(float[] offsets)
146
offsets = new float[this.offsets.length];
147
System.arraycopy(this.offsets, 0, offsets, 0, Math.min(this.offsets.length,
153
* Returns the number of scaling factors / offsets.
155
* @return the number of scaling factors / offsets.
157
public final int getNumFactors()
163
* @see java.awt.image.BufferedImageOp#getRenderingHints()
165
public final RenderingHints getRenderingHints()
171
* Converts the source image using the scale factors and offsets specified in
172
* the constructor. The resulting image is stored in the destination image if
173
* one is provided; otherwise a new BufferedImage is created and returned.
175
* The source image cannot use an IndexColorModel, and the destination image
176
* (if one is provided) must have the same size.
178
* If the final value of a sample is beyond the range of the color model, it
179
* will be clipped to the appropriate maximum / minimum.
181
* @param src The source image.
182
* @param dst The destination image.
183
* @throws IllegalArgumentException if the rasters and/or color spaces are
185
* @return The rescaled image.
187
public final BufferedImage filter(BufferedImage src, BufferedImage dst)
190
if (scale.length != 1
191
&& scale.length != src.getColorModel().getNumComponents()
192
&& (scale.length != src.getColorModel().getNumColorComponents()))
193
throw new IllegalArgumentException("Source image has wrong number of "
194
+ "bands for these scaling factors.");
197
dst = createCompatibleDestImage(src, null);
198
else if (src.getHeight() != dst.getHeight()
199
|| src.getWidth() != dst.getWidth())
200
throw new IllegalArgumentException("Source and destination images are "
201
+ "different sizes.");
203
// Prepare for possible colorspace conversion
204
BufferedImage dst2 = dst;
205
if (dst.getColorModel().getColorSpace().getType() != src.getColorModel().getColorSpace().getType())
206
dst2 = createCompatibleDestImage(src, src.getColorModel());
208
// Figure out how many bands to scale
209
int numBands = scale.length;
210
if (scale.length == 1)
211
numBands = src.getColorModel().getNumColorComponents();
212
boolean[] bands = new boolean[numBands];
213
// this assumes the alpha, if present, is the last band
214
Arrays.fill(bands, true);
217
filter(src.getRaster(), dst2.getRaster(), bands);
219
// Copy alpha band if needed (ie if it exists and wasn't scaled)
220
// NOTE: This assumes the alpha component is the last band!
221
if (src.getColorModel().hasAlpha()
222
&& numBands == src.getColorModel().getNumColorComponents())
225
dst2.getRaster().setSamples(0, 0, src.getWidth(), src.getHeight(),
227
src.getRaster().getSamples(0, 0,
234
// Perform colorspace conversion if needed
236
new ColorConvertOp(hints).filter(dst2, dst);
242
* @see java.awt.image.RasterOp#filter(java.awt.image.Raster, java.awt.image.WritableRaster)
244
public final WritableRaster filter(Raster src, WritableRaster dest)
246
// Required sanity checks
247
if (scale.length != 1 && scale.length != src.numBands)
248
throw new IllegalArgumentException("Number of rasters is incompatible "
249
+ "with the number of scaling "
250
+ "factors provided.");
253
dest = src.createCompatibleWritableRaster();
254
else if (src.getHeight() != dest.getHeight()
255
|| src.getWidth() != dest.getWidth())
256
throw new IllegalArgumentException("Source and destination rasters are "
257
+ "different sizes.");
258
else if (src.numBands != dest.numBands)
259
throw new IllegalArgumentException("Source and destination rasters "
260
+ "are incompatible.");
263
boolean[] bands = new boolean[src.getNumBands()];
264
Arrays.fill(bands, true);
265
return filter(src, dest, bands);
269
* Perform raster-based filtering on a selected number of bands.
271
* The length of the bands array should equal the number of bands; a true
272
* element indicates filtering should happen on the corresponding band, while
273
* a false element will skip the band.
275
* The rasters are assumed to be compatible and non-null.
277
* @param src the source raster.
278
* @param dest the destination raster.
279
* @param bands an array indicating which bands to filter.
280
* @throws NullPointerException if any parameter is null.
281
* @throws ArrayIndexOutOfBoundsException if the bands array is too small.
282
* @return the destination raster.
284
private WritableRaster filter(Raster src, WritableRaster dest, boolean[] bands)
286
int[] values = new int[src.getHeight() * src.getWidth()];
287
float scaleFactor, offset;
289
// Find max sample value, to be used for clipping later
290
int[] maxValue = src.getSampleModel().getSampleSize();
291
for (int i = 0; i < maxValue.length; i++)
292
maxValue[i] = (int)Math.pow(2, maxValue[i]) - 1;
294
// TODO: can this be optimized further?
295
// Filter all samples of all requested bands
296
for (int band = 0; band < bands.length; band++)
299
values = src.getSamples(src.getMinX(), src.getMinY(), src.getWidth(),
300
src.getHeight(), band, values);
302
if (scale.length == 1)
304
scaleFactor = scale[0];
309
scaleFactor = scale[band];
310
offset = offsets[band];
313
for (int i = 0; i < values.length; i++)
315
values[i] = (int) (values[i] * scaleFactor + offset);
320
if (values[i] > maxValue[band])
321
values[i] = maxValue[band];
324
dest.setSamples(dest.getMinX(), dest.getMinY(), dest.getWidth(),
325
dest.getHeight(), band, values);
334
* @see java.awt.image.BufferedImageOp#createCompatibleDestImage(java.awt.image.BufferedImage,
335
* java.awt.image.ColorModel)
337
public BufferedImage createCompatibleDestImage(BufferedImage src,
341
return new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
343
return new BufferedImage(dstCM,
344
src.getRaster().createCompatibleWritableRaster(),
345
src.isAlphaPremultiplied(), null);
349
* @see java.awt.image.RasterOp#createCompatibleDestRaster(java.awt.image.Raster)
351
public WritableRaster createCompatibleDestRaster(Raster src)
353
return src.createCompatibleWritableRaster();
357
* @see java.awt.image.BufferedImageOp#getBounds2D(java.awt.image.BufferedImage)
359
public final Rectangle2D getBounds2D(BufferedImage src)
361
return src.getRaster().getBounds();
365
* @see java.awt.image.RasterOp#getBounds2D(java.awt.image.Raster)
367
public final Rectangle2D getBounds2D(Raster src)
369
return src.getBounds();
373
* @see java.awt.image.BufferedImageOp#getPoint2D(java.awt.geom.Point2D, java.awt.geom.Point2D)
375
public final Point2D getPoint2D(Point2D src, Point2D dst)
378
dst = (Point2D) src.clone();
380
dst.setLocation(src);