1 /* 2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /* 24 * @test 25 * @bug 5009033 6603000 6666362 26 * @summary Verifies that images transformed with bilinear filtering do not 27 * leave artifacts at the edges. 28 * @run main/othervm DrawImageBilinear 29 * @run main/othervm -Dsun.java2d.opengl=True DrawImageBilinear 30 * @author campbelc 31 */ 32 33 import java.awt.Canvas; 34 import java.awt.Color; 35 import java.awt.Component; 36 import java.awt.Dimension; 37 import java.awt.Frame; 38 import java.awt.Graphics; 39 import java.awt.Graphics2D; 40 import java.awt.GraphicsConfiguration; 41 import java.awt.Point; 42 import java.awt.Rectangle; 43 import java.awt.RenderingHints; 44 import java.awt.Robot; 45 import java.awt.Toolkit; 46 import java.awt.image.BufferedImage; 47 import java.awt.image.IndexColorModel; 48 import java.awt.image.VolatileImage; 49 50 public class DrawImageBilinear extends Canvas { 51 52 private static final int SIZE = 5; 53 54 private static boolean done; 55 private BufferedImage bimg1, bimg2; 56 private VolatileImage vimg; 57 private static volatile BufferedImage capture; 58 private static void doCapture(Component test) { 59 try { 60 Thread.sleep(2000); 61 } catch (InterruptedException ex) {} 62 // Grab the screen region 63 try { 64 Robot robot = new Robot(); 65 Point pt1 = test.getLocationOnScreen(); 66 Rectangle rect = 67 new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight()); 68 capture = robot.createScreenCapture(rect); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 } 73 74 private void renderPattern(Graphics g) { 75 g.setColor(Color.red); 76 g.fillRect(0, 0, SIZE, SIZE); 77 //g.setColor(Color.green); 78 //g.drawRect(0, 0, SIZE-1, SIZE-1); 79 g.dispose(); 80 } 81 82 public void paint(Graphics g) { 83 Graphics2D g2d = (Graphics2D)g; 84 85 if (bimg1 == null) { 86 bimg1 = (BufferedImage)createImage(SIZE, SIZE); 87 bimg1.setAccelerationPriority(0.0f); 88 renderPattern(bimg1.createGraphics()); 89 90 bimg2 = (BufferedImage)createImage(SIZE, SIZE); 91 renderPattern(bimg2.createGraphics()); 92 93 vimg = createVolatileImage(SIZE, SIZE); 94 vimg.validate(getGraphicsConfiguration()); 95 renderPattern(vimg.createGraphics()); 96 } 97 98 do { 99 g2d.setColor(Color.white); 100 g2d.fillRect(0, 0, getWidth(), getHeight()); 101 102 g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 103 RenderingHints.VALUE_INTERPOLATION_BILINEAR); 104 105 // first time will be a sw->surface blit 106 g2d.drawImage(bimg1, 10, 10, 40, 40, null); 107 108 // second time will be a texture->surface blit 109 g2d.drawImage(bimg2, 80, 10, 40, 40, null); 110 if (!skipOglTextureTest) { 111 g2d.drawImage(bimg2, 80, 10, 40, 40, null); 112 } 113 114 // third time will be a pbuffer->surface blit 115 if (vimg.validate(getGraphicsConfiguration()) != VolatileImage.IMAGE_OK) { 116 renderPattern(vimg.createGraphics()); 117 } 118 g2d.drawImage(vimg, 150, 10, 40, 40, null); 119 120 Toolkit.getDefaultToolkit().sync(); 121 } while (vimg.contentsLost()); 122 123 synchronized (this) { 124 if (!done) { 125 doCapture(this); 126 done = true; 127 } 128 notifyAll(); 129 } 130 } 131 132 public Dimension getPreferredSize() { 133 return new Dimension(200, 100); 134 } 135 136 private static void testRegion(BufferedImage bi, 137 Rectangle affectedRegion) 138 { 139 int x1 = affectedRegion.x; 140 int y1 = affectedRegion.y; 141 int x2 = x1 + affectedRegion.width; 142 int y2 = y1 + affectedRegion.height; 143 144 for (int y = y1; y < y2; y++) { 145 for (int x = x1; x < x2; x++) { 146 int actual = bi.getRGB(x, y); 147 if ((actual != 0xfffe0000) && (actual != 0xffff0000)) { 148 throw new RuntimeException("Test failed at x="+x+" y="+y+ 149 " (expected=0xffff0000"+ 150 " actual=0x"+ 151 Integer.toHexString(actual) + 152 ")"); 153 } 154 } 155 } 156 } 157 158 private static boolean skipOglTextureTest = false; 159 160 public static void main(String[] args) { 161 boolean show = false; 162 for (String arg : args) { 163 if ("-show".equals(arg)) { 164 show = true; 165 } 166 } 167 168 String arch = System.getProperty("os.arch"); 169 boolean isOglEnabled = Boolean.getBoolean("sun.java2d.opengl"); 170 skipOglTextureTest = isOglEnabled && ("sparc".equals(arch)); 171 System.out.println("Skip OpenGL texture test: " + skipOglTextureTest); 172 173 DrawImageBilinear test = new DrawImageBilinear(); 174 Frame frame = new Frame(); 175 frame.add(test); 176 frame.pack(); 177 frame.setVisible(true); 178 179 // Wait until the component's been painted 180 synchronized (test) { 181 while (!done) { 182 try { 183 test.wait(); 184 } catch (InterruptedException e) { 185 throw new RuntimeException("Failed: Interrupted"); 186 } 187 } 188 } 189 190 GraphicsConfiguration gc = frame.getGraphicsConfiguration(); 191 if (gc.getColorModel() instanceof IndexColorModel) { 192 System.out.println("IndexColorModel detected: " + 193 "test considered PASSED"); 194 frame.dispose(); 195 return; 196 } 197 198 if (!show) { 199 frame.dispose(); 200 } 201 if (capture == null) { 202 throw new RuntimeException("Failed: capture is null"); 203 } 204 205 // Test background color 206 int pixel = capture.getRGB(5, 5); 207 if (pixel != 0xffffffff) { 208 throw new RuntimeException("Failed: Incorrect color for " + 209 "background"); 210 } 211 212 // Test pixels 213 testRegion(capture, new Rectangle(10, 10, 40, 40)); 214 testRegion(capture, new Rectangle(80, 10, 40, 40)); 215 testRegion(capture, new Rectangle(150, 10, 40, 40)); 216 } 217 }