1 7 8 package java.awt.image; 9 10 import java.awt.GraphicsEnvironment ; 11 import java.awt.color.ICC_Profile ; 12 import java.awt.geom.Rectangle2D ; 13 import java.awt.Rectangle ; 14 import java.awt.geom.Point2D ; 15 import java.awt.RenderingHints ; 16 import sun.awt.image.ImagingLib; 17 18 42 public class BandCombineOp implements RasterOp { 43 float[][] matrix; 44 int nrows = 0; 45 int ncols = 0; 46 RenderingHints hints; 47 48 66 public BandCombineOp (float[][] matrix, RenderingHints hints) { 67 nrows = matrix.length; 68 ncols = matrix[0].length; 69 this.matrix = new float[nrows][ncols+1]; 70 for (int i=0; i < nrows; i++) { 71 System.arraycopy(matrix[i], 0, this.matrix[i], 0, ncols); 72 } 73 this.hints = hints; 74 } 75 76 81 public final float[][] getMatrix() { 82 return (float[][]) matrix.clone(); 83 } 84 85 104 public WritableRaster filter(Raster src, WritableRaster dst) { 105 int nBands = src.getNumBands(); 106 if (ncols != nBands && ncols != (nBands+1)) { 107 throw new IllegalArgumentException ("Number of columns in the "+ 108 "matrix ("+ncols+ 109 ") must be equal to the number"+ 110 " of bands ([+1]) in src ("+ 111 nBands+")."); 112 } 113 if (dst == null) { 114 dst = createCompatibleDestRaster(src); 115 } 116 else if (nrows != dst.getNumBands()) { 117 throw new IllegalArgumentException ("Number of rows in the "+ 118 "matrix ("+nrows+ 119 ") must be equal to the number"+ 120 " of bands ([+1]) in dst ("+ 121 nBands+")."); 122 } 123 124 if (ImagingLib.filter(this, src, dst) != null) { 125 return dst; 126 } 127 128 int[] pixel = null; 129 int[] dstPixel = new int[dst.getNumBands()]; 130 float accum; 131 int sminX = src.getMinX(); 132 int sY = src.getMinY(); 133 int dminX = dst.getMinX(); 134 int dY = dst.getMinY(); 135 int sX; 136 int dX; 137 if (ncols == nBands) { 138 for (int y=0; y < src.getHeight(); y++, sY++, dY++) { 139 dX = dminX; 140 sX = sminX; 141 for (int x=0; x < src.getWidth(); x++, sX++, dX++) { 142 pixel = src.getPixel(sX, sY, pixel); 143 for (int r=0; r < nrows; r++) { 144 accum = 0.f; 145 for (int c=0; c < ncols; c++) { 146 accum += matrix[r][c]*pixel[c]; 147 } 148 dstPixel[r] = (int) accum; 149 } 150 dst.setPixel(dX, dY, dstPixel); 151 } 152 } 153 } 154 else { 155 for (int y=0; y < src.getHeight(); y++, sY++, dY++) { 157 dX = dminX; 158 sX = sminX; 159 for (int x=0; x < src.getWidth(); x++, sX++, dX++) { 160 pixel = src.getPixel(sX, sY, pixel); 161 for (int r=0; r < nrows; r++) { 162 accum = 0.f; 163 for (int c=0; c < nBands; c++) { 164 accum += matrix[r][c]*pixel[c]; 165 } 166 dstPixel[r] = (int) (accum+matrix[r][nBands]); 167 } 168 dst.setPixel(dX, dY, dstPixel); 169 } 170 } 171 } 172 173 return dst; 174 } 175 176 192 public final Rectangle2D getBounds2D (Raster src) { 193 return src.getBounds(); 194 } 195 196 197 208 public WritableRaster createCompatibleDestRaster (Raster src) { 209 int nBands = src.getNumBands(); 210 if ((ncols != nBands) && (ncols != (nBands+1))) { 211 throw new IllegalArgumentException ("Number of columns in the "+ 212 "matrix ("+ncols+ 213 ") must be equal to the number"+ 214 " of bands ([+1]) in src ("+ 215 nBands+")."); 216 } 217 if (src.getNumBands() == nrows) { 218 return src.createCompatibleWritableRaster(); 219 } 220 else { 221 throw new IllegalArgumentException ("Don't know how to create a "+ 222 " compatible Raster with "+ 223 nrows+" bands."); 224 } 225 } 226 227 241 public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) { 242 if (dstPt == null) { 243 dstPt = new Point2D.Float (); 244 } 245 dstPt.setLocation(srcPt.getX(), srcPt.getY()); 246 247 return dstPt; 248 } 249 250 256 public final RenderingHints getRenderingHints() { 257 return hints; 258 } 259 } 260 | Popular Tags |