private synchronized void computeHistogram(Rectangle visibleRect, PlanarImage image) { int channels = image.getSampleModel().getNumBands(); Histogram hist = new Histogram(256, 256, 512, channels); bins = hist.getBins(); // Raster raster = image.getData(visibleRect); Rectangle bounds = visibleRect; // image.getBounds(); int pixel[] = null; int maxPixels = 256; int incX = bounds.width >= 2 * maxPixels ? bounds.width / maxPixels : 1; int incY = bounds.height >= 2 * maxPixels ? bounds.height / maxPixels : 1; double log2 = Math.log(2); int minTileX = image.XToTileX(bounds.x); int maxTileX = image.XToTileX(bounds.x + bounds.width - 1); int minTileY = image.YToTileY(bounds.y); int maxTileY = image.YToTileY(bounds.y + bounds.height - 1); for (int tx = minTileX; tx <= maxTileX; tx++) for (int ty = minTileY; ty <= maxTileY; ty++) { Raster raster = image.getTile(tx, ty); int minX = Math.max(bounds.x, raster.getMinX()); int maxX = Math.min(bounds.x + bounds.width, raster.getMinX() + raster.getWidth()); int minY = Math.max(bounds.y, raster.getMinY()); int maxY = Math.min(bounds.y + bounds.height, raster.getMinY() + raster.getHeight()); for (int x = minX; x < maxX; x += incX) for (int y = minY; y < maxY; y += incY) { pixel = raster.getPixel(x, y, pixel); for (int c = 0; c < channels; c++) { int v = (int) (511 * logTable[pixel[c]] / (16 * log2)); if (v > 255) bins[c][v - 256]++; else bins[c][0]++; } } } bins = hist.getBins(); }
private BufferedImage cropScaleGrayscale(Rectangle visibleRect, RenderedImage image) { int minX = image.getMinX(); int minY = image.getMinY(); int width = image.getWidth(); int height = image.getHeight(); Rectangle bounds = new Rectangle(minX, minY, width, height); visibleRect = bounds.intersection(visibleRect); if (bounds.contains(visibleRect)) { ParameterBlock pb = new ParameterBlock(); pb.addSource(image); pb.add((float) visibleRect.x); pb.add((float) visibleRect.y); pb.add((float) visibleRect.width); pb.add((float) visibleRect.height); image = JAI.create("Crop", pb, JAIContext.noCacheHint); } Dimension previewSize = getSize(); if ((visibleRect.width > previewSize.width) || (visibleRect.height > previewSize.height)) { float scale = Math.min( previewSize.width / (float) visibleRect.width, previewSize.height / (float) visibleRect.height); image = ConvolveDescriptor.create(image, Functions.getGaussKernel(.25 / scale), null); ParameterBlock pb = new ParameterBlock(); pb.addSource(image); pb.add(scale); pb.add(scale); image = JAI.create("Scale", pb, JAIContext.noCacheHint); } image = Functions.toColorSpace(image, JAIContext.systemColorSpace, null); if (image.getSampleModel().getDataType() == DataBuffer.TYPE_USHORT) { image = Functions.fromUShortToByte(image, null); } return Functions.toFastBufferedImage(image); }