LUTCompareTest.java revision 2362
2N/A/*
2N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2N/A *
2N/A * This code is free software; you can redistribute it and/or modify it
2N/A * under the terms of the GNU General Public License version 2 only, as
2N/A * published by the Free Software Foundation.
2N/A *
2N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2N/A * version 2 for more details (a copy is included in the LICENSE file that
2N/A * accompanied this code).
2N/A *
2N/A * You should have received a copy of the GNU General Public License version
2N/A * 2 along with this work; if not, write to the Free Software Foundation,
2N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2N/A *
2N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2N/A * or visit www.oracle.com if you need additional information or have any
2N/A * questions.
2N/A */
2N/A
2N/A/*
2N/A * @test
2N/A * @bug 6570475
2N/A * @summary Test verifies that palette comparison procedure of
2N/A * ImageRepresentation class does not produce extra transparent
2N/A * pixels.
2N/A *
2N/A * @run main LUTCompareTest
2N/A */
2N/A
2N/A
2N/Aimport java.awt.Color;
2N/Aimport java.awt.Dimension;
2N/Aimport java.awt.Graphics;
2N/Aimport java.awt.Graphics2D;
2N/Aimport java.awt.Image;
2N/Aimport java.awt.MediaTracker;
2N/Aimport java.awt.Toolkit;
2N/Aimport java.awt.image.BufferedImage;
2N/Aimport java.awt.image.DataBuffer;
2N/Aimport java.awt.image.ImageObserver;
2N/Aimport java.awt.image.IndexColorModel;
2N/Aimport java.awt.image.WritableRaster;
2N/Aimport java.io.File;
2N/Aimport java.io.IOException;
2N/Aimport java.util.Arrays;
2N/Aimport javax.imageio.IIOImage;
2N/Aimport javax.imageio.ImageIO;
2N/Aimport javax.imageio.ImageWriteParam;
2N/Aimport javax.imageio.ImageWriter;
2N/Aimport javax.imageio.stream.ImageOutputStream;
2N/Aimport javax.swing.JComponent;
2N/Aimport javax.swing.JFrame;
2N/A
2N/Apublic class LUTCompareTest implements ImageObserver {
2N/A
2N/A public static void main(String[] args) throws IOException {
2N/A Image img = createTestImage();
2N/A
2N/A Toolkit tk = Toolkit.getDefaultToolkit();
2N/A
2N/A LUTCompareTest o = new LUTCompareTest(img);
2N/A
2N/A tk.prepareImage(img, -1, -1, o);
2N/A
2N/A while(!o.isImageReady()) {
2N/A synchronized(lock) {
2N/A try {
2N/A lock.wait(200);
2N/A } catch (InterruptedException e) {
2N/A }
2N/A }
2N/A }
2N/A
2N/A checkResults(img);
2N/A }
2N/A
2N/A private static Object lock = new Object();
2N/A
2N/A Image image;
2N/A
2N/A boolean isReady = false;
2N/A
2N/A public LUTCompareTest(Image img) {
2N/A this.image = img;
2N/A }
2N/A
2N/A public boolean imageUpdate(Image image, int info,
2N/A int x, int y, int w, int h) {
2N/A if (image == this.image) {
2N/A System.out.println("Image status: " + dump(info));
2N/A synchronized(this) {
2N/A isReady = (info & ImageObserver.ALLBITS) != 0;
2N/A if (isReady) {
2N/A synchronized(lock) {
2N/A lock.notifyAll();
2N/A }
2N/A }
2N/A }
2N/A return !isReady;
2N/A } else {
2N/A return true;
2N/A }
2N/A }
2N/A
2N/A public synchronized boolean isImageReady() {
2N/A return isReady;
2N/A }
2N/A
2N/A private static void checkResults(Image image) {
2N/A BufferedImage buf = new BufferedImage(w, h,
2N/A BufferedImage.TYPE_INT_RGB);
2N/A Graphics2D g = buf.createGraphics();
2N/A g.setColor(Color.pink);
2N/A g.fillRect(0, 0, w, h);
2N/A
2N/A g.drawImage(image, 0, 0, null);
2N/A
2N/A g.dispose();
2N/A
2N/A int rgb = buf.getRGB(w/2, h/2);
2N/A
2N/A System.out.printf("Result color: %x\n", rgb);
2N/A
2N/A /* Buffered image should be the same as the last frame
2N/A * of animated sequence (which is filled with blue).
2N/A * Any other color indicates the problem.
2N/A */
2N/A if (rgb != 0xff0000ff) {
2N/A throw new RuntimeException("Test FAILED!");
2N/A }
2N/A
2N/A System.out.println("Test PASSED.");
2N/A }
2N/A
2N/A private static int w = 100;
2N/A private static int h = 100;
2N/A
2N/A /* Create test image with two frames:
2N/A * 1) with {red, red} palette
2N/A * 2) with {blue, red } palette
2N/A */
2N/A private static Image createTestImage() throws IOException {
2N/A BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
2N/A BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
2N/A
2N/A ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
2N/A ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
2N/A ImageWriteParam param = writer.getDefaultWriteParam();
2N/A writer.setOutput(ios);
2N/A writer.prepareWriteSequence(null);
2N/A writer.writeToSequence(new IIOImage(frame1, null, null), param);
2N/A writer.writeToSequence(new IIOImage(frame2, null, null), param);
2N/A writer.endWriteSequence();
2N/A writer.reset();
2N/A writer.dispose();
2N/A
2N/A ios.flush();
2N/A ios.close();
2N/A
2N/A return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
2N/A }
2N/A
2N/A private static BufferedImage createFrame(int[] palette) {
2N/A IndexColorModel icm = new IndexColorModel(getNumBits(palette.length),
2N/A palette.length, palette, 0, false, -1, DataBuffer.TYPE_BYTE);
2N/A WritableRaster wr = icm.createCompatibleWritableRaster(w, h);
2N/A int[] samples = new int[w * h];
2N/A Arrays.fill(samples, 0);
2N/A wr.setSamples(0, 0, w, h, 0, samples);
2N/A
2N/A BufferedImage img = new BufferedImage(icm, wr, false, null);
2N/A return img;
2N/A }
2N/A
2N/A private static int getNumBits(int size) {
2N/A if (size < 0) {
2N/A throw new RuntimeException("Invalid palette size: " + size);
2N/A } else if (size < 3) {
2N/A return 1;
2N/A } else if (size < 5) {
2N/A return 2;
2N/A } else {
2N/A throw new RuntimeException("Palette size is not supported: " + size);
2N/A }
2N/A }
2N/A
2N/A private static String[] name = new String[] {
2N/A "WIDTH", "HEIGHT", "PROPERTIES", "SOMEBITS",
2N/A "FRAMEBITS", "ALLBITS", "ERROR", "ABORT"
2N/A };
2N/A
2N/A private static String dump(int info) {
2N/A String res = "";
2N/A int count = 0;
2N/A while (info != 0) {
2N/A //System.out.println("info = " + info);
2N/A if ((info & 0x1) == 1) {
2N/A res += name[count];
2N/A if ((info >> 1) != 0) {
2N/A res += " ";
2N/A }
2N/A
2N/A }
2N/A count ++;
2N/A info = (info >> 1);
2N/A }
2N/A return res;
2N/A }
2N/A}
2N/A