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