Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4652742

AlphaComposite.SRC implementation is wrong

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.0
    • client-libs
    • 2d
    • sparc
    • solaris_2.5



      Name: apR10100 Date: 03/14/2002



      Current JDK1.4 spec for AlhpaComposite states:
      --------------------------------------------------------------------------------
      SRC
      public static final int SRCPorter-Duff Source rule. The source is copied to
      the destination. The destination is not used as input.
      Fs = 1 and Fd = 0, thus:

        Cd = Cs
        Ad = As
      --------------------------------------------------------------------------------
      public static AlphaComposite getInstance(int rule,
                                               float alpha)
      Creates an AlphaComposite object with the specified rule and the constant
      alpha to multiply with the alpha of the source. The source is multiplied with
      the specified alpha before being composited with the destination.
      --------------------------------------------------------------------------------

      Example below shows that alpha component is calculated
      incorrectly.

      import java.awt.*;
      import java.awt.image.*;

      public class Test4 {
          public static void main(String argv[]) {

              GraphicsEnvironment lge = GraphicsEnvironment.getLocalGraphicsEnvironment();
              GraphicsConfiguration gc = lge.getDefaultScreenDevice().getDefaultConfiguration();
              BufferedImage bi = gc.createCompatibleImage(200, 200);
              Graphics2D g2 = bi.createGraphics();

              Color c_before = new Color(bi.getRGB(0, 0), !bi.isAlphaPremultiplied());

              AlphaComposite ac1 = AlphaComposite.getInstance(AlphaComposite.SRC, 0.5f);
              g2.setComposite(ac1);
              g2.setColor(Color.pink);
              g2.fillRect(0,0,1,1);
              Color c_after = new Color(bi.getRGB(0, 0), !bi.isAlphaPremultiplied());

              Color c_expected = calcSrc(c_before, Color.pink, 0.5f, bi.isAlphaPremultiplied());

              if(!c_after.equals(c_expected)) {
                  System.out.println("Original color: "+c_before);
                  System.out.println("Compositing rule: (AlphaComposite.SRC, 0.5f)");
                  System.out.println("Returned color: "+c_after);
                  System.out.println("Expected color: "+c_expected);
                  System.out.println("Returned color alpha: "+c_after.getAlpha());
                  System.out.println("Expected color alpha: "+c_expected.getAlpha());
              }


              System.out.println("DONE");
          }


          // Fs = 1 and Fd = 0, thus:
          // Cd = Cs
          // Ad = As
          static Color calcSrc(Color cb, Color c, float alpha, boolean isAlphaPremultiplied) {

              double alphasource = c.getAlpha() * alpha / 255.0d;

              int r = (int) (c.getRed() * alphasource);
              int g = (int) (c.getGreen() * alphasource);
              int b = (int) (c.getBlue() * alphasource);
              double a = alphasource;
              int alpha1= (int)(a*255);

              if(isAlphaPremultiplied) {
                 r = (int) (a*r);
                 g = (int) (a*g);
                 b = (int) (a*b);
                 alpha1 = 255;
              }
                  System.out.println("r: "+r);
                  System.out.println("g: "+g);
                  System.out.println("b: "+b);
                  System.out.println("a: "+alpha1);
                  System.out.println(isAlphaPremultiplied);
              return new Color(r, g, b, alpha1);
          }

      }

      --------------------------
      Z:\tmp>java Test4
      r: 127
      g: 87
      b: 87
      a: 127
      false
      Original color: java.awt.Color[r=0,g=0,b=0]
      Compositing rule: (AlphaComposite.SRC, 0.5f)
      Returned color: java.awt.Color[r=255,g=174,b=174]
      Expected color: java.awt.Color[r=127,g=87,b=87]
      Returned color alpha: 255
      Expected color alpha: 127
      DONE

            flar Jim Graham
            avpsunw Avp Avp (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: