Source for gnu.java.awt.ComponentDataBlitOp

   1: /* Copyright (C) 2000, 2002, 2004  Free Software Foundation
   2: 
   3: This file is part of GNU Classpath.
   4: 
   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)
   8: any later version.
   9: 
  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.
  14: 
  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
  18: 02110-1301 USA.
  19: 
  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
  23: combination.
  24: 
  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. */
  36: 
  37: package gnu.java.awt;
  38: 
  39: import java.awt.RenderingHints;
  40: import java.awt.geom.Point2D;
  41: import java.awt.geom.Rectangle2D;
  42: import java.awt.image.ComponentSampleModel;
  43: import java.awt.image.DataBuffer;
  44: import java.awt.image.Raster;
  45: import java.awt.image.RasterOp;
  46: import java.awt.image.WritableRaster;
  47: 
  48: /**
  49:  * This raster copy operation assumes that both source and destination
  50:  * sample models are tightly pixel packed and contain the same number
  51:  * of bands.
  52:  *
  53:  * @throws java.lang.ClassCastException if the sample models of the
  54:  * rasters are not of type ComponentSampleModel.
  55:  *
  56:  * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  57:  */
  58: public class ComponentDataBlitOp implements RasterOp
  59: {
  60:   public static final ComponentDataBlitOp INSTANCE = new ComponentDataBlitOp();
  61: 
  62:   public WritableRaster filter(Raster src, WritableRaster dest)
  63:   {
  64:     if (dest == null)
  65:       dest = createCompatibleDestRaster(src);
  66: 
  67:     DataBuffer  srcDB =  src.getDataBuffer();
  68:     DataBuffer destDB = dest.getDataBuffer();
  69: 
  70:     ComponentSampleModel  srcSM = (ComponentSampleModel)  src.getSampleModel();
  71:     ComponentSampleModel destSM = (ComponentSampleModel) dest.getSampleModel();
  72: 
  73: 
  74:     // Calculate offset to data in the underlying arrays:
  75: 
  76:     int  srcScanlineStride =  srcSM.getScanlineStride();
  77:     int destScanlineStride = destSM.getScanlineStride();
  78:     int srcX  =  src.getMinX() -  src.getSampleModelTranslateX();
  79:     int srcY  =  src.getMinY() -  src.getSampleModelTranslateY();
  80:     int destX = dest.getMinX() - dest.getSampleModelTranslateX();
  81:     int destY = dest.getMinY() - dest.getSampleModelTranslateY();
  82: 
  83:     int numBands = srcSM.getNumBands();
  84: 
  85:     /* We can't use getOffset(x, y) from the sample model since we
  86:        don't want the band offset added in. */
  87: 
  88:     int srcOffset =
  89:       numBands*srcX + srcScanlineStride*srcY +    // from sample model
  90:       srcDB.getOffset();                          // from data buffer
  91: 
  92:     int destOffset =
  93:       numBands*destX + destScanlineStride*destY + // from sample model
  94:       destDB.getOffset();                         // from data buffer
  95: 
  96:     // Determine how much, and how many times to blit.
  97: 
  98:     int rowSize = src.getWidth()*numBands;
  99:     int h = src.getHeight();
 100: 
 101:     if ((rowSize == srcScanlineStride) &&
 102:         (rowSize == destScanlineStride))
 103:       {
 104:         // collapse scan line blits to one large blit.
 105:         rowSize *= h;
 106:         h = 1;
 107:       }
 108: 
 109: 
 110:     // Do blitting
 111: 
 112:     Object srcArray  = Buffers.getData(srcDB);
 113:     Object destArray = Buffers.getData(destDB);
 114: 
 115:     for (int yd = 0; yd<h; yd++)
 116:       {
 117:         System.arraycopy(srcArray, srcOffset,
 118:                          destArray, destOffset,
 119:                          rowSize);
 120:         srcOffset  +=  srcScanlineStride;
 121:         destOffset += destScanlineStride;
 122:       }
 123: 
 124: 
 125:     return dest;
 126:   }
 127: 
 128:   public Rectangle2D getBounds2D(Raster src)
 129:   {
 130:     return src.getBounds();
 131:   }
 132: 
 133:   public WritableRaster createCompatibleDestRaster(Raster src) {
 134: 
 135:     /* FIXME: Maybe we should explicitly create a raster with a
 136:        tightly pixel packed sample model, rather than assuming
 137:        that the createCompatibleWritableRaster() method in Raster
 138:        will create one. */
 139: 
 140:     return src.createCompatibleWritableRaster();
 141:   }
 142: 
 143:   public Point2D getPoint2D(Point2D srcPoint, Point2D destPoint)
 144:   {
 145:     if (destPoint == null)
 146:       return (Point2D) srcPoint.clone();
 147: 
 148:     destPoint.setLocation(srcPoint);
 149:     return destPoint;
 150:   }
 151: 
 152:   public RenderingHints getRenderingHints()
 153:   {
 154:     throw new UnsupportedOperationException("not implemented");
 155:   }
 156: }