1 7 8 17 18 package java.awt.image; 19 20 47 48 49 public final class BandedSampleModel extends ComponentSampleModel 50 { 51 52 66 public BandedSampleModel(int dataType, int w, int h, int numBands) { 67 super(dataType, w, h, 1, w, 68 BandedSampleModel.createIndicesArray(numBands), 69 BandedSampleModel.createOffsetArray(numBands)); 70 } 71 72 88 public BandedSampleModel(int dataType, 89 int w, int h, 90 int scanlineStride, 91 int bankIndices[], 92 int bandOffsets[]) { 93 94 super(dataType, w, h, 1,scanlineStride, bankIndices, bandOffsets); 95 } 96 97 115 public SampleModel createCompatibleSampleModel(int w, int h) { 116 int[] bandOffs; 117 118 if (numBanks == 1) { 119 bandOffs = orderBands(bandOffsets, w*h); 120 } 121 else { 122 bandOffs = new int[bandOffsets.length]; 123 } 124 125 SampleModel sampleModel = 126 new BandedSampleModel (dataType, w, h, w, bankIndices, bandOffs); 127 return sampleModel; 128 } 129 130 142 public SampleModel createSubsetSampleModel(int bands[]) { 143 if (bands.length > bankIndices.length) 144 throw new RasterFormatException ("There are only " + 145 bankIndices.length + 146 " bands"); 147 int newBankIndices[] = new int[bands.length]; 148 int newBandOffsets[] = new int[bands.length]; 149 150 for (int i=0; i<bands.length; i++) { 151 newBankIndices[i] = bankIndices[bands[i]]; 152 newBandOffsets[i] = bandOffsets[bands[i]]; 153 } 154 155 return new BandedSampleModel (this.dataType, width, height, 156 this.scanlineStride, 157 newBankIndices, newBandOffsets); 158 } 159 160 167 public DataBuffer createDataBuffer() { 168 DataBuffer dataBuffer = null; 169 170 int size = scanlineStride * height; 171 switch (dataType) { 172 case DataBuffer.TYPE_BYTE: 173 dataBuffer = new DataBufferByte (size, numBanks); 174 break; 175 case DataBuffer.TYPE_USHORT: 176 dataBuffer = new DataBufferUShort (size, numBanks); 177 break; 178 case DataBuffer.TYPE_SHORT: 179 dataBuffer = new DataBufferShort (size, numBanks); 180 break; 181 case DataBuffer.TYPE_INT: 182 dataBuffer = new DataBufferInt (size, numBanks); 183 break; 184 case DataBuffer.TYPE_FLOAT: 185 dataBuffer = new DataBufferFloat (size, numBanks); 186 break; 187 case DataBuffer.TYPE_DOUBLE: 188 dataBuffer = new DataBufferDouble (size, numBanks); 189 break; 190 default: 191 throw new IllegalArgumentException ("dataType is not one " + 192 "of the supported types."); 193 } 194 195 return dataBuffer; 196 } 197 198 199 237 public Object getDataElements(int x, int y, Object obj, DataBuffer data) { 238 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 239 throw new ArrayIndexOutOfBoundsException 240 ("Coordinate out of bounds!"); 241 } 242 int type = getTransferType(); 243 int numDataElems = getNumDataElements(); 244 int pixelOffset = y*scanlineStride + x; 245 246 switch(type) { 247 248 case DataBuffer.TYPE_BYTE: 249 250 byte[] bdata; 251 252 if (obj == null) { 253 bdata = new byte[numDataElems]; 254 } else { 255 bdata = (byte[])obj; 256 } 257 258 for (int i=0; i<numDataElems; i++) { 259 bdata[i] = (byte)data.getElem(bankIndices[i], 260 pixelOffset + bandOffsets[i]); 261 } 262 263 obj = (Object )bdata; 264 break; 265 266 case DataBuffer.TYPE_USHORT: 267 case DataBuffer.TYPE_SHORT: 268 269 short[] sdata; 270 271 if (obj == null) { 272 sdata = new short[numDataElems]; 273 } else { 274 sdata = (short[])obj; 275 } 276 277 for (int i=0; i<numDataElems; i++) { 278 sdata[i] = (short)data.getElem(bankIndices[i], 279 pixelOffset + bandOffsets[i]); 280 } 281 282 obj = (Object )sdata; 283 break; 284 285 case DataBuffer.TYPE_INT: 286 287 int[] idata; 288 289 if (obj == null) { 290 idata = new int[numDataElems]; 291 } else { 292 idata = (int[])obj; 293 } 294 295 for (int i=0; i<numDataElems; i++) { 296 idata[i] = data.getElem(bankIndices[i], 297 pixelOffset + bandOffsets[i]); 298 } 299 300 obj = (Object )idata; 301 break; 302 303 case DataBuffer.TYPE_FLOAT: 304 305 float[] fdata; 306 307 if (obj == null) { 308 fdata = new float[numDataElems]; 309 } else { 310 fdata = (float[])obj; 311 } 312 313 for (int i=0; i<numDataElems; i++) { 314 fdata[i] = data.getElemFloat(bankIndices[i], 315 pixelOffset + bandOffsets[i]); 316 } 317 318 obj = (Object )fdata; 319 break; 320 321 case DataBuffer.TYPE_DOUBLE: 322 323 double[] ddata; 324 325 if (obj == null) { 326 ddata = new double[numDataElems]; 327 } else { 328 ddata = (double[])obj; 329 } 330 331 for (int i=0; i<numDataElems; i++) { 332 ddata[i] = data.getElemDouble(bankIndices[i], 333 pixelOffset + bandOffsets[i]); 334 } 335 336 obj = (Object )ddata; 337 break; 338 } 339 340 return obj; 341 } 342 343 353 public int[] getPixel(int x, int y, int iArray[], DataBuffer data) { 354 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 355 throw new ArrayIndexOutOfBoundsException 356 ("Coordinate out of bounds!"); 357 } 358 359 int[] pixels; 360 361 if (iArray != null) { 362 pixels = iArray; 363 } else { 364 pixels = new int [numBands]; 365 } 366 367 int pixelOffset = y*scanlineStride + x; 368 for (int i=0; i<numBands; i++) { 369 pixels[i] = data.getElem(bankIndices[i], 370 pixelOffset + bandOffsets[i]); 371 } 372 return pixels; 373 } 374 375 388 public int[] getPixels(int x, int y, int w, int h, 389 int iArray[], DataBuffer data) { 390 if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) { 391 throw new ArrayIndexOutOfBoundsException 392 ("Coordinate out of bounds!"); 393 } 394 int[] pixels; 395 396 if (iArray != null) { 397 pixels = iArray; 398 } else { 399 pixels = new int[w*h*numBands]; 400 } 401 402 for (int k = 0; k < numBands; k++) { 403 int lineOffset = y*scanlineStride + x + bandOffsets[k]; 404 int srcOffset = k; 405 int bank = bankIndices[k]; 406 407 for (int i = 0; i < h; i++) { 408 int pixelOffset = lineOffset; 409 for (int j = 0; j < w; j++) { 410 pixels[srcOffset] = data.getElem(bank, pixelOffset++); 411 srcOffset += numBands; 412 } 413 lineOffset += scanlineStride; 414 } 415 } 416 return pixels; 417 } 418 419 430 public int getSample(int x, int y, int b, DataBuffer data) { 431 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 433 throw new ArrayIndexOutOfBoundsException 434 ("Coordinate out of bounds!"); 435 } 436 int sample = 437 data.getElem(bankIndices[b], 438 y*scanlineStride + x + bandOffsets[b]); 439 return sample; 440 } 441 442 453 public float getSampleFloat(int x, int y, int b, DataBuffer data) { 454 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 456 throw new ArrayIndexOutOfBoundsException 457 ("Coordinate out of bounds!"); 458 } 459 460 float sample = data.getElemFloat(bankIndices[b], 461 y*scanlineStride + x + bandOffsets[b]); 462 return sample; 463 } 464 465 476 public double getSampleDouble(int x, int y, int b, DataBuffer data) { 477 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 479 throw new ArrayIndexOutOfBoundsException 480 ("Coordinate out of bounds!"); 481 } 482 483 double sample = data.getElemDouble(bankIndices[b], 484 y*scanlineStride + x + bandOffsets[b]); 485 return sample; 486 } 487 488 503 public int[] getSamples(int x, int y, int w, int h, int b, 504 int iArray[], DataBuffer data) { 505 if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) { 507 throw new ArrayIndexOutOfBoundsException 508 ("Coordinate out of bounds!"); 509 } 510 int samples[]; 511 if (iArray != null) { 512 samples = iArray; 513 } else { 514 samples = new int [w*h]; 515 } 516 517 int lineOffset = y*scanlineStride + x + bandOffsets[b]; 518 int srcOffset = 0; 519 int bank = bankIndices[b]; 520 521 for (int i = 0; i < h; i++) { 522 int sampleOffset = lineOffset; 523 for (int j = 0; j < w; j++) { 524 samples[srcOffset++] = data.getElem(bank, sampleOffset++); 525 } 526 lineOffset += scanlineStride; 527 } 528 return samples; 529 } 530 531 565 public void setDataElements(int x, int y, Object obj, DataBuffer data) { 566 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 567 throw new ArrayIndexOutOfBoundsException 568 ("Coordinate out of bounds!"); 569 } 570 int type = getTransferType(); 571 int numDataElems = getNumDataElements(); 572 int pixelOffset = y*scanlineStride + x; 573 574 switch(type) { 575 576 case DataBuffer.TYPE_BYTE: 577 578 byte[] barray = (byte[])obj; 579 580 for (int i=0; i<numDataElems; i++) { 581 data.setElem(bankIndices[i], pixelOffset + bandOffsets[i], 582 barray[i] & 0xff); 583 } 584 break; 585 586 case DataBuffer.TYPE_USHORT: 587 case DataBuffer.TYPE_SHORT: 588 589 short[] sarray = (short[])obj; 590 591 for (int i=0; i<numDataElems; i++) { 592 data.setElem(bankIndices[i], pixelOffset + bandOffsets[i], 593 sarray[i] & 0xffff); 594 } 595 break; 596 597 case DataBuffer.TYPE_INT: 598 599 int[] iarray = (int[])obj; 600 601 for (int i=0; i<numDataElems; i++) { 602 data.setElem(bankIndices[i], pixelOffset + bandOffsets[i], 603 iarray[i]); 604 } 605 break; 606 607 case DataBuffer.TYPE_FLOAT: 608 609 float[] farray = (float[])obj; 610 611 for (int i=0; i<numDataElems; i++) { 612 data.setElemFloat(bankIndices[i], pixelOffset + bandOffsets[i], 613 farray[i]); 614 } 615 break; 616 617 case DataBuffer.TYPE_DOUBLE: 618 619 double[] darray = (double[])obj; 620 621 for (int i=0; i<numDataElems; i++) { 622 data.setElemDouble(bankIndices[i], pixelOffset + bandOffsets[i], 623 darray[i]); 624 } 625 break; 626 627 } 628 } 629 630 639 public void setPixel(int x, int y, int iArray[], DataBuffer data) { 640 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 641 throw new ArrayIndexOutOfBoundsException 642 ("Coordinate out of bounds!"); 643 } 644 int pixelOffset = y*scanlineStride + x; 645 for (int i=0; i<numBands; i++) { 646 data.setElem(bankIndices[i], pixelOffset + bandOffsets[i], 647 iArray[i]); 648 } 649 } 650 651 663 public void setPixels(int x, int y, int w, int h, 664 int iArray[], DataBuffer data) { 665 if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) { 666 throw new ArrayIndexOutOfBoundsException 667 ("Coordinate out of bounds!"); 668 } 669 670 for (int k = 0; k < numBands; k++) { 671 int lineOffset = y*scanlineStride + x + bandOffsets[k]; 672 int srcOffset = k; 673 int bank = bankIndices[k]; 674 675 for (int i = 0; i < h; i++) { 676 int pixelOffset = lineOffset; 677 for (int j = 0; j < w; j++) { 678 data.setElem(bank, pixelOffset++, iArray[srcOffset]); 679 srcOffset += numBands; 680 } 681 lineOffset += scanlineStride; 682 } 683 } 684 } 685 686 697 public void setSample(int x, int y, int b, int s, 698 DataBuffer data) { 699 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 701 throw new ArrayIndexOutOfBoundsException 702 ("Coordinate out of bounds!"); 703 } 704 data.setElem(bankIndices[b], 705 y*scanlineStride + x + bandOffsets[b], s); 706 } 707 708 719 public void setSample(int x, int y, int b, 720 float s , 721 DataBuffer data) { 722 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 724 throw new ArrayIndexOutOfBoundsException 725 ("Coordinate out of bounds!"); 726 } 727 data.setElemFloat(bankIndices[b], 728 y*scanlineStride + x + bandOffsets[b], s); 729 } 730 731 742 public void setSample(int x, int y, int b, 743 double s, 744 DataBuffer data) { 745 if ((x < 0) || (y < 0) || (x >= width) || (y >= height)) { 747 throw new ArrayIndexOutOfBoundsException 748 ("Coordinate out of bounds!"); 749 } 750 data.setElemDouble(bankIndices[b], 751 y*scanlineStride + x + bandOffsets[b], s); 752 } 753 754 767 public void setSamples(int x, int y, int w, int h, int b, 768 int iArray[], DataBuffer data) { 769 if ((x < 0) || (y < 0) || (x + w > width) || (y + h > height)) { 771 throw new ArrayIndexOutOfBoundsException 772 ("Coordinate out of bounds!"); 773 } 774 int lineOffset = y*scanlineStride + x + bandOffsets[b]; 775 int srcOffset = 0; 776 int bank = bankIndices[b]; 777 778 for (int i = 0; i < h; i++) { 779 int sampleOffset = lineOffset; 780 for (int j = 0; j < w; j++) { 781 data.setElem(bank, sampleOffset++, iArray[srcOffset++]); 782 } 783 lineOffset += scanlineStride; 784 } 785 } 786 787 private static int[] createOffsetArray(int numBands) { 788 int[] bandOffsets = new int[numBands]; 789 for (int i=0; i < numBands; i++) { 790 bandOffsets[i] = 0; 791 } 792 return bandOffsets; 793 } 794 795 private static int[] createIndicesArray(int numBands) { 796 int[] bankIndices = new int[numBands]; 797 for (int i=0; i < numBands; i++) { 798 bankIndices[i] = i; 799 } 800 return bankIndices; 801 } 802 803 public int hashCode() { 805 return super.hashCode() ^ 0x2; 806 } 807 } 808 | Popular Tags |