@Test public void testDisposeWritableRenderedImage() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_BYTE_GRAY); WritableRenderedImageAdapter wria = new WritableRenderedImageAdapter(bi); ImageUtilities.disposeSinglePlanarImage(wria); Class<? extends Object> theClass = wria.getClass(); Field f = theClass.getSuperclass().getDeclaredField("theImage"); f.setAccessible(true); Object image = f.get(wria); assertNull(image); f = theClass.getDeclaredField("theWritableImage"); f.setAccessible(true); image = f.get(wria); assertNull(image); }
public static void disposeSinglePlanarImage(PlanarImage planarImage) { Object roi = null; try { roi = planarImage.getProperty("ROI"); } catch (NullPointerException npe) { } if ((roi != null) && ((ROI.class.equals(roi.getClass()) || (roi instanceof RenderedImage)))) { if (roi instanceof ROI) { ROI roiImage = (ROI) roi; Rectangle bounds = roiImage.getBounds(); if (!(bounds.isEmpty())) { PlanarImage image = roiImage.getAsImage(); if (image != null) { image.dispose(); } } } else { disposeImage((RenderedImage) roi); } } try { if (planarImage instanceof WritableRenderedImageAdapter) { cleanField(planarImage, "theWritableImage"); cleanField(planarImage, "theImage", true); } else if (planarImage instanceof RenderedImageAdapter) { cleanField(planarImage, "theImage"); } } catch (NoSuchFieldException | IllegalAccessException e) { LOGGER.log( Level.FINE, "Failed to clear rendered image adapters field to null. " + "Not a problem per se, but if the finalizer thread is not fast enough, this might result in a OOM", e); } planarImage.dispose(); }
ImageUtilities { public static void disposeSinglePlanarImage(PlanarImage planarImage) { Object roi = null; try { roi = planarImage.getProperty("ROI"); } catch (NullPointerException npe) { } if ((roi != null) && ((ROI.class.equals(roi.getClass()) || (roi instanceof RenderedImage)))) { if (roi instanceof ROI) { ROI roiImage = (ROI) roi; Rectangle bounds = roiImage.getBounds(); if (!(bounds.isEmpty())) { PlanarImage image = roiImage.getAsImage(); if (image != null) { image.dispose(); } } } else { disposeImage((RenderedImage) roi); } } try { if (planarImage instanceof WritableRenderedImageAdapter) { cleanField(planarImage, "theWritableImage"); cleanField(planarImage, "theImage", true); } else if (planarImage instanceof RenderedImageAdapter) { cleanField(planarImage, "theImage"); } } catch (NoSuchFieldException | IllegalAccessException e) { LOGGER.log( Level.FINE, "Failed to clear rendered image adapters field to null. " + "Not a problem per se, but if the finalizer thread is not fast enough, this might result in a OOM", e); } planarImage.dispose(); } }
ImageUtilities { public static void disposeSinglePlanarImage(PlanarImage planarImage) { Object roi = null; try { roi = planarImage.getProperty("ROI"); } catch (NullPointerException npe) { } if ((roi != null) && ((ROI.class.equals(roi.getClass()) || (roi instanceof RenderedImage)))) { if (roi instanceof ROI) { ROI roiImage = (ROI) roi; Rectangle bounds = roiImage.getBounds(); if (!(bounds.isEmpty())) { PlanarImage image = roiImage.getAsImage(); if (image != null) { image.dispose(); } } } else { disposeImage((RenderedImage) roi); } } try { if (planarImage instanceof WritableRenderedImageAdapter) { cleanField(planarImage, "theWritableImage"); cleanField(planarImage, "theImage", true); } else if (planarImage instanceof RenderedImageAdapter) { cleanField(planarImage, "theImage"); } } catch (NoSuchFieldException | IllegalAccessException e) { LOGGER.log( Level.FINE, "Failed to clear rendered image adapters field to null. " + "Not a problem per se, but if the finalizer thread is not fast enough, this might result in a OOM", e); } planarImage.dispose(); } private  ImageUtilities(); }
ImageUtilities { public static void disposeSinglePlanarImage(PlanarImage planarImage) { Object roi = null; try { roi = planarImage.getProperty("ROI"); } catch (NullPointerException npe) { } if ((roi != null) && ((ROI.class.equals(roi.getClass()) || (roi instanceof RenderedImage)))) { if (roi instanceof ROI) { ROI roiImage = (ROI) roi; Rectangle bounds = roiImage.getBounds(); if (!(bounds.isEmpty())) { PlanarImage image = roiImage.getAsImage(); if (image != null) { image.dispose(); } } } else { disposeImage((RenderedImage) roi); } } try { if (planarImage instanceof WritableRenderedImageAdapter) { cleanField(planarImage, "theWritableImage"); cleanField(planarImage, "theImage", true); } else if (planarImage instanceof RenderedImageAdapter) { cleanField(planarImage, "theImage"); } } catch (NoSuchFieldException | IllegalAccessException e) { LOGGER.log( Level.FINE, "Failed to clear rendered image adapters field to null. " + "Not a problem per se, but if the finalizer thread is not fast enough, this might result in a OOM", e); } planarImage.dispose(); } private  ImageUtilities(); static ImageLayout getImageLayout(final RenderedImage image); static RenderingHints getRenderingHints(final RenderedImage image); static Dimension toTileSize(final Dimension size); static ImageLayout createIntersection(
            final ImageLayout layout, final List<RenderedImage> sources); static Interpolation toInterpolation(final Object type); static String getInterpolationName(Interpolation interp); static RenderedOp tileImage(final RenderedOp image); static void fill(final WritableRenderedImage image, final Number value); static boolean isMediaLibAvailable(); static void disposePlanarImageChain(PlanarImage pi); static RenderedImage maskColor(final Color transparentColor, final RenderedImage image); static ImageReadParam cloneImageReadParam(ImageReadParam param); static Rectangle2D layoutHelper(
            RenderedImage source,
            float scaleX,
            float scaleY,
            float transX,
            float transY,
            Interpolation interp); static ReferencedEnvelope getReferencedEnvelopeFromGeographicBoundingBox(
            final GeographicBoundingBox geographicBBox); static ReferencedEnvelope getWGS84ReferencedEnvelope(
            final GeneralEnvelope coverageEnvelope); static Rectangle getDimension(
            final int imageIndex, final ImageInputStream inStream, final ImageReader reader); static final boolean checkEmptySourceRegion(
            final ImageReadParam readParameters, final Rectangle dimensions); static Number[] getBackgroundValues(
            final SampleModel sampleModel, final double[] backgroundValues); static void disposeImage(RenderedImage inputImage); static void disposeSinglePlanarImage(PlanarImage planarImage); static String getDatabufferTypeName(int dataType); static RenderedImage applyRescaling(
            Double[] scales, Double[] offsets, RenderedImage image, Hints hints); }
ImageUtilities { public static void disposeSinglePlanarImage(PlanarImage planarImage) { Object roi = null; try { roi = planarImage.getProperty("ROI"); } catch (NullPointerException npe) { } if ((roi != null) && ((ROI.class.equals(roi.getClass()) || (roi instanceof RenderedImage)))) { if (roi instanceof ROI) { ROI roiImage = (ROI) roi; Rectangle bounds = roiImage.getBounds(); if (!(bounds.isEmpty())) { PlanarImage image = roiImage.getAsImage(); if (image != null) { image.dispose(); } } } else { disposeImage((RenderedImage) roi); } } try { if (planarImage instanceof WritableRenderedImageAdapter) { cleanField(planarImage, "theWritableImage"); cleanField(planarImage, "theImage", true); } else if (planarImage instanceof RenderedImageAdapter) { cleanField(planarImage, "theImage"); } } catch (NoSuchFieldException | IllegalAccessException e) { LOGGER.log( Level.FINE, "Failed to clear rendered image adapters field to null. " + "Not a problem per se, but if the finalizer thread is not fast enough, this might result in a OOM", e); } planarImage.dispose(); } private  ImageUtilities(); static ImageLayout getImageLayout(final RenderedImage image); static RenderingHints getRenderingHints(final RenderedImage image); static Dimension toTileSize(final Dimension size); static ImageLayout createIntersection(
            final ImageLayout layout, final List<RenderedImage> sources); static Interpolation toInterpolation(final Object type); static String getInterpolationName(Interpolation interp); static RenderedOp tileImage(final RenderedOp image); static void fill(final WritableRenderedImage image, final Number value); static boolean isMediaLibAvailable(); static void disposePlanarImageChain(PlanarImage pi); static RenderedImage maskColor(final Color transparentColor, final RenderedImage image); static ImageReadParam cloneImageReadParam(ImageReadParam param); static Rectangle2D layoutHelper(
            RenderedImage source,
            float scaleX,
            float scaleY,
            float transX,
            float transY,
            Interpolation interp); static ReferencedEnvelope getReferencedEnvelopeFromGeographicBoundingBox(
            final GeographicBoundingBox geographicBBox); static ReferencedEnvelope getWGS84ReferencedEnvelope(
            final GeneralEnvelope coverageEnvelope); static Rectangle getDimension(
            final int imageIndex, final ImageInputStream inStream, final ImageReader reader); static final boolean checkEmptySourceRegion(
            final ImageReadParam readParameters, final Rectangle dimensions); static Number[] getBackgroundValues(
            final SampleModel sampleModel, final double[] backgroundValues); static void disposeImage(RenderedImage inputImage); static void disposeSinglePlanarImage(PlanarImage planarImage); static String getDatabufferTypeName(int dataType); static RenderedImage applyRescaling(
            Double[] scales, Double[] offsets, RenderedImage image, Hints hints); static final double[][] RGB_TO_GRAY_MATRIX; static final double[][] RGBA_TO_GRAY_MATRIX; static final RenderingHints DONT_REPLACE_INDEX_COLOR_MODEL; static final RenderingHints REPLACE_INDEX_COLOR_MODEL; static final RenderingHints NN_INTERPOLATION_HINT; static final RenderingHints NOCACHE_HINT; static final RenderingHints EXTEND_BORDER_BY_COPYING; static final RenderingHints EXTEND_BORDER_BY_REFLECT; static final String[] INTERPOLATION_NAMES; static final int[] INTERPOLATION_TYPES; static final BorderExtender DEFAULT_BORDER_EXTENDER; static final RenderingHints BORDER_EXTENDER_HINTS; static final String DIRECT_KAKADU_PLUGIN; static final float RATIONAL_TOLERANCE; }