1 /*
   2  * Copyright (c) 2011, 2013, 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 /*
  25  * @test
  26  * @summary Tests basic interpolation behavior
  27  * @summary com.apple.junit.java.graphics.Interpolation
  28  * @run junit InterpolationTest
  29  */
  30 import junit.framework.*;
  31 import java.awt.*;
  32 import java.awt.image.BufferedImage;
  33 
  34 public class InterpolationTest extends TestCase {
  35 
  36     static final int srcWidth = 5;
  37     static final int srcHeight = 3;
  38     static final Rectangle srcRect = new Rectangle(0, 0, srcWidth, srcHeight);
  39     static final int dstWidth = srcWidth;
  40     static final int dstHeight = srcHeight * 3;
  41     static final Rectangle dstRect = new Rectangle(0, 0, dstWidth, dstHeight);
  42     static final int kThreshold = 3;
  43     protected GraphicsEnvironment ge;
  44     protected GraphicsDevice gd;
  45     protected GraphicsConfiguration gc;
  46     protected BufferedImage src;
  47     protected BufferedImage dst;
  48 
  49     protected void setUp() {
  50 // Get the default (or compatible) buffered image
  51         ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  52         gd = ge.getDefaultScreenDevice();
  53         gc = gd.getDefaultConfiguration();
  54 
  55 // create source and destination images
  56         src = gc.createCompatibleImage(srcWidth, srcHeight);
  57         dst = gc.createCompatibleImage(dstWidth, dstHeight);
  58 
  59     }
  60 
  61     public void testExpandNN() {
  62         assertNotNull(src);
  63         assertNotNull(dst);
  64 
  65 // Draw some bits into the source image
  66         Graphics2D sg = (Graphics2D) src.createGraphics();
  67         sg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
  68         sg.setColor(Color.black);
  69         sg.fillRect(0, 0, srcWidth, srcHeight);
  70 
  71 // draw a single horizonal blue line
  72         sg.setColor(Color.blue);
  73         sg.drawLine(0, 1, srcWidth - 1, 1);
  74 
  75 
  76 // Now stretch it using VALUE_INTERPOLATION_NEAREST_NEIGHBOR
  77         Graphics2D dg = (Graphics2D) dst.createGraphics();
  78         dg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
  79         dg.drawImage(src, 0, 0, dstWidth, dstHeight, null);
  80         dg.dispose();
  81 
  82 // spot check the values
  83         int blues[] = new int[9];
  84         for (int i = 0; i < 9; i++) {
  85             blues[i] = dst.getRGB(3, i) & 0x000000FF;
  86         }
  87 
  88         assertEquals("top part, pixel 0 should be clear", 0x00, blues[0]);
  89         assertEquals("top part, pixel 1 should be clear", 0x00, blues[1]);
  90         assertEquals("top part, pixel 2 should be clear", 0x00, blues[2]);
  91 
  92         assertEquals("middle part, pixel 3 should be blue", 0xFF, blues[3]);
  93         assertEquals("middle part, pixel 4 should be blue", 0xFF, blues[4]);
  94         assertEquals("middle part, pixel 5 should be blue", 0xFF, blues[5]);
  95 
  96         assertEquals("bottom part, pixel 6 should be clear", 0x00, blues[6]);
  97         assertEquals("bottom part, pixel 7 should be clear", 0x00, blues[7]);
  98         assertEquals("bottom part, pixel 8 should be clear", 0x00, blues[8]);
  99 
 100         sg.dispose();
 101     }
 102 
 103     public void testExpandBIL() {
 104         assertNotNull(src);
 105         assertNotNull(dst);
 106 
 107 // Draw some bits into the source image
 108         Graphics2D sg = (Graphics2D) src.createGraphics();
 109         sg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
 110         sg.setColor(Color.black);
 111         sg.fillRect(0, 0, srcWidth, srcHeight);
 112 
 113         sg.setColor(Color.blue);
 114         sg.drawLine(0, 1, srcWidth - 1, 1);
 115 
 116 
 117 // Now stretch it using VALUE_INTERPOLATION_BILINEAR
 118         Graphics2D dg = (Graphics2D) dst.createGraphics();
 119         dg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 120         dg.drawImage(src, 0, 0, dstWidth, dstHeight, null);
 121         dg.dispose();
 122 
 123 // spot check the values
 124         int blues[] = new int[9];
 125         for (int i = 0; i < 9; i++) {
 126             blues[i] = dst.getRGB(3, i) & 0x000000FF;
 127         }
 128 
 129         assertTrue("top part, pixel 0 should be almost completely clear", blues[0] < kThreshold);
 130         assertTrue("top part, pixel 1 should be almost completely clear", blues[1] < kThreshold);
 131 
 132         assertTrue("top part, pixel 1 should fade up to blue", blues[1] < blues[2]);
 133         assertTrue("top middle part should fade up to blue", blues[1] < blues[2]);
 134         assertTrue("top middle part should fade up to blue", blues[2] < blues[3]);
 135         assertTrue("top middle part should fade up to blue", blues[3] < blues[4]);
 136 
 137         assertTrue("middle part, pixel 4 should be almost completely blue", blues[4] > (0xFF - kThreshold));
 138 
 139         assertTrue("bottom middle part should fade down to white", blues[4] > blues[5]);
 140         assertTrue("bottom middle part should fade down to white", blues[5] > blues[6]);
 141         assertTrue("bottom part, pixel 7 should fade down to white", blues[6] > blues[7]);
 142 
 143         assertTrue("bottom part, pixel 7 should be almost completely clear", blues[7] < kThreshold);
 144         assertTrue("bottom part, pixel 8 should be almost completely clear", blues[8] < kThreshold);
 145 
 146         sg.dispose();
 147     }
 148 
 149 // boilerplate below
 150     public static Test suite() {
 151         return new TestSuite(InterpolationTest.class);
 152     }
 153 
 154     public static void main(String[] args) throws RuntimeException {
 155         TestResult tr = junit.textui.TestRunner.run(suite());
 156         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 157             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
 158         }
 159     }
 160 }