|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.thebuzzmedia.imgscalr.Scalr
public class Scalr
Class used to implement performant, good-quality and intelligent image scaling algorithms in native Java 2D. This class utilizes the Java2D "best practices" for image-scaling, ensuring that images are hardware accelerated at all times if provided by the platform and host-VM.
Hardware acceleration also includes execution of optional caller-suppliedBufferedImageOp
s that are applied to the resultant images before
returning them as well as any optional rotations specified.
resize
methods
provided by this class.
In order to maintain the proportionality of the original images, this class
implements the following behavior:
targetWidth
as the primary dimension and re-calculate the
targetHeight
regardless of what is passed in.targetHeight
as the
primary dimension and re-calculate the targetWidth
regardless of
what is passed in.Scalr.Mode
value of Scalr.Mode.FIT_TO_WIDTH
or
Scalr.Mode.FIT_TO_HEIGHT
is passed in to the resize
method,
the image's orientation is ignored and the scaled image is fit to the
dimension the user specified with the Scalr.Mode
.Scalr.Method
).
This class also implements the incremental scaling algorithm presented by
Chris Campbell in his Perils of
Image.getScaledInstance() article in order to give the best-looking
results to images scaled down below roughly 800px in size where using a
single scaling operation (even with
RenderingHints.VALUE_INTERPOLATION_BICUBIC
interpolation) would
produce a much worse-looking result.
Only when scaling using the Scalr.Method.AUTOMATIC
method will this class
look at the size of the image before selecting an approach to scaling the
image. If Scalr.Method.QUALITY
is specified, the best-looking algorithm
possible is always used.
Minor modifications are made to Campbell's original implementation in the
form of:
RenderingHints.VALUE_INTERPOLATION_BICUBIC
interpolation is always
used. This was done after A/B comparison testing with large images
down-scaled to thumbnail sizes showed noticeable "blurring" when BILINEAR
interpolation was used. Given that Campbell's algorithm is only used in
QUALITY mode when down-scaling, it was determined that the user's expectation
of a much less blurry picture would require that BICUBIC be the default
interpolation in order to meet the QUALITY expectation.Image.flush()
on the interim temporary BufferedImage
instances created by the algorithm in an attempt to ensure a more complete GC
cycle by the VM when cleaning up the temporary instances (this is in addition
to disposing of the temporary Graphics2D
references as well).Image.flush()
on any of the source images passed in by calling code; it is up to
the original caller to dispose of their source images when they are no longer
needed so the VM can most efficiently GC them.
BufferedImage.TYPE_*
variables, unfortunately not all image
types are supported equally in Java2D. Some more obscure image types either
have poor or no support, leading to severely degraded quality when an attempt
is made by imgscalr to create a scaled instance of the same type as
the source image.
To avoid imgscalr generating significantly worse-looking results than
alternative scaling approaches (e.g.
Image.getScaledInstance(int, int, int)
), all resultant images
generated by imgscalr are one of two types:
depending on if the source image utilizes transparency or not.
This is a recommended approach by the Java2D team for dealing with poorly (or
non) supported image types. More can be read about this issue here.
log(String, Object...)
method. At this time logging is done directly
to System.out
via the printf
method. This allows
the logging to be light weight and easy to capture while adding no
dependencies to the library.
Implementation of logging in this class is as efficient as possible; avoiding
any calls to the logger or passing of arguments if logging is not enabled to
avoid the (hidden) cost of constructing the Object[] argument for the varargs
method call.
IndexColorModel
is sub-par, both in accurate color-selection and in
maintaining transparency when moving to an image of type
BufferedImage.TYPE_INT_ARGB
; because of this issue when a GIF image
is processed by imgscalr and the result saved as a GIF file, it is possible
to lose the alpha channel of a transparent image or in the case of applying
an optional BufferedImageOp
, lose the entire picture all together in
the result (long standing JDK bugs are filed for these).
imgscalr currently does nothing to work around this manually because it is a
defect in the native platform code itself. Fortunately it looks like the
issues are half-fixed in Java 7 and any manual workarounds we could attempt
internally are relatively expensive, in the form of hand-creating and setting
RGB values pixel-by-pixel with a custom ColorModel
in the scaled
image. This would lead to a very measurable negative impact on performance
without the caller understanding why.
Workaround: A workaround to this issue with all version of
Java is to simply save a GIF as a PNG; no change to your code needs to be
made except when the image is saved out, e.g. using ImageIO
. When a
file type of "PNG" is used, both the transparency and high color quality will
be maintained as the PNG code path in Java2D is superior to the GIF
implementation.
If the issue with optional BufferedImageOp
s destroying GIF image
content is ever fixed in the platform, saving out resulting images as GIFs
should suddenly start working.
More can be read about the issue here and here.
Nested Class Summary | |
---|---|
static class |
Scalr.Method
Used to define the different scaling hints that the algorithm can use. |
static class |
Scalr.Mode
Used to define the different modes of resizing that the algorithm can use. |
static class |
Scalr.Rotation
Used to define the different types of rotations that can be applied to an image during a resize operation. |
Field Summary | |
---|---|
static boolean |
DEBUG
Flag used to indicate if debugging output has been enabled by setting the "imgscalr.debug" system property to true . |
static String |
LOG_PREFIX
Prefix to every log message this library logs. |
static ConvolveOp |
OP_ANTIALIAS
A ConvolveOp using a very light "blur" kernel that acts like an
anti-aliasing filter (softens the image a bit) when applied to an image. |
static int |
THRESHOLD_BALANCED_SPEED
Threshold (in pixels) at which point the scaling operation using the Scalr.Method.AUTOMATIC method will decide if a Scalr.Method.BALANCED
method will be used (if smaller than or equal to threshold) or a
Scalr.Method.SPEED method will be used (if larger than threshold). |
static int |
THRESHOLD_QUALITY_BALANCED
Threshold (in pixels) at which point the scaling operation using the Scalr.Method.AUTOMATIC method will decide if a Scalr.Method.QUALITY
method will be used (if smaller than or equal to threshold) or a
Scalr.Method.BALANCED method will be used (if larger than threshold). |
Constructor Summary | |
---|---|
Scalr()
|
Method Summary | |
---|---|
protected static Scalr.Method |
determineScalingMethod(int targetWidth,
int targetHeight,
float ratio)
Used to determine the scaling Scalr.Method that is best suited for
scaling the image to the targeted dimensions. |
protected static void |
log(String message,
Object... params)
Helper method used to ensure a message is loggable before it is logged and then pre-pend a universal prefix to all log messages generated by this library to make the log entries easy to parse visually or programmatically. |
static BufferedImage |
resize(BufferedImage src,
int targetSize,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to a width and height no bigger than targetSize and apply the given
BufferedImageOp s (if any) to the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to the target width and height and apply the given BufferedImageOp s (if any) to
the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Method scalingMethod,
int targetSize,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to a width and height no bigger than targetSize using the given scaling
method and apply the given BufferedImageOp s (if any) to the
result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Method scalingMethod,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to the target width and height using the given scaling method and apply the given BufferedImageOp s (if any) to the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
int targetSize,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to a width and height no bigger than targetSize (or fitting the image to
the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode
specified) using the given scaling method and apply the given
BufferedImageOp s (if any) to the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to the target width and height (or fitting the image to the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode specified) using the given
scaling method and apply the given BufferedImageOp s (if any) to
the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
Scalr.Rotation rotation,
int targetSize,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to a width and height no bigger than targetSize (or fitting the image to
the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode
specified) using the given scaling method, apply the given
BufferedImageOp s (if any) and apply the given rotation to the
result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Mode resizeMode,
Scalr.Rotation rotation,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to the target width and height (or fitting the image to the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode specified) using the given
scaling method, apply the given BufferedImageOp s (if any) and
apply the given rotation to the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Rotation rotation,
int targetSize,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to a width and height no bigger than targetSize using the given scaling
method, apply the given BufferedImageOp s (if any) and then apply
the given rotation to the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Method scalingMethod,
Scalr.Rotation rotation,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to the target width and height using the given scaling method, apply the given BufferedImageOp s (if any) and apply the given rotation to the
result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Mode resizeMode,
int targetSize,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to a width and height no bigger than targetSize (or fitting the image to
the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode
specified) and then apply the given BufferedImageOp s (if any) to
the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Mode resizeMode,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to the target width and height (or fitting the image to the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode specified) and then apply the
given BufferedImageOp s (if any) to the result before returning
it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Mode resizeMode,
Scalr.Rotation rotation,
int targetSize,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to a width and height no bigger than targetSize (or fitting the image to
the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode
specified), apply the given BufferedImageOp s (if any) and then
apply the given rotation to the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Mode resizeMode,
Scalr.Rotation rotation,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to the target width and height (or fitting the image to the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode specified), apply the given
BufferedImageOp s (if any) and then apply the given rotation to
the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Rotation rotation,
int targetSize,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to a width and height no bigger than targetSize , apply the given
BufferedImageOp s (if any) and then apply the given rotation to
the result before returning it. |
static BufferedImage |
resize(BufferedImage src,
Scalr.Rotation rotation,
int targetWidth,
int targetHeight,
BufferedImageOp... ops)
Resize a given image (maintaining its original proportion) to the target width and height, apply the given BufferedImageOp s (if any) and
apply the given rotation to the result before returning it. |
protected static BufferedImage |
scaleImage(BufferedImage src,
int targetWidth,
int targetHeight,
Object interpolationHintValue)
Used to implement a straight-forward image-scaling operation using Java 2D. |
protected static BufferedImage |
scaleImageIncrementally(BufferedImage src,
int targetWidth,
int targetHeight,
Object interpolationHintValue)
Used to implement Chris Campbell's incremental-scaling algorithm: http://today.java.net/pub/a/today/2007/04/03/perils -of-image-getscaledinstance.html. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final boolean DEBUG
true
. This value will be
false
if the "imgscalr.debug" system property is undefined
or set to false
.
This system property can be set on startup with:
-Dimgscalr.debug=true
or by calling System.setProperty(String, String)
before
this class is loaded.
Default value is false
.
public static final String LOG_PREFIX
public static final ConvolveOp OP_ANTIALIAS
ConvolveOp
using a very light "blur" kernel that acts like an
anti-aliasing filter (softens the image a bit) when applied to an image.
A common request by users of the library was that they wished to "soften"
resulting images when scaling them down drastically. After quite a bit of
A/B testing, the kernel used by this Op was selected as the closest match
for the target which was the softer results from the deprecated
AreaAveragingScaleFilter
(which is used internally by the
deprecated Image.getScaledInstance(int, int, int)
method in the
JDK that imgscalr is meant to replace).
This ConvolveOp uses a 3x3 kernel with the values:
.0f | .08f | .0f |
.08f | .68f | .08f |
.0f | .08f | .0f |
ConvolveOp.EDGE_NO_OP
instruction to not process the pixels along
the very edge of the image (otherwise EDGE_ZERO_FILL would create a
black-border around the image). If you have not worked with a ConvolveOp
before, it just means this default OP will "do the right thing" and not
give you garbage results.
This ConvolveOp uses no RenderingHints
values as internally the
ConvolveOp
class only uses hints when doing a color conversion
between the source and destination BufferedImage
targets.
imgscalr allows the ConvolveOp
to create its own destination
image every time, so no color conversion is ever needed and thus no
hints.
ConvolveOp
s are hardware accelerated when
possible. For more information on if your image op is hardware
accelerated or not, check the source code of the underlying JDK class
that actually executes the Op code, sun.awt.image.ImagingLib.
public static final int THRESHOLD_BALANCED_SPEED
Scalr.Method.AUTOMATIC
method will decide if a Scalr.Method.BALANCED
method will be used (if smaller than or equal to threshold) or a
Scalr.Method.SPEED
method will be used (if larger than threshold).
The bigger the image is being scaled to, the less noticeable degradations
in the image becomes and the faster algorithms can be selected.
The value of this threshold (1600) was chosen after visual, by-hand, A/B
testing between different types of images scaled with this library; both
photographs and screenshots. It was determined that images below this
size need to use a Scalr.Method.BALANCED
scale method to look decent in
most all cases while using the faster Scalr.Method.SPEED
method for
images bigger than this threshold showed no noticeable degradation over a
BALANCED
scale.
public static final int THRESHOLD_QUALITY_BALANCED
Scalr.Method.AUTOMATIC
method will decide if a Scalr.Method.QUALITY
method will be used (if smaller than or equal to threshold) or a
Scalr.Method.BALANCED
method will be used (if larger than threshold).
The bigger the image is being scaled to, the less noticeable degradations
in the image becomes and the faster algorithms can be selected.
The value of this threshold (800) was chosen after visual, by-hand, A/B
testing between different types of images scaled with this library; both
photographs and screenshots. It was determined that images below this
size need to use a Scalr.Method.QUALITY
scale method to look decent in
most all cases while using the faster Scalr.Method.BALANCED
method for
images bigger than this threshold showed no noticeable degradation over a
QUALITY
scale.
Constructor Detail |
---|
public Scalr()
Method Detail |
---|
public static BufferedImage resize(BufferedImage src, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
targetSize
and apply the given
BufferedImageOp
s (if any) to the result before returning it.
A scaling method of Scalr.Method.AUTOMATIC
, mode of
Scalr.Mode.AUTOMATIC
and rotation of Scalr.Rotation.NONE
are used.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.targetSize
- The target width and height (square) that you wish the image
to fit within.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if targetSize
is < 0.OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Rotation rotation, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
targetSize
, apply the given
BufferedImageOp
s (if any) and then apply the given rotation to
the result before returning it.
A scaling method of Scalr.Method.AUTOMATIC
and mode of
Scalr.Mode.AUTOMATIC
are used.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.rotation
- The rotation to be applied to the scaled image right before it
is returned.targetSize
- The target width and height (square) that you wish the image
to fit within.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if rotation
is null
.
IllegalArgumentException
- if targetSize
is < 0.Scalr.Rotation
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Method scalingMethod, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
targetSize
using the given scaling
method and apply the given BufferedImageOp
s (if any) to the
result before returning it.
A mode of Scalr.Mode.AUTOMATIC
and rotation of Scalr.Rotation.NONE
are used.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.scalingMethod
- The method used for scaling the image; preferring speed to
quality or a balance of both.targetSize
- The target width and height (square) that you wish the image
to fit within.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if scalingMethod
is null
.
IllegalArgumentException
- if targetSize
is < 0.Scalr.Method
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Rotation rotation, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
targetSize
using the given scaling
method, apply the given BufferedImageOp
s (if any) and then apply
the given rotation to the result before returning it.
A mode of Scalr.Mode.AUTOMATIC
is used.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.scalingMethod
- The method used for scaling the image; preferring speed to
quality or a balance of both.rotation
- The rotation to be applied to the scaled image right before it
is returned.targetSize
- The target width and height (square) that you wish the image
to fit within.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if scalingMethod
is null
.
IllegalArgumentException
- if rotation
is null
.
IllegalArgumentException
- if targetSize
is < 0.Scalr.Method
,
Scalr.Rotation
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Mode resizeMode, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
targetSize
(or fitting the image to
the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode
specified) and then apply the given BufferedImageOp
s (if any) to
the result before returning it.
A scaling method of Scalr.Method.AUTOMATIC
and rotation of
Scalr.Rotation.NONE
are used.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.resizeMode
- Used to indicate how imgscalr should calculate the final
target size for the image, either fitting the image to the
given width (Scalr.Mode.FIT_TO_WIDTH
) or fitting the image
to the given height (Scalr.Mode.FIT_TO_HEIGHT
). If
Scalr.Mode.AUTOMATIC
is passed in, imgscalr will calculate
proportional dimensions for the scaled image based on its
orientation (landscape, square or portrait). Unless you have
very specific size requirements, most of the time you just
want to use Scalr.Mode.AUTOMATIC
to "do the right thing".targetSize
- The target width and height (square) that you wish the image
to fit within.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if resizeMode
is null
.
IllegalArgumentException
- if targetSize
is < 0.Scalr.Mode
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Mode resizeMode, Scalr.Rotation rotation, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
targetSize
(or fitting the image to
the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode
specified), apply the given BufferedImageOp
s (if any) and then
apply the given rotation to the result before returning it.
A scaling method of Scalr.Method.AUTOMATIC
is used.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.resizeMode
- Used to indicate how imgscalr should calculate the final
target size for the image, either fitting the image to the
given width (Scalr.Mode.FIT_TO_WIDTH
) or fitting the image
to the given height (Scalr.Mode.FIT_TO_HEIGHT
). If
Scalr.Mode.AUTOMATIC
is passed in, imgscalr will calculate
proportional dimensions for the scaled image based on its
orientation (landscape, square or portrait). Unless you have
very specific size requirements, most of the time you just
want to use Scalr.Mode.AUTOMATIC
to "do the right thing".rotation
- The rotation to be applied to the scaled image right before it
is returned.targetSize
- The target width and height (square) that you wish the image
to fit within.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if resizeMode
is null
.
IllegalArgumentException
- if rotation
is null
.
IllegalArgumentException
- if targetSize
is < 0.Scalr.Mode
,
Scalr.Rotation
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Mode resizeMode, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
targetSize
(or fitting the image to
the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode
specified) using the given scaling method and apply the given
BufferedImageOp
s (if any) to the result before returning it.
A rotation of Scalr.Rotation.NONE
is used.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.scalingMethod
- The method used for scaling the image; preferring speed to
quality or a balance of both.resizeMode
- Used to indicate how imgscalr should calculate the final
target size for the image, either fitting the image to the
given width (Scalr.Mode.FIT_TO_WIDTH
) or fitting the image
to the given height (Scalr.Mode.FIT_TO_HEIGHT
). If
Scalr.Mode.AUTOMATIC
is passed in, imgscalr will calculate
proportional dimensions for the scaled image based on its
orientation (landscape, square or portrait). Unless you have
very specific size requirements, most of the time you just
want to use Scalr.Mode.AUTOMATIC
to "do the right thing".targetSize
- The target width and height (square) that you wish the image
to fit within.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if scalingMethod
is null
.
IllegalArgumentException
- if resizeMode
is null
.Scalr.Method
,
Scalr.Mode
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Mode resizeMode, Scalr.Rotation rotation, int targetSize, BufferedImageOp... ops) throws IllegalArgumentException
targetSize
(or fitting the image to
the given WIDTH or HEIGHT explicitly, depending on the Scalr.Mode
specified) using the given scaling method, apply the given
BufferedImageOp
s (if any) and apply the given rotation to the
result before returning it.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.scalingMethod
- The method used for scaling the image; preferring speed to
quality or a balance of both.resizeMode
- Used to indicate how imgscalr should calculate the final
target size for the image, either fitting the image to the
given width (Scalr.Mode.FIT_TO_WIDTH
) or fitting the image
to the given height (Scalr.Mode.FIT_TO_HEIGHT
). If
Scalr.Mode.AUTOMATIC
is passed in, imgscalr will calculate
proportional dimensions for the scaled image based on its
orientation (landscape, square or portrait). Unless you have
very specific size requirements, most of the time you just
want to use Scalr.Mode.AUTOMATIC
to "do the right thing".rotation
- The rotation to be applied to the scaled image right before it
is returned.targetSize
- The target width and height (square) that you wish the image
to fit within.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if scalingMethod
is null
.
IllegalArgumentException
- if resizeMode
is null
.
IllegalArgumentException
- if rotation
is null
.Scalr.Method
,
Scalr.Mode
,
Scalr.Rotation
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
BufferedImageOp
s (if any) to
the result before returning it.
A scaling method of Scalr.Method.AUTOMATIC
, mode of
Scalr.Mode.AUTOMATIC
and rotation of Scalr.Rotation.NONE
are used.
TIP: See the class description to understand how this
class handles recalculation of the targetWidth
or
targetHeight
depending on the image's orientation in order
to maintain the original proportion.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.targetWidth
- The target width that you wish the image to have.targetHeight
- The target height that you wish the image to have.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if targetWidth
is < 0 or if
targetHeight
is < 0.OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Rotation rotation, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
BufferedImageOp
s (if any) and
apply the given rotation to the result before returning it.
A scaling method of Scalr.Method.AUTOMATIC
and mode of
Scalr.Mode.AUTOMATIC
are used.
TIP: See the class description to understand how this
class handles recalculation of the targetWidth
or
targetHeight
depending on the image's orientation in order
to maintain the original proportion.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.rotation
- The rotation to be applied to the scaled image right before it
is returned.targetWidth
- The target width that you wish the image to have.targetHeight
- The target height that you wish the image to have.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if rotation
is null
.
IllegalArgumentException
- if targetWidth
is < 0 or if
targetHeight
is < 0.Scalr.Rotation
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Method scalingMethod, int targetWidth, int targetHeight, BufferedImageOp... ops)
BufferedImageOp
s (if any) to the result before returning it.
A mode of Scalr.Mode.AUTOMATIC
and rotation of Scalr.Rotation.NONE
are used.
TIP: See the class description to understand how this
class handles recalculation of the targetWidth
or
targetHeight
depending on the image's orientation in order
to maintain the original proportion.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.scalingMethod
- The method used for scaling the image; preferring speed to
quality or a balance of both.targetWidth
- The target width that you wish the image to have.targetHeight
- The target height that you wish the image to have.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if scalingMethod
is null
.
IllegalArgumentException
- if targetWidth
is < 0 or if
targetHeight
is < 0.Scalr.Method
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Rotation rotation, int targetWidth, int targetHeight, BufferedImageOp... ops)
BufferedImageOp
s (if any) and apply the given rotation to the
result before returning it.
A mode of Scalr.Mode.AUTOMATIC
is used.
TIP: See the class description to understand how this
class handles recalculation of the targetWidth
or
targetHeight
depending on the image's orientation in order
to maintain the original proportion.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.scalingMethod
- The method used for scaling the image; preferring speed to
quality or a balance of both.rotation
- The rotation to be applied to the scaled image right before it
is returned.targetWidth
- The target width that you wish the image to have.targetHeight
- The target height that you wish the image to have.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if scalingMethod
is null
.
IllegalArgumentException
- if rotation
is null
.
IllegalArgumentException
- if targetWidth
is < 0 or if
targetHeight
is < 0.Scalr.Method
,
Scalr.Rotation
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Mode resizeMode, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
Scalr.Mode
specified) and then apply the
given BufferedImageOp
s (if any) to the result before returning
it.
A scaling method of Scalr.Method.AUTOMATIC
and rotation of
Scalr.Rotation.NONE
are used.
TIP: See the class description to understand how this
class handles recalculation of the targetWidth
or
targetHeight
depending on the image's orientation in order
to maintain the original proportion.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.resizeMode
- Used to indicate how imgscalr should calculate the final
target size for the image, either fitting the image to the
given width (Scalr.Mode.FIT_TO_WIDTH
) or fitting the image
to the given height (Scalr.Mode.FIT_TO_HEIGHT
). If
Scalr.Mode.AUTOMATIC
is passed in, imgscalr will calculate
proportional dimensions for the scaled image based on its
orientation (landscape, square or portrait). Unless you have
very specific size requirements, most of the time you just
want to use Scalr.Mode.AUTOMATIC
to "do the right thing".targetWidth
- The target width that you wish the image to have.targetHeight
- The target height that you wish the image to have.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if resizeMode
is null
.
IllegalArgumentException
- if targetWidth
is < 0 or if
targetHeight
is < 0.Scalr.Mode
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Mode resizeMode, Scalr.Rotation rotation, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
Scalr.Mode
specified), apply the given
BufferedImageOp
s (if any) and then apply the given rotation to
the result before returning it.
A scaling method of Scalr.Method.AUTOMATIC
is used.
TIP: See the class description to understand how this
class handles recalculation of the targetWidth
or
targetHeight
depending on the image's orientation in order
to maintain the original proportion.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.resizeMode
- Used to indicate how imgscalr should calculate the final
target size for the image, either fitting the image to the
given width (Scalr.Mode.FIT_TO_WIDTH
) or fitting the image
to the given height (Scalr.Mode.FIT_TO_HEIGHT
). If
Scalr.Mode.AUTOMATIC
is passed in, imgscalr will calculate
proportional dimensions for the scaled image based on its
orientation (landscape, square or portrait). Unless you have
very specific size requirements, most of the time you just
want to use Scalr.Mode.AUTOMATIC
to "do the right thing".rotation
- The rotation to be applied to the scaled image right before it
is returned.targetWidth
- The target width that you wish the image to have.targetHeight
- The target height that you wish the image to have.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if resizeMode
is null
.
IllegalArgumentException
- if rotation
is null
.
IllegalArgumentException
- if targetWidth
is < 0 or if
targetHeight
is < 0.Scalr.Mode
,
Scalr.Rotation
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Mode resizeMode, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
Scalr.Mode
specified) using the given
scaling method and apply the given BufferedImageOp
s (if any) to
the result before returning it.
A rotation of Scalr.Rotation.NONE
is used.
TIP: See the class description to understand how this
class handles recalculation of the targetWidth
or
targetHeight
depending on the image's orientation in order
to maintain the original proportion.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.scalingMethod
- The method used for scaling the image; preferring speed to
quality or a balance of both.resizeMode
- Used to indicate how imgscalr should calculate the final
target size for the image, either fitting the image to the
given width (Scalr.Mode.FIT_TO_WIDTH
) or fitting the image
to the given height (Scalr.Mode.FIT_TO_HEIGHT
). If
Scalr.Mode.AUTOMATIC
is passed in, imgscalr will calculate
proportional dimensions for the scaled image based on its
orientation (landscape, square or portrait). Unless you have
very specific size requirements, most of the time you just
want to use Scalr.Mode.AUTOMATIC
to "do the right thing".targetWidth
- The target width that you wish the image to have.targetHeight
- The target height that you wish the image to have.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if scalingMethod
is null
.
IllegalArgumentException
- if resizeMode
is null
.
IllegalArgumentException
- if targetWidth
is < 0 or if
targetHeight
is < 0.Scalr.Method
,
Scalr.Mode
,
OP_ANTIALIAS
public static BufferedImage resize(BufferedImage src, Scalr.Method scalingMethod, Scalr.Mode resizeMode, Scalr.Rotation rotation, int targetWidth, int targetHeight, BufferedImageOp... ops) throws IllegalArgumentException
Scalr.Mode
specified) using the given
scaling method, apply the given BufferedImageOp
s (if any) and
apply the given rotation to the result before returning it.
TIP: See the class description to understand how this
class handles recalculation of the targetWidth
or
targetHeight
depending on the image's orientation in order
to maintain the original proportion.
Performance: Not all BufferedImageOp
s are
hardware accelerated operations, but many of the most popular (like
ConvolveOp
) are. For more information on if your image op is
hardware accelerated or not, check the source code of the underlying JDK
class that actually executes the Op code, sun.awt.image.ImagingLib.
src
- The image that will be scaled.scalingMethod
- The method used for scaling the image; preferring speed to
quality or a balance of both.resizeMode
- Used to indicate how imgscalr should calculate the final
target size for the image, either fitting the image to the
given width (Scalr.Mode.FIT_TO_WIDTH
) or fitting the image
to the given height (Scalr.Mode.FIT_TO_HEIGHT
). If
Scalr.Mode.AUTOMATIC
is passed in, imgscalr will calculate
proportional dimensions for the scaled image based on its
orientation (landscape, square or portrait). Unless you have
very specific size requirements, most of the time you just
want to use Scalr.Mode.AUTOMATIC
to "do the right thing".rotation
- The rotation to be applied to the scaled image right before it
is returned.targetWidth
- The target width that you wish the image to have.targetHeight
- The target height that you wish the image to have.ops
- Zero or more optional image operations (e.g. sharpen, blur,
etc.) that can be applied to the final result before returning
the image.
IllegalArgumentException
- if src
is null
.
IllegalArgumentException
- if scalingMethod
is null
.
IllegalArgumentException
- if resizeMode
is null
.
IllegalArgumentException
- if rotation
is null
.
IllegalArgumentException
- if targetWidth
is < 0 or if
targetHeight
is < 0.Scalr.Method
,
Scalr.Mode
,
Scalr.Rotation
,
OP_ANTIALIAS
protected static void log(String message, Object... params)
params
array, care should
be taken not to call this method with primitive values unless
DEBUG
is true
; otherwise the VM will be spending
time performing unnecessary auto-boxing calculations.
message
- The log message in format string syntax that will be logged.params
- The parameters that will be swapped into all the place holders
in the original messages before being logged.LOG_PREFIX
protected static Scalr.Method determineScalingMethod(int targetWidth, int targetHeight, float ratio)
Scalr.Method
that is best suited for
scaling the image to the targeted dimensions.
This method is intended to be used to select a specific scaling
Scalr.Method
when a Scalr.Method.AUTOMATIC
method is specified. This
method utilizes the THRESHOLD_QUALITY_BALANCED
and
THRESHOLD_BALANCED_SPEED
thresholds when selecting which method
should be used by comparing the primary dimension (width or height)
against the threshold and seeing where the image falls. The primary
dimension is determined by looking at the orientation of the image:
landscape or square images use their width and portrait-oriented images
use their height.
targetWidth
- The target width for the scaled image.targetHeight
- The target height for the scaled image.ratio
- A height/width ratio used to determine the orientation of the
image so the primary dimension (width or height) can be
selected to test if it is greater than or less than a
particular threshold.
Scalr.Method
suited for scaling the image to the
specified dimensions while maintaining a good-looking result.protected static BufferedImage scaleImage(BufferedImage src, int targetWidth, int targetHeight, Object interpolationHintValue)
Graphics2D.drawImage(...)
to scale the given image with the
given interpolation hint.
src
- The image that will be scaled.targetWidth
- The target width for the scaled image.targetHeight
- The target height for the scaled image.interpolationHintValue
- The RenderingHints
interpolation value used to
indicate the method that Graphics2D
should use when
scaling the image.
src
to the given
dimensions using the given interpolation method.protected static BufferedImage scaleImageIncrementally(BufferedImage src, int targetWidth, int targetHeight, Object interpolationHintValue)
src
- The image that will be scaled.targetWidth
- The target width for the scaled image.targetHeight
- The target height for the scaled image.interpolationHintValue
- The RenderingHints
interpolation value used to
indicate the method that Graphics2D
should use when
scaling the image.
|
Copyright 2011 The Buzz Media, LLC | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |