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