1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* **************************************************************** 27 ****************************************************************** 28 ****************************************************************** 29 *** COPYRIGHT (c) Eastman Kodak Company, 1997 30 *** As an unpublished work pursuant to Title 17 of the United 31 *** States Code. All rights reserved. 32 ****************************************************************** 33 ****************************************************************** 34 ******************************************************************/ 35 36 37 package java.awt.image; 38 import java.awt.Rectangle; 39 import java.awt.Point; 40 41 import sun.awt.image.ByteInterleavedRaster; 42 import sun.awt.image.ShortInterleavedRaster; 43 import sun.awt.image.IntegerInterleavedRaster; 44 import sun.awt.image.ByteBandedRaster; 45 import sun.awt.image.ShortBandedRaster; 46 import sun.awt.image.BytePackedRaster; 47 import sun.awt.image.SunWritableRaster; 48 49 /** 50 * A class representing a rectangular array of pixels. A Raster 51 * encapsulates a DataBuffer that stores the sample values and a 52 * SampleModel that describes how to locate a given sample value in a 53 * DataBuffer. 54 * <p> 55 * A Raster defines values for pixels occupying a particular 56 * rectangular area of the plane, not necessarily including (0, 0). 57 * The rectangle, known as the Raster's bounding rectangle and 58 * available by means of the getBounds method, is defined by minX, 59 * minY, width, and height values. The minX and minY values define 60 * the coordinate of the upper left corner of the Raster. References 61 * to pixels outside of the bounding rectangle may result in an 62 * exception being thrown, or may result in references to unintended 63 * elements of the Raster's associated DataBuffer. It is the user's 64 * responsibility to avoid accessing such pixels. 65 * <p> 66 * A SampleModel describes how samples of a Raster 67 * are stored in the primitive array elements of a DataBuffer. 68 * Samples may be stored one per data element, as in a 69 * PixelInterleavedSampleModel or BandedSampleModel, or packed several to 70 * an element, as in a SinglePixelPackedSampleModel or 71 * MultiPixelPackedSampleModel. The SampleModel is also 72 * controls whether samples are sign extended, allowing unsigned 73 * data to be stored in signed Java data types such as byte, short, and 74 * int. 75 * <p> 76 * Although a Raster may live anywhere in the plane, a SampleModel 77 * makes use of a simple coordinate system that starts at (0, 0). A 78 * Raster therefore contains a translation factor that allows pixel 79 * locations to be mapped between the Raster's coordinate system and 80 * that of the SampleModel. The translation from the SampleModel 81 * coordinate system to that of the Raster may be obtained by the 82 * getSampleModelTranslateX and getSampleModelTranslateY methods. 83 * <p> 84 * A Raster may share a DataBuffer with another Raster either by 85 * explicit construction or by the use of the createChild and 86 * createTranslatedChild methods. Rasters created by these methods 87 * can return a reference to the Raster they were created from by 88 * means of the getParent method. For a Raster that was not 89 * constructed by means of a call to createTranslatedChild or 90 * createChild, getParent will return null. 91 * <p> 92 * The createTranslatedChild method returns a new Raster that 93 * shares all of the data of the current Raster, but occupies a 94 * bounding rectangle of the same width and height but with a 95 * different starting point. For example, if the parent Raster 96 * occupied the region (10, 10) to (100, 100), and the translated 97 * Raster was defined to start at (50, 50), then pixel (20, 20) of the 98 * parent and pixel (60, 60) of the child occupy the same location in 99 * the DataBuffer shared by the two Rasters. In the first case, (-10, 100 * -10) should be added to a pixel coordinate to obtain the 101 * corresponding SampleModel coordinate, and in the second case (-50, 102 * -50) should be added. 103 * <p> 104 * The translation between a parent and child Raster may be 105 * determined by subtracting the child's sampleModelTranslateX and 106 * sampleModelTranslateY values from those of the parent. 107 * <p> 108 * The createChild method may be used to create a new Raster 109 * occupying only a subset of its parent's bounding rectangle 110 * (with the same or a translated coordinate system) or 111 * with a subset of the bands of its parent. 112 * <p> 113 * All constructors are protected. The correct way to create a 114 * Raster is to use one of the static create methods defined in this 115 * class. These methods create instances of Raster that use the 116 * standard Interleaved, Banded, and Packed SampleModels and that may 117 * be processed more efficiently than a Raster created by combining 118 * an externally generated SampleModel and DataBuffer. 119 * @see java.awt.image.DataBuffer 120 * @see java.awt.image.SampleModel 121 * @see java.awt.image.PixelInterleavedSampleModel 122 * @see java.awt.image.BandedSampleModel 123 * @see java.awt.image.SinglePixelPackedSampleModel 124 * @see java.awt.image.MultiPixelPackedSampleModel 125 */ 126 public class Raster { 127 128 /** 129 * The SampleModel that describes how pixels from this Raster 130 * are stored in the DataBuffer. 131 */ 132 protected SampleModel sampleModel; 133 134 /** The DataBuffer that stores the image data. */ 135 protected DataBuffer dataBuffer; 136 137 /** The X coordinate of the upper-left pixel of this Raster. */ 138 protected int minX; 139 140 /** The Y coordinate of the upper-left pixel of this Raster. */ 141 protected int minY; 142 143 /** The width of this Raster. */ 144 protected int width; 145 146 /** The height of this Raster. */ 147 protected int height; 148 149 /** 150 * The X translation from the coordinate space of the 151 * Raster's SampleModel to that of the Raster. 152 */ 153 protected int sampleModelTranslateX; 154 155 /** 156 * The Y translation from the coordinate space of the 157 * Raster's SampleModel to that of the Raster. 158 */ 159 protected int sampleModelTranslateY; 160 161 /** The number of bands in the Raster. */ 162 protected int numBands; 163 164 /** The number of DataBuffer data elements per pixel. */ 165 protected int numDataElements; 166 167 /** The parent of this Raster, or null. */ 168 protected Raster parent; 169 170 private static native void initIDs(); 171 static { 172 ColorModel.loadLibraries(); 173 initIDs(); 174 } 175 176 /** 177 * Creates a Raster based on a PixelInterleavedSampleModel with the 178 * specified data type, width, height, and number of bands. 179 * 180 * <p> The upper left corner of the Raster is given by the 181 * location argument. If location is null, (0, 0) will be used. 182 * The dataType parameter should be one of the enumerated values 183 * defined in the DataBuffer class. 184 * 185 * <p> Note that interleaved {@code DataBuffer.TYPE_INT} 186 * Rasters are not supported. To create a 1-band Raster of type 187 * {@code DataBuffer.TYPE_INT}, use 188 * Raster.createPackedRaster(). 189 * <p> The only dataTypes supported currently are TYPE_BYTE 190 * and TYPE_USHORT. 191 * @param dataType the data type for storing samples 192 * @param w the width in pixels of the image data 193 * @param h the height in pixels of the image data 194 * @param bands the number of bands 195 * @param location the upper-left corner of the {@code Raster} 196 * @return a WritableRaster object with the specified data type, 197 * width, height and number of bands. 198 * @throws RasterFormatException if {@code w} or {@code h} 199 * is less than or equal to zero, or computing either 200 * {@code location.x + w} or 201 * {@code location.y + h} results in integer 202 * overflow 203 */ 204 public static WritableRaster createInterleavedRaster(int dataType, 205 int w, int h, 206 int bands, 207 Point location) { 208 int[] bandOffsets = new int[bands]; 209 for (int i = 0; i < bands; i++) { 210 bandOffsets[i] = i; 211 } 212 return createInterleavedRaster(dataType, w, h, w*bands, bands, 213 bandOffsets, location); 214 } 215 216 /** 217 * Creates a Raster based on a PixelInterleavedSampleModel with the 218 * specified data type, width, height, scanline stride, pixel 219 * stride, and band offsets. The number of bands is inferred from 220 * bandOffsets.length. 221 * 222 * <p> The upper left corner of the Raster is given by the 223 * location argument. If location is null, (0, 0) will be used. 224 * The dataType parameter should be one of the enumerated values 225 * defined in the DataBuffer class. 226 * 227 * <p> Note that interleaved {@code DataBuffer.TYPE_INT} 228 * Rasters are not supported. To create a 1-band Raster of type 229 * {@code DataBuffer.TYPE_INT}, use 230 * Raster.createPackedRaster(). 231 * <p> The only dataTypes supported currently are TYPE_BYTE 232 * and TYPE_USHORT. 233 * @param dataType the data type for storing samples 234 * @param w the width in pixels of the image data 235 * @param h the height in pixels of the image data 236 * @param scanlineStride the line stride of the image data 237 * @param pixelStride the pixel stride of the image data 238 * @param bandOffsets the offsets of all bands 239 * @param location the upper-left corner of the {@code Raster} 240 * @return a WritableRaster object with the specified data type, 241 * width, height, scanline stride, pixel stride and band 242 * offsets. 243 * @throws RasterFormatException if {@code w} or {@code h} 244 * is less than or equal to zero, or computing either 245 * {@code location.x + w} or 246 * {@code location.y + h} results in integer 247 * overflow 248 * @throws IllegalArgumentException if {@code dataType} is not 249 * one of the supported data types, which are 250 * {@code DataBuffer.TYPE_BYTE}, or 251 * {@code DataBuffer.TYPE_USHORT}. 252 */ 253 public static WritableRaster createInterleavedRaster(int dataType, 254 int w, int h, 255 int scanlineStride, 256 int pixelStride, 257 int bandOffsets[], 258 Point location) { 259 DataBuffer d; 260 261 int size = scanlineStride * (h - 1) + // fisrt (h - 1) scans 262 pixelStride * w; // last scan 263 264 switch(dataType) { 265 case DataBuffer.TYPE_BYTE: 266 d = new DataBufferByte(size); 267 break; 268 269 case DataBuffer.TYPE_USHORT: 270 d = new DataBufferUShort(size); 271 break; 272 273 default: 274 throw new IllegalArgumentException("Unsupported data type " + 275 dataType); 276 } 277 278 return createInterleavedRaster(d, w, h, scanlineStride, 279 pixelStride, bandOffsets, location); 280 } 281 282 /** 283 * Creates a Raster based on a BandedSampleModel with the 284 * specified data type, width, height, and number of bands. 285 * 286 * <p> The upper left corner of the Raster is given by the 287 * location argument. If location is null, (0, 0) will be used. 288 * The dataType parameter should be one of the enumerated values 289 * defined in the DataBuffer class. 290 * 291 * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT, 292 * and TYPE_INT. 293 * @param dataType the data type for storing samples 294 * @param w the width in pixels of the image data 295 * @param h the height in pixels of the image data 296 * @param bands the number of bands 297 * @param location the upper-left corner of the {@code Raster} 298 * @return a WritableRaster object with the specified data type, 299 * width, height and number of bands. 300 * @throws RasterFormatException if {@code w} or {@code h} 301 * is less than or equal to zero, or computing either 302 * {@code location.x + w} or 303 * {@code location.y + h} results in integer 304 * overflow 305 * @throws ArrayIndexOutOfBoundsException if {@code bands} 306 * is less than 1 307 */ 308 public static WritableRaster createBandedRaster(int dataType, 309 int w, int h, 310 int bands, 311 Point location) { 312 if (bands < 1) { 313 throw new ArrayIndexOutOfBoundsException("Number of bands ("+ 314 bands+") must"+ 315 " be greater than 0"); 316 } 317 int[] bankIndices = new int[bands]; 318 int[] bandOffsets = new int[bands]; 319 for (int i = 0; i < bands; i++) { 320 bankIndices[i] = i; 321 bandOffsets[i] = 0; 322 } 323 324 return createBandedRaster(dataType, w, h, w, 325 bankIndices, bandOffsets, 326 location); 327 } 328 329 /** 330 * Creates a Raster based on a BandedSampleModel with the 331 * specified data type, width, height, scanline stride, bank 332 * indices and band offsets. The number of bands is inferred from 333 * bankIndices.length and bandOffsets.length, which must be the 334 * same. 335 * 336 * <p> The upper left corner of the Raster is given by the 337 * location argument. The dataType parameter should be one of the 338 * enumerated values defined in the DataBuffer class. 339 * 340 * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT, 341 * and TYPE_INT. 342 * @param dataType the data type for storing samples 343 * @param w the width in pixels of the image data 344 * @param h the height in pixels of the image data 345 * @param scanlineStride the line stride of the image data 346 * @param bankIndices the bank indices for each band 347 * @param bandOffsets the offsets of all bands 348 * @param location the upper-left corner of the {@code Raster} 349 * @return a WritableRaster object with the specified data type, 350 * width, height, scanline stride, bank indices and band 351 * offsets. 352 * @throws RasterFormatException if {@code w} or {@code h} 353 * is less than or equal to zero, or computing either 354 * {@code location.x + w} or 355 * {@code location.y + h} results in integer 356 * overflow 357 * @throws IllegalArgumentException if {@code dataType} is not 358 * one of the supported data types, which are 359 * {@code DataBuffer.TYPE_BYTE}, 360 * {@code DataBuffer.TYPE_USHORT} 361 * or {@code DataBuffer.TYPE_INT} 362 * @throws ArrayIndexOutOfBoundsException if {@code bankIndices} 363 * or {@code bandOffsets} is {@code null} 364 */ 365 public static WritableRaster createBandedRaster(int dataType, 366 int w, int h, 367 int scanlineStride, 368 int bankIndices[], 369 int bandOffsets[], 370 Point location) { 371 DataBuffer d; 372 int bands = bandOffsets.length; 373 374 if (bankIndices == null) { 375 throw new 376 ArrayIndexOutOfBoundsException("Bank indices array is null"); 377 } 378 if (bandOffsets == null) { 379 throw new 380 ArrayIndexOutOfBoundsException("Band offsets array is null"); 381 } 382 383 // Figure out the #banks and the largest band offset 384 int maxBank = bankIndices[0]; 385 int maxBandOff = bandOffsets[0]; 386 for (int i = 1; i < bands; i++) { 387 if (bankIndices[i] > maxBank) { 388 maxBank = bankIndices[i]; 389 } 390 if (bandOffsets[i] > maxBandOff) { 391 maxBandOff = bandOffsets[i]; 392 } 393 } 394 int banks = maxBank + 1; 395 int size = maxBandOff + 396 scanlineStride * (h - 1) + // first (h - 1) scans 397 w; // last scan 398 399 switch(dataType) { 400 case DataBuffer.TYPE_BYTE: 401 d = new DataBufferByte(size, banks); 402 break; 403 404 case DataBuffer.TYPE_USHORT: 405 d = new DataBufferUShort(size, banks); 406 break; 407 408 case DataBuffer.TYPE_INT: 409 d = new DataBufferInt(size, banks); 410 break; 411 412 default: 413 throw new IllegalArgumentException("Unsupported data type " + 414 dataType); 415 } 416 417 return createBandedRaster(d, w, h, scanlineStride, 418 bankIndices, bandOffsets, location); 419 } 420 421 /** 422 * Creates a Raster based on a SinglePixelPackedSampleModel with 423 * the specified data type, width, height, and band masks. 424 * The number of bands is inferred from bandMasks.length. 425 * 426 * <p> The upper left corner of the Raster is given by the 427 * location argument. If location is null, (0, 0) will be used. 428 * The dataType parameter should be one of the enumerated values 429 * defined in the DataBuffer class. 430 * 431 * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT, 432 * and TYPE_INT. 433 * @param dataType the data type for storing samples 434 * @param w the width in pixels of the image data 435 * @param h the height in pixels of the image data 436 * @param bandMasks an array containing an entry for each band 437 * @param location the upper-left corner of the {@code Raster} 438 * @return a WritableRaster object with the specified data type, 439 * width, height, and band masks. 440 * @throws RasterFormatException if {@code w} or {@code h} 441 * is less than or equal to zero, or computing either 442 * {@code location.x + w} or 443 * {@code location.y + h} results in integer 444 * overflow 445 * @throws IllegalArgumentException if {@code dataType} is not 446 * one of the supported data types, which are 447 * {@code DataBuffer.TYPE_BYTE}, 448 * {@code DataBuffer.TYPE_USHORT} 449 * or {@code DataBuffer.TYPE_INT} 450 */ 451 public static WritableRaster createPackedRaster(int dataType, 452 int w, int h, 453 int bandMasks[], 454 Point location) { 455 DataBuffer d; 456 457 switch(dataType) { 458 case DataBuffer.TYPE_BYTE: 459 d = new DataBufferByte(w*h); 460 break; 461 462 case DataBuffer.TYPE_USHORT: 463 d = new DataBufferUShort(w*h); 464 break; 465 466 case DataBuffer.TYPE_INT: 467 d = new DataBufferInt(w*h); 468 break; 469 470 default: 471 throw new IllegalArgumentException("Unsupported data type " + 472 dataType); 473 } 474 475 return createPackedRaster(d, w, h, w, bandMasks, location); 476 } 477 478 /** 479 * Creates a Raster based on a packed SampleModel with the 480 * specified data type, width, height, number of bands, and bits 481 * per band. If the number of bands is one, the SampleModel will 482 * be a MultiPixelPackedSampleModel. 483 * 484 * <p> If the number of bands is more than one, the SampleModel 485 * will be a SinglePixelPackedSampleModel, with each band having 486 * bitsPerBand bits. In either case, the requirements on dataType 487 * and bitsPerBand imposed by the corresponding SampleModel must 488 * be met. 489 * 490 * <p> The upper left corner of the Raster is given by the 491 * location argument. If location is null, (0, 0) will be used. 492 * The dataType parameter should be one of the enumerated values 493 * defined in the DataBuffer class. 494 * 495 * <p> The only dataTypes supported currently are TYPE_BYTE, TYPE_USHORT, 496 * and TYPE_INT. 497 * @param dataType the data type for storing samples 498 * @param w the width in pixels of the image data 499 * @param h the height in pixels of the image data 500 * @param bands the number of bands 501 * @param bitsPerBand the number of bits per band 502 * @param location the upper-left corner of the {@code Raster} 503 * @return a WritableRaster object with the specified data type, 504 * width, height, number of bands, and bits per band. 505 * @throws RasterFormatException if {@code w} or {@code h} 506 * is less than or equal to zero, or computing either 507 * {@code location.x + w} or 508 * {@code location.y + h} results in integer 509 * overflow 510 * @throws IllegalArgumentException if the product of 511 * {@code bitsPerBand} and {@code bands} is 512 * greater than the number of bits held by 513 * {@code dataType} 514 * @throws IllegalArgumentException if {@code bitsPerBand} or 515 * {@code bands} is not greater than zero 516 * @throws IllegalArgumentException if {@code dataType} is not 517 * one of the supported data types, which are 518 * {@code DataBuffer.TYPE_BYTE}, 519 * {@code DataBuffer.TYPE_USHORT} 520 * or {@code DataBuffer.TYPE_INT} 521 */ 522 public static WritableRaster createPackedRaster(int dataType, 523 int w, int h, 524 int bands, 525 int bitsPerBand, 526 Point location) { 527 DataBuffer d; 528 529 if (bands <= 0) { 530 throw new IllegalArgumentException("Number of bands ("+bands+ 531 ") must be greater than 0"); 532 } 533 534 if (bitsPerBand <= 0) { 535 throw new IllegalArgumentException("Bits per band ("+bitsPerBand+ 536 ") must be greater than 0"); 537 } 538 539 if (bands != 1) { 540 int[] masks = new int[bands]; 541 int mask = (1 << bitsPerBand) - 1; 542 int shift = (bands-1)*bitsPerBand; 543 544 /* Make sure the total mask size will fit in the data type */ 545 if (shift+bitsPerBand > DataBuffer.getDataTypeSize(dataType)) { 546 throw new IllegalArgumentException("bitsPerBand("+ 547 bitsPerBand+") * bands is "+ 548 " greater than data type "+ 549 "size."); 550 } 551 switch(dataType) { 552 case DataBuffer.TYPE_BYTE: 553 case DataBuffer.TYPE_USHORT: 554 case DataBuffer.TYPE_INT: 555 break; 556 default: 557 throw new IllegalArgumentException("Unsupported data type " + 558 dataType); 559 } 560 561 for (int i = 0; i < bands; i++) { 562 masks[i] = mask << shift; 563 shift = shift - bitsPerBand; 564 } 565 566 return createPackedRaster(dataType, w, h, masks, location); 567 } 568 else { 569 double fw = w; 570 switch(dataType) { 571 case DataBuffer.TYPE_BYTE: 572 d = new DataBufferByte((int)(Math.ceil(fw/(8/bitsPerBand)))*h); 573 break; 574 575 case DataBuffer.TYPE_USHORT: 576 d = new DataBufferUShort((int)(Math.ceil(fw/(16/bitsPerBand)))*h); 577 break; 578 579 case DataBuffer.TYPE_INT: 580 d = new DataBufferInt((int)(Math.ceil(fw/(32/bitsPerBand)))*h); 581 break; 582 583 default: 584 throw new IllegalArgumentException("Unsupported data type " + 585 dataType); 586 } 587 588 return createPackedRaster(d, w, h, bitsPerBand, location); 589 } 590 } 591 592 /** 593 * Creates a Raster based on a PixelInterleavedSampleModel with the 594 * specified DataBuffer, width, height, scanline stride, pixel 595 * stride, and band offsets. The number of bands is inferred from 596 * bandOffsets.length. The upper left corner of the Raster 597 * is given by the location argument. If location is null, (0, 0) 598 * will be used. 599 * <p> Note that interleaved {@code DataBuffer.TYPE_INT} 600 * Rasters are not supported. To create a 1-band Raster of type 601 * {@code DataBuffer.TYPE_INT}, use 602 * Raster.createPackedRaster(). 603 * @param dataBuffer the {@code DataBuffer} that contains the 604 * image data 605 * @param w the width in pixels of the image data 606 * @param h the height in pixels of the image data 607 * @param scanlineStride the line stride of the image data 608 * @param pixelStride the pixel stride of the image data 609 * @param bandOffsets the offsets of all bands 610 * @param location the upper-left corner of the {@code Raster} 611 * @return a WritableRaster object with the specified 612 * {@code DataBuffer}, width, height, scanline stride, 613 * pixel stride and band offsets. 614 * @throws RasterFormatException if {@code w} or {@code h} 615 * is less than or equal to zero, or computing either 616 * {@code location.x + w} or 617 * {@code location.y + h} results in integer 618 * overflow 619 * @throws IllegalArgumentException if {@code dataType} is not 620 * one of the supported data types, which are 621 * {@code DataBuffer.TYPE_BYTE}, 622 * {@code DataBuffer.TYPE_USHORT} 623 * @throws RasterFormatException if {@code dataBuffer} has more 624 * than one bank. 625 * @throws NullPointerException if {@code dataBuffer} is null 626 */ 627 public static WritableRaster createInterleavedRaster(DataBuffer dataBuffer, 628 int w, int h, 629 int scanlineStride, 630 int pixelStride, 631 int bandOffsets[], 632 Point location) { 633 if (dataBuffer == null) { 634 throw new NullPointerException("DataBuffer cannot be null"); 635 } 636 if (location == null) { 637 location = new Point(0, 0); 638 } 639 int dataType = dataBuffer.getDataType(); 640 641 PixelInterleavedSampleModel csm = 642 new PixelInterleavedSampleModel(dataType, w, h, 643 pixelStride, 644 scanlineStride, 645 bandOffsets); 646 switch(dataType) { 647 case DataBuffer.TYPE_BYTE: 648 return new ByteInterleavedRaster(csm, dataBuffer, location); 649 650 case DataBuffer.TYPE_USHORT: 651 return new ShortInterleavedRaster(csm, dataBuffer, location); 652 653 default: 654 throw new IllegalArgumentException("Unsupported data type " + 655 dataType); 656 } 657 } 658 659 /** 660 * Creates a Raster based on a BandedSampleModel with the 661 * specified DataBuffer, width, height, scanline stride, bank 662 * indices, and band offsets. The number of bands is inferred 663 * from bankIndices.length and bandOffsets.length, which must be 664 * the same. The upper left corner of the Raster is given by the 665 * location argument. If location is null, (0, 0) will be used. 666 * @param dataBuffer the {@code DataBuffer} that contains the 667 * image data 668 * @param w the width in pixels of the image data 669 * @param h the height in pixels of the image data 670 * @param scanlineStride the line stride of the image data 671 * @param bankIndices the bank indices for each band 672 * @param bandOffsets the offsets of all bands 673 * @param location the upper-left corner of the {@code Raster} 674 * @return a WritableRaster object with the specified 675 * {@code DataBuffer}, width, height, scanline stride, 676 * bank indices and band offsets. 677 * @throws RasterFormatException if {@code w} or {@code h} 678 * is less than or equal to zero, or computing either 679 * {@code location.x + w} or 680 * {@code location.y + h} results in integer 681 * overflow 682 * @throws IllegalArgumentException if {@code dataType} is not 683 * one of the supported data types, which are 684 * {@code DataBuffer.TYPE_BYTE}, 685 * {@code DataBuffer.TYPE_USHORT} 686 * or {@code DataBuffer.TYPE_INT} 687 * @throws NullPointerException if {@code dataBuffer} is null 688 */ 689 public static WritableRaster createBandedRaster(DataBuffer dataBuffer, 690 int w, int h, 691 int scanlineStride, 692 int bankIndices[], 693 int bandOffsets[], 694 Point location) { 695 if (dataBuffer == null) { 696 throw new NullPointerException("DataBuffer cannot be null"); 697 } 698 if (location == null) { 699 location = new Point(0,0); 700 } 701 int dataType = dataBuffer.getDataType(); 702 703 int bands = bankIndices.length; 704 if (bandOffsets.length != bands) { 705 throw new IllegalArgumentException( 706 "bankIndices.length != bandOffsets.length"); 707 } 708 709 BandedSampleModel bsm = 710 new BandedSampleModel(dataType, w, h, 711 scanlineStride, 712 bankIndices, bandOffsets); 713 714 switch(dataType) { 715 case DataBuffer.TYPE_BYTE: 716 return new ByteBandedRaster(bsm, dataBuffer, location); 717 718 case DataBuffer.TYPE_USHORT: 719 return new ShortBandedRaster(bsm, dataBuffer, location); 720 721 case DataBuffer.TYPE_INT: 722 return new SunWritableRaster(bsm, dataBuffer, location); 723 724 default: 725 throw new IllegalArgumentException("Unsupported data type " + 726 dataType); 727 } 728 } 729 730 /** 731 * Creates a Raster based on a SinglePixelPackedSampleModel with 732 * the specified DataBuffer, width, height, scanline stride, and 733 * band masks. The number of bands is inferred from bandMasks.length. 734 * The upper left corner of the Raster is given by 735 * the location argument. If location is null, (0, 0) will be used. 736 * @param dataBuffer the {@code DataBuffer} that contains the 737 * image data 738 * @param w the width in pixels of the image data 739 * @param h the height in pixels of the image data 740 * @param scanlineStride the line stride of the image data 741 * @param bandMasks an array containing an entry for each band 742 * @param location the upper-left corner of the {@code Raster} 743 * @return a WritableRaster object with the specified 744 * {@code DataBuffer}, width, height, scanline stride, 745 * and band masks. 746 * @throws RasterFormatException if {@code w} or {@code h} 747 * is less than or equal to zero, or computing either 748 * {@code location.x + w} or 749 * {@code location.y + h} results in integer 750 * overflow 751 * @throws IllegalArgumentException if {@code dataType} is not 752 * one of the supported data types, which are 753 * {@code DataBuffer.TYPE_BYTE}, 754 * {@code DataBuffer.TYPE_USHORT} 755 * or {@code DataBuffer.TYPE_INT} 756 * @throws RasterFormatException if {@code dataBuffer} has more 757 * than one bank. 758 * @throws NullPointerException if {@code dataBuffer} is null 759 */ 760 public static WritableRaster createPackedRaster(DataBuffer dataBuffer, 761 int w, int h, 762 int scanlineStride, 763 int bandMasks[], 764 Point location) { 765 if (dataBuffer == null) { 766 throw new NullPointerException("DataBuffer cannot be null"); 767 } 768 if (location == null) { 769 location = new Point(0,0); 770 } 771 int dataType = dataBuffer.getDataType(); 772 773 SinglePixelPackedSampleModel sppsm = 774 new SinglePixelPackedSampleModel(dataType, w, h, scanlineStride, 775 bandMasks); 776 777 switch(dataType) { 778 case DataBuffer.TYPE_BYTE: 779 return new ByteInterleavedRaster(sppsm, dataBuffer, location); 780 781 case DataBuffer.TYPE_USHORT: 782 return new ShortInterleavedRaster(sppsm, dataBuffer, location); 783 784 case DataBuffer.TYPE_INT: 785 return new IntegerInterleavedRaster(sppsm, dataBuffer, location); 786 787 default: 788 throw new IllegalArgumentException("Unsupported data type " + 789 dataType); 790 } 791 } 792 793 /** 794 * Creates a Raster based on a MultiPixelPackedSampleModel with the 795 * specified DataBuffer, width, height, and bits per pixel. The upper 796 * left corner of the Raster is given by the location argument. If 797 * location is null, (0, 0) will be used. 798 * @param dataBuffer the {@code DataBuffer} that contains the 799 * image data 800 * @param w the width in pixels of the image data 801 * @param h the height in pixels of the image data 802 * @param bitsPerPixel the number of bits for each pixel 803 * @param location the upper-left corner of the {@code Raster} 804 * @return a WritableRaster object with the specified 805 * {@code DataBuffer}, width, height, and 806 * bits per pixel. 807 * @throws RasterFormatException if {@code w} or {@code h} 808 * is less than or equal to zero, or computing either 809 * {@code location.x + w} or 810 * {@code location.y + h} results in integer 811 * overflow 812 * @throws IllegalArgumentException if {@code dataType} is not 813 * one of the supported data types, which are 814 * {@code DataBuffer.TYPE_BYTE}, 815 * {@code DataBuffer.TYPE_USHORT} 816 * or {@code DataBuffer.TYPE_INT} 817 * @throws RasterFormatException if {@code dataBuffer} has more 818 * than one bank. 819 * @throws NullPointerException if {@code dataBuffer} is null 820 */ 821 public static WritableRaster createPackedRaster(DataBuffer dataBuffer, 822 int w, int h, 823 int bitsPerPixel, 824 Point location) { 825 if (dataBuffer == null) { 826 throw new NullPointerException("DataBuffer cannot be null"); 827 } 828 if (location == null) { 829 location = new Point(0,0); 830 } 831 int dataType = dataBuffer.getDataType(); 832 833 if (dataType != DataBuffer.TYPE_BYTE && 834 dataType != DataBuffer.TYPE_USHORT && 835 dataType != DataBuffer.TYPE_INT) { 836 throw new IllegalArgumentException("Unsupported data type " + 837 dataType); 838 } 839 840 if (dataBuffer.getNumBanks() != 1) { 841 throw new 842 RasterFormatException("DataBuffer for packed Rasters"+ 843 " must only have 1 bank."); 844 } 845 846 MultiPixelPackedSampleModel mppsm = 847 new MultiPixelPackedSampleModel(dataType, w, h, bitsPerPixel); 848 849 if (dataType == DataBuffer.TYPE_BYTE && 850 (bitsPerPixel == 1 || bitsPerPixel == 2 || bitsPerPixel == 4)) { 851 return new BytePackedRaster(mppsm, dataBuffer, location); 852 } else { 853 return new SunWritableRaster(mppsm, dataBuffer, location); 854 } 855 } 856 857 858 /** 859 * Creates a Raster with the specified SampleModel and DataBuffer. 860 * The upper left corner of the Raster is given by the location argument. 861 * If location is null, (0, 0) will be used. 862 * @param sm the specified {@code SampleModel} 863 * @param db the specified {@code DataBuffer} 864 * @param location the upper-left corner of the {@code Raster} 865 * @return a {@code Raster} with the specified 866 * {@code SampleModel}, {@code DataBuffer}, and 867 * location. 868 * @throws RasterFormatException if computing either 869 * {@code location.x + sm.getWidth()} or 870 * {@code location.y + sm.getHeight()} results in integer 871 * overflow 872 * @throws RasterFormatException if {@code db} has more 873 * than one bank and {@code sm} is a 874 * PixelInterleavedSampleModel, SinglePixelPackedSampleModel, 875 * or MultiPixelPackedSampleModel. 876 * @throws NullPointerException if either SampleModel or DataBuffer is 877 * null 878 */ 879 public static Raster createRaster(SampleModel sm, 880 DataBuffer db, 881 Point location) { 882 if ((sm == null) || (db == null)) { 883 throw new NullPointerException("SampleModel and DataBuffer cannot be null"); 884 } 885 886 if (location == null) { 887 location = new Point(0,0); 888 } 889 int dataType = sm.getDataType(); 890 891 if (sm instanceof PixelInterleavedSampleModel) { 892 switch(dataType) { 893 case DataBuffer.TYPE_BYTE: 894 return new ByteInterleavedRaster(sm, db, location); 895 896 case DataBuffer.TYPE_USHORT: 897 return new ShortInterleavedRaster(sm, db, location); 898 } 899 } else if (sm instanceof SinglePixelPackedSampleModel) { 900 switch(dataType) { 901 case DataBuffer.TYPE_BYTE: 902 return new ByteInterleavedRaster(sm, db, location); 903 904 case DataBuffer.TYPE_USHORT: 905 return new ShortInterleavedRaster(sm, db, location); 906 907 case DataBuffer.TYPE_INT: 908 return new IntegerInterleavedRaster(sm, db, location); 909 } 910 } else if (sm instanceof MultiPixelPackedSampleModel && 911 dataType == DataBuffer.TYPE_BYTE && 912 sm.getSampleSize(0) < 8) { 913 return new BytePackedRaster(sm, db, location); 914 } 915 916 // we couldn't do anything special - do the generic thing 917 918 return new Raster(sm,db,location); 919 } 920 921 /** 922 * Creates a WritableRaster with the specified SampleModel. 923 * The upper left corner of the Raster is given by the location argument. 924 * If location is null, (0, 0) will be used. 925 * @param sm the specified {@code SampleModel} 926 * @param location the upper-left corner of the 927 * {@code WritableRaster} 928 * @return a {@code WritableRaster} with the specified 929 * {@code SampleModel} and location. 930 * @throws RasterFormatException if computing either 931 * {@code location.x + sm.getWidth()} or 932 * {@code location.y + sm.getHeight()} results in integer 933 * overflow 934 */ 935 public static WritableRaster createWritableRaster(SampleModel sm, 936 Point location) { 937 if (location == null) { 938 location = new Point(0,0); 939 } 940 941 return createWritableRaster(sm, sm.createDataBuffer(), location); 942 } 943 944 /** 945 * Creates a WritableRaster with the specified SampleModel and DataBuffer. 946 * The upper left corner of the Raster is given by the location argument. 947 * If location is null, (0, 0) will be used. 948 * @param sm the specified {@code SampleModel} 949 * @param db the specified {@code DataBuffer} 950 * @param location the upper-left corner of the 951 * {@code WritableRaster} 952 * @return a {@code WritableRaster} with the specified 953 * {@code SampleModel}, {@code DataBuffer}, and 954 * location. 955 * @throws RasterFormatException if computing either 956 * {@code location.x + sm.getWidth()} or 957 * {@code location.y + sm.getHeight()} results in integer 958 * overflow 959 * @throws RasterFormatException if {@code db} has more 960 * than one bank and {@code sm} is a 961 * PixelInterleavedSampleModel, SinglePixelPackedSampleModel, 962 * or MultiPixelPackedSampleModel. 963 * @throws NullPointerException if either SampleModel or DataBuffer is null 964 */ 965 public static WritableRaster createWritableRaster(SampleModel sm, 966 DataBuffer db, 967 Point location) { 968 if ((sm == null) || (db == null)) { 969 throw new NullPointerException("SampleModel and DataBuffer cannot be null"); 970 } 971 if (location == null) { 972 location = new Point(0,0); 973 } 974 975 int dataType = sm.getDataType(); 976 977 if (sm instanceof PixelInterleavedSampleModel) { 978 switch(dataType) { 979 case DataBuffer.TYPE_BYTE: 980 if (db instanceof DataBufferByte) { 981 return new ByteInterleavedRaster(sm, db, location); 982 } 983 break; 984 985 case DataBuffer.TYPE_USHORT: 986 if (db instanceof DataBufferUShort) { 987 return new ShortInterleavedRaster(sm, db, location); 988 } 989 break; 990 } 991 } else if (sm instanceof SinglePixelPackedSampleModel) { 992 switch(dataType) { 993 case DataBuffer.TYPE_BYTE: 994 if (db instanceof DataBufferByte) { 995 return new ByteInterleavedRaster(sm, db, location); 996 } 997 break; 998 999 case DataBuffer.TYPE_USHORT: 1000 if (db instanceof DataBufferUShort) { 1001 return new ShortInterleavedRaster(sm, db, location); 1002 } 1003 break; 1004 1005 case DataBuffer.TYPE_INT: 1006 if (db instanceof DataBufferInt) { 1007 return new IntegerInterleavedRaster(sm, db, location); 1008 } 1009 break; 1010 } 1011 } else if (sm instanceof MultiPixelPackedSampleModel && 1012 dataType == DataBuffer.TYPE_BYTE && 1013 sm.getSampleSize(0) < 8) { 1014 return new BytePackedRaster(sm, db, location); 1015 } 1016 1017 // we couldn't do anything special - do the generic thing 1018 return new SunWritableRaster(sm,db,location); 1019 } 1020 1021 /** 1022 * Constructs a Raster with the given SampleModel. The Raster's 1023 * upper left corner is origin and it is the same size as the 1024 * SampleModel. A DataBuffer large enough to describe the 1025 * Raster is automatically created. 1026 * @param sampleModel The SampleModel that specifies the layout 1027 * @param origin The Point that specified the origin 1028 * @throws RasterFormatException if computing either 1029 * {@code origin.x + sampleModel.getWidth()} or 1030 * {@code origin.y + sampleModel.getHeight()} results in 1031 * integer overflow 1032 * @throws NullPointerException either {@code sampleModel} or 1033 * {@code origin} is null 1034 */ 1035 protected Raster(SampleModel sampleModel, 1036 Point origin) { 1037 this(sampleModel, 1038 sampleModel.createDataBuffer(), 1039 new Rectangle(origin.x, 1040 origin.y, 1041 sampleModel.getWidth(), 1042 sampleModel.getHeight()), 1043 origin, 1044 null); 1045 } 1046 1047 /** 1048 * Constructs a Raster with the given SampleModel and DataBuffer. 1049 * The Raster's upper left corner is origin and it is the same size 1050 * as the SampleModel. The DataBuffer is not initialized and must 1051 * be compatible with SampleModel. 1052 * @param sampleModel The SampleModel that specifies the layout 1053 * @param dataBuffer The DataBuffer that contains the image data 1054 * @param origin The Point that specifies the origin 1055 * @throws RasterFormatException if computing either 1056 * {@code origin.x + sampleModel.getWidth()} or 1057 * {@code origin.y + sampleModel.getHeight()} results in 1058 * integer overflow 1059 * @throws NullPointerException either {@code sampleModel} or 1060 * {@code origin} is null 1061 */ 1062 protected Raster(SampleModel sampleModel, 1063 DataBuffer dataBuffer, 1064 Point origin) { 1065 this(sampleModel, 1066 dataBuffer, 1067 new Rectangle(origin.x, 1068 origin.y, 1069 sampleModel.getWidth(), 1070 sampleModel.getHeight()), 1071 origin, 1072 null); 1073 } 1074 1075 /** 1076 * Constructs a Raster with the given SampleModel, DataBuffer, and 1077 * parent. aRegion specifies the bounding rectangle of the new 1078 * Raster. When translated into the base Raster's coordinate 1079 * system, aRegion must be contained by the base Raster. 1080 * (The base Raster is the Raster's ancestor which has no parent.) 1081 * sampleModelTranslate specifies the sampleModelTranslateX and 1082 * sampleModelTranslateY values of the new Raster. 1083 * 1084 * Note that this constructor should generally be called by other 1085 * constructors or create methods, it should not be used directly. 1086 * @param sampleModel The SampleModel that specifies the layout 1087 * @param dataBuffer The DataBuffer that contains the image data 1088 * @param aRegion The Rectangle that specifies the image area 1089 * @param sampleModelTranslate The Point that specifies the translation 1090 * from SampleModel to Raster coordinates 1091 * @param parent The parent (if any) of this raster 1092 * @throws NullPointerException if any of {@code sampleModel}, 1093 * {@code dataBuffer}, {@code aRegion} or 1094 * {@code sampleModelTranslate} is null 1095 * @throws RasterFormatException if {@code aRegion} has width 1096 * or height less than or equal to zero, or computing either 1097 * {@code aRegion.x + aRegion.width} or 1098 * {@code aRegion.y + aRegion.height} results in integer 1099 * overflow 1100 */ 1101 protected Raster(SampleModel sampleModel, 1102 DataBuffer dataBuffer, 1103 Rectangle aRegion, 1104 Point sampleModelTranslate, 1105 Raster parent) { 1106 1107 if ((sampleModel == null) || (dataBuffer == null) || 1108 (aRegion == null) || (sampleModelTranslate == null)) { 1109 throw new NullPointerException("SampleModel, dataBuffer, aRegion and " + 1110 "sampleModelTranslate cannot be null"); 1111 } 1112 this.sampleModel = sampleModel; 1113 this.dataBuffer = dataBuffer; 1114 minX = aRegion.x; 1115 minY = aRegion.y; 1116 width = aRegion.width; 1117 height = aRegion.height; 1118 if (width <= 0 || height <= 0) { 1119 throw new RasterFormatException("negative or zero " + 1120 ((width <= 0) ? "width" : "height")); 1121 } 1122 if ((minX + width) < minX) { 1123 throw new RasterFormatException( 1124 "overflow condition for X coordinates of Raster"); 1125 } 1126 if ((minY + height) < minY) { 1127 throw new RasterFormatException( 1128 "overflow condition for Y coordinates of Raster"); 1129 } 1130 1131 sampleModelTranslateX = sampleModelTranslate.x; 1132 sampleModelTranslateY = sampleModelTranslate.y; 1133 1134 numBands = sampleModel.getNumBands(); 1135 numDataElements = sampleModel.getNumDataElements(); 1136 this.parent = parent; 1137 } 1138 1139 1140 /** 1141 * Returns the parent Raster (if any) of this Raster or null. 1142 * @return the parent Raster or {@code null}. 1143 */ 1144 public Raster getParent() { 1145 return parent; 1146 } 1147 1148 /** 1149 * Returns the X translation from the coordinate system of the 1150 * SampleModel to that of the Raster. To convert a pixel's X 1151 * coordinate from the Raster coordinate system to the SampleModel 1152 * coordinate system, this value must be subtracted. 1153 * @return the X translation from the coordinate space of the 1154 * Raster's SampleModel to that of the Raster. 1155 */ 1156 public final int getSampleModelTranslateX() { 1157 return sampleModelTranslateX; 1158 } 1159 1160 /** 1161 * Returns the Y translation from the coordinate system of the 1162 * SampleModel to that of the Raster. To convert a pixel's Y 1163 * coordinate from the Raster coordinate system to the SampleModel 1164 * coordinate system, this value must be subtracted. 1165 * @return the Y translation from the coordinate space of the 1166 * Raster's SampleModel to that of the Raster. 1167 */ 1168 public final int getSampleModelTranslateY() { 1169 return sampleModelTranslateY; 1170 } 1171 1172 /** 1173 * Create a compatible WritableRaster the same size as this Raster with 1174 * the same SampleModel and a new initialized DataBuffer. 1175 * @return a compatible {@code WritableRaster} with the same sample 1176 * model and a new data buffer. 1177 */ 1178 public WritableRaster createCompatibleWritableRaster() { 1179 return new SunWritableRaster(sampleModel, new Point(0,0)); 1180 } 1181 1182 /** 1183 * Create a compatible WritableRaster with the specified size, a new 1184 * SampleModel, and a new initialized DataBuffer. 1185 * @param w the specified width of the new {@code WritableRaster} 1186 * @param h the specified height of the new {@code WritableRaster} 1187 * @return a compatible {@code WritableRaster} with the specified 1188 * size and a new sample model and data buffer. 1189 * @exception RasterFormatException if the width or height is less than 1190 * or equal to zero. 1191 */ 1192 public WritableRaster createCompatibleWritableRaster(int w, int h) { 1193 if (w <= 0 || h <=0) { 1194 throw new RasterFormatException("negative " + 1195 ((w <= 0) ? "width" : "height")); 1196 } 1197 1198 SampleModel sm = sampleModel.createCompatibleSampleModel(w,h); 1199 1200 return new SunWritableRaster(sm, new Point(0,0)); 1201 } 1202 1203 /** 1204 * Create a compatible WritableRaster with location (minX, minY) 1205 * and size (width, height) specified by rect, a 1206 * new SampleModel, and a new initialized DataBuffer. 1207 * @param rect a {@code Rectangle} that specifies the size and 1208 * location of the {@code WritableRaster} 1209 * @return a compatible {@code WritableRaster} with the specified 1210 * size and location and a new sample model and data buffer. 1211 * @throws RasterFormatException if {@code rect} has width 1212 * or height less than or equal to zero, or computing either 1213 * {@code rect.x + rect.width} or 1214 * {@code rect.y + rect.height} results in integer 1215 * overflow 1216 * @throws NullPointerException if {@code rect} is null 1217 */ 1218 public WritableRaster createCompatibleWritableRaster(Rectangle rect) { 1219 if (rect == null) { 1220 throw new NullPointerException("Rect cannot be null"); 1221 } 1222 return createCompatibleWritableRaster(rect.x, rect.y, 1223 rect.width, rect.height); 1224 } 1225 1226 /** 1227 * Create a compatible WritableRaster with the specified 1228 * location (minX, minY) and size (width, height), a 1229 * new SampleModel, and a new initialized DataBuffer. 1230 * @param x the X coordinate of the upper-left corner of 1231 * the {@code WritableRaster} 1232 * @param y the Y coordinate of the upper-left corner of 1233 * the {@code WritableRaster} 1234 * @param w the specified width of the {@code WritableRaster} 1235 * @param h the specified height of the {@code WritableRaster} 1236 * @return a compatible {@code WritableRaster} with the specified 1237 * size and location and a new sample model and data buffer. 1238 * @throws RasterFormatException if {@code w} or {@code h} 1239 * is less than or equal to zero, or computing either 1240 * {@code x + w} or 1241 * {@code y + h} results in integer 1242 * overflow 1243 */ 1244 public WritableRaster createCompatibleWritableRaster(int x, int y, 1245 int w, int h) { 1246 WritableRaster ret = createCompatibleWritableRaster(w, h); 1247 return ret.createWritableChild(0,0,w,h,x,y,null); 1248 } 1249 1250 /** 1251 * Create a Raster with the same size, SampleModel and DataBuffer 1252 * as this one, but with a different location. The new Raster 1253 * will possess a reference to the current Raster, accessible 1254 * through its getParent() method. 1255 * 1256 * @param childMinX the X coordinate of the upper-left 1257 * corner of the new {@code Raster} 1258 * @param childMinY the Y coordinate of the upper-left 1259 * corner of the new {@code Raster} 1260 * @return a new {@code Raster} with the same size, SampleModel, 1261 * and DataBuffer as this {@code Raster}, but with the 1262 * specified location. 1263 * @throws RasterFormatException if computing either 1264 * {@code childMinX + this.getWidth()} or 1265 * {@code childMinY + this.getHeight()} results in integer 1266 * overflow 1267 */ 1268 public Raster createTranslatedChild(int childMinX, int childMinY) { 1269 return createChild(minX,minY,width,height, 1270 childMinX,childMinY,null); 1271 } 1272 1273 /** 1274 * Returns a new Raster which shares all or part of this Raster's 1275 * DataBuffer. The new Raster will possess a reference to the 1276 * current Raster, accessible through its getParent() method. 1277 * 1278 * <p> The parentX, parentY, width and height parameters 1279 * form a Rectangle in this Raster's coordinate space, 1280 * indicating the area of pixels to be shared. An error will 1281 * be thrown if this Rectangle is not contained with the bounds 1282 * of the current Raster. 1283 * 1284 * <p> The new Raster may additionally be translated to a 1285 * different coordinate system for the plane than that used by the current 1286 * Raster. The childMinX and childMinY parameters give the new 1287 * (x, y) coordinate of the upper-left pixel of the returned 1288 * Raster; the coordinate (childMinX, childMinY) in the new Raster 1289 * will map to the same pixel as the coordinate (parentX, parentY) 1290 * in the current Raster. 1291 * 1292 * <p> The new Raster may be defined to contain only a subset of 1293 * the bands of the current Raster, possibly reordered, by means 1294 * of the bandList parameter. If bandList is null, it is taken to 1295 * include all of the bands of the current Raster in their current 1296 * order. 1297 * 1298 * <p> To create a new Raster that contains a subregion of the current 1299 * Raster, but shares its coordinate system and bands, 1300 * this method should be called with childMinX equal to parentX, 1301 * childMinY equal to parentY, and bandList equal to null. 1302 * 1303 * @param parentX The X coordinate of the upper-left corner 1304 * in this Raster's coordinates 1305 * @param parentY The Y coordinate of the upper-left corner 1306 * in this Raster's coordinates 1307 * @param width Width of the region starting at (parentX, parentY) 1308 * @param height Height of the region starting at (parentX, parentY). 1309 * @param childMinX The X coordinate of the upper-left corner 1310 * of the returned Raster 1311 * @param childMinY The Y coordinate of the upper-left corner 1312 * of the returned Raster 1313 * @param bandList Array of band indices, or null to use all bands 1314 * @return a new {@code Raster}. 1315 * @exception RasterFormatException if the specified subregion is outside 1316 * of the raster bounds. 1317 * @throws RasterFormatException if {@code width} or 1318 * {@code height} 1319 * is less than or equal to zero, or computing any of 1320 * {@code parentX + width}, {@code parentY + height}, 1321 * {@code childMinX + width}, or 1322 * {@code childMinY + height} results in integer 1323 * overflow 1324 */ 1325 public Raster createChild(int parentX, int parentY, 1326 int width, int height, 1327 int childMinX, int childMinY, 1328 int bandList[]) { 1329 if (parentX < this.minX) { 1330 throw new RasterFormatException("parentX lies outside raster"); 1331 } 1332 if (parentY < this.minY) { 1333 throw new RasterFormatException("parentY lies outside raster"); 1334 } 1335 if ((parentX + width < parentX) || 1336 (parentX + width > this.width + this.minX)) { 1337 throw new RasterFormatException("(parentX + width) is outside raster"); 1338 } 1339 if ((parentY + height < parentY) || 1340 (parentY + height > this.height + this.minY)) { 1341 throw new RasterFormatException("(parentY + height) is outside raster"); 1342 } 1343 1344 SampleModel subSampleModel; 1345 // Note: the SampleModel for the child Raster should have the same 1346 // width and height as that for the parent, since it represents 1347 // the physical layout of the pixel data. The child Raster's width 1348 // and height represent a "virtual" view of the pixel data, so 1349 // they may be different than those of the SampleModel. 1350 if (bandList == null) { 1351 subSampleModel = sampleModel; 1352 } else { 1353 subSampleModel = sampleModel.createSubsetSampleModel(bandList); 1354 } 1355 1356 int deltaX = childMinX - parentX; 1357 int deltaY = childMinY - parentY; 1358 1359 return new Raster(subSampleModel, getDataBuffer(), 1360 new Rectangle(childMinX, childMinY, width, height), 1361 new Point(sampleModelTranslateX + deltaX, 1362 sampleModelTranslateY + deltaY), this); 1363 } 1364 1365 /** 1366 * Returns the bounding Rectangle of this Raster. This function returns 1367 * the same information as getMinX/MinY/Width/Height. 1368 * @return the bounding box of this {@code Raster}. 1369 */ 1370 public Rectangle getBounds() { 1371 return new Rectangle(minX, minY, width, height); 1372 } 1373 1374 /** Returns the minimum valid X coordinate of the Raster. 1375 * @return the minimum x coordinate of this {@code Raster}. 1376 */ 1377 public final int getMinX() { 1378 return minX; 1379 } 1380 1381 /** Returns the minimum valid Y coordinate of the Raster. 1382 * @return the minimum y coordinate of this {@code Raster}. 1383 */ 1384 public final int getMinY() { 1385 return minY; 1386 } 1387 1388 /** Returns the width in pixels of the Raster. 1389 * @return the width of this {@code Raster}. 1390 */ 1391 public final int getWidth() { 1392 return width; 1393 } 1394 1395 /** Returns the height in pixels of the Raster. 1396 * @return the height of this {@code Raster}. 1397 */ 1398 public final int getHeight() { 1399 return height; 1400 } 1401 1402 /** Returns the number of bands (samples per pixel) in this Raster. 1403 * @return the number of bands of this {@code Raster}. 1404 */ 1405 public final int getNumBands() { 1406 return numBands; 1407 } 1408 1409 /** 1410 * Returns the number of data elements needed to transfer one pixel 1411 * via the getDataElements and setDataElements methods. When pixels 1412 * are transferred via these methods, they may be transferred in a 1413 * packed or unpacked format, depending on the implementation of the 1414 * underlying SampleModel. Using these methods, pixels are transferred 1415 * as an array of getNumDataElements() elements of a primitive type given 1416 * by getTransferType(). The TransferType may or may not be the same 1417 * as the storage data type of the DataBuffer. 1418 * @return the number of data elements. 1419 */ 1420 public final int getNumDataElements() { 1421 return sampleModel.getNumDataElements(); 1422 } 1423 1424 /** 1425 * Returns the TransferType used to transfer pixels via the 1426 * getDataElements and setDataElements methods. When pixels 1427 * are transferred via these methods, they may be transferred in a 1428 * packed or unpacked format, depending on the implementation of the 1429 * underlying SampleModel. Using these methods, pixels are transferred 1430 * as an array of getNumDataElements() elements of a primitive type given 1431 * by getTransferType(). The TransferType may or may not be the same 1432 * as the storage data type of the DataBuffer. The TransferType will 1433 * be one of the types defined in DataBuffer. 1434 * @return this transfer type. 1435 */ 1436 public final int getTransferType() { 1437 return sampleModel.getTransferType(); 1438 } 1439 1440 /** Returns the DataBuffer associated with this Raster. 1441 * @return the {@code DataBuffer} of this {@code Raster}. 1442 */ 1443 public DataBuffer getDataBuffer() { 1444 return dataBuffer; 1445 } 1446 1447 /** Returns the SampleModel that describes the layout of the image data. 1448 * @return the {@code SampleModel} of this {@code Raster}. 1449 */ 1450 public SampleModel getSampleModel() { 1451 return sampleModel; 1452 } 1453 1454 /** 1455 * Returns data for a single pixel in a primitive array of type 1456 * TransferType. For image data supported by the Java 2D(tm) API, 1457 * this will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, 1458 * DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, 1459 * or DataBuffer.TYPE_DOUBLE. Data may be returned in a packed format, 1460 * thus increasing efficiency for data transfers. 1461 * An ArrayIndexOutOfBoundsException may be thrown 1462 * if the coordinates are not in bounds. However, explicit bounds 1463 * checking is not guaranteed. 1464 * A ClassCastException will be thrown if the input object is non null 1465 * and references anything other than an array of TransferType. 1466 * @see java.awt.image.SampleModel#getDataElements(int, int, Object, DataBuffer) 1467 * @param x The X coordinate of the pixel location 1468 * @param y The Y coordinate of the pixel location 1469 * @param outData An object reference to an array of type defined by 1470 * getTransferType() and length getNumDataElements(). 1471 * If null, an array of appropriate type and size will be 1472 * allocated 1473 * @return An object reference to an array of type defined by 1474 * getTransferType() with the requested pixel data. 1475 * 1476 * @throws ArrayIndexOutOfBoundsException if the coordinates are not 1477 * in bounds, or if outData is too small to hold the output. 1478 */ 1479 public Object getDataElements(int x, int y, Object outData) { 1480 return sampleModel.getDataElements(x - sampleModelTranslateX, 1481 y - sampleModelTranslateY, 1482 outData, dataBuffer); 1483 } 1484 1485 /** 1486 * Returns the pixel data for the specified rectangle of pixels in a 1487 * primitive array of type TransferType. 1488 * For image data supported by the Java 2D API, this 1489 * will be one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, 1490 * DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, 1491 * or DataBuffer.TYPE_DOUBLE. Data may be returned in a packed format, 1492 * thus increasing efficiency for data transfers. 1493 * An ArrayIndexOutOfBoundsException may be thrown 1494 * if the coordinates are not in bounds. However, explicit bounds 1495 * checking is not guaranteed. 1496 * A ClassCastException will be thrown if the input object is non null 1497 * and references anything other than an array of TransferType. 1498 * @see java.awt.image.SampleModel#getDataElements(int, int, int, int, Object, DataBuffer) 1499 * @param x The X coordinate of the upper-left pixel location 1500 * @param y The Y coordinate of the upper-left pixel location 1501 * @param w Width of the pixel rectangle 1502 * @param h Height of the pixel rectangle 1503 * @param outData An object reference to an array of type defined by 1504 * getTransferType() and length w*h*getNumDataElements(). 1505 * If null, an array of appropriate type and size will be 1506 * allocated. 1507 * @return An object reference to an array of type defined by 1508 * getTransferType() with the requested pixel data. 1509 * 1510 * @throws ArrayIndexOutOfBoundsException if the coordinates are not 1511 * in bounds, or if outData is too small to hold the output. 1512 */ 1513 public Object getDataElements(int x, int y, int w, int h, Object outData) { 1514 return sampleModel.getDataElements(x - sampleModelTranslateX, 1515 y - sampleModelTranslateY, 1516 w, h, outData, dataBuffer); 1517 } 1518 1519 /** 1520 * Returns the samples in an array of int for the specified pixel. 1521 * An ArrayIndexOutOfBoundsException may be thrown 1522 * if the coordinates are not in bounds. However, explicit bounds 1523 * checking is not guaranteed. 1524 * @param x The X coordinate of the pixel location 1525 * @param y The Y coordinate of the pixel location 1526 * @param iArray An optionally preallocated int array 1527 * @return the samples for the specified pixel. 1528 * 1529 * @throws ArrayIndexOutOfBoundsException if the coordinates are not 1530 * in bounds, or if iArray is too small to hold the output. 1531 */ 1532 public int[] getPixel(int x, int y, int iArray[]) { 1533 return sampleModel.getPixel(x - sampleModelTranslateX, 1534 y - sampleModelTranslateY, 1535 iArray, dataBuffer); 1536 } 1537 1538 /** 1539 * Returns the samples in an array of float for the 1540 * specified pixel. 1541 * An ArrayIndexOutOfBoundsException may be thrown 1542 * if the coordinates are not in bounds. However, explicit bounds 1543 * checking is not guaranteed. 1544 * @param x The X coordinate of the pixel location 1545 * @param y The Y coordinate of the pixel location 1546 * @param fArray An optionally preallocated float array 1547 * @return the samples for the specified pixel. 1548 * 1549 * @throws ArrayIndexOutOfBoundsException if the coordinates are not 1550 * in bounds, or if fArray is too small to hold the output. 1551 */ 1552 public float[] getPixel(int x, int y, float fArray[]) { 1553 return sampleModel.getPixel(x - sampleModelTranslateX, 1554 y - sampleModelTranslateY, 1555 fArray, dataBuffer); 1556 } 1557 1558 /** 1559 * Returns the samples in an array of double for the specified pixel. 1560 * An ArrayIndexOutOfBoundsException may be thrown 1561 * if the coordinates are not in bounds. However, explicit bounds 1562 * checking is not guaranteed. 1563 * @param x The X coordinate of the pixel location 1564 * @param y The Y coordinate of the pixel location 1565 * @param dArray An optionally preallocated double array 1566 * @return the samples for the specified pixel. 1567 * 1568 * @throws ArrayIndexOutOfBoundsException if the coordinates are not 1569 * in bounds, or if dArray is too small to hold the output. 1570 */ 1571 public double[] getPixel(int x, int y, double dArray[]) { 1572 return sampleModel.getPixel(x - sampleModelTranslateX, 1573 y - sampleModelTranslateY, 1574 dArray, dataBuffer); 1575 } 1576 1577 /** 1578 * Returns an int array containing all samples for a rectangle of pixels, 1579 * one sample per array element. 1580 * An ArrayIndexOutOfBoundsException may be thrown 1581 * if the coordinates are not in bounds. However, explicit bounds 1582 * checking is not guaranteed. 1583 * @param x The X coordinate of the upper-left pixel location 1584 * @param y The Y coordinate of the upper-left pixel location 1585 * @param w Width of the pixel rectangle 1586 * @param h Height of the pixel rectangle 1587 * @param iArray An optionally pre-allocated int array 1588 * @return the samples for the specified rectangle of pixels. 1589 * 1590 * @throws ArrayIndexOutOfBoundsException if the coordinates are not 1591 * in bounds, or if iArray is too small to hold the output. 1592 */ 1593 public int[] getPixels(int x, int y, int w, int h, int iArray[]) { 1594 return sampleModel.getPixels(x - sampleModelTranslateX, 1595 y - sampleModelTranslateY, w, h, 1596 iArray, dataBuffer); 1597 } 1598 1599 /** 1600 * Returns a float array containing all samples for a rectangle of pixels, 1601 * one sample per array element. 1602 * An ArrayIndexOutOfBoundsException may be thrown 1603 * if the coordinates are not in bounds. However, explicit bounds 1604 * checking is not guaranteed. 1605 * @param x The X coordinate of the pixel location 1606 * @param y The Y coordinate of the pixel location 1607 * @param w Width of the pixel rectangle 1608 * @param h Height of the pixel rectangle 1609 * @param fArray An optionally pre-allocated float array 1610 * @return the samples for the specified rectangle of pixels. 1611 * 1612 * @throws ArrayIndexOutOfBoundsException if the coordinates are not 1613 * in bounds, or if fArray is too small to hold the output. 1614 */ 1615 public float[] getPixels(int x, int y, int w, int h, 1616 float fArray[]) { 1617 return sampleModel.getPixels(x - sampleModelTranslateX, 1618 y - sampleModelTranslateY, w, h, 1619 fArray, dataBuffer); 1620 } 1621 1622 /** 1623 * Returns a double array containing all samples for a rectangle of pixels, 1624 * one sample per array element. 1625 * An ArrayIndexOutOfBoundsException may be thrown 1626 * if the coordinates are not in bounds. However, explicit bounds 1627 * checking is not guaranteed. 1628 * @param x The X coordinate of the upper-left pixel location 1629 * @param y The Y coordinate of the upper-left pixel location 1630 * @param w Width of the pixel rectangle 1631 * @param h Height of the pixel rectangle 1632 * @param dArray An optionally pre-allocated double array 1633 * @return the samples for the specified rectangle of pixels. 1634 * 1635 * @throws ArrayIndexOutOfBoundsException if the coordinates are not 1636 * in bounds, or if dArray is too small to hold the output. 1637 */ 1638 public double[] getPixels(int x, int y, int w, int h, 1639 double dArray[]) { 1640 return sampleModel.getPixels(x - sampleModelTranslateX, 1641 y - sampleModelTranslateY, 1642 w, h, dArray, dataBuffer); 1643 } 1644 1645 1646 /** 1647 * Returns the sample in a specified band for the pixel located 1648 * at (x,y) as an int. 1649 * An ArrayIndexOutOfBoundsException may be thrown 1650 * if the coordinates are not in bounds. However, explicit bounds 1651 * checking is not guaranteed. 1652 * @param x The X coordinate of the pixel location 1653 * @param y The Y coordinate of the pixel location 1654 * @param b The band to return 1655 * @return the sample in the specified band for the pixel at the 1656 * specified coordinate. 1657 * 1658 * @throws ArrayIndexOutOfBoundsException if the coordinates or 1659 * the band index are not in bounds. 1660 */ 1661 public int getSample(int x, int y, int b) { 1662 return sampleModel.getSample(x - sampleModelTranslateX, 1663 y - sampleModelTranslateY, b, 1664 dataBuffer); 1665 } 1666 1667 /** 1668 * Returns the sample in a specified band 1669 * for the pixel located at (x,y) as a float. 1670 * An ArrayIndexOutOfBoundsException may be thrown 1671 * if the coordinates are not in bounds. However, explicit bounds 1672 * checking is not guaranteed. 1673 * @param x The X coordinate of the pixel location 1674 * @param y The Y coordinate of the pixel location 1675 * @param b The band to return 1676 * @return the sample in the specified band for the pixel at the 1677 * specified coordinate. 1678 * 1679 * @throws ArrayIndexOutOfBoundsException if the coordinates or 1680 * the band index are not in bounds. 1681 */ 1682 public float getSampleFloat(int x, int y, int b) { 1683 return sampleModel.getSampleFloat(x - sampleModelTranslateX, 1684 y - sampleModelTranslateY, b, 1685 dataBuffer); 1686 } 1687 1688 /** 1689 * Returns the sample in a specified band 1690 * for a pixel located at (x,y) as a double. 1691 * An ArrayIndexOutOfBoundsException may be thrown 1692 * if the coordinates are not in bounds. However, explicit bounds 1693 * checking is not guaranteed. 1694 * @param x The X coordinate of the pixel location 1695 * @param y The Y coordinate of the pixel location 1696 * @param b The band to return 1697 * @return the sample in the specified band for the pixel at the 1698 * specified coordinate. 1699 * 1700 * @throws ArrayIndexOutOfBoundsException if the coordinates or 1701 * the band index are not in bounds. 1702 */ 1703 public double getSampleDouble(int x, int y, int b) { 1704 return sampleModel.getSampleDouble(x - sampleModelTranslateX, 1705 y - sampleModelTranslateY, 1706 b, dataBuffer); 1707 } 1708 1709 /** 1710 * Returns the samples for a specified band for the specified rectangle 1711 * of pixels in an int array, one sample per array element. 1712 * An ArrayIndexOutOfBoundsException may be thrown 1713 * if the coordinates are not in bounds. However, explicit bounds 1714 * checking is not guaranteed. 1715 * @param x The X coordinate of the upper-left pixel location 1716 * @param y The Y coordinate of the upper-left pixel location 1717 * @param w Width of the pixel rectangle 1718 * @param h Height of the pixel rectangle 1719 * @param b The band to return 1720 * @param iArray An optionally pre-allocated int array 1721 * @return the samples for the specified band for the specified 1722 * rectangle of pixels. 1723 * 1724 * @throws ArrayIndexOutOfBoundsException if the coordinates or 1725 * the band index are not in bounds, or if iArray is too small to 1726 * hold the output. 1727 */ 1728 public int[] getSamples(int x, int y, int w, int h, int b, 1729 int iArray[]) { 1730 return sampleModel.getSamples(x - sampleModelTranslateX, 1731 y - sampleModelTranslateY, 1732 w, h, b, iArray, 1733 dataBuffer); 1734 } 1735 1736 /** 1737 * Returns the samples for a specified band for the specified rectangle 1738 * of pixels in a float array, one sample per array element. 1739 * An ArrayIndexOutOfBoundsException may be thrown 1740 * if the coordinates are not in bounds. However, explicit bounds 1741 * checking is not guaranteed. 1742 * @param x The X coordinate of the upper-left pixel location 1743 * @param y The Y coordinate of the upper-left pixel location 1744 * @param w Width of the pixel rectangle 1745 * @param h Height of the pixel rectangle 1746 * @param b The band to return 1747 * @param fArray An optionally pre-allocated float array 1748 * @return the samples for the specified band for the specified 1749 * rectangle of pixels. 1750 * 1751 * @throws ArrayIndexOutOfBoundsException if the coordinates or 1752 * the band index are not in bounds, or if fArray is too small to 1753 * hold the output. 1754 */ 1755 public float[] getSamples(int x, int y, int w, int h, int b, 1756 float fArray[]) { 1757 return sampleModel.getSamples(x - sampleModelTranslateX, 1758 y - sampleModelTranslateY, 1759 w, h, b, fArray, dataBuffer); 1760 } 1761 1762 /** 1763 * Returns the samples for a specified band for a specified rectangle 1764 * of pixels in a double array, one sample per array element. 1765 * An ArrayIndexOutOfBoundsException may be thrown 1766 * if the coordinates are not in bounds. However, explicit bounds 1767 * checking is not guaranteed. 1768 * @param x The X coordinate of the upper-left pixel location 1769 * @param y The Y coordinate of the upper-left pixel location 1770 * @param w Width of the pixel rectangle 1771 * @param h Height of the pixel rectangle 1772 * @param b The band to return 1773 * @param dArray An optionally pre-allocated double array 1774 * @return the samples for the specified band for the specified 1775 * rectangle of pixels. 1776 * 1777 * @throws ArrayIndexOutOfBoundsException if the coordinates or 1778 * the band index are not in bounds, or if dArray is too small to 1779 * hold the output. 1780 */ 1781 public double[] getSamples(int x, int y, int w, int h, int b, 1782 double dArray[]) { 1783 return sampleModel.getSamples(x - sampleModelTranslateX, 1784 y - sampleModelTranslateY, 1785 w, h, b, dArray, dataBuffer); 1786 } 1787 1788 }