1. /*
  2. * @(#)Kernel.java 1.18 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.image;
  8. /**
  9. * The <code>Kernel</code> class defines a matrix that describes how a
  10. * specified pixel and its surrounding pixels affect the value
  11. * computed for the pixel's position in the output image of a filtering
  12. * operation. The X origin and Y origin indicate the kernel matrix element
  13. * that corresponds to the pixel position for which an output value is
  14. * being computed.
  15. *
  16. * @see ConvolveOp
  17. * @version 10 Feb 1997
  18. */
  19. public class Kernel implements Cloneable {
  20. private int width;
  21. private int height;
  22. private int xOrigin;
  23. private int yOrigin;
  24. private float data[];
  25. private static native void initIDs();
  26. static {
  27. ColorModel.loadLibraries();
  28. initIDs();
  29. }
  30. /**
  31. * Constructs a <code>Kernel</code> object from an array of floats.
  32. * The first <code>width</code>*<code>height</code> elements of
  33. * the <code>data</code> array are copied.
  34. * If the length of the <code>data</code> array is less
  35. * than width*height, an <code>IllegalArgumentException</code> is thrown.
  36. * The X origin is (width-1)/2 and the Y origin is (height-1)/2.
  37. * @param width width of the kernel
  38. * @param height height of the kernel
  39. * @param data kernel data in row major order
  40. * @throws IllegalArgumentException if the length of <code>data</code>
  41. * is less than the product of <code>width</code> and
  42. * <code>height</code>
  43. */
  44. public Kernel(int width, int height, float data[]) {
  45. this.width = width;
  46. this.height = height;
  47. this.xOrigin = (width-1)>>1;
  48. this.yOrigin = (height-1)>>1;
  49. int len = width*height;
  50. if (data.length < len) {
  51. throw new IllegalArgumentException("Data array too small "+
  52. "(is "+data.length+
  53. " and should be "+len);
  54. }
  55. this.data = new float[len];
  56. System.arraycopy(data, 0, this.data, 0, len);
  57. }
  58. /**
  59. * Returns the X origin of this <code>Kernel</code>.
  60. * @return the X origin.
  61. */
  62. final public int getXOrigin(){
  63. return xOrigin;
  64. }
  65. /**
  66. * Returns the Y origin of this <code>Kernel</code>.
  67. * @return the Y origin.
  68. */
  69. final public int getYOrigin() {
  70. return yOrigin;
  71. }
  72. /**
  73. * Returns the width of this <code>Kernel</code>.
  74. * @return the width of this <code>Kernel</code>.
  75. */
  76. final public int getWidth() {
  77. return width;
  78. }
  79. /**
  80. * Returns the height of this <code>Kernel</code>.
  81. * @return the height of this <code>Kernel</code>.
  82. */
  83. final public int getHeight() {
  84. return height;
  85. }
  86. /**
  87. * Returns the kernel data in row major order.
  88. * The <code>data</code> array is returned. If <code>data</code>
  89. * is <code>null</code>, a new array is allocated.
  90. * @param data if non-null, contains the returned kernel data
  91. * @return the <code>data</code> array containing the kernel data
  92. * in row major order or, if <code>data</code> is
  93. * <code>null</code>, a newly allocated array containing
  94. * the kernel data in row major order
  95. * @throws IllegalArgumentException if <code>data</code> is less
  96. * than the size of this <code>Kernel</code>
  97. */
  98. final public float[] getKernelData(float[] data) {
  99. if (data == null) {
  100. data = new float[this.data.length];
  101. }
  102. else if (data.length < this.data.length) {
  103. throw new IllegalArgumentException("Data array too small "+
  104. "(should be "+this.data.length+
  105. " but is "+
  106. data.length+" )");
  107. }
  108. System.arraycopy(this.data, 0, data, 0, this.data.length);
  109. return data;
  110. }
  111. /**
  112. * Clones this object.
  113. * @return a clone of this object.
  114. */
  115. public Object clone() {
  116. try {
  117. return super.clone();
  118. } catch (CloneNotSupportedException e) {
  119. // this shouldn't happen, since we are Cloneable
  120. throw new InternalError();
  121. }
  122. }
  123. }