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.GraphicsDevice;
  29 import java.awt.GraphicsEnvironment;
  30 import java.awt.RenderingHints;
  31 import java.awt.image.BufferedImage;
  32 import java.awt.image.DataBuffer;
  33 import java.awt.image.DataBufferByte;
  34 import java.awt.image.DataBufferInt;
  35 import java.awt.image.DataBufferShort;
  36 import java.awt.image.VolatileImage;
  37 
  38 import static java.awt.Transparency.TRANSLUCENT;
  39 
  40 /**
  41  * @test
  42  * @bug 8062164
  43  * @summary We should get correct alpha, when we draw to/from VolatileImage and
  44  *          bicubic interpolation is enabled
  45  * @author Sergey Bylokhov
  46  */
  47 public final class IncorrectAlphaConversionBicubic {
  48 
  49     private static final Color RGB = new Color(200, 255, 7, 123);
  50     private static final int SIZE = 100;
  51 
  52     public static void main(final String[] args) {
  53         final GraphicsEnvironment ge =
  54                 GraphicsEnvironment.getLocalGraphicsEnvironment();
  55         final GraphicsDevice gd = ge.getDefaultScreenDevice();
  56         final GraphicsConfiguration gc = gd.getDefaultConfiguration();
  57         final VolatileImage vi =
  58                 gc.createCompatibleVolatileImage(SIZE, SIZE, TRANSLUCENT);
  59         final BufferedImage bi = makeUnmanagedBI(gc, TRANSLUCENT);
  60         final int expected = bi.getRGB(2, 2);
  61 
  62         int attempt = 0;
  63         BufferedImage snapshot;
  64         while (true) {
  65             if (++attempt > 10) {
  66                 throw new RuntimeException("Too many attempts: " + attempt);
  67             }
  68             vi.validate(gc);
  69             final Graphics2D g2d = vi.createGraphics();
  70             g2d.setComposite(AlphaComposite.Src);
  71             g2d.scale(2, 2);
  72             g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  73                                  RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  74             g2d.drawImage(bi, 0, 0, null);
  75             g2d.dispose();
  76 
  77             snapshot = vi.getSnapshot();
  78             if (vi.contentsLost()) {
  79                 continue;
  80             }
  81             break;
  82         }
  83         final int actual = snapshot.getRGB(2, 2);
  84         if (actual != expected) {
  85             System.err.println("Actual: " + Integer.toHexString(actual));
  86             System.err.println("Expected: " + Integer.toHexString(expected));
  87             throw new RuntimeException("Test failed");
  88         }
  89     }
  90 
  91     private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc,
  92                                                  int type) {
  93         BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type);
  94         Graphics2D g2d = img.createGraphics();
  95         g2d.setColor(RGB);
  96         g2d.fillRect(0, 0, SIZE, SIZE);
  97         g2d.dispose();
  98         final DataBuffer db = img.getRaster().getDataBuffer();
  99         if (db instanceof DataBufferInt) {
 100             ((DataBufferInt) db).getData();
 101         } else if (db instanceof DataBufferShort) {
 102             ((DataBufferShort) db).getData();
 103         } else if (db instanceof DataBufferByte) {
 104             ((DataBufferByte) db).getData();
 105         } else {
 106             try {
 107                 img.setAccelerationPriority(0.0f);
 108             } catch (final Throwable ignored) {
 109             }
 110         }
 111         return img;
 112     }
 113 }