Package net.sourceforge.jiu.geometry
Class Resample
java.lang.Object
net.sourceforge.jiu.ops.Operation
net.sourceforge.jiu.ops.ImageToImageOperation
net.sourceforge.jiu.geometry.Resample
Resizes grayscale and truecolor images using filters.
For other image types (including paletted or bilevel images), you might
want to use the Check out the homepage for the Delphi resample code, a demo application
to compare the different filtering algorithms is also provided:
http://www.melander.dk/delphi/resampler/index.html.
You will also find the original C code there.
The site seems to have gone for good.
ScaleReplication
class or convert the images to
grayscale (or RGB truecolor) first and then use this class.
Several algorithms for resampling are implemented, they differ in resulting image quality
and computational complexity.
Usage example
This will scaleimage
to 150 percent of its original size
in both directions, using the Lanczos3 filter type:
Resample resample = new Resample(); resample.setInputImage(image); resample.setSize(image.getWidth() * 3 / 2, image.getHeight() * 3 / 2); resample.setFilter(Resample.FILTER_TYPE_LANCZOS3); resample.process(); PixelImage scaledImage = resample.getOutputImage();
Known problems
- Scaling down certain images (with stripe or checkers patterns in them) will lead to moire effects in the resulting image. These effects can be somewhat reduced by scaling down in several step (e.g. first from 1600 x 1200 to 800 x 600, then from 800 x 600 to 400 x 300, and so on).
- Scaling down with filter type
FILTER_TYPE_BOX
can lead to errors in the scaled image. No fix known yet. Workaround: Use the classScaleReplication
.
Origin
This code is a port of Anders Melander's Object Pascal (Delphi) unit resample.pas to Java. The Delphi code is an adaptation (with some improvements) of Dale Schumacher's fzoom C code.Theory
The theoretical background for all implementations is Dale Schumacher's article General Filtered Image Rescaling in Graphics Gems III, editor David Kirk, Academic Press, pages 8-16, 1994.The Graphics Gems Repository can be found at http://www.acm.org/tog/GraphicsGems/. It also includes information on the books and how to order them.
- Author:
- Marco Schmidt
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) class
(package private) class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ResampleFilter
static final int
Constant for the B-Spline filter.static final int
Constant for the Bell filter.static final int
Constant for the Box filter (also known as Nearest Neighbor filter).static final int
Constant for the Hermite filter.static final int
Constant for the Lanczos3 filter.static final int
Constant for the Mitchell filter.static final int
Constant for the Triangle filter (also known as Linear filter or Bilinear filter).private Integer
private Integer
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static ResampleFilter
createFilter
(int filterType) Returns the filter to be used in this operation.static String[]
Returns the names of all predefined filters.static int
Returns the number of predefined filters.void
process()
This method does the actual work of the operation.private void
process
(IntegerImage in, IntegerImage out) This method does the actual work of rescaling an image.void
setFilter
(int filterType) Sets a new filter type, using the default sampling radius of that filter.void
setFilter
(int filterType, float samplingRadius) Sets a new filter type with a user-defined sampling radius.void
setFilter
(ResampleFilter newFilter) Set a new filter object to be used with this operation.void
setSize
(int width, int height) Set the pixel resolution of the output image.Methods inherited from class net.sourceforge.jiu.ops.ImageToImageOperation
canInputAndOutputBeEqual, ensureImagesHaveSameResolution, ensureInputImageIsAvailable, ensureOutputImageResolution, getInputImage, getOutputImage, setCanInputAndOutputBeEqual, setInputImage, setOutputImage
Methods inherited from class net.sourceforge.jiu.ops.Operation
addProgressListener, addProgressListeners, getAbort, removeProgressListener, setAbort, setProgress, setProgress
-
Field Details
-
FILTER_TYPE_BOX
public static final int FILTER_TYPE_BOXConstant for the Box filter (also known as Nearest Neighbor filter).- See Also:
-
FILTER_TYPE_TRIANGLE
public static final int FILTER_TYPE_TRIANGLEConstant for the Triangle filter (also known as Linear filter or Bilinear filter).- See Also:
-
FILTER_TYPE_HERMITE
public static final int FILTER_TYPE_HERMITEConstant for the Hermite filter.- See Also:
-
FILTER_TYPE_BELL
public static final int FILTER_TYPE_BELLConstant for the Bell filter.- See Also:
-
FILTER_TYPE_B_SPLINE
public static final int FILTER_TYPE_B_SPLINEConstant for the B-Spline filter.- See Also:
-
FILTER_TYPE_LANCZOS3
public static final int FILTER_TYPE_LANCZOS3Constant for the Lanczos3 filter.- See Also:
-
FILTER_TYPE_MITCHELL
public static final int FILTER_TYPE_MITCHELLConstant for the Mitchell filter.- See Also:
-
outWidth
-
outHeight
-
filter
-
-
Constructor Details
-
Resample
public Resample()
-
-
Method Details
-
createFilter
-
getFilter
Returns the filter to be used in this operation.- Returns:
- ResampleFilter object or
null
if none was defined yet
-
getFilterNames
Returns the names of all predefined filters. Each FILTER_TYPE_xyz constant can be used as an index into the array that is returned. Names are retrieved by creating an object of each predefined filter class and calling its getName method.- Returns:
- String array with filter names
-
getNumFilters
public static int getNumFilters()Returns the number of predefined filters.- Returns:
- number of filters
-
process
This method does the actual work of rescaling an image. -
process
Description copied from class:Operation
This method does the actual work of the operation. It must be called after all parameters have been given to the operation object.- Overrides:
process
in classOperation
- Throws:
MissingParameterException
- if any mandatory parameter was not given to the operationWrongParameterException
- if at least one of the input parameters was not initialized appropriately (values out of the valid interval, etc.)
-
setSize
public void setSize(int width, int height) Set the pixel resolution of the output image.- Parameters:
width
- the horizontal resolution of the output imageheight
- the vertical resolution of the output image
-
setFilter
Set a new filter object to be used with this operation.- Parameters:
newFilter
- a resample filter to be used for scaling
-
setFilter
public void setFilter(int filterType) Sets a new filter type, using the default sampling radius of that filter.- Parameters:
filterType
- the new filter type, one of the FILTER_TYPE_xyz constants of this class
-
setFilter
public void setFilter(int filterType, float samplingRadius) Sets a new filter type with a user-defined sampling radius.- Parameters:
filterType
- the new filter type, one of the FILTER_TYPE_xyz constants of this classsamplingRadius
- the sampling radius to be used with that filter, must be larger than 0.0f
-