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.Rectangle;
  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 import java.io.File;
  38 import java.io.IOException;
  39 
  40 import javax.imageio.ImageIO;
  41 
  42 import static java.awt.Transparency.TRANSLUCENT;
  43 import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
  44 
  45 /**
  46  * @test
  47  * @bug 8059942
  48  * @summary Tests rotated clip when unmanaged image is drawn to VI.
  49  *          Results of the blit to compatibleImage are used for comparison.
  50  * @author Sergey Bylokhov
  51  */
  52 public final class IncorrectUnmanagedImageRotatedClip {
  53 
  54     public static void main(final String[] args) throws IOException {
  55         BufferedImage bi = makeUnmanagedBI();
  56         fill(bi);
  57         test(bi);
  58     }
  59 
  60     private static void test(final BufferedImage bi) throws IOException {
  61         GraphicsEnvironment ge = GraphicsEnvironment
  62                 .getLocalGraphicsEnvironment();
  63         GraphicsConfiguration gc = ge.getDefaultScreenDevice()
  64                                      .getDefaultConfiguration();
  65         VolatileImage vi = gc.createCompatibleVolatileImage(500, 200,
  66                                                             TRANSLUCENT);
  67         BufferedImage gold = gc.createCompatibleImage(500, 200, TRANSLUCENT);
  68         // draw to compatible Image
  69         draw(bi, gold);
  70         // draw to volatile image
  71         int attempt = 0;
  72         BufferedImage snapshot;
  73         while (true) {
  74             if (++attempt > 10) {
  75                 throw new RuntimeException("Too many attempts: " + attempt);
  76             }
  77             vi.validate(gc);
  78             if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
  79                 continue;
  80             }
  81             draw(bi, vi);
  82             snapshot = vi.getSnapshot();
  83             if (vi.contentsLost()) {
  84                 continue;
  85             }
  86             break;
  87         }
  88         // validate images
  89         for (int x = 0; x < gold.getWidth(); ++x) {
  90             for (int y = 0; y < gold.getHeight(); ++y) {
  91                 if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) {
  92                     ImageIO.write(gold, "png", new File("gold.png"));
  93                     ImageIO.write(snapshot, "png", new File("bi.png"));
  94                     throw new RuntimeException("Test failed.");
  95                 }
  96             }
  97         }
  98     }
  99 
 100     private static void draw(final BufferedImage from,final Image to) {
 101         final Graphics2D g2d = (Graphics2D) to.getGraphics();
 102         g2d.setComposite(AlphaComposite.Src);
 103         g2d.setColor(Color.ORANGE);
 104         g2d.fillRect(0, 0, to.getWidth(null), to.getHeight(null));
 105         g2d.rotate(Math.toRadians(45));
 106         g2d.clip(new Rectangle(41, 42, 43, 44));
 107         g2d.drawImage(from, 50, 50, Color.blue, null);
 108         g2d.dispose();
 109     }
 110 
 111     private static BufferedImage makeUnmanagedBI() {
 112         final BufferedImage bi = new BufferedImage(500, 200, TYPE_INT_ARGB);
 113         final DataBuffer db = bi.getRaster().getDataBuffer();
 114         if (db instanceof DataBufferInt) {
 115             ((DataBufferInt) db).getData();
 116         } else if (db instanceof DataBufferShort) {
 117             ((DataBufferShort) db).getData();
 118         } else if (db instanceof DataBufferByte) {
 119             ((DataBufferByte) db).getData();
 120         } else {
 121             try {
 122                 bi.setAccelerationPriority(0.0f);
 123             } catch (final Throwable ignored) {
 124             }
 125         }
 126         return bi;
 127     }
 128 
 129     private static void fill(final Image image) {
 130         final Graphics2D graphics = (Graphics2D) image.getGraphics();
 131         graphics.setComposite(AlphaComposite.Src);
 132         for (int i = 0; i < image.getHeight(null); ++i) {
 133             graphics.setColor(new Color(i, 0, 0));
 134             graphics.fillRect(0, i, image.getWidth(null), 1);
 135         }
 136         graphics.dispose();
 137     }
 138 }