I implemented image rendering for the new Java2D. This was easier than I
thought. I implemented all draw*Image() methods, so that they finally
end up calling drawRenderedImageImpl() (this of course works only on
RenderableImage and RenderedImage so far), which then uses an ImagePaint
to fetch the (transformed) pixels from the source image, and feed the
rectangle together with this ImagePaint into the normal rendering
pipeline, which has the effect that we get the clipping, transformation
and compositing at no additional cost.

Using this I can now draw the following: 

http://kennke.org/~roman/java2d-image2.png

which is a BMP image read by the BMP ImageIO plugin, rendered scaled and
rotated, and have my 'Hello World' polygon+rectangle+text composited
over it.

2006-05-10  Roman Kennke <[EMAIL PROTECTED]>

        * gnu/java/awt/java2d/AbstractGraphics2D.java
        (drawImage(Image,AffineTransform,ImageObserver)): Implemented.
        (drawImageImpl(Image,AffineTransform,ImageObserver,Rectangle)):
        New method.
        (drawImage(BufferedImage,BufferedImageOp,int,int)): Implemented.
        (drawRenderedImage(RenderedImage,AffineTransform)): Implemented.

(drawRenderedImageImpl(RenderedImage,AffineTransform,Rectangle)):
        New method.
        (drawRenderableImage(RenderableImage,AffineTransform)):
Implemented.

(drawRenderableImageImpl(RenderableImage,AffineTransform,Rectangle)):
        New method.
        (scale): Inverse transform by doing 1/scale instead of -scale.
        (drawImage(Image,int,int,ImageObserver)): Implemented.
        (drawImage(Image,int,int,int,int,ImageObserver)): Implemented.
        (drawImage(Image,int,int,Color,ImageObserver)): Implemented.
        (drawImage(Image,int,int,int,int,Color,ImageObserver)):
Implemented.

(drawImage(Image,int,int,int,int,int,int,int,int,ImageObserver)):
        Implemented.

(drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver)):
        Implemented.
        (fillScanline): Work on translated destination raster for
        correct compositin.
        (init): Fetch the clip after the destination raster is
initialized.
        * gnu/java/awt/java2d/ImagePaint.java: New file.
        * gnu/java/awt/java2d/RasterGraphics
        (drawImage): Removed.

/Roman

-- 
“Improvement makes straight roads, but the crooked roads, without
Improvement, are roads of Genius.” - William Blake
Index: gnu/java/awt/java2d/AbstractGraphics2D.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/java2d/AbstractGraphics2D.java,v
retrieving revision 1.6
diff -u -1 -0 -r1.6 AbstractGraphics2D.java
--- gnu/java/awt/java2d/AbstractGraphics2D.java	8 May 2006 14:39:17 -0000	1.6
+++ gnu/java/awt/java2d/AbstractGraphics2D.java	10 May 2006 09:11:39 -0000
@@ -217,42 +217,220 @@
 //    Shape clipped = clipShape(strokedShape);
 //    if (clipped != null)
 //      {
 //        // Fill the shape.
 //        fillShape(clipped, false);
 //      }
     // FIXME: Clipping doesn't seem to work.
     fillShape(strokedShape, false);
   }
 
+
+  /**
+   * Draws the specified image and apply the transform for image space ->
+   * user space conversion.
+   *
+   * This method is implemented to special case RenderableImages and
+   * RenderedImages and delegate to
+   * [EMAIL PROTECTED] #drawRenderableImage(RenderableImage, AffineTransform)} and
+   * [EMAIL PROTECTED] #drawRenderedImage(RenderedImage, AffineTransform)} accordingly.
+   * Other image types are not yet handled.
+   *
+   * @param image the image to be rendered
+   * @param xform the transform from image space to user space
+   * @param obs the image observer to be notified
+   */
   public boolean drawImage(Image image, AffineTransform xform, ImageObserver obs)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    boolean ret = false;
+    Rectangle areaOfInterest = new Rectangle(0, 0, image.getWidth(obs),
+                                             image.getHeight(obs));
+    return drawImageImpl(image, xform, obs, areaOfInterest);
   }
 
+  /**
+   * Draws the specified image and apply the transform for image space ->
+   * user space conversion. This method only draw the part of the image
+   * specified by <code>areaOfInterest</code>.
+   *
+   * This method is implemented to special case RenderableImages and
+   * RenderedImages and delegate to
+   * [EMAIL PROTECTED] #drawRenderableImage(RenderableImage, AffineTransform)} and
+   * [EMAIL PROTECTED] #drawRenderedImage(RenderedImage, AffineTransform)} accordingly.
+   * Other image types are not yet handled.
+   *
+   * @param image the image to be rendered
+   * @param xform the transform from image space to user space
+   * @param obs the image observer to be notified
+   * @param areaOfInterest the area in image space that is rendered
+   */
+  private boolean drawImageImpl(Image image, AffineTransform xform,
+                             ImageObserver obs, Rectangle areaOfInterest)
+  {
+    boolean ret;
+    if (image == null)
+      {
+        ret = true;
+      }
+    else if (image instanceof RenderedImage)
+      {
+        // FIXME: Handle the ImageObserver.
+        drawRenderedImageImpl((RenderedImage) image, xform, areaOfInterest);
+        ret = true;
+      }
+    else if (image instanceof RenderableImage)
+      {
+        // FIXME: Handle the ImageObserver.
+        drawRenderableImageImpl((RenderableImage) image, xform, areaOfInterest);
+        ret = true;
+      }
+    else
+      {
+        // FIXME: Implement rendering of other Image types.
+        ret = false;
+      }
+    return ret;
+  }
+
+  /**
+   * Renders a BufferedImage and applies the specified BufferedImageOp before
+   * to filter the BufferedImage somehow. The resulting BufferedImage is then
+   * passed on to [EMAIL PROTECTED] #drawRenderedImage(RenderedImage, AffineTransform)}
+   * to perform the final rendering.
+   *
+   * @param image the source buffered image
+   * @param op the filter to apply to the buffered image before rendering
+   * @param x the x coordinate to render the image to 
+   * @param y the y coordinate to render the image to 
+   */
   public void drawImage(BufferedImage image, BufferedImageOp op, int x, int y)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    BufferedImage filtered =
+      op.createCompatibleDestImage(image, image.getColorModel());
+    AffineTransform t = new AffineTransform();
+    t.translate(x, y);
+    drawRenderedImage(filtered, t);
   }
 
+  /**
+   * Renders the specified image to the destination raster. The specified
+   * transform is used to convert the image into user space. The transform
+   * of this AbstractGraphics2D object is used to transform from user space
+   * to device space.
+   * 
+   * The rendering is performed using the scanline algorithm that performs the
+   * rendering of other shapes and a custom Paint implementation, that supplies
+   * the pixel values of the rendered image.
+   *
+   * @param image the image to render to the destination raster
+   * @param xform the transform from image space to user space
+   */
   public void drawRenderedImage(RenderedImage image, AffineTransform xform)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    Rectangle areaOfInterest = new Rectangle(image.getMinX(),
+                                             image.getHeight(),
+                                             image.getWidth(),
+                                             image.getHeight());
+    drawRenderedImageImpl(image, xform, areaOfInterest);
   }
 
+  /**
+   * Renders the specified image to the destination raster. The specified
+   * transform is used to convert the image into user space. The transform
+   * of this AbstractGraphics2D object is used to transform from user space
+   * to device space. Only the area specified by <code>areaOfInterest</code>
+   * is finally rendered to the target.
+   * 
+   * The rendering is performed using the scanline algorithm that performs the
+   * rendering of other shapes and a custom Paint implementation, that supplies
+   * the pixel values of the rendered image.
+   *
+   * @param image the image to render to the destination raster
+   * @param xform the transform from image space to user space
+   */
+  private void drawRenderedImageImpl(RenderedImage image,
+                                     AffineTransform xform,
+                                     Rectangle areaOfInterest)
+  {
+    // First we compute the transformation. This is made up of 3 parts:
+    // 1. The areaOfInterest -> image space transform.
+    // 2. The image space -> user space transform.
+    // 3. The user space -> device space transform.
+    AffineTransform t = new AffineTransform();
+    t.translate(- areaOfInterest.x - image.getMinX(),
+                - areaOfInterest.y - image.getMinY());
+    t.concatenate(xform);
+    t.concatenate(transform);
+    AffineTransform it = null;
+    try
+      {
+        it = t.createInverse();
+      }
+    catch (NoninvertibleTransformException ex)
+      {
+        // Ignore -- we return if the transform is not invertible.
+      }
+    if (it != null)
+      {
+        // Transform the area of interest into user space.
+        GeneralPath aoi = new GeneralPath(areaOfInterest);
+        aoi.transform(xform);
+        // Render the shape using the standard renderer, but with a temporary
+        // ImagePaint.
+        ImagePaint p = new ImagePaint(image, it);
+        Paint savedPaint = paint;
+        try
+          {
+            paint = p;
+            fillShape(aoi, false);
+          }
+        finally
+          {
+            paint = savedPaint;
+          }
+      }
+  }
+
+  /**
+   * Renders a renderable image. This produces a RenderedImage, which is
+   * then passed to [EMAIL PROTECTED] #drawRenderedImage(RenderedImage, AffineTransform)}
+   * to perform the final rendering.
+   *
+   * @param image the renderable image to be rendered
+   * @param xform the transform from image space to user space
+   */
   public void drawRenderableImage(RenderableImage image, AffineTransform xform)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    Rectangle areaOfInterest = new Rectangle((int) image.getMinX(),
+                                             (int) image.getHeight(),
+                                             (int) image.getWidth(),
+                                             (int) image.getHeight());
+    drawRenderableImageImpl(image, xform, areaOfInterest);
+                                                       
+  }
+
+  /**
+   * Renders a renderable image. This produces a RenderedImage, which is
+   * then passed to [EMAIL PROTECTED] #drawRenderedImage(RenderedImage, AffineTransform)}
+   * to perform the final rendering. Only the area of the image specified
+   * by <code>areaOfInterest</code> is rendered.
+   *
+   * @param image the renderable image to be rendered
+   * @param xform the transform from image space to user space
+   */
+  private void drawRenderableImageImpl(RenderableImage image,
+                                       AffineTransform xform,
+                                       Rectangle areaOfInterest)
+  {
+    // TODO: Maybe make more clever usage of a RenderContext here.
+    RenderedImage rendered = image.createDefaultRendering();
+    drawRenderedImageImpl(rendered, xform, areaOfInterest);
   }
 
   /**
    * Draws the specified string at the specified location.
    *
    * @param text the string to draw
    * @param x the x location, relative to the bounding rectangle of the text
    * @param y the y location, relative to the bounding rectangle of the text
    */
   public void drawString(String text, int x, int y)
@@ -527,21 +705,21 @@
    *
    * @param scaleX the factor by which to scale the X axis
    * @param scaleY the factor by which to scale the Y axis
    */
   public void scale(double scaleX, double scaleY)
   {
     transform.scale(scaleX, scaleY);
     if (clip != null)
       {
         AffineTransform clipTransform = new AffineTransform();
-        clipTransform.scale(-scaleX, -scaleY);
+        clipTransform.scale(1 / scaleX, 1 / scaleY);
         updateClip(clipTransform);
       }
     updateOptimization();
   }
 
   /**
    * Shears the coordinate system by <code>shearX</code> and
    * <code>shearY</code>.
    *
    * @param shearX the X shearing
@@ -1077,61 +1255,160 @@
   }
 
   /**
    * Fills the outline of a polygon.
    */
   public void fillPolygon(int[] xPoints, int[] yPoints, int npoints)
   {
     fill(new Polygon(xPoints, yPoints, npoints));
   }
 
+  /**
+   * Draws the specified image at the specified location. This forwards
+   * to [EMAIL PROTECTED] #drawImage(Image, AffineTransform, ImageObserver)}.
+   *
+   * @param image the image to render
+   * @param x the x location to render to
+   * @param y the y location to render to
+   * @param observer the image observer to receive notification
+   */
   public boolean drawImage(Image image, int x, int y, ImageObserver observer)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    AffineTransform t = new AffineTransform();
+    t.translate(x, y);
+    return drawImage(image, t, observer);
   }
 
+  /**
+   * Draws the specified image at the specified location. The image
+   * is scaled to the specified width and height. This forwards
+   * to [EMAIL PROTECTED] #drawImage(Image, AffineTransform, ImageObserver)}.
+   *
+   * @param image the image to render
+   * @param x the x location to render to
+   * @param y the y location to render to
+   * @param width the target width of the image
+   * @param height the target height of the image
+   * @param observer the image observer to receive notification
+   */
   public boolean drawImage(Image image, int x, int y, int width, int height,
                            ImageObserver observer)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    AffineTransform t = new AffineTransform();
+    t.translate(x, y);
+    double scaleX = (double) image.getWidth(observer) / (double) width;
+    double scaleY = (double) image.getHeight(observer) / (double) height;
+    t.scale(scaleX, scaleY);
+    return drawImage(image, t, observer);
   }
 
+  /**
+   * Draws the specified image at the specified location. This forwards
+   * to [EMAIL PROTECTED] #drawImage(Image, AffineTransform, ImageObserver)}.
+   *
+   * @param image the image to render
+   * @param x the x location to render to
+   * @param y the y location to render to
+   * @param bgcolor the background color to use for transparent pixels
+   * @param observer the image observer to receive notification
+   */
   public boolean drawImage(Image image, int x, int y, Color bgcolor,
                            ImageObserver observer)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    AffineTransform t = new AffineTransform();
+    t.translate(x, y);
+    // TODO: Somehow implement the background option.
+    return drawImage(image, t, observer);
   }
 
+  /**
+   * Draws the specified image at the specified location. The image
+   * is scaled to the specified width and height. This forwards
+   * to [EMAIL PROTECTED] #drawImage(Image, AffineTransform, ImageObserver)}.
+   *
+   * @param image the image to render
+   * @param x the x location to render to
+   * @param y the y location to render to
+   * @param width the target width of the image
+   * @param height the target height of the image
+   * @param bgcolor the background color to use for transparent pixels
+   * @param observer the image observer to receive notification
+   */
   public boolean drawImage(Image image, int x, int y, int width, int height,
                            Color bgcolor, ImageObserver observer)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    AffineTransform t = new AffineTransform();
+    t.translate(x, y);
+    double scaleX = (double) image.getWidth(observer) / (double) width;
+    double scaleY = (double) image.getHeight(observer) / (double) height;
+    t.scale(scaleX, scaleY);
+    // TODO: Somehow implement the background option.
+    return drawImage(image, t, observer);
   }
 
+  /**
+   * Draws an image fragment to a rectangular area of the target.
+   *
+   * @param image the image to render
+   * @param dx1 the first corner of the destination rectangle
+   * @param dy1 the first corner of the destination rectangle
+   * @param dx2 the second corner of the destination rectangle
+   * @param dy2 the second corner of the destination rectangle
+   * @param sx1 the first corner of the source rectangle
+   * @param sy1 the first corner of the source rectangle
+   * @param sx2 the second corner of the source rectangle
+   * @param sy2 the second corner of the source rectangle
+   * @param observer the image observer to be notified
+   */
   public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2,
                            int sx1, int sy1, int sx2, int sy2,
                            ImageObserver observer)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    int sx = Math.min(sx1, sx1);
+    int sy = Math.min(sy1, sy2);
+    int sw = Math.abs(sx1 - sx2);
+    int sh = Math.abs(sy1 - sy2);
+    int dx = Math.min(dx1, dx1);
+    int dy = Math.min(dy1, dy2);
+    int dw = Math.abs(dx1 - dx2);
+    int dh = Math.abs(dy1 - dy2);
+    
+    AffineTransform t = new AffineTransform();
+    t.translate(sx - dx, sy - dy);
+    double scaleX = (double) sw / (double) dw;
+    double scaleY = (double) sh / (double) dh;
+    t.scale(scaleX, scaleY);
+    Rectangle areaOfInterest = new Rectangle(sx, sy, sw, sh);
+    return drawImageImpl(image, t, observer, areaOfInterest);
   }
 
+  /**
+   * Draws an image fragment to a rectangular area of the target.
+   *
+   * @param image the image to render
+   * @param dx1 the first corner of the destination rectangle
+   * @param dy1 the first corner of the destination rectangle
+   * @param dx2 the second corner of the destination rectangle
+   * @param dy2 the second corner of the destination rectangle
+   * @param sx1 the first corner of the source rectangle
+   * @param sy1 the first corner of the source rectangle
+   * @param sx2 the second corner of the source rectangle
+   * @param sy2 the second corner of the source rectangle
+   * @param bgcolor the background color to use for transparent pixels
+   * @param observer the image observer to be notified
+   */
   public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2,
                            int sx1, int sy1, int sx2, int sy2, Color bgcolor,
                            ImageObserver observer)
   {
-    // FIXME: Implement this.
-    throw new UnsupportedOperationException("Not yet implemented");
+    // FIXME: Do something with bgcolor.
+    return drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
   }
 
   /**
    * Disposes this graphics object.
    */
   public void dispose()
   {
     // Nothing special to do here.
   }
 
@@ -1479,21 +1756,22 @@
    * @param x1 the right offset
    * @param y the scanline
    */
   protected void fillScanline(PaintContext pCtx, int x0, int x1, int y)
   {
     Raster paintRaster = pCtx.getRaster(x0, y, x1 - x0, 1);
     ColorModel paintColorModel = pCtx.getColorModel();
     CompositeContext cCtx = composite.createContext(paintColorModel,
                                                     getColorModel(),
                                                     renderingHints);
-    cCtx.compose(paintRaster, destinationRaster, destinationRaster);
+    WritableRaster targetChild = destinationRaster.createWritableTranslatedChild(-x0,- y);
+    cCtx.compose(paintRaster, targetChild, targetChild);
     updateRaster(destinationRaster, x0, y, x1 - x0, 1);
     cCtx.dispose();
   }
 
   /**
    * Fills arbitrary shapes in an anti-aliased fashion.
    *
    * @param segs the line segments which define the shape which is to be filled
    */
   private void fillShapeAntialias(ArrayList segs, Rectangle2D deviceBounds2D,
@@ -1733,22 +2011,22 @@
    * order to correctly initialize the state of this object.
    */
   protected void init()
   {
     setPaint(Color.BLACK);
     setFont(new Font("SansSerif", Font.PLAIN, 12));
     isOptimized = true;
 
     // FIXME: Should not be necessary. A clip of null should mean
     // 'clip against device bounds.
-    clip = getDeviceBounds();
     destinationRaster = getDestinationRaster();
+    clip = getDeviceBounds();
   }
 
   /**
    * Returns a WritableRaster that is used by this class to perform the
    * rendering in. It is not necessary that the target surface immediately
    * reflects changes in the raster. Updates to the raster are notified via
    * [EMAIL PROTECTED] #updateRaster}.
    *
    * @return the destination raster
    */
Index: gnu/java/awt/java2d/RasterGraphics.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/awt/java2d/RasterGraphics.java,v
retrieving revision 1.2
diff -u -1 -0 -r1.2 RasterGraphics.java
--- gnu/java/awt/java2d/RasterGraphics.java	9 May 2006 15:31:15 -0000	1.2
+++ gnu/java/awt/java2d/RasterGraphics.java	10 May 2006 09:11:39 -0000
@@ -32,25 +32,21 @@
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package gnu.java.awt.java2d;
 
 import java.awt.GraphicsConfiguration;
-import java.awt.Image;
-import java.awt.image.BufferedImage;
 import java.awt.image.ColorModel;
-import java.awt.image.ImageObserver;
-import java.awt.image.Raster;
 import java.awt.image.WritableRaster;
 
 /**
  * A Graphics2D implementation that operates on Raster objects. This is
  * primarily used for BufferedImages, but can theoretically be used on
  * arbitrary WritableRasters.
  *
  * @author Roman Kennke ([EMAIL PROTECTED])
  */
 public class RasterGraphics
@@ -97,28 +93,11 @@
   {
     return raster;
   }
 
   public GraphicsConfiguration getDeviceConfiguration()
   {
     // TODO Auto-generated method stub
     return null;
   }
 
-  public boolean drawImage(Image image, int x, int y, ImageObserver observer)
-  {
-    // TODO: Hack to make ImageTest working and evaluate image rendering.
-    // Remove as soon as this is fully supported in AbstractGraphics2D.
-    if (image instanceof BufferedImage)
-      {
-        BufferedImage bImage = (BufferedImage) image;
-        Raster src = bImage.getRaster();
-        int srcX = src.getMinX();
-        int srcY = src.getMinY();
-        int w = src.getWidth();
-        int h = src.getHeight();
-        Object data = src.getDataElements(srcX, srcY, w, h, null);
-        raster.setDataElements(x, y, w, h, data);
-      }
-    return true;
-  }
 }
Index: gnu/java/awt/java2d/ImagePaint.java
===================================================================
RCS file: gnu/java/awt/java2d/ImagePaint.java
diff -N gnu/java/awt/java2d/ImagePaint.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/java/awt/java2d/ImagePaint.java	10 May 2006 09:11:39 -0000
@@ -0,0 +1,192 @@
+/* ImagePaint.java -- Supplies the pixels for image rendering
+   Copyright (C) 2006 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.awt.java2d;
+
+import java.awt.Paint;
+import java.awt.PaintContext;
+import java.awt.Rectangle;
+import java.awt.RenderingHints;
+import java.awt.Transparency;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.ColorModel;
+import java.awt.image.Raster;
+import java.awt.image.RenderedImage;
+import java.awt.image.WritableRaster;
+
+/**
+ * This class is used as a temporary Paint object to supply the pixel values
+ * for image rendering using the normal scanline conversion implementation.
+ *
+ * @author Roman Kennke ([EMAIL PROTECTED])
+ */
+public class ImagePaint
+  implements Paint
+{
+
+  /**
+   * The PaintContext implementation for the ImagePaint.
+   */
+  private class ImagePaintContext
+    implements PaintContext
+  {
+
+    /**
+     * The target raster.
+     */
+    private WritableRaster target;
+
+    /**
+     * Nothing to do here.
+     */
+    public void dispose()
+    {
+      // Nothing to do here.
+    }
+
+    /**
+     * Returns the color model.
+     *
+     * @return the color model
+     */
+    public ColorModel getColorModel()
+    {
+      return image.getColorModel();
+    }
+
+    /**
+     * Supplies the pixel to be rendered.
+     *
+     * @see PaintContext#getRaster(int, int, int, int)
+     */
+    public Raster getRaster(int x1, int y1, int w, int h)
+    {
+      ensureRasterSize(w, h);
+      int x2 = x1 + w;
+      int y2 = y1 + h;
+      float[] src = new float[2];
+      float[] dest = new float[2];
+      Raster source = image.getData();
+      int minX = source.getMinX();
+      int maxX = source.getWidth() + minX;
+      int minY = source.getMinY();
+      int maxY = source.getHeight() + minY;
+      Object pixel = null;
+      for (int y = y1; y < y2; y++)
+        {
+          for (int x = x1; x < x2; x++)
+            {
+              src[0] = x;
+              src[1] = y;
+              transform.transform(src, 0, dest, 0, 1);
+              int dx = (int) dest[0];
+              int dy = (int) dest[1];
+              // Pixels outside the source image are not of interest, skip
+              // them.
+              if (dx >= minX && dx < maxX && dy >= minY && dy < maxY)
+                {
+                  pixel = source.getDataElements(dx, dy, pixel);
+                  target.setDataElements(x - x1, y - y1, pixel);
+                }
+            }
+        }
+      return target;
+    }
+
+    /**
+     * Ensures that the target raster exists and has at least the specified
+     * size.
+     *
+     * @param w the requested target width
+     * @param h the requested target height
+     */
+    private void ensureRasterSize(int w, int h)
+    {
+      if (target == null || target.getWidth() < w || target.getHeight() < h)
+        {
+          Raster s = image.getData();
+          target = s.createCompatibleWritableRaster(w, h);
+        }
+    }
+  }
+
+  /**
+   * The image to render.
+   */
+  RenderedImage image;
+
+  /**
+   * The transform from image space to device space. This is the inversed
+   * transform of the concatenated
+   * transform image space -> user space -> device space transform.
+   */
+  AffineTransform transform;
+
+  /**
+   * Creates a new ImagePaint for rendering the specified image using the
+   * specified device space -> image space transform. This transform
+   * is the inversed transform of the usual image space -> user space -> device
+   * space transform.
+   *
+   * The ImagePaint will only render the image in the specified area of
+   * interest (which is specified in image space).
+   *
+   * @param i the image to render
+   * @param t the device space to user space transform
+   */
+  ImagePaint(RenderedImage i, AffineTransform t)
+  {
+    image = i;
+    transform = t;
+  }
+
+  public PaintContext createContext(ColorModel cm, Rectangle deviceBounds,
+                                    Rectangle2D userBounds,
+                                    AffineTransform xform,
+                                    RenderingHints hints)
+  {
+    return new ImagePaintContext();
+  }
+
+  public int getTransparency()
+  {
+    return Transparency.OPAQUE;
+  }
+
+}

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil

Reply via email to