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