/** * Tests the "Multiply" operation. * * @throws IOException */ @Test public void testMultiply() throws IOException { final GridCoverage2D shortCoverage = EXAMPLES.get(5); final GridCoverage2D floatCoverage = EXAMPLES.get(4); final GridCoverage2D result = doOp("Multiply", shortCoverage, floatCoverage); final RenderedImage image = result.getRenderedImage(); final RenderedImage extrema = ExtremaDescriptor.create(image, null, 1, 1, false, 1, null); double[][] minMax = (double[][]) extrema.getProperty("Extrema"); assertEquals(minMax[0][0], 0.0, DELTA); assertEquals(minMax[1][0], 6.5272192E7, DELTA); }
@Test public void testTranslatedImageTileGridROINoData() { // Testing color indexing with Nodata and ROI BufferedImage image_ = new BufferedImage(256, 256, BufferedImage.TYPE_BYTE_GRAY); Graphics g = image_.createGraphics(); g.setColor(Color.WHITE); g.fillRect(236, 236, 20, 20); g.setColor(new Color(80, 80, 80)); // A dark gray g.fillRect(216, 216, 20, 20); g.setColor(new Color(200, 200, 200)); // A light gray g.fillRect(216, 236, 20, 20); g.dispose(); TiledImage image = new TiledImage( 0, 0, 256, 256, 128, 128, image_.getColorModel().createCompatibleSampleModel(256, 256), image_.getColorModel()); image.set(image_); Range range = RangeFactory.create((byte) 255, (byte) 255); ROIShape roi = new ROIShape(new Rectangle(0, 0, 20, 20)); RenderedImage indexed = quantize(image, roi, range, 1); assertTrue(indexed.getColorModel() instanceof IndexColorModel); IndexColorModel icm = (IndexColorModel) indexed.getColorModel(); assertEquals(4, icm.getMapSize()); // Black background, white fill, // light gray fill, dark gray fill = // 4 colors // check image not black RenderedImage component = forceComponentColorModel(indexed); final double[][] coeff = new double[1][5]; Arrays.fill(coeff[0], 0, 4, 1.0 / 4); component = BandCombineDescriptor.create(component, coeff, null, null, destinationNoData, null); StatsType[] stats = new StatsType[] {StatsType.EXTREMA}; component = StatisticsDescriptor.create(component, 1, 1, null, null, false, new int[] {0}, stats, null); Statistics stat = ((Statistics[][]) component.getProperty(Statistics.STATS_PROPERTY))[0][0]; double[] result = (double[]) stat.getResult(); final double minimum = result[0]; final double maximum = result[1]; assertFalse(Math.abs(maximum - minimum) < TOLERANCE); }
public static float getImageMeanValue(PlanarImage image) { // Set up the parameter block for the source image and // the three parameters. ParameterBlock pb = new ParameterBlock(); pb.addSource(image); // The source image pb.add(null); // null ROI means whole image pb.add(1); // check every pixel horizontally pb.add(1); // check every pixel vertically // Perform the mean operation on the source image. RenderedImage meanImage = JAI.create("mean", pb, null); // Retrieve and report the mean pixel value. double[] mean = (double[]) meanImage.getProperty("mean"); return (float) mean[0]; }
/** Returns the properties from the source image. */ protected synchronized Hashtable getProperties() { // Selection of all the image properties String[] propertyNames = getPropertyNames(); // Selection of the source image RenderedImage sourceImage = getSourceImage(0); // Creation of a Map containing all the source image properties Hashtable<String, Object> properties = new Hashtable<String, Object>(); // Cycle on all the properties if (propertyNames != null) { for (String property : propertyNames) { // Addition of the selected property properties.put(property, sourceImage.getProperty(property)); } return properties; } else { return null; } }
public static java.awt.Image load( String codecName, java.io.InputStream inputStream, edu.cmu.cs.stage3.image.codec.ImageDecodeParam imageDecodeParam) throws java.io.IOException { java.io.BufferedInputStream bufferedInputStream; if (inputStream instanceof java.io.BufferedInputStream) { bufferedInputStream = (java.io.BufferedInputStream) inputStream; } else { bufferedInputStream = new java.io.BufferedInputStream(inputStream); } edu.cmu.cs.stage3.image.codec.ImageDecoder imageDecoder = edu.cmu.cs.stage3.image.codec.ImageCodec.createImageDecoder( codecName, bufferedInputStream, imageDecodeParam); java.awt.image.RenderedImage renderedImage = imageDecoder.decodeAsRenderedImage(); if (renderedImage instanceof java.awt.Image) { return (java.awt.Image) renderedImage; } else { java.awt.image.Raster raster = renderedImage.getData(); java.awt.image.ColorModel colorModel = renderedImage.getColorModel(); java.util.Hashtable properties = null; String[] propertyNames = renderedImage.getPropertyNames(); if (propertyNames != null) { properties = new java.util.Hashtable(); for (String propertyName : propertyNames) { properties.put(propertyName, renderedImage.getProperty(propertyName)); } } java.awt.image.WritableRaster writableRaster; if (raster instanceof java.awt.image.WritableRaster) { writableRaster = (java.awt.image.WritableRaster) raster; } else { writableRaster = raster.createCompatibleWritableRaster(); } java.awt.image.BufferedImage bufferedImage = new java.awt.image.BufferedImage( renderedImage.getColorModel(), writableRaster, colorModel.isAlphaPremultiplied(), properties); return bufferedImage; } }
/** * Converts a rendered image to a {@code BufferedImage}. This utility method has come from a forum * post by Jim Moore at: * * <p><a href="http://www.jguru.com/faq/view.jsp?EID=114602"> * http://www.jguru.com/faq/view.jsp?EID=114602</a> * * @param img the rendered image. * @return A buffered image. */ private static BufferedImage convertRenderedImage(RenderedImage img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } ColorModel cm = img.getColorModel(); int width = img.getWidth(); int height = img.getHeight(); WritableRaster raster = cm.createCompatibleWritableRaster(width, height); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); Hashtable properties = new Hashtable(); String[] keys = img.getPropertyNames(); if (keys != null) { for (int i = 0; i < keys.length; i++) { properties.put(keys[i], img.getProperty(keys[i])); } } BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties); img.copyData(raster); return result; }
/** @see Graphics2D#drawRenderedImage(RenderedImage, AffineTransform) */ public void drawRenderedImage(RenderedImage img, AffineTransform xform) { BufferedImage image = null; if (img instanceof BufferedImage) { image = (BufferedImage) img; } else { ColorModel cm = img.getColorModel(); int width = img.getWidth(); int height = img.getHeight(); WritableRaster raster = cm.createCompatibleWritableRaster(width, height); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); Hashtable properties = new Hashtable(); String[] keys = img.getPropertyNames(); if (keys != null) { for (int i = 0; i < keys.length; i++) { properties.put(keys[i], img.getProperty(keys[i])); } } BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties); img.copyData(raster); image = result; } drawImage(image, xform, null); }
/** * Returns the specified property. * * @param name Property name. * @param opNode Operation node. */ public Object getProperty(String name, Object opNode) { validate(name, opNode); if (opNode instanceof RenderedOp && name.equalsIgnoreCase("roi")) { RenderedOp op = (RenderedOp) opNode; ParameterBlock pb = op.getParameterBlock(); // Retrieve the rendered source image and its ROI. RenderedImage src = pb.getRenderedSource(0); Object property = src.getProperty("ROI"); if (property == null || property.equals(java.awt.Image.UndefinedProperty) || !(property instanceof ROI)) { return java.awt.Image.UndefinedProperty; } // Return undefined also if source ROI is empty. ROI srcROI = (ROI) property; if (srcROI.getBounds().isEmpty()) { return java.awt.Image.UndefinedProperty; } // Retrieve the Interpolation object. Interpolation interp = (Interpolation) pb.getObjectParameter(1); // Determine the effective source bounds. Rectangle srcBounds = null; PlanarImage dst = op.getRendering(); if (dst instanceof GeometricOpImage && ((GeometricOpImage) dst).getBorderExtender() == null) { srcBounds = new Rectangle( src.getMinX() + interp.getLeftPadding(), src.getMinY() + interp.getTopPadding(), src.getWidth() - interp.getWidth() + 1, src.getHeight() - interp.getHeight() + 1); } else { srcBounds = new Rectangle(src.getMinX(), src.getMinY(), src.getWidth(), src.getHeight()); } // If necessary, clip the ROI to the effective source bounds. if (!srcBounds.contains(srcROI.getBounds())) { srcROI = srcROI.intersect(new ROIShape(srcBounds)); } // Retrieve the Warp object. Warp warp = (Warp) pb.getObjectParameter(0); // Setting constant image to be warped as a ROI Rectangle dstBounds = op.getBounds(); // Setting layout of the constant image ImageLayout2 layout = new ImageLayout2(); int minx = (int) srcBounds.getMinX(); int miny = (int) srcBounds.getMinY(); int w = (int) srcBounds.getWidth(); int h = (int) srcBounds.getHeight(); layout.setMinX(minx); layout.setMinY(miny); layout.setWidth(w); layout.setHeight(h); RenderingHints hints = op.getRenderingHints(); hints.add(new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout)); final PlanarImage constantImage = ConstantDescriptor.create(new Float(w), new Float(h), new Byte[] {(byte) 255}, hints); PlanarImage roiImage = null; // Make sure to specify tileCache, tileScheduler, tileRecyclier, by cloning hints. RenderingHints warpingHints = op.getRenderingHints(); warpingHints.remove(JAI.KEY_IMAGE_LAYOUT); // Creating warped roi by the same way (Warp, Interpolation, source ROI) we warped the // input image. final ParameterBlock paramBlk = new ParameterBlock(); paramBlk.addSource(constantImage); paramBlk.add(warp); paramBlk.add(interp); paramBlk.add(null); paramBlk.add(srcROI); // force in the image layout, this way we get exactly the same // as the affine we're eliminating Hints localHints = new Hints(op.getRenderingHints()); localHints.remove(JAI.KEY_IMAGE_LAYOUT); ImageLayout il = new ImageLayout(); il.setMinX(dstBounds.x); il.setMinY(dstBounds.y); il.setWidth(dstBounds.width); il.setHeight(dstBounds.height); localHints.put(JAI.KEY_IMAGE_LAYOUT, il); roiImage = JAI.create("Warp", paramBlk, localHints); ROI dstROI = new ROI(roiImage, 1); // If necessary, clip the warped ROI to the destination bounds. if (!dstBounds.contains(dstROI.getBounds())) { dstROI = dstROI.intersect(new ROIShape(dstBounds)); } // Return the warped and possibly clipped ROI. return dstROI; } return java.awt.Image.UndefinedProperty; }