summaryrefslogtreecommitdiff
path: root/java/awt/image/ConvolveOp.java
blob: cf30e762599b7e42b6b0ef08bdc9cab874bc1df2 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* ConvolveOp.java --
   Copyright (C) 2004, 2005, 2006, Free Software Foundation -- ConvolveOp

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.awt.image;

import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

/**
 * Convolution filter.
 * 
 * ConvolveOp convolves the source image with a Kernel to generate a
 * destination image.  This involves multiplying each pixel and its neighbors
 * with elements in the kernel to compute a new pixel.
 * 
 * Each band in a Raster is convolved and copied to the destination Raster.
 * For BufferedImages, convolution is applied to all components.  Color 
 * conversion will be applied if needed.
 * 
 * Note that this filter ignores whether the source or destination is alpha
 * premultiplied.  The reference spec states that data will be premultiplied
 * prior to convolving and divided back out afterwards (if needed), but testing
 * has shown that this is not the case with their implementation.
 * 
 * @author jlquinn@optonline.net
 */
public class ConvolveOp implements BufferedImageOp, RasterOp
{
  /** Edge pixels are set to 0. */
  public static final int EDGE_ZERO_FILL = 0;
  
  /** Edge pixels are copied from the source. */
  public static final int EDGE_NO_OP = 1;
  
  private Kernel kernel;
  private int edge;
  private RenderingHints hints;

  /**
   * Construct a ConvolveOp.
   * 
   * The edge condition specifies that pixels outside the area that can be
   * filtered are either set to 0 or copied from the source image.
   * 
   * @param kernel The kernel to convolve with.
   * @param edgeCondition Either EDGE_ZERO_FILL or EDGE_NO_OP.
   * @param hints Rendering hints for color conversion, or null.
   */
  public ConvolveOp(Kernel kernel,
      				int edgeCondition,
      				RenderingHints hints)
  {
    this.kernel = kernel;
    edge = edgeCondition;
    this.hints = hints;
  }
  
  /**
   * Construct a ConvolveOp.
   * 
   * The edge condition defaults to EDGE_ZERO_FILL.
   * 
   * @param kernel The kernel to convolve with.
   */
  public ConvolveOp(Kernel kernel)
  {
    this.kernel = kernel;
    edge = EDGE_ZERO_FILL;
    hints = null;
  }

  /**
   * Converts the source image using the kernel specified in the
   * constructor.  The resulting image is stored in the destination image if one
   * is provided; otherwise a new BufferedImage is created and returned. 
   * 
   * The source and destination BufferedImage (if one is supplied) must have
   * the same dimensions.
   *
   * @param src The source image.
   * @param dst The destination image.
   * @throws IllegalArgumentException if the rasters and/or color spaces are
   *            incompatible.
   * @return The convolved image.
   */
  public final BufferedImage filter(BufferedImage src, BufferedImage dst)
  {
    if (src == dst)
      throw new IllegalArgumentException("Source and destination images " +
            "cannot be the same.");
    
    if (dst == null)
      dst = createCompatibleDestImage(src, src.getColorModel());
    
    // Make sure source image is premultiplied
    BufferedImage src1 = src;
    // The spec says we should do this, but mauve testing shows that Sun's
    // implementation does not check this.
    /*
    if (!src.isAlphaPremultiplied())
    {
      src1 = createCompatibleDestImage(src, src.getColorModel());
      src.copyData(src1.getRaster());
      src1.coerceData(true);
    }
    */

    BufferedImage dst1 = dst;
    if (src1.getColorModel().getColorSpace().getType() != dst.getColorModel().getColorSpace().getType())
      dst1 = createCompatibleDestImage(src, src.getColorModel());

    filter(src1.getRaster(), dst1.getRaster());
    
    // Since we don't coerceData above, we don't need to divide it back out.
    // This is wrong (one mauve test specifically tests converting a non-
    // premultiplied image to a premultiplied image, and it shows that Sun
    // simply ignores the premultipled flag, contrary to the spec), but we
    // mimic it for compatibility.
    /*
	if (! dst.isAlphaPremultiplied())
	  dst1.coerceData(false);
    */

    // Convert between color models if needed
    if (dst1 != dst)
      new ColorConvertOp(hints).filter(dst1, dst);

    return dst;
  }

  /**
   * Creates an empty BufferedImage with the size equal to the source and the
   * correct number of bands. The new image is created with the specified 
   * ColorModel, or if no ColorModel is supplied, an appropriate one is chosen.
   *
   * @param src The source image.
   * @param dstCM A color model for the destination image (may be null).
   * @return The new compatible destination image.
   */
  public BufferedImage createCompatibleDestImage(BufferedImage src,
                                                 ColorModel dstCM)
  {
    if (dstCM != null)
      return new BufferedImage(dstCM,
                               src.getRaster().createCompatibleWritableRaster(),
                               src.isAlphaPremultiplied(), null);

    return new BufferedImage(src.getWidth(), src.getHeight(), src.getType());
  }

  /* (non-Javadoc)
   * @see java.awt.image.RasterOp#getRenderingHints()
   */
  public final RenderingHints getRenderingHints()
  {
    return hints;
  }
  
  /**
   * Get the edge condition for this Op.
   * 
   * @return The edge condition.
   */
  public int getEdgeCondition()
  {
    return edge;
  }
  
  /**
   * Returns (a clone of) the convolution kernel.
   *
   * @return The convolution kernel.
   */
  public final Kernel getKernel()
  {
    return (Kernel) kernel.clone();
  }

  /**
   * Converts the source raster using the kernel specified in the constructor.  
   * The resulting raster is stored in the destination raster if one is 
   * provided; otherwise a new WritableRaster is created and returned.
   * 
   * If the convolved value for a sample is outside the range of [0-255], it
   * will be clipped.
   * 
   * The source and destination raster (if one is supplied) cannot be the same,
   * and must also have the same dimensions.
   *
   * @param src The source raster.
   * @param dest The destination raster.
   * @throws IllegalArgumentException if the rasters identical.
   * @throws ImagingOpException if the convolution is not possible.
   * @return The transformed raster.
   */
  public final WritableRaster filter(Raster src, WritableRaster dest)
  {
    if (src == dest)
      throw new IllegalArgumentException("src == dest is not allowed.");
    if (kernel.getWidth() > src.getWidth() 
        || kernel.getHeight() > src.getHeight())
      throw new ImagingOpException("The kernel is too large.");
    if (dest == null)
      dest = createCompatibleDestRaster(src);
    else if (src.getNumBands() != dest.getNumBands())
      throw new ImagingOpException("src and dest have different band counts.");

    // calculate the borders that the op can't reach...
    int kWidth = kernel.getWidth();
    int kHeight = kernel.getHeight();
    int left = kernel.getXOrigin();
    int right = Math.max(kWidth - left - 1, 0);
    int top = kernel.getYOrigin();
    int bottom = Math.max(kHeight - top - 1, 0);
    
    // Calculate max sample values for clipping
    int[] maxValue = src.getSampleModel().getSampleSize();
    for (int i = 0; i < maxValue.length; i++)
      maxValue[i] = (int)Math.pow(2, maxValue[i]) - 1;
    
    // process the region that is reachable...
    int regionW = src.width - left - right;
    int regionH = src.height - top - bottom;
    float[] kvals = kernel.getKernelData(null);
    float[] tmp = new float[kWidth * kHeight];

    for (int x = 0; x < regionW; x++)
      {
        for (int y = 0; y < regionH; y++)
          {
            // FIXME: This needs a much more efficient implementation
            for (int b = 0; b < src.getNumBands(); b++)
            {
              float v = 0;
              src.getSamples(x, y, kWidth, kHeight, b, tmp);
              for (int i = 0; i < tmp.length; i++)
                v += tmp[tmp.length - i - 1] * kvals[i];
                // FIXME: in the above line, I've had to reverse the order of 
                // the samples array to make the tests pass.  I haven't worked 
                // out why this is necessary.

              // This clipping is is undocumented, but determined by testing.
              if (v > maxValue[b])
                v = maxValue[b];
              else if (v < 0)
                v = 0;

              dest.setSample(x + kernel.getXOrigin(), y + kernel.getYOrigin(), 
                             b, v);
            }
          }
      }
    
    // fill in the top border
    fillEdge(src, dest, 0, 0, src.width, top, edge);
    
    // fill in the bottom border
    fillEdge(src, dest, 0, src.height - bottom, src.width, bottom, edge);
    
    // fill in the left border
    fillEdge(src, dest, 0, top, left, regionH, edge);
    
    // fill in the right border
    fillEdge(src, dest, src.width - right, top, right, regionH, edge);
    
    return dest;  
  }
  
  /**
   * Fills a range of pixels (typically at the edge of a raster) with either
   * zero values (if <code>edgeOp</code> is <code>EDGE_ZERO_FILL</code>) or the 
   * corresponding pixel values from the source raster (if <code>edgeOp</code>
   * is <code>EDGE_NO_OP</code>).  This utility method is called by the 
   * {@link #fillEdge(Raster, WritableRaster, int, int, int, int, int)} method.
   * 
   * @param src  the source raster.
   * @param dest  the destination raster.
   * @param x  the x-coordinate of the top left pixel in the range.
   * @param y  the y-coordinate of the top left pixel in the range.
   * @param w  the width of the pixel range.
   * @param h  the height of the pixel range.
   * @param edgeOp  indicates how to determine the values for the range
   *     (either {@link #EDGE_ZERO_FILL} or {@link #EDGE_NO_OP}).
   */
  private void fillEdge(Raster src, WritableRaster dest, int x, int y, int w, 
                        int h, int edgeOp) 
  {
    if (w <= 0)
      return;
    if (h <= 0)
      return;
    if (edgeOp == EDGE_ZERO_FILL)  // fill region with zeroes
      {
        float[] zeros = new float[src.getNumBands() * w * h];
        dest.setPixels(x, y, w, h, zeros); 
      }
    else  // copy pixels from source
      {
        float[] pixels = new float[src.getNumBands() * w * h];
        src.getPixels(x, y, w, h, pixels);
        dest.setPixels(x, y, w, h, pixels);
      }
  }

  /* (non-Javadoc)
   * @see java.awt.image.RasterOp#createCompatibleDestRaster(java.awt.image.Raster)
   */
  public WritableRaster createCompatibleDestRaster(Raster src)
  {
    return src.createCompatibleWritableRaster();
  }

  /* (non-Javadoc)
   * @see java.awt.image.BufferedImageOp#getBounds2D(java.awt.image.BufferedImage)
   */
  public final Rectangle2D getBounds2D(BufferedImage src)
  {
    return src.getRaster().getBounds();
  }

  /* (non-Javadoc)
   * @see java.awt.image.RasterOp#getBounds2D(java.awt.image.Raster)
   */
  public final Rectangle2D getBounds2D(Raster src)
  {
    return src.getBounds();
  }

  /**
   * Returns the corresponding destination point for a source point. Because
   * this is not a geometric operation, the destination and source points will
   * be identical.
   * 
   * @param src The source point.
   * @param dst The transformed destination point.
   * @return The transformed destination point.
   */
  public final Point2D getPoint2D(Point2D src, Point2D dst)
  {
    if (dst == null) return (Point2D)src.clone();
    dst.setLocation(src);
    return dst;
  }
}