/** Tests that flipping axis on a coverage whose origin is not (0,0) works as expected */ @Test public void testFlipTranslated() throws Exception { // build a translated image SampleModel sm = RasterFactory.createPixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 256, 256, 3); ColorModel cm = PlanarImage.createColorModel(sm); TiledImage ti = new TiledImage(-10, -10, 5, 5, 0, 0, sm, cm); Graphics2D g = ti.createGraphics(); g.setColor(Color.GREEN); g.fillRect(-10, -10, 5, 5); g.dispose(); // build a coverage around it CoordinateReferenceSystem wgs84LatLon = CRS.decode("EPSG:4326"); final GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null); GridCoverage2D coverage = factory.create("translated", ti, new Envelope2D(wgs84LatLon, 3, 5, 6, 8)); // verify we're good int[] pixel = new int[3]; coverage.evaluate((DirectPosition) new DirectPosition2D(4, 6), pixel); assertEquals(0, pixel[0]); assertEquals(255, pixel[1]); assertEquals(0, pixel[2]); // now reproject flipping the axis CoordinateReferenceSystem wgs84LonLat = CRS.decode("EPSG:4326", true); GridGeometry gg = new GridGeometry2D( new GridEnvelope2D(-10, -10, 5, 5), (Envelope) new Envelope2D(wgs84LonLat, 5, 3, 8, 6)); GridCoverage2D flipped = (GridCoverage2D) Operations.DEFAULT.resample( coverage, wgs84LonLat, gg, Interpolation.getInstance(Interpolation.INTERP_NEAREST)); // before the fix the pixel would have been black flipped.evaluate((DirectPosition) new DirectPosition2D(6, 4), pixel); assertEquals(0, pixel[0]); assertEquals(255, pixel[1]); assertEquals(0, pixel[2]); }
protected RenderedImage createRenderedImage( RenderedImage renderedImage, int height, int width, DataBuffer dataBuffer) { SampleModel sampleModel = RasterFactory.createPixelInterleavedSampleModel( DataBuffer.TYPE_BYTE, width, height, _NUM_OF_BANDS); ColorModel colorModel = PlanarImage.createColorModel(sampleModel); TiledImage tiledImage = new TiledImage(0, 0, width, height, 0, 0, sampleModel, colorModel); Raster raster = RasterFactory.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0)); tiledImage.setData(raster); /*javax.media.jai.JAI.create( "filestore", tiledImage, "test.png", "PNG"); printImage(renderedImage); printImage(tiledImage);}*/ return tiledImage; }
/* * Derive the image layout (tile grid, image bounds, ColorModel, and * SampleModel) by querying the IIP server. */ private static ImageLayout layoutHelper(String URLSpec, int level, int subImage) { // Create an ImageLayout by construction or cloning. ImageLayout il = new ImageLayout(); // Set the tile offsets to (0,0). il.setTileGridXOffset(0); il.setTileGridYOffset(0); // Set the tile dimensions. il.setTileWidth(TILE_SIZE); il.setTileHeight(TILE_SIZE); // Set the image origin to (0,0). il.setMinX(0); il.setMinY(0); // Retrieve the number of resolutions available and the maximum // width and height (the dimensions of resolution numRes - 1). int maxWidth = -1; int maxHeight = -1; int numRes = -1; int resolution = -1; String[] cmd = new String[] {"OBJ=Max-size", "OBJ=Resolution-number"}; InputStream stream = postCommands(URLSpec, cmd); String label = null; while ((label = getLabel(stream)) != null) { if (label.equals("max-size")) { String data = getDataAsString(stream, false); int[] wh = stringToIntArray(data); maxWidth = wh[0]; maxHeight = wh[1]; } else if (label.equals("resolution-number")) { String data = getDataAsString(stream, false); numRes = Integer.valueOf(data).intValue(); if (level < 0) { resolution = 0; } else if (level >= numRes) { resolution = numRes - 1; } else { resolution = level; } } else { checkError(label, stream, true); } } closeStream(stream); // Derive the width and height for this resolution level. int w = maxWidth; int h = maxHeight; for (int i = numRes - 1; i > resolution; i--) { w = (w + 1) / 2; h = (h + 1) / 2; } il.setWidth(w); il.setHeight(h); // Determine image opacity attributes. boolean hasAlpha = false; boolean isAlphaPremultiplied = false; cmd = new String[] {"OBJ=Colorspace," + resolution + "," + subImage}; stream = postCommands(URLSpec, cmd); int colorSpaceIndex = 0; int numBands = 0; while ((label = getLabel(stream)) != null) { if (label.startsWith("colorspace")) { int[] ia = stringToIntArray(getDataAsString(stream, false)); numBands = ia[3]; switch (ia[2]) { case CS_MONOCHROME: colorSpaceIndex = ColorSpace.CS_GRAY; break; case CS_PHOTOYCC: colorSpaceIndex = ColorSpace.CS_PYCC; break; case CS_NIFRGB: colorSpaceIndex = ColorSpace.CS_sRGB; break; default: colorSpaceIndex = numBands < 3 ? ColorSpace.CS_GRAY : ColorSpace.CS_sRGB; } for (int j = 1; j <= numBands; j++) { if (ia[3 + j] == CS_PLANE_ALPHA) { hasAlpha = true; } } isAlphaPremultiplied = ia[1] == 1; } else { checkError(label, stream, true); } } closeStream(stream); // Set the ColorModel. ColorSpace cs = ColorSpace.getInstance(colorSpaceIndex); int dtSize = DataBuffer.getDataTypeSize(DataBuffer.TYPE_BYTE); int[] bits = new int[numBands]; for (int i = 0; i < numBands; i++) { bits[i] = dtSize; } int transparency = hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE; ColorModel cm = new ComponentColorModel( cs, bits, hasAlpha, isAlphaPremultiplied, transparency, DataBuffer.TYPE_BYTE); il.setColorModel(cm); // Set the SampleModel. int[] bandOffsets = new int[numBands]; for (int i = 0; i < numBands; i++) { bandOffsets[i] = i; } il.setSampleModel( RasterFactory.createPixelInterleavedSampleModel( DataBuffer.TYPE_BYTE, TILE_SIZE, TILE_SIZE, numBands, numBands * TILE_SIZE, bandOffsets)); return il; }