jdk/test/javax/imageio/plugins/png/GrayPngTest.java
author kvn
Thu, 21 Feb 2008 14:03:41 -0800
changeset 199 fb51d01039ff
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6621084: ciMethodBlocks::split_block_at() is broken for methods with exception handler Summary: After an exception handler block is split the exception information is not moved to the new block which starts in exception handler BCI. Reviewed-by: jrose
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     6557713
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test verifies that PNG image writer correctly handles indexed images with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          various types of transparency.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Test for 4bpp OPAQUE image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run     main GrayPngTest 4 1 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Test for 4bpp BITMASK image with transparent pixel 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @run     main GrayPngTest 4 2 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Test for 4bpp TRANSLUCENT image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @run     main GrayPngTest 4 3 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Test for 8bpp OPAQUE image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @run     main GrayPngTest 8 1 127
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Test for 8bpp BITMASK image with transparent pixel 127
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @run     main GrayPngTest 8 2 127
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Test for 8bpp TRANSLUCENT image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @run     main GrayPngTest 8 3 127
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.awt.Transparency;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class GrayPngTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static void main(String[] args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
         * Expected argiments:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * args[0] - bits per pixel. Supported range: [1, 8]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * args[1] - transparency type. Should be one form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         *           java.awt.Transparency type constants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         * args[2] - transparent pixel for BITMASK transparency type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         *           otherwise is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int bpp = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int trans_type = Transparency.BITMASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int trans_pixel = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            bpp = Integer.parseInt(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            trans_type = Integer.parseInt(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            trans_pixel = Integer.parseInt(args[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.out.println("Ignore ncorrect bpp value: " + args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            System.out.println("Default test argumens.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        new GrayPngTest(bpp).doTest(trans_type, trans_pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private BufferedImage getTestImage(int trans_type, int trans_pixel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        IndexColorModel icm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        switch(trans_type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            case Transparency.OPAQUE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                icm = new IndexColorModel(bpp, numColors, r, g, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            case Transparency.BITMASK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                icm = new IndexColorModel(bpp, numColors, r, g, b, trans_pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            case Transparency.TRANSLUCENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                a = Arrays.copyOf(r, r.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                icm = new IndexColorModel(bpp, numColors, r, g, b, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                throw new RuntimeException("Invalid transparency: " + trans_type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int w = 256 * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int h = 200;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        dx = w / (numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        WritableRaster wr = icm.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        for (int i = 0; i < numColors; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int rx = i * dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            int[] samples = new int[h * dx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            Arrays.fill(samples, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            wr.setPixels(rx, 0, dx, h, samples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // horizontal line with transparent color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int[] samples = new int[w * 10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Arrays.fill(samples, trans_pixel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        wr.setPixels(0, h / 2 - 5, w, 10, samples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // create index color model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return new BufferedImage(icm, wr, false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static File pwd = new File(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private BufferedImage src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private BufferedImage dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private int bpp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    private int numColors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private int dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private byte[] r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private byte[] g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private byte[] b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private byte[] a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected GrayPngTest(int bpp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (0 > bpp || bpp > 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new RuntimeException("Invalid bpp: " + bpp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.bpp = bpp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        numColors = (1 << bpp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        System.out.println("Num colors: " + numColors);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // create palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        r = new byte[numColors];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        g = new byte[numColors];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        b = new byte[numColors];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        int dc = 0xff / (numColors - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        System.out.println("dc = " + dc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (int i = 0; i < numColors; i ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            byte l = (byte)(i * dc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            r[i] = l; g[i] = l; b[i] = l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void doTest() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (int i = 0; i < numColors; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            doTest(Transparency.BITMASK, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void doTest(int trans_type, int trans_index) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        src = getTestImage(trans_type, trans_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        System.out.println("src: " + src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        File f = File.createTempFile("gray_png_" + bpp + "bpp_" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
              trans_type + "tt_" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    trans_index + "tp_", ".png", pwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        System.out.println("File: " + f.getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (!ImageIO.write(src, "png", f)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            throw new RuntimeException("Writing failed!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            dst = ImageIO.read(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            System.out.println("dst: " + dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            throw new RuntimeException("Test FAILED.", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        checkImages();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private void checkImages() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        for (int i = 0; i < numColors; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            int src_rgb = src.getRGB(i * dx, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            int dst_rgb = dst.getRGB(i * dx, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // here we check transparency only due to possible colors space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // differences (sRGB in indexed source and Gray in gray+alpha destination)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if ((0xff000000 & src_rgb) != (0xff000000 & dst_rgb)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                throw new RuntimeException("Test FAILED. Color difference detected: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        Integer.toHexString(dst_rgb) + " instead of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        Integer.toHexString(src_rgb) + " for index " + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}