300N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
300N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
300N/A *
300N/A * This code is free software; you can redistribute it and/or modify it
300N/A * under the terms of the GNU General Public License version 2 only, as
300N/A * published by the Free Software Foundation.
300N/A *
300N/A * This code is distributed in the hope that it will be useful, but WITHOUT
300N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
300N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
300N/A * version 2 for more details (a copy is included in the LICENSE file that
300N/A * accompanied this code).
300N/A *
300N/A * You should have received a copy of the GNU General Public License version
300N/A * 2 along with this work; if not, write to the Free Software Foundation,
300N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
300N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
300N/A */
300N/A
300N/A/**
300N/A * @test
300N/A * @bug 6679308
300N/A * @summary test drawing to Alpha surfaces
300N/A */
300N/A
300N/Aimport java.awt.*;
300N/Aimport java.awt.image.*;
300N/A
300N/Apublic class AlphaSurfaceText {
300N/A
300N/A int wid=400, hgt=200;
300N/A
300N/A public AlphaSurfaceText(int biType, Color c) {
300N/A BufferedImage opaquebi0 =
300N/A new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);
300N/A drawText(opaquebi0, c);
300N/A
300N/A BufferedImage alphabi = new BufferedImage(wid, hgt, biType);
300N/A drawText(alphabi, c);
300N/A BufferedImage opaquebi1 =
300N/A new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);
300N/A Graphics2D g2d = opaquebi1.createGraphics();
300N/A g2d.drawImage(alphabi, 0, 0, null);
300N/A compare(opaquebi0, opaquebi1, biType, c);
300N/A }
300N/A
300N/A private void drawText(BufferedImage bi, Color c) {
300N/A Graphics2D g = bi.createGraphics();
300N/A g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
300N/A RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
300N/A g.setColor(c);
300N/A g.setFont(new Font("sansserif", Font.PLAIN, 70));
300N/A g.drawString("Hello!", 20, 100);
300N/A g.setFont(new Font("sansserif", Font.PLAIN, 12));
300N/A g.drawString("Hello!", 20, 130);
300N/A g.setFont(new Font("sansserif", Font.PLAIN, 10));
300N/A g.drawString("Hello!", 20, 150);
300N/A }
300N/A
300N/A // Need to allow for minimal rounding error, so allow each component
300N/A // to differ by 1.
300N/A void compare(BufferedImage bi0, BufferedImage bi1, int biType, Color c) {
300N/A for (int x=0; x<wid; x++) {
300N/A for (int y=0; y<hgt; y++) {
300N/A int rgb0 = bi0.getRGB(x, y);
300N/A int rgb1 = bi1.getRGB(x, y);
300N/A if (rgb0 == rgb1) continue;
300N/A int r0 = (rgb0 & 0xff0000) >> 16;
300N/A int r1 = (rgb1 & 0xff0000) >> 16;
300N/A int rdiff = r0-r1; if (rdiff<0) rdiff = -rdiff;
300N/A int g0 = (rgb0 & 0x00ff00) >> 8;
300N/A int g1 = (rgb1 & 0x00ff00) >> 8;
300N/A int gdiff = g0-g1; if (gdiff<0) gdiff = -gdiff;
300N/A int b0 = (rgb0 & 0x0000ff);
300N/A int b1 = (rgb1 & 0x0000ff);
300N/A int bdiff = b0-b1; if (bdiff<0) bdiff = -bdiff;
300N/A if (rdiff > 1 || gdiff > 1 || bdiff > 1) {
300N/A throw new RuntimeException(
300N/A "Images differ for type "+biType + " col="+c +
300N/A " at x=" + x + " y="+ y + " " +
300N/A Integer.toHexString(rgb0) + " vs " +
300N/A Integer.toHexString(rgb1));
300N/A }
300N/A }
300N/A }
300N/A
300N/A }
300N/A public static void main(String[] args) {
300N/A new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.white);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.red);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.blue);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.white);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.red);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.blue);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.white);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.red);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.blue);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.white);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.red);
300N/A new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.blue);
300N/A }
300N/A}