1 /* ImagePaint.java -- Supplies the pixels for image rendering 2 Copyright (C) 2006 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 39 package gnu.java.awt.java2d; 40 41 import java.awt.Paint; 42 import java.awt.PaintContext; 43 import java.awt.Rectangle; 44 import java.awt.RenderingHints; 45 import java.awt.Transparency; 46 import java.awt.geom.AffineTransform; 47 import java.awt.geom.Rectangle2D; 48 import java.awt.image.ColorModel; 49 import java.awt.image.Raster; 50 import java.awt.image.RenderedImage; 51 import java.awt.image.WritableRaster; 52 53 /** 54 * This class is used as a temporary Paint object to supply the pixel values 55 * for image rendering using the normal scanline conversion implementation. 56 * 57 * @author Roman Kennke (kennke@aicas.com) 58 */ 59 public class ImagePaint 60 implements Paint 61 { 62 63 /** 64 * The PaintContext implementation for the ImagePaint. 65 */ 66 private class ImagePaintContext 67 implements PaintContext 68 { 69 70 /** 71 * The target raster. 72 */ 73 private WritableRaster target; 74 75 /** 76 * Nothing to do here. 77 */ dispose()78 public void dispose() 79 { 80 // Nothing to do here. 81 } 82 83 /** 84 * Returns the color model. 85 * 86 * @return the color model 87 */ getColorModel()88 public ColorModel getColorModel() 89 { 90 return image.getColorModel(); 91 } 92 93 /** 94 * Supplies the pixel to be rendered. 95 * 96 * @see PaintContext#getRaster(int, int, int, int) 97 */ getRaster(int x1, int y1, int w, int h)98 public Raster getRaster(int x1, int y1, int w, int h) 99 { 100 ensureRasterSize(w, h); 101 int x2 = x1 + w; 102 int y2 = y1 + h; 103 float[] src = new float[2]; 104 float[] dest = new float[2]; 105 Raster source = image.getData(); 106 int minX = source.getMinX(); 107 int maxX = source.getWidth() + minX; 108 int minY = source.getMinY(); 109 int maxY = source.getHeight() + minY; 110 Object pixel = null; 111 for (int y = y1; y < y2; y++) 112 { 113 for (int x = x1; x < x2; x++) 114 { 115 src[0] = x; 116 src[1] = y; 117 transform.transform(src, 0, dest, 0, 1); 118 int dx = (int) dest[0]; 119 int dy = (int) dest[1]; 120 // Pixels outside the source image are not of interest, skip 121 // them. 122 if (dx >= minX && dx < maxX && dy >= minY && dy < maxY) 123 { 124 pixel = source.getDataElements(dx, dy, pixel); 125 target.setDataElements(x - x1, y - y1, pixel); 126 } 127 } 128 } 129 return target; 130 } 131 132 /** 133 * Ensures that the target raster exists and has at least the specified 134 * size. 135 * 136 * @param w the requested target width 137 * @param h the requested target height 138 */ ensureRasterSize(int w, int h)139 private void ensureRasterSize(int w, int h) 140 { 141 if (target == null || target.getWidth() < w || target.getHeight() < h) 142 { 143 Raster s = image.getData(); 144 target = s.createCompatibleWritableRaster(w, h); 145 } 146 } 147 } 148 149 /** 150 * The image to render. 151 */ 152 RenderedImage image; 153 154 /** 155 * The transform from image space to device space. This is the inversed 156 * transform of the concatenated 157 * transform image space -> user space -> device space transform. 158 */ 159 AffineTransform transform; 160 161 /** 162 * Creates a new ImagePaint for rendering the specified image using the 163 * specified device space -> image space transform. This transform 164 * is the inversed transform of the usual image space -> user space -> device 165 * space transform. 166 * 167 * The ImagePaint will only render the image in the specified area of 168 * interest (which is specified in image space). 169 * 170 * @param i the image to render 171 * @param t the device space to user space transform 172 */ ImagePaint(RenderedImage i, AffineTransform t)173 ImagePaint(RenderedImage i, AffineTransform t) 174 { 175 image = i; 176 transform = t; 177 } 178 createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints)179 public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, 180 Rectangle2D userBounds, 181 AffineTransform xform, 182 RenderingHints hints) 183 { 184 return new ImagePaintContext(); 185 } 186 getTransparency()187 public int getTransparency() 188 { 189 return Transparency.OPAQUE; 190 } 191 192 } 193