jdk/test/java/awt/Graphics2D/DrawString/AlphaSurfaceText.java
author ohair
Thu, 17 Sep 2009 13:17:57 -0700
changeset 3790 ab4f91337479
parent 549 4273b1234967
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
549
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     1
/*
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     4
 *
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     8
 *
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    13
 * accompanied this code).
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    14
 *
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    18
 *
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    21
 * have any questions.
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    22
 */
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    23
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    24
/**
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    25
 * @test
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    26
 * @bug 6679308
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    27
 * @summary test drawing to Alpha surfaces
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    28
 */
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    29
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    30
import java.awt.*;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    31
import java.awt.image.*;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    32
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    33
public class AlphaSurfaceText {
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    34
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    35
    int wid=400, hgt=200;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    36
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    37
    public AlphaSurfaceText(int biType, Color c) {
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    38
        BufferedImage opaquebi0 =
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    39
           new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    40
        drawText(opaquebi0, c);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    41
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    42
        BufferedImage alphabi = new BufferedImage(wid, hgt, biType);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    43
        drawText(alphabi, c);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    44
        BufferedImage opaquebi1 =
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    45
           new BufferedImage(wid, hgt, BufferedImage.TYPE_INT_RGB);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    46
        Graphics2D g2d = opaquebi1.createGraphics();
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    47
        g2d.drawImage(alphabi, 0, 0, null);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    48
        compare(opaquebi0, opaquebi1, biType, c);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    49
    }
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    50
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    51
    private void drawText(BufferedImage bi, Color c) {
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    52
        Graphics2D g = bi.createGraphics();
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    53
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    54
                           RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    55
        g.setColor(c);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    56
        g.setFont(new Font("sansserif", Font.PLAIN, 70));
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    57
        g.drawString("Hello!", 20, 100);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    58
        g.setFont(new Font("sansserif", Font.PLAIN, 12));
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    59
        g.drawString("Hello!", 20, 130);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    60
        g.setFont(new Font("sansserif", Font.PLAIN, 10));
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    61
        g.drawString("Hello!", 20, 150);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    62
    }
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    63
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    64
    // Need to allow for minimal rounding error, so allow each component
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    65
    // to differ by 1.
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    66
    void compare(BufferedImage bi0, BufferedImage bi1, int biType, Color c) {
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    67
        for (int x=0; x<wid; x++) {
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    68
            for (int y=0; y<hgt; y++) {
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    69
                int rgb0 = bi0.getRGB(x, y);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    70
                int rgb1 = bi1.getRGB(x, y);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    71
                if (rgb0 == rgb1) continue;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    72
                int r0 = (rgb0 & 0xff0000) >> 16;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    73
                int r1 = (rgb1 & 0xff0000) >> 16;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    74
                int rdiff = r0-r1; if (rdiff<0) rdiff = -rdiff;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    75
                int g0 = (rgb0 & 0x00ff00) >> 8;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    76
                int g1 = (rgb1 & 0x00ff00) >> 8;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    77
                int gdiff = g0-g1; if (gdiff<0) gdiff = -gdiff;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    78
                int b0 = (rgb0 & 0x0000ff);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    79
                int b1 = (rgb1 & 0x0000ff);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    80
                int bdiff = b0-b1; if (bdiff<0) bdiff = -bdiff;
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    81
                if (rdiff > 1 || gdiff > 1 || bdiff > 1) {
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    82
                    throw new RuntimeException(
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    83
                      "Images differ for type "+biType + " col="+c +
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    84
                      " at x=" + x + " y="+ y + " " +
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    85
                      Integer.toHexString(rgb0) + " vs " +
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    86
                      Integer.toHexString(rgb1));
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    87
                }
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    88
            }
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    89
        }
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    90
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    91
    }
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    92
    public static void main(String[] args) {
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    93
        new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.white);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    94
        new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.red);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    95
        new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB, Color.blue);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    96
        new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.white);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    97
        new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.red);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    98
        new AlphaSurfaceText(BufferedImage.TYPE_INT_ARGB_PRE, Color.blue);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
    99
        new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.white);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
   100
        new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.red);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
   101
        new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR, Color.blue);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
   102
        new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.white);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
   103
        new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.red);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
   104
        new AlphaSurfaceText(BufferedImage.TYPE_4BYTE_ABGR_PRE, Color.blue);
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
   105
   }
4273b1234967 6679308: Poor text rendering on translucent image.
prr
parents:
diff changeset
   106
}