home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / image / Kernel.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  3.8 KB  |  145 lines

  1. /*
  2.  * @(#)Kernel.java    1.9 98/03/18
  3.  *
  4.  * Copyright 1997 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.image;
  16.  
  17.  
  18. /**
  19.  * This class defines a Kernel object.  A kernel is a matrix describing
  20.  * how a given pixel and its surrounding pixels affect the value of the
  21.  * given pixel in a filtering operation.
  22.  *
  23.  * @see ConvolveOp
  24.  * @version 10 Feb 1997
  25.  */
  26. public class Kernel {
  27.     private int  width;
  28.     private int  height;
  29.     private int  xOrigin;
  30.     private int  yOrigin;
  31.     private float data[];
  32.  
  33.     private static native void initIDs();
  34.     static {
  35.         ColorModel.loadLibraries();
  36.         initIDs();
  37.     }
  38.  
  39.     /**
  40.      * Default 3x3 kernels
  41.      */
  42.  
  43.     /**
  44.      * 3x3 Sharpening kernel:<pre>
  45.      *   -1 -1 -1
  46.      *   -1  9 -1
  47.      *   -1 -1 -1</pre>
  48.      */
  49. public static final float[] SHARPEN3x3_1 = {-1.f, -1.f, -1.f,
  50.                                             -1.f,  9.f, -1.f,
  51.                                             -1.f, -1.f, -1.f};
  52.     /**
  53.      * 3x3 Sharpening kernel:<pre>
  54.      *   1 -2  1
  55.      *  -2  5 -2
  56.      *   1 -2  1</pre>
  57.      */
  58.  
  59. public static final float[] SHARPEN3x3_2 = { 1.f, -2.f,  1.f,
  60.                                             -2.f,  5.f, -2.f,
  61.                                              1.f, -2.f,  1.f};
  62.     
  63.     /**
  64.      * 3x3 Sharpening kernel:<pre>
  65.      *   0 -1  0
  66.      *  -1  5 -1
  67.      *   0 -1  0</pre>
  68.      */
  69. public static final float[] SHARPEN3x3_3 = { 0.f, -1.f,  0.f,
  70.                                             -1.f,  5.f, -1.f,
  71.                                              0.f, -1.f,  0.f};
  72.     
  73.     
  74.     /**
  75.      * Create a Kernel object from an array of floats.  The data array
  76.      * is copied.
  77.      * @param width         Width of the kernel.
  78.      * @param height        Height of the kernel.
  79.      * @param data          Kernel data in row major order.
  80.      */
  81.     public Kernel(int width, int height, float data[]) {
  82.         this.width  = width;
  83.         this.height = height;
  84.         this.xOrigin  = width>>1;
  85.         this.yOrigin  = height>>1;
  86.         int len = width*height;
  87.         if (data.length < len) {
  88.             throw new IllegalArgumentException("Data array too small "+
  89.                                                "(is "+data.length+
  90.                                                " and should be "+len);
  91.         }
  92.         this.data = new float[len];
  93.         System.arraycopy(data, 0, this.data, 0, len);
  94.  
  95.     }
  96.  
  97.     /**
  98.      * Returns the X origin.
  99.      */
  100.     final public int getXOrigin(){
  101.         return xOrigin;
  102.     }
  103.  
  104.     /**
  105.      * Returns the Y origin.
  106.      */
  107.     final public int getYOrigin() {
  108.         return yOrigin;
  109.     }
  110.  
  111.     /**
  112.      * Returns the width
  113.      */
  114.     final public int getWidth() {
  115.         return width;
  116.     }
  117.  
  118.     /**
  119.      * Returns the height
  120.      */
  121.     final public int getHeight() {
  122.         return height;
  123.     }
  124.     
  125.     /**
  126.      * Returns the kernel data in row major order.
  127.      * @param data        If non-null, will contain the returned kernel data.
  128.      */
  129.     final public float[] getKernelData(float[] data) {
  130.         if (data == null) {
  131.             data = new float[this.data.length];
  132.         }
  133.         else if (data.length < this.data.length) {
  134.             throw new IllegalArgumentException("Data array too small "+
  135.                                                "(should be "+this.data.length+
  136.                                                " but is "+
  137.                                                data.length+" )");
  138.         }
  139.         System.arraycopy(this.data, 0, data, 0, this.data.length);
  140.  
  141.         return data;
  142.     }
  143.  
  144. }
  145.