1 /*
   2  * Copyright (c) 2014, 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 import java.io.File;
  33 import java.io.IOException;
  34 
  35 import javax.imageio.ImageIO;
  36 
  37 import static java.awt.Transparency.TRANSLUCENT;
  38 import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR;
  39 import static java.awt.image.BufferedImage.TYPE_4BYTE_ABGR_PRE;
  40 import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
  41 import static java.awt.image.BufferedImage.TYPE_INT_ARGB_PRE;
  42 
  43 /**
  44  * @test
  45  * @bug 8017626
  46  * @summary Tests drawing transparent volatile image to transparent BI.
  47  *          Results of the blit compatibleImage to transparent BI used for
  48  *          comparison.
  49  * @author Sergey Bylokhov
  50  */
  51 public final class IncorrectAlphaSurface2SW {
  52 
  53     private static final int[] SCALES = {1, 2, 4, 8};
  54     private static final int[] SIZES = {1, 2, 3, 127, 128, 254, 255, 256};
  55     private static final int[] dstTypes = {TYPE_INT_ARGB, TYPE_INT_ARGB_PRE,
  56             TYPE_4BYTE_ABGR, TYPE_4BYTE_ABGR_PRE};
  57     private static final int[] srcTypes = {TRANSLUCENT};
  58 
  59 
  60     public static void main(final String[] args) throws IOException {
  61         GraphicsEnvironment ge = GraphicsEnvironment
  62                 .getLocalGraphicsEnvironment();
  63         GraphicsConfiguration gc = ge.getDefaultScreenDevice()
  64                                      .getDefaultConfiguration();
  65         BufferedImage destVI;
  66         BufferedImage destBI;
  67         BufferedImage sourceBI;
  68         VolatileImage sourceVI;
  69 
  70         for (final int s : SIZES) {
  71             for (final int srcType : srcTypes) {
  72                 for (final int dstType : dstTypes) {
  73                     for (final int scale : SCALES) {
  74                         int sw = s * scale;
  75                         destVI = new BufferedImage(sw, sw, dstType);
  76                         destBI = new BufferedImage(sw, sw, dstType);
  77                         sourceBI = gc.createCompatibleImage(sw, sw, srcType);
  78                         sourceVI = gc.createCompatibleVolatileImage(s, s, srcType);
  79 
  80                         // draw to dest BI using compatible image
  81                         fill(sourceBI, s);
  82                         Graphics2D big = destBI.createGraphics();
  83                         big.setComposite(AlphaComposite.Src);
  84                         big.drawImage(sourceBI, 0, 0, sw, sw, null);
  85                         big.dispose();
  86 
  87                         // draw to dest BI using compatible image
  88                         fill(sourceVI, s);
  89                         drawVItoBI(gc, destVI, sourceVI);
  90 
  91                         validate(destVI, destBI);
  92                         sourceVI.flush();
  93                     }
  94                 }
  95             }
  96         }
  97         System.out.println("Test PASSED");
  98     }
  99 
 100     private static void drawVItoBI(GraphicsConfiguration gc,
 101                                    BufferedImage bi, VolatileImage vi) {
 102         while (true) {
 103             vi.validate(gc);
 104             fill(vi, vi.getHeight());
 105             if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
 106                 try {
 107                     Thread.sleep(100);
 108                 } catch (final InterruptedException ignored) {
 109                 }
 110                 continue;
 111             }
 112 
 113             Graphics2D big = bi.createGraphics();
 114             big.setComposite(AlphaComposite.Src);
 115             big.drawImage(vi, 0, 0, bi.getWidth(), bi.getHeight(), null);
 116             big.dispose();
 117 
 118             if (vi.contentsLost()) {
 119                 try {
 120                     Thread.sleep(100);
 121                 } catch (final InterruptedException ignored) {
 122                 }
 123                 continue;
 124             }
 125             break;
 126         }
 127     }
 128 
 129     private static void validate(BufferedImage bi, BufferedImage gold)
 130             throws IOException {
 131         for (int x = 0; x < bi.getWidth(); ++x) {
 132             for (int y = 0; y < bi.getHeight(); ++y) {
 133                 if (gold.getRGB(x, y) != bi.getRGB(x, y)) {
 134                     System.err.println("Expected color = " + gold.getRGB(x, y));
 135                     System.err.println("Actual color = " + bi.getRGB(x, y));
 136                     ImageIO.write(gold, "png", new File("gold.png"));
 137                     ImageIO.write(bi, "png", new File("bi.png"));
 138                     throw new RuntimeException("Test failed.");
 139                 }
 140             }
 141         }
 142     }
 143 
 144     /**
 145      * Fills the whole image using different alpha for each row.
 146      *
 147      * @param image to fill
 148      */
 149     private static void fill(final Image image, final int size) {
 150         Graphics2D graphics = (Graphics2D) image.getGraphics();
 151         graphics.setComposite(AlphaComposite.Src);
 152         graphics.setColor(Color.GREEN);
 153         graphics.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
 154         int row = image.getHeight(null) / size;
 155         for (int i = 0; i < size; ++i) {
 156             graphics.setColor(new Color(23, 127, 189, i));
 157             graphics.fillRect(0, i * row, image.getWidth(null), row);
 158         }
 159         graphics.dispose();
 160     }
 161 }