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

Compositing of transparent images into BufferedImages is wrong

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.2.0
    • client-libs
    • 2d
    • x86
    • windows_nt



      Name: rm29839 Date: 04/30/98


      /*
       * Java2D compositing bug demonstrator applet.
       *
       * This applet demonstrates a bug with compositing in Java2D. It draws
       * 7 images involving transparency. The first three are just sample images
       * involving transparency (call these A, B, and C). The second two are
       * composite drawing of A and B, and then A, B, and C directly onto the
       * applet. These work properly. The final two images are similar
       * composites formed by drawing A and B, and then A, B, and C into a
       * translucent BufferedImage, then drawing the BufferedImage onto the applet
       * surface. These should look the same as the previous 2, but they don't.
       * They produce incorrect colors in various areas.
       *
       * I am running JDK 1.2beta2 on an Pentium+MMX NT 4.0 machine with 16 bit
       * color.
       *
       */
      package photoshop_utils;

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

      public class draw_bug extends Applet {

        protected static int[] data1;
        protected static Image image1;
        protected static int[] data2;
        protected static Image image2;
        protected static int[] data3;
        protected static Image image3;
        protected static Image comp12;
        protected static Image comp123;

        public void init()
          {
            int x,y;
            int r,g,b,a;
            Graphics2D cdrw;

            /* fill data1 as follows:
                 y < 50 : black with alpha = 0
                 y >= 50 : red with alpha gradient from 50% to 100%
            */
            data1 = new int[100*100];
            for (y=0; y<100; y++)
              for (x=0; x<100; x++)
                {
                  if (y >= 50)
                    { r=255; g=0; b=0; a=(255*y)/100;}
                  else
                    { r=0; g=0; b=0; a=0;}
                  data1[y*100+x] = (a<<24) | (r<<16) | (g<<8) | b;
                }

            /* make an image with the data */
            image1 = Toolkit.getDefaultToolkit().createImage(
      new MemoryImageSource(100,100,data1,0,100));

            /* fill data2 as follows:
                 x < 50 : black with alpha = 0
                 x >= 50 : green with alpha gradient from 50% to 100%
            */
            data2 = new int[100*100];
            for (y=0; y<100; y++)
              for (x=0; x<100; x++)
                {
                  if (x >= 50)
                    { r=0; g=255; b=0; a=(255*x)/100;}
                  else
                    { r=0; g=0; b=0; a=0;}
                  data2[y*100+x] = (a<<24) | (r<<16) | (g<<8) | b;
                }

            /* make an image with the data */
            image2 = Toolkit.getDefaultToolkit().createImage(
      new MemoryImageSource(100,100,data2,0,100));

            /* fill data3 as follows:
                 x < y : black with alpha = 0
                 x >= y : blue with alpha gradient
            */
            data3 = new int[100*100];
            for (y=0; y<100; y++)
              for (x=0; x<100; x++)
                {
                  if (x >= y)
                    { r=0; g=0; b=255; a=(255*(x+y))/200;}
                  else
                    { r=0; g=0; b=0; a=0;}
                  data3[y*100+x] = (a<<24) | (r<<16) | (g<<8) | b;
                }

            /* make an image with the data */
            image3 = Toolkit.getDefaultToolkit().createImage(
      new MemoryImageSource(100,100,data3,0,100));

            /* create a composite of 1 and 2 */
            comp12 = GraphicsEnvironment.getLocalGraphicsEnvironment()
              .getDefaultScreenDevice().getDefaultConfiguration()
              .createCompatibleImage(100,100,Transparency.TRANSLUCENT);
            cdrw = (Graphics2D)comp12.getGraphics();
            cdrw.setComposite(AlphaComposite.getInstance(
                                 AlphaComposite.CLEAR));
            cdrw.setColor(Color.white);
            cdrw.clearRect(0,0,100,100);
            cdrw.setComposite(AlphaComposite.getInstance(
                                 AlphaComposite.SRC_OVER, 1.0f));
            cdrw.drawImage(image1,0,0,this);
            cdrw.drawImage(image2,0,0,this);

            /* create a composite of 1, 2, and 3 */
            comp123 = GraphicsEnvironment.getLocalGraphicsEnvironment()
              .getDefaultScreenDevice().getDefaultConfiguration()
              .createCompatibleImage(100,100,Transparency.TRANSLUCENT);
            cdrw = (Graphics2D)comp123.getGraphics();
            cdrw.setComposite(AlphaComposite.getInstance(
                                 AlphaComposite.CLEAR));
            cdrw.setColor(Color.white);
            cdrw.clearRect(0,0,100,100);
            cdrw.setComposite(AlphaComposite.getInstance(
                                 AlphaComposite.SRC_OVER, 1.0f));
            cdrw.drawImage(image1,0,0,this);
            cdrw.drawImage(image2,0,0,this);
            cdrw.drawImage(image3,0,0,this);
          }

        public void paint(Graphics g)
          {
            Graphics2D real_g = (Graphics2D)g;

            /* clear the whole applet */
            Dimension sz = getSize();
            real_g.clearRect(0,0,sz.width,sz.height);


            /* draw image 1 */
            real_g.setComposite(AlphaComposite.getInstance(
                                 AlphaComposite.SRC_OVER, 1.0f));
            real_g.drawImage(image1, 0,0, this);
            real_g.drawImage(image1, 300,0, this);
            real_g.drawImage(image1, 400,0, this);

            /* draw image 2 */
            real_g.drawImage(image2, 100,0, this);
            real_g.drawImage(image2, 300,0, this);
            real_g.drawImage(image2, 400,0, this);

            /* draw image 3 */
            real_g.drawImage(image3, 200,0, this);
            real_g.drawImage(image3, 400,0, this);

            /* draw composite of 1 and 2 */
            real_g.drawImage(comp12, 500,0, this);

            /* draw composite of 1, 2, and 3 */
            real_g.drawImage(comp123, 600,0, this);
          }
      }

      /*
      <APPLET CODE="photoshop_utils.draw_bug.class" CODEBASE=".."
                                    HEIGHT=150 WIDTH=700>
      </APPLET>
      */
      (Review ID: 28012)
      ======================================================================

            tnguyensunw Thanh Nguyen (Inactive)
            rmandelsunw Ronan Mandel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: