1 /* 2 * Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 5028259 27 * @summary Verifies that usage of the destination type does not cause the 28 * increase of size of the result jpeg file 29 */ 30 31 import java.awt.Color; 32 import java.awt.Graphics; 33 import java.awt.image.BufferedImage; 34 import java.io.ByteArrayOutputStream; 35 import java.io.IOException; 36 37 import javax.imageio.IIOImage; 38 import javax.imageio.ImageIO; 39 import javax.imageio.ImageReader; 40 import javax.imageio.ImageTypeSpecifier; 41 import javax.imageio.ImageWriteParam; 42 import javax.imageio.ImageWriter; 43 import javax.imageio.event.IIOReadWarningListener; 44 import javax.imageio.event.IIOWriteWarningListener; 45 import javax.imageio.metadata.IIOMetadata; 46 import javax.imageio.stream.ImageOutputStream; 47 48 public class DestTypeTest implements IIOWriteWarningListener, IIOReadWarningListener { 49 50 ImageWriter w; 51 ImageReader r; 52 53 public static void main(String[] args) throws IOException { 54 BufferedImage bi_rgb = createTestImage(BufferedImage.TYPE_INT_RGB); 55 56 DestTypeTest bug = new DestTypeTest(); 57 byte[] rgb_data = bug.writeTest(bi_rgb); 58 59 System.out.println("rgb jpeg data length is " + rgb_data.length); 60 61 BufferedImage bi_argb = createTestImage(BufferedImage.TYPE_INT_ARGB); 62 63 ImageWriteParam p = bug.getWriteParam(); 64 IIOMetadata m = bug.getMetadata(p); 65 66 byte[] submeta_data = bug.writeTest(bi_argb, p, m); 67 System.out.println("desttype and metadata jpeg data length is " + submeta_data.length); 68 69 p = bug.getWriteParam(); 70 byte[] subbanded_data = bug.writeTest(bi_argb, p); 71 System.out.println("desttype jpeg data length is " + subbanded_data.length); 72 73 if (submeta_data.length > rgb_data.length) { 74 throw new RuntimeException("Too big result jpeg: " + submeta_data.length + 75 "(rgb image size is " + rgb_data.length + ")"); 76 } 77 if (subbanded_data.length > rgb_data.length) { 78 throw new RuntimeException("Too big result jpeg: " + subbanded_data.length + 79 "(rgb image size is " + rgb_data.length + ")"); 80 } 81 } 82 83 public DestTypeTest() { 84 w = (ImageWriter) 85 ImageIO.getImageWritersByFormatName("jpeg").next(); 86 w.addIIOWriteWarningListener(this); 87 88 r = (ImageReader) 89 ImageIO.getImageReadersByFormatName("jpeg").next(); 90 r.addIIOReadWarningListener(this); 91 } 92 93 public ImageWriteParam getWriteParam() { 94 ImageWriteParam p = w.getDefaultWriteParam(); 95 p.setSourceBands(new int[] {0, 1, 2}); 96 ImageTypeSpecifier type = 97 ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB); 98 p.setDestinationType(type); 99 100 return p; 101 } 102 103 public IIOMetadata getMetadata(ImageWriteParam p) { 104 return w.getDefaultImageMetadata(p.getDestinationType(), null); 105 } 106 107 public byte[] writeTest(BufferedImage bi) throws IOException { 108 return writeTest(bi, null); 109 } 110 111 public byte[] writeTest(BufferedImage bi, 112 ImageWriteParam p) throws IOException { 113 return writeTest(bi, p, null); 114 } 115 public byte[] writeTest(BufferedImage bi, 116 ImageWriteParam p, 117 IIOMetadata m) throws IOException { 118 ByteArrayOutputStream baos = 119 new ByteArrayOutputStream(); 120 121 // write test image as jpeg 122 ImageOutputStream ios = 123 ImageIO.createImageOutputStream(baos); 124 w.setOutput(ios); 125 w.write(null, 126 new IIOImage(bi, null, m), 127 p); 128 ios.close(); 129 return baos.toByteArray(); 130 } 131 132 public static BufferedImage createTestImage(int type) { 133 int w = 100; 134 int h = 500; 135 BufferedImage bi = new BufferedImage(3*w, h, type); 136 Graphics g = bi.createGraphics(); 137 g.setColor(Color.red); 138 g.fillRect(0,0,w,h); 139 g.setColor(Color.green); 140 g.fillRect(w, 0,w,h); 141 g.setColor(Color.blue); 142 g.fillRect(2*w,0,w,h); 143 144 return bi; 145 } 146 147 public void warningOccurred(ImageWriter source, 148 int imageIndex, 149 String warning) { 150 System.out.println("WRITING WARNING: " + warning); 151 } 152 153 public void warningOccurred(ImageReader source, 154 String warning) { 155 System.out.println("READING WARNING: " + warning); 156 } 157 }