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); 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); 197 } catch (RuntimeException re) { 198 System.out.println("Can't write a type " 199 + biTypeNames[type] + 200 " BufferedImage!"); 201 } 202 203 return file; 204 } 205 206 private int colorDistance(int color, int r, int g, int b) { 207 int r0 = ((color >> 16) & 0xff) - r; 208 int g0 = ((color >> 8) & 0xff) - g; 209 int b0 = (color & 0xff) - b; 210 return r0*r0 + g0*g0 + b0*b0; 211 } 212 213 private boolean testReadRGB(File file) throws IOException { 214 int[] rgb = new int[3]; 215 216 BufferedImage bi = ImageIO.read(file); 217 if (bi == null) { 218 System.out.println("Couldn't read image!"); 219 return false; 220 } | 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 java.util.Iterator; 39 import javax.imageio.ImageIO; 40 import javax.imageio.ImageTypeSpecifier; 41 import javax.imageio.ImageWriter; 42 import javax.imageio.stream.ImageOutputStream; 43 44 public class BitDepth { 45 46 public static void main(String[] args) throws IOException { 47 new BitDepth(args); 48 } 49 50 // Check that the PNG writer can write an all-white image correctly 51 private static boolean testPNGByteBinary() throws IOException { 52 int width = 10; 53 int height = 10; 54 55 File f = new File("BlackStripe.png"); 56 BufferedImage bi = new BufferedImage(width, height, 57 BufferedImage.TYPE_BYTE_BINARY); 58 Graphics2D g = bi.createGraphics(); 59 g.setColor(new Color(255, 255, 255)); 60 g.fillRect(0, 0, width, height); 61 62 ImageIO.write(bi, "png", f); 101 102 private static final String[] biTypeNames = { 103 "CUSTOM", 104 "INT_RGB", 105 "INT_ARGB", 106 "INT_ARGB_PRE", 107 "INT_BGR", 108 "3BYTE_BGR", 109 "4BYTE_ABGR", 110 "4BYTE_ABGR_PRE", 111 "USHORT_565_RGB", 112 "USHORT_555_RGB", 113 "BYTE_GRAY", 114 "USHORT_GRAY", 115 "BYTE_BINARY", 116 "BYTE_INDEXED" 117 }; 118 119 private int width = 80; 120 private int height = 80; 121 private String[] formats = { "png", "jpeg", "tif", "bmp", "gif" }; 122 123 public BitDepth(String[] args) throws IOException { 124 if (args.length > 0) { 125 formats = args; 126 } 127 128 for (String format : formats) { 129 testFormat(format); 130 } 131 } 132 133 private void testFormat(String format) throws IOException { 134 135 boolean allOK = true; 136 137 for (int type : biRGBTypes) { 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 System.out.println("Testing " + format + 152 " writer for type " + biTypeNames[type]); 153 File f = testWriteRGB(format, type); 154 if (f == null) 155 continue; 156 157 boolean ok = testReadRGB(f); 158 if (ok) { 159 f.delete(); 160 } 161 allOK = allOK && ok; 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 ImageTypeSpecifier spec = new ImageTypeSpecifier(bi); 195 Iterator<ImageWriter> writers = ImageIO.getImageWriters(spec, format); 196 File file = new File("BitDepth_" + biTypeNames[type] + "." + format); 197 if (!writers.hasNext()) { 198 System.out.println("\tNo writers available for type " + biTypeNames[type] 199 + " BufferedImage!"); 200 } else { 201 ImageWriter writer = writers.next(); 202 try (ImageOutputStream out = ImageIO.createImageOutputStream(file)) { 203 writer.setOutput(out); 204 writer.write(bi); 205 } catch (Exception e) { 206 System.out.println("\tCan't write a type " + biTypeNames[type] 207 + " BufferedImage!"); 208 return null; 209 } 210 } 211 212 return file; 213 } 214 215 private int colorDistance(int color, int r, int g, int b) { 216 int r0 = ((color >> 16) & 0xff) - r; 217 int g0 = ((color >> 8) & 0xff) - g; 218 int b0 = (color & 0xff) - b; 219 return r0*r0 + g0*g0 + b0*b0; 220 } 221 222 private boolean testReadRGB(File file) throws IOException { 223 int[] rgb = new int[3]; 224 225 BufferedImage bi = ImageIO.read(file); 226 if (bi == null) { 227 System.out.println("Couldn't read image!"); 228 return false; 229 } |