1 /*
2  * Copyright (c) 2008, 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 /**
25  * @test
26  * @bug 6679308
27  * @summary test drawing to Alpha surfaces
28  */
29 
30 import java.awt.*;
31 import java.awt.image.*;
32 
33 public class AlphaSurfaceText {
34 
35     int wid=400, hgt=200;
36 
AlphaSurfaceText(int biType, Color c)37     public AlphaSurfaceText(int biType, Color c) {
38         BufferedImage opaquebi0 =
39            new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);
40         drawText(opaquebi0, c);
41 
42         BufferedImage alphabi = new BufferedImage(wid, hgt, biType);
43         drawText(alphabi, c);
44         BufferedImage opaquebi1 =
45            new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);
46         Graphics2D g2d = opaquebi1.createGraphics();
47         g2d.drawImage(alphabi, 0, 0, null);
48         compare(opaquebi0, opaquebi1, biType, c);
49     }
50 
drawText(BufferedImage bi, Color c)51     private void drawText(BufferedImage bi, Color c) {
52         Graphics2D g = bi.createGraphics();
53         g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
54                            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
55         g.setColor(c);
56         g.setFont(new Font("sansserif", Font.PLAIN, 70));
57         g.drawString("Hello!", 20, 100);
58         g.setFont(new Font("sansserif", Font.PLAIN, 12));
59         g.drawString("Hello!", 20, 130);
60         g.setFont(new Font("sansserif", Font.PLAIN, 10));
61         g.drawString("Hello!", 20, 150);
62     }
63 
64     // Need to allow for minimal rounding error, so allow each component
65     // to differ by 1.
compare(BufferedImage bi0, BufferedImage bi1, int biType, Color c)66     void compare(BufferedImage bi0, BufferedImage bi1, int biType, Color c) {
67         for (int x=0; x<wid; x++) {
68             for (int y=0; y<hgt; y++) {
69                 int rgb0 = bi0.getRGB(x, y);
70                 int rgb1 = bi1.getRGB(x, y);
71                 if (rgb0 == rgb1) continue;
72                 int r0 = (rgb0 & 0xff0000) >> 16;
73                 int r1 = (rgb1 & 0xff0000) >> 16;
74                 int rdiff = r0-r1; if (rdiff<0) rdiff = -rdiff;
75                 int g0 = (rgb0 & 0x00ff00) >> 8;
76                 int g1 = (rgb1 & 0x00ff00) >> 8;
77                 int gdiff = g0-g1; if (gdiff<0) gdiff = -gdiff;
78                 int b0 = (rgb0 & 0x0000ff);
79                 int b1 = (rgb1 & 0x0000ff);
80                 int bdiff = b0-b1; if (bdiff<0) bdiff = -bdiff;
81                 if (rdiff > 1 || gdiff > 1 || bdiff > 1) {
82                     throw new RuntimeException(
83                       "Images differ for type "+biType + " col="+c +
84                       " at x=" + x + " y="+ y + " " +
85                       Integer.toHexString(rgb0) + " vs " +
86                       Integer.toHexString(rgb1));
87                 }
88             }
89         }
90 
91     }
main(String[] args)92     public static void main(String[] args) {
93         new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.white);
94         new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.red);
95         new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.blue);
96         new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.white);
97         new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.red);
98         new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.blue);
99         new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.white);
100         new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.red);
101         new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.blue);
102         new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.white);
103         new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.red);
104         new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.blue);
105    }
106 }
107