1 /*
   2  * Copyright (c) 2015, 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 import java.awt.AlphaComposite;
  25 import java.awt.Color;
  26 import java.awt.Graphics2D;
  27 import java.awt.GraphicsConfiguration;
  28 import java.awt.GraphicsEnvironment;
  29 import java.awt.Image;
  30 import java.awt.image.BufferedImage;
  31 import java.awt.image.VolatileImage;
  32 
  33 import static java.awt.Transparency.BITMASK;
  34 
  35 /**
  36  * @test
  37  * @bug 7188942
  38  * @summary We should get correct volatile image, when we use BITMASK
  39  *          transparency
  40  */
  41 public final class BitmaskVolatileImage {
  42 
  43     public static final int S = 8;
  44 
  45     public static void main(final String[] args) {
  46         GraphicsConfiguration gc =
  47                 GraphicsEnvironment.getLocalGraphicsEnvironment()
  48                         .getDefaultScreenDevice().getDefaultConfiguration();
  49         VolatileImage vi = gc.createCompatibleVolatileImage(S, S, BITMASK);
  50         BufferedImage ci = gc.createCompatibleImage(S, S, BITMASK);
  51 
  52         int attempt = 0;
  53         do {
  54             if (++attempt > 10) {
  55                 throw new RuntimeException("Too many attempts: " + attempt);
  56             }
  57             vi.validate(gc);
  58             test(vi, ci, gc);
  59         } while (vi.contentsLost());
  60     }
  61 
  62     private static void test(VolatileImage vi, BufferedImage ci, GraphicsConfiguration gc) {
  63         for (int r = 0; r <= 255; ++r) {
  64             for (int a = 0; a <= 255; ++a) {
  65                 fill(vi, new Color(r, 0, 0, a));
  66                 fill(ci, new Color(r, 0, 0, a));
  67                 validate(ci, vi.getSnapshot());
  68             }
  69         }
  70     }
  71 
  72     private static void fill(Image image, Color color) {
  73         Graphics2D g2d = (Graphics2D) image.getGraphics();
  74         g2d.setColor(color);
  75         g2d.setComposite(AlphaComposite.Src);
  76         g2d.fillRect(0, 0, S, S);
  77         g2d.dispose();
  78     }
  79 
  80     private static void validate(BufferedImage ci, BufferedImage snapshot) {
  81         for (int y = 0; y < ci.getHeight(); y++) {
  82             for (int x = 0; x < ci.getWidth(); x++) {
  83                 int ci_rgb = ci.getRGB(x, y);
  84                 int vi_rgb = snapshot.getRGB(x, y);
  85                 if (ci_rgb != vi_rgb) {
  86                     System.err.println("Exp:" + Integer.toHexString(ci_rgb));
  87                     System.err.println("Actual:" + Integer.toHexString(vi_rgb));
  88                     throw new RuntimeException("Colors mismatch!");
  89                 }
  90             }
  91         }
  92     }
  93 }