1 /*
   2  * Copyright (c) 2007, 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  * @bug     6556332 8011992 8012112
  27  * @summary Test verifies that on-demnad loading of medialib library does
  28  *          not break imageing ops based on this library.
  29  * @run     main MlibOpsTest
  30  * @run     main/othervm/policy=mlib.security.policy MlibOpsTest
  31  */
  32 
  33 
  34 import java.awt.Color;
  35 import java.awt.Graphics2D;
  36 import java.awt.RadialGradientPaint;
  37 import java.awt.geom.AffineTransform;
  38 import java.awt.geom.Point2D;
  39 import java.awt.image.AffineTransformOp;
  40 import java.awt.image.BufferedImage;
  41 import java.awt.image.BufferedImageOp;
  42 import java.awt.image.ByteLookupTable;
  43 import java.awt.image.ConvolveOp;
  44 import java.awt.image.Kernel;
  45 import java.awt.image.LookupOp;
  46 import java.util.Arrays;
  47 
  48 import sun.awt.image.ImagingLib;
  49 
  50 public class MlibOpsTest {
  51 
  52     public static void main(String[] args) {
  53         System.out.println("AffineTransformOp:");
  54         BufferedImageOp op = getAffineTransformOp();
  55         doTest(op);
  56 
  57         System.out.println("ConvolveOp:");
  58         op = getConvolveOp();
  59         doTest(op);
  60 
  61         System.out.println("LookupOp:");
  62         op = getLookupOp();
  63         doTest(op);
  64     }
  65 
  66     public static void doTest(BufferedImageOp op) {
  67         BufferedImage src = createSrcImage();
  68         BufferedImage dst = createImage();
  69         BufferedImage ret = null;
  70         try {
  71             ret = ImagingLib.filter(op, src, dst);
  72         } catch (Exception e) {
  73             throw new RuntimeException("Test FAILED.", e);
  74         }
  75         if (ret == null) {
  76             throw new RuntimeException("Test FAILED: null output");
  77         }
  78 
  79         System.out.println("ret: " + ret);
  80         System.out.println("Test PASSED for " + op.getClass().getName());
  81     }
  82 
  83     private static BufferedImageOp getAffineTransformOp() {
  84         AffineTransform at = new AffineTransform();
  85        return new AffineTransformOp(at,
  86                                     AffineTransformOp.TYPE_BICUBIC);
  87     }
  88 
  89     private static BufferedImageOp getConvolveOp() {
  90         int kw = 3;
  91         int kh = 3;
  92         int size = kw * kh;
  93         float[] kdata = new float[size];
  94         Arrays.fill(kdata, 1.0f / size);
  95 
  96         Kernel k  = new Kernel(kw, kh, kdata);
  97         return new ConvolveOp(k);
  98     }
  99 
 100     private static BufferedImageOp getLookupOp() {
 101         byte[] inv = new byte[256];
 102         for (int i = 0; i < 256; i++) {
 103             inv[i] = (byte)(255 - i);
 104         }
 105         ByteLookupTable table = new ByteLookupTable(0, inv);
 106         return new LookupOp(table, null);
 107     }
 108 
 109 
 110     private static int w = 100;
 111     private static int h = 100;
 112 
 113     private static BufferedImage createImage() {
 114         return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
 115     }
 116 
 117     private static BufferedImage createSrcImage() {
 118         BufferedImage img = createImage();
 119 
 120         Graphics2D g = img.createGraphics();
 121         Color[] colors = { Color.red, Color.green, Color.blue };
 122         float[] dist = {0.0f, 0.5f, 1.0f };
 123         Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
 124 
 125         RadialGradientPaint p =
 126                 new RadialGradientPaint(center, 0.5f * w, dist, colors);
 127         g.setPaint(p);
 128         g.fillRect(0, 0, w, h);
 129         g.dispose();
 130 
 131         return img;
 132     }
 133 }