1 /*
   2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6603887
  27  * @summary Verifies that drawImage with bg color works correctly for ICM image
  28  * @run main/othervm DrawImageBgTest
  29  * @run main/othervm -Dsun.java2d.pmoffscreen=true DrawImageBgTest
  30  */
  31 import java.awt.Color;
  32 import java.awt.Graphics;
  33 import java.awt.GraphicsConfiguration;
  34 import java.awt.GraphicsEnvironment;
  35 import java.awt.image.BufferedImage;
  36 import java.awt.image.IndexColorModel;
  37 import java.awt.image.VolatileImage;
  38 import java.awt.image.WritableRaster;
  39 import java.io.File;
  40 import java.io.IOException;
  41 import javax.imageio.ImageIO;
  42 
  43 public class DrawImageBgTest {
  44     public static void main(String[] args) {
  45         GraphicsConfiguration gc =
  46             GraphicsEnvironment.getLocalGraphicsEnvironment().
  47                 getDefaultScreenDevice().getDefaultConfiguration();
  48 
  49         if (gc.getColorModel().getPixelSize() <= 8) {
  50             System.out.println("8-bit color model, test considered passed");
  51             return;
  52         }
  53 
  54         /*
  55          * Set up images:
  56          * 1.) VolatileImge for rendering to,
  57          * 2.) BufferedImage for reading back the contents of the VI
  58          * 3.) The image triggering the problem
  59          */
  60         VolatileImage vImg = null;
  61         BufferedImage readBackBImg;
  62 
  63         // create a BITMASK ICM such that the transparent color is
  64         // tr. black (and it's the first in the color map so a buffered image
  65         // created with this ICM is transparent
  66         byte r[] = { 0x00, (byte)0xff};
  67         byte g[] = { 0x00, (byte)0xff};
  68         byte b[] = { 0x00, (byte)0xff};
  69         IndexColorModel icm = new IndexColorModel(8, 2, r, g, b, 0);
  70         WritableRaster wr = icm.createCompatibleWritableRaster(25, 25);
  71         BufferedImage tImg = new BufferedImage(icm, wr, false, null);
  72 
  73         do {
  74             if (vImg == null ||
  75                 vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE)
  76             {
  77                 vImg = gc.createCompatibleVolatileImage(tImg.getWidth(),
  78                                                         tImg.getHeight());
  79             }
  80 
  81             Graphics viG = vImg.getGraphics();
  82             viG.setColor(Color.red);
  83             viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
  84 
  85             viG.drawImage(tImg, 0, 0, Color.green, null);
  86             viG.fillRect(0, 0, vImg.getWidth(), vImg.getHeight());
  87             viG.drawImage(tImg, 0, 0, Color.white, null);
  88 
  89             readBackBImg = vImg.getSnapshot();
  90         } while (vImg.contentsLost());
  91 
  92         for (int x = 0; x < readBackBImg.getWidth(); x++) {
  93             for (int y = 0; y < readBackBImg.getHeight(); y++) {
  94                 int currPixel = readBackBImg.getRGB(x, y);
  95                 if (currPixel != Color.white.getRGB()) {
  96                     String fileName = "DrawImageBgTest.png";
  97                     try {
  98                         ImageIO.write(readBackBImg, "png", new File(fileName));
  99                         System.err.println("Dumped image to " + fileName);
 100                     } catch (IOException ex) {}
 101                     throw new
 102                         RuntimeException("Test Failed: found wrong color: 0x"+
 103                                          Integer.toHexString(currPixel));
 104                 }
 105             }
 106         }
 107         System.out.println("Test Passed.");
 108     }
 109 }