Source for java.awt.image.Raster

   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 rectangular collection of pixels composed from a {@link DataBuffer} which
  45:  * stores the pixel values, and a {@link SampleModel} which is used to retrieve
  46:  * the pixel values.
  47:  * 
  48:  * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  49:  */
  50: public class Raster
  51: {
  52:   /** The sample model used to access the pixel values. */
  53:   protected SampleModel sampleModel;
  54:   
  55:   /** The data buffer used to store the pixel values. */
  56:   protected DataBuffer dataBuffer;
  57:   
  58:   /** The x-coordinate of the top left corner of the raster. */
  59:   protected int minX;
  60:   
  61:   /** The y-coordinate of the top left corner of the raster. */
  62:   protected int minY;
  63:   
  64:   /** The width of the raster. */
  65:   protected int width;
  66:   
  67:   /** The height of the raster. */
  68:   protected int height;
  69:   
  70:   protected int sampleModelTranslateX;
  71:   
  72:   protected int sampleModelTranslateY;
  73:   
  74:   /** The number of bands. */
  75:   protected int numBands;
  76:   
  77:   protected int numDataElements;
  78:   
  79:   /** The raster's parent. */
  80:   protected Raster parent;
  81:   
  82:   /**
  83:    * Creates a new raster.
  84:    * 
  85:    * @param sampleModel  the sample model.
  86:    * @param origin  the origin.
  87:    */
  88:   protected Raster(SampleModel sampleModel, Point origin)
  89:   {
  90:     this(sampleModel, sampleModel.createDataBuffer(), origin);
  91:   }
  92:   
  93:   /**
  94:    * Creates a new raster.
  95:    * 
  96:    * @param sampleModel  the sample model.
  97:    * @param dataBuffer  the data buffer.
  98:    * @param origin  the origin.
  99:    */
 100:   protected Raster(SampleModel sampleModel, DataBuffer dataBuffer,
 101:                    Point origin)
 102:   {
 103:     this(sampleModel, dataBuffer, new Rectangle(origin.x, origin.y,
 104:          sampleModel.getWidth(), sampleModel.getHeight()), origin, null);
 105:   }
 106: 
 107:   /**
 108:    * Creates a new raster.
 109:    * 
 110:    * @param sampleModel  the sample model.
 111:    * @param dataBuffer  the data buffer.
 112:    * @param aRegion  the raster's bounds.
 113:    * @param sampleModelTranslate  the translation (<code>null</code> permitted).
 114:    * @param parent  the raster's parent.
 115:    */
 116:   protected Raster(SampleModel sampleModel, DataBuffer dataBuffer,
 117:       Rectangle aRegion, Point sampleModelTranslate, Raster parent)
 118:   {
 119:     this.sampleModel = sampleModel;
 120:     this.dataBuffer = dataBuffer;
 121:     this.minX = aRegion.x;
 122:     this.minY = aRegion.y;
 123:     this.width = aRegion.width;
 124:     this.height = aRegion.height;
 125:     
 126:     // If sampleModelTranslate is null, use (0,0).  Methods such as
 127:     // Raster.createRaster are specified to allow for a null argument.
 128:     if (sampleModelTranslate != null)
 129:     {
 130:       this.sampleModelTranslateX = sampleModelTranslate.x;
 131:       this.sampleModelTranslateY = sampleModelTranslate.y;
 132:     }
 133: 
 134:     this.numBands = sampleModel.getNumBands();
 135:     this.numDataElements = sampleModel.getNumDataElements();
 136:     this.parent = parent;
 137:   }
 138:     
 139:   /**
 140:    * Creates an interleaved raster using the specified data type.
 141:    * 
 142:    * @param dataType  the data type.
 143:    * @param w  the width.
 144:    * @param h  the height.
 145:    * @param bands  the number of bands.
 146:    * @param location
 147:    * 
 148:    * @return The new raster.
 149:    */
 150:   public static WritableRaster createInterleavedRaster(int dataType,
 151:       int w, int h, int bands, Point location)
 152:   {
 153:     int[] bandOffsets = new int[bands];
 154:     // TODO: Maybe not generate this every time.
 155:     for (int b = 0; b < bands; b++) 
 156:       bandOffsets[b] = b;
 157:     
 158:     int scanlineStride = bands * w;
 159:     return createInterleavedRaster(dataType, w, h, scanlineStride, bands,
 160:                                    bandOffsets, location);
 161:   }
 162: 
 163:   /**
 164:    * Creates an interleaved raster.
 165:    * 
 166:    * @param dataType  the data type.
 167:    * @param w  the width.
 168:    * @param h  the height.
 169:    * @param scanlineStride  the number of data elements from a sample on one 
 170:    *     row to the corresponding sample on the next row.
 171:    * @param pixelStride  the number of elements from a sample in one pixel to
 172:    *     the corresponding sample in the next pixel.
 173:    * @param bandOffsets  the band offsets.
 174:    * @param location
 175:    * 
 176:    * @return The new raster.
 177:    */
 178:   public static WritableRaster createInterleavedRaster(int dataType, 
 179:       int w, int h, int scanlineStride, int pixelStride, int[] bandOffsets,
 180:       Point location)
 181:   {
 182:     SampleModel sm = new ComponentSampleModel(dataType, w, h, pixelStride,
 183:         scanlineStride, bandOffsets);
 184:     return createWritableRaster(sm, location);
 185:   }
 186: 
 187:   /**
 188:    * Creates a new banded raster.
 189:    * 
 190:    * @param dataType  the data type.
 191:    * @param w  the width.
 192:    * @param h  the height.
 193:    * @param bands  the number of bands.
 194:    * @param location  
 195:    * 
 196:    * @return The new raster.
 197:    */
 198:   public static WritableRaster createBandedRaster(int dataType, int w, int h, 
 199:       int bands, Point location)
 200:   {
 201:     SampleModel sm = new BandedSampleModel(dataType, w, h, bands);
 202:     return createWritableRaster(sm, location);
 203:   }
 204: 
 205:   /**
 206:    * Creates a new banded raster.
 207:    * 
 208:    * @param dataType  the data type.
 209:    * @param w  the width.
 210:    * @param h  the height.
 211:    * @param scanlineStride  the number of data elements from a sample on one 
 212:    *     row to the corresponding sample on the next row.
 213:    * @param bankIndices  the index for each bank.
 214:    * @param bandOffsets  the offset for each band.
 215:    * @param location
 216:    * 
 217:    * @return The new raster.
 218:    */
 219:   public static WritableRaster createBandedRaster(int dataType, int w, int h,
 220:       int scanlineStride, int[] bankIndices, int[] bandOffsets, Point location)
 221:   {
 222:     SampleModel sm = new BandedSampleModel(dataType, w, h, scanlineStride,
 223:                                            bankIndices, bandOffsets);
 224:     return createWritableRaster(sm, location);
 225:   }
 226:   
 227:   /**
 228:    * Creates a new packed raster.
 229:    * 
 230:    * @param dataType  the data type.
 231:    * @param w  the width.
 232:    * @param h  the height.
 233:    * @param bandMasks  the bit mask for each band.
 234:    * @param location 
 235:    * 
 236:    * @return The new raster.
 237:    */
 238:   public static WritableRaster createPackedRaster(int dataType, int w, int h,
 239:       int[] bandMasks, Point location)
 240:   {
 241:     SampleModel sm = new SinglePixelPackedSampleModel(dataType, w, h,
 242:                                                      bandMasks);
 243:     return createWritableRaster(sm, location);
 244:   }
 245: 
 246:   /**
 247:    * Creates a new raster.
 248:    * 
 249:    * @param dataType  the data type.
 250:    * @param w  the width.
 251:    * @param h  the height.
 252:    * @param bands  the number of bands.
 253:    * @param bitsPerBand  the number of bits per band.
 254:    * @param location
 255:    * 
 256:    * @return The new raster.
 257:    */
 258:   public static WritableRaster createPackedRaster(int dataType,
 259:       int w, int h, int bands, int bitsPerBand, Point location)
 260:   {
 261:     if (bands <= 0 || (bands * bitsPerBand > getTypeBits(dataType)))
 262:       throw new IllegalArgumentException();
 263: 
 264:     SampleModel sm;
 265: 
 266:     if (bands == 1)
 267:       sm = new MultiPixelPackedSampleModel(dataType, w, h, bitsPerBand);
 268:     else
 269:       {
 270:         int[] bandMasks = new int[bands];
 271:         int mask = 0x1;
 272:         for (int bits = bitsPerBand; --bits != 0;)
 273:           mask = (mask << 1) | 0x1;
 274:         for (int i = 0; i < bands; i++)
 275:           {
 276:             bandMasks[i] = mask;
 277:             mask <<= bitsPerBand;
 278:           }
 279:           
 280:         sm = new SinglePixelPackedSampleModel(dataType, w, h, bandMasks);
 281:       }
 282:     return createWritableRaster(sm, location);
 283:   }
 284: 
 285:   /**
 286:    * Creates a new interleaved raster.
 287:    * 
 288:    * @param dataBuffer  the data buffer.
 289:    * @param w  the width.
 290:    * @param h  the height.
 291:    * @param scanlineStride  the number of data elements from a sample on one 
 292:    *     row to the corresponding sample on the next row.
 293:    * @param pixelStride  the number of elements from a sample in one pixel to
 294:    *     the corresponding sample in the next pixel.
 295:    * @param bandOffsets  the offset for each band.
 296:    * @param location
 297:    * 
 298:    * @return The new raster.
 299:    */
 300:   public static WritableRaster createInterleavedRaster(DataBuffer dataBuffer, 
 301:       int w, int h, int scanlineStride, int pixelStride, int[] bandOffsets, 
 302:       Point location)
 303:   {
 304:     SampleModel sm = new ComponentSampleModel(dataBuffer.getDataType(),
 305:         w, h, scanlineStride, pixelStride, bandOffsets);
 306:     return createWritableRaster(sm, dataBuffer, location);
 307:   }
 308: 
 309:   /**
 310:    * Creates a new banded raster.
 311:    * 
 312:    * @param dataBuffer  the data buffer.
 313:    * @param w  the width.
 314:    * @param h  the height.
 315:    * @param scanlineStride  the number of data elements from a sample on one 
 316:    *     row to the corresponding sample on the next row.
 317:    * @param bankIndices  the index for each bank.
 318:    * @param bandOffsets  the band offsets.
 319:    * @param location
 320:    * 
 321:    * @return The new raster.
 322:    */
 323:   public static WritableRaster createBandedRaster(DataBuffer dataBuffer,
 324:       int w, int h, int scanlineStride, int[] bankIndices, int[] bandOffsets,
 325:       Point location)
 326:   {
 327:     SampleModel sm = new BandedSampleModel(dataBuffer.getDataType(),
 328:         w, h, scanlineStride, bankIndices, bandOffsets);
 329:     return createWritableRaster(sm, dataBuffer, location);
 330:   }
 331:   
 332:   /**
 333:    * Creates a new packed raster.
 334:    * 
 335:    * @param dataBuffer  the data buffer.
 336:    * @param w  the width.
 337:    * @param h  the height.
 338:    * @param scanlineStride  the number of data elements from a sample on one 
 339:    *     row to the corresponding sample on the next row.
 340:    * @param bandMasks  the bit mask for each band.
 341:    * @param location
 342:    * 
 343:    * @return The new raster.
 344:    */
 345:   public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 346:       int w, int h, int scanlineStride, int[] bandMasks, Point location)
 347:  {
 348:     SampleModel sm = new SinglePixelPackedSampleModel(dataBuffer.getDataType(),
 349:         w, h, scanlineStride, bandMasks);
 350:     return createWritableRaster(sm, dataBuffer, location);
 351:   }
 352:   
 353:   /**
 354:    * Creates a new packed raster.
 355:    * 
 356:    * @param dataBuffer  the data buffer.
 357:    * @param w  the width.
 358:    * @param h  the height.
 359:    * @param bitsPerPixel  the number of bits per pixel.
 360:    * @param location
 361:    * 
 362:    * @return The new raster.
 363:    */
 364:   public static WritableRaster createPackedRaster(DataBuffer dataBuffer,
 365:       int w, int h, int bitsPerPixel, Point location)
 366:   {
 367:     SampleModel sm = new MultiPixelPackedSampleModel(dataBuffer.getDataType(),
 368:         w, h, bitsPerPixel);
 369:     return createWritableRaster(sm, dataBuffer, location);
 370:   }
 371:     
 372:   /**
 373:    * Creates a new raster.
 374:    * 
 375:    * @param sm  the sample model.
 376:    * @param db  the data buffer.
 377:    * @param location
 378:    * 
 379:    * @return The new raster.
 380:    */
 381:   public static Raster createRaster(SampleModel sm, DataBuffer db,
 382:                                     Point location)
 383:   {
 384:     return new Raster(sm, db, location);
 385:   }
 386: 
 387:   /**
 388:    * Creates a new writable raster.
 389:    * 
 390:    * @param sm  the sample model.
 391:    * @param location
 392:    * 
 393:    * @return The new writable raster.
 394:    */
 395:   public static WritableRaster createWritableRaster(SampleModel sm,
 396:                                                     Point location)
 397:   {
 398:     return new WritableRaster(sm, location);
 399:   }
 400: 
 401:   /**
 402:    * Creates a new writable raster.
 403:    * 
 404:    * @param sm  the sample model.
 405:    * @param db  the data buffer.
 406:    * @param location 
 407:    * 
 408:    * @return The new writable raster.
 409:    */
 410:   public static WritableRaster createWritableRaster(SampleModel sm,
 411:       DataBuffer db, Point location)
 412:   {
 413:     return new WritableRaster(sm, db, location);
 414:   }
 415: 
 416:   /**
 417:    * Returns the raster's parent.
 418:    * 
 419:    * @return The raster's parent.
 420:    */
 421:   public Raster getParent()
 422:   {
 423:     return parent;
 424:   }
 425: 
 426:   /**
 427:    * Returns the x-translation.
 428:    * 
 429:    * @return The x-translation.
 430:    */
 431:   public final int getSampleModelTranslateX()
 432:   {
 433:     return sampleModelTranslateX;
 434:   }
 435: 
 436:   /**
 437:    * Returns the y-translation.
 438:    * 
 439:    * @return The y-translation.
 440:    */
 441:   public final int getSampleModelTranslateY()
 442:   {
 443:     return sampleModelTranslateY;
 444:   }
 445: 
 446:   /**
 447:    * Creates a new writable raster that is compatible with this raster.
 448:    * 
 449:    * @return A new writable raster.
 450:    */
 451:   public WritableRaster createCompatibleWritableRaster()
 452:   {
 453:     return new WritableRaster(getSampleModel(), new Point(minX, minY));
 454:   }
 455: 
 456:   /**
 457:    * Creates a new writable raster that is compatible with this raster.
 458:    * 
 459:    * @param w  the width.
 460:    * @param h  the height.
 461:    * 
 462:    * @return A new writable raster.
 463:    */
 464:   public WritableRaster createCompatibleWritableRaster(int w, int h)
 465:   {
 466:     return createCompatibleWritableRaster(minX, minY, w, h);
 467:   }
 468: 
 469:   /**
 470:    * Creates a new writable raster that is compatible with this raster, with
 471:    * the specified bounds.
 472:    * 
 473:    * @param rect  the raster bounds.
 474:    * 
 475:    * @return A new writable raster.
 476:    */
 477:   public WritableRaster createCompatibleWritableRaster(Rectangle rect)
 478:   {
 479:     return createCompatibleWritableRaster(rect.x, rect.y,
 480:                                           rect.width, rect.height);
 481:   }
 482: 
 483:   /**
 484:    * Creates a new writable raster that is compatible with this raster, with
 485:    * the specified bounds.
 486:    * 
 487:    * @param x  the x-coordinate of the top-left corner of the raster.
 488:    * @param y  the y-coordinate of the top-left corner of the raster.
 489:    * @param w  the raster width.
 490:    * @param h  the raster height.
 491:    * 
 492:    * @return A new writable raster.
 493:    */
 494:   public WritableRaster createCompatibleWritableRaster(int x, int y,
 495:                                                        int w, int h)
 496:   {
 497:     SampleModel sm = getSampleModel().createCompatibleSampleModel(w, h);
 498:     return new WritableRaster(sm, sm.createDataBuffer(), new Point(x, y));
 499:   }
 500: 
 501:   public Raster createTranslatedChild(int childMinX, int childMinY) {
 502:     int tcx = sampleModelTranslateX - minX + childMinX;
 503:     int tcy = sampleModelTranslateY - minY + childMinY;
 504:     
 505:     return new Raster(sampleModel, dataBuffer,
 506:                       new Rectangle(childMinX, childMinY, width, height),
 507:                       new Point(tcx, tcy), this);
 508:   }
 509: 
 510:   public Raster createChild(int parentX, int parentY, int width,
 511:                             int height, int childMinX, int childMinY,
 512:                             int[] bandList)
 513:   {
 514:     /* FIXME: Throw RasterFormatException if child bounds extends
 515:        beyond the bounds of this raster. */
 516: 
 517:     SampleModel sm = (bandList == null) ?
 518:       sampleModel :
 519:       sampleModel.createSubsetSampleModel(bandList);
 520: 
 521:     /*
 522:         data origin
 523:        /
 524:       +-------------------------
 525:       |\. __ parent trans
 526:       | \`.  
 527:       |  \ `.    parent origin
 528:       |   \  `. /
 529:       |   /\   +-------- - -
 530:       |trans\ /<\-- deltaTrans
 531:       |child +-+-\---- - - 
 532:       |     /|`|  \__ parent [x, y]
 533:       |child | |`. \
 534:       |origin| :  `.\
 535:       |      |    / `\
 536:       |      :   /    +
 537:       | child [x, y] 
 538: 
 539:       parent_xy - parent_trans = child_xy - child_trans
 540: 
 541:       child_trans = parent_trans + child_xy - parent_xy
 542:     */
 543: 
 544:     return new Raster(sm, dataBuffer,
 545:         new Rectangle(childMinX, childMinY, width, height),
 546:         new Point(sampleModelTranslateX + childMinX - parentX,
 547:                   sampleModelTranslateY + childMinY - parentY),
 548:         this);
 549:   }
 550: 
 551:   /**
 552:    * Returns a new rectangle containing the bounds of this raster.
 553:    * 
 554:    * @return A new rectangle containing the bounds of this raster.
 555:    */
 556:   public Rectangle getBounds()
 557:   {
 558:     return new Rectangle(minX, minY, width, height);
 559:   }
 560: 
 561:   /**
 562:    * Returns the x-coordinate of the top left corner of the raster.
 563:    * 
 564:    * @return The x-coordinate of the top left corner of the raster.
 565:    */
 566:   public final int getMinX()
 567:   {
 568:     return minX;
 569:   }
 570: 
 571:   /**
 572:    * Returns the t-coordinate of the top left corner of the raster.
 573:    * 
 574:    * @return The t-coordinate of the top left corner of the raster.
 575:    */
 576:   public final int getMinY()
 577:   {
 578:     return minY;
 579:   }
 580: 
 581:   /**
 582:    * Returns the width of the raster.
 583:    * 
 584:    * @return The width of the raster.
 585:    */
 586:   public final int getWidth()
 587:   {
 588:     return width;
 589:   }
 590: 
 591:   /**
 592:    * Returns the height of the raster.
 593:    * 
 594:    * @return The height of the raster.
 595:    */
 596:   public final int getHeight()
 597:   {
 598:     return height;
 599:   }
 600: 
 601:   /**
 602:    * Returns the number of bands for this raster.
 603:    * 
 604:    * @return The number of bands.
 605:    */
 606:   public final int getNumBands()
 607:   {
 608:     return numBands;
 609:   }
 610:     
 611:   public final int getNumDataElements()
 612:   {
 613:     return numDataElements;
 614:   }
 615:   
 616:   /**
 617:    * Returns the transfer type for the raster (this is determined by the 
 618:    * raster's sample model).
 619:    * 
 620:    * @return The transfer type.
 621:    */
 622:   public final int getTransferType()
 623:   {
 624:     return sampleModel.getTransferType();
 625:   }
 626: 
 627:   /**
 628:    * Returns the data buffer that stores the pixel data for this raster.
 629:    * 
 630:    * @return The data buffer.
 631:    */
 632:   public DataBuffer getDataBuffer()
 633:   {
 634:     return dataBuffer;
 635:   }
 636: 
 637:   /**
 638:    * Returns the sample model that accesses the data buffer (to extract pixel
 639:    * data) for this raster.
 640:    * 
 641:    * @return The sample model.
 642:    */
 643:   public SampleModel getSampleModel()
 644:   {
 645:     return sampleModel;
 646:   }
 647: 
 648:   public Object getDataElements(int x, int y, Object outData)
 649:   {
 650:     return sampleModel.getDataElements(x - sampleModelTranslateX,
 651:         y - sampleModelTranslateY, outData, dataBuffer);
 652:   }
 653: 
 654:   public Object getDataElements(int x, int y, int w, int h, Object outData)
 655:   {
 656:     return sampleModel.getDataElements(x - sampleModelTranslateX,
 657:         y - sampleModelTranslateY, w, h, outData, dataBuffer);
 658:   }
 659: 
 660:   /**
 661:    * Returns an array containing the samples for the pixel at (x, y) in the
 662:    * raster.  If <code>iArray</code> is not <code>null</code>, it will be 
 663:    * populated with the sample values and returned as the result of
 664:    * this function (this avoids allocating a new array instance).
 665:    * 
 666:    * @param x  the x-coordinate of the pixel.
 667:    * @param y  the y-coordinate of the pixel.
 668:    * @param iArray  an array to populate with the sample values and return as 
 669:    *     the result (if <code>null</code>, a new array will be allocated).
 670:    * 
 671:    * @return The pixel sample values.
 672:    */
 673:   public int[] getPixel(int x, int y, int[] iArray)
 674:   {
 675:     return sampleModel.getPixel(x - sampleModelTranslateX,
 676:         y - sampleModelTranslateY, iArray, dataBuffer);
 677:   }
 678: 
 679:   /**
 680:    * Returns an array containing the samples for the pixel at (x, y) in the
 681:    * raster.  If <code>fArray</code> is not <code>null</code>, it will be 
 682:    * populated with the sample values and returned as the result of
 683:    * this function (this avoids allocating a new array instance).
 684:    * 
 685:    * @param x  the x-coordinate of the pixel.
 686:    * @param y  the y-coordinate of the pixel.
 687:    * @param fArray  an array to populate with the sample values and return as 
 688:    *     the result (if <code>null</code>, a new array will be allocated).
 689:    * 
 690:    * @return The pixel sample values.
 691:    */
 692:   public float[] getPixel(int x, int y, float[] fArray)
 693:   {
 694:     return sampleModel.getPixel(x - sampleModelTranslateX,
 695:         y - sampleModelTranslateY, fArray, dataBuffer);
 696:   }
 697: 
 698:   /**
 699:    * Returns an array containing the samples for the pixel at (x, y) in the
 700:    * raster.  If <code>dArray</code> is not <code>null</code>, it will be 
 701:    * populated with the sample values and returned as the result of
 702:    * this function (this avoids allocating a new array instance).
 703:    * 
 704:    * @param x  the x-coordinate of the pixel.
 705:    * @param y  the y-coordinate of the pixel.
 706:    * @param dArray  an array to populate with the sample values and return as 
 707:    *     the result (if <code>null</code>, a new array will be allocated).
 708:    * 
 709:    * @return The pixel sample values.
 710:    */
 711:   public double[] getPixel(int x, int y, double[] dArray)
 712:   {
 713:     return sampleModel.getPixel(x - sampleModelTranslateX,
 714:         y - sampleModelTranslateY, dArray, dataBuffer);
 715:   }
 716: 
 717:   /**
 718:    * Returns an array containing the samples for the pixels in the region 
 719:    * specified by (x, y, w, h) in the raster.  The array is ordered by pixels 
 720:    * (that is, all the samples for the first pixel are grouped together, 
 721:    * followed by all the samples for the second pixel, and so on).  
 722:    * If <code>iArray</code> is not <code>null</code>, it will be populated 
 723:    * with the sample values and returned as the result of this function (this 
 724:    * avoids allocating a new array instance).
 725:    * 
 726:    * @param x  the x-coordinate of the top-left pixel.
 727:    * @param y  the y-coordinate of the top-left pixel.
 728:    * @param w  the width of the region of pixels.
 729:    * @param h  the height of the region of pixels.
 730:    * @param iArray  an array to populate with the sample values and return as 
 731:    *     the result (if <code>null</code>, a new array will be allocated).
 732:    * 
 733:    * @return The pixel sample values.
 734:    */
 735:   public int[] getPixels(int x, int y, int w, int h, int[] iArray)
 736:   {
 737:     return sampleModel.getPixels(x - sampleModelTranslateX,
 738:         y - sampleModelTranslateY, w, h, iArray, dataBuffer);
 739:   }
 740: 
 741:   /**
 742:    * Returns an array containing the samples for the pixels in the region 
 743:    * specified by (x, y, w, h) in the raster.  The array is ordered by pixels 
 744:    * (that is, all the samples for the first pixel are grouped together, 
 745:    * followed by all the samples for the second pixel, and so on).  
 746:    * If <code>fArray</code> is not <code>null</code>, it will be populated 
 747:    * with the sample values and returned as the result of this function (this 
 748:    * avoids allocating a new array instance).
 749:    * 
 750:    * @param x  the x-coordinate of the top-left pixel.
 751:    * @param y  the y-coordinate of the top-left pixel.
 752:    * @param w  the width of the region of pixels.
 753:    * @param h  the height of the region of pixels.
 754:    * @param fArray  an array to populate with the sample values and return as 
 755:    *     the result (if <code>null</code>, a new array will be allocated).
 756:    * 
 757:    * @return The pixel sample values.
 758:    */
 759:   public float[] getPixels(int x, int y, int w, int h, float[] fArray)
 760:   {
 761:     return sampleModel.getPixels(x - sampleModelTranslateX,
 762:         y - sampleModelTranslateY, w, h, fArray, dataBuffer);
 763:   }
 764: 
 765:   /**
 766:    * Returns an array containing the samples for the pixels in the region 
 767:    * specified by (x, y, w, h) in the raster.  The array is ordered by pixels 
 768:    * (that is, all the samples for the first pixel are grouped together, 
 769:    * followed by all the samples for the second pixel, and so on).  
 770:    * If <code>dArray</code> is not <code>null</code>, it will be populated 
 771:    * with the sample values and returned as the result of this function (this 
 772:    * avoids allocating a new array instance).
 773:    * 
 774:    * @param x  the x-coordinate of the top-left pixel.
 775:    * @param y  the y-coordinate of the top-left pixel.
 776:    * @param w  the width of the region of pixels.
 777:    * @param h  the height of the region of pixels.
 778:    * @param dArray  an array to populate with the sample values and return as 
 779:    *     the result (if <code>null</code>, a new array will be allocated).
 780:    * 
 781:    * @return The pixel sample values.
 782:    */
 783:   public double[] getPixels(int x, int y, int w, int h, double[] dArray)
 784:   {
 785:     return sampleModel.getPixels(x - sampleModelTranslateX,
 786:         y - sampleModelTranslateY, w, h, dArray, dataBuffer);
 787:   }
 788: 
 789:   /**
 790:    * Returns the sample value for the pixel at (x, y) in the raster.
 791:    * 
 792:    * @param x  the x-coordinate of the pixel.
 793:    * @param y  the y-coordinate of the pixel.
 794:    * @param b  the band (in the range <code>0</code> to 
 795:    *     <code>getNumBands() - 1</code>).
 796:    * 
 797:    * @return The sample value.
 798:    */
 799:   public int getSample(int x, int y, int b)
 800:   {
 801:     return sampleModel.getSample(x - sampleModelTranslateX,
 802:         y - sampleModelTranslateY, b, dataBuffer);
 803:   }
 804: 
 805:   /**
 806:    * Returns the sample value for the pixel at (x, y) in the raster.
 807:    * 
 808:    * @param x  the x-coordinate of the pixel.
 809:    * @param y  the y-coordinate of the pixel.
 810:    * @param b  the band (in the range <code>0</code> to 
 811:    *     <code>getNumBands() - 1</code>).
 812:    * 
 813:    * @return The sample value.
 814:    * 
 815:    * @see #getSample(int, int, int)
 816:    */
 817:   public float getSampleFloat(int x, int y, int b)
 818:   {
 819:     return sampleModel.getSampleFloat(x - sampleModelTranslateX,
 820:         y - sampleModelTranslateY, b, dataBuffer);
 821:   }
 822: 
 823:   /**
 824:    * Returns the sample value for the pixel at (x, y) in the raster.
 825:    * 
 826:    * @param x  the x-coordinate of the pixel.
 827:    * @param y  the y-coordinate of the pixel.
 828:    * @param b  the band (in the range <code>0</code> to 
 829:    *     <code>getNumBands() - 1</code>).
 830:    * 
 831:    * @return The sample value.
 832:    * 
 833:    * @see #getSample(int, int, int)
 834:    */
 835:   public double getSampleDouble(int x, int y, int b)
 836:   {
 837:     return sampleModel.getSampleDouble(x - sampleModelTranslateX,
 838:         y - sampleModelTranslateY, b, dataBuffer);
 839:   }
 840: 
 841:   /**
 842:    * Returns an array containing the samples from one band for the pixels in 
 843:    * the region specified by (x, y, w, h) in the raster.  If 
 844:    * <code>iArray</code> is not <code>null</code>, it will be 
 845:    * populated with the sample values and returned as the result of this 
 846:    * function (this avoids allocating a new array instance).
 847:    * 
 848:    * @param x  the x-coordinate of the top-left pixel.
 849:    * @param y  the y-coordinate of the top-left pixel.
 850:    * @param w  the width of the region of pixels.
 851:    * @param h  the height of the region of pixels.
 852:    * @param b  the band (in the range <code>0</code> to 
 853:    *     </code>getNumBands() - 1</code>).
 854:    * @param iArray  an array to populate with the sample values and return as 
 855:    *     the result (if <code>null</code>, a new array will be allocated).
 856:    * 
 857:    * @return The sample values.
 858:    */
 859:   public int[] getSamples(int x, int y, int w, int h, int b,
 860:                           int[] iArray)
 861:   {
 862:     return sampleModel.getSamples(x - sampleModelTranslateX,
 863:         y - sampleModelTranslateY, w, h, b, iArray, dataBuffer);
 864:   }
 865: 
 866:   /**
 867:    * Returns an array containing the samples from one band for the pixels in 
 868:    * the region specified by (x, y, w, h) in the raster.  If 
 869:    * <code>fArray</code> is not <code>null</code>, it will be 
 870:    * populated with the sample values and returned as the result of this 
 871:    * function (this avoids allocating a new array instance).
 872:    *
 873:    * @param x  the x-coordinate of the top-left pixel.
 874:    * @param y  the y-coordinate of the top-left pixel.
 875:    * @param w  the width of the region of pixels.
 876:    * @param h  the height of the region of pixels.
 877:    * @param b  the band (in the range <code>0</code> to 
 878:    *     </code>getNumBands() - 1</code>).
 879:    * @param fArray  an array to populate with the sample values and return as 
 880:    *     the result (if <code>null</code>, a new array will be allocated).
 881:    * 
 882:    * @return The sample values.
 883:    */  
 884:   public float[] getSamples(int x, int y, int w, int h, int b, float[] fArray)
 885:   {
 886:     return sampleModel.getSamples(x - sampleModelTranslateX,
 887:         y - sampleModelTranslateY, w, h, b, fArray, dataBuffer);
 888:   }
 889: 
 890:   /**
 891:    * Returns an array containing the samples from one band for the pixels in 
 892:    * the region specified by (x, y, w, h) in the raster.  If 
 893:    * <code>dArray</code> is not <code>null</code>, it will be 
 894:    * populated with the sample values and returned as the result of this 
 895:    * function (this avoids allocating a new array instance).
 896:    * 
 897:    * @param x  the x-coordinate of the top-left pixel.
 898:    * @param y  the y-coordinate of the top-left pixel.
 899:    * @param w  the width of the region of pixels.
 900:    * @param h  the height of the region of pixels.
 901:    * @param b  the band (in the range <code>0</code> to 
 902:    *     </code>getNumBands() - 1</code>).
 903:    * @param dArray  an array to populate with the sample values and return as 
 904:    *     the result (if <code>null</code>, a new array will be allocated).
 905:    * 
 906:    * @return The sample values.
 907:    */
 908:   public double[] getSamples(int x, int y, int w, int h, int b, 
 909:                              double[] dArray)
 910:   {
 911:     return sampleModel.getSamples(x - sampleModelTranslateX,
 912:         y - sampleModelTranslateY, w, h, b, dArray, dataBuffer);
 913:   }
 914:   
 915:   /**
 916:    * Create a String representing the state of this Raster.
 917:    * 
 918:    * @return A String representing the stat of this Raster.
 919:    */
 920:   public String toString()
 921:   {
 922:     StringBuffer result = new StringBuffer();
 923:     
 924:     result.append(getClass().getName());
 925:     result.append("[(");
 926:     result.append(minX).append(",").append(minY).append("), ");
 927:     result.append(width).append(" x ").append(height).append(",");
 928:     result.append(sampleModel).append(",");
 929:     result.append(dataBuffer);
 930:     result.append("]");
 931:     
 932:     return result.toString();
 933:   }
 934: 
 935:   /**
 936:    * Returns the number of bits used to represent the specified data type.  
 937:    * Valid types are:
 938:    * <ul>
 939:    *   <li>{@link DataBuffer#TYPE_BYTE};</li>
 940:    *   <li>{@link DataBuffer#TYPE_USHORT};</li>
 941:    *   <li>{@link DataBuffer#TYPE_SHORT};</li>
 942:    *   <li>{@link DataBuffer#TYPE_INT};</li>
 943:    *   <li>{@link DataBuffer#TYPE_FLOAT};</li>
 944:    *   <li>{@link DataBuffer#TYPE_DOUBLE};</li>
 945:    * </ul>
 946:    * This method returns 0 for invalid data types.
 947:    * 
 948:    * @param dataType  the data type.
 949:    * 
 950:    * @return The number of bits used to represent the specified data type.
 951:    */
 952:   private static int getTypeBits(int dataType)
 953:   {
 954:     switch (dataType)
 955:       {
 956:       case DataBuffer.TYPE_BYTE:
 957:         return 8;
 958:       case DataBuffer.TYPE_USHORT:
 959:       case DataBuffer.TYPE_SHORT:
 960:         return 16;
 961:       case DataBuffer.TYPE_INT:
 962:       case DataBuffer.TYPE_FLOAT:
 963:         return 32;
 964:       case DataBuffer.TYPE_DOUBLE:
 965:         return 64;
 966:       default:
 967:         return 0;
 968:       }
 969:   }
 970: }