Path: blob/master/test/jdk/javax/imageio/plugins/bmp/BMPWriteParamTest.java
41153 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.Graphics2D;31import java.awt.Rectangle;32import java.awt.image.BufferedImage;33import java.awt.image.ColorModel;34import java.awt.image.Raster;35import java.io.ByteArrayInputStream;36import java.io.ByteArrayOutputStream;37import java.io.IOException;38import java.util.Iterator;3940import javax.imageio.IIOImage;41import javax.imageio.ImageIO;42import javax.imageio.ImageReader;43import javax.imageio.ImageTypeSpecifier;44import javax.imageio.ImageWriteParam;45import javax.imageio.ImageWriter;46import javax.imageio.metadata.IIOMetadata;47import javax.imageio.stream.ImageOutputStream;4849public class BMPWriteParamTest {5051static final String format = "BMP";5253public static void main(String[] args) {5455ImageWriter iw = null;56Iterator writers = ImageIO.getImageWritersByFormatName(format);57if (!writers.hasNext()) {58throw new RuntimeException("No available Image writer for "+format);59}60iw = (ImageWriter)writers.next();6162try {63BufferedImage img = createTestImage();6465BufferedImage bmp_res = getWriteResult(img, "BMP");66BufferedImage png_res = getWriteResult(img, "PNG");6768compare(bmp_res, png_res);69} catch (Exception ex) {70ex.printStackTrace();71throw new RuntimeException("Unexpected exception: " + ex);72}73}7475private static BufferedImage getWriteResult(BufferedImage img,76String format77) throws IOException {78ImageWriter iw = null;79Iterator writers = ImageIO.getImageWritersByFormatName(format);80while (writers.hasNext()) {81iw = (ImageWriter)writers.next();82System.out.println(format + " -> " + iw.toString());83}84if (iw==null) {85throw new RuntimeException("No available Image writer for "+format);86}87ImageWriteParam param = iw.getDefaultWriteParam();8889param.setSourceRegion(new Rectangle(10, 10, 31, 31));90param.setSourceSubsampling(3, 3, 0, 0);9192IIOMetadata meta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);9394IIOImage iio_img = new IIOImage(img, null, meta);9596ByteArrayOutputStream baos = new ByteArrayOutputStream();97ImageOutputStream ios = ImageIO.createImageOutputStream(baos);98iw.setOutput(ios);99iw.write(meta, iio_img, param);100ios.flush();101102byte[] ba_image = baos.toByteArray();103104ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);105106ImageReader ir = null;107108Iterator readers = ImageIO.getImageReadersByFormatName(format);109while (readers.hasNext()) {110ir = (ImageReader)readers.next();111System.out.println(format + " -> " + ir.toString());112}113if (ir==null) {114throw new RuntimeException("No available Image reader for "+format);115}116117ir.setInput(ImageIO.createImageInputStream(bais));118119BufferedImage res = ir.read(0);120return res;121}122123private static BufferedImage createTestImage()124throws IOException {125126int w = 50;127int h = 50;128BufferedImage b = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);129Graphics2D g = b.createGraphics();130g.setColor(Color.red);131g.fillRect(0,0, w, h);132g.setColor(Color.white);133for (int i=10; i<=10+30; i+= 3) {134g.drawLine(i, 10, i, 40);135g.drawLine(10, i, 40, i);136}137return b;138}139140private static boolean compare(final BufferedImage in,141final BufferedImage out)142{143final int width = in.getWidth();144int height = in.getHeight();145if (out.getWidth() != width || out.getHeight() != height) {146throw new RuntimeException("Dimensions changed!");147}148149Raster oldras = in.getRaster();150ColorModel oldcm = in.getColorModel();151Raster newras = out.getRaster();152ColorModel newcm = out.getColorModel();153154for (int j = 0; j < height; j++) {155for (int i = 0; i < width; i++) {156Object oldpixel = oldras.getDataElements(i, j, null);157int oldrgb = oldcm.getRGB(oldpixel);158int oldalpha = oldcm.getAlpha(oldpixel);159160Object newpixel = newras.getDataElements(i, j, null);161int newrgb = newcm.getRGB(newpixel);162int newalpha = newcm.getAlpha(newpixel);163164if (newrgb != oldrgb ||165newalpha != oldalpha) {166// showDiff(in, out);167throw new RuntimeException("Pixels differ at " + i +168", " + j + " new = " + Integer.toHexString(newrgb) + " old = " + Integer.toHexString(oldrgb));169}170}171}172return true;173}174}175176177