1 /*
   2  * Copyright (c) 2002, 2018, 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 
  41 package j2dbench;
  42 
  43 import java.awt.Image;
  44 import java.awt.Component;
  45 import java.awt.GraphicsConfiguration;
  46 import java.awt.Transparency;
  47 import java.awt.color.ColorSpace;
  48 import java.awt.image.BufferedImage;
  49 import java.awt.image.ComponentColorModel;
  50 import java.awt.image.DataBuffer;
  51 import java.awt.image.WritableRaster;
  52 
  53 import j2dbench.tests.GraphicsTests;
  54 import j2dbench.tests.ImageTests;
  55 
  56 public abstract class Destinations extends Option.Enable {
  57     public static Group.EnableSet destroot;
  58     public static Group bufimgdestroot;
  59     public static Group compatimgdestroot;
  60     public static Group volimgdestroot;
  61 
  62     public static void init() {
  63         destroot = new Group.EnableSet(TestEnvironment.globaloptroot,
  64                                        "dest", "Output Destination Options");
  65 
  66         new Screen();
  67         new OffScreen();
  68 
  69         if (GraphicsTests.hasGraphics2D) {
  70             if (ImageTests.hasCompatImage) {
  71                 compatimgdestroot =
  72                     new Group.EnableSet(destroot, "compatimg",
  73                                         "Compatible Image Destinations");
  74                 compatimgdestroot.setHorizontal();
  75 
  76                 new CompatImg();
  77                 new CompatImg(Transparency.OPAQUE);
  78                 new CompatImg(Transparency.BITMASK);
  79                 new CompatImg(Transparency.TRANSLUCENT);
  80             }
  81 
  82             if (ImageTests.hasVolatileImage) {
  83                 volimgdestroot = new Group.EnableSet(destroot, "volimg",
  84                         "Output to Volatile Image");
  85 
  86                 volimgdestroot.setHorizontal();
  87                 new VolatileImg();
  88                 if (ImageTests.hasTransparentVolatileImage) {
  89                     new VolatileImg(Transparency.OPAQUE);
  90                     new VolatileImg(Transparency.BITMASK);
  91                     new VolatileImg(Transparency.TRANSLUCENT);
  92                 }
  93             }
  94 
  95             bufimgdestroot = new Group.EnableSet(destroot, "bufimg",
  96                                                  "BufferedImage Destinations");
  97 
  98             new BufImg(BufferedImage.TYPE_INT_RGB);
  99             new BufImg(BufferedImage.TYPE_INT_ARGB);
 100             new BufImg(BufferedImage.TYPE_INT_ARGB_PRE);
 101             new BufImg(BufferedImage.TYPE_3BYTE_BGR);
 102             new BufImg(BufferedImage.TYPE_BYTE_INDEXED);
 103             new BufImg(BufferedImage.TYPE_BYTE_GRAY);
 104             new BufImg(BufferedImage.TYPE_4BYTE_ABGR);
 105             new BufImg(BufferedImage.TYPE_4BYTE_ABGR_PRE);
 106             new CustomImg();
 107         }
 108     }
 109 
 110     public Destinations(Group parent,
 111                         String nodename, String description,
 112                         boolean defenabled)
 113     {
 114         super(parent, nodename, description, defenabled);
 115     }
 116 
 117     public void modifyTest(TestEnvironment env) {
 118         setDestination(env);
 119     }
 120 
 121     public void restoreTest(TestEnvironment env) {
 122         env.setTestImage(null);
 123     }
 124 
 125     public String getAbbreviatedModifierDescription(Object val) {
 126         return "to "+getModifierValueName(val);
 127     }
 128 
 129     public abstract void setDestination(TestEnvironment env);
 130 
 131     public static class Screen extends Destinations {
 132         public Screen() {
 133             super(destroot, "screen", "Output to Screen", false);
 134         }
 135 
 136         public String getModifierValueName(Object val) {
 137             return "Screen";
 138         }
 139 
 140         public void setDestination(TestEnvironment env) {
 141             env.setTestImage(null);
 142         }
 143     }
 144 
 145     public static class OffScreen extends Destinations {
 146         public OffScreen() {
 147             super(destroot, "offscreen", "Output to OffScreen Image", false);
 148         }
 149 
 150         public String getModifierValueName(Object val) {
 151             return "OffScreen";
 152         }
 153 
 154         public void setDestination(TestEnvironment env) {
 155             Component c = env.getCanvas();
 156             env.setTestImage(c.createImage(env.getWidth(), env.getHeight()));
 157         }
 158     }
 159 
 160     public static class CompatImg extends Destinations {
 161         int transparency;
 162 
 163         public static String[] ShortNames = {
 164             "compatimg",
 165             "opqcompatimg",
 166             "bmcompatimg",
 167             "transcompatimg",
 168         };
 169 
 170         public static String[] ShortDescriptions = {
 171             "Default",
 172             "Opaque",
 173             "Bitmask",
 174             "Translucent",
 175         };
 176 
 177         public static String[] LongDescriptions = {
 178             "Default Compatible Image",
 179             "Opaque Compatible Image",
 180             "Bitmask Compatible Image",
 181             "Translucent Compatible Image",
 182         };
 183 
 184         public static String[] ModifierNames = {
 185             "CompatImage()",
 186             "CompatImage(Opaque)",
 187             "CompatImage(Bitmask)",
 188             "CompatImage(Translucent)",
 189         };
 190 
 191         public CompatImg() {
 192             this(0);
 193         }
 194 
 195         public CompatImg(int transparency) {
 196             super(compatimgdestroot,
 197                   ShortNames[transparency],
 198                   ShortDescriptions[transparency],
 199                   false);
 200             this.transparency = transparency;
 201         }
 202 
 203         public String getModifierValueName(Object val) {
 204             return ModifierNames[transparency];
 205         }
 206 
 207         public void setDestination(TestEnvironment env) {
 208             Component c = env.getCanvas();
 209             GraphicsConfiguration gc = c.getGraphicsConfiguration();
 210             int w = env.getWidth();
 211             int h = env.getHeight();
 212             if (transparency == 0) {
 213                 env.setTestImage(gc.createCompatibleImage(w, h));
 214             } else {
 215                 env.setTestImage(gc.createCompatibleImage(w, h, transparency));
 216             }
 217         }
 218     }
 219 
 220     public static class VolatileImg extends Destinations {
 221         private final int transparency;
 222 
 223         public static final String[] ShortNames = {
 224                 "volimg",
 225                 "opqvolimg",
 226                 "bmvolimg",
 227                 "transvolimg",
 228         };
 229 
 230         public static final String[] ShortDescriptions = {
 231                 "Default",
 232                 "Opaque",
 233                 "Bitmask",
 234                 "Translucent",
 235         };
 236 
 237         public static final String[] LongDescriptions = {
 238                 "Default VolatileImg Image",
 239                 "Opaque VolatileImg Image",
 240                 "Bitmask VolatileImg Image",
 241                 "Translucent VolatileImg Image",
 242         };
 243 
 244         public static final String[] ModifierNames = {
 245                 "VolatileImg()",
 246                 "VolatileImg(Opaque)",
 247                 "VolatileImg(Bitmask)",
 248                 "VolatileImg(Translucent)",
 249         };
 250 
 251         public VolatileImg() {
 252             this(0);
 253         }
 254 
 255         public VolatileImg(final int transparency) {
 256             super(volimgdestroot,
 257                   ShortNames[transparency],
 258                   ShortDescriptions[transparency],
 259                   false);
 260             this.transparency = transparency;
 261         }
 262 
 263         public String getModifierValueName(final Object val) {
 264             return ModifierNames[transparency];
 265         }
 266 
 267         public void setDestination(final TestEnvironment env) {
 268             Component c = env.getCanvas();
 269             GraphicsConfiguration gc = c.getGraphicsConfiguration();
 270             int w = env.getWidth();
 271             int h = env.getHeight();
 272             if (transparency == 0) {
 273                 env.setTestImage(gc.createCompatibleVolatileImage(w, h));
 274             } else {
 275                 env.setTestImage(gc.createCompatibleVolatileImage(w, h, transparency));
 276             }
 277         }
 278     }
 279 
 280 
 281     public static class BufImg extends Destinations {
 282         int type;
 283         Image img;
 284 
 285         public static String[] ShortNames = {
 286             "custom",
 287             "IntXrgb",
 288             "IntArgb",
 289             "IntArgbPre",
 290             "IntXbgr",
 291             "3ByteBgr",
 292             "4ByteAbgr",
 293             "4ByteAbgrPre",
 294             "Short565",
 295             "Short555",
 296             "ByteGray",
 297             "ShortGray",
 298             "ByteBinary",
 299             "ByteIndexed",
 300         };
 301 
 302         public static String[] Descriptions = {
 303             "Custom Image",
 304             "32-bit XRGB Packed Image",
 305             "32-bit ARGB Packed Image",
 306             "32-bit ARGB Alpha Premultiplied Packed Image",
 307             "32-bit XBGR Packed Image",
 308             "3-byte BGR Component Image",
 309             "4-byte ABGR Component Image",
 310             "4-byte ABGR Alpha Premultiplied Component Image",
 311             "16-bit 565 RGB Packed Image",
 312             "15-bit 555 RGB Packed Image",
 313             "8-bit Grayscale Image",
 314             "16-bit Grayscale Image",
 315             "1-bit Binary Image",
 316             "8-bit Indexed Image",
 317         };
 318 
 319         public BufImg(int type) {
 320             super(bufimgdestroot, ShortNames[type], Descriptions[type], false);
 321             this.type = type;
 322         }
 323 
 324         public String getModifierValueName(Object val) {
 325             return "BufImg("+getNodeName()+")";
 326         }
 327 
 328         public void setDestination(TestEnvironment env) {
 329             if (img == null) {
 330                 img = new BufferedImage(env.getWidth(), env.getHeight(), type);
 331             }
 332             env.setTestImage(img);
 333         }
 334     }
 335 
 336     public static class CustomImg extends Destinations {
 337         private Image img;
 338 
 339         public CustomImg() {
 340             super(bufimgdestroot,
 341                   "custom",
 342                   "Custom (3-float RGB) Image",
 343                   false);
 344         }
 345 
 346         public String getModifierValueName(Object val) {
 347             return "CustomImg";
 348         }
 349 
 350         public void setDestination(TestEnvironment env) {
 351             if (img == null) {
 352                 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
 353                 ComponentColorModel cm =
 354                     new ComponentColorModel(cs, false, false,
 355                                             Transparency.OPAQUE,
 356                                             DataBuffer.TYPE_FLOAT);
 357                 WritableRaster raster =
 358                     cm.createCompatibleWritableRaster(env.getWidth(),
 359                                                       env.getHeight());
 360                 img = new BufferedImage(cm, raster, false, null);
 361             }
 362             env.setTestImage(img);
 363         }
 364     }
 365 }