private static BufferedImage rgbaToIndexedBufferedImage(final BufferedImage sourceBufferedImage) { final BufferedImage indexedImage = new BufferedImage( sourceBufferedImage.getWidth(), sourceBufferedImage.getHeight(), BufferedImage.TYPE_BYTE_INDEXED); final ColorModel cm = indexedImage.getColorModel(); final IndexColorModel icm = (IndexColorModel) cm; final int size = icm.getMapSize(); final byte[] reds = new byte[size]; final byte[] greens = new byte[size]; final byte[] blues = new byte[size]; icm.getReds(reds); icm.getGreens(greens); icm.getBlues(blues); final WritableRaster raster = indexedImage.getRaster(); final int pixel = raster.getSample(0, 0, 0); final IndexColorModel resultIcm = new IndexColorModel(8, size, reds, greens, blues, pixel); final BufferedImage resultIndexedImage = new BufferedImage(resultIcm, raster, sourceBufferedImage.isAlphaPremultiplied(), null); resultIndexedImage.getGraphics().drawImage(sourceBufferedImage, 0, 0, null); return resultIndexedImage; }
/** * Whether the pixel is black. * * @param image source image * @param x * @param y * @return */ public static boolean isBlack(BufferedImage image, int x, int y) { if (image.getType() == BufferedImage.TYPE_BYTE_BINARY) { WritableRaster raster = image.getRaster(); int pixelRGBValue = raster.getSample(x, y, 0); return pixelRGBValue == 0; } int luminanceValue = 140; return isBlack(image, x, y, luminanceValue); }
painter.paint(g2, pointShape, legend, 1, false); assertEquals(0, image.getRaster().getSample(0, 0, 0)); assertEquals(64, image.getRaster().getSample(0, 0, 1)); assertEquals(128, image.getRaster().getSample(0, 0, 2)); assertEquals(255, image.getRaster().getSample(30, 30, 0)); assertEquals(255, image.getRaster().getSample(30, 30, 1)); assertEquals(255, image.getRaster().getSample(30, 30, 2)); assertEquals(0, image.getRaster().getSample(90, 30, 0)); assertEquals(0, image.getRaster().getSample(90, 30, 1)); assertEquals(0, image.getRaster().getSample(90, 30, 2)); g2 = (Graphics2D) image.getGraphics(); painter.paint(g2, pointShape, legend, 2, false); assertEquals(255, image.getRaster().getSample(0, 0, 0)); assertEquals(255, image.getRaster().getSample(0, 0, 1)); assertEquals(192, image.getRaster().getSample(0, 0, 2)); assertEquals(0, image.getRaster().getSample(30, 30, 0)); assertEquals(0, image.getRaster().getSample(30, 30, 1)); assertEquals(0, image.getRaster().getSample(30, 30, 2)); assertEquals(0, image.getRaster().getSample(90, 30, 0)); assertEquals(0, image.getRaster().getSample(90, 30, 1)); assertEquals(0, image.getRaster().getSample(90, 30, 2));
WritableRaster wr = image.getRaster() ; for (int y=0, nb=0 ; y < image.getHeight() ; y++) for (int x=0 ; x < image.getWidth() ; x++, nb++) { int r = wr.getSample(x, y, 0) ; // You just give the channel number, no need to handle the encoding. int g = wr.getSample(x, y, 1) ; int b = wr.getSample(x, y, 2) ; }
WritableRaster r = image.getRaster() ; for (int y=0, nb=0 ; y < image.getHeight() ; y++) for (int x=0 ; x < image.getWidth() ; x++, nb++) pixels[nb] = r.getSample(x, y, 0) ; // For a gray level image.
WritableRaster wr = myBufferedImage.getRaster(); for(int i = 0; i < myBufferedImage.getWidth(); i++){ for(int j = 0; j < myBufferedImage.getHeight(); j++){ int grayLevelPixel = wr.getSample(i, j, 0); wr.setSample(i, j, 0, grayLevelPixel); // Setting same gray level, will do nothing on the image, just to show how. } }
public Category get(int i, int j) { return categories.get(bufferedImage.getRaster().getSample(i, j, 0)); }
/** * Performs a lookup against an internal raster. * * @param grid * @param idx * @return */ private boolean lookup(GridSubset grid, long[] idx) { BufferedImage mat = matrices.get(grid.getName())[(int) idx[2]]; long[] gridCoverage = grid.getCoverage((int) idx[2]); // Changing index to top left hand origin long x = idx[0] - gridCoverage[0]; long y = gridCoverage[3] - idx[1]; return (mat.getRaster().getSample((int) x, (int) y, 0) == 0); }
public static BufferedImage rgbaToIndexedBufferedImage(BufferedImage sourceBufferedImage) { //With this constructor we create an indexed bufferedimage with the same dimensiosn and with a default 256 color model BufferedImage indexedImage= new BufferedImage(sourceBufferedImage.getWidth(),sourceBufferedImage.getHeight(),BufferedImage.TYPE_BYTE_INDEXED); ColorModel cm = indexedImage.getColorModel(); IndexColorModel icm=(IndexColorModel) cm; int size=icm.getMapSize(); byte[] reds = new byte[size]; byte[] greens = new byte[size]; byte[] blues = new byte[size]; icm.getReds(reds); icm.getGreens(greens); icm.getBlues(blues); WritableRaster raster=indexedImage.getRaster(); int pixel = raster.getSample(0, 0, 0); IndexColorModel icm2 = new IndexColorModel(8, size, reds, greens, blues,pixel); indexedImage=new BufferedImage(icm2, raster,sourceBufferedImage.isAlphaPremultiplied(), null); indexedImage.getGraphics().drawImage(sourceBufferedImage, 0, 0, null); return indexedImage; }
IndexColorModel icm = (IndexColorModel) cm; WritableRaster raster = image.getRaster(); int pixel = raster.getSample(x, y, 0); // pixel is offset in ICM's palette int size = icm.getMapSize(); byte[] reds = new byte[size];
/** * Inner method used for checking if the Borders contains No Data and substituting them. (Byte images). * * @param dest */ private void fillNoDataByte(WritableRaster dest) { Rectangle srcBounds = getSourceImage(0).getBounds(); Rectangle destBounds = dest.getBounds(); // The operation is performed only on the image borders if (!srcBounds.contains(destBounds) && !(destBounds.intersection(srcBounds).isEmpty())) { int minX = dest.getMinX(); int minY = dest.getMinY(); int maxX = minX + dest.getWidth(); int maxY = minY + dest.getHeight(); int numBands = dest.getNumBands(); for (int i = minX; i < maxX; i++) { for (int j = minY; j < maxY; j++) { for (int b = 0; b < numBands; b++) { int sample = dest.getSample(i, j, b) & 0xFF; // No Data check dest.setSample(i, j, b, byteLookupTable[sample]); } } } } }
/** * Inner method used for checking if the Borders contains No Data and substituting them. (Short images). * * @param dest */ private void fillNoDataShort(WritableRaster dest) { Rectangle srcBounds = getSourceImage(0).getBounds(); Rectangle destBounds = dest.getBounds(); // The operation is performed only on the image borders if (!srcBounds.contains(destBounds) && !(destBounds.intersection(srcBounds).isEmpty())) { int minX = dest.getMinX(); int minY = dest.getMinY(); int maxX = minX + dest.getWidth(); int maxY = minY + dest.getHeight(); int numBands = dest.getNumBands(); for (int i = minX; i < maxX; i++) { for (int j = minY; j < maxY; j++) { for (int b = 0; b < numBands; b++) { short sample = (short) dest.getSample(i, j, b); // No Data check if (noData.contains(sample)) { dest.setSample(i, j, b, destNoDataShort); } else { dest.setSample(i, j, b, sample); } } } } } }
int sample = dest.getSample(i, j, b) & 0xFFFF;
/** * Inner method used for checking if the Borders contains No Data and substituting them. (Integer images). * * @param dest */ private void fillNoDataInt(WritableRaster dest) { Rectangle srcBounds = getSourceImage(0).getBounds(); Rectangle destBounds = dest.getBounds(); // The operation is performed only on the image borders if (!srcBounds.contains(destBounds) && !(destBounds.intersection(srcBounds).isEmpty())) { int minX = dest.getMinX(); int minY = dest.getMinY(); int maxX = minX + dest.getWidth(); int maxY = minY + dest.getHeight(); int numBands = dest.getNumBands(); for (int i = minX; i < maxX; i++) { for (int j = minY; j < maxY; j++) { for (int b = 0; b < numBands; b++) { int sample = dest.getSample(i, j, b); // No Data check if (noData.contains(sample)) { dest.setSample(i, j, b, destNoDataInt); } else { dest.setSample(i, j, b, sample); } } } } } }
@Test public void testGetMapOnByteNodataGrayScale() throws Exception { GetMapRequest request = new GetMapRequest(); CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84; ReferencedEnvelope bbox = new ReferencedEnvelope(new Envelope(145, 146, -43, -41), crs); request.setBbox(bbox); request.setHeight(768); request.setWidth(384); request.setSRS("urn:x-ogc:def:crs:EPSG:4326"); request.setFormat("image/png"); request.setTransparent(true); final WMSMapContent map = new WMSMapContent(request); map.setMapHeight(768); map.setMapWidth(384); map.setBgColor(BG_COLOR); map.setTransparent(true); map.getViewport().setBounds(bbox); addRasterToMap(map, TAZ_BYTE); map.getViewport().setBounds(bbox); RenderedImageMap imageMap = this.rasterMapProducer.produceMap(map); RenderedOp op = (RenderedOp) imageMap.getImage(); BufferedImage image = op.getAsBufferedImage(); imageMap.dispose(); // check that a pixel in nodata area is transparent assertEquals(0, image.getRaster().getSample(40, 400, 0)); assertEquals(0, image.getRaster().getSample(40, 400, 1)); }