Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/plugins/bmp/BMPCompressionTest.java
38853 views
/*1* Copyright (c) 2003, 2017, 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 464187226* @summary Tests writing compression modes of BMP plugin27*/2829import java.awt.Color;30import java.awt.Dimension;31import java.awt.Graphics;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.PixelInterleavedSampleModel;42import java.awt.image.Raster;43import java.awt.image.SampleModel;44import java.awt.image.SinglePixelPackedSampleModel;45import java.awt.image.WritableRaster;46import java.io.ByteArrayInputStream;47import java.io.ByteArrayOutputStream;48import java.io.File;49import java.io.FileOutputStream;50import java.io.IOException;51import java.util.Arrays;52import java.util.Iterator;53import java.util.LinkedList;54import java.util.List;5556import javax.imageio.IIOImage;57import javax.imageio.ImageIO;58import javax.imageio.ImageReader;59import javax.imageio.ImageTypeSpecifier;60import javax.imageio.ImageWriteParam;61import javax.imageio.ImageWriter;62import javax.imageio.metadata.IIOMetadata;63import javax.imageio.plugins.bmp.BMPImageWriteParam;64import javax.imageio.stream.ImageOutputStream;65import javax.swing.JComponent;66import javax.swing.JFrame;6768import com.sun.imageio.plugins.bmp.BMPMetadata;6970public class BMPCompressionTest {7172static final String format = "BMP";7374public static void main(String[] args) {7576ImageWriter iw = null;77Iterator writers = ImageIO.getImageWritersByFormatName(format);78if (!writers.hasNext()) {79throw new RuntimeException("No available Image writer for "+format);80}81iw = (ImageWriter)writers.next();828384Iterator tests = Test.createTestSet(iw);8586while(tests.hasNext()) {8788Test t = (Test)tests.next();89System.out.println(t.getDescription());90t.doTest();91}9293}949596static class Test {97static ImageWriter iw;98private BufferedImage img;99private String description;100private BMPImageWriteParam param;101private IIOMetadata meta;102103104public static Iterator createTestSet(ImageWriter w) {105List l = new LinkedList();106107Test.iw = w;108109// variate compression types110BMPImageWriteParam param = (BMPImageWriteParam)iw.getDefaultWriteParam();111param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);112param.setCompressionType("BI_RGB");113if (param.canWriteCompressed()) {114String[] cTypes = param.getCompressionTypes();115String[] cDescr = param.getCompressionQualityDescriptions();116float[] cValues = param.getCompressionQualityValues();117118if (cDescr == null) {119System.out.println("There are no compression quality description!");120} else {121for(int i=0; i<cDescr.length; i++) {122System.out.println("Quality[" + i + "]=\""+cDescr[i]+"\"");123}124}125if (cValues == null) {126System.out.println("There are no compression quality values!");127} else {128for(int i=0; i<cValues.length; i++) {129System.out.println("Value["+i+"]=\""+cValues[i]+"\"");130}131}132133for(int i=0; i<cTypes.length; i++) {134String compressionType = cTypes[i];135BufferedImage img = null;136137int type = BufferedImage.TYPE_INT_BGR;138try {139img = createTestImage(type);140if (compressionType.equals("BI_RLE8")) {141img = createTestImage2(8, DataBuffer.TYPE_BYTE);142} else if (compressionType.equals("BI_RLE4")) {143img = createTestImage3(4, DataBuffer.TYPE_BYTE);144} else if (compressionType.equals("BI_BITFIELDS")) {145img = createTestImage4(32);146}147148} catch (IOException ex) {149throw new RuntimeException("Unable to create test image");150}151BMPImageWriteParam p = (BMPImageWriteParam)iw.getDefaultWriteParam();152System.out.println("Current compression type is \""+cTypes[i]+"\"");153p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);154p.setCompressionType(compressionType);155156IIOMetadata md = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), p);157158l.add( new Test(p, md, img));159}160}161// }162return l.iterator();163164}165166private Test(BMPImageWriteParam p, IIOMetadata md, BufferedImage i) {167param = p;168meta = md;169img = i;170171172description = "Compression type is " + p.getCompressionType();173}174175public String getDescription() {176return description;177}178179public void doTest() {180try {181System.out.println(this.getDescription());182if (param.getCompressionMode() != ImageWriteParam.MODE_EXPLICIT) {183System.out.println("Warning: compression mode is not MODE_EXPLICIT");184}185// change metadata according to ImageWriteParam186IIOMetadata new_meta = iw.convertImageMetadata(meta, new ImageTypeSpecifier(img), param);187188IIOImage iio_img = new IIOImage(img, null, new_meta);189190ByteArrayOutputStream baos = new ByteArrayOutputStream();191ImageOutputStream ios = ImageIO.createImageOutputStream(baos);192iw.setOutput(ios);193System.out.print("write image...");194System.out.println("Current compression Type is \""+param.getCompressionType()+"\"");195iw.write(new_meta, iio_img, param);196//iw.write(iio_img);197System.out.println("OK");198System.out.print("read image ... ");199ios.flush();200201byte[] ba_image = baos.toByteArray();202203System.out.println("Array length=" + ba_image.length);204FileOutputStream fos = new FileOutputStream(new File(param.getCompressionType()+".bmp"));205fos.write(ba_image);206fos.flush();207fos = null;208ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);209210ImageReader ir = ImageIO.getImageReader(iw);211ir.setInput(ImageIO.createImageInputStream(bais));212213BufferedImage res = ir.read(0);214System.out.println("OK");215216if (!param.getCompressionType().equals("BI_JPEG")) {217System.out.print("compare images ... ");218boolean r = compare(img,res);219System.out.println(r?"OK":"FAILED");220if (!r) {221throw new RuntimeException("Compared images are not equals. Test failed.");222}223}224225226BMPMetadata mdata = (BMPMetadata)ir.getImageMetadata(0);227228if (!param.getCompressionType().equals(param.getCompressionTypes()[mdata.compression])) {229throw new RuntimeException("Different compression value");230}231232} catch (Exception ex) {233ex.printStackTrace();234throw new RuntimeException("Unexpected exception: " + ex);235}236237}238239private boolean compare(final BufferedImage in, final BufferedImage out) {240241final int width = in.getWidth();242int height = in.getHeight();243if (out.getWidth() != width || out.getHeight() != height) {244throw new RuntimeException("Dimensions changed!");245}246247Raster oldras = in.getRaster();248ColorModel oldcm = in.getColorModel();249Raster newras = out.getRaster();250ColorModel newcm = out.getColorModel();251252for (int j = 0; j < height; j++) {253for (int i = 0; i < width; i++) {254Object oldpixel = oldras.getDataElements(i, j, null);255int oldrgb = oldcm.getRGB(oldpixel);256int oldalpha = oldcm.getAlpha(oldpixel);257258Object newpixel = newras.getDataElements(i, j, null);259int newrgb = newcm.getRGB(newpixel);260int newalpha = newcm.getAlpha(newpixel);261262if (newrgb != oldrgb ||263newalpha != oldalpha) {264// showDiff(in, out);265throw new RuntimeException("Pixels differ at " + i +266", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));267}268}269}270return true;271}272273private static BufferedImage createTestImage2(int nbits, int transfertype) {274final int colorShift = 2;275int SIZE = 256;276BufferedImage image = null;277278ColorSpace colorSpace =279ColorSpace.getInstance(ColorSpace.CS_GRAY);280ColorModel colorModel =281new ComponentColorModel(colorSpace,282new int[] {nbits},283false,284false,285Transparency.OPAQUE,286transfertype);287288SampleModel sampleModel =289new PixelInterleavedSampleModel(transfertype,290SIZE,291SIZE,2921,293SIZE,294new int[] {0});295296image =297new BufferedImage(colorModel,298Raster.createWritableRaster(sampleModel, null),299false, null);300WritableRaster raster = image.getWritableTile(0, 0);301int[] samples = raster.getSamples(0, 0, SIZE, SIZE, 0, (int[])null);302int off = 0;303int[] row = new int[SIZE];304for(int i = 0; i < SIZE; i++) {305Arrays.fill(row, i << colorShift);306System.arraycopy(row, 0, samples, off, SIZE);307off += SIZE;308}309raster.setSamples(0, 0, SIZE, SIZE, 0, samples);310311return image;312}313314315private static BufferedImage createTestImage3(int nbits, int transfertype) {316final int colorShift = 2;317int SIZE = 256;318BufferedImage image = null;319320ColorSpace colorSpace =321ColorSpace.getInstance(ColorSpace.CS_sRGB);322ColorModel colorModel =323new IndexColorModel(nbits,3244,325new byte[] { (byte)255, 0, 0, (byte)255},326new byte[] { 0, (byte)255, 0, (byte)255},327new byte[] { 0, 0, (byte)255, (byte)255});328329SampleModel sampleModel =330new PixelInterleavedSampleModel(transfertype,331SIZE,332SIZE,3331,334SIZE,335new int[] {0});336337image =338new BufferedImage(colorModel,339Raster.createWritableRaster(sampleModel, null),340341false, null);342343Graphics2D g = image.createGraphics();344g.setColor(Color.white);345g.fillRect(0,0, SIZE, SIZE);346g.setColor(Color.red);347g.fillOval(10, 10, SIZE -20, SIZE-20);348349return image;350}351352private static BufferedImage createTestImage4(int nbits) {353int SIZE = 10;354355356BufferedImage image = null;357358ColorSpace colorSpace =359ColorSpace.getInstance(ColorSpace.CS_sRGB);360ColorModel colorModel =361new DirectColorModel(colorSpace,362nbits, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, false, DataBuffer.TYPE_INT);363364SampleModel sampleModel =365new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT,366SIZE,367SIZE,368new int[] { 0xff0000, 0x00ff00, 0x0000ff} );369370371image =372new BufferedImage(colorModel,373Raster.createWritableRaster(sampleModel, null),374375false, null);376377Graphics2D g = image.createGraphics();378g.setColor(Color.red);379g.fillRect(0,0, SIZE, SIZE);380g.setColor(Color.green);381//g.fillOval(10, 10, SIZE -20, SIZE-20);382g.drawLine(7, 0, 7, SIZE);383g.setColor(Color.blue);384g.drawLine(1, 0, 1, SIZE);385g.setColor(Color.white);386g.drawLine(3, 0, 3, SIZE);387g.setColor(Color.yellow);388g.drawLine(5, 0, 5, SIZE);389return image;390}391392private static BufferedImage createTestImage(int type)393throws IOException {394395int w = 200;396int h = 200;397BufferedImage b = new BufferedImage(w, h, type);398Graphics2D g = b.createGraphics();399g.setColor(Color.white);400g.fillRect(0,0, w, h);401g.setColor(Color.black);402g.fillOval(10, 10, w -20, h-20);403404return b;405}406407408}409410private static void showDiff(final BufferedImage in,411final BufferedImage out) {412final int width = in.getWidth();413final int height = in.getHeight();414415JFrame f = new JFrame("");416f.getContentPane().add( new JComponent() {417public Dimension getPreferredSize() {418return new Dimension(2*width+2, height);419}420public void paintComponent(Graphics g) {421g.setColor(Color.black);422g.drawImage(in, 0,0, null);423424g.drawImage(out, width+2, 0, null);425}426});427f.pack();428f.setVisible(true);429}430431}432433434