< prev index next >

test/javax/imageio/plugins/shared/BitDepth.java

Print this page


   1 /*
   2  * Copyright (c) 2007, 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
  27  * @run     main BitDepth
  28  * @summary Checks that the PNG and JPEG writers can handle various
  29  * BufferedImage types.  An optional list of arguments may be used to
  30  * test a different format writer or writers.
  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 


  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     };
  86 
  87     private static final int[] biRGBATypes = {
  88         BufferedImage.TYPE_INT_ARGB,
  89         BufferedImage.TYPE_INT_ARGB_PRE,
  90         BufferedImage.TYPE_4BYTE_ABGR,
  91         BufferedImage.TYPE_4BYTE_ABGR_PRE
  92     };
  93 
  94     private static final int[] biGrayTypes = {
  95         BufferedImage.TYPE_BYTE_GRAY,
  96         BufferedImage.TYPE_USHORT_GRAY,
  97         BufferedImage.TYPE_BYTE_BINARY
  98     };

  99 
 100     private static final String[] biTypeNames = {
 101         "CUSTOM",
 102         "INT_RGB",
 103         "INT_ARGB",
 104         "INT_ARGB_PRE",
 105         "INT_BGR",
 106         "3BYTE_BGR",
 107         "4BYTE_ABGR",
 108         "4BYTE_ABGR_PRE",
 109         "USHORT_565_RGB",
 110         "USHORT_555_RGB",
 111         "BYTE_GRAY",
 112         "USHORT_GRAY",
 113         "BYTE_BINARY",
 114         "BYTE_INDEXED"
 115     };
 116 
 117     private int width = 80;
 118     private int height = 80;
 119     private String[] format = { "png", "jpeg" };
 120 
 121     public BitDepth(String[] args) throws IOException {
 122         if (args.length > 0) {
 123             format = args;
 124         }
 125 
 126         for (int i = 0; i < format.length; i++) {
 127             testFormat(format[i]);
 128         }
 129     }
 130 
 131     private void testFormat(String format) throws IOException {

 132         boolean allOK = true;
 133 
 134         for (int i = 0; i < biRGBTypes.length; i++) {

 135             int type = biRGBTypes[i];
















 136             System.out.println("Testing " + format +
 137                                " writer for type " + biTypeNames[type]);
 138             File f = testWriteRGB(format, type);
 139             boolean ok = testReadRGB(f);
 140             if (ok) {
 141                 f.delete();
 142             }
 143             allOK = allOK && ok;
 144         }
 145 


 146         if (format.equals("png")) {
 147             System.out.println("Testing png writer for black stripe");
 148             boolean ok = testPNGByteBinary();
 149             allOK = allOK && ok;
 150         }
 151 
 152         if (!allOK) {
 153             throw new RuntimeException("Test failed");
 154         }
 155     }
 156 
 157     private File testWriteRGB(String format, int type)
 158         throws IOException {
 159         BufferedImage bi = new BufferedImage(width, height, type);
 160         Graphics2D g = bi.createGraphics();
 161 
 162         Color white = new Color(255, 255, 255);
 163         Color red = new Color(255, 0, 0);
 164         Color green = new Color(0, 255, 0);
 165         Color blue = new Color(0, 0, 255);
 166 
 167         g.setColor(white);
 168         g.fillRect(0, 0, width, height);
 169         g.setColor(red);
 170         g.fillRect(10, 10, 20, 20);
 171         g.setColor(green);
 172         g.fillRect(30, 30, 20, 20);
 173         g.setColor(blue);
 174         g.fillRect(50, 50, 20, 20);
 175 
 176         File file = new File("BitDepth_" + biTypeNames[type] + "." + format);
 177         try {
 178             ImageIO.write(bi, format, file);


   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 


  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 i = 0; i < biRGBTypes.length; i++) {
 134 
 135             int type = biRGBTypes[i];
 136 
 137 
 138             // TODO: remove the following 'if' block after the 8147448 fix
 139             if ( format.toLowerCase().equals("bmp") && (
 140                 (type == BufferedImage.TYPE_INT_ARGB       ) ||
 141                 (type == BufferedImage.TYPE_INT_ARGB_PRE   ) ||
 142                 (type == BufferedImage.TYPE_4BYTE_ABGR     ) ||
 143                 (type == BufferedImage.TYPE_4BYTE_ABGR_PRE ))) {
 144 
 145                 System.err.println("cannot use " + biTypeNames[type] +
 146                 " for bmp because of JDK-8147448.\t" +
 147                 " please update the test after fix of this bug!");
 148                 continue;
 149             }
 150 
 151 
 152             System.out.println("Testing " + format +
 153                                " writer for type " + biTypeNames[type]);
 154             File f = testWriteRGB(format, type);
 155             boolean ok = testReadRGB(f);
 156             if (ok) {
 157                 f.delete();
 158             }
 159             allOK = allOK && ok;
 160         }
 161 
 162 
 163 
 164         if (format.equals("png")) {
 165             System.out.println("Testing png writer for black stripe");
 166             boolean ok = testPNGByteBinary();
 167             allOK = allOK && ok;
 168         }
 169 
 170         if (!allOK) {
 171             throw new RuntimeException("Test failed");
 172         }
 173     }
 174 
 175     private File testWriteRGB(String format, int type) throws IOException {
 176 
 177         BufferedImage bi = new BufferedImage(width, height, type);
 178         Graphics2D g = bi.createGraphics();
 179 
 180         Color white = new Color(255, 255, 255);
 181         Color red = new Color(255, 0, 0);
 182         Color green = new Color(0, 255, 0);
 183         Color blue = new Color(0, 0, 255);
 184 
 185         g.setColor(white);
 186         g.fillRect(0, 0, width, height);
 187         g.setColor(red);
 188         g.fillRect(10, 10, 20, 20);
 189         g.setColor(green);
 190         g.fillRect(30, 30, 20, 20);
 191         g.setColor(blue);
 192         g.fillRect(50, 50, 20, 20);
 193 
 194         File file = new File("BitDepth_" + biTypeNames[type] + "." + format);
 195         try {
 196             ImageIO.write(bi, format, file);


< prev index next >