1 /*
2  * Copyright (c) 2006, 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.tests.iio;
42 
43 import java.awt.AlphaComposite;
44 import java.awt.Color;
45 import java.awt.Graphics2D;
46 import java.awt.Image;
47 import java.awt.Toolkit;
48 import java.awt.image.BufferedImage;
49 import javax.imageio.ImageIO;
50 
51 import j2dbench.Group;
52 import j2dbench.Option;
53 import j2dbench.Result;
54 import j2dbench.Test;
55 import j2dbench.TestEnvironment;
56 
57 public abstract class IIOTests extends Test {
58 
59     protected static final String CONTENT_BLANK  = "blank";
60     protected static final String CONTENT_RANDOM = "random";
61     protected static final String CONTENT_VECTOR = "vector";
62     protected static final String CONTENT_PHOTO  = "photo";
63 
64     static boolean hasImageIO;
65 
66     static {
67         try {
68             hasImageIO = (javax.imageio.ImageIO.class != null);
69         } catch (NoClassDefFoundError e) {
70         }
71     }
72 
73     protected static Group iioRoot;
74     protected static Group iioOptRoot;
75 
76     protected static Option sizeList;
77     protected static Option contentList;
78     protected static Option listenerTog;
79 
init()80     public static void init() {
81         if (!hasImageIO) {
82             // REMIND: We currently rely on Image I/O to generate the image
83             //         files that are used in the InputImageTests, so
84             //         unfortunately we need to punt on pre-1.4 JDKs...
85             return;
86         }
87 
88         iioRoot = new Group("imageio", "Image I/O Benchmarks");
89         iioRoot.setTabbed();
90 
91         iioOptRoot = new Group(iioRoot, "opts", "General Options");
92 
93         int[] sizes = new int[] {1, 20, 250, 1000, 4000};
94         String[] sizeStrs = new String[] {
95             "1x1", "20x20", "250x250", "1000x1000", "4000x4000"
96         };
97         String[] sizeDescs = new String[] {
98             "Tiny Images (1x1)",
99             "Small Images (20x20)",
100             "Medium Images (250x250)",
101             "Large Images (1000x1000)",
102             "Huge Images (4000x4000)",
103         };
104         sizeList = new Option.IntList(iioOptRoot,
105                                       "size", "Image Size",
106                                       sizes, sizeStrs, sizeDescs, 0x4);
107         ((Option.ObjectList) sizeList).setNumRows(5);
108 
109         String[] contentStrs = new String[] {
110             CONTENT_BLANK, CONTENT_RANDOM, CONTENT_VECTOR, CONTENT_PHOTO,
111         };
112         String[] contentDescs = new String[] {
113             "Blank (opaque black)",
114             "Random",
115             "Vector Art",
116             "Photograph",
117         };
118         contentList = new Option.ObjectList(iioOptRoot,
119                                             "content", "Image Content",
120                                             contentStrs, contentStrs,
121                                             contentStrs, contentDescs,
122                                             0x8);
123 
124         InputTests.init();
125         if (hasImageIO) {
126             OutputTests.init();
127         }
128     }
129 
IIOTests(Group parent, String nodeName, String description)130     protected IIOTests(Group parent, String nodeName, String description) {
131         super(parent, nodeName, description);
132         addDependencies(iioOptRoot, true);
133     }
134 
createBufferedImage(int width, int height, String type, boolean hasAlpha)135     protected static BufferedImage createBufferedImage(int width,
136                                                        int height,
137                                                        String type,
138                                                        boolean hasAlpha)
139     {
140         BufferedImage image;
141         image = new BufferedImage(width, height, hasAlpha ?
142                                   BufferedImage.TYPE_INT_ARGB :
143                                   BufferedImage.TYPE_INT_RGB);
144 
145         if (type.equals(CONTENT_RANDOM)) {
146             for (int y = 0; y < height; y++) {
147                 for (int x = 0; x < width; x++) {
148                     int rgb = (int)(Math.random() * 0xffffff);
149                     if (hasAlpha) {
150                         rgb |= 0x7f000000;
151                     }
152                     image.setRGB(x, y, rgb);
153                 }
154             }
155         } else if (type.equals(CONTENT_VECTOR)) {
156             Graphics2D g = image.createGraphics();
157             if (hasAlpha) {
158                 // fill background with a translucent color
159                 g.setComposite(AlphaComposite.getInstance(
160                                    AlphaComposite.SRC, 0.5f));
161             }
162             g.setColor(Color.blue);
163             g.fillRect(0, 0, width, height);
164             g.setComposite(AlphaComposite.Src);
165             g.setColor(Color.yellow);
166             g.fillOval(2, 2, width-4, height-4);
167             g.setColor(Color.red);
168             g.fillOval(4, 4, width-8, height-8);
169             g.setColor(Color.green);
170             g.fillRect(8, 8, width-16, height-16);
171             g.setColor(Color.white);
172             g.drawLine(0, 0, width, height);
173             g.drawLine(0, height, width, 0);
174             g.dispose();
175         } else if (type.equals(CONTENT_PHOTO)) {
176             Image photo = null;
177             try {
178                 photo = Toolkit.getDefaultToolkit().createImage(
179                     IIOTests.class.getResource("images/photo.jpg"));
180             } catch (Exception e) {
181                 System.err.println("error loading photo");
182                 e.printStackTrace();
183             }
184             Graphics2D g = image.createGraphics();
185             if (hasAlpha) {
186                 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC,
187                                                           0.5f));
188             }
189             g.drawImage(photo, 0, 0, width, height, null);
190             g.dispose();
191         } else { // CONTENT_BLANK
192             // leave the image empty
193         }
194 
195         return image;
196     }
197 }
198