< prev index next >

test/jdk/javax/imageio/plugins/png/VerifyRGBValuesFromBKGDChunk.java

Print this page




   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     6574555
  27  * @summary Test verifies that PNGImageWriter encodes the R, G, B
  28  *          values of bKGD chunk properly.
  29  * @run     main VerifyRGBValuesFromBKGDChunk
  30  */
  31 
  32 import java.awt.image.BufferedImage;
  33 import java.io.IOException;
  34 import java.util.Iterator;
  35 import javax.imageio.ImageTypeSpecifier;
  36 import javax.imageio.ImageWriter;
  37 import javax.imageio.ImageReader;
  38 import javax.imageio.ImageIO;
  39 import javax.imageio.ImageWriteParam;

  40 import javax.imageio.metadata.IIOMetadata;
  41 import javax.imageio.metadata.IIOMetadataNode;
  42 import javax.imageio.IIOImage;
  43 import javax.imageio.stream.ImageInputStream;
  44 import javax.imageio.stream.ImageOutputStream;
  45 import java.io.ByteArrayInputStream;
  46 import java.io.ByteArrayOutputStream;
  47 import java.io.InputStream;
  48 
  49 public class VerifyRGBValuesFromBKGDChunk {
  50 
  51     public static void main(String[] args) throws IOException {
  52         int width = 1;
  53         int height = 1;
  54         BufferedImage img = new
  55             BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
  56         Iterator<ImageWriter> iterWriter =
  57             ImageIO.getImageWritersBySuffix("png");
  58         ImageWriter writer = iterWriter.next();













  59 
  60         ImageWriteParam param = writer.getDefaultWriteParam();
  61         ImageTypeSpecifier specifier =
  62             ImageTypeSpecifier.
  63                 createFromBufferedImageType(BufferedImage.TYPE_INT_BGR);
  64         IIOMetadata encodeMetadata =
  65             writer.getDefaultImageMetadata(specifier, param);
  66 
  67         // Write png image with bKGD chunk
  68         IIOMetadataNode bKGD_rgb = new IIOMetadataNode("bKGD_RGB");
  69         String red = "100";
  70         String green = "150";
  71         String blue = "200";
  72         bKGD_rgb.setAttribute("red", red);
  73         bKGD_rgb.setAttribute("green", green);
  74         bKGD_rgb.setAttribute("blue", blue);
  75 
  76         IIOMetadataNode bKGD = new IIOMetadataNode("bKGD");
  77         bKGD.appendChild(bKGD_rgb);
  78         IIOMetadataNode encodeRoot =
  79             new IIOMetadataNode("javax_imageio_png_1.0");
  80         encodeRoot.appendChild(bKGD);
  81 
  82         encodeMetadata.mergeTree("javax_imageio_png_1.0", encodeRoot);

  83 

  84         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  85         ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
  86         writer.setOutput(ios);
  87 
  88         writer.write(encodeMetadata,
  89                      new IIOImage(img, null, encodeMetadata), param);
  90         writer.dispose();
  91 
  92         baos.flush();
  93         byte[] imageByteArray = baos.toByteArray();
  94         baos.close();
  95 
  96         // Get bKGD chunk from image and verify the values
  97         InputStream input= new ByteArrayInputStream(imageByteArray);
  98         ImageInputStream iis = ImageIO.createImageInputStream(input);
  99         Iterator<ImageReader> iterReader =
 100             ImageIO.getImageReadersBySuffix("png");
 101         ImageReader reader = iterReader.next();
 102         reader.setInput(iis, false, false);
 103 
 104         IIOMetadata decodeMetadata = reader.getImageMetadata(0);
 105         IIOMetadataNode decodeRoot =
 106             (IIOMetadataNode) decodeMetadata.
 107                 getAsTree("javax_imageio_png_1.0");
 108         bKGD_rgb = (IIOMetadataNode)
 109             decodeRoot.getElementsByTagName("bKGD_RGB").item(0);
 110         reader.dispose();











































 111 
 112         if (!(red.equals(bKGD_rgb.getAttribute("red")) &&
 113               green.equals(bKGD_rgb.getAttribute("green")) &&
 114               blue.equals(bKGD_rgb.getAttribute("blue")))) {
 115             throw new RuntimeException("bKGD RGB values are not stored" +
 116             " properly");
 117         }
 118     }
 119 }
 120 


   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     6574555 5109146
  27  * @summary Test verifies that PNGImageWriter encodes the R, G, B
  28  *          values of bKGD chunk properly.
  29  * @run     main VerifyRGBValuesFromBKGDChunk
  30  */
  31 
  32 import java.awt.image.BufferedImage;
  33 import java.io.IOException;
  34 import java.util.Iterator;
  35 import javax.imageio.ImageTypeSpecifier;
  36 import javax.imageio.ImageWriter;
  37 import javax.imageio.ImageReader;
  38 import javax.imageio.ImageIO;
  39 import javax.imageio.ImageWriteParam;
  40 import javax.imageio.metadata.IIOInvalidTreeException;
  41 import javax.imageio.metadata.IIOMetadata;
  42 import javax.imageio.metadata.IIOMetadataNode;
  43 import javax.imageio.IIOImage;
  44 import javax.imageio.stream.ImageInputStream;
  45 import javax.imageio.stream.ImageOutputStream;
  46 import java.io.ByteArrayInputStream;
  47 import java.io.ByteArrayOutputStream;
  48 import java.io.InputStream;
  49 
  50 public class VerifyRGBValuesFromBKGDChunk {
  51 
  52     private static IIOMetadata encodeMetadata;
  53     private static ImageWriter writer;
  54     private static BufferedImage img;
  55     private static ImageWriteParam param;
  56     private static boolean nativeBKGDFail, standardBKGDFail;
  57     private static IIOMetadataNode bKGD_RGBNode;
  58     private static final String BKGDRED = "100";
  59     private static final String BKGDGREEN = "150";
  60     private static final String BKGDBLUE = "200";
  61 
  62     private static void mergeStandardMetadata() throws IIOInvalidTreeException {
  63         IIOMetadataNode background_rgb = new IIOMetadataNode("BackgroundColor");
  64 
  65         background_rgb.setAttribute("red", BKGDRED);
  66         background_rgb.setAttribute("green", BKGDGREEN);
  67         background_rgb.setAttribute("blue", BKGDBLUE);
  68 
  69         IIOMetadataNode chroma = new IIOMetadataNode("Chroma");
  70         chroma.appendChild(background_rgb);
  71         IIOMetadataNode encodeRoot = new IIOMetadataNode("javax_imageio_1.0");
  72         encodeRoot.appendChild(chroma);
  73 
  74         encodeMetadata.mergeTree("javax_imageio_1.0", encodeRoot);
  75     }




  76 
  77     private static void mergeNativeMetadata() throws IIOInvalidTreeException {
  78         IIOMetadataNode bKGD_rgb = new IIOMetadataNode("bKGD_RGB");
  79 
  80         bKGD_rgb.setAttribute("red", BKGDRED);
  81         bKGD_rgb.setAttribute("green", BKGDGREEN);
  82         bKGD_rgb.setAttribute("blue", BKGDBLUE);


  83 
  84         IIOMetadataNode bKGD = new IIOMetadataNode("bKGD");
  85         bKGD.appendChild(bKGD_rgb);
  86         IIOMetadataNode encodeRoot =
  87             new IIOMetadataNode("javax_imageio_png_1.0");
  88         encodeRoot.appendChild(bKGD);
  89 
  90         encodeMetadata.mergeTree("javax_imageio_png_1.0", encodeRoot);
  91     }
  92 
  93     private static void writeAndReadMetadata() throws IOException {
  94         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  95         ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
  96         writer.setOutput(ios);
  97 
  98         writer.write(encodeMetadata,
  99                      new IIOImage(img, null, encodeMetadata), param);
 100         writer.dispose();
 101 
 102         baos.flush();
 103         byte[] imageByteArray = baos.toByteArray();
 104         baos.close();
 105 
 106         // Get bKGD chunk from image and verify the values
 107         InputStream input= new ByteArrayInputStream(imageByteArray);
 108         ImageInputStream iis = ImageIO.createImageInputStream(input);
 109         Iterator<ImageReader> iterReader =
 110             ImageIO.getImageReadersBySuffix("png");
 111         ImageReader reader = iterReader.next();
 112         reader.setInput(iis, false, false);
 113 
 114         IIOMetadata decodeMetadata = reader.getImageMetadata(0);
 115         IIOMetadataNode decodeRoot =
 116             (IIOMetadataNode) decodeMetadata.
 117                 getAsTree("javax_imageio_png_1.0");
 118         bKGD_RGBNode = (IIOMetadataNode)
 119             decodeRoot.getElementsByTagName("bKGD_RGB").item(0);
 120         reader.dispose();
 121     }
 122 
 123     private static boolean verifyRGBValues() {
 124         return (!(BKGDRED.equals(bKGD_RGBNode.getAttribute("red")) &&
 125                   BKGDGREEN.equals(bKGD_RGBNode.getAttribute("green")) &&
 126                   BKGDBLUE.equals(bKGD_RGBNode.getAttribute("blue"))));
 127     }
 128 
 129     private static void VerifyNativeRGBValuesFromBKGDChunk()
 130         throws IOException {
 131 
 132         mergeNativeMetadata();
 133         writeAndReadMetadata();
 134         nativeBKGDFail = verifyRGBValues();
 135     }
 136 
 137     private static void VerifyStandardRGBValuesFromBKGDChunk()
 138         throws IOException {
 139 
 140         mergeStandardMetadata();
 141         writeAndReadMetadata();
 142         standardBKGDFail = verifyRGBValues();
 143     }
 144 
 145     public static void main(String[] args) throws IOException {
 146         int width = 1;
 147         int height = 1;
 148         img = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
 149         Iterator<ImageWriter> iterWriter =
 150             ImageIO.getImageWritersBySuffix("png");
 151         writer = iterWriter.next();
 152 
 153         param = writer.getDefaultWriteParam();
 154         ImageTypeSpecifier specifier =
 155             ImageTypeSpecifier.
 156                 createFromBufferedImageType(BufferedImage.TYPE_INT_BGR);
 157         encodeMetadata = writer.getDefaultImageMetadata(specifier, param);
 158 
 159         // Verify bKGD RGB values after merging metadata using native tree
 160         VerifyNativeRGBValuesFromBKGDChunk();
 161 
 162         // Verify bKGD RGB values after merging metadata using standard tree
 163         VerifyStandardRGBValuesFromBKGDChunk();
 164 
 165         if (nativeBKGDFail || standardBKGDFail) {


 166             throw new RuntimeException("bKGD RGB values are not stored" +
 167                 " properly");
 168         }
 169     }
 170 }
 171 
< prev index next >