1 7 8 17 18 package java.awt.image; 19 import java.awt.Rectangle ; 20 import java.awt.Point ; 21 22 31 public class WritableRaster extends Raster { 32 33 45 protected WritableRaster(SampleModel sampleModel, 46 Point origin) { 47 this(sampleModel, 48 sampleModel.createDataBuffer(), 49 new Rectangle (origin.x, 50 origin.y, 51 sampleModel.getWidth(), 52 sampleModel.getHeight()), 53 origin, 54 null); 55 } 56 57 70 protected WritableRaster(SampleModel sampleModel, 71 DataBuffer dataBuffer, 72 Point origin) { 73 this(sampleModel, 74 dataBuffer, 75 new Rectangle (origin.x, 76 origin.y, 77 sampleModel.getWidth(), 78 sampleModel.getHeight()), 79 origin, 80 null); 81 } 82 83 106 protected WritableRaster(SampleModel sampleModel, 107 DataBuffer dataBuffer, 108 Rectangle aRegion, 109 Point sampleModelTranslate, 110 WritableRaster parent){ 111 super(sampleModel,dataBuffer,aRegion,sampleModelTranslate,parent); 112 } 113 114 119 public WritableRaster getWritableParent() { 120 return (WritableRaster )parent; 121 } 122 123 138 public WritableRaster createWritableTranslatedChild(int childMinX, 139 int childMinY) { 140 return createWritableChild(minX,minY,width,height, 141 childMinX,childMinY,null); 142 } 143 144 199 public WritableRaster createWritableChild(int parentX, int parentY, 200 int w, int h, 201 int childMinX, int childMinY, 202 int bandList[]) { 203 if (parentX < this.minX) { 204 throw new RasterFormatException ("parentX lies outside raster"); 205 } 206 if (parentY < this.minY) { 207 throw new RasterFormatException ("parentY lies outside raster"); 208 } 209 if ((parentX+w < parentX) || (parentX+w > this.width + this.minX)) { 210 throw new RasterFormatException ("(parentX + width) is outside raster"); 211 } 212 if ((parentY+h < parentY) || (parentY+h > this.height + this.minY)) { 213 throw new RasterFormatException ("(parentY + height) is outside raster"); 214 } 215 216 SampleModel sm; 217 if (bandList != null) { 223 sm = sampleModel.createSubsetSampleModel(bandList); 224 } 225 else { 226 sm = sampleModel; 227 } 228 229 int deltaX = childMinX - parentX; 230 int deltaY = childMinY - parentY; 231 232 return new WritableRaster (sm, 236 getDataBuffer(), 237 new Rectangle (childMinX,childMinY, 238 w, h), 239 new Point (sampleModelTranslateX+deltaX, 240 sampleModelTranslateY+deltaY), 241 this); 242 } 243 244 267 public void setDataElements(int x, int y, Object inData) { 268 sampleModel.setDataElements(x-sampleModelTranslateX, 269 y-sampleModelTranslateY, 270 inData, dataBuffer); 271 } 272 273 291 public void setDataElements(int x, int y, Raster inRaster) { 292 int dstOffX = x+inRaster.getMinX(); 293 int dstOffY = y+inRaster.getMinY(); 294 int width = inRaster.getWidth(); 295 int height = inRaster.getHeight(); 296 if ((dstOffX < this.minX) || (dstOffY < this.minY) || 297 (dstOffX + width > this.minX + this.width) || 298 (dstOffY + height > this.minY + this.height)) { 299 throw new ArrayIndexOutOfBoundsException 300 ("Coordinate out of bounds!"); 301 } 302 303 int srcOffX = inRaster.getMinX(); 304 int srcOffY = inRaster.getMinY(); 305 Object tdata = null; 306 307 for (int startY=0; startY < height; startY++) { 308 tdata = inRaster.getDataElements(srcOffX, srcOffY+startY, 309 width, 1, tdata); 310 setDataElements(dstOffX, dstOffY+startY, 311 width, 1, tdata); 312 } 313 } 314 315 342 public void setDataElements(int x, int y, int w, int h, Object inData) { 343 sampleModel.setDataElements(x-sampleModelTranslateX, 344 y-sampleModelTranslateY, 345 w,h,inData,dataBuffer); 346 } 347 348 387 public void setRect(Raster srcRaster) { 388 setRect(0,0,srcRaster); 389 } 390 391 409 public void setRect(int dx, int dy, Raster srcRaster) { 410 int width = srcRaster.getWidth(); 411 int height = srcRaster.getHeight(); 412 int srcOffX = srcRaster.getMinX(); 413 int srcOffY = srcRaster.getMinY(); 414 int dstOffX = dx+srcOffX; 415 int dstOffY = dy+srcOffY; 416 417 if (dstOffX < this.minX) { 419 int skipX = this.minX - dstOffX; 420 width -= skipX; 421 srcOffX += skipX; 422 dstOffX = this.minX; 423 } 424 if (dstOffY < this.minY) { 425 int skipY = this.minY - dstOffY; 426 height -= skipY; 427 srcOffY += skipY; 428 dstOffY = this.minY; 429 } 430 if (dstOffX+width > this.minX+this.width) { 431 width = this.minX + this.width - dstOffX; 432 } 433 if (dstOffY+height > this.minY+this.height) { 434 height = this.minY + this.height - dstOffY; 435 } 436 437 if (width <= 0 || height <= 0) { 438 return; 439 } 440 441 switch (srcRaster.getSampleModel().getDataType()) { 442 case DataBuffer.TYPE_BYTE: 443 case DataBuffer.TYPE_SHORT: 444 case DataBuffer.TYPE_USHORT: 445 case DataBuffer.TYPE_INT: 446 int[] iData = null; 447 for (int startY=0; startY < height; startY++) { 448 iData = 450 srcRaster.getPixels(srcOffX, srcOffY+startY, width, 1, 451 iData); 452 setPixels(dstOffX, dstOffY+startY, width, 1, iData); 453 } 454 break; 455 456 case DataBuffer.TYPE_FLOAT: 457 float[] fData = null; 458 for (int startY=0; startY < height; startY++) { 459 fData = 460 srcRaster.getPixels(srcOffX, srcOffY+startY, width, 1, 461 fData); 462 setPixels(dstOffX, dstOffY+startY, width, 1, fData); 463 } 464 break; 465 466 case DataBuffer.TYPE_DOUBLE: 467 double[] dData = null; 468 for (int startY=0; startY < height; startY++) { 469 dData = 471 srcRaster.getPixels(srcOffX, srcOffY+startY, width, 1, 472 dData); 473 setPixels(dstOffX, dstOffY+startY, width, 1, dData); 474 } 475 break; 476 } 477 } 478 479 492 public void setPixel(int x, int y, int iArray[]) { 493 sampleModel.setPixel(x-sampleModelTranslateX,y-sampleModelTranslateY, 494 iArray,dataBuffer); 495 } 496 497 510 public void setPixel(int x, int y, float fArray[]) { 511 sampleModel.setPixel(x-sampleModelTranslateX,y-sampleModelTranslateY, 512 fArray,dataBuffer); 513 } 514 515 528 public void setPixel(int x, int y, double dArray[]) { 529 sampleModel.setPixel(x-sampleModelTranslateX,y-sampleModelTranslateY, 530 dArray,dataBuffer); 531 } 532 533 549 public void setPixels(int x, int y, int w, int h, int iArray[]) { 550 sampleModel.setPixels(x-sampleModelTranslateX,y-sampleModelTranslateY, 551 w,h,iArray,dataBuffer); 552 } 553 554 570 public void setPixels(int x, int y, int w, int h, float fArray[]) { 571 sampleModel.setPixels(x-sampleModelTranslateX,y-sampleModelTranslateY, 572 w,h,fArray,dataBuffer); 573 } 574 575 591 public void setPixels(int x, int y, int w, int h, double dArray[]) { 592 sampleModel.setPixels(x-sampleModelTranslateX,y-sampleModelTranslateY, 593 w,h,dArray,dataBuffer); 594 } 595 596 610 public void setSample(int x, int y, int b, int s) { 611 sampleModel.setSample(x-sampleModelTranslateX, 612 y-sampleModelTranslateY, b, s, 613 dataBuffer); 614 } 615 616 630 public void setSample(int x, int y, int b, float s){ 631 sampleModel.setSample(x-sampleModelTranslateX,y-sampleModelTranslateY, 632 b,s,dataBuffer); 633 } 634 635 649 public void setSample(int x, int y, int b, double s){ 650 sampleModel.setSample(x-sampleModelTranslateX,y-sampleModelTranslateY, 651 b,s,dataBuffer); 652 } 653 654 672 public void setSamples(int x, int y, int w, int h, int b, 673 int iArray[]) { 674 sampleModel.setSamples(x-sampleModelTranslateX,y-sampleModelTranslateY, 675 w,h,b,iArray,dataBuffer); 676 } 677 678 696 public void setSamples(int x, int y, int w, int h, int b, 697 float fArray[]) { 698 sampleModel.setSamples(x-sampleModelTranslateX,y-sampleModelTranslateY, 699 w,h,b,fArray,dataBuffer); 700 } 701 702 720 public void setSamples(int x, int y, int w, int h, int b, 721 double dArray[]) { 722 sampleModel.setSamples(x-sampleModelTranslateX,y-sampleModelTranslateY, 723 w,h,b,dArray,dataBuffer); 724 } 725 726 } 727 | Popular Tags |