1289N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1289N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1289N/A *
1289N/A * This code is free software; you can redistribute it and/or modify it
1289N/A * under the terms of the GNU General Public License version 2 only, as
1289N/A * published by the Free Software Foundation.
1289N/A *
1289N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1289N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1289N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1289N/A * version 2 for more details (a copy is included in the LICENSE file that
1289N/A * accompanied this code).
1289N/A *
1289N/A * You should have received a copy of the GNU General Public License version
1289N/A * 2 along with this work; if not, write to the Free Software Foundation,
1289N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1289N/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.
1289N/A */
1289N/A
1289N/A/**
1289N/A * @test
1289N/A * @bug 6296893
1289N/A * @summary Test verifies that the isTopDown flag does not cause
1289N/A * a writing of bmp image in wrong scanline layout.
1289N/A * @run main TopDownTest
1289N/A */
1289N/A
1289N/Aimport java.awt.Color;
1289N/Aimport java.awt.Graphics;
1289N/Aimport java.awt.image.BufferedImage;
1289N/A
1289N/Aimport java.awt.image.IndexColorModel;
1289N/Aimport java.io.File;
1289N/Aimport java.io.IOException;
1289N/Aimport javax.imageio.IIOImage;
1289N/Aimport javax.imageio.ImageIO;
1289N/Aimport javax.imageio.ImageWriteParam;
1289N/Aimport javax.imageio.ImageWriter;
1289N/Aimport javax.imageio.plugins.bmp.BMPImageWriteParam;
1289N/Aimport javax.imageio.stream.ImageOutputStream;
1289N/Aimport static java.awt.image.BufferedImage.TYPE_INT_RGB;
1289N/Aimport static java.awt.image.BufferedImage.TYPE_BYTE_INDEXED;
1289N/A
1289N/Apublic class TopDownTest {
1289N/A
1289N/A public static void main(String[] args) throws IOException {
1289N/A BufferedImage src = createTestImage(24);
1289N/A
1289N/A writeWithCompression(src, "BI_BITFIELDS");
1289N/A
1289N/A writeWithCompression(src, "BI_RGB");
1289N/A
1289N/A src = createTestImage(8);
1289N/A writeWithCompression(src, "BI_RLE8");
1289N/A
1289N/A src = createTestImage(4);
1289N/A writeWithCompression(src, "BI_RLE4");
1289N/A
1289N/A }
1289N/A
1289N/A private static void writeWithCompression(BufferedImage src,
1289N/A String compression) throws IOException
1289N/A {
1289N/A System.out.println("Compression: " + compression);
1289N/A ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();
1289N/A if (writer == null) {
1289N/A throw new RuntimeException("Test failed: no bmp writer available");
1289N/A }
1289N/A File fout = File.createTempFile(compression + "_", ".bmp",
1289N/A new File("."));
1289N/A
1289N/A ImageOutputStream ios = ImageIO.createImageOutputStream(fout);
1289N/A writer.setOutput(ios);
1289N/A
1289N/A BMPImageWriteParam param = (BMPImageWriteParam)
1289N/A writer.getDefaultWriteParam();
1289N/A param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
1289N/A param.setCompressionType(compression);
1289N/A param.setTopDown(true);
1289N/A writer.write(null, new IIOImage(src, null, null), param);
1289N/A writer.dispose();
1289N/A ios.flush();
1289N/A ios.close();
1289N/A
1289N/A BufferedImage dst = ImageIO.read(fout);
1289N/A
1289N/A verify(dst);
1289N/A }
1289N/A
1289N/A private static void verify(BufferedImage dst) {
1289N/A int top_rgb = dst.getRGB(50, 25);
1289N/A System.out.printf("top_rgb: %x\n", top_rgb);
1289N/A int bot_rgb = dst.getRGB(50, 75);
1289N/A System.out.printf("bot_rgb: %x\n", bot_rgb);
1289N/A
1289N/A // expect to see blue color on the top of image
1289N/A if (top_rgb != 0xff0000ff) {
1289N/A throw new RuntimeException("Invaid top color: " +
1289N/A Integer.toHexString(bot_rgb));
1289N/A }
1289N/A if (bot_rgb != 0xffff0000) {
1289N/A throw new RuntimeException("Invalid bottom color: " +
1289N/A Integer.toHexString(bot_rgb));
1289N/A }
1289N/A }
1289N/A
1289N/A private static BufferedImage createTestImage(int bpp) {
1289N/A
1289N/A BufferedImage img = null;
1289N/A switch (bpp) {
1289N/A case 8:
1289N/A img = new BufferedImage(100, 100, TYPE_BYTE_INDEXED);
1289N/A break;
1289N/A case 4: {
1289N/A byte[] r = new byte[16];
1289N/A byte[] g = new byte[16];
1289N/A byte[] b = new byte[16];
1289N/A
1289N/A r[1] = (byte)0xff;
1289N/A b[0] = (byte)0xff;
1289N/A
1289N/A IndexColorModel icm = new IndexColorModel(4, 16, r, g, b);
1289N/A img = new BufferedImage(100, 100, TYPE_BYTE_INDEXED, icm);
1289N/A }
1289N/A break;
1289N/A case 24:
1289N/A default:
1289N/A img = new BufferedImage(100, 100, TYPE_INT_RGB);
1289N/A }
1289N/A Graphics g = img.createGraphics();
1289N/A g.setColor(Color.blue);
1289N/A g.fillRect(0, 0, 100, 50);
1289N/A g.setColor(Color.red);
1289N/A g.fillRect(0, 50, 100, 50);
1289N/A g.dispose();
1289N/A return img;
1289N/A }
1289N/A}