/*
* *****************************************************************************
* Copyright (C) 2014-2022 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
GraphicsUtilities contains a set of tools to perform
* common graphics operations easily. These operations are divided into
* several themes, listed below.
Compatible images can, and should, be used to increase drawing * performance. This class provides a number of methods to load compatible * images directly from files or to convert existing images to compatibles * images.
* *This class provides a number of methods to easily scale down images. * Some of these methods offer a trade-off between speed and result quality and * should be used all the time. They also offer the advantage of producing * compatible images, thus automatically resulting into better runtime * performance.
* *All these methods are both faster than
* {@link java.awt.Image#getScaledInstance(int, int, int)} and produce
* better-looking results than the various drawImage() methods
* in {@link java.awt.Graphics}, which can be used for image scaling.
This class provides two methods to get and set pixels in a buffered image. * These methods try to avoid unmanaging the image in order to keep good * performance.
* * @author Romain GuyReturns a new BufferedImage using the same color model
* as the image passed as a parameter. The returned image is only compatible
* with the image passed as a parameter. This does not mean the returned
* image is compatible with the hardware.
BufferedImage, compatible with the color model
* of image
*/
public static BufferedImage createColorModelCompatibleImage(BufferedImage image) {
ColorModel cm = image.getColorModel();
return new BufferedImage(cm,
cm.createCompatibleWritableRaster(image.getWidth(),
image.getHeight()),
cm.isAlphaPremultiplied(), null);
}
/**
* Returns a new compatible image with the same width, height and * transparency as the image specified as a parameter. That is, the * returned BufferedImage will be compatible with the graphics hardware. * If this method is called in a headless environment, then * the returned BufferedImage will be compatible with the source * image.
* * @see java.awt.Transparency * @see #createCompatibleImage(int, int) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createCompatibleTranslucentImage(int, int) * @see #loadCompatibleImage(java.net.URL) * @see #toCompatibleImage(java.awt.image.BufferedImage) * @param image the reference image from which the dimension and the * transparency of the new image are obtained * @return a new compatibleBufferedImage with the same
* dimension and transparency as image
*/
public static BufferedImage createCompatibleImage(BufferedImage image) {
return createCompatibleImage(image, image.getWidth(), image.getHeight());
}
/**
* Returns a new compatible image of the specified width and height, and
* the same transparency setting as the image specified as a parameter.
* That is, the returned BufferedImage is compatible with
* the graphics hardware. If the method is called in a headless
* environment, then the returned BufferedImage will be compatible with
* the source image.
BufferedImage with the same
* transparency as image and the specified dimension
*/
public static BufferedImage createCompatibleImage(BufferedImage image,
int width, int height) {
return isHeadless() ?
new BufferedImage(width, height, image.getType()) :
getGraphicsConfiguration().createCompatibleImage(width, height,
image.getTransparency());
}
/**
* Returns a new opaque compatible image of the specified width and
* height. That is, the returned BufferedImage is compatible with
* the graphics hardware. If the method is called in a headless
* environment, then the returned BufferedImage will be compatible with
* the source image.
BufferedImage of the
* specified width and height
*/
public static BufferedImage createCompatibleImage(int width, int height) {
return isHeadless() ?
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) :
getGraphicsConfiguration().createCompatibleImage(width, height);
}
/**
* Returns a new translucent compatible image of the specified width and
* height. That is, the returned BufferedImage is compatible with
* the graphics hardware. If the method is called in a headless
* environment, then the returned BufferedImage will be compatible with
* the source image.
BufferedImage of the
* specified width and height
*/
public static BufferedImage createCompatibleTranslucentImage(int width,
int height) {
return isHeadless() ?
new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) :
getGraphicsConfiguration().createCompatibleImage(width, height,
Transparency.TRANSLUCENT);
}
/**
* * Returns a new compatible image from a stream. The image is loaded from * the specified stream and then turned, if necessary into a compatible * image. *
* * @see #createCompatibleImage(java.awt.image.BufferedImage) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createCompatibleImage(int, int) * @see #createCompatibleTranslucentImage(int, int) * @see #toCompatibleImage(java.awt.image.BufferedImage) * @param in * the stream of the picture to load as a compatible image * @return a new translucent compatibleBufferedImage of the
* specified width and height
* @throws java.io.IOException
* if the image cannot be read or loaded
*/
public static BufferedImage loadCompatibleImage(InputStream in) throws IOException {
BufferedImage image = ImageIO.read(in);
if(image == null) return null;
return toCompatibleImage(image);
}
/**
* Returns a new compatible image from a URL. The image is loaded from the * specified location and then turned, if necessary into a compatible * image.
* * @see #createCompatibleImage(java.awt.image.BufferedImage) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createCompatibleImage(int, int) * @see #createCompatibleTranslucentImage(int, int) * @see #toCompatibleImage(java.awt.image.BufferedImage) * @param resource the URL of the picture to load as a compatible image * @return a new translucent compatibleBufferedImage of the
* specified width and height
* @throws java.io.IOException if the image cannot be read or loaded
*/
public static BufferedImage loadCompatibleImage(URL resource)
throws IOException {
BufferedImage image = ImageIO.read(resource);
return toCompatibleImage(image);
}
/**
* Return a new compatible image that contains a copy of the specified * image. This method ensures an image is compatible with the hardware, * and therefore optimized for fast blitting operations.
* *If the method is called in a headless environment, then the returned
* BufferedImage will be the source image.
image
*/
public static BufferedImage toCompatibleImage(BufferedImage image) {
if (isHeadless()) {
return image;
}
if (image.getColorModel().equals(
getGraphicsConfiguration().getColorModel())) {
return image;
}
BufferedImage compatibleImage =
getGraphicsConfiguration().createCompatibleImage(
image.getWidth(), image.getHeight(),
image.getTransparency());
Graphics g = compatibleImage.getGraphics();
try {
g.drawImage(image, 0, 0, null);
} finally {
g.dispose();
}
return compatibleImage;
}
/**
* Returns a thumbnail of a source image. newSize defines
* the length of the longest dimension of the thumbnail. The other
* dimension is then computed according to the dimensions ratio of the
* original picture.
This method favors speed over quality. When the new size is less than * half the longest dimension of the source image, * {@link #createThumbnail(BufferedImage, int)} or * {@link #createThumbnail(BufferedImage, int, int)} should be used instead * to ensure the quality of the result without sacrificing too much * performance.
* * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image the source image * @param newSize the length of the largest dimension of the thumbnail * @return a new compatibleBufferedImage containing a
* thumbnail of image
* @throws IllegalArgumentException if newSize is larger than
* the largest dimension of image or <= 0
*/
public static BufferedImage createThumbnailFast(BufferedImage image,
int newSize) {
float ratio;
int width = image.getWidth();
int height = image.getHeight();
if (width > height) {
if (newSize >= width) {
throw new IllegalArgumentException("newSize must be lower than" +
" the image width");
} else if (newSize <= 0) {
throw new IllegalArgumentException("newSize must" +
" be greater than 0");
}
ratio = (float) width / (float) height;
width = newSize;
height = (int) (newSize / ratio);
} else {
if (newSize >= height) {
throw new IllegalArgumentException("newSize must be lower than" +
" the image height");
} else if (newSize <= 0) {
throw new IllegalArgumentException("newSize must" +
" be greater than 0");
}
ratio = (float) height / (float) width;
height = newSize;
width = (int) (newSize / ratio);
}
BufferedImage temp = createCompatibleImage(image, width, height);
Graphics2D g2 = temp.createGraphics();
try {
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
} finally {
g2.dispose();
}
return temp;
}
/**
* Returns a thumbnail of a source image.
*This method favors speed over quality. When the new size is less than * half the longest dimension of the source image, * {@link #createThumbnail(BufferedImage, int)} or * {@link #createThumbnail(BufferedImage, int, int)} should be used instead * to ensure the quality of the result without sacrificing too much * performance.
* * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image the source image * @param newWidth the width of the thumbnail * @param newHeight the height of the thumbnail * @return a new compatibleBufferedImage containing a
* thumbnail of image
* @throws IllegalArgumentException if newWidth is larger than
* the width of image or if code>newHeight is larger
* than the height of image or if one of the dimensions
* is <= 0
*/
public static BufferedImage createThumbnailFast(BufferedImage image,
int newWidth, int newHeight) {
if (newWidth >= image.getWidth() ||
newHeight >= image.getHeight()) {
throw new IllegalArgumentException("newWidth and newHeight cannot" +
" be greater than the image" +
" dimensions");
} else if (newWidth <= 0 || newHeight <= 0) {
throw new IllegalArgumentException("newWidth and newHeight must" +
" be greater than 0");
}
BufferedImage temp = createCompatibleImage(image, newWidth, newHeight);
Graphics2D g2 = temp.createGraphics();
try {
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
} finally {
g2.dispose();
}
return temp;
}
/**
* Returns a thumbnail of a source image. newSize defines
* the length of the longest dimension of the thumbnail. The other
* dimension is then computed according to the dimensions ratio of the
* original picture.
This method offers a good trade-off between speed and quality. * The result looks better than * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when * the new size is less than half the longest dimension of the source * image, yet the rendering speed is almost similar.
* * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image the source image * @param newSize the length of the largest dimension of the thumbnail * @return a new compatibleBufferedImage containing a
* thumbnail of image
* @throws IllegalArgumentException if newSize is larger than
* the largest dimension of image or <= 0
*/
public static BufferedImage createThumbnail(BufferedImage image,
int newSize) {
int width = image.getWidth();
int height = image.getHeight();
boolean isTranslucent = image.getTransparency() != Transparency.OPAQUE;
boolean isWidthGreater = width > height;
if (isWidthGreater) {
if (newSize >= width) {
throw new IllegalArgumentException("newSize must be lower than" +
" the image width");
}
} else if (newSize >= height) {
throw new IllegalArgumentException("newSize must be lower than" +
" the image height");
}
if (newSize <= 0) {
throw new IllegalArgumentException("newSize must" +
" be greater than 0");
}
float ratioWH = (float) width / (float) height;
float ratioHW = (float) height / (float) width;
BufferedImage thumb = image;
BufferedImage temp = null;
Graphics2D g2 = null;
try {
int previousWidth = width;
int previousHeight = height;
do {
if (isWidthGreater) {
width /= 2;
if (width < newSize) {
width = newSize;
}
height = (int) (width / ratioWH);
} else {
height /= 2;
if (height < newSize) {
height = newSize;
}
width = (int) (height / ratioHW);
}
if (temp == null || isTranslucent) {
if (g2 != null) {
//do not need to wrap with finally
//outer finally block will ensure
//that resources are properly reclaimed
g2.dispose();
}
temp = createCompatibleImage(image, width, height);
g2 = temp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
}
if (g2 != null) // always the case
g2.drawImage(thumb, 0, 0, width, height,
0, 0, previousWidth, previousHeight, null);
previousWidth = width;
previousHeight = height;
thumb = temp;
} while (newSize != (isWidthGreater ? width : height));
} finally {
if (g2 != null) {
g2.dispose();
}
}
if (width != thumb.getWidth() || height != thumb.getHeight()) {
temp = createCompatibleImage(image, width, height);
g2 = temp.createGraphics();
try {
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(thumb, 0, 0, width, height, 0, 0, width, height, null);
} finally {
g2.dispose();
}
thumb = temp;
}
return thumb;
}
/**
* Returns a thumbnail of a source image.
*This method offers a good trade-off between speed and quality. * The result looks better than * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when * the new size is less than half the longest dimension of the source * image, yet the rendering speed is almost similar.
* * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @param image the source image * @param newWidth the width of the thumbnail * @param newHeight the height of the thumbnail * @return a new compatibleBufferedImage containing a
* thumbnail of image
* @throws IllegalArgumentException if newWidth is larger than
* the width of image or if code>newHeight is larger
* than the height of image or if one the dimensions is not > 0
*/
public static BufferedImage createThumbnail(BufferedImage image,
int newWidth, int newHeight) {
int width = image.getWidth();
int height = image.getHeight();
boolean isTranslucent = image.getTransparency() != Transparency.OPAQUE;
if (newWidth >= width || newHeight >= height) {
throw new IllegalArgumentException("newWidth and newHeight cannot" +
" be greater than the image" +
" dimensions");
} else if (newWidth <= 0 || newHeight <= 0) {
throw new IllegalArgumentException("newWidth and newHeight must" +
" be greater than 0");
}
BufferedImage thumb = image;
BufferedImage temp = null;
Graphics2D g2 = null;
try {
int previousWidth = width;
int previousHeight = height;
do {
if (width > newWidth) {
width /= 2;
if (width < newWidth) {
width = newWidth;
}
}
if (height > newHeight) {
height /= 2;
if (height < newHeight) {
height = newHeight;
}
}
if (temp == null || isTranslucent) {
if (g2 != null) {
//do not need to wrap with finally
//outer finally block will ensure
//that resources are properly reclaimed
g2.dispose();
}
temp = createCompatibleImage(image, width, height);
g2 = temp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
}
if (g2 != null) // always the case
g2.drawImage(thumb, 0, 0, width, height,
0, 0, previousWidth, previousHeight, null);
previousWidth = width;
previousHeight = height;
thumb = temp;
} while (width != newWidth || height != newHeight);
} finally {
if (g2 != null) {
g2.dispose();
}
}
if (width != thumb.getWidth() || height != thumb.getHeight()) {
temp = createCompatibleImage(image, width, height);
g2 = temp.createGraphics();
try {
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(thumb, 0, 0, width, height, 0, 0, width, height, null);
} finally {
g2.dispose();
}
thumb = temp;
}
return thumb;
}
/**
* Returns an array of pixels, stored as integers, from a
* BufferedImage. The pixels are grabbed from a rectangular
* area defined by a location and two dimensions. Calling this method on
* an image of type different from BufferedImage.TYPE_INT_ARGB
* and BufferedImage.TYPE_INT_RGB will unmanage the image.
pixels if non-null, a new array of integers
* otherwise
* @throws IllegalArgumentException is pixels is non-null and
* of length < w*h
*/
public static int[] getPixels(BufferedImage img,
int x, int y, int w, int h, int[] pixels) {
if (w == 0 || h == 0) {
return new int[0];
}
if (pixels == null) {
pixels = new int[w * h];
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length" +
" >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_INT_ARGB ||
imageType == BufferedImage.TYPE_INT_RGB) {
Raster raster = img.getRaster();
return (int[]) raster.getDataElements(x, y, w, h, pixels);
}
// Unmanages the image
return img.getRGB(x, y, w, h, pixels, 0, w);
}
/**
* Writes a rectangular area of pixels in the destination
* BufferedImage. Calling this method on
* an image of type different from BufferedImage.TYPE_INT_ARGB
* and BufferedImage.TYPE_INT_RGB will unmanage the image.
pixels is non-null and
* of length < w*h
*/
public static void setPixels(BufferedImage img,
int x, int y, int w, int h, int[] pixels) {
if (pixels == null || w == 0 || h == 0) {
return;
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length" +
" >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_INT_ARGB ||
imageType == BufferedImage.TYPE_INT_RGB) {
WritableRaster raster = img.getRaster();
raster.setDataElements(x, y, w, h, pixels);
} else {
// Unmanages the image
img.setRGB(x, y, w, h, pixels, 0, w);
}
}
/**
* Clears the data from the image.
*
* @param img
* the image to erase
*/
public static void clear(Image img) {
Graphics g = img.getGraphics();
try {
if (g instanceof Graphics2D) {
((Graphics2D) g).setComposite(AlphaComposite.Clear);
} else {
g.setColor(new Color(0, 0, 0, 0));
}
g.fillRect(0, 0, img.getWidth(null), img.getHeight(null));
} finally {
g.dispose();
}
}
/**
* Sets the clip on a graphics object by merging a supplied clip with the existing one. The new
* clip will be an intersection of the old clip and the supplied clip. The old clip shape will
* be returned. This is useful for resetting the old clip after an operation is performed.
*
* @param g
* the graphics object to update
* @param clip
* a new clipping region to add to the graphics clip.
* @return the current clipping region of the supplied graphics object. This may return {@code
* null} if the current clip is {@code null}.
* @throws NullPointerException
* if any parameter is {@code null}
* @deprecated Use {@link ShapeUtils#mergeClip(Graphics,Shape)} instead
*/
@Deprecated
public static Shape mergeClip(Graphics g, Shape clip) {
return ShapeUtils.mergeClip(g, clip);
}
/**
* Draws an image on top of a component by doing a 3x3 grid stretch of the image
* using the specified insets.
* @param g the graphics object
* @param comp the component
* @param img the image
* @param ins the insets
*/
public static void tileStretchPaint(Graphics g,
JComponent comp,
BufferedImage img,
Insets ins) {
int left = ins.left;
int right = ins.right;
int top = ins.top;
int bottom = ins.bottom;
// top
g.drawImage(img,
0,0,left,top,
0,0,left,top,
null);
g.drawImage(img,
left, 0,
comp.getWidth() - right, top,
left, 0,
img.getWidth() - right, top,
null);
g.drawImage(img,
comp.getWidth() - right, 0,
comp.getWidth(), top,
img.getWidth() - right, 0,
img.getWidth(), top,
null);
// middle
g.drawImage(img,
0, top,
left, comp.getHeight()-bottom,
0, top,
left, img.getHeight()-bottom,
null);
g.drawImage(img,
left, top,
comp.getWidth()-right, comp.getHeight()-bottom,
left, top,
img.getWidth()-right, img.getHeight()-bottom,
null);
g.drawImage(img,
comp.getWidth()-right, top,
comp.getWidth(), comp.getHeight()-bottom,
img.getWidth()-right, top,
img.getWidth(), img.getHeight()-bottom,
null);
// bottom
g.drawImage(img,
0,comp.getHeight()-bottom,
left, comp.getHeight(),
0,img.getHeight()-bottom,
left,img.getHeight(),
null);
g.drawImage(img,
left, comp.getHeight()-bottom,
comp.getWidth()-right, comp.getHeight(),
left, img.getHeight()-bottom,
img.getWidth()-right, img.getHeight(),
null);
g.drawImage(img,
comp.getWidth()-right, comp.getHeight()-bottom,
comp.getWidth(), comp.getHeight(),
img.getWidth()-right, img.getHeight()-bottom,
img.getWidth(), img.getHeight(),
null);
}
}