34 import sun.awt.image.ImagingLib; |
34 import sun.awt.image.ImagingLib; |
35 import java.util.Arrays; |
35 import java.util.Arrays; |
36 |
36 |
37 /** |
37 /** |
38 * This class performs an arbitrary linear combination of the bands |
38 * This class performs an arbitrary linear combination of the bands |
39 * in a <CODE>Raster</CODE>, using a specified matrix. |
39 * in a {@code Raster}, using a specified matrix. |
40 * <p> |
40 * <p> |
41 * The width of the matrix must be equal to the number of bands in the |
41 * The width of the matrix must be equal to the number of bands in the |
42 * source <CODE>Raster</CODE>, optionally plus one. If there is one more |
42 * source {@code Raster}, optionally plus one. If there is one more |
43 * column in the matrix than the number of bands, there is an implied 1 at the |
43 * column in the matrix than the number of bands, there is an implied 1 at the |
44 * end of the vector of band samples representing a pixel. The height |
44 * end of the vector of band samples representing a pixel. The height |
45 * of the matrix must be equal to the number of bands in the destination. |
45 * of the matrix must be equal to the number of bands in the destination. |
46 * <p> |
46 * <p> |
47 * For example, a 3-banded <CODE>Raster</CODE> might have the following |
47 * For example, a 3-banded {@code Raster} might have the following |
48 * transformation applied to each pixel in order to invert the second band of |
48 * transformation applied to each pixel in order to invert the second band of |
49 * the <CODE>Raster</CODE>. |
49 * the {@code Raster}. |
50 * <pre> |
50 * <pre> |
51 * [ 1.0 0.0 0.0 0.0 ] [ b1 ] |
51 * [ 1.0 0.0 0.0 0.0 ] [ b1 ] |
52 * [ 0.0 -1.0 0.0 255.0 ] x [ b2 ] |
52 * [ 0.0 -1.0 0.0 255.0 ] x [ b2 ] |
53 * [ 0.0 0.0 1.0 0.0 ] [ b3 ] |
53 * [ 0.0 0.0 1.0 0.0 ] [ b3 ] |
54 * [ 1 ] |
54 * [ 1 ] |
62 int nrows = 0; |
62 int nrows = 0; |
63 int ncols = 0; |
63 int ncols = 0; |
64 RenderingHints hints; |
64 RenderingHints hints; |
65 |
65 |
66 /** |
66 /** |
67 * Constructs a <CODE>BandCombineOp</CODE> with the specified matrix. |
67 * Constructs a {@code BandCombineOp} with the specified matrix. |
68 * The width of the matrix must be equal to the number of bands in |
68 * The width of the matrix must be equal to the number of bands in |
69 * the source <CODE>Raster</CODE>, optionally plus one. If there is one |
69 * the source {@code Raster}, optionally plus one. If there is one |
70 * more column in the matrix than the number of bands, there is an implied |
70 * more column in the matrix than the number of bands, there is an implied |
71 * 1 at the end of the vector of band samples representing a pixel. The |
71 * 1 at the end of the vector of band samples representing a pixel. The |
72 * height of the matrix must be equal to the number of bands in the |
72 * height of the matrix must be equal to the number of bands in the |
73 * destination. |
73 * destination. |
74 * <p> |
74 * <p> |
75 * The first subscript is the row index and the second |
75 * The first subscript is the row index and the second |
76 * is the column index. This operation uses none of the currently |
76 * is the column index. This operation uses none of the currently |
77 * defined rendering hints; the <CODE>RenderingHints</CODE> argument can be |
77 * defined rendering hints; the {@code RenderingHints} argument can be |
78 * null. |
78 * null. |
79 * |
79 * |
80 * @param matrix The matrix to use for the band combine operation. |
80 * @param matrix The matrix to use for the band combine operation. |
81 * @param hints The <CODE>RenderingHints</CODE> object for this operation. |
81 * @param hints The {@code RenderingHints} object for this operation. |
82 * Not currently used so it can be null. |
82 * Not currently used so it can be null. |
83 */ |
83 */ |
84 public BandCombineOp (float[][] matrix, RenderingHints hints) { |
84 public BandCombineOp (float[][] matrix, RenderingHints hints) { |
85 nrows = matrix.length; |
85 nrows = matrix.length; |
86 ncols = matrix[0].length; |
86 ncols = matrix[0].length; |
111 } |
111 } |
112 return ret; |
112 return ret; |
113 } |
113 } |
114 |
114 |
115 /** |
115 /** |
116 * Transforms the <CODE>Raster</CODE> using the matrix specified in the |
116 * Transforms the {@code Raster} using the matrix specified in the |
117 * constructor. An <CODE>IllegalArgumentException</CODE> may be thrown if |
117 * constructor. An {@code IllegalArgumentException} may be thrown if |
118 * the number of bands in the source or destination is incompatible with |
118 * the number of bands in the source or destination is incompatible with |
119 * the matrix. See the class comments for more details. |
119 * the matrix. See the class comments for more details. |
120 * <p> |
120 * <p> |
121 * If the destination is null, it will be created with a number of bands |
121 * If the destination is null, it will be created with a number of bands |
122 * equalling the number of rows in the matrix. No exception is thrown |
122 * equalling the number of rows in the matrix. No exception is thrown |
123 * if the operation causes a data overflow. |
123 * if the operation causes a data overflow. |
124 * |
124 * |
125 * @param src The <CODE>Raster</CODE> to be filtered. |
125 * @param src The {@code Raster} to be filtered. |
126 * @param dst The <CODE>Raster</CODE> in which to store the results |
126 * @param dst The {@code Raster} in which to store the results |
127 * of the filter operation. |
127 * of the filter operation. |
128 * |
128 * |
129 * @return The filtered <CODE>Raster</CODE>. |
129 * @return The filtered {@code Raster}. |
130 * |
130 * |
131 * @throws IllegalArgumentException If the number of bands in the |
131 * @throws IllegalArgumentException If the number of bands in the |
132 * source or destination is incompatible with the matrix. |
132 * source or destination is incompatible with the matrix. |
133 */ |
133 */ |
134 public WritableRaster filter(Raster src, WritableRaster dst) { |
134 public WritableRaster filter(Raster src, WritableRaster dst) { |
205 |
205 |
206 /** |
206 /** |
207 * Returns the bounding box of the transformed destination. Since |
207 * Returns the bounding box of the transformed destination. Since |
208 * this is not a geometric operation, the bounding box is the same for |
208 * this is not a geometric operation, the bounding box is the same for |
209 * the source and destination. |
209 * the source and destination. |
210 * An <CODE>IllegalArgumentException</CODE> may be thrown if the number of |
210 * An {@code IllegalArgumentException} may be thrown if the number of |
211 * bands in the source is incompatible with the matrix. See |
211 * bands in the source is incompatible with the matrix. See |
212 * the class comments for more details. |
212 * the class comments for more details. |
213 * |
213 * |
214 * @param src The <CODE>Raster</CODE> to be filtered. |
214 * @param src The {@code Raster} to be filtered. |
215 * |
215 * |
216 * @return The <CODE>Rectangle2D</CODE> representing the destination |
216 * @return The {@code Rectangle2D} representing the destination |
217 * image's bounding box. |
217 * image's bounding box. |
218 * |
218 * |
219 * @throws IllegalArgumentException If the number of bands in the source |
219 * @throws IllegalArgumentException If the number of bands in the source |
220 * is incompatible with the matrix. |
220 * is incompatible with the matrix. |
221 */ |
221 */ |
223 return src.getBounds(); |
223 return src.getBounds(); |
224 } |
224 } |
225 |
225 |
226 |
226 |
227 /** |
227 /** |
228 * Creates a zeroed destination <CODE>Raster</CODE> with the correct size |
228 * Creates a zeroed destination {@code Raster} with the correct size |
229 * and number of bands. |
229 * and number of bands. |
230 * An <CODE>IllegalArgumentException</CODE> may be thrown if the number of |
230 * An {@code IllegalArgumentException} may be thrown if the number of |
231 * bands in the source is incompatible with the matrix. See |
231 * bands in the source is incompatible with the matrix. See |
232 * the class comments for more details. |
232 * the class comments for more details. |
233 * |
233 * |
234 * @param src The <CODE>Raster</CODE> to be filtered. |
234 * @param src The {@code Raster} to be filtered. |
235 * |
235 * |
236 * @return The zeroed destination <CODE>Raster</CODE>. |
236 * @return The zeroed destination {@code Raster}. |
237 */ |
237 */ |
238 public WritableRaster createCompatibleDestRaster (Raster src) { |
238 public WritableRaster createCompatibleDestRaster (Raster src) { |
239 int nBands = src.getNumBands(); |
239 int nBands = src.getNumBands(); |
240 if ((ncols != nBands) && (ncols != (nBands+1))) { |
240 if ((ncols != nBands) && (ncols != (nBands+1))) { |
241 throw new IllegalArgumentException("Number of columns in the "+ |
241 throw new IllegalArgumentException("Number of columns in the "+ |
254 } |
254 } |
255 } |
255 } |
256 |
256 |
257 /** |
257 /** |
258 * Returns the location of the corresponding destination point given a |
258 * Returns the location of the corresponding destination point given a |
259 * point in the source <CODE>Raster</CODE>. If <CODE>dstPt</CODE> is |
259 * point in the source {@code Raster}. If {@code dstPt} is |
260 * specified, it is used to hold the return value. |
260 * specified, it is used to hold the return value. |
261 * Since this is not a geometric operation, the point returned |
261 * Since this is not a geometric operation, the point returned |
262 * is the same as the specified <CODE>srcPt</CODE>. |
262 * is the same as the specified {@code srcPt}. |
263 * |
263 * |
264 * @param srcPt The <code>Point2D</code> that represents the point in |
264 * @param srcPt The {@code Point2D} that represents the point in |
265 * the source <code>Raster</code> |
265 * the source {@code Raster} |
266 * @param dstPt The <CODE>Point2D</CODE> in which to store the result. |
266 * @param dstPt The {@code Point2D} in which to store the result. |
267 * |
267 * |
268 * @return The <CODE>Point2D</CODE> in the destination image that |
268 * @return The {@code Point2D} in the destination image that |
269 * corresponds to the specified point in the source image. |
269 * corresponds to the specified point in the source image. |
270 */ |
270 */ |
271 public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) { |
271 public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) { |
272 if (dstPt == null) { |
272 if (dstPt == null) { |
273 dstPt = new Point2D.Float(); |
273 dstPt = new Point2D.Float(); |