Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Adds selectivity to the JAI convolve operation. An ROI can
be used to control which source image pixels are convolved and/or
which neighbourhood pixels are included in the convolution.
/*
* Copyright (c) 2009-2011, Michael Bedward. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jaitools.media.jai.maskedconvolve;
import java.awt.Rectangle;
import java.awt.image.DataBuffer;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.WritableRaster;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.media.jai.AreaOpImage;
import javax.media.jai.BorderExtender;
import javax.media.jai.ImageLayout;
import javax.media.jai.KernelJAI;
import javax.media.jai.ROI;
import javax.media.jai.RasterAccessor;
import javax.media.jai.RasterFormatTag;
import org.jaitools.CollectionFactory;
import org.jaitools.numeric.CompareOp;
import org.jaitools.numeric.Range;
/**
* An operator to perform masked convolution on a source image.
* @see MaskedConvolveDescriptor Description of the algorithm and example
*
* @author Michael Bedward
* @since 1.0
* @version $Id$
*/
public class MaskedConvolveOpImage extends AreaOpImage {
/* Source image variables */
private int[] srcBandOffsets;
private int srcPixelStride;
private int srcScanlineStride;
/* Destination image variables */
private int destWidth;
private int destHeight;
private int destBands;
private int[] dstBandOffsets;
private int dstPixelStride;
private int dstScanlineStride;
/* Kernel variables. */
private final float[] kernelData;
private final int kernelW, kernelH;
private final int kernelKeyX, kernelKeyY;
/* Records which kernel cells have non-zero values */
private final boolean[] kernelActive;
/**
* The tolerance used when marking kernel cells as active
* (ie. having a non-zero value that contributes to the
* convolution result) or inactive. Cells with an absolute
* value less than this tolerance will be treated as
* inactive (zero).
*/
public final static float KERNEL_TOL = 1.0e-6F;
/* ROI and options */
private final ROI roi;
private final boolean maskSrc;
private final boolean maskDest;
/* Flags whether NO_DATA values are defined */
private final boolean noDataDefined;
/* Whether any NO_DATA values in a pixel neighbourhood prevent convolution */
private final boolean strictNodata;
/* List of Numbers to treat as NO_DATA */
private final List noDataNumbers;
/* List of Ranges to treat as NO_DATA */
private final List> noDataRanges;
/*
* The value to write to the destination when there is no
* convolution result
*/
private final Number nilValueNumber;
/*
* The minimum number of non-zero kernel cells that must overlap
* unmasked source image cells for a convolution result to be written
* to the destination image
*/
private final int minKernelCells;
/**
* Creates a new instance.
*
* @param source the source image to convolve
*
* @param extender an optional {@code BorderExtender}, or {@code null}
*
* @param config configurable attributes of the image (see {@link AreaOpImage})
*
* @param layout an optional {@code ImageLayout} specifying destination image
* parameters, or {@code null}
*
* @param kernel the convolution kernel
*
* @param roi the ROI used to control masking; must contain the source image bounds
*
* @param maskSrc if true, exclude masked pixels ({@code roi.contains == false}) from
* convolution kernel calculation
*
* @param maskDest if true, do not place kernel over masked pixels (dest will be 0)
*
* @param nilValue value to write to the destination image for pixels where
* there is no convolution result
*
* @param minCells the minimum number of non-zero kernel cells that be positioned over
* unmasked source image cells for convolution to be performed for the target cell
*
* @param noDataValues option {@code Collection} of values and/or {@code Ranges} to
* treat as NO_DATA
*
* @param strictNodata if {@code true} no convolution is performed for pixels with any
* NO_DATA values in their neighbourhood
*
* @throws IllegalArgumentException if the roi's bounds do not contain the entire
* source image
*/
public MaskedConvolveOpImage(RenderedImage source,
BorderExtender extender,
Map config,
ImageLayout layout,
KernelJAI kernel,
ROI roi,
Boolean maskSrc,
Boolean maskDest,
Number nilValue,
int minCells,
Collection