Ingo noticed some performance glitches when working with the default RGB
color model supplied by ColorModel.getRGBDefault(). This was caused by :
- creating a new instance every time
- DirectColorModel not optimized for sRGB

He wrote a small specialized SRGBColorModel, which directly accesses the
pixel data and delivers the color values as fast as possible. The
getRGBDefault() method has been changed to always return the same
instance.

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

        Reported by Ingo Proetel ([EMAIL PROTECTED])
        * java/awt/image/ColorModel.java
        (S_RGB_MODEL): New constant field.
        (getRGBDefault): Return constant SRGBColorModel.
        (SRGBColorModel): Specialized color model for sRGB.

/Roman
-- 
“Improvement makes straight roads, but the crooked roads, without
Improvement, are roads of Genius.” - William Blake
Index: java/awt/image/ColorModel.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/image/ColorModel.java,v
retrieving revision 1.28
diff -u -1 -0 -r1.28 ColorModel.java
--- java/awt/image/ColorModel.java	5 May 2006 13:07:01 -0000	1.28
+++ java/awt/image/ColorModel.java	10 May 2006 10:00:41 -0000
@@ -85,21 +85,26 @@
 public abstract class ColorModel implements Transparency
 {
   protected int pixel_bits;
   protected int transferType;
 
   int[] bits;
   ColorSpace cspace;
   int transparency;
   boolean hasAlpha;
   boolean isAlphaPremultiplied;
-    
+
+  /**
+   * The standard color model for the common sRGB.
+   */
+  private static final ColorModel S_RGB_MODEL = new SRGBColorModel();
+
   static int[] nArray(int value, int times)
   {
     int[] array = new int[times];
     java.util.Arrays.fill(array, value);
     return array;
   }
 
   static byte[] nArray(byte value, int times)
   {
     byte[] array = new byte[times];
@@ -189,21 +194,21 @@
   {
     // Do nothing here.
   }
 
   /**
    * Returns the default color model which in Sun's case is an instance
    * of <code>DirectColorModel</code>.
    */
   public static ColorModel getRGBdefault()
   {
-    return new DirectColorModel(32, 0xff0000, 0xff00, 0xff, 0xff000000);
+    return S_RGB_MODEL;
   }
 
   public final boolean hasAlpha()
   {
     return hasAlpha;
   }
 
   public final boolean isAlphaPremultiplied()
   {
     return isAlphaPremultiplied;
@@ -754,11 +759,63 @@
       ", transferType=" + transferType +
       ", transparency=" + transparency +
       ", hasAlpha=" + hasAlpha +
       ", isAlphaPremultiplied=" + isAlphaPremultiplied;
   }
 
   public String toString()
   {
     return getClass().getName() + "[" + stringParam() + "]";
   }
+
+  /**
+   * A color model optimized for standard sRGB.
+   */
+  private static class SRGBColorModel
+    extends DirectColorModel
+  {
+    
+    SRGBColorModel()
+    {
+      super(32,0x00FF0000,0x0000FF00,0x000000FF,0xFF000000);
+    }
+
+    public int getAlpha(Object inData)
+    {
+      return ((((int[]) inData)[0]) >> 24) & 0xFF;
+    }
+
+    public int getBlue(Object inData)
+    {
+      return ((((int[]) inData)[0])) & 0xFF;
+    }
+
+    public int getGreen(Object inData)
+    {
+      return ((((int[]) inData)[0]) >>  8) & 0xFF;
+    }
+
+    public int getRed(Object inData)
+    {
+      return ((((int[]) inData)[0]) >> 16) & 0xFF;
+    }
+
+    public int getRGB(Object inData)
+    {
+      return ((int[]) inData)[0];
+    }
+
+    public Object getDataElements(int rgb, Object pixel)
+    {
+      if(pixel == null)
+        {
+          pixel = new int[]{rgb};  
+        }
+      else
+        {
+          ((int[]) pixel)[0] = rgb;  
+        }
+      
+      return pixel;
+    }
+  }
 }

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

Reply via email to