Path: blob/master/test/jdk/sun/awt/image/ImageRepresentation/LUTCompareTest.java
41153 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 657047526* @summary Test verifies that palette comparison procedure of27* ImageRepresentation class does not produce extra transparent28* pixels.29*30* @run main LUTCompareTest31*/323334import java.awt.Color;35import java.awt.Dimension;36import java.awt.Graphics;37import java.awt.Graphics2D;38import java.awt.Image;39import java.awt.MediaTracker;40import java.awt.Toolkit;41import java.awt.image.BufferedImage;42import java.awt.image.DataBuffer;43import java.awt.image.ImageObserver;44import java.awt.image.IndexColorModel;45import java.awt.image.WritableRaster;46import java.io.File;47import java.io.IOException;48import java.util.Arrays;49import javax.imageio.IIOImage;50import javax.imageio.ImageIO;51import javax.imageio.ImageWriteParam;52import javax.imageio.ImageWriter;53import javax.imageio.stream.ImageOutputStream;54import javax.swing.JComponent;55import javax.swing.JFrame;5657public class LUTCompareTest implements ImageObserver {5859public static void main(String[] args) throws IOException {60Image img = createTestImage();6162Toolkit tk = Toolkit.getDefaultToolkit();6364LUTCompareTest o = new LUTCompareTest(img);6566tk.prepareImage(img, -1, -1, o);6768while(!o.isImageReady()) {69synchronized(lock) {70try {71lock.wait(200);72} catch (InterruptedException e) {73}74}75}7677checkResults(img);78}7980private static Object lock = new Object();8182Image image;8384boolean isReady = false;8586public LUTCompareTest(Image img) {87this.image = img;88}8990public boolean imageUpdate(Image image, int info,91int x, int y, int w, int h) {92if (image == this.image) {93System.out.println("Image status: " + dump(info));94synchronized(this) {95isReady = (info & ImageObserver.ALLBITS) != 0;96if (isReady) {97synchronized(lock) {98lock.notifyAll();99}100}101}102return !isReady;103} else {104return true;105}106}107108public synchronized boolean isImageReady() {109return isReady;110}111112private static void checkResults(Image image) {113BufferedImage buf = new BufferedImage(w, h,114BufferedImage.TYPE_INT_RGB);115Graphics2D g = buf.createGraphics();116g.setColor(Color.pink);117g.fillRect(0, 0, w, h);118119g.drawImage(image, 0, 0, null);120121g.dispose();122123int rgb = buf.getRGB(w/2, h/2);124125System.out.printf("Result color: %x\n", rgb);126127/* Buffered image should be the same as the last frame128* of animated sequence (which is filled with blue).129* Any other color indicates the problem.130*/131if (rgb != 0xff0000ff) {132throw new RuntimeException("Test FAILED!");133}134135System.out.println("Test PASSED.");136}137138private static int w = 100;139private static int h = 100;140141/* Create test image with two frames:142* 1) with {red, red} palette143* 2) with {blue, red } palette144*/145private static Image createTestImage() throws IOException {146BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });147BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });148149ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();150ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));151ImageWriteParam param = writer.getDefaultWriteParam();152writer.setOutput(ios);153writer.prepareWriteSequence(null);154writer.writeToSequence(new IIOImage(frame1, null, null), param);155writer.writeToSequence(new IIOImage(frame2, null, null), param);156writer.endWriteSequence();157writer.reset();158writer.dispose();159160ios.flush();161ios.close();162163return Toolkit.getDefaultToolkit().createImage("lut_test.gif");164}165166private static BufferedImage createFrame(int[] palette) {167IndexColorModel icm = new IndexColorModel(getNumBits(palette.length),168palette.length, palette, 0, false, -1, DataBuffer.TYPE_BYTE);169WritableRaster wr = icm.createCompatibleWritableRaster(w, h);170int[] samples = new int[w * h];171Arrays.fill(samples, 0);172wr.setSamples(0, 0, w, h, 0, samples);173174BufferedImage img = new BufferedImage(icm, wr, false, null);175return img;176}177178private static int getNumBits(int size) {179if (size < 0) {180throw new RuntimeException("Invalid palette size: " + size);181} else if (size < 3) {182return 1;183} else if (size < 5) {184return 2;185} else {186throw new RuntimeException("Palette size is not supported: " + size);187}188}189190private static String[] name = new String[] {191"WIDTH", "HEIGHT", "PROPERTIES", "SOMEBITS",192"FRAMEBITS", "ALLBITS", "ERROR", "ABORT"193};194195private static String dump(int info) {196String res = "";197int count = 0;198while (info != 0) {199//System.out.println("info = " + info);200if ((info & 0x1) == 1) {201res += name[count];202if ((info >> 1) != 0) {203res += " ";204}205206}207count ++;208info = (info >> 1);209}210return res;211}212}213214215