1 /*
   2  * Copyright (c) 2016, 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 8164750
  27  * @summary Verify reader does not fail when the BaselineTIFFTagSet is
  28  *          removed via the TIFFImageReadParam both when ignoring and
  29  *          not ignoring metadata.
  30  */
  31 
  32 import java.awt.image.BufferedImage;
  33 import java.io.ByteArrayInputStream;
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.IOException;
  36 import java.util.Map;
  37 import javax.imageio.IIOImage;
  38 import javax.imageio.ImageIO;
  39 import javax.imageio.ImageReader;
  40 import javax.imageio.ImageWriteParam;
  41 import javax.imageio.ImageWriter;
  42 import javax.imageio.plugins.tiff.BaselineTIFFTagSet;
  43 import javax.imageio.plugins.tiff.TIFFImageReadParam;
  44 import javax.imageio.stream.ImageInputStream;
  45 import javax.imageio.stream.ImageOutputStream;
  46 import javax.imageio.stream.MemoryCacheImageInputStream;
  47 import javax.imageio.stream.MemoryCacheImageOutputStream;
  48 
  49 public class ReadWithoutBaselineTagSet {
  50     private static final int WIDTH = 47;
  51     private static final int HEIGHT = 53;
  52 
  53     private static final Map<Integer,String[]> typeToCompression =
  54         Map.of(BufferedImage.TYPE_3BYTE_BGR,
  55             new String[] {null, "LZW", "JPEG", "ZLib", "PackBits"},
  56             BufferedImage.TYPE_BYTE_BINARY,
  57             new String[] {null, "CCITT RLE", "CCITT T.4", "CCITT T.6",
  58                 "LZW", "PackBits"},
  59             BufferedImage.TYPE_BYTE_GRAY,
  60             new String[] {null, "LZW", "JPEG", "ZLib", "PackBits"},
  61             BufferedImage.TYPE_USHORT_GRAY,
  62             new String[] {null, "LZW", "ZLib", "PackBits"});
  63 
  64     public static void main(String[] args) throws IOException {
  65         test();
  66     }
  67 
  68     private static void test() throws IOException {
  69         int failures = 0;
  70 
  71         for (int imageType : typeToCompression.keySet()) {
  72             BufferedImage image = new BufferedImage(WIDTH, HEIGHT, imageType);
  73             System.out.println("Image: " + image.toString());
  74 
  75             for (String compression : typeToCompression.get(imageType)) {
  76                 System.out.println("Compression: "
  77                         + (compression == null ? "None" : compression));
  78                 ByteArrayOutputStream output = new ByteArrayOutputStream();
  79                 ImageOutputStream ios = new MemoryCacheImageOutputStream(output);
  80                 ImageWriter writer =
  81                         ImageIO.getImageWritersByFormatName("TIFF").next();
  82                 ImageWriteParam wparam = writer.getDefaultWriteParam();
  83                 if (compression == null) {
  84                     wparam.setCompressionMode(ImageWriteParam.MODE_DEFAULT);
  85                 } else {
  86                     wparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  87                     wparam.setCompressionType(compression);
  88                 }
  89                 writer.setOutput(ios);
  90                 writer.write(null, new IIOImage(image, null, null), wparam);
  91                 ios.flush();
  92 
  93                 ImageReader reader =
  94                         ImageIO.getImageReadersByFormatName("TIFF").next();
  95                 ByteArrayInputStream input
  96                         = new ByteArrayInputStream(output.toByteArray());
  97                 ImageInputStream iis = new MemoryCacheImageInputStream(input);
  98                 iis.mark();
  99 
 100                 TIFFImageReadParam rparam = new TIFFImageReadParam();
 101                 rparam.removeAllowedTagSet(BaselineTIFFTagSet.getInstance());
 102 
 103                 reader.setInput(iis, false, false);
 104                 BufferedImage resultFalse = reader.read(0, rparam);
 105                 if (resultFalse.getWidth() != WIDTH
 106                         || resultFalse.getHeight() != HEIGHT) {
 107                     System.err.printf("Read image dimensions != %d x %d%n",
 108                             WIDTH, HEIGHT);
 109                     failures++;
 110                 } else {
 111                     System.out.println("ignoreMetadata == false test passed");
 112                 }
 113 
 114                 iis.reset();
 115                 reader.setInput(iis, false, true);
 116                 BufferedImage resultTrue;
 117                 try {
 118                     resultTrue = reader.read(0, rparam);
 119                     if (resultTrue.getWidth() != WIDTH
 120                             || resultTrue.getHeight() != HEIGHT) {
 121                         System.err.printf("Read image dimensions != %d x %d%n",
 122                                 WIDTH, HEIGHT);
 123                         failures++;
 124                     } else {
 125                         System.out.println("ignoreMetadata == true test passed");
 126                     }
 127                 } catch (Exception e) {
 128                     e.printStackTrace();
 129                     failures++;
 130                 }
 131             }
 132         }
 133 
 134         if (failures != 0) {
 135             throw new RuntimeException("Test failed with "
 136                     + failures + " errors!");
 137         }
 138     }
 139 }