classpath
[Top][All Lists]
Advanced

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

Patch to implement ColorGraphicsContext


From: Ingo Prötel
Subject: Patch to implement ColorGraphicsContext
Date: Mon, 05 Apr 2004 14:59:43 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040113

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

--
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/image/ColorModel.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/image/ColorModel.java,v
retrieving revision 1.17
diff -u -r1.17 ColorModel.java
--- java/awt/image/ColorModel.java      24 Jun 2003 17:07:07 -0000      1.17
+++ java/awt/image/ColorModel.java      5 Apr 2004 11:06:05 -0000
@@ -166,7 +166,7 @@
    */
   public static ColorModel getRGBdefault()
   {
-    return new DirectColorModel(8, 0xff0000, 0xff00, 0xff, 0xff000000);
+    return new DirectColorModel(32, 0xff0000, 0xff00, 0xff, 0xff000000);
   }
 
   public final boolean hasAlpha()
@@ -597,7 +597,11 @@
     return null;
   }
     
-  // Typically overridden
+  /**
+   * Checks if the given raster has a compatible data-layout (SampleModel).
+   * @param raster The Raster to test.
+   * @return true if raster is compatible.
+   */ 
   public boolean isCompatibleRaster(Raster raster)
   {
     SampleModel sampleModel = raster.getSampleModel();
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;
+ 
   /**
    * Create the context for a given color.
    *
    * @param c the solid color to use
    */
-  ColorPaintContext(int c)
+  ColorPaintContext(ColorModel cm,int c)
   {
     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);
   }
+  
+  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);
+        }
+               
+        
+        
+        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
Index: java/awt/SystemColor.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/SystemColor.java,v
retrieving revision 1.5
diff -u -r1.5 SystemColor.java
--- java/awt/SystemColor.java   8 May 2002 03:07:25 -0000       1.5
+++ java/awt/SystemColor.java   5 Apr 2004 12:36:04 -0000
@@ -427,7 +427,7 @@
    * as the system color is solid, the context does not need any of the
    * passed parameters to do its job.
    *
-   * @param cm the requested color model, ignored
+   * @param cm the requested color model
    * @param deviceBounds the bounding box in device coordinates, ignored
    * @param userBounds the bounding box in user coordinates, ignored
    * @param xform the bounds transformation, ignored
@@ -441,8 +441,8 @@
   {
     Toolkit.getDefaultToolkit().loadSystemColors(colors);
     int color = colors[value] | ALPHA_MASK;
-    if (context == null || color != context.color)
-      context = new ColorPaintContext(color);
+    if (context == null || color != context.color || 
!context.getColorModel().equals(cm))
+      context = new ColorPaintContext(cm,color);
     return context;
   }    
 

reply via email to

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