Path: blob/master/test/jdk/javax/imageio/plugins/bmp/BMPSubsamplingTest.java
41153 views
/*1* Copyright (c) 2006, 2007, 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 6299168 6399660 651960026* @summary Test verifies that the subsampling usage does not causes color27* changes.28* @run main BMPSubsamplingTest29* @author andrew.brygin30*/31import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.Transparency;34import java.awt.color.ColorSpace;35import java.awt.image.BufferedImage;36import java.awt.image.ColorModel;37import java.awt.image.ComponentColorModel;38import java.awt.image.DataBuffer;39import java.awt.image.DirectColorModel;40import java.awt.image.IndexColorModel;41import java.awt.image.Raster;42import java.awt.image.WritableRaster;43import java.io.File;44import java.io.IOException;45import javax.imageio.IIOImage;46import javax.imageio.ImageIO;47import javax.imageio.ImageTypeSpecifier;48import javax.imageio.ImageWriteParam;49import javax.imageio.ImageWriter;50import javax.imageio.stream.ImageOutputStream;515253public class BMPSubsamplingTest {54private static final int TYPE_INT_GRB = 0x100;55private static final int TYPE_INT_GBR = 0x101;56private static final int TYPE_INT_RBG = 0x102;57private static final int TYPE_INT_BRG = 0x103;58private static final int TYPE_3BYTE_RGB = 0x104;59private static final int TYPE_3BYTE_GRB = 0x105;60private static final int TYPE_USHORT_555_GRB = 0x106;61private static final int TYPE_USHORT_555_BGR = 0x107;62private static final int TYPE_USHORT_565_BGR = 0x108;63private static final int TYPE_4BPP_INDEXED = 0x109;6465private String format = "BMP";6667private int[] img_types = new int[] {68BufferedImage.TYPE_INT_RGB,69BufferedImage.TYPE_INT_BGR,70TYPE_INT_GRB,71TYPE_INT_GBR,72TYPE_INT_RBG,73TYPE_INT_BRG,74BufferedImage.TYPE_USHORT_555_RGB,75BufferedImage.TYPE_USHORT_565_RGB,76TYPE_USHORT_555_GRB,77TYPE_USHORT_555_BGR,78TYPE_USHORT_565_BGR,79BufferedImage.TYPE_3BYTE_BGR,80TYPE_3BYTE_RGB,81TYPE_3BYTE_GRB,82BufferedImage.TYPE_BYTE_INDEXED,83TYPE_4BPP_INDEXED84};85Color[] colors = new Color[] { Color.red, Color.green, Color.blue };8687private final int srcXSubsampling = 3;88private final int srcYSubsampling = 3;8990int dx = 300;91int h = 300;92int w = dx * colors.length + srcXSubsampling;93949596public BMPSubsamplingTest() throws IOException {97ImageWriter writer =98ImageIO.getImageWritersByFormatName(format).next();99100ImageWriteParam wparam = writer.getDefaultWriteParam();101wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);102String[] types = wparam.getCompressionTypes();103for (int t = 0; t < img_types.length; t++) {104int img_type = img_types[t];105System.out.println("Test for " + getImageTypeName(img_type));106BufferedImage image = getTestImage(img_type);107108ImageTypeSpecifier specifier = new ImageTypeSpecifier(image);109110if (!writer.getOriginatingProvider().canEncodeImage(specifier)) {111System.out.println("Writer does not support encoding this buffered image type.");112continue;113}114115for(int i=0; i<types.length; i++) {116if ("BI_JPEG".equals(types[i])) {117// exclude BI_JPEG from automatic test118// due to color diffusion effect on the borders.119continue;120}121122if (canEncodeImage(types[i], specifier, img_type)) {123System.out.println("compression type: " + types[i] +124" Supported for " + getImageTypeName(img_type));125} else {126System.out.println("compression type: " + types[i] +127" NOT Supported for " + getImageTypeName(img_type));128continue;129}130ImageWriteParam imageWriteParam = getImageWriteParam(writer, types[i]);131132imageWriteParam.setSourceSubsampling(srcXSubsampling,133srcYSubsampling,1340, 0);135File outputFile = new File("subsampling_test_" +136getImageTypeName(img_type) + "__" +137types[i] + ".bmp");138ImageOutputStream ios =139ImageIO.createImageOutputStream(outputFile);140writer.setOutput(ios);141142IIOImage iioImg = new IIOImage(image, null, null);143144writer.write(null, iioImg, imageWriteParam);145146ios.flush();147ios.close();148149BufferedImage outputImage = ImageIO.read(outputFile);150checkTestImage(outputImage);151}152}153}154155private ImageWriteParam getImageWriteParam(ImageWriter writer, String value) {156ImageWriteParam imageWriteParam = writer.getDefaultWriteParam();157imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);158imageWriteParam.setCompressionType(value);159return imageWriteParam;160}161162163private boolean canEncodeImage(String compression,164ImageTypeSpecifier imgType, int rawType)165{166int biType = imgType.getBufferedImageType();167if ((!compression.equals("BI_BITFIELDS")) &&168((rawType == BufferedImage.TYPE_USHORT_565_RGB) ||169(rawType == TYPE_USHORT_565_BGR)))170{171return false;172}173174int bpp = imgType.getColorModel().getPixelSize();175if (compression.equals("BI_RLE4") && bpp != 4) {176// only 4bpp images can be encoded as BI_RLE4177return false;178}179if (compression.equals("BI_RLE8") && bpp != 8) {180// only 8bpp images can be encoded as BI_RLE8181return false;182}183184if (compression.equals("BI_PNG") &&185((rawType == TYPE_USHORT_555_GRB) ||186(rawType == TYPE_USHORT_555_BGR)))187{188return false;189}190191return true;192}193194private String getImageTypeName(int t) {195switch(t) {196case BufferedImage.TYPE_INT_RGB:197return "TYPE_INT_RGB";198case BufferedImage.TYPE_INT_BGR:199return "TYPE_INT_BGR";200case TYPE_INT_GRB:201return "TYPE_INT_GRB";202case TYPE_INT_GBR:203return "TYPE_INT_GBR";204case TYPE_INT_RBG:205return "TYPE_INT_RBG";206case TYPE_INT_BRG:207return "TYPE_INT_BRG";208case BufferedImage.TYPE_USHORT_555_RGB:209return "TYPE_USHORT_555_RGB";210case BufferedImage.TYPE_USHORT_565_RGB:211return "TYPE_USHORT_565_RGB";212case TYPE_USHORT_555_GRB:213return "TYPE_USHORT_555_GRB";214case TYPE_USHORT_555_BGR:215return "TYPE_USHORT_555_BGR";216case TYPE_USHORT_565_BGR:217return "TYPE_USHORT_565_BGR";218case BufferedImage.TYPE_3BYTE_BGR:219return "TYPE_3BYTE_BGR";220case TYPE_3BYTE_RGB:221return "TYPE_3BYTE_RGB";222case TYPE_3BYTE_GRB:223return "TYPE_3BYTE_GRB";224case BufferedImage.TYPE_BYTE_INDEXED:225return "TYPE_BYTE_INDEXED";226case TYPE_4BPP_INDEXED:227return "TYPE_BYTE_INDEXED (4bpp)";228default:229throw new IllegalArgumentException("Unknown image type: " + t);230}231}232233private BufferedImage getTestImage(int type) {234BufferedImage dst = null;235ColorModel colorModel = null;236WritableRaster raster = null;237switch(type) {238case TYPE_INT_GRB:239colorModel = new DirectColorModel(24,2400x0000ff00,2410x00ff0000,2420x000000ff);243break;244case TYPE_INT_GBR:245colorModel = new DirectColorModel(24,2460x000000ff,2470x00ff0000,2480x0000ff00);249break;250case TYPE_INT_RBG:251colorModel = new DirectColorModel(24,2520x00ff0000,2530x000000ff,2540x0000ff00);255break;256case TYPE_INT_BRG:257colorModel = new DirectColorModel(24,2580x0000ff00,2590x000000ff,2600x00ff0000);261break;262case TYPE_3BYTE_RGB:263dst = create3ByteImage(new int[] {8, 8, 8},264new int[] {0, 1, 2});265break;266case TYPE_3BYTE_GRB:267dst = create3ByteImage(new int[] {8, 8, 8},268new int[] {1, 0, 2});269break;270case TYPE_USHORT_555_GRB:271colorModel = new DirectColorModel(16,2720x03E0,2730x7C00,2740x001F);275break;276case TYPE_USHORT_555_BGR:277colorModel = new DirectColorModel(16,2780x001F,2790x03E0,2800x7C00);281break;282case TYPE_USHORT_565_BGR:283colorModel = new DirectColorModel(16,2840x001F,2850x07E0,2860xf800);287break;288case TYPE_4BPP_INDEXED:289dst = createIndexImage(4);290break;291default:292dst = new BufferedImage(w, h, type);293}294if (dst == null) {295raster = colorModel.createCompatibleWritableRaster(w, h);296dst = new BufferedImage(colorModel, raster, false, null);297}298Graphics2D g = dst.createGraphics();299for (int i = 0; i < colors.length; i++) {300g.setColor(colors[i]);301g.fillRect(i * dx, 0, dx, h);302}303g.dispose();304return dst;305}306307private BufferedImage createIndexImage(int bpp) {308// calculate palette size309int psize = (1 << bpp);310311// prepare palette;312byte[] r = new byte[psize];313byte[] g = new byte[psize];314byte[] b = new byte[psize];315316for (int i = 0; i < colors.length; i++) {317r[i] = (byte)(0xff & colors[i].getRed());318g[i] = (byte)(0xff & colors[i].getGreen());319b[i] = (byte)(0xff & colors[i].getBlue());320}321322// now prepare appropriate index clor model323IndexColorModel icm = new IndexColorModel(bpp, psize, r, g, b);324325return new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);326}327328private BufferedImage create3ByteImage(int[] nBits, int[] bOffs) {329ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);330ColorModel colorModel =331new ComponentColorModel(cs, nBits,332false, false,333Transparency.OPAQUE,334DataBuffer.TYPE_BYTE);335WritableRaster raster =336Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,337w, h,338w*3, 3,339bOffs, null);340return new BufferedImage(colorModel, raster, false, null);341}342343private void checkTestImage(BufferedImage dst) {344// NB: do not forget about subsampling factor.345int x = dx / (2 * srcXSubsampling);346int y = h / (2 * srcYSubsampling);347System.out.println("Check result: width=" + dst.getWidth() +348", height=" + dst.getHeight());349for (int i = 0; i < colors.length; i++) {350System.out.println("\tcheck at: " + x + ", " + y);351int srcRgb = colors[i].getRGB();352int dstRgb = dst.getRGB(x, y);353if (srcRgb != dstRgb) {354throw new RuntimeException("Test failed due to wrong dst color " +355Integer.toHexString(dstRgb) + " at " + x + "," + y +356"instead of " + Integer.toHexString(srcRgb));357}358x += dx / srcXSubsampling;359}360}361362public static void main(String args[]) throws IOException {363BMPSubsamplingTest test = new BMPSubsamplingTest();364}365}366367368