1 18 package org.apache.batik.ext.awt.image.codec; 19 20 import java.awt.Point ; 21 import java.awt.Rectangle ; 22 import java.awt.image.ColorModel ; 23 import java.awt.image.Raster ; 24 import java.awt.image.RenderedImage ; 25 import java.awt.image.SampleModel ; 26 import java.awt.image.WritableRaster ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Iterator ; 30 import java.util.Vector ; 31 32 42 public abstract class SimpleRenderedImage implements RenderedImage { 43 44 45 protected int minX; 46 47 48 protected int minY; 49 50 51 protected int width; 52 53 54 protected int height; 55 56 57 protected int tileWidth; 58 59 60 protected int tileHeight; 61 62 63 protected int tileGridXOffset = 0; 64 65 66 protected int tileGridYOffset = 0; 67 68 69 protected SampleModel sampleModel = null; 70 71 72 protected ColorModel colorModel = null; 73 74 75 protected Vector sources = new Vector (); 76 77 78 protected Hashtable properties = new Hashtable (); 79 80 public SimpleRenderedImage() {} 81 82 83 public int getMinX() { 84 return minX; 85 } 86 87 93 public final int getMaxX() { 94 return getMinX() + getWidth(); 95 } 96 97 98 public int getMinY() { 99 return minY; 100 } 101 102 108 public final int getMaxY() { 109 return getMinY() + getHeight(); 110 } 111 112 113 public int getWidth() { 114 return width; 115 } 116 117 118 public int getHeight() { 119 return height; 120 } 121 122 123 public Rectangle getBounds() { 124 return new Rectangle (getMinX(), getMinY(), 125 getWidth(), getHeight()); 126 } 127 128 129 public int getTileWidth() { 130 return tileWidth; 131 } 132 133 134 public int getTileHeight() { 135 return tileHeight; 136 } 137 138 141 public int getTileGridXOffset() { 142 return tileGridXOffset; 143 } 144 145 148 public int getTileGridYOffset() { 149 return tileGridYOffset; 150 } 151 152 157 public int getMinTileX() { 158 return XToTileX(getMinX()); 159 } 160 161 166 public int getMaxTileX() { 167 return XToTileX(getMaxX() - 1); 168 } 169 170 176 public int getNumXTiles() { 177 return getMaxTileX() - getMinTileX() + 1; 178 } 179 180 185 public int getMinTileY() { 186 return YToTileY(getMinY()); 187 } 188 189 194 public int getMaxTileY() { 195 return YToTileY(getMaxY() - 1); 196 } 197 198 204 public int getNumYTiles() { 205 return getMaxTileY() - getMinTileY() + 1; 206 } 207 208 209 public SampleModel getSampleModel() { 210 return sampleModel; 211 } 212 213 214 public ColorModel getColorModel() { 215 return colorModel; 216 } 217 218 228 public Object getProperty(String name) { 229 name = name.toLowerCase(); 230 return properties.get(name); 231 } 232 233 241 public String [] getPropertyNames() { 242 String [] names = new String [properties.size()]; 243 int index = 0; 244 245 Enumeration e = properties.keys(); 246 while (e.hasMoreElements()) { 247 String name = (String )e.nextElement(); 248 names[index++] = name; 249 } 250 251 return names; 252 } 253 254 267 public String [] getPropertyNames(String prefix) { 268 String propertyNames[] = getPropertyNames(); 269 if (propertyNames == null) { 270 return null; 271 } 272 273 prefix = prefix.toLowerCase(); 274 275 Vector names = new Vector (); 276 for (int i = 0; i < propertyNames.length; i++) { 277 if (propertyNames[i].startsWith(prefix)) { 278 names.addElement(propertyNames[i]); 279 } 280 } 281 282 if (names.size() == 0) { 283 return null; 284 } 285 286 String prefixNames[] = new String [names.size()]; 288 int count = 0; 289 for (Iterator it = names.iterator(); it.hasNext(); ) { 290 prefixNames[count++] = (String )it.next(); 291 } 292 293 return prefixNames; 294 } 295 296 298 303 public static int XToTileX(int x, int tileGridXOffset, int tileWidth) { 304 x -= tileGridXOffset; 305 if (x < 0) { 306 x += 1 - tileWidth; } 308 return x/tileWidth; 309 } 310 311 316 public static int YToTileY(int y, int tileGridYOffset, int tileHeight) { 317 y -= tileGridYOffset; 318 if (y < 0) { 319 y += 1 - tileHeight; } 321 return y/tileHeight; 322 } 323 324 332 public int XToTileX(int x) { 333 return XToTileX(x, getTileGridXOffset(), getTileWidth()); 334 } 335 336 344 public int YToTileY(int y) { 345 return YToTileY(y, getTileGridYOffset(), getTileHeight()); 346 } 347 348 353 public static int tileXToX(int tx, int tileGridXOffset, int tileWidth) { 354 return tx*tileWidth + tileGridXOffset; 355 } 356 357 362 public static int tileYToY(int ty, int tileGridYOffset, int tileHeight) { 363 return ty*tileHeight + tileGridYOffset; 364 } 365 366 374 public int tileXToX(int tx) { 375 return tx*tileWidth + tileGridXOffset; 376 } 377 378 386 public int tileYToY(int ty) { 387 return ty*tileHeight + tileGridYOffset; 388 } 389 390 public Vector getSources() { 391 return null; 392 } 393 394 410 public Raster getData() { 411 Rectangle rect = new Rectangle (getMinX(), getMinY(), 412 getWidth(), getHeight()); 413 return getData(rect); 414 } 415 416 433 public Raster getData(Rectangle bounds) { 434 int startX = XToTileX(bounds.x); 435 int startY = YToTileY(bounds.y); 436 int endX = XToTileX(bounds.x + bounds.width - 1); 437 int endY = YToTileY(bounds.y + bounds.height - 1); 438 Raster tile; 439 440 if ((startX == endX) && (startY == endY)) { 441 tile = getTile(startX, startY); 442 return tile.createChild(bounds.x, bounds.y, 443 bounds.width, bounds.height, 444 bounds.x, bounds.y, null); 445 } else { 446 SampleModel sm = 448 sampleModel.createCompatibleSampleModel(bounds.width, 449 bounds.height); 450 451 WritableRaster dest = 453 Raster.createWritableRaster(sm, bounds.getLocation()); 454 455 for (int j = startY; j <= endY; j++) { 456 for (int i = startX; i <= endX; i++) { 457 tile = getTile(i, j); 458 Rectangle intersectRect = 459 bounds.intersection(tile.getBounds()); 460 Raster liveRaster = tile.createChild(intersectRect.x, 461 intersectRect.y, 462 intersectRect.width, 463 intersectRect.height, 464 intersectRect.x, 465 intersectRect.y, 466 null); 467 dest.setDataElements(0, 0, liveRaster); 468 } 469 } 470 return dest; 471 } 472 } 473 474 491 public WritableRaster copyData(WritableRaster dest) { 492 Rectangle bounds; 493 Raster tile; 494 495 if (dest == null) { 496 bounds = getBounds(); 497 Point p = new Point (minX, minY); 498 499 SampleModel sm = sampleModel.createCompatibleSampleModel( 500 width, height); 501 dest = Raster.createWritableRaster(sm, p); 502 } else { 503 bounds = dest.getBounds(); 504 } 505 506 int startX = XToTileX(bounds.x); 507 int startY = YToTileY(bounds.y); 508 int endX = XToTileX(bounds.x + bounds.width - 1); 509 int endY = YToTileY(bounds.y + bounds.height - 1); 510 511 for (int j = startY; j <= endY; j++) { 512 for (int i = startX; i <= endX; i++) { 513 tile = getTile(i, j); 514 Rectangle intersectRect = 515 bounds.intersection(tile.getBounds()); 516 Raster liveRaster = tile.createChild(intersectRect.x, 517 intersectRect.y, 518 intersectRect.width, 519 intersectRect.height, 520 intersectRect.x, 521 intersectRect.y, 522 null); 523 524 531 dest.setDataElements(0, 0, liveRaster); 532 } 533 } 534 return dest; 535 } 536 } 537 | Popular Tags |