001    /* Copyright (C) 2004  Free Software Foundation
002    
003    This file is part of GNU Classpath.
004    
005    GNU Classpath is free software; you can redistribute it and/or modify
006    it under the terms of the GNU General Public License as published by
007    the Free Software Foundation; either version 2, or (at your option)
008    any later version.
009    
010    GNU Classpath is distributed in the hope that it will be useful, but
011    WITHOUT ANY WARRANTY; without even the implied warranty of
012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013    General Public License for more details.
014    
015    You should have received a copy of the GNU General Public License
016    along with GNU Classpath; see the file COPYING.  If not, write to the
017    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
018    02110-1301 USA.
019    
020    Linking this library statically or dynamically with other modules is
021    making a combined work based on this library.  Thus, the terms and
022    conditions of the GNU General Public License cover the whole
023    combination.
024    
025    As a special exception, the copyright holders of this library give you
026    permission to link this library with independent modules to produce an
027    executable, regardless of the license terms of these independent
028    modules, and to copy and distribute the resulting executable under
029    terms of your choice, provided that you also meet, for each linked
030    independent module, the terms and conditions of the license of that
031    module.  An independent module is a module which is not derived from
032    or based on this library.  If you modify this library, you may extend
033    this exception to your version of the library, but you are not
034    obligated to do so.  If you do not wish to do so, delete this
035    exception statement from your version. */
036    
037    package java.awt.image;
038    
039    import java.awt.Point;
040    
041    /**
042     * The BufferedImageFilter class wraps BufferedImageOp objects in a Filter.
043     *
044     * When pixels are pushed through the filter, we create a BufferedImage,
045     * apply the BufferedImageOp, and pass the filtered pixels to the base class.
046     *
047     * @author jlquinn@optonline.net
048     */
049    public class BufferedImageFilter extends ImageFilter implements Cloneable
050    {
051      private BufferedImageOp op;
052    
053      /**
054       *
055       */
056      public BufferedImageFilter(BufferedImageOp op)
057      {
058        super();
059        if (op == null)
060          throw new NullPointerException("BufferedImageFilter null"
061                                         + " op in constructor");
062        this.op = op;
063      }
064    
065      /**
066       * @return Returns the contained BufferedImageOp.
067       */
068      public BufferedImageOp getBufferedImageOp()
069      {
070        return op;
071      }
072    
073      // FIXME: Definitely not sure this is the right thing.  I'm not sure how to
074      // create a compatible sample model that incorporates scansize != w.  I
075      // asume off is handled by the db itself.
076      public void setPixels(int x, int y, int w, int h, ColorModel model,
077                            byte[] pixels, int off, int scansize)
078      {
079        // Create an input BufferedImage
080        DataBufferByte db = new DataBufferByte(pixels, scansize * h + off, off);
081        SampleModel sm = model.createCompatibleSampleModel(scansize, h);
082        WritableRaster wr = new WritableRaster(sm, db, new Point(0, 0));
083        BufferedImage in =
084          new BufferedImage(model, wr, model.isAlphaPremultiplied(), null);
085        BufferedImage out = op.createCompatibleDestImage(in, model);
086        op.filter(in, out);
087        DataBuffer dbout = out.getRaster().getDataBuffer();
088        super.setPixels(0, 0, w, h, model, ((DataBufferByte)dbout).getData(), 0,
089                        scansize);
090      }
091    
092      // FIXME: Definitely not sure this is the right thing.  I'm not sure how
093      // to create a compatible sample model that incorporates
094      // scansize != w.  I asume off is handled by the db itself.
095      public void setPixels(int x, int y, int w, int h, ColorModel model,
096                            int[] pixels, int off, int scansize)
097      {
098        // Create an input BufferedImage
099        DataBufferInt db = new DataBufferInt(pixels, scansize * h + off, off);
100        SampleModel sm = model.createCompatibleSampleModel(scansize, h);
101        WritableRaster wr = new WritableRaster(sm, db, new Point(0, 0));
102        BufferedImage in =
103          new BufferedImage(model, wr, model.isAlphaPremultiplied(), null);
104        BufferedImage out = op.createCompatibleDestImage(in, model);
105        op.filter(in, out);
106        DataBuffer dbout = out.getRaster().getDataBuffer();
107        super.setPixels(0, 0, w, h, model, ((DataBufferInt)dbout).getData(), 0,
108                        scansize);
109      }
110    }