1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 import java.awt.Point ; 21 import java.awt.Rectangle ; 22 import java.awt.image.ColorModel ; 23 import java.awt.image.DataBufferInt ; 24 import java.awt.image.Raster ; 25 import java.awt.image.SampleModel ; 26 import java.awt.image.WritableRaster ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.apache.batik.ext.awt.image.GraphicsUtil; 31 import org.apache.batik.util.HaltingThread; 32 33 42 public abstract class AbstractTiledRed 43 extends AbstractRed 44 implements TileGenerator { 45 46 private TileStore tiles; 47 48 private static int defaultTileSize = 128; 49 public static int getDefaultTileSize() { return defaultTileSize; } 50 51 57 protected AbstractTiledRed() { } 58 59 60 67 protected AbstractTiledRed(Rectangle bounds, Map props) { 68 super(bounds, props); 69 } 70 71 78 protected AbstractTiledRed(CachableRed src, Map props) { 79 super(src, props); 80 } 81 82 90 protected AbstractTiledRed(CachableRed src, Rectangle bounds, Map props) { 91 super(src, bounds, props); 92 } 93 94 107 protected AbstractTiledRed(CachableRed src, Rectangle bounds, 108 ColorModel cm, SampleModel sm, 109 Map props) { 110 super(src, bounds, cm, sm, props); 111 } 112 113 130 protected AbstractTiledRed(CachableRed src, Rectangle bounds, 131 ColorModel cm, SampleModel sm, 132 int tileGridXOff, int tileGridYOff, 133 Map props) { 134 super(src, bounds, cm, sm, tileGridXOff, tileGridYOff, props); 135 } 136 137 157 protected void init(CachableRed src, Rectangle bounds, 158 ColorModel cm, SampleModel sm, 159 int tileGridXOff, int tileGridYOff, 160 Map props) { 161 init(src, bounds, cm, sm, tileGridXOff, tileGridYOff, null, props); 162 } 163 164 165 186 protected void init(CachableRed src, Rectangle bounds, 187 ColorModel cm, SampleModel sm, 188 int tileGridXOff, int tileGridYOff, 189 TileStore tiles, 190 Map props) { 191 super.init(src, bounds, cm, sm, tileGridXOff, tileGridYOff, props); 192 this.tiles = tiles; 193 if (this.tiles == null) 194 this.tiles = createTileStore(); 195 } 196 197 206 protected AbstractTiledRed(List srcs, Rectangle bounds, Map props) { 207 super(srcs, bounds, props); 208 } 209 210 225 protected AbstractTiledRed(List srcs, Rectangle bounds, 226 ColorModel cm, SampleModel sm, 227 Map props) { 228 super(srcs, bounds, cm, sm, props); 229 } 230 231 250 protected AbstractTiledRed(List srcs, Rectangle bounds, 251 ColorModel cm, SampleModel sm, 252 int tileGridXOff, int tileGridYOff, 253 Map props) { 254 super(srcs, bounds, cm, sm, tileGridXOff, tileGridYOff, props); 255 } 256 257 275 protected void init(List srcs, Rectangle bounds, 276 ColorModel cm, SampleModel sm, 277 int tileGridXOff, int tileGridYOff, 278 Map props) { 279 super.init(srcs, bounds, cm, sm, tileGridXOff, tileGridYOff, props); 280 tiles = createTileStore(); 281 } 282 283 public TileStore getTileStore() { 284 return tiles; 285 } 286 287 protected void setTileStore(TileStore tiles) { 288 this.tiles = tiles; 289 } 290 291 protected TileStore createTileStore() { 292 return TileCache.getTileMap(this); 293 } 294 295 public WritableRaster copyData(WritableRaster wr) { 296 copyToRasterByBlocks(wr); 297 return wr; 298 } 299 300 301 public Raster getData(Rectangle rect) { 302 int xt0 = getXTile(rect.x); 303 int xt1 = getXTile(rect.x+rect.width-1); 304 int yt0 = getYTile(rect.y); 305 int yt1 = getYTile(rect.y+rect.height-1); 306 307 if ((xt0 == xt1) && (yt0 == yt1)) { 308 Raster r = getTile(xt0, yt0); 309 return r.createChild(rect.x, rect.y, rect.width, rect.height, 310 rect.x, rect.y, null); 311 } 312 return super.getData(rect); 314 } 315 316 317 public Raster getTile(int x, int y) { 318 return tiles.getTile(x, y); 319 } 320 321 public Raster genTile(int x, int y) { 322 WritableRaster wr = makeTile(x, y); 323 genRect(wr); 324 return wr; 325 } 326 327 public abstract void genRect(WritableRaster wr); 328 330 331 public void setTile(int x, int y, Raster ras) { 332 tiles.setTile(x, y, ras); 333 } 334 335 public void copyToRasterByBlocks(WritableRaster wr) { 336 final boolean is_INT_PACK = 337 GraphicsUtil.is_INT_PACK_Data(getSampleModel(), false); 338 339 Rectangle bounds = getBounds(); 340 Rectangle wrR = wr.getBounds(); 341 342 int tx0 = getXTile(wrR.x); 343 int ty0 = getYTile(wrR.y); 344 int tx1 = getXTile(wrR.x+wrR.width -1); 345 int ty1 = getYTile(wrR.y+wrR.height-1); 346 347 if (tx0 < minTileX) tx0 = minTileX; 348 if (ty0 < minTileY) ty0 = minTileY; 349 350 if (tx1 >= minTileX+numXTiles) tx1 = minTileX+numXTiles-1; 351 if (ty1 >= minTileY+numYTiles) ty1 = minTileY+numYTiles-1; 352 353 if ((tx1 < tx0) || (ty1 < ty0)) 354 return; 355 356 359 int insideTx0 = tx0; 360 int insideTx1 = tx1; 361 362 int insideTy0 = ty0; 363 int insideTy1 = ty1; 364 365 int tx, ty; 367 tx = tx0*tileWidth+tileGridXOff; 368 if ((tx < wrR.x) && (bounds.x != wrR.x)) 369 insideTx0++; 371 372 ty= ty0*tileHeight+tileGridYOff; 373 if ((ty < wrR.y) && (bounds.y != wrR.y)) 374 insideTy0++; 376 377 tx= (tx1+1)*tileWidth+tileGridXOff-1; 378 if ((tx >= (wrR.x+wrR.width)) && 379 ((bounds.x+bounds.width) != (wrR.x+wrR.width))) 380 insideTx1--; 382 383 ty= (ty1+1)*tileHeight+tileGridYOff-1; 384 if ((ty >= (wrR.y+wrR.height)) && 385 ((bounds.y+bounds.height) != (wrR.y+wrR.height))) 386 insideTy1--; 388 389 int xtiles = insideTx1-insideTx0+1; 390 int ytiles = insideTy1-insideTy0+1; 391 boolean [] occupied = null; 392 if ((xtiles > 0) && (ytiles > 0)) 393 occupied = new boolean[xtiles*ytiles]; 394 395 boolean [] got = new boolean[2*(tx1-tx0+1) + 2*(ty1-ty0+1)]; 396 int idx = 0; 397 int numFound = 0; 398 for (int y=ty0; y<=ty1; y++) { 400 for (int x=tx0; x<=tx1; x++) { 401 Raster ras = tiles.getTileNoCompute(x, y); 402 boolean found = (ras != null); 403 if ((y>=insideTy0) && (y<=insideTy1) && 404 (x>=insideTx0) && (x<=insideTx1)) 405 occupied[(x-insideTx0)+(y-insideTy0)*xtiles] = found; 406 else 407 got[idx++] = found; 408 409 if (!found) continue; 410 411 numFound++; 412 413 if (is_INT_PACK) 414 GraphicsUtil.copyData_INT_PACK(ras, wr); 415 else 416 GraphicsUtil.copyData_FALLBACK(ras, wr); 417 } 418 } 419 420 423 if ((xtiles > 0) && (ytiles > 0)) { 425 TileBlock block = new TileBlock 426 (insideTx0, insideTy0, xtiles, ytiles, occupied, 427 0, 0, xtiles, ytiles); 428 drawBlock(block, wr); 430 } 433 434 if (HaltingThread.hasBeenHalted()) 436 return; 437 438 idx = 0; 439 for (ty=ty0; ty<=ty1; ty++) { 441 442 for (tx=tx0; tx<=tx1; tx++) { 443 Raster ras = tiles.getTileNoCompute(tx, ty); 445 446 if ((ty>=insideTy0) && (ty<=insideTy1) && 447 (tx>=insideTx0) && (tx<=insideTx1)) { 448 449 if (ras != null) continue; 450 451 WritableRaster tile = makeTile(tx, ty); 454 if (is_INT_PACK) 455 GraphicsUtil.copyData_INT_PACK(wr, tile); 456 else 457 GraphicsUtil.copyData_FALLBACK(wr, tile); 458 459 tiles.setTile(tx, ty, tile); 460 } 461 else { 462 if (got[idx++]) continue; 463 464 466 ras = getTile(tx, ty); if (HaltingThread.hasBeenHalted()) 469 return; 470 471 if (is_INT_PACK) 472 GraphicsUtil.copyData_INT_PACK(ras, wr); 473 else 474 GraphicsUtil.copyData_FALLBACK(ras, wr); 475 } 476 } 477 } 478 479 } 481 482 488 public void copyToRaster(WritableRaster wr) { 489 Rectangle wrR = wr.getBounds(); 490 491 int tx0 = getXTile(wrR.x); 492 int ty0 = getYTile(wrR.y); 493 int tx1 = getXTile(wrR.x+wrR.width -1); 494 int ty1 = getYTile(wrR.y+wrR.height-1); 495 496 if (tx0 < minTileX) tx0 = minTileX; 497 if (ty0 < minTileY) ty0 = minTileY; 498 499 if (tx1 >= minTileX+numXTiles) tx1 = minTileX+numXTiles-1; 500 if (ty1 >= minTileY+numYTiles) ty1 = minTileY+numYTiles-1; 501 502 final boolean is_INT_PACK = 503 GraphicsUtil.is_INT_PACK_Data(getSampleModel(), false); 504 505 int xtiles = (tx1-tx0+1); 506 boolean [] got = new boolean[xtiles*(ty1-ty0+1)]; 507 508 for (int y=ty0; y<=ty1; y++) 511 for (int x=tx0; x<=tx1; x++) { 512 Raster r = tiles.getTileNoCompute(x, y); 513 if (r == null) continue; 515 got[x-tx0 + (y-ty0)*xtiles] = true; 516 517 if (is_INT_PACK) 518 GraphicsUtil.copyData_INT_PACK(r, wr); 519 else 520 GraphicsUtil.copyData_FALLBACK(r, wr); 521 } 522 523 for (int y=ty0; y<=ty1; y++) 525 for (int x=tx0; x<=tx1; x++) { 526 if (got[x-tx0 + (y-ty0)*xtiles]) continue; 528 Raster r = getTile(x, y); 529 if (is_INT_PACK) 530 GraphicsUtil.copyData_INT_PACK(r, wr); 531 else 532 GraphicsUtil.copyData_FALLBACK(r, wr); 533 } 534 } 535 536 protected void drawBlock(TileBlock block, WritableRaster wr) { 537 TileBlock [] blocks = block.getBestSplit(); 538 if (blocks == null) 539 return; 540 541 drawBlockInPlace(blocks, wr); 542 } 543 544 protected void drawBlockAndCopy(TileBlock []blocks, WritableRaster wr) { 545 if (blocks.length == 1) { 546 TileBlock curr = blocks[0]; 547 int xloc = curr.getXLoc()*tileWidth +tileGridXOff; 548 int yloc = curr.getYLoc()*tileHeight+tileGridYOff; 549 if ((xloc == wr.getMinX()) && 550 (yloc == wr.getMinY())) { 551 drawBlockInPlace(blocks, wr); 553 return; 554 } 555 } 556 557 int maxSz=0; 558 for (int i=0; i<blocks.length; i++) { 559 int sz = ((blocks[i].getWidth() *tileWidth)* 560 (blocks[i].getHeight()*tileHeight)); 561 if (sz > maxSz) maxSz=sz; 562 } 563 DataBufferInt dbi = new DataBufferInt (maxSz); 564 int [] masks = { 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000 }; 565 boolean use_INT_PACK = GraphicsUtil.is_INT_PACK_Data 566 (wr.getSampleModel(), false); 567 568 for (int i=0; i<blocks.length; i++) { 569 TileBlock curr = blocks[i]; 570 int xloc = curr.getXLoc()*tileWidth +tileGridXOff; 571 int yloc = curr.getYLoc()*tileHeight+tileGridYOff; 572 Rectangle tb = new Rectangle (xloc, yloc, 573 curr.getWidth()*tileWidth, 574 curr.getHeight()*tileHeight); 575 tb = tb.intersection(bounds); 576 Point loc = new Point (tb.x, tb.y); 577 WritableRaster child = Raster.createPackedRaster 578 (dbi, tb.width, tb.height, tb.width, masks, loc); 579 genRect(child); 580 if (use_INT_PACK) GraphicsUtil.copyData_INT_PACK(child, wr); 581 else GraphicsUtil.copyData_FALLBACK(child, wr); 582 583 if (HaltingThread.hasBeenHalted()) 585 return; 586 } 587 } 588 589 590 protected void drawBlockInPlace(TileBlock [] blocks, WritableRaster wr) { 591 593 for (int i=0; i<blocks.length; i++) { 594 TileBlock curr = blocks[i]; 595 596 598 int xloc = curr.getXLoc()*tileWidth +tileGridXOff; 599 int yloc = curr.getYLoc()*tileHeight+tileGridYOff; 600 Rectangle tb = new Rectangle (xloc, yloc, 601 curr.getWidth()*tileWidth, 602 curr.getHeight()*tileHeight); 603 tb = tb.intersection(bounds); 604 605 WritableRaster child = 606 wr.createWritableChild(tb.x, tb.y, tb.width, tb.height, 607 tb.x, tb.y, null); 608 genRect(child); 610 611 if (HaltingThread.hasBeenHalted()) 613 return; 614 } 615 } 616 } 617 618 | Popular Tags |