1 /*
   2  * Copyright (c) 2007, 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     4413109 4418221 6607198 8147448
  27  * @run     main BitDepth
  28  * @summary Checks that ImageIO writers for standard formats can handle
  29  *          various BufferedImage RGB types. An optional list of arguments
  30  *          may be used to test the writers for a different list of formats.
  31  */
  32 
  33 import java.awt.Color;
  34 import java.awt.Graphics2D;
  35 import java.awt.image.BufferedImage;
  36 import java.io.File;
  37 import java.io.IOException;
  38 import javax.imageio.ImageIO;
  39 
  40 public class BitDepth {
  41 
  42     public static void main(String[] args) throws IOException {
  43         new BitDepth(args);
  44     }
  45 
  46     // Check that the PNG writer can write an all-white image correctly
  47     private static boolean testPNGByteBinary() throws IOException {
  48         int width = 10;
  49         int height = 10;
  50 
  51         File f = new File("BlackStripe.png");
  52         BufferedImage bi = new BufferedImage(width, height,
  53                                              BufferedImage.TYPE_BYTE_BINARY);
  54         Graphics2D g = bi.createGraphics();
  55         g.setColor(new Color(255, 255, 255));
  56         g.fillRect(0, 0, width, height);
  57 
  58         ImageIO.write(bi, "png", f);
  59         BufferedImage bi2 = ImageIO.read(f);
  60         if (bi2.getWidth() != width || bi2.getHeight() != height) {
  61             System.out.println("Dimensions changed!");
  62             return false;
  63         }
  64 
  65         for (int y = 0; y < height; y++) {
  66             for (int x = 0; x < width; x++) {
  67                 int rgb = bi2.getRGB(x, y);
  68                 if (rgb != 0xffffffff) {
  69                     System.out.println("Found a non-white pixel!");
  70                     return false;
  71                 }
  72             }
  73         }
  74 
  75         f.delete();
  76         return true;
  77     }
  78 
  79     private static final int[] biRGBTypes = {
  80         BufferedImage.TYPE_INT_RGB,
  81         BufferedImage.TYPE_INT_BGR,
  82         BufferedImage.TYPE_3BYTE_BGR,
  83         BufferedImage.TYPE_USHORT_565_RGB,
  84         BufferedImage.TYPE_USHORT_555_RGB,
  85         BufferedImage.TYPE_INT_ARGB,
  86         BufferedImage.TYPE_INT_ARGB_PRE,
  87         BufferedImage.TYPE_4BYTE_ABGR,
  88         BufferedImage.TYPE_4BYTE_ABGR_PRE
  89     };
  90 
  91     //private static final int[] biGrayTypes = {
  92     //    BufferedImage.TYPE_BYTE_GRAY,
  93     //    BufferedImage.TYPE_USHORT_GRAY,
  94     //    BufferedImage.TYPE_BYTE_BINARY
  95     //};
  96 
  97 
  98     private static final String[] biTypeNames = {
  99         "CUSTOM",
 100         "INT_RGB",
 101         "INT_ARGB",
 102         "INT_ARGB_PRE",
 103         "INT_BGR",
 104         "3BYTE_BGR",
 105         "4BYTE_ABGR",
 106         "4BYTE_ABGR_PRE",
 107         "USHORT_565_RGB",
 108         "USHORT_555_RGB",
 109         "BYTE_GRAY",
 110         "USHORT_GRAY",
 111         "BYTE_BINARY",
 112         "BYTE_INDEXED"
 113     };
 114 
 115     private int width = 80;
 116     private int height = 80;
 117     private String[] format = { "png", "jpeg", "tif", "bmp", "gif" };
 118 
 119     public BitDepth(String[] args) throws IOException {
 120         if (args.length > 0) {
 121             format = args;
 122         }
 123 
 124         for (int i = 0; i < format.length; i++) {
 125             testFormat(format[i]);
 126         }
 127     }
 128 
 129     private void testFormat(String format) throws IOException {
 130 
 131         boolean allOK = true;
 132 
 133         for (int type : biRGBTypes) {
 134             // TODO: remove the following 'if' block after the 8147448 fix
 135             if ( format.toLowerCase().equals("bmp") && (
 136                 (type == BufferedImage.TYPE_INT_ARGB       ) ||
 137                 (type == BufferedImage.TYPE_INT_ARGB_PRE   ) ||
 138                 (type == BufferedImage.TYPE_4BYTE_ABGR     ) ||
 139                 (type == BufferedImage.TYPE_4BYTE_ABGR_PRE ))) {
 140 
 141                 System.err.println("cannot use " + biTypeNames[type] +
 142                 " for bmp because of JDK-8147448.\t" +
 143                 " please update the test after fix of this bug!");
 144                 continue;
 145             }
 146 
 147 
 148             System.out.println("Testing " + format +
 149                                " writer for type " + biTypeNames[type]);
 150             boolean ok = false;
 151             File f = null;
 152             try {
 153                 f = testWriteRGB(format, type);
 154                 ok = testReadRGB(f);
 155             } catch (javax.imageio.IIOException e) {
 156                 // The follow two formats are not supported on OpenJDK
 157                 // for jpg files.
 158                 if (format.toLowerCase().equals("jpg") &&
 159                     (type == BufferedImage.TYPE_4BYTE_ABGR ||
 160                      type == BufferedImage.TYPE_4BYTE_ABGR_PRE))
 161                     continue;
 162             } finally {
 163                 if (ok) {
 164                     f.delete();
 165                 }
 166                 allOK = allOK && (ok || f == null);
 167             }
 168         }
 169 
 170 
 171 
 172         if (format.equals("png")) {
 173             System.out.println("Testing png writer for black stripe");
 174             boolean ok = testPNGByteBinary();
 175             allOK = allOK && ok;
 176         }
 177 
 178         if (!allOK) {
 179             throw new RuntimeException("Test failed");
 180         }
 181     }
 182 
 183     private File testWriteRGB(String format, int type) throws IOException {
 184 
 185         BufferedImage bi = new BufferedImage(width, height, type);
 186         Graphics2D g = bi.createGraphics();
 187 
 188         Color white = new Color(255, 255, 255);
 189         Color red = new Color(255, 0, 0);
 190         Color green = new Color(0, 255, 0);
 191         Color blue = new Color(0, 0, 255);
 192 
 193         g.setColor(white);
 194         g.fillRect(0, 0, width, height);
 195         g.setColor(red);
 196         g.fillRect(10, 10, 20, 20);
 197         g.setColor(green);
 198         g.fillRect(30, 30, 20, 20);
 199         g.setColor(blue);
 200         g.fillRect(50, 50, 20, 20);
 201 
 202         File file = new File("BitDepth_" + biTypeNames[type] + "." + format);
 203         try {
 204             ImageIO.write(bi, format, file);
 205         } catch (RuntimeException re) {
 206             System.out.println("Can't write a type "
 207                                + biTypeNames[type] +
 208                                " BufferedImage!");
 209         }
 210 
 211         return file;
 212     }
 213 
 214     private int colorDistance(int color, int r, int g, int b) {
 215         int r0 = ((color >> 16) & 0xff) - r;
 216         int g0 = ((color >> 8) & 0xff) - g;
 217         int b0 = (color & 0xff) - b;
 218         return r0*r0 + g0*g0 + b0*b0;
 219     }
 220 
 221     private boolean testReadRGB(File file) throws IOException {
 222         int[] rgb = new int[3];
 223 
 224         BufferedImage bi = ImageIO.read(file);
 225         if (bi == null) {
 226             System.out.println("Couldn't read image!");
 227             return false;
 228         }
 229         int r = bi.getRGB(15, 15);
 230         if (colorDistance(r, 255, 0, 0) > 20) {
 231             System.out.println("Red was distorted!");
 232             return false;
 233         }
 234         int g = bi.getRGB(35, 35);
 235         if (colorDistance(g, 0, 255, 0) > 20) {
 236             System.out.println("Green was distorted!");
 237             return false;
 238         }
 239         int b = bi.getRGB(55, 55);
 240         if (colorDistance(b, 0, 0, 255) > 20) {
 241             System.out.println("Blue was distorted!");
 242             return false;
 243         }
 244         int w = bi.getRGB(55, 15);
 245         if (colorDistance(w, 255, 255, 255) > 20) {
 246             System.out.println("White was distorted!");
 247             return false;
 248         }
 249 
 250         return true;
 251     }
 252 }