1 /*
   2  * Copyright (c) 2005, 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 6286578
  27  * @summary Test verifies that RGB images does not convert to gray-scaled if
  28  *          default image metadata is used
  29  */
  30 
  31 import java.awt.Color;
  32 import java.awt.Graphics;
  33 import java.awt.image.BufferedImage;
  34 import java.io.ByteArrayInputStream;
  35 import java.io.ByteArrayOutputStream;
  36 import java.io.IOException;
  37 
  38 import javax.imageio.IIOImage;
  39 import javax.imageio.ImageIO;
  40 import javax.imageio.ImageReader;
  41 import javax.imageio.ImageTypeSpecifier;
  42 import javax.imageio.ImageWriteParam;
  43 import javax.imageio.ImageWriter;
  44 import javax.imageio.metadata.IIOMetadata;
  45 import javax.imageio.stream.ImageInputStream;
  46 import javax.imageio.stream.ImageOutputStream;
  47 
  48 public class RGBImageTest {
  49 
  50     Color[] usedColors = {
  51         Color.red, Color.green, Color.blue, Color.yellow,
  52         Color.cyan, Color.magenta, Color.white, Color.black };
  53 
  54     BufferedImage src = null;
  55     int dx= 20;
  56     int height = 100;
  57 
  58     protected BufferedImage getSrc() {
  59         if (src == null) {
  60             src = new BufferedImage(dx * usedColors.length, height,
  61                                     BufferedImage.TYPE_INT_RGB);
  62             Graphics g = src.createGraphics();
  63             for (int i = 0; i < usedColors.length; i++) {
  64                 g.setColor(usedColors[i]);
  65                 g.fillRect(dx * i,  0, dx, height);
  66             }
  67         }
  68         return src;
  69     }
  70 
  71     protected void doTest() throws IOException {
  72         BufferedImage biSrc = getSrc();
  73 
  74         ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
  75         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  76         ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
  77         writer.setOutput(ios);
  78 
  79         ImageWriteParam writeParam = writer.getDefaultWriteParam();
  80         IIOMetadata imageMetadata =
  81             writer.getDefaultImageMetadata(new ImageTypeSpecifier(biSrc), writeParam);
  82 
  83         IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(writeParam);
  84 
  85         IIOImage iioImg = new IIOImage(biSrc, null, imageMetadata);
  86         writer.write(streamMetadata, iioImg, writeParam);
  87         ios.close();
  88 
  89         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  90         ImageInputStream iis = ImageIO.createImageInputStream(bais);
  91         ImageReader reader = ImageIO.getImageReader(writer);
  92         reader.setInput(iis);
  93         BufferedImage dst = reader.read(0);
  94 
  95         // do test
  96         int x = dx / 2;
  97         int y = height / 2;
  98 
  99         for (int i = 0; i < usedColors.length; i++) {
 100             int dstRgb = dst.getRGB(x, y);
 101             System.out.println("dstColor: " + Integer.toHexString(dstRgb));
 102             int srcRgb = usedColors[i].getRGB();
 103             System.out.println("srcColor: " + Integer.toHexString(srcRgb));
 104             if (dstRgb != srcRgb) {
 105                 throw new RuntimeException("wrong color " + i + ": " + Integer.toHexString(dstRgb));
 106             }
 107             x += dx;
 108         }
 109 
 110     }
 111 
 112     public static void main(String[] args) throws IOException {
 113         RGBImageTest t = new RGBImageTest();
 114         t.doTest();
 115     }
 116 }