1 /*
   2  * Copyright (c) 2013, 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 package j2dbench.tests.cmm;
  41 
  42 import java.awt.image.BufferedImage;
  43 import java.io.IOException;
  44 import java.net.URL;
  45 
  46 import javax.imageio.ImageIO;
  47 import javax.imageio.ImageReader;
  48 import javax.imageio.stream.ImageInputStream;
  49 
  50 import j2dbench.Group;
  51 import j2dbench.Option;
  52 import j2dbench.Result;
  53 import j2dbench.TestEnvironment;
  54 
  55 /* This benchmark verifies how changes in cmm library affects image decoding */
  56 public class EmbeddedProfileTests extends ColorConversionTests {
  57 
  58     protected static Group grpRoot;
  59     protected static Group grpOptionsRoot;
  60 
  61     protected static Option inputImages;
  62 
  63     public static void init() {
  64         grpRoot = new Group(colorConvRoot, "embed", "Embedded Profile Tests");
  65 
  66         grpOptionsRoot = new Group(grpRoot, "embedOptions", "Options");
  67 
  68         inputImages = createImageList();
  69 
  70         new ReadImageTest();
  71     }
  72 
  73     private static class IccImageResource {
  74         static IccImageResource SMALL = new IccImageResource("images/img_icc_small.jpg", "512x512", "Small: 512x512");
  75         static IccImageResource MEDIUM = new IccImageResource("images/img_icc_medium.jpg", "2048x2048", "Medium: 2048x2048");
  76         static IccImageResource LARGE = new IccImageResource("images/img_icc_large.jpg", "4096x4096", "Large: 4096x4096");
  77 
  78         private IccImageResource(String file, String name, String description) {
  79             this.url = CMMTests.class.getResource(file);
  80             this.abbrev = name;
  81             this.description = description;
  82         }
  83 
  84         public final URL url;
  85         public final String abbrev;
  86         public final String description;
  87 
  88         public static IccImageResource[] values() {
  89             return new IccImageResource[]{SMALL, MEDIUM, LARGE};
  90         }
  91     }
  92 
  93     private static Option createImageList() {
  94         IccImageResource[] images = IccImageResource.values();
  95 
  96         int num = images.length;
  97 
  98         String[] names = new String[num];
  99         String[] abbrev = new String[num];
 100         String[] descr = new String[num];
 101 
 102         for (int i = 0; i < num; i++) {
 103             names[i] = images[i].abbrev;
 104             abbrev[i] = images[i].abbrev;
 105             descr[i] = images[i].description;
 106         }
 107 
 108          Option list = new Option.ObjectList(grpOptionsRoot,
 109                 "Images", "Input Images",
 110                 names, images, abbrev, descr, 1);
 111 
 112          return list;
 113     }
 114 
 115     public EmbeddedProfileTests(Group parent, String nodeName, String description) {
 116         super(parent, nodeName, description);
 117         addDependencies(grpOptionsRoot, true);
 118     }
 119 
 120     private static class Context {
 121         URL input;
 122 
 123         public Context(TestEnvironment env, Result res) {
 124 
 125             IccImageResource icc_input = (IccImageResource)
 126                     env.getModifier(inputImages);
 127 
 128             input = icc_input.url;
 129         }
 130     }
 131 
 132      public Object initTest(TestEnvironment env, Result res) {
 133         return new Context(env, res);
 134     }
 135 
 136     public void cleanupTest(TestEnvironment env, Object o) {
 137         Context ctx = (Context)o;
 138         ctx.input = null;
 139     }
 140 
 141     private static class ReadImageTest extends EmbeddedProfileTests {
 142         public ReadImageTest() {
 143             super(grpRoot, "embd_img_read", "ImageReader.read()");
 144         }
 145 
 146         public void runTest(Object octx, int numReps) {
 147             final Context ctx = (Context)octx;
 148             final URL url = ctx.input;
 149             ImageInputStream iis = null;
 150             ImageReader reader = null;
 151 
 152             try {
 153                 iis = ImageIO.createImageInputStream(url.openStream());
 154                 reader = (ImageReader) ImageIO.getImageReaders(iis).next();
 155             } catch (IOException e) {
 156                 throw new RuntimeException("Unable to run the benchmark", e);
 157             }
 158 
 159             do {
 160                 try {
 161                     reader.setInput(iis);
 162                     BufferedImage img = reader.read(0);
 163                     reader.reset();
 164 
 165                     iis = ImageIO.createImageInputStream(url.openStream());
 166                 } catch (Exception e) {
 167                     e.printStackTrace();
 168                 }
 169             } while (--numReps >= 0);
 170         }
 171     }
 172 }