Source for java.awt.image.WritableRaster

   1: /* Copyright (C) 2000, 2002, 2003  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: 
  38: package java.awt.image;
  39: 
  40: import java.awt.Point;
  41: import java.awt.Rectangle;
  42: 
  43: /**
  44:  * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  45:  */
  46: public class WritableRaster extends Raster
  47: {
  48:   protected WritableRaster(SampleModel sampleModel, Point origin) 
  49:   {
  50:     this(sampleModel, sampleModel.createDataBuffer(), origin);
  51:   }
  52:   
  53:   protected WritableRaster(SampleModel sampleModel,
  54:                DataBuffer dataBuffer, Point origin)
  55:   {
  56:     this(sampleModel, dataBuffer,
  57:      new Rectangle(origin != null ? origin.x : 0,
  58:                        origin != null ? origin.y : 0,
  59:                sampleModel.getWidth(), sampleModel.getHeight()),
  60:      origin,
  61:      null);
  62:   }
  63: 
  64:   protected WritableRaster(SampleModel sampleModel, 
  65:                DataBuffer dataBuffer,
  66:                Rectangle aRegion,
  67:                Point sampleModelTranslate,
  68:                WritableRaster parent)
  69:   {
  70:     super(sampleModel, dataBuffer, aRegion, sampleModelTranslate,
  71:       parent);
  72:   }
  73: 
  74:   public WritableRaster getWritableParent()
  75:   {
  76:     return (WritableRaster) getParent();
  77:   }
  78:   
  79:   public WritableRaster createWritableTranslatedChild(int childMinX,
  80:                               int childMinY)
  81:   {
  82:     // This mirrors the code from the super class
  83:     int tcx = sampleModelTranslateX - minX + childMinX;
  84:     int tcy = sampleModelTranslateY - minY + childMinY;
  85:     
  86:     return new WritableRaster(sampleModel, dataBuffer,
  87:                   new Rectangle(childMinX, childMinY,
  88:                         width, height),
  89:                   new Point(tcx, tcy),
  90:                   this);
  91:   }
  92: 
  93:   public WritableRaster createWritableChild(int parentX,
  94:                         int parentY,
  95:                         int w, int h,
  96:                         int childMinX,
  97:                         int childMinY,
  98:                         int[] bandList)
  99:   {
 100:     // This mirrors the code from the super class
 101:     
 102:     // FIXME: Throw RasterFormatException if child bounds extends
 103:     // beyond the bounds of this raster.
 104:     
 105:     SampleModel sm = (bandList == null) ?
 106:       sampleModel :
 107:       sampleModel.createSubsetSampleModel(bandList);
 108:     
 109:     return new
 110:       WritableRaster(sm, dataBuffer,
 111:              new Rectangle(childMinX, childMinY,
 112:                    w, h),
 113:              new Point(sampleModelTranslateX+childMinX-parentX,
 114:                    sampleModelTranslateY+childMinY-parentY),
 115:              this);
 116:   }
 117: 
 118:   public void setDataElements(int x, int y, Object inData)
 119:   {
 120:     sampleModel.setDataElements(x-sampleModelTranslateX,
 121:                 y-sampleModelTranslateY,
 122:                 inData, dataBuffer);
 123:   }
 124: 
 125:   public void setDataElements(int x, int y, Raster inRaster)
 126:   {
 127:     Object dataElements = getDataElements(0, 0,
 128:                       inRaster.getWidth(),
 129:                       inRaster.getHeight(),
 130:                       null);
 131:     setDataElements(x, y, dataElements);
 132:   }
 133: 
 134:   public void setDataElements(int x, int y, int w, int h,
 135:                   Object inData)
 136:   {
 137:     sampleModel.setDataElements(x-sampleModelTranslateX,
 138:                 y-sampleModelTranslateY,
 139:                 w, h, inData, dataBuffer);
 140:   }
 141: 
 142:   public void setRect(Raster srcRaster)
 143:   {
 144:     setRect(0, 0, srcRaster);
 145:   }
 146: 
 147:   public void setRect(int dx, int dy, Raster srcRaster) 
 148:   {
 149:     Rectangle targetUnclipped = new Rectangle(srcRaster.getMinX()+dx,
 150:                           srcRaster.getMinY()+dy,
 151:                           srcRaster.getWidth(),
 152:                           srcRaster.getHeight());
 153:     
 154:     Rectangle target = getBounds().intersection(targetUnclipped);
 155: 
 156:     if (target.isEmpty()) return;
 157:     
 158:     int sx = target.x - dx;
 159:     int sy = target.y - dy;
 160:     
 161:     // FIXME: Do tests on rasters and use get/set data instead.
 162:     
 163:     /* The JDK documentation seems to imply this implementation.
 164:        (the trucation of higher bits), but an implementation using
 165:        get/setDataElements would be more efficient. None of the
 166:        implementations would do anything sensible when the sample
 167:        models don't match.
 168:        
 169:        But this is probably not the place to consider such
 170:        optimizations.*/
 171: 
 172:     int[] pixels = srcRaster.getPixels(sx, sy,
 173:                        target.width, target.height,
 174:                        (int[]) null);
 175: 
 176:     setPixels(target.x, target.y, target.width, target.height, pixels);
 177:   }
 178: 
 179:   public void setPixel(int x, int y, int[] iArray)
 180:   {
 181:     sampleModel.setPixel(x-sampleModelTranslateX,
 182:              y-sampleModelTranslateY,
 183:              iArray, dataBuffer);
 184:   }
 185: 
 186:   public void setPixel(int x, int y, float[] fArray)
 187:   {
 188:     sampleModel.setPixel(x-sampleModelTranslateX,
 189:              y-sampleModelTranslateY,
 190:              fArray, dataBuffer);
 191:   }
 192: 
 193:   public void setPixel(int x, int y, double[] dArray)
 194:   {
 195:     sampleModel.setPixel(x-sampleModelTranslateX,
 196:              y-sampleModelTranslateY,
 197:              dArray, dataBuffer);
 198:   }
 199: 
 200:   public void setPixels(int x, int y, int w, int h, int[] iArray)
 201:   {
 202:     sampleModel.setPixels(x-sampleModelTranslateX,
 203:               y-sampleModelTranslateY,
 204:               w, h, iArray, dataBuffer);
 205:   }
 206: 
 207:   public void setPixels(int x, int y, int w, int h, float[] fArray)
 208:   {
 209:     sampleModel.setPixels(x-sampleModelTranslateX,
 210:               y-sampleModelTranslateY,
 211:               w, h, fArray, dataBuffer);
 212:   }
 213: 
 214:   public void setPixels(int x, int y, int w, int h, double[] dArray)
 215:   {
 216:     sampleModel.setPixels(x-sampleModelTranslateX,
 217:               y-sampleModelTranslateY,
 218:               w, h, dArray, dataBuffer);
 219:   }
 220: 
 221:   public void setSample(int x, int y, int b, int s)
 222:   {
 223:     sampleModel.setSample(x-sampleModelTranslateX,
 224:               y-sampleModelTranslateY,
 225:               b, s, dataBuffer);
 226:   }
 227: 
 228:   public void setSample(int x, int y, int b, float s)
 229:   {
 230:     sampleModel.setSample(x-sampleModelTranslateX,
 231:               y-sampleModelTranslateY,
 232:               b, s, dataBuffer);
 233:   }
 234: 
 235:   public void setSample(int x, int y, int b, double s)
 236:   {
 237:     sampleModel.setSample(x-sampleModelTranslateX,
 238:               y-sampleModelTranslateY,
 239:               b, s, dataBuffer);
 240:   }
 241: 
 242:   public void setSamples(int x, int y, int w, int h, int b,
 243:              int[] iArray)
 244:   {
 245:     sampleModel.setSamples(x-sampleModelTranslateX,
 246:                y-sampleModelTranslateY,
 247:                w, h, b, iArray, dataBuffer);
 248:   }
 249: 
 250:   public void setSamples(int x, int y, int w, int h, int b,
 251:              float[] fArray)
 252:   {
 253:     sampleModel.setSamples(x-sampleModelTranslateX,
 254:                y-sampleModelTranslateY,
 255:                w, h, b, fArray, dataBuffer);
 256:   }
 257: 
 258:   public void setSamples(int x, int y, int w, int h, int b,
 259:              double[] dArray)
 260:   {
 261:     sampleModel.setSamples(x-sampleModelTranslateX,
 262:                y-sampleModelTranslateY,
 263:                w, h, b, dArray, dataBuffer);
 264:   }
 265: }