📄 lookupop.java
字号:
if (ltable instanceof ByteLookupTable) { byteFilter ((ByteLookupTable) ltable, src, dst, width, height, numBands); } else if (ltable instanceof ShortLookupTable) { shortFilter ((ShortLookupTable) ltable, src, dst, width, height, numBands); } else { // Not one we recognize so do it slowly int sminX = src.getMinX(); int sY = src.getMinY(); int dminX = dst.getMinX(); int dY = dst.getMinY(); for (int y=0; y < height; y++, sY++, dY++) { int sX = sminX; int dX = dminX; for (int x=0; x < width; x++, sX++, dX++) { // Find data for all bands at this x,y position src.getPixel(sX, sY, srcPix); // Lookup the data for all bands at this x,y position ltable.lookupPixel(srcPix, srcPix); // Put it back for all bands dst.setPixel(dX, dY, srcPix); } } } return dst; } /** * Returns the bounding box of the filtered destination image. Since * this is not a geometric operation, the bounding box does not * change. * @param src the <code>BufferedImage</code> to be filtered * @return the bounds of the filtered definition image. */ public final Rectangle2D getBounds2D (BufferedImage src) { return getBounds2D(src.getRaster()); } /** * Returns the bounding box of the filtered destination Raster. Since * this is not a geometric operation, the bounding box does not * change. * @param src the <code>Raster</code> to be filtered * @return the bounds of the filtered definition <code>Raster</code>. */ public final Rectangle2D getBounds2D (Raster src) { return src.getBounds(); } /** * Creates a zeroed destination image with the correct size and number of * bands. If destCM is <code>null</code>, an appropriate * <code>ColorModel</code> will be used. * @param src Source image for the filter operation. * @param destCM the destination's <code>ColorModel</code>, which * can be <code>null</code>. * @return a filtered destination <code>BufferedImage</code>. */ public BufferedImage createCompatibleDestImage (BufferedImage src, ColorModel destCM) { BufferedImage image; int w = src.getWidth(); int h = src.getHeight(); int transferType = DataBuffer.TYPE_BYTE; if (destCM == null) { ColorModel cm = src.getColorModel(); Raster raster = src.getRaster(); if (cm instanceof ComponentColorModel) { DataBuffer db = raster.getDataBuffer(); boolean hasAlpha = cm.hasAlpha(); boolean isPre = cm.isAlphaPremultiplied(); int trans = cm.getTransparency(); int[] nbits = null; if (ltable instanceof ByteLookupTable) { if (db.getDataType() == db.TYPE_USHORT) { // Dst raster should be of type byte if (hasAlpha) { nbits = new int[2]; if (trans == cm.BITMASK) { nbits[1] = 1; } else { nbits[1] = 8; } } else { nbits = new int[1]; } nbits[0] = 8; } // For byte, no need to change the cm } else if (ltable instanceof ShortLookupTable) { transferType = DataBuffer.TYPE_USHORT; if (db.getDataType() == db.TYPE_BYTE) { if (hasAlpha) { nbits = new int[2]; if (trans == cm.BITMASK) { nbits[1] = 1; } else { nbits[1] = 16; } } else { nbits = new int[1]; } nbits[0] = 16; } } if (nbits != null) { cm = new ComponentColorModel(cm.getColorSpace(), nbits, hasAlpha, isPre, trans, transferType); } } image = new BufferedImage(cm, cm.createCompatibleWritableRaster(w, h), cm.isAlphaPremultiplied(), null); } else { image = new BufferedImage(destCM, destCM.createCompatibleWritableRaster(w, h), destCM.isAlphaPremultiplied(), null); } return image; } /** * Creates a zeroed-destination <code>Raster</code> with the * correct size and number of bands, given this source. * @param src the <code>Raster</code> to be transformed * @return the zeroed-destination <code>Raster</code>. */ public WritableRaster createCompatibleDestRaster (Raster src) { return src.createCompatibleWritableRaster(); } /** * Returns the location of the destination point given a * point in the source. If <code>dstPt</code> is not * <code>null</code>, it will be used to hold the return value. * Since this is not a geometric operation, the <code>srcPt</code> * will equal the <code>dstPt</code>. * @param srcPt a <code>Point2D</code> that represents a point * in the source image * @param dstPt a <code>Point2D</code>that represents the location * in the destination * @return the <code>Point2D</code> in the destination that * corresponds to the specified point in the source. */ public final Point2D getPoint2D (Point2D srcPt, Point2D dstPt) { if (dstPt == null) { dstPt = new Point2D.Float(); } dstPt.setLocation(srcPt.getX(), srcPt.getY()); return dstPt; } /** * Returns the rendering hints for this op. * @return the <code>RenderingHints</code> object associated * with this op. */ public final RenderingHints getRenderingHints() { return hints; } private final void byteFilter(ByteLookupTable lookup, Raster src, WritableRaster dst, int width, int height, int numBands) { int[] srcPix = null; // Find the ref to the table and the offset byte[][] table = lookup.getTable(); int offset = lookup.getOffset(); int tidx; int step=1; // Check if it is one lookup applied to all bands if (table.length == 1) { step=0; } int x; int y; int band; int len = table[0].length; // Loop through the data for ( y=0; y < height; y++) { tidx = 0; for ( band=0; band < numBands; band++, tidx+=step) { // Find data for this band, scanline srcPix = src.getSamples(0, y, width, 1, band, srcPix); for ( x=0; x < width; x++) { int index = srcPix[x]-offset; if (index < 0 || index > len) { throw new IllegalArgumentException("index ("+index+ "(out of range: "+ " srcPix["+x+ "]="+ srcPix[x]+ " offset="+ offset); } // Do the lookup srcPix[x] = table[tidx][index]; } // Put it back dst.setSamples(0, y, width, 1, band, srcPix); } } } private final void shortFilter(ShortLookupTable lookup, Raster src, WritableRaster dst, int width, int height, int numBands) { int band; int[] srcPix = null; // Find the ref to the table and the offset short[][] table = lookup.getTable(); int offset = lookup.getOffset(); int tidx; int step=1; // Check if it is one lookup applied to all bands if (table.length == 1) { step=0; } int x = 0; int y = 0; int index; int maxShort = (1<<16)-1; // Loop through the data for (y=0; y < height; y++) { tidx = 0; for ( band=0; band < numBands; band++, tidx+=step) { // Find data for this band, scanline srcPix = src.getSamples(0, y, width, 1, band, srcPix); for ( x=0; x < width; x++) { index = srcPix[x]-offset; if (index < 0 || index > maxShort) { throw new IllegalArgumentException("index out of range "+ index+" x is "+x+ "srcPix[x]="+srcPix[x] +" offset="+ offset); } // Do the lookup srcPix[x] = table[tidx][index]; } // Put it back dst.setSamples(0, y, width, 1, band, srcPix); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -