Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/RasterWithMinXTest.java
41152 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* @modules java.desktop/com.sun.imageio.plugins.jpeg28*/2930import java.awt.Rectangle;31import java.awt.image.BufferedImage;32import java.awt.image.RasterFormatException;33import java.awt.image.WritableRaster;34import java.io.ByteArrayOutputStream;35import java.util.Arrays;36import java.util.Iterator;3738import javax.imageio.IIOImage;39import javax.imageio.ImageIO;40import javax.imageio.ImageTypeSpecifier;41import javax.imageio.ImageWriteParam;42import javax.imageio.ImageWriter;43import javax.imageio.metadata.IIOMetadata;44import javax.imageio.stream.ImageOutputStream;45import javax.imageio.stream.MemoryCacheImageOutputStream;4647public class RasterWithMinXTest {4849public static void main(String[] args) {50String format = "jpeg";5152// Set output file.53ImageOutputStream output = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());5455// Create image.56BufferedImage bi = new BufferedImage(256, 256,57BufferedImage.TYPE_3BYTE_BGR);5859// Populate image.60int[] rgbArray = new int[256];61for(int i = 0; i < 256; i++) {62Arrays.fill(rgbArray, i);63bi.setRGB(0, i, 256, 1, rgbArray, 0, 256);64}6566// create translated raster in order to get non-zero minX and minY67WritableRaster r = (WritableRaster)bi.getRaster().createTranslatedChild(64,64);6869Iterator i = ImageIO.getImageWritersByFormatName(format);70ImageWriter iw = null;71while(i.hasNext() && iw == null) {72Object o = i.next();73if (o instanceof com.sun.imageio.plugins.jpeg.JPEGImageWriter) {74iw = (ImageWriter)o;75}76}77if (iw == null) {78throw new RuntimeException("No available image writer");79}8081ImageWriteParam iwp = iw.getDefaultWriteParam();82IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);8384IIOImage img = new IIOImage(r, null, metadata);8586iw.setOutput(output);87try {88iw.write(img);89} catch (RasterFormatException e) {90e.printStackTrace();91throw new RuntimeException("RasterException occurs. Test Failed!");92} catch (Exception ex) {93ex.printStackTrace();94throw new RuntimeException("Unexpected Exception");95}9697// test case of theImageWriteParam with non-null sourceRegion98iwp.setSourceRegion(new Rectangle(32,32,192,192));99metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(bi.getColorModel(), r.getSampleModel()), iwp);100try {101iw.write(metadata, img, iwp);102} catch (RasterFormatException e) {103e.printStackTrace();104throw new RuntimeException("SetSourceRegion causes the RasterException. Test Failed!");105} catch (Exception ex) {106ex.printStackTrace();107throw new RuntimeException("Unexpected Exception");108}109110}111}112113114