classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Patch to implement ColorGraphicsContext


From: Ingo Prötel
Subject: Re: Patch to implement ColorGraphicsContext
Date: Wed, 07 Apr 2004 17:34:45 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040113

Hi,

this is my revised patch for ColorPaintContext.

ingo

Thomas Fitzsimmons wrote:
On Mon, 2004-04-05 at 08:59, Ingo Prötel wrote:

I suggest the following patch
2004-04-05  Ingo Proetel  <address@hidden>

        * java/awt/ColorPaintContext.java (<init>): Added ColorModel to 
signature.
        (getColorModel): Return the actual color model.
        (getRaster): Implemented.
        (ColorRaster): New inner class.
        * java/awt/SystemColor.java (createContext): Use ColorModel when 
creating
        a PaintContext.
        * java/awt/Color.java (<init>): Make exception more verbose.
        (createContext): Use ColorModel when creating a PaintContext.
        

ingo



Index: java/awt/ColorPaintContext.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/java/awt/ColorPaintContext.java,v
retrieving revision 1.1
diff -u -r1.1 ColorPaintContext.java
--- java/awt/ColorPaintContext.java     8 May 2002 03:07:24
-0000       1.1
+++ java/awt/ColorPaintContext.java     5 Apr 2004 12:43:00 -0000
@@ -39,7 +39,13 @@
package java.awt;

import java.awt.image.ColorModel;
+import java.awt.image.DataBuffer;
+import java.awt.image.DataBufferByte;
+import java.awt.image.DataBufferInt;
+import java.awt.image.DataBufferUShort;
import java.awt.image.Raster;
+import java.awt.image.SampleModel;
+import java.util.Arrays;

/**
 * This class provides a paint context which will fill a rectanglar
region of
@@ -55,15 +61,19 @@
   * SystemColor.
   */
  final int color;
+  final ColorModel colorModel;

+  private ColorRaster mCachedRaster;
+


We don't use the 'm' prefix on fields.


  /**
   * Create the context for a given color.
   *
   * @param c the solid color to use
   */
-  ColorPaintContext(int c)
+  ColorPaintContext(ColorModel cm,int c)


You should probably create a new constructor (that uses the previous
default colour model), rather than changing the old one.  Color.java,
for example, calls ColorPaintContext(int c).

I added a constructor. But I think it is not useful at all. And people really should not use it because it has the potential of been a big efficiency draw back.

  {
    color = c;
+    colorModel = cm;
  }

  /**
@@ -75,14 +85,13 @@
  }

  /**
-   * Return the color model of this context. This ignores the model
passed
-   * in the request, since colors are always in sRGB.
+ * Return the color model of this context. *
   * @return the context color model
   */
  public ColorModel getColorModel()
  {
-    return ColorModel.getRGBdefault();
+    return colorModel;
  }

  /**
@@ -96,8 +105,71 @@
   */
  public Raster getRaster(int x, int y, int w, int h)
  {
-    // XXX Implement. Sun uses undocumented implementation class
-    // sun.awt.image.IntegerInterleavedRaster.
-    throw new Error("not implemented");
+ if( mCachedRaster == null + || mCachedRaster.getWidth() < w
+       || mCachedRaster.getHeight() < h)
+    {
+     mCachedRaster = new ColorRaster(colorModel, 0, 0, w, h, color);
+    }
+   return mCachedRaster.createChild(0,0,w,h,x,y,null);


                                       ^^^^^^^^^^^^^^^^

Arguments should be separated by spaces.
Ok.


  }
+ + private class ColorRaster extends Raster{ + + /** + * + * @param cm
+         * @param rgbPixel
+         */
+        ColorRaster(ColorModel cm,int x, int y, int width, int
height, int rgbPixel)
+ { + super(cm.createCompatibleSampleModel(width,height),new
Point(x,y));
+          Object pixel = cm.getDataElements(rgbPixel,null);
+          getSampleModel().setDataElements(0,
0,width,height,multiplyData(pixel,null,width*height),dataBuffer);



Lines should be no more than 80 characters long, so lines like this need
to be wrapped.

Ok.


+        }
+ + + + private Object multiplyData(Object src, Object dest, int
factor)
+        {
+          Object from;
+          int srcLength = 0;
+          if (src instanceof byte[])
+           {
+            srcLength = ((byte[])src).length;
+ + if (dest == null) dest = new byte[factor * srcLength];
+          }
+          else if (src instanceof short[])
+           {
+            srcLength = ((short[])src).length;
+            if (dest == null) dest = new short[factor * srcLength];
+          }
+          else if (src instanceof int[])
+           {
+            srcLength = ((int[]) src).length;
+            if (dest == null) dest = new int[factor * srcLength];
+          }
+          else
+           {
+            throw new ClassCastException("Unknown data buffer type");
+          }
+
+          System.arraycopy(src,0,dest,0,srcLength);
+ + int count = 1;
+          while(count*2 < factor)
+          {
+            System.arraycopy(dest, 0, dest, count * srcLength,
count*srcLength);
+ count *= 2; + } + + if(factor > count)
+            System.arraycopy(dest,0, dest, count * srcLength, (factor
- count) * srcLength );
+ + return dest;
+        }


The indentation is a little wonky in this section (e.g. some open/close
braces don't line up).

Fixed. I think.


+ + } + } // class ColorPaintContext


Sorry to nit-pick about formatting.  Don't worry too much about stuff
that's already committed.  One day, hopefully soon, Jalopy will be ready
to reformat Classpath, and we can do this stuff automatically from then
on.

Thanks,
Tom




--
Ingo Prötel                                          address@hidden
aicas GmbH                                        http://www.aicas.com
Haid-und-Neu-Str. 18                        phone   +49 721 663 968-32
76131 Karlsruhe                             fax     +49 721 663 968-93
Germany
Index: java/awt/ColorPaintContext.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/ColorPaintContext.java,v
retrieving revision 1.1
diff -u -r1.1 ColorPaintContext.java
--- java/awt/ColorPaintContext.java     8 May 2002 03:07:24 -0000       1.1
+++ java/awt/ColorPaintContext.java     7 Apr 2004 15:27:09 -0000
@@ -1,5 +1,5 @@
 /* ColorPaintContext.java -- context for painting solid colors
-   Copyright (C) 2002 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2004 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -55,15 +55,31 @@
    * SystemColor.
    */
   final int color;
+  final ColorModel colorModel;
 
+  private ColorRaster cachedRaster;
+
+  
   /**
    * Create the context for a given color.
    *
-   * @param c the solid color to use
+   * @param c The solid color to use.
    */
-  ColorPaintContext(int c)
+  ColorPaintContext(int colorRGB)
   {
-    color = c;
+    this(ColorModel.getRGBdefault(), colorRGB);
+  }
+  
+  /**
+   * Create the context for a given color.
+   *
+   * @param cm The color model of this context. 
+   * @param c The solid color to use.
+   */
+  ColorPaintContext(ColorModel cm,int colorRGB)
+  {
+    color = colorRGB;
+    colorModel = cm;
   }
 
   /**
@@ -75,14 +91,13 @@
   }
 
   /**
-   * Return the color model of this context. This ignores the model passed
-   * in the request, since colors are always in sRGB.
+   * Return the color model of this context. 
    *
    * @return the context color model
    */
   public ColorModel getColorModel()
   {
-    return ColorModel.getRGBdefault();
+    return colorModel;
   }
 
   /**
@@ -94,10 +109,87 @@
    * @param h the height, in device space
    * @return a raster for the given area and color
    */
-  public Raster getRaster(int x, int y, int w, int h)
+  public Raster getRaster(int x, int y, int width, int height)
+  {
+   if(  cachedRaster == null 
+       || cachedRaster.getWidth() < width
+       || cachedRaster.getHeight() < height)
+   {
+     cachedRaster = new ColorRaster(colorModel, 0, 0, width, height, color);
+   }
+   return cachedRaster.createChild(0 ,0 ,width ,height ,x ,y , null);
+  }
+  
+  /**
+   * A ColorRaster is a raster that is completely filled with one color. The 
+   * data layout is taken from the color model given to the constructor.
+   */
+  private class ColorRaster extends Raster
   {
-    // XXX Implement. Sun uses undocumented implementation class
-    // sun.awt.image.IntegerInterleavedRaster.
-    throw new Error("not implemented");
+    
+    /**
+     * Create a raster that is compaltible with the given color model and 
+     * filled with the given color.
+     * @param cm The color model for this raster.
+     * @param x The smallest horizontal corrdinate in the raster.
+     * @param y The smallest vertical coordinate in the raster.
+     * @param width The width of the raster.
+     * @param height The height of the raster.
+     * @param rgbPixel The RGB value of the color for this raster.
+     */
+    ColorRaster(ColorModel cm,int x, int y, int width, int height, int 
rgbPixel)
+    {         
+      super(cm.createCompatibleSampleModel(width,height),new Point(x,y));
+      Object pixel = cm.getDataElements(rgbPixel,null);
+      getSampleModel().setDataElements(0, 0,
+                                       width, height,
+                                       multiplyData(pixel,null,width*height),
+                                       dataBuffer);
+    }
+    
+    
+    
+    private Object multiplyData(Object src, Object dest, int factor)
+    {
+      Object from;
+      int srcLength = 0;
+      if (src instanceof byte[])
+      {
+        srcLength = ((byte[])src).length;
+        
+        if (dest == null) dest = new byte[factor * srcLength];
+      }
+      else if (src instanceof short[])
+      {
+        srcLength = ((short[])src).length;
+        if (dest == null) dest = new short[factor * srcLength];
+      }
+      else if (src instanceof int[])
+      {
+        srcLength = ((int[]) src).length;
+        if (dest == null) dest = new int[factor * srcLength];
+      }
+      else
+      {
+        throw new ClassCastException("Unknown data buffer type");
+      }
+      
+      System.arraycopy(src,0,dest,0,srcLength);
+      
+      int count = 1;
+      while(count*2 < factor)
+      {
+        System.arraycopy(dest, 0, dest, count * srcLength, count*srcLength);
+        count *= 2; 
+      }
+      
+      if(factor > count)
+        System.arraycopy(dest,0, dest, count * srcLength, 
+                         (factor - count) * srcLength );
+      
+      return dest;
+    }
+    
   }
+  
 } // class ColorPaintContext

reply via email to

[Prev in Thread] Current Thread [Next in Thread]