986N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
986N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
986N/A *
986N/A * This code is free software; you can redistribute it and/or modify it
986N/A * under the terms of the GNU General Public License version 2 only, as
986N/A * published by the Free Software Foundation.
986N/A *
986N/A * This code is distributed in the hope that it will be useful, but WITHOUT
986N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
986N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
986N/A * version 2 for more details (a copy is included in the LICENSE file that
986N/A * accompanied this code).
986N/A *
986N/A * You should have received a copy of the GNU General Public License version
986N/A * 2 along with this work; if not, write to the Free Software Foundation,
986N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
986N/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.
986N/A */
986N/A
986N/A/**
986N/A * @test
986N/A * @bug 6795544
986N/A *
986N/A * @summary Test verifes that Image I/O gif writer correctly handles
986N/A * buffered images based on translated reasters (typically
986N/A * produced by getSubImage() method).
986N/A *
986N/A * @run main EncodeSubImageTest gif
986N/A */
986N/A
986N/Aimport java.awt.Color;
986N/Aimport java.awt.Graphics;
986N/Aimport java.awt.image.BufferedImage;
986N/Aimport java.awt.image.Raster;
986N/Aimport java.io.File;
986N/Aimport java.io.IOException;
986N/Aimport javax.imageio.IIOImage;
986N/Aimport javax.imageio.ImageIO;
986N/Aimport javax.imageio.ImageWriteParam;
986N/Aimport javax.imageio.ImageWriter;
986N/Aimport javax.imageio.stream.ImageOutputStream;
986N/A
986N/Apublic class EncodeSubImageTest {
986N/A private static String format = "gif";
986N/A private static ImageWriter writer;
986N/A private static String file_suffix;
986N/A private static final int subSampleX = 2;
986N/A private static final int subSampleY = 2;
986N/A
986N/A public static void main(String[] args) throws IOException {
986N/A if (args.length > 0) {
986N/A format = args[0];
986N/A }
986N/A
986N/A writer = ImageIO.getImageWritersByFormatName(format).next();
986N/A
986N/A file_suffix =writer.getOriginatingProvider().getFileSuffixes()[0];
986N/A
986N/A BufferedImage src = createTestImage();
986N/A EncodeSubImageTest m1 = new EncodeSubImageTest(src);
986N/A m1.doTest("test_src");
986N/A
986N/A BufferedImage sub = src.getSubimage(subImageOffset, subImageOffset,
986N/A src.getWidth() - 2 * subImageOffset,
986N/A src.getHeight() - 2 * subImageOffset);
986N/A EncodeSubImageTest m2 = new EncodeSubImageTest(sub);
986N/A m2.doTest("test_sub");
986N/A }
986N/A
986N/A BufferedImage img;
986N/A
986N/A public EncodeSubImageTest(BufferedImage img) {
986N/A this.img = img;
986N/A }
986N/A
986N/A public void doTest(String prefix) throws IOException {
986N/A System.out.println(prefix);
986N/A File f = new File(prefix + file_suffix);
986N/A write(f, false);
986N/A verify(f, false);
986N/A
986N/A System.out.println(prefix + "_subsampled");
986N/A f = new File(prefix + "_subsampled");
986N/A write(f, true);
986N/A verify(f, true);
986N/A
986N/A System.out.println(prefix + ": Test PASSED.");
986N/A }
986N/A
986N/A private static final int subImageOffset = 10;
986N/A
986N/A private void verify(File f, boolean isSubsampled) {
986N/A BufferedImage dst = null;
986N/A try {
986N/A dst = ImageIO.read(f);
986N/A } catch (IOException e) {
986N/A throw new RuntimeException("Test FAILED: can't readin test image " +
986N/A f.getAbsolutePath(), e);
986N/A }
986N/A if (dst == null) {
986N/A throw new RuntimeException("Test FAILED: no dst image available.");
986N/A }
986N/A
986N/A checkPixel(dst, 0, 0, isSubsampled);
986N/A
986N/A checkPixel(dst, img.getWidth() / 2, img.getHeight() / 2, isSubsampled);
986N/A }
986N/A
986N/A private void checkPixel(BufferedImage dst, int x, int y,
986N/A boolean isSubsampled)
986N/A {
986N/A int dx = isSubsampled ? x / subSampleX : x;
986N/A int dy = isSubsampled ? y / subSampleY : y;
986N/A int src_rgb = img.getRGB(x, y);
986N/A System.out.printf("src_rgb: %x\n", src_rgb);
986N/A
986N/A int dst_rgb = dst.getRGB(dx, dy);
986N/A System.out.printf("dst_rgb: %x\n", dst_rgb);
986N/A
986N/A if (src_rgb != dst_rgb) {
986N/A throw new RuntimeException("Test FAILED: invalid color in dst");
986N/A }
986N/A }
986N/A
986N/A private static BufferedImage createTestImage() {
986N/A int w = 100;
986N/A int h = 100;
986N/A
986N/A BufferedImage src = new BufferedImage(w, h,
986N/A BufferedImage.TYPE_BYTE_INDEXED);
986N/A Graphics g = src.createGraphics();
986N/A g.setColor(Color.red);
986N/A g.fillRect(0, 0, w, h);
986N/A g.setColor(Color.green);
986N/A g.fillRect(subImageOffset, subImageOffset,
986N/A w - 2 * subImageOffset, h - 2* subImageOffset);
986N/A g.setColor(Color.blue);
986N/A g.fillRect(2 * subImageOffset, 2 * subImageOffset,
986N/A w - 4 * subImageOffset, h - 4 * subImageOffset);
986N/A g.dispose();
986N/A
986N/A return src;
986N/A }
986N/A
986N/A private void write(File f, boolean subsample) throws IOException {
986N/A ImageOutputStream ios = ImageIO.createImageOutputStream(f);
986N/A
986N/A writer.setOutput(ios);
986N/A ImageWriteParam p = writer.getDefaultWriteParam();
986N/A if (subsample) {
986N/A p.setSourceSubsampling(subSampleX, subSampleY, 0, 0);
986N/A }
986N/A writer.write(null, new IIOImage(img, null, null), p);
986N/A ios.close();
986N/A writer.reset();
986N/A }
986N/A}