Source for java.awt.image.WritableRaster

   1: /* Copyright (C) 2000, 2002, 2003, 2006,  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:  * A raster with methods to support updating pixel values.
  45:  * 
  46:  * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  47:  */
  48: public class WritableRaster extends Raster
  49: {
  50:   /**
  51:    * Creates a new <code>WritableRaster</code>.
  52:    * 
  53:    * @param sampleModel  the sample model.
  54:    * @param origin  the origin.
  55:    */
  56:   protected WritableRaster(SampleModel sampleModel, Point origin) 
  57:   {
  58:     this(sampleModel, sampleModel.createDataBuffer(), origin);
  59:   }
  60:   
  61:   /**
  62:    * Creates a new <code>WritableRaster</code> instance.
  63:    * 
  64:    * @param sampleModel  the sample model.
  65:    * @param dataBuffer  the data buffer.
  66:    * @param origin  the origin.
  67:    */
  68:   protected WritableRaster(SampleModel sampleModel, DataBuffer dataBuffer, 
  69:                            Point origin)
  70:   {
  71:     this(sampleModel, dataBuffer,
  72:          new Rectangle(origin != null ? origin.x : 0,
  73:                        origin != null ? origin.y : 0,
  74:                        sampleModel.getWidth(), sampleModel.getHeight()),
  75:          origin, null);
  76:   }
  77: 
  78:   /**
  79:    * Creates a new <code>WritableRaster</code> instance.
  80:    * 
  81:    * @param sampleModel  the sample model.
  82:    * @param dataBuffer  the data buffer.
  83:    * @param aRegion  the raster's bounds.
  84:    * @param sampleModelTranslate  the translation.
  85:    * @param parent  the parent.
  86:    */
  87:   protected WritableRaster(SampleModel sampleModel, 
  88:                            DataBuffer dataBuffer,
  89:                            Rectangle aRegion,
  90:                            Point sampleModelTranslate,
  91:                            WritableRaster parent)
  92:   {
  93:     super(sampleModel, dataBuffer, aRegion, sampleModelTranslate, parent);
  94:   }
  95: 
  96:   /**
  97:    * Returns the raster's parent, cast as a {@link WritableRaster}.
  98:    * 
  99:    * @return The raster's parent.
 100:    */
 101:   public WritableRaster getWritableParent()
 102:   {
 103:     return (WritableRaster) getParent();
 104:   }
 105:   
 106:   /**
 107:    * @param childMinX
 108:    * @param childMinY
 109:    * @return
 110:    */
 111:   public WritableRaster createWritableTranslatedChild(int childMinX,
 112:                                                       int childMinY)
 113:   {
 114:     // This mirrors the code from the super class
 115:     int tcx = sampleModelTranslateX - minX + childMinX;
 116:     int tcy = sampleModelTranslateY - minY + childMinY;
 117:     
 118:     return new WritableRaster(sampleModel, dataBuffer,
 119:         new Rectangle(childMinX, childMinY, width, height), 
 120:         new Point(tcx, tcy), this);
 121:   }
 122: 
 123:   /**
 124:    * 
 125:    * @param parentX
 126:    * @param parentY
 127:    * @param w
 128:    * @param h
 129:    * @param childMinX
 130:    * @param childMinY
 131:    * @param bandList
 132:    * @return
 133:    */
 134:   public WritableRaster createWritableChild(int parentX, int parentY,
 135:       int w, int h, int childMinX, int childMinY, int[] bandList)
 136:   {
 137:     // This mirrors the code from the super class
 138:     
 139:     // FIXME: Throw RasterFormatException if child bounds extends
 140:     // beyond the bounds of this raster.
 141:     
 142:     SampleModel sm = (bandList == null) ?
 143:       sampleModel :
 144:       sampleModel.createSubsetSampleModel(bandList);
 145:     
 146:     return new WritableRaster(sm, dataBuffer,
 147:         new Rectangle(childMinX, childMinY, w, h),
 148:         new Point(sampleModelTranslateX + childMinX - parentX,
 149:                   sampleModelTranslateY + childMinY - parentY),
 150:         this);
 151:   }
 152: 
 153:   public void setDataElements(int x, int y, Object inData)
 154:   {
 155:     sampleModel.setDataElements(x - sampleModelTranslateX, 
 156:         y - sampleModelTranslateY, inData, dataBuffer);
 157:   }
 158: 
 159:   public void setDataElements(int x, int y, Raster inRaster)
 160:   {
 161:     Object dataElements = getDataElements(0, 0, inRaster.getWidth(),
 162:         inRaster.getHeight(), null);
 163:     setDataElements(x, y, dataElements);
 164:   }
 165: 
 166:   public void setDataElements(int x, int y, int w, int h, Object inData)
 167:   {
 168:     sampleModel.setDataElements(x - sampleModelTranslateX,
 169:         y - sampleModelTranslateY, w, h, inData, dataBuffer);
 170:   }
 171: 
 172:   /**
 173:    * 
 174:    * @param srcRaster
 175:    */
 176:   public void setRect(Raster srcRaster)
 177:   {
 178:     setRect(0, 0, srcRaster);
 179:   }
 180: 
 181:   /**
 182:    * 
 183:    * @param dx
 184:    * @param dy
 185:    * @param srcRaster
 186:    */
 187:   public void setRect(int dx, int dy, Raster srcRaster) 
 188:   {
 189:     Rectangle targetUnclipped = new Rectangle(srcRaster.getMinX() + dx,
 190:         srcRaster.getMinY() + dy, srcRaster.getWidth(), srcRaster.getHeight());
 191:         
 192:     Rectangle target = getBounds().intersection(targetUnclipped);
 193: 
 194:     if (target.isEmpty()) return;
 195:     
 196:     int sx = target.x - dx;
 197:     int sy = target.y - dy;
 198:     
 199:     // FIXME: Do tests on rasters and use get/set data instead.
 200:     
 201:     /* The JDK documentation seems to imply this implementation.
 202:        (the trucation of higher bits), but an implementation using
 203:        get/setDataElements would be more efficient. None of the
 204:        implementations would do anything sensible when the sample
 205:        models don't match.
 206:        
 207:        But this is probably not the place to consider such
 208:        optimizations.*/
 209: 
 210:     int[] pixels = srcRaster.getPixels(sx, sy, target.width, target.height,
 211:                                        (int[]) null);
 212: 
 213:     setPixels(target.x, target.y, target.width, target.height, pixels);
 214:   }
 215: 
 216:   /**
 217:    * Sets the samples for the pixel at (x, y) in the raster to the specified 
 218:    * values. 
 219:    * 
 220:    * @param x  the x-coordinate of the pixel.
 221:    * @param y  the y-coordinate of the pixel.
 222:    * @param iArray  the sample values (<code>null</code> not permitted).
 223:    * 
 224:    * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
 225:    */
 226:   public void setPixel(int x, int y, int[] iArray)
 227:   {
 228:     sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY,
 229:                          iArray, dataBuffer);
 230:   }
 231: 
 232:   /**
 233:    * Sets the samples for the pixel at (x, y) in the raster to the specified 
 234:    * values. 
 235:    * 
 236:    * @param x  the x-coordinate of the pixel.
 237:    * @param y  the y-coordinate of the pixel.
 238:    * @param fArray  the sample values (<code>null</code> not permitted).
 239:    * 
 240:    * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
 241:    */
 242:   public void setPixel(int x, int y, float[] fArray)
 243:   {
 244:     sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY,
 245:                          fArray, dataBuffer);
 246:   }
 247: 
 248:   /**
 249:    * Sets the samples for the pixel at (x, y) in the raster to the specified 
 250:    * values. 
 251:    * 
 252:    * @param x  the x-coordinate of the pixel.
 253:    * @param y  the y-coordinate of the pixel.
 254:    * @param dArray  the sample values (<code>null</code> not permitted).
 255:    * 
 256:    * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
 257:    */
 258:   public void setPixel(int x, int y, double[] dArray)
 259:   {
 260:     sampleModel.setPixel(x - sampleModelTranslateX, y - sampleModelTranslateY,
 261:                          dArray, dataBuffer);
 262:   }
 263: 
 264:   /**
 265:    * Sets the sample values for the pixels in the region specified by 
 266:    * (x, y, w, h) in the raster.  The array is ordered by pixels (that is, all 
 267:    * the samples for the first pixel are grouped together, followed by all the 
 268:    * samples for the second pixel, and so on). 
 269:    *  
 270:    * @param x  the x-coordinate of the top-left pixel.
 271:    * @param y  the y-coordinate of the top-left pixel.
 272:    * @param w  the width of the region of pixels.
 273:    * @param h  the height of the region of pixels.
 274:    * @param iArray  the pixel sample values (<code>null</code> not permitted).
 275:    * 
 276:    * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
 277:    */
 278:   public void setPixels(int x, int y, int w, int h, int[] iArray)
 279:   {
 280:     sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY,
 281:                           w, h, iArray, dataBuffer);
 282:   }
 283: 
 284:   /**
 285:    * Sets the sample values for the pixels in the region specified by 
 286:    * (x, y, w, h) in the raster.  The array is ordered by pixels (that is, all 
 287:    * the samples for the first pixel are grouped together, followed by all the 
 288:    * samples for the second pixel, and so on). 
 289:    *  
 290:    * @param x  the x-coordinate of the top-left pixel.
 291:    * @param y  the y-coordinate of the top-left pixel.
 292:    * @param w  the width of the region of pixels.
 293:    * @param h  the height of the region of pixels.
 294:    * @param fArray  the pixel sample values (<code>null</code> not permitted).
 295:    * 
 296:    * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
 297:    */
 298:   public void setPixels(int x, int y, int w, int h, float[] fArray)
 299:   {
 300:     sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY,
 301:                           w, h, fArray, dataBuffer);
 302:   }
 303: 
 304:   /**
 305:    * Sets the sample values for the pixels in the region specified by 
 306:    * (x, y, w, h) in the raster.  The array is ordered by pixels (that is, all 
 307:    * the samples for the first pixel are grouped together, followed by all the 
 308:    * samples for the second pixel, and so on). 
 309:    *  
 310:    * @param x  the x-coordinate of the top-left pixel.
 311:    * @param y  the y-coordinate of the top-left pixel.
 312:    * @param w  the width of the region of pixels.
 313:    * @param h  the height of the region of pixels.
 314:    * @param dArray  the pixel sample values (<code>null</code> not permitted).
 315:    * 
 316:    * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
 317:    */
 318:   public void setPixels(int x, int y, int w, int h, double[] dArray)
 319:   {
 320:     sampleModel.setPixels(x - sampleModelTranslateX, y - sampleModelTranslateY,
 321:                           w, h, dArray, dataBuffer);
 322:   }
 323: 
 324:   /**
 325:    * Sets the sample value for a band for the pixel at (x, y) in the raster. 
 326:    * 
 327:    * @param x  the x-coordinate of the pixel.
 328:    * @param y  the y-coordinate of the pixel.
 329:    * @param b  the band (in the range <code>0</code> to 
 330:    *     <code>getNumBands() - 1</code>).
 331:    * @param s  the sample value.
 332:    */
 333:   public void setSample(int x, int y, int b, int s)
 334:   {
 335:     sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY,
 336:                           b, s, dataBuffer);
 337:   }
 338: 
 339:   /**
 340:    * Sets the sample value for a band for the pixel at (x, y) in the raster. 
 341:    * 
 342:    * @param x  the x-coordinate of the pixel.
 343:    * @param y  the y-coordinate of the pixel.
 344:    * @param b  the band (in the range <code>0</code> to 
 345:    *     <code>getNumBands() - 1</code>).
 346:    * @param s  the sample value.
 347:    */
 348:   public void setSample(int x, int y, int b, float s)
 349:   {
 350:     sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY,
 351:                           b, s, dataBuffer);
 352:   }
 353: 
 354:   /**
 355:    * Sets the sample value for a band for the pixel at (x, y) in the raster. 
 356:    * 
 357:    * @param x  the x-coordinate of the pixel.
 358:    * @param y  the y-coordinate of the pixel.
 359:    * @param b  the band (in the range <code>0</code> to 
 360:    *     <code>getNumBands() - 1</code>).
 361:    * @param s  the sample value.
 362:    */
 363:   public void setSample(int x, int y, int b, double s)
 364:   {
 365:     sampleModel.setSample(x - sampleModelTranslateX, y - sampleModelTranslateY,
 366:                           b, s, dataBuffer);
 367:   }
 368: 
 369:   /**
 370:    * Sets the sample values for one band for the pixels in the region 
 371:    * specified by (x, y, w, h) in the raster. 
 372:    * 
 373:    * @param x  the x-coordinate of the top-left pixel.
 374:    * @param y  the y-coordinate of the top-left pixel.
 375:    * @param w  the width of the region of pixels.
 376:    * @param h  the height of the region of pixels.
 377:    * @param b  the band (in the range <code>0</code> to 
 378:    *     </code>getNumBands() - 1</code>).
 379:    * @param iArray  the sample values (<code>null</code> not permitted).
 380:    * 
 381:    * @throws NullPointerException if <code>iArray</code> is <code>null</code>.
 382:    */
 383:   public void setSamples(int x, int y, int w, int h, int b,
 384:                          int[] iArray)
 385:   {
 386:     sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY,
 387:                            w, h, b, iArray, dataBuffer);
 388:   }
 389: 
 390:   /**
 391:    * Sets the sample values for one band for the pixels in the region 
 392:    * specified by (x, y, w, h) in the raster. 
 393:    * 
 394:    * @param x  the x-coordinate of the top-left pixel.
 395:    * @param y  the y-coordinate of the top-left pixel.
 396:    * @param w  the width of the region of pixels.
 397:    * @param h  the height of the region of pixels.
 398:    * @param b  the band (in the range <code>0</code> to 
 399:    *     </code>getNumBands() - 1</code>).
 400:    * @param fArray  the sample values (<code>null</code> not permitted).
 401:    * 
 402:    * @throws NullPointerException if <code>fArray</code> is <code>null</code>.
 403:    */
 404:   public void setSamples(int x, int y, int w, int h, int b,
 405:                          float[] fArray)
 406:   {
 407:     sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY,
 408:                            w, h, b, fArray, dataBuffer);
 409:   }
 410: 
 411:   /**
 412:    * Sets the sample values for one band for the pixels in the region 
 413:    * specified by (x, y, w, h) in the raster. 
 414:    * 
 415:    * @param x  the x-coordinate of the top-left pixel.
 416:    * @param y  the y-coordinate of the top-left pixel.
 417:    * @param w  the width of the region of pixels.
 418:    * @param h  the height of the region of pixels.
 419:    * @param b  the band (in the range <code>0</code> to 
 420:    *     </code>getNumBands() - 1</code>).
 421:    * @param dArray  the sample values (<code>null</code> not permitted).
 422:    * 
 423:    * @throws NullPointerException if <code>dArray</code> is <code>null</code>.
 424:    */
 425:   public void setSamples(int x, int y, int w, int h, int b,
 426:                          double[] dArray)
 427:   {
 428:     sampleModel.setSamples(x - sampleModelTranslateX, y - sampleModelTranslateY,
 429:                            w, h, b, dArray, dataBuffer);
 430:   }
 431: }