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