Path: blob/master/test/jdk/javax/imageio/plugins/tiff/IFDTest.java
41153 views
/*1* Copyright (c) 2016, 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 815296626* @summary Verify that casting of TIFFDirectory to TIFFIFD has been fixed.27*/2829import java.awt.Color;30import java.awt.Graphics;31import java.awt.image.BufferedImage;32import java.io.*;33import javax.imageio.*;34import javax.imageio.metadata.*;35import javax.imageio.stream.*;363738import javax.imageio.plugins.tiff.*;394041public class IFDTest {4243private ImageWriter getTIFFWriter() {4445java.util.Iterator<ImageWriter> writers =46ImageIO.getImageWritersByFormatName("TIFF");47if (!writers.hasNext()) {48throw new RuntimeException("No readers available for TIFF format");49}50return writers.next();51}5253private void writeImage() throws Exception {5455File file = File.createTempFile("IFDTest", "tif", new File("."));56file.deleteOnExit();5758OutputStream s = new BufferedOutputStream(59new FileOutputStream("test.tiff"));60try (ImageOutputStream ios = ImageIO.createImageOutputStream(s)) {6162ImageWriter writer = getTIFFWriter();63writer.setOutput(ios);6465BufferedImage img = new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);66Graphics g = img.getGraphics();67g.setColor(Color.GRAY);68g.fillRect(0, 0, 20, 20);69g.dispose();7071IIOMetadata metadata = writer.getDefaultImageMetadata(72new ImageTypeSpecifier(img), writer.getDefaultWriteParam());7374TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);7576int type = TIFFTag.TIFF_IFD_POINTER;77int nTag = ExifParentTIFFTagSet.TAG_EXIF_IFD_POINTER;78TIFFTag tag = new TIFFTag("Exif IFD", nTag, 1 << type);79TIFFTagSet sets[] = {ExifTIFFTagSet.getInstance()};8081TIFFField f = new TIFFField(tag, type, 42L, new TIFFDirectory(sets, tag));82dir.addTIFFField(f);8384writer.write(new IIOImage(img, null, dir.getAsMetadata()));8586ios.flush();87writer.dispose();88} finally {89s.close();90file.delete();91}92}939495public static void main(String[] args) throws Exception {96(new IFDTest()).writeImage();97}98}99100101