Path: blob/master/test/jdk/javax/imageio/plugins/bmp/TopDownTest.java
41153 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 629689326* @summary Test verifies that the isTopDown flag does not cause27* a writing of bmp image in wrong scanline layout.28* @run main TopDownTest29*/3031import java.awt.Color;32import java.awt.Graphics;33import java.awt.image.BufferedImage;3435import java.awt.image.IndexColorModel;36import java.io.File;37import java.io.IOException;38import javax.imageio.IIOImage;39import javax.imageio.ImageIO;40import javax.imageio.ImageWriteParam;41import javax.imageio.ImageWriter;42import javax.imageio.plugins.bmp.BMPImageWriteParam;43import javax.imageio.stream.ImageOutputStream;44import static java.awt.image.BufferedImage.TYPE_INT_RGB;45import static java.awt.image.BufferedImage.TYPE_BYTE_INDEXED;4647public class TopDownTest {4849public static void main(String[] args) throws IOException {50BufferedImage src = createTestImage(24);5152writeWithCompression(src, "BI_BITFIELDS");5354writeWithCompression(src, "BI_RGB");5556src = createTestImage(8);57writeWithCompression(src, "BI_RLE8");5859src = createTestImage(4);60writeWithCompression(src, "BI_RLE4");6162}6364private static void writeWithCompression(BufferedImage src,65String compression) throws IOException66{67System.out.println("Compression: " + compression);68ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next();69if (writer == null) {70throw new RuntimeException("Test failed: no bmp writer available");71}72File fout = File.createTempFile(compression + "_", ".bmp",73new File("."));7475ImageOutputStream ios = ImageIO.createImageOutputStream(fout);76writer.setOutput(ios);7778BMPImageWriteParam param = (BMPImageWriteParam)79writer.getDefaultWriteParam();80param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);81param.setCompressionType(compression);82param.setTopDown(true);83writer.write(null, new IIOImage(src, null, null), param);84writer.dispose();85ios.flush();86ios.close();8788BufferedImage dst = ImageIO.read(fout);8990verify(dst);91}9293private static void verify(BufferedImage dst) {94int top_rgb = dst.getRGB(50, 25);95System.out.printf("top_rgb: %x\n", top_rgb);96int bot_rgb = dst.getRGB(50, 75);97System.out.printf("bot_rgb: %x\n", bot_rgb);9899// expect to see blue color on the top of image100if (top_rgb != 0xff0000ff) {101throw new RuntimeException("Invaid top color: " +102Integer.toHexString(bot_rgb));103}104if (bot_rgb != 0xffff0000) {105throw new RuntimeException("Invalid bottom color: " +106Integer.toHexString(bot_rgb));107}108}109110private static BufferedImage createTestImage(int bpp) {111112BufferedImage img = null;113switch (bpp) {114case 8:115img = new BufferedImage(100, 100, TYPE_BYTE_INDEXED);116break;117case 4: {118byte[] r = new byte[16];119byte[] g = new byte[16];120byte[] b = new byte[16];121122r[1] = (byte)0xff;123b[0] = (byte)0xff;124125IndexColorModel icm = new IndexColorModel(4, 16, r, g, b);126img = new BufferedImage(100, 100, TYPE_BYTE_INDEXED, icm);127}128break;129case 24:130default:131img = new BufferedImage(100, 100, TYPE_INT_RGB);132}133Graphics g = img.createGraphics();134g.setColor(Color.blue);135g.fillRect(0, 0, 100, 50);136g.setColor(Color.red);137g.fillRect(0, 50, 100, 50);138g.dispose();139return img;140}141}142143144