1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 package j2dbench.tests.cmm; 41 42 import j2dbench.Group; 43 import j2dbench.Option; 44 import j2dbench.Result; 45 import j2dbench.TestEnvironment; 46 import j2dbench.tests.iio.IIOTests; 47 import java.awt.AlphaComposite; 48 import java.awt.Color; 49 import java.awt.Graphics2D; 50 import java.awt.Image; 51 import java.awt.color.ColorSpace; 52 import java.awt.image.BufferedImage; 53 import java.awt.image.ColorConvertOp; 54 import java.awt.image.Raster; 55 import java.awt.image.WritableRaster; 56 import javax.imageio.ImageIO; 57 58 public class ColorConvertOpTests extends ColorConversionTests { 59 60 private static enum ImageContent { 61 BLANK("bank", "Blank (opaque black)"), 62 RANDOM("random", "Random"), 63 VECTOR("vector", "Vector Art"), 64 PHOTO("photo", "Photograph"); 65 66 public final String name; 67 public final String descr; 68 69 private ImageContent(String name, String descr) { 70 this.name = name; 71 this.descr = descr; 72 } 73 } 74 75 private static enum ImageType { 76 INT_ARGB(BufferedImage.TYPE_INT_ARGB, "INT_ARGB", "TYPE_INT_ARGB"), 77 INT_RGB(BufferedImage.TYPE_INT_RGB, "INT_RGB", "TYPE_INT_RGB"), 78 INT_BGR(BufferedImage.TYPE_INT_BGR, "INT_BGR", "TYPE_INT_BGR"), 79 BYTE_3BYTE_BGR(BufferedImage.TYPE_3BYTE_BGR, "3BYTE_BGR", "TYPE_3BYTE_BGR"), 80 BYTE_4BYTE_ABGR(BufferedImage.TYPE_4BYTE_ABGR, "4BYTE_BGR", "TYPE_4BYTE_BGR"), 81 COMPATIBLE_DST(0, "Compatible", "Compatible destination"); 82 83 private ImageType(int type, String abbr, String descr) { 84 this.type = type; 85 this.abbrev = abbr; 86 this.descr = descr; 87 } 88 89 public final int type; 90 public final String abbrev; 91 public final String descr; 92 } 93 94 private static enum ListType { 95 SRC("srcType", "Source Images"), 96 DST("dstType", "Destination Images"); 97 98 private ListType(String name, String description) { 99 this.name = name; 100 this.description = description; 101 } 102 public final String name; 103 public final String description; 104 } 105 106 public static Option createImageTypeList(ListType listType) { 107 108 109 ImageType[] allTypes = ImageType.values(); 110 111 int num = allTypes.length; 112 if (listType == ListType.SRC) { 113 num -= 1; // exclude compatible destination 114 } 115 116 ImageType[] t = new ImageType[num]; 117 String[] names = new String[num]; 118 String[] abbrev = new String[num]; 119 String[] descr = new String[num]; 120 121 for (int i = 0; i < num; i++) { 122 t[i] = allTypes[i]; 123 names[i] = t[i].toString(); 124 abbrev[i] = t[i].abbrev; 125 descr[i] = t[i].descr; 126 } 127 128 Option list = new Option.ObjectList(opOptionsRoot, 129 listType.name, listType.description, 130 names, t, abbrev, descr, 1); 131 return list; 132 } 133 134 protected static Group opConvRoot; 135 136 protected static Group opOptionsRoot; 137 protected static Option sizeList; 138 protected static Option contentList; 139 140 protected static Option sourceType; 141 142 protected static Option destinationType; 143 144 public static void init() { 145 opConvRoot = new Group(colorConvRoot, "ccop", "ColorConvertOp Tests"); 146 147 opOptionsRoot = new Group(opConvRoot, "ccopOptions", "Options"); 148 149 // size list 150 int[] sizes = new int[] {1, 20, 250, 1000, 4000}; 151 String[] sizeStrs = new String[] { 152 "1x1", "20x20", "250x250", "1000x1000", "4000x4000" 153 }; 154 String[] sizeDescs = new String[] { 155 "Tiny Images (1x1)", 156 "Small Images (20x20)", 157 "Medium Images (250x250)", 158 "Large Images (1000x1000)", 159 "Huge Images (4000x4000)", 160 }; 161 sizeList = new Option.IntList(opOptionsRoot, 162 "size", "Image Size", 163 sizes, sizeStrs, sizeDescs, 0x4); 164 ((Option.ObjectList) sizeList).setNumRows(5); 165 166 // image content 167 ImageContent[] c = ImageContent.values(); 168 169 String[] contentStrs = new String[c.length]; 170 String[] contentDescs = new String[c.length]; 171 172 for (int i = 0; i < c.length; i++) { 173 contentStrs[i] = c[i].name; 174 contentDescs[i] = c[i].descr; 175 }; 176 177 contentList = new Option.ObjectList(opOptionsRoot, 178 "content", "Image Content", 179 contentStrs, c, 180 contentStrs, contentDescs, 181 0x8); 182 183 sourceType = createImageTypeList(ListType.SRC); 184 185 destinationType = createImageTypeList(ListType.DST); 186 187 new ConvertImageTest(); 188 new ConvertRasterTest(); 189 new DrawImageTest(); 190 } 191 192 public ColorConvertOpTests(Group parent, String nodeName, String description) { 193 super(parent, nodeName, description); 194 addDependencies(opOptionsRoot, true); 195 } 196 197 public Object initTest(TestEnvironment env, Result res) { 198 return new Context(env, res); 199 } 200 201 public void cleanupTest(TestEnvironment env, Object o) { 202 Context ctx = (Context)o; 203 ctx.cs = null; 204 ctx.op_img = null; 205 ctx.op_rst = null; 206 ctx.dst = null; 207 ctx.src = null; 208 ctx.graphics = null; 209 } 210 211 private static class Context { 212 ColorSpace cs; 213 Graphics2D graphics; 214 ColorConvertOp op_img; 215 ColorConvertOp op_rst; 216 217 BufferedImage src; 218 BufferedImage dst; 219 220 WritableRaster rsrc; 221 WritableRaster rdst; 222 223 public Context(TestEnvironment env, Result res) { 224 225 graphics = (Graphics2D)env.getGraphics(); 226 cs = getColorSpace(env); 227 228 // TODO: provide rendering hints 229 op_img = new ColorConvertOp(cs, null); 230 ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB); 231 op_rst = new ColorConvertOp(sRGB, cs, null); 232 233 int size = env.getIntValue(sizeList); 234 235 ImageContent content = (ImageContent)env.getModifier(contentList); 236 ImageType srcType = (ImageType)env.getModifier(sourceType); 237 238 src = createBufferedImage(size, size, content, srcType.type); 239 rsrc = src.getRaster(); 240 241 ImageType dstType = (ImageType)env.getModifier(destinationType); 242 if (dstType == ImageType.COMPATIBLE_DST) { 243 dst = op_img.createCompatibleDestImage(src, null); 244 } else { 245 dst = createBufferedImage(size, size, content, dstType.type); 246 } 247 // raster always has to be comatible 248 rdst = op_rst.createCompatibleDestRaster(rsrc); 249 } 250 } 251 252 private static class ConvertImageTest extends ColorConvertOpTests { 253 public ConvertImageTest() { 254 super(opConvRoot, "op_img", "op.filetr(BufferedImage)"); 255 } 256 257 public void runTest(Object octx, int numReps) { 258 final Context ctx = (Context)octx; 259 final ColorConvertOp op = ctx.op_img; 260 261 final BufferedImage src = ctx.src; 262 BufferedImage dst = ctx.dst; 263 do { 264 try { 265 dst = op.filter(src, dst); 266 } catch (Exception e) { 267 e.printStackTrace(); 268 } 269 } while (--numReps >= 0); 270 } 271 } 272 273 private static class ConvertRasterTest extends ColorConvertOpTests { 274 public ConvertRasterTest() { 275 super(opConvRoot, "op_rst", "op.filetr(Raster)"); 276 } 277 278 public void runTest(Object octx, int numReps) { 279 final Context ctx = (Context)octx; 280 final ColorConvertOp op = ctx.op_rst; 281 282 final Raster src = ctx.rsrc; 283 WritableRaster dst = ctx.rdst; 284 do { 285 try { 286 dst = op.filter(src, dst); 287 } catch (Exception e) { 288 e.printStackTrace(); 289 } 290 } while (--numReps >= 0); 291 } 292 } 293 294 private static class DrawImageTest extends ColorConvertOpTests { 295 public DrawImageTest() { 296 super(opConvRoot, "op_draw", "drawImage(ColorConvertOp)"); 297 } 298 299 public void runTest(Object octx, int numReps) { 300 final Context ctx = (Context)octx; 301 final ColorConvertOp op = ctx.op_img; 302 303 final Graphics2D g = ctx.graphics; 304 305 final BufferedImage src = ctx.src; 306 307 do { 308 g.drawImage(src, op, 0, 0); 309 } while (--numReps >= 0); 310 311 } 312 } 313 314 315 /************************************************************************** 316 ****** Helper routines 317 *************************************************************************/ 318 protected static BufferedImage createBufferedImage(int width, 319 int height, 320 ImageContent contentType, 321 int type) 322 { 323 BufferedImage image; 324 image = new BufferedImage(width, height, type); 325 boolean hasAlpha = image.getColorModel().hasAlpha(); 326 switch (contentType) { 327 case RANDOM: 328 for (int y = 0; y < height; y++) { 329 for (int x = 0; x < width; x++) { 330 int rgb = (int)(Math.random() * 0xffffff); 331 if (hasAlpha) { 332 rgb |= 0x7f000000; 333 } 334 image.setRGB(x, y, rgb); 335 } 336 } 337 break; 338 case VECTOR: 339 { 340 Graphics2D g = image.createGraphics(); 341 if (hasAlpha) { 342 // fill background with a translucent color 343 g.setComposite(AlphaComposite.getInstance( 344 AlphaComposite.SRC, 0.5f)); 345 } 346 g.setColor(Color.blue); 347 g.fillRect(0, 0, width, height); 348 g.setComposite(AlphaComposite.Src); 349 g.setColor(Color.yellow); 350 g.fillOval(2, 2, width-4, height-4); 351 g.setColor(Color.red); 352 g.fillOval(4, 4, width-8, height-8); 353 g.setColor(Color.green); 354 g.fillRect(8, 8, width-16, height-16); 355 g.setColor(Color.white); 356 g.drawLine(0, 0, width, height); 357 g.drawLine(0, height, width, 0); 358 g.dispose(); 359 break; 360 } 361 case PHOTO: 362 { 363 Image photo = null; 364 try { 365 photo = ImageIO.read( 366 IIOTests.class.getResourceAsStream("images/photo.jpg")); 367 } catch (Exception e) { 368 System.err.println("error loading photo"); 369 e.printStackTrace(); 370 } 371 Graphics2D g = image.createGraphics(); 372 if (hasAlpha) { 373 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 374 0.5f)); 375 } 376 g.drawImage(photo, 0, 0, width, height, null); 377 g.dispose(); 378 break; 379 } 380 default: 381 break; 382 } 383 384 return image; 385 } 386 }