Path: blob/master/test/jdk/javax/imageio/plugins/gif/EncodeSubImageTest.java
41154 views
/*1* Copyright (c) 2009, 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 679554426*27* @summary Test verifes that Image I/O gif writer correctly handles28* buffered images based on translated reasters (typically29* produced by getSubImage() method).30*31* @run main EncodeSubImageTest gif32*/3334import java.awt.Color;35import java.awt.Graphics;36import java.awt.image.BufferedImage;37import java.awt.image.Raster;38import java.io.File;39import java.io.IOException;40import javax.imageio.IIOImage;41import javax.imageio.ImageIO;42import javax.imageio.ImageWriteParam;43import javax.imageio.ImageWriter;44import javax.imageio.stream.ImageOutputStream;4546public class EncodeSubImageTest {47private static String format = "gif";48private static ImageWriter writer;49private static String file_suffix;50private static final int subSampleX = 2;51private static final int subSampleY = 2;5253public static void main(String[] args) throws IOException {54if (args.length > 0) {55format = args[0];56}5758writer = ImageIO.getImageWritersByFormatName(format).next();5960file_suffix =writer.getOriginatingProvider().getFileSuffixes()[0];6162BufferedImage src = createTestImage();63EncodeSubImageTest m1 = new EncodeSubImageTest(src);64m1.doTest("test_src");6566BufferedImage sub = src.getSubimage(subImageOffset, subImageOffset,67src.getWidth() - 2 * subImageOffset,68src.getHeight() - 2 * subImageOffset);69EncodeSubImageTest m2 = new EncodeSubImageTest(sub);70m2.doTest("test_sub");71}7273BufferedImage img;7475public EncodeSubImageTest(BufferedImage img) {76this.img = img;77}7879public void doTest(String prefix) throws IOException {80System.out.println(prefix);81File f = new File(prefix + file_suffix);82write(f, false);83verify(f, false);8485System.out.println(prefix + "_subsampled");86f = new File(prefix + "_subsampled");87write(f, true);88verify(f, true);8990System.out.println(prefix + ": Test PASSED.");91}9293private static final int subImageOffset = 10;9495private void verify(File f, boolean isSubsampled) {96BufferedImage dst = null;97try {98dst = ImageIO.read(f);99} catch (IOException e) {100throw new RuntimeException("Test FAILED: can't readin test image " +101f.getAbsolutePath(), e);102}103if (dst == null) {104throw new RuntimeException("Test FAILED: no dst image available.");105}106107checkPixel(dst, 0, 0, isSubsampled);108109checkPixel(dst, img.getWidth() / 2, img.getHeight() / 2, isSubsampled);110}111112private void checkPixel(BufferedImage dst, int x, int y,113boolean isSubsampled)114{115int dx = isSubsampled ? x / subSampleX : x;116int dy = isSubsampled ? y / subSampleY : y;117int src_rgb = img.getRGB(x, y);118System.out.printf("src_rgb: %x\n", src_rgb);119120int dst_rgb = dst.getRGB(dx, dy);121System.out.printf("dst_rgb: %x\n", dst_rgb);122123if (src_rgb != dst_rgb) {124throw new RuntimeException("Test FAILED: invalid color in dst");125}126}127128private static BufferedImage createTestImage() {129int w = 100;130int h = 100;131132BufferedImage src = new BufferedImage(w, h,133BufferedImage.TYPE_BYTE_INDEXED);134Graphics g = src.createGraphics();135g.setColor(Color.red);136g.fillRect(0, 0, w, h);137g.setColor(Color.green);138g.fillRect(subImageOffset, subImageOffset,139w - 2 * subImageOffset, h - 2* subImageOffset);140g.setColor(Color.blue);141g.fillRect(2 * subImageOffset, 2 * subImageOffset,142w - 4 * subImageOffset, h - 4 * subImageOffset);143g.dispose();144145return src;146}147148private void write(File f, boolean subsample) throws IOException {149ImageOutputStream ios = ImageIO.createImageOutputStream(f);150151writer.setOutput(ios);152ImageWriteParam p = writer.getDefaultWriteParam();153if (subsample) {154p.setSourceSubsampling(subSampleX, subSampleY, 0, 0);155}156writer.write(null, new IIOImage(img, null, null), p);157ios.close();158writer.reset();159}160}161162163