Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/plugins/jpeg/RasterWithMinXTest.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 484389526* @summary Tests that we handle raster with non-zero minX and minY correctly27*/2829import java.awt.Rectangle;30import java.awt.image.BufferedImage;31import java.awt.image.RasterFormatException;32import java.awt.image.WritableRaster;33import java.io.ByteArrayOutputStream;34import java.util.Arrays;35import java.util.Iterator;3637import javax.imageio.IIOImage;38import javax.imageio.ImageIO;39import javax.imageio.ImageTypeSpecifier;40import javax.imageio.ImageWriteParam;41import javax.imageio.ImageWriter;42import javax.imageio.metadata.IIOMetadata;43import javax.imageio.stream.ImageOutputStream;44import javax.imageio.stream.MemoryCacheImageOutputStream;4546public class RasterWithMinXTest {4748public static void main(String[] args) {49String format = "jpeg";5051// Set output file.52ImageOutputStream output = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());5354// Create image.55BufferedImage bi = new BufferedImage(256, 256,56BufferedImage.TYPE_3BYTE_BGR);5758// Populate image.59int[] rgbArray = new int[256];60for(int i = 0; i < 256; i++) {61Arrays.fill(rgbArray, i);62bi.setRGB(0, i, 256, 1, rgbArray, 0, 256);63}6465// create translated raster in order to get non-zero minX and minY66WritableRaster r = (WritableRaster)bi.getRaster().createTranslatedChild(64,64);6768Iterator i = ImageIO.getImageWritersByFormatName(format);69ImageWriter iw = null;70while(i.hasNext() && iw == null) {71Object o = i.next();72if (o instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter) {73iw = (ImageWriter)o;74}75}76if (iw == null) {77throw new RuntimeException("No available image writer");78}7980ImageWriteParam iwp = iw.getDefaultWriteParam();81IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);8283IIOImage img = new IIOImage(r, null, metadata);8485iw.setOutput(output);86try {87iw.write(img);88} catch (RasterFormatException e) {89e.printStackTrace();90throw new RuntimeException("RasterException occurs. Test Failed!");91} catch (Exception ex) {92ex.printStackTrace();93throw new RuntimeException("Unexpected Exception");94}9596// test case of theImageWriteParam with non-null sourceRegion97iwp.setSourceRegion(new Rectangle(32,32,192,192));98metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);99try {100iw.write(metadata, img, iwp);101} catch (RasterFormatException e) {102e.printStackTrace();103throw new RuntimeException("SetSourceRegion causes the RasterException. Test Failed!");104} catch (Exception ex) {105ex.printStackTrace();106throw new RuntimeException("Unexpected Exception");107}108109}110}111112113