1 18 package org.apache.batik.gvt; 19 20 import java.awt.PaintContext ; 21 import java.awt.Rectangle ; 22 import java.awt.RenderingHints ; 23 import java.awt.color.ColorSpace ; 24 import java.awt.geom.AffineTransform ; 25 import java.awt.geom.Rectangle2D ; 26 import java.awt.image.BufferedImage ; 27 import java.awt.image.ColorModel ; 28 import java.awt.image.Raster ; 29 import java.awt.image.RenderedImage ; 30 import java.awt.image.WritableRaster ; 31 import java.awt.image.renderable.RenderContext ; 32 33 import org.apache.batik.ext.awt.image.GraphicsUtil; 34 import org.apache.batik.ext.awt.image.renderable.Filter; 35 import org.apache.batik.ext.awt.image.renderable.TileRable; 36 import org.apache.batik.ext.awt.image.renderable.TileRable8Bit; 37 import org.apache.batik.ext.awt.image.rendered.TileCacheRed; 38 39 46 public class PatternPaintContext implements PaintContext { 47 48 51 private ColorModel rasterCM; 52 53 56 private WritableRaster raster; 57 58 61 private RenderedImage tiled; 62 63 protected AffineTransform usr2dev; 64 65 public AffineTransform getUsr2Dev() { return usr2dev; } 66 67 private static Rectangle EVERYTHING = 68 new Rectangle (Integer.MIN_VALUE/4, Integer.MIN_VALUE/4, 69 Integer.MAX_VALUE/2, Integer.MAX_VALUE/2); 70 71 79 public PatternPaintContext(ColorModel destCM, 80 AffineTransform usr2dev, 81 RenderingHints hints, 82 Filter tile, 83 Rectangle2D patternRegion, 84 boolean overflow) { 85 86 if(usr2dev == null){ 87 throw new IllegalArgumentException (); 88 } 89 90 if(hints == null){ 91 hints = new RenderingHints (null); 92 } 93 94 if(tile == null){ 95 throw new IllegalArgumentException (); 96 } 97 98 this.usr2dev = usr2dev; 99 100 103 TileRable tileRable = new TileRable8Bit(tile, 104 EVERYTHING, 105 patternRegion, 106 overflow); 107 ColorSpace destCS = destCM.getColorSpace(); 108 if (destCS == ColorSpace.getInstance(ColorSpace.CS_sRGB)) 109 tileRable.setColorSpaceLinear(false); 110 else if (destCS == ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)) 111 tileRable.setColorSpaceLinear(true); 112 113 RenderContext rc = new RenderContext (usr2dev, EVERYTHING, hints); 114 tiled = tileRable.createRendering(rc); 115 118 if(tiled != null) { 120 Rectangle2D devRgn = usr2dev.createTransformedShape 121 (patternRegion).getBounds(); 122 if ((devRgn.getWidth() > 128) || 123 (devRgn.getHeight() > 128)) 124 tiled = new TileCacheRed(GraphicsUtil.wrap(tiled), 256, 64); 125 } else { 126 rasterCM = ColorModel.getRGBdefault(); 128 WritableRaster wr; 129 wr = rasterCM.createCompatibleWritableRaster(32, 32); 130 tiled = GraphicsUtil.wrap 131 (new BufferedImage (rasterCM, wr, false, null)); 132 return; 133 } 134 135 rasterCM = tiled.getColorModel(); 136 if (rasterCM.hasAlpha()) { 137 if (destCM.hasAlpha()) 138 rasterCM = GraphicsUtil.coerceColorModel 139 (rasterCM, destCM.isAlphaPremultiplied()); 140 else 141 rasterCM = GraphicsUtil.coerceColorModel(rasterCM, false); 142 } 143 } 144 145 public void dispose(){ 146 raster = null; 147 } 148 149 public ColorModel getColorModel(){ 150 return rasterCM; 151 } 152 153 public Raster getRaster(int x, int y, int width, int height){ 154 155 if ((raster == null) || 158 (raster.getWidth() < width) || 159 (raster.getHeight() < height)) { 160 raster = rasterCM.createCompatibleWritableRaster(width, height); 161 } 162 163 WritableRaster wr 164 = raster.createWritableChild(0, 0, width, height, x, y, null); 165 166 tiled.copyData(wr); 167 GraphicsUtil.coerceData(wr, tiled.getColorModel(), 168 rasterCM.isAlphaPremultiplied()); 169 170 if ((raster.getWidth() == width) && 175 (raster.getHeight() == height)) 176 return raster; 177 178 return wr.createTranslatedChild(0,0); 179 } 180 } 181 | Popular Tags |