jdk/test/sun/awt/image/ImageRepresentation/LUTCompareTest.java
author duke
Wed, 05 Jul 2017 16:48:18 +0200
changeset 2092 b86b8d6d48bd
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
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     6570475
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test verifies that palette comparison procedure of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          ImageRepresentation class does not produce extra transparent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run     main LUTCompareTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.awt.MediaTracker;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.awt.image.BufferedImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.image.DataBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.image.ImageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.image.IndexColorModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.image.WritableRaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.imageio.IIOImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.imageio.ImageIO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.imageio.ImageWriteParam;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.imageio.ImageWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.imageio.stream.ImageOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.swing.JComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.swing.JFrame;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class LUTCompareTest implements ImageObserver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String[] args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Image img = createTestImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        LUTCompareTest o = new LUTCompareTest(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        tk.prepareImage(img, -1, -1, o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        while(!o.isImageReady()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    lock.wait(200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        checkResults(img);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    Image image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    boolean isReady = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public LUTCompareTest(Image img) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this.image = img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public boolean imageUpdate(Image image, int info,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                               int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (image == this.image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            System.out.println("Image status: " + dump(info));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                isReady = (info & ImageObserver.ALLBITS) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    if (isReady) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return !isReady;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public synchronized boolean isImageReady() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return isReady;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private static void checkResults(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        BufferedImage buf = new BufferedImage(w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                              BufferedImage.TYPE_INT_RGB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Graphics2D g = buf.createGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        g.setColor(Color.pink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        g.fillRect(0, 0, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        g.drawImage(image, 0, 0, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        g.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int rgb = buf.getRGB(w/2, h/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        System.out.printf("Result color: %x\n", rgb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        /* Buffered image should be the same as the last frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * of animated sequence (which is filled with blue).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * Any other color indicates the problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (rgb != 0xff0000ff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new RuntimeException("Test FAILED!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        System.out.println("Test PASSED.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private static int w = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private static int h = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /* Create test image with two frames:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *  1) with {red, red} palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *  2) with {blue, red } palette
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static Image createTestImage() throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        ImageWriteParam param = writer.getDefaultWriteParam();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        writer.setOutput(ios);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        writer.prepareWriteSequence(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        writer.writeToSequence(new IIOImage(frame1, null, null), param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        writer.writeToSequence(new IIOImage(frame2, null, null), param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        writer.endWriteSequence();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        writer.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        writer.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        ios.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        ios.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static BufferedImage createFrame(int[] palette) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        IndexColorModel icm = new IndexColorModel(getNumBits(palette.length),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            palette.length, palette, 0, false, -1, DataBuffer.TYPE_BYTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        WritableRaster wr = icm.createCompatibleWritableRaster(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int[] samples = new int[w * h];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Arrays.fill(samples, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        wr.setSamples(0, 0, w, h, 0, samples);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        BufferedImage img = new BufferedImage(icm, wr, false, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return img;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private static int getNumBits(int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (size < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            throw new RuntimeException("Invalid palette size: " + size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else if (size < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else if (size < 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new RuntimeException("Palette size is not supported: " + size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     private static String[] name = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        "WIDTH", "HEIGHT", "PROPERTIES", "SOMEBITS",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        "FRAMEBITS", "ALLBITS", "ERROR", "ABORT"
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 static String dump(int info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        String res = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        while (info != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            //System.out.println("info = " + info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if ((info & 0x1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                res += name[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                if ((info >> 1) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    res += " ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            count ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            info = (info >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
}