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.Result;
  44 import j2dbench.TestEnvironment;
  45 import java.awt.color.ColorSpace;
  46 
  47 public class DataConversionTests extends ColorConversionTests {
  48 
  49     protected static Group dataConvRoot;
  50 
  51     public static void init() {
  52         dataConvRoot = new Group(colorConvRoot, "data", "Data Conversoion Tests");
  53 
  54         new FromRGBTest();
  55         new ToRGBTest();
  56         new FromCIEXYZTest();
  57         new ToCIEXYZTest();
  58     }
  59 
  60     public DataConversionTests(Group parent, String nodeName, String description) {
  61         super(parent, nodeName, description);
  62     }
  63 
  64     protected static class Context {
  65 
  66         ColorSpace cs;
  67         int numComponents;
  68         float[] val;
  69         float[] rgb;
  70         float[] cie;
  71         TestEnvironment env;
  72         Result res;
  73 
  74         public Context(TestEnvironment env, Result result, ColorSpace cs) {
  75             this.cs = cs;
  76             this.env = env;
  77             this.res = result;
  78 
  79             numComponents = cs.getNumComponents();
  80 
  81             val = new float[numComponents];
  82 
  83             for (int i = 0; i < numComponents; i++) {
  84                 float min = cs.getMinValue(i);
  85                 float max = cs.getMaxValue(i);
  86 
  87                 val[i] = 0.5f * (max - min);
  88             }
  89 
  90             rgb = new float[]{0.5f, 0.5f, 0.5f};
  91             cie = new float[]{0.5f, 0.5f, 0.5f};
  92         }
  93     }
  94 
  95     @Override
  96     public Object initTest(TestEnvironment env, Result result) {
  97         ColorSpace cs = getColorSpace(env);
  98         return new Context(env, result, cs);
  99     }
 100 
 101     @Override
 102     public void cleanupTest(TestEnvironment te, Object o) {
 103     }
 104 
 105     private static class FromRGBTest extends DataConversionTests {
 106 
 107         public FromRGBTest() {
 108             super(dataConvRoot,
 109                     "fromRGB",
 110                     "ColorSpace.fromRGB()");
 111         }
 112 
 113         public void runTest(Object ctx, int numReps) {
 114             final Context ictx = (Context) ctx;
 115             final ColorSpace cs = ictx.cs;
 116 
 117             final float[] rgb = ictx.rgb;
 118             do {
 119                 try {
 120                     cs.fromRGB(rgb);
 121                 } catch (Exception e) {
 122                     e.printStackTrace();
 123                 }
 124             } while (--numReps >= 0);
 125         }
 126     }
 127 
 128     private static class FromCIEXYZTest extends DataConversionTests {
 129 
 130         public FromCIEXYZTest() {
 131             super(dataConvRoot,
 132                     "fromCIEXYZ",
 133                     "ColorSpace.fromCIEXYZ()");
 134         }
 135 
 136         public void runTest(Object ctx, int numReps) {
 137             final Context ictx = (Context) ctx;
 138             final ColorSpace cs = ictx.cs;
 139 
 140             final float[] val = ictx.cie;
 141             do {
 142                 try {
 143                     cs.fromCIEXYZ(val);
 144                 } catch (Exception e) {
 145                     e.printStackTrace();
 146                 }
 147             } while (--numReps >= 0);
 148         }
 149     }
 150 
 151     private static class ToCIEXYZTest extends DataConversionTests {
 152 
 153         public ToCIEXYZTest() {
 154             super(dataConvRoot,
 155                     "toCIEXYZ",
 156                     "ColorSpace.toCIEXYZ()");
 157         }
 158 
 159         public void runTest(Object ctx, int numReps) {
 160             final Context ictx = (Context) ctx;
 161             final ColorSpace cs = ictx.cs;
 162 
 163             final float[] val = ictx.val;
 164 
 165             do {
 166                 try {
 167                     cs.toCIEXYZ(val);
 168                 } catch (Exception e) {
 169                     e.printStackTrace();
 170                 }
 171             } while (--numReps >= 0);
 172         }
 173     }
 174 
 175     private static class ToRGBTest extends DataConversionTests {
 176 
 177         public ToRGBTest() {
 178             super(dataConvRoot,
 179                     "toRGB",
 180                     "ColorSpace.toRGB()");
 181         }
 182 
 183         public void runTest(Object ctx, int numReps) {
 184             final Context ictx = (Context) ctx;
 185             final ColorSpace cs = ictx.cs;
 186 
 187             final float[] val = ictx.val;
 188 
 189             do {
 190                 try {
 191                     cs.toRGB(val);
 192                 } catch (Exception e) {
 193                     e.printStackTrace();
 194                 }
 195             } while (--numReps >= 0);
 196         }
 197     }
 198 }