1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 /** 18 * @author Igor V. Stolyarov 19 * @version $Revision$ 20 */ 21 22 package java.awt.image; 23 24 import org.apache.harmony.awt.internal.nls.Messages; 25 26 /** 27 * The SampleModel class is abstract class for retrieving pixel's samples in the 28 * data of an image. Each pixel contains several samples. A sample is the set of 29 * values of the bands for single pixel. For example, each pixel in the RGB 30 * model contains three samples and there are three corresponding bands in the 31 * image data of such pixels representing red, green and blue components. 32 * <p> 33 * The image data is represented as a Raster with a DataBuffer and a 34 * SampleModel. The SampleModel allows access to the samples in the DataBuffer. 35 * 36 * @since Android 1.0 37 */ 38 public abstract class SampleModel { 39 40 /** 41 * The width of the image data which this SampleModel describes. 42 */ 43 protected int width; 44 45 /** 46 * The height of the image data which this SampleModel describes. 47 */ 48 protected int height; 49 50 /** 51 * The number of bands of image data which this SampleModel describes. 52 */ 53 protected int numBands; 54 55 /** 56 * The data type of the image data which this SampleModel describes. 57 */ 58 protected int dataType; 59 60 /** 61 * Instantiates a new SampleModel with the specified data type, width, 62 * height and number of bands. 63 * 64 * @param dataType 65 * the data type of the image data. 66 * @param w 67 * the width of the image data. 68 * @param h 69 * the height of the image data. 70 * @param numBands 71 * the number of bands of the image data. 72 */ 73 public SampleModel(int dataType, int w, int h, int numBands) { 74 if (w <= 0 || h <= 0) { 75 // awt.22E=w or h is less than or equal to zero 76 throw new IllegalArgumentException(Messages.getString("awt.22E")); //$NON-NLS-1$ 77 } 78 79 double squre = ((double)w) * ((double)h); 80 if (squre >= Integer.MAX_VALUE) { 81 // awt.22F=The product of w and h is greater than Integer.MAX_VALUE 82 throw new IllegalArgumentException(Messages.getString("awt.22F")); //$NON-NLS-1$ 83 } 84 85 if (dataType < DataBuffer.TYPE_BYTE || dataType > DataBuffer.TYPE_DOUBLE 86 && dataType != DataBuffer.TYPE_UNDEFINED) { 87 // awt.230=dataType is not one of the supported data types 88 throw new IllegalArgumentException(Messages.getString("awt.230")); //$NON-NLS-1$ 89 } 90 91 if (numBands < 1) { 92 // awt.231=Number of bands must be more then 0 93 throw new IllegalArgumentException(Messages.getString("awt.231")); //$NON-NLS-1$ 94 } 95 96 this.dataType = dataType; 97 this.width = w; 98 this.height = h; 99 this.numBands = numBands; 100 101 } 102 103 /** 104 * Gets the data array for the specified pixel of the specified DataBuffer 105 * with one of the following types: DataBuffer.TYPE_BYTE, 106 * DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, 107 * DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE. 108 * 109 * @param x 110 * the X coordinate of pixel. 111 * @param y 112 * the Y coordinate of pixel. 113 * @param obj 114 * the Object is a data where the result will be stored. 115 * @param data 116 * the image data. 117 * @return the data array for the specified pixel of the specified 118 * DataBuffer. 119 */ 120 public abstract Object getDataElements(int x, int y, Object obj, DataBuffer data); 121 122 /** 123 * Gets the array of pixel data for the specified rectangular area of pixels 124 * of the specified DataBuffer with one of the following types: 125 * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, 126 * DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE. 127 * 128 * @param x 129 * the X coordinate of the rectangular pixel area. 130 * @param y 131 * the Y coordinate of the rectangular pixel area. 132 * @param w 133 * the width of the rectangular pixel area. 134 * @param h 135 * the height of the rectangular pixel area. 136 * @param obj 137 * the Object is an array with the primitive type, where the 138 * result array will be stored. 139 * @param data 140 * the image data. 141 * @return the array of pixel data for the specified rectangular area of 142 * pixels of the specified DataBuffer object. 143 */ 144 public Object getDataElements(int x, int y, int w, int h, Object obj, DataBuffer data) { 145 int numDataElements = getNumDataElements(); 146 int idx = 0; 147 148 switch (getTransferType()) { 149 case DataBuffer.TYPE_BYTE: 150 byte bdata[]; 151 byte bbuf[] = null; 152 153 if (obj == null) { 154 bdata = new byte[numDataElements * w * h]; 155 } else { 156 bdata = (byte[])obj; 157 } 158 159 for (int i = y; i < y + h; i++) { 160 for (int j = x; j < x + w; j++) { 161 bbuf = (byte[])getDataElements(j, i, bbuf, data); 162 for (int n = 0; n < numDataElements; n++) { 163 bdata[idx++] = bbuf[n]; 164 } 165 } 166 } 167 obj = bdata; 168 break; 169 170 case DataBuffer.TYPE_SHORT: 171 case DataBuffer.TYPE_USHORT: 172 short sdata[]; 173 short sbuf[] = null; 174 175 if (obj == null) { 176 sdata = new short[numDataElements * w * h]; 177 } else { 178 sdata = (short[])obj; 179 } 180 181 for (int i = y; i < y + h; i++) { 182 for (int j = x; j < x + w; j++) { 183 sbuf = (short[])getDataElements(j, i, sbuf, data); 184 for (int n = 0; n < numDataElements; n++) { 185 sdata[idx++] = sbuf[n]; 186 } 187 } 188 } 189 obj = sdata; 190 break; 191 192 case DataBuffer.TYPE_INT: 193 int idata[]; 194 int ibuf[] = null; 195 196 if (obj == null) { 197 idata = new int[numDataElements * w * h]; 198 } else { 199 idata = (int[])obj; 200 } 201 202 for (int i = y; i < y + h; i++) { 203 for (int j = x; j < x + w; j++) { 204 ibuf = (int[])getDataElements(j, i, ibuf, data); 205 for (int n = 0; n < numDataElements; n++) { 206 idata[idx++] = ibuf[n]; 207 } 208 } 209 } 210 obj = idata; 211 break; 212 213 case DataBuffer.TYPE_FLOAT: 214 float fdata[]; 215 float fbuf[] = null; 216 217 if (obj == null) { 218 fdata = new float[numDataElements * w * h]; 219 } else { 220 fdata = (float[])obj; 221 } 222 223 for (int i = y; i < y + h; i++) { 224 for (int j = x; j < x + w; j++) { 225 fbuf = (float[])getDataElements(j, i, fbuf, data); 226 for (int n = 0; n < numDataElements; n++) { 227 fdata[idx++] = fbuf[n]; 228 } 229 } 230 } 231 obj = fdata; 232 break; 233 234 case DataBuffer.TYPE_DOUBLE: 235 double ddata[]; 236 double dbuf[] = null; 237 238 if (obj == null) { 239 ddata = new double[numDataElements * w * h]; 240 } else { 241 ddata = (double[])obj; 242 } 243 244 for (int i = y; i < y + h; i++) { 245 for (int j = x; j < x + w; j++) { 246 dbuf = (double[])getDataElements(j, i, dbuf, data); 247 for (int n = 0; n < numDataElements; n++) { 248 ddata[idx++] = dbuf[n]; 249 } 250 } 251 } 252 obj = ddata; 253 break; 254 255 } 256 257 return obj; 258 } 259 260 /** 261 * Sets the data for a single pixel in the specified DataBuffer from a 262 * primitive array with one of the following types: DataBuffer.TYPE_BYTE, 263 * DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, DataBuffer.TYPE_SHORT, 264 * DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE. 265 * 266 * @param x 267 * the X coordinate of pixel. 268 * @param y 269 * the Y coordinate of pixel. 270 * @param obj 271 * the Object - the array of primitive pixel data to be set. 272 * @param data 273 * the image data. 274 */ 275 public abstract void setDataElements(int x, int y, Object obj, DataBuffer data); 276 277 /** 278 * Sets the data elements for a rectangular area of pixels in the specified 279 * DataBuffer from a primitive array with one of the following types: 280 * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT, 281 * DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, or DataBuffer.TYPE_DOUBLE. 282 * 283 * @param x 284 * the X coordinate of the specified rectangular area. 285 * @param y 286 * the Y coordinate of the specified rectangular area. 287 * @param w 288 * the width of rectangle. 289 * @param h 290 * the height of rectangle. 291 * @param obj 292 * the Object - the array of primitive pixel data to be set. 293 * @param data 294 * the image data. 295 */ 296 public void setDataElements(int x, int y, int w, int h, Object obj, DataBuffer data) { 297 int numDataElements = getNumDataElements(); 298 int idx = 0; 299 300 switch (getTransferType()) { 301 case DataBuffer.TYPE_BYTE: 302 byte bbuf[] = new byte[numDataElements]; 303 for (int i = y; i < y + h; i++) { 304 for (int j = x; j < x + w; j++) { 305 for (int n = 0; n < numDataElements; n++) { 306 bbuf[n] = ((byte[])obj)[idx++]; 307 } 308 setDataElements(j, i, bbuf, data); 309 } 310 } 311 312 break; 313 314 case DataBuffer.TYPE_SHORT: 315 case DataBuffer.TYPE_USHORT: 316 short sbuf[] = new short[numDataElements]; 317 for (int i = y; i < y + h; i++) { 318 for (int j = x; j < x + w; j++) { 319 for (int n = 0; n < numDataElements; n++) { 320 sbuf[n] = ((short[])obj)[idx++]; 321 } 322 setDataElements(j, i, sbuf, data); 323 } 324 } 325 break; 326 327 case DataBuffer.TYPE_INT: 328 int ibuf[] = new int[numDataElements]; 329 for (int i = y; i < y + h; i++) { 330 for (int j = x; j < x + w; j++) { 331 for (int n = 0; n < numDataElements; n++) { 332 ibuf[n] = ((int[])obj)[idx++]; 333 } 334 setDataElements(j, i, ibuf, data); 335 } 336 } 337 break; 338 339 case DataBuffer.TYPE_FLOAT: 340 float fbuf[] = new float[numDataElements]; 341 for (int i = y; i < y + h; i++) { 342 for (int j = x; j < x + w; j++) { 343 for (int n = 0; n < numDataElements; n++) { 344 fbuf[n] = ((float[])obj)[idx++]; 345 } 346 setDataElements(j, i, fbuf, data); 347 } 348 } 349 break; 350 351 case DataBuffer.TYPE_DOUBLE: 352 double dbuf[] = new double[numDataElements]; 353 for (int i = y; i < y + h; i++) { 354 for (int j = x; j < x + w; j++) { 355 for (int n = 0; n < numDataElements; n++) { 356 dbuf[n] = ((double[])obj)[idx++]; 357 } 358 setDataElements(j, i, dbuf, data); 359 } 360 } 361 break; 362 363 } 364 } 365 366 /** 367 * Creates a new SampleModel with the specified bands of this SampleModel. 368 * 369 * @param bands 370 * the array of bands from this SampleModel. 371 * @return the SampleModel with the specified bands of this SampleModel. 372 */ 373 public abstract SampleModel createSubsetSampleModel(int bands[]); 374 375 /** 376 * Creates the SampleModel which has the same data as in this SampleModel 377 * with a different width and height. 378 * 379 * @param a0 380 * the width of the image data. 381 * @param a1 382 * the height of the image data. 383 * @return the SampleModel which has the same data as in this SampleModel 384 * with a different width and height. 385 */ 386 public abstract SampleModel createCompatibleSampleModel(int a0, int a1); 387 388 /** 389 * Gets the samples of the specified pixel as an integer array. 390 * 391 * @param x 392 * the X coordinate of pixel. 393 * @param y 394 * the Y coordinate of pixel. 395 * @param iArray 396 * the integer array where result will be stored. 397 * @param data 398 * the image data. 399 * @return the integer array with the samples of the specified pixel. 400 */ 401 public int[] getPixel(int x, int y, int iArray[], DataBuffer data) { 402 if (x < 0 || y < 0 || x >= this.width || y >= this.height) { 403 // awt.63=Coordinates are not in bounds 404 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 405 } 406 int pixel[]; 407 408 if (iArray == null) { 409 pixel = new int[numBands]; 410 } else { 411 pixel = iArray; 412 } 413 414 for (int i = 0; i < numBands; i++) { 415 pixel[i] = getSample(x, y, i, data); 416 } 417 418 return pixel; 419 } 420 421 /** 422 * Sets a pixel of the DataBuffer from a integer array of samples. 423 * 424 * @param x 425 * the X coordinate of pixel. 426 * @param y 427 * the Y coordinate of pixel. 428 * @param iArray 429 * the integer array. 430 * @param data 431 * the image data. 432 */ 433 public void setPixel(int x, int y, int iArray[], DataBuffer data) { 434 if (x < 0 || y < 0 || x >= this.width || y >= this.height) { 435 // awt.63=Coordinates are not in bounds 436 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 437 } 438 for (int i = 0; i < numBands; i++) { 439 setSample(x, y, i, iArray[i], data); 440 } 441 } 442 443 /** 444 * Gets the samples of the specified pixel as a float array. 445 * 446 * @param x 447 * the X coordinate of pixel. 448 * @param y 449 * the Y coordinate of pixel. 450 * @param fArray 451 * the float array where result will be stored. 452 * @param data 453 * the image data. 454 * @return the float array with the samples of the specified pixel. 455 */ 456 public float[] getPixel(int x, int y, float fArray[], DataBuffer data) { 457 if (x < 0 || y < 0 || x >= this.width || y >= this.height) { 458 // awt.63=Coordinates are not in bounds 459 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 460 } 461 float pixel[]; 462 463 if (fArray == null) { 464 pixel = new float[numBands]; 465 } else { 466 pixel = fArray; 467 } 468 469 for (int i = 0; i < numBands; i++) { 470 pixel[i] = getSampleFloat(x, y, i, data); 471 } 472 473 return pixel; 474 } 475 476 /** 477 * Sets a pixel of the DataBuffer from a float array of samples. 478 * 479 * @param x 480 * the X coordinate of pixel. 481 * @param y 482 * the Y coordinate of pixel. 483 * @param fArray 484 * the float array. 485 * @param data 486 * the image data. 487 */ 488 public void setPixel(int x, int y, float fArray[], DataBuffer data) { 489 if (x < 0 || y < 0 || x >= this.width || y >= this.height) { 490 // awt.63=Coordinates are not in bounds 491 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 492 } 493 for (int i = 0; i < numBands; i++) { 494 setSample(x, y, i, fArray[i], data); 495 } 496 } 497 498 /** 499 * Gets the samples of the specified pixel as a double array. 500 * 501 * @param x 502 * the X coordinate of pixel. 503 * @param y 504 * the Y coordinate of pixel. 505 * @param dArray 506 * the double array where result will be stored. 507 * @param data 508 * the image data. 509 * @return the double array with the samples of the specified pixel. 510 */ 511 public double[] getPixel(int x, int y, double dArray[], DataBuffer data) { 512 if (x < 0 || y < 0 || x >= this.width || y >= this.height) { 513 // awt.63=Coordinates are not in bounds 514 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 515 } 516 double pixel[]; 517 518 if (dArray == null) { 519 pixel = new double[numBands]; 520 } else { 521 pixel = dArray; 522 } 523 524 for (int i = 0; i < numBands; i++) { 525 pixel[i] = getSampleDouble(x, y, i, data); 526 } 527 528 return pixel; 529 } 530 531 /** 532 * Sets a pixel of the DataBuffer from a double array of samples. 533 * 534 * @param x 535 * the X coordinate of pixel. 536 * @param y 537 * the Y coordinate of pixel. 538 * @param dArray 539 * the double array. 540 * @param data 541 * the image data. 542 */ 543 public void setPixel(int x, int y, double dArray[], DataBuffer data) { 544 if (x < 0 || y < 0 || x >= this.width || y >= this.height) { 545 // awt.63=Coordinates are not in bounds 546 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 547 } 548 for (int i = 0; i < numBands; i++) { 549 setSample(x, y, i, dArray[i], data); 550 } 551 } 552 553 /** 554 * Gets the sample of a specified band for the specified pixel as an 555 * integer. 556 * 557 * @param x 558 * the X coordinate of pixel. 559 * @param y 560 * the Y coordinate of pixel. 561 * @param b 562 * the specified band. 563 * @param data 564 * the image data. 565 * @return the sample of a specified band for the specified pixel. 566 */ 567 public abstract int getSample(int x, int y, int b, DataBuffer data); 568 569 /** 570 * Gets the sample of a specified band for the specified pixel as a float. 571 * 572 * @param x 573 * the X coordinate of pixel. 574 * @param y 575 * the Y coordinate of pixel. 576 * @param b 577 * the specified band. 578 * @param data 579 * the image data. 580 * @return the sample of a specified band for the specified pixel. 581 */ 582 public float getSampleFloat(int x, int y, int b, DataBuffer data) { 583 return getSample(x, y, b, data); 584 } 585 586 /** 587 * Gets the sample of a specified band for the specified pixel as a double. 588 * 589 * @param x 590 * the X coordinate of pixel. 591 * @param y 592 * the Y coordinate of pixel. 593 * @param b 594 * the specified band. 595 * @param data 596 * the image data. 597 * @return the sample of a specified band for the specified pixel. 598 */ 599 public double getSampleDouble(int x, int y, int b, DataBuffer data) { 600 return getSample(x, y, b, data); 601 } 602 603 /** 604 * Gets the samples of the specified rectangular area of pixels as an 605 * integer array. 606 * 607 * @param x 608 * the X coordinate of the rectangle of pixels. 609 * @param y 610 * the Y coordinate of the rectangle of pixels. 611 * @param w 612 * the width of the rectangle of pixels. 613 * @param h 614 * the height of the rectangle of pixels. 615 * @param iArray 616 * the integer array where result will be stored. 617 * @param data 618 * the image data. 619 * @return the integer array with the samples of the specified rectangular 620 * area of pixels. 621 */ 622 public int[] getPixels(int x, int y, int w, int h, int iArray[], DataBuffer data) { 623 if (x < 0 || y < 0 || x + w > this.width || y + h > this.height) { 624 // awt.63=Coordinates are not in bounds 625 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 626 } 627 int pixels[]; 628 int idx = 0; 629 630 if (iArray == null) { 631 pixels = new int[w * h * numBands]; 632 } else { 633 pixels = iArray; 634 } 635 636 for (int i = y; i < y + h; i++) { 637 for (int j = x; j < x + w; j++) { 638 for (int n = 0; n < numBands; n++) { 639 pixels[idx++] = getSample(j, i, n, data); 640 } 641 } 642 } 643 return pixels; 644 } 645 646 /** 647 * Sets all of the samples for a rectangular area of pixels of the 648 * DataBuffer from an integer array. 649 * 650 * @param x 651 * the X coordinate of the rectangle of pixels. 652 * @param y 653 * the Y coordinate of the rectangle of pixels. 654 * @param w 655 * the width of the rectangle of pixels. 656 * @param h 657 * the height of the rectangle of pixels. 658 * @param iArray 659 * the integer array. 660 * @param data 661 * the image data. 662 */ 663 public void setPixels(int x, int y, int w, int h, int iArray[], DataBuffer data) { 664 if (x < 0 || y < 0 || x + w > this.width || y + h > this.height) { 665 // awt.63=Coordinates are not in bounds 666 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 667 } 668 int idx = 0; 669 for (int i = y; i < y + h; i++) { 670 for (int j = x; j < x + w; j++) { 671 for (int n = 0; n < numBands; n++) { 672 setSample(j, i, n, iArray[idx++], data); 673 } 674 } 675 } 676 } 677 678 /** 679 * Gets the samples of the specified rectangular area of pixels as a float 680 * array. 681 * 682 * @param x 683 * the X coordinate of the rectangle of pixels. 684 * @param y 685 * the Y coordinate of the rectangle of pixels. 686 * @param w 687 * the width of the rectangle of pixels. 688 * @param h 689 * the height of the rectangle of pixels. 690 * @param fArray 691 * the float array where result will be stored. 692 * @param data 693 * the image data. 694 * @return the float array with the samples of the specified rectangular 695 * area of pixels. 696 */ 697 public float[] getPixels(int x, int y, int w, int h, float fArray[], DataBuffer data) { 698 if (x < 0 || y < 0 || x + w > this.width || y + h > this.height) { 699 // awt.63=Coordinates are not in bounds 700 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 701 } 702 float pixels[]; 703 int idx = 0; 704 705 if (fArray == null) { 706 pixels = new float[w * h * numBands]; 707 } else { 708 pixels = fArray; 709 } 710 711 for (int i = y; i < y + h; i++) { 712 for (int j = x; j < x + w; j++) { 713 for (int n = 0; n < numBands; n++) { 714 pixels[idx++] = getSampleFloat(j, i, n, data); 715 } 716 } 717 } 718 return pixels; 719 } 720 721 /** 722 * Sets all of the samples for a rectangular area of pixels of the 723 * DataBuffer from a float array. 724 * 725 * @param x 726 * the X coordinate of the rectangle of pixels. 727 * @param y 728 * the Y coordinate of the rectangle of pixels. 729 * @param w 730 * the width of the rectangle of pixels. 731 * @param h 732 * the height of the rectangle of pixels. 733 * @param fArray 734 * the float array. 735 * @param data 736 * the image data. 737 */ 738 public void setPixels(int x, int y, int w, int h, float fArray[], DataBuffer data) { 739 if (x < 0 || y < 0 || x + w > this.width || y + h > this.height) { 740 // awt.63=Coordinates are not in bounds 741 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 742 } 743 int idx = 0; 744 for (int i = y; i < y + h; i++) { 745 for (int j = x; j < x + w; j++) { 746 for (int n = 0; n < numBands; n++) { 747 setSample(j, i, n, fArray[idx++], data); 748 } 749 } 750 } 751 } 752 753 /** 754 * Gets the samples of the specified rectangular area of pixels as a double 755 * array. 756 * 757 * @param x 758 * the X coordinate of the rectangle of pixels. 759 * @param y 760 * the Y coordinate of the rectangle of pixels. 761 * @param w 762 * the width of the rectangle of pixels. 763 * @param h 764 * the height of the rectangle of pixels. 765 * @param dArray 766 * the double array where result will be stored. 767 * @param data 768 * the image data. 769 * @return the double array with the samples of the specified rectangular 770 * area of pixels. 771 */ 772 public double[] getPixels(int x, int y, int w, int h, double dArray[], DataBuffer data) { 773 if (x < 0 || y < 0 || x + w > this.width || y + h > this.height) { 774 // awt.63=Coordinates are not in bounds 775 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 776 } 777 double pixels[]; 778 int idx = 0; 779 780 if (dArray == null) { 781 pixels = new double[w * h * numBands]; 782 } else { 783 pixels = dArray; 784 } 785 786 for (int i = y; i < y + h; i++) { 787 for (int j = x; j < x + w; j++) { 788 for (int n = 0; n < numBands; n++) { 789 pixels[idx++] = getSampleDouble(j, i, n, data); 790 } 791 } 792 } 793 return pixels; 794 } 795 796 /** 797 * Sets all of the samples for a rectangular area of pixels of the 798 * DataBuffer from a double array. 799 * 800 * @param x 801 * the X coordinate of the rectangle of pixels. 802 * @param y 803 * the Y coordinate of the rectangle of pixels. 804 * @param w 805 * the width of the rectangle of pixels. 806 * @param h 807 * the height of the rectangle of pixels. 808 * @param dArray 809 * the double array. 810 * @param data 811 * the image data. 812 */ 813 public void setPixels(int x, int y, int w, int h, double dArray[], DataBuffer data) { 814 if (x < 0 || y < 0 || x + w > this.width || y + h > this.height) { 815 // awt.63=Coordinates are not in bounds 816 throw new ArrayIndexOutOfBoundsException(Messages.getString("awt.63")); //$NON-NLS-1$ 817 } 818 int idx = 0; 819 for (int i = y; i < y + h; i++) { 820 for (int j = x; j < x + w; j++) { 821 for (int n = 0; n < numBands; n++) { 822 setSample(j, i, n, dArray[idx++], data); 823 } 824 } 825 } 826 } 827 828 /** 829 * Sets a sample of the specified band for the specified pixel in the 830 * DataBuffer as integer value. 831 * 832 * @param x 833 * the X coordinate of the pixel. 834 * @param y 835 * the Y coordinate of the pixel. 836 * @param b 837 * the specified band. 838 * @param s 839 * the sample as an integer value. 840 * @param data 841 * the image data. 842 */ 843 public abstract void setSample(int x, int y, int b, int s, DataBuffer data); 844 845 /** 846 * Gets the samples of a specified band for a specified rectangular area of 847 * pixels as a integer array. 848 * 849 * @param x 850 * the X coordinate of the rectangle. 851 * @param y 852 * the Y coordinate of the rectangle. 853 * @param w 854 * the width of the rectangle. 855 * @param h 856 * the height of the rectangle. 857 * @param b 858 * the specified band. 859 * @param iArray 860 * the integer array where result will be stored. 861 * @param data 862 * the image data. 863 * @return the samples of a specified band for a specified rectangular area 864 * of pixels. 865 */ 866 public int[] getSamples(int x, int y, int w, int h, int b, int iArray[], DataBuffer data) { 867 int samples[]; 868 int idx = 0; 869 870 if (iArray == null) { 871 samples = new int[w * h]; 872 } else { 873 samples = iArray; 874 } 875 876 for (int i = y; i < y + h; i++) { 877 for (int j = x; j < x + w; j++) { 878 samples[idx++] = getSample(j, i, b, data); 879 } 880 } 881 882 return samples; 883 } 884 885 /** 886 * Sets the samples from an integer array in the specified band for the 887 * specified rectangle of pixels. 888 * 889 * @param x 890 * the X coordinate of the rectangle. 891 * @param y 892 * the Y coordinate of the rectangle. 893 * @param w 894 * the width of the rectangle. 895 * @param h 896 * the height of the rectangle. 897 * @param b 898 * the specified band. 899 * @param iArray 900 * the integer array. 901 * @param data 902 * the image data. 903 */ 904 public void setSamples(int x, int y, int w, int h, int b, int iArray[], DataBuffer data) { 905 int idx = 0; 906 for (int i = y; i < y + h; i++) { 907 for (int j = x; j < x + w; j++) { 908 setSample(j, i, b, iArray[idx++], data); 909 } 910 } 911 } 912 913 /** 914 * Gets the samples of a specified band for a specified rectangular area of 915 * pixels as a float array. 916 * 917 * @param x 918 * the X coordinate of the rectangle. 919 * @param y 920 * the Y coordinate of the rectangle. 921 * @param w 922 * the width of the rectangle. 923 * @param h 924 * the height of the rectangle. 925 * @param b 926 * the specified band. 927 * @param fArray 928 * the float array where result will be stored. 929 * @param data 930 * the image data. 931 * @return the samples of a specified band for a specified rectangular area 932 * of pixels. 933 */ 934 public float[] getSamples(int x, int y, int w, int h, int b, float fArray[], DataBuffer data) { 935 float samples[]; 936 int idx = 0; 937 938 if (fArray == null) { 939 samples = new float[w * h]; 940 } else { 941 samples = fArray; 942 } 943 944 for (int i = y; i < y + h; i++) { 945 for (int j = x; j < x + w; j++) { 946 samples[idx++] = getSampleFloat(j, i, b, data); 947 } 948 } 949 950 return samples; 951 } 952 953 /** 954 * Sets the samples from an float array in the specified band for the 955 * specified rectangle of pixels. 956 * 957 * @param x 958 * the X coordinate of the rectangle. 959 * @param y 960 * the Y coordinate of the rectangle. 961 * @param w 962 * the width of the rectangle. 963 * @param h 964 * the height of the rectangle. 965 * @param b 966 * the specified band. 967 * @param fArray 968 * the float array. 969 * @param data 970 * the image data. 971 */ 972 public void setSamples(int x, int y, int w, int h, int b, float fArray[], DataBuffer data) { 973 int idx = 0; 974 for (int i = y; i < y + h; i++) { 975 for (int j = x; j < x + w; j++) { 976 setSample(j, i, b, fArray[idx++], data); 977 } 978 } 979 } 980 981 /** 982 * Gets the samples of a specified band for a specified rectangular area of 983 * pixels as a double array. 984 * 985 * @param x 986 * the X coordinate of the rectangle. 987 * @param y 988 * the Y coordinate of the rectangle. 989 * @param w 990 * the width of the rectangle. 991 * @param h 992 * the height of the rectangle. 993 * @param b 994 * the specified band. 995 * @param dArray 996 * the double array where result will be stored. 997 * @param data 998 * the image data. 999 * @return the samples of a specified band for a specified rectangular area 1000 * of pixels. 1001 */ 1002 public double[] getSamples(int x, int y, int w, int h, int b, double dArray[], DataBuffer data) { 1003 double samples[]; 1004 int idx = 0; 1005 1006 if (dArray == null) { 1007 samples = new double[w * h]; 1008 } else { 1009 samples = dArray; 1010 } 1011 1012 for (int i = y; i < y + h; i++) { 1013 for (int j = x; j < x + w; j++) { 1014 samples[idx++] = getSampleDouble(j, i, b, data); 1015 } 1016 } 1017 1018 return samples; 1019 } 1020 1021 /** 1022 * Sets the samples from an double array in the specified band for the 1023 * specified rectangle of pixels. 1024 * 1025 * @param x 1026 * the X coordinate of the rectangle. 1027 * @param y 1028 * the Y coordinate of the rectangle. 1029 * @param w 1030 * the width of the rectangle. 1031 * @param h 1032 * the height of the rectangle. 1033 * @param b 1034 * the specified band. 1035 * @param dArray 1036 * the double array. 1037 * @param data 1038 * the image data. 1039 */ 1040 public void setSamples(int x, int y, int w, int h, int b, double dArray[], DataBuffer data) { 1041 int idx = 0; 1042 for (int i = y; i < y + h; i++) { 1043 for (int j = x; j < x + w; j++) { 1044 setSample(j, i, b, dArray[idx++], data); 1045 } 1046 } 1047 } 1048 1049 /** 1050 * Sets a sample of the specified band for the specified pixel in the 1051 * DataBuffer as float value. 1052 * 1053 * @param x 1054 * the X coordinate of the pixel. 1055 * @param y 1056 * the Y coordinate of the pixel. 1057 * @param b 1058 * the specified band. 1059 * @param s 1060 * the sample as float value. 1061 * @param data 1062 * the image data. 1063 */ 1064 public void setSample(int x, int y, int b, float s, DataBuffer data) { 1065 setSample(x, y, b, (int)s, data); 1066 } 1067 1068 /** 1069 * Sets a sample of the specified band for the specified pixel in the 1070 * DataBuffer as double value. 1071 * 1072 * @param x 1073 * the X coordinate of the pixel. 1074 * @param y 1075 * the Y coordinate of the pixel. 1076 * @param b 1077 * the specified band. 1078 * @param s 1079 * the sample as double value. 1080 * @param data 1081 * the image data. 1082 */ 1083 public void setSample(int x, int y, int b, double s, DataBuffer data) { 1084 setSample(x, y, b, (int)s, data); 1085 } 1086 1087 /** 1088 * Creates a DataBuffer object which corresponds to the SampleModel. 1089 * 1090 * @return the DataBuffer object which corresponds to the SampleModel. 1091 */ 1092 public abstract DataBuffer createDataBuffer(); 1093 1094 /** 1095 * Gets the sample size in bits for the specified band. 1096 * 1097 * @param band 1098 * the specified band. 1099 * @return the sample size in bits for the specified band. 1100 */ 1101 public abstract int getSampleSize(int band); 1102 1103 /** 1104 * Gets an array of the sample size in bits for all bands. 1105 * 1106 * @return an array of the sample size in bits for all bands. 1107 */ 1108 public abstract int[] getSampleSize(); 1109 1110 /** 1111 * Gets the width of the image data of this SampleModel object. 1112 * 1113 * @return the width of the image data of this SampleModel object. 1114 */ 1115 public final int getWidth() { 1116 return width; 1117 } 1118 1119 /** 1120 * Gets the transfer type used to transfer pixels via the getDataElements 1121 * and setDataElements methods. Transfer type value can be one of the 1122 * predefined type from DataBuffer class or not. 1123 * 1124 * @return the transfer type. 1125 */ 1126 public int getTransferType() { 1127 return dataType; 1128 } 1129 1130 /** 1131 * Returns the number of data elements for pixel transferring via the 1132 * getDataElements and setDataElements methods. 1133 * 1134 * @return the number of data elements for pixel transferring via the 1135 * getDataElements and setDataElements methods. 1136 */ 1137 public abstract int getNumDataElements(); 1138 1139 /** 1140 * Gets the number of bands in the image data of this SampleModel object. 1141 * 1142 * @return the number of bands in the image data of this SampleModel object. 1143 */ 1144 public final int getNumBands() { 1145 return numBands; 1146 } 1147 1148 /** 1149 * Gets the height of the image data of this SampleModel object. 1150 * 1151 * @return the height of the image data of this SampleModel object. 1152 */ 1153 public final int getHeight() { 1154 return height; 1155 } 1156 1157 /** 1158 * Gets the data type of image data of this SampleModel object. 1159 * 1160 * @return the data type of image data of this SampleModel object. 1161 */ 1162 public final int getDataType() { 1163 return dataType; 1164 } 1165 1166 } 1167