1 /*
   2  * Copyright (c) 2002, 2011, 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 
  61     public static void init() {
  62         destroot = new Group.EnableSet(TestEnvironment.globaloptroot,
  63                                        "dest", "Output Destination Options");
  64 
  65         new Screen();
  66         new OffScreen();
  67 
  68         if (GraphicsTests.hasGraphics2D) {
  69             if (ImageTests.hasCompatImage) {
  70                 compatimgdestroot =
  71                     new Group.EnableSet(destroot, "compatimg",
  72                                         "Compatible Image Destinations");
  73                 compatimgdestroot.setHorizontal();
  74 
  75                 new CompatImg();
  76                 new CompatImg(Transparency.OPAQUE);
  77                 new CompatImg(Transparency.BITMASK);
  78                 new CompatImg(Transparency.TRANSLUCENT);
  79             }
  80 
  81             if (ImageTests.hasVolatileImage) {














  82                 new VolatileImg();
  83             }

  84 
  85             bufimgdestroot = new Group.EnableSet(destroot, "bufimg",
  86                                                  "BufferedImage Destinations");
  87 
  88             new BufImg(BufferedImage.TYPE_INT_RGB);
  89             new BufImg(BufferedImage.TYPE_INT_ARGB);
  90             new BufImg(BufferedImage.TYPE_INT_ARGB_PRE);
  91             new BufImg(BufferedImage.TYPE_3BYTE_BGR);
  92             new BufImg(BufferedImage.TYPE_BYTE_INDEXED);
  93             new BufImg(BufferedImage.TYPE_BYTE_GRAY);


  94             new CustomImg();
  95         }
  96     }
  97 
  98     public Destinations(Group parent,
  99                         String nodename, String description,
 100                         boolean defenabled)
 101     {
 102         super(parent, nodename, description, defenabled);
 103     }
 104 
 105     public void modifyTest(TestEnvironment env) {
 106         setDestination(env);
 107     }
 108 
 109     public void restoreTest(TestEnvironment env) {
 110         env.setTestImage(null);
 111     }
 112 
 113     public String getAbbreviatedModifierDescription(Object val) {
 114         return "to "+getModifierValueName(val);
 115     }
 116 
 117     public abstract void setDestination(TestEnvironment env);
 118 
 119     public static class Screen extends Destinations {
 120         public Screen() {
 121             super(destroot, "screen", "Output to Screen", false);
 122         }
 123 
 124         public String getModifierValueName(Object val) {
 125             return "Screen";
 126         }
 127 
 128         public void setDestination(TestEnvironment env) {
 129             env.setTestImage(null);
 130         }
 131     }
 132 
 133     public static class OffScreen extends Destinations {
 134         public OffScreen() {
 135             super(destroot, "offscreen", "Output to OffScreen Image", false);
 136         }
 137 
 138         public String getModifierValueName(Object val) {
 139             return "OffScreen";
 140         }
 141 
 142         public void setDestination(TestEnvironment env) {
 143             Component c = env.getCanvas();
 144             env.setTestImage(c.createImage(env.getWidth(), env.getHeight()));
 145         }
 146     }
 147 
 148     public static class CompatImg extends Destinations {
 149         int transparency;
 150 
 151         public static String ShortNames[] = {
 152             "compatimg",
 153             "opqcompatimg",
 154             "bmcompatimg",
 155             "transcompatimg",
 156         };
 157 
 158         public static String ShortDescriptions[] = {
 159             "Default",
 160             "Opaque",
 161             "Bitmask",
 162             "Translucent",
 163         };
 164 
 165         public static String LongDescriptions[] = {
 166             "Default Compatible Image",
 167             "Opaque Compatible Image",
 168             "Bitmask Compatible Image",
 169             "Translucent Compatible Image",
 170         };
 171 
 172         public static String ModifierNames[] = {
 173             "CompatImage()",
 174             "CompatImage(Opaque)",
 175             "CompatImage(Bitmask)",
 176             "CompatImage(Translucent)",
 177         };
 178 
 179         public CompatImg() {
 180             this(0);
 181         }
 182 
 183         public CompatImg(int transparency) {
 184             super(compatimgdestroot,
 185                   ShortNames[transparency],
 186                   ShortDescriptions[transparency],
 187                   false);
 188             this.transparency = transparency;
 189         }
 190 
 191         public String getModifierValueName(Object val) {
 192             return ModifierNames[transparency];
 193         }
 194 
 195         public void setDestination(TestEnvironment env) {
 196             Component c = env.getCanvas();
 197             GraphicsConfiguration gc = c.getGraphicsConfiguration();
 198             int w = env.getWidth();
 199             int h = env.getHeight();
 200             if (transparency == 0) {
 201                 env.setTestImage(gc.createCompatibleImage(w, h));
 202             } else {
 203                 env.setTestImage(gc.createCompatibleImage(w, h, transparency));
 204             }
 205         }
 206     }
 207 
 208     public static class VolatileImg extends Destinations {






























 209         public VolatileImg() {
 210             super(destroot, "volimg", "Output to Volatile Image", false);
 211         }
 212 
 213         public String getModifierValueName(Object val) {
 214             return "VolatileImg";




 215         }
 216 
 217         public void setDestination(TestEnvironment env) {




 218             Component c = env.getCanvas();
 219             env.setTestImage(c.createVolatileImage(env.getWidth(),
 220                                                    env.getHeight()));






 221         }
 222     }
 223 
 224 
 225     public static class BufImg extends Destinations {
 226         int type;
 227         Image img;
 228 
 229         public static String ShortNames[] = {
 230             "custom",
 231             "IntXrgb",
 232             "IntArgb",
 233             "IntArgbPre",
 234             "IntXbgr",
 235             "3ByteBgr",
 236             "4ByteAbgr",
 237             "4ByteAbgrPre",
 238             "Short565",
 239             "Short555",
 240             "ByteGray",
 241             "ShortGray",
 242             "ByteBinary",
 243             "ByteIndexed",
 244         };
 245 
 246         public static String Descriptions[] = {
 247             "Custom Image",
 248             "32-bit XRGB Packed Image",
 249             "32-bit ARGB Packed Image",
 250             "32-bit ARGB Alpha Premultiplied Packed Image",
 251             "32-bit XBGR Packed Image",
 252             "3-byte BGR Component Image",
 253             "4-byte ABGR Component Image",
 254             "4-byte ABGR Alpha Premultiplied Component Image",
 255             "16-bit 565 RGB Packed Image",
 256             "15-bit 555 RGB Packed Image",
 257             "8-bit Grayscale Image",
 258             "16-bit Grayscale Image",
 259             "1-bit Binary Image",
 260             "8-bit Indexed Image",
 261         };
 262 
 263         public BufImg(int type) {
 264             super(bufimgdestroot, ShortNames[type], Descriptions[type], false);
 265             this.type = type;
 266         }
 267 
 268         public String getModifierValueName(Object val) {
 269             return "BufImg("+getNodeName()+")";
 270         }
 271 
 272         public void setDestination(TestEnvironment env) {
 273             if (img == null) {
 274                 img = new BufferedImage(env.getWidth(), env.getHeight(), type);
 275             }
 276             env.setTestImage(img);
 277         }
 278     }
 279 
 280     public static class CustomImg extends Destinations {
 281         private Image img;
 282 
 283         public CustomImg() {
 284             super(bufimgdestroot,
 285                   "custom",
 286                   "Custom (3-float RGB) Image",
 287                   false);
 288         }
 289 
 290         public String getModifierValueName(Object val) {
 291             return "CustomImg";
 292         }
 293 
 294         public void setDestination(TestEnvironment env) {
 295             if (img == null) {
 296                 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
 297                 ComponentColorModel cm =
 298                     new ComponentColorModel(cs, false, false,
 299                                             Transparency.OPAQUE,
 300                                             DataBuffer.TYPE_FLOAT);
 301                 WritableRaster raster =
 302                     cm.createCompatibleWritableRaster(env.getWidth(),
 303                                                       env.getHeight());
 304                 img = new BufferedImage(cm, raster, false, null);
 305             }
 306             env.setTestImage(img);
 307         }
 308     }
 309 }
--- EOF ---