1 /*
   2  * Copyright (c) 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.awt.*;
  27 import java.awt.image.*;
  28 import java.util.*;
  29 import javax.swing.*;
  30 
  31 /**
  32  * @test
  33  * @bug 8024895
  34  * @summary tests if changing extra alpha values are honored for transformed blits
  35  * @author ceisserer
  36  */
  37 public class EABlitTest extends Frame {
  38     protected void test() {
  39         BufferedImage srcImg = createSrcImage();
  40         Image dstImg = getGraphicsConfiguration().createCompatibleVolatileImage(20, 50);
  41 
  42         //be over-cautious and render twice to avoid BI caching issues
  43         renderToVI(srcImg, dstImg);
  44         renderToVI(srcImg, dstImg);
  45 
  46         BufferedImage validationImg = new BufferedImage(20, 50, BufferedImage.TYPE_INT_RGB);
  47         Graphics2D valG = (Graphics2D) validationImg.getGraphics();
  48         valG.drawImage(dstImg, 0, 0, null);
  49 
  50         //Loop over all pixel, and count the different pixel values encountered.
  51         TreeSet<Integer> colorCntSet = new TreeSet<>();
  52         for (int x=0; x < validationImg.getWidth(); x++) {
  53             for (int y=0; y < validationImg.getHeight(); y++) {
  54                 int rgb = validationImg.getRGB(x, y);
  55                 colorCntSet.add(rgb);
  56             }
  57         }
  58 
  59         //Check if we encountered 3 different color values in total
  60         if (colorCntSet.size() == 3) {
  61             System.out.println("Passed!");
  62         } else {
  63             throw new RuntimeException("Test FAILED!");
  64         }
  65     }
  66 
  67     protected void renderToVI(BufferedImage src, Image dst) {
  68         Graphics2D g = (Graphics2D) dst.getGraphics();
  69 
  70         g.setColor(Color.WHITE);
  71         g.fillRect(0, 0, 50, 50);
  72         g.rotate(0.5f);
  73         g.setRenderingHint(RenderingHints.KEY_RENDERING,
  74                            RenderingHints.VALUE_RENDER_QUALITY);
  75 
  76         g.setComposite(AlphaComposite.SrcOver.derive(1f));
  77         g.drawImage(src, 10, 10, null);
  78 
  79         g.setComposite(AlphaComposite.SrcOver.derive(0.5f));
  80         g.drawImage(src, 20, 20, null);
  81     }
  82 
  83     protected BufferedImage createSrcImage() {
  84         BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
  85         Graphics2D g = (Graphics2D) bi.getGraphics();
  86         g.setColor(Color.YELLOW);
  87         g.fillRect(0, 0, 10, 10);
  88         return bi;
  89     }
  90 
  91     public static void main(String[] args) {
  92          new EABlitTest().test();
  93     }
  94 }