1/* 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * @test 26 * @bug 6296893 27 * @summary Test verifies that the isTopDown flag does not cause 28 * a writing of bmp image in wrong scanline layout. 29 * @run main TopDownTest 30 */ 31 32import java.awt.Color; 33import java.awt.Graphics; 34import java.awt.image.BufferedImage; 35 36import java.awt.image.IndexColorModel; 37import java.io.File; 38import java.io.IOException; 39import javax.imageio.IIOImage; 40import javax.imageio.ImageIO; 41import javax.imageio.ImageWriteParam; 42import javax.imageio.ImageWriter; 43import javax.imageio.plugins.bmp.BMPImageWriteParam; 44import javax.imageio.stream.ImageOutputStream; 45import static java.awt.image.BufferedImage.TYPE_INT_RGB; 46import static java.awt.image.BufferedImage.TYPE_BYTE_INDEXED; 47 48public class TopDownTest { 49 50 public static void main(String[] args) throws IOException { 51 BufferedImage src = createTestImage(24); 52 53 writeWithCompression(src, "BI_BITFIELDS"); 54 55 writeWithCompression(src, "BI_RGB"); 56 57 src = createTestImage(8); 58 writeWithCompression(src, "BI_RLE8"); 59 60 src = createTestImage(4); 61 writeWithCompression(src, "BI_RLE4"); 62 63 } 64 65 private static void writeWithCompression(BufferedImage src, 66 String compression) throws IOException 67 { 68 System.out.println("Compression: " + compression); 69 ImageWriter writer = ImageIO.getImageWritersByFormatName("BMP").next(); 70 if (writer == null) { 71 throw new RuntimeException("Test failed: no bmp writer available"); 72 } 73 File fout = File.createTempFile(compression + "_", ".bmp", 74 new File(".")); 75 76 ImageOutputStream ios = ImageIO.createImageOutputStream(fout); 77 writer.setOutput(ios); 78 79 BMPImageWriteParam param = (BMPImageWriteParam) 80 writer.getDefaultWriteParam(); 81 param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 82 param.setCompressionType(compression); 83 param.setTopDown(true); 84 writer.write(null, new IIOImage(src, null, null), param); 85 writer.dispose(); 86 ios.flush(); 87 ios.close(); 88 89 BufferedImage dst = ImageIO.read(fout); 90 91 verify(dst); 92 } 93 94 private static void verify(BufferedImage dst) { 95 int top_rgb = dst.getRGB(50, 25); 96 System.out.printf("top_rgb: %x\n", top_rgb); 97 int bot_rgb = dst.getRGB(50, 75); 98 System.out.printf("bot_rgb: %x\n", bot_rgb); 99 100 // expect to see blue color on the top of image 101 if (top_rgb != 0xff0000ff) { 102 throw new RuntimeException("Invaid top color: " + 103 Integer.toHexString(bot_rgb)); 104 } 105 if (bot_rgb != 0xffff0000) { 106 throw new RuntimeException("Invalid bottom color: " + 107 Integer.toHexString(bot_rgb)); 108 } 109 } 110 111 private static BufferedImage createTestImage(int bpp) { 112 113 BufferedImage img = null; 114 switch (bpp) { 115 case 8: 116 img = new BufferedImage(100, 100, TYPE_BYTE_INDEXED); 117 break; 118 case 4: { 119 byte[] r = new byte[16]; 120 byte[] g = new byte[16]; 121 byte[] b = new byte[16]; 122 123 r[1] = (byte)0xff; 124 b[0] = (byte)0xff; 125 126 IndexColorModel icm = new IndexColorModel(4, 16, r, g, b); 127 img = new BufferedImage(100, 100, TYPE_BYTE_INDEXED, icm); 128 } 129 break; 130 case 24: 131 default: 132 img = new BufferedImage(100, 100, TYPE_INT_RGB); 133 } 134 Graphics g = img.createGraphics(); 135 g.setColor(Color.blue); 136 g.fillRect(0, 0, 100, 50); 137 g.setColor(Color.red); 138 g.fillRect(0, 50, 100, 50); 139 g.dispose(); 140 return img; 141 } 142} 143