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 Simple test that draws an orange rectangle into an image, does a ColorConvertOp using ColorSpace.CS_GRAY and then tests for the converted color.
  27  * @summary This test is gonna vary based on the color space profile, therefore when we test for the expected color we are testing for a very wide range.
  28  * @summary This is supposed to catch big errors such as endianess issues.
  29  * @summary com.apple.junit.java.graphics.images
  30  */
  31 
  32 import junit.framework.*;
  33 import java.awt.*;
  34 import java.awt.color.ColorSpace;
  35 import java.awt.image.*;
  36 
  37 public class TestColorConvertOp extends TestCase {
  38 
  39     static final int        w = 400;
  40     static final int        h = 400;
  41     GraphicsEnvironment     ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  42     GraphicsDevice          gd = ge.getDefaultScreenDevice();
  43     GraphicsConfiguration   gc = gd.getDefaultConfiguration();
  44 
  45 
  46     public void testColorConvertOp() {
  47         BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  48         performTest(bi);
  49 
  50         bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  51         performTest(bi);
  52 
  53         bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
  54         performTest(bi);
  55 
  56         bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
  57         performTest(bi);
  58     }
  59 
  60     public void performTest(BufferedImage bi) {
  61         Graphics2D g2d = bi.createGraphics();
  62         g2d.setColor(Color.orange);
  63         g2d.fillRect(0, 0, w, h);
  64 
  65         ColorConvertOp grayOp = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
  66         grayOp.filter(bi, bi);
  67 
  68         Color c = new Color(bi.getRGB(100, 100));
  69 
  70         int r = c.getRed();
  71         int g = c.getGreen();
  72         int b = c.getBlue();
  73 
  74         int threshold = 10; // treshold for the color mismatch on difference machines
  75         boolean success = true;
  76 
  77         // with a gray transform the color space should be
  78         if (((r - threshold) > b) || ((r + threshold) < b)) {
  79             success = false;
  80         }
  81 
  82         if (((b - threshold) > g) || ((b + threshold) < g)) {
  83             success = false;
  84         }
  85 
  86         //System.err.println(" r= " + r + ", g= " + g + ", b= " + b + " success= " + success);
  87 
  88         assertEquals( "There was an error with the ColorConvertOp", true, success);
  89     }
  90 
  91 
  92     public static Test suite() {
  93         return new TestSuite(TestColorConvertOp.class);
  94     }
  95 
  96 
  97     public static void main (String[] args) throws RuntimeException {
  98         TestResult tr = junit.textui.TestRunner.run(suite());
  99         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 100             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
 101         }
 102     }
 103 }