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