public final class AWTImageTools extends Object
BufferedImage
form.
To work with images in primitive array form,
use the ImageTools
class.
Much code was stolen and adapted from
DrLaszloJamf's posts
on the Java forums.Modifier and Type | Field and Description |
---|---|
private static org.slf4j.Logger |
LOGGER |
protected static Component |
OBS
ImageObserver for working with AWT images.
|
Modifier | Constructor and Description |
---|---|
private |
AWTImageTools() |
Modifier and Type | Method and Description |
---|---|
static BufferedImage |
autoscale(BufferedImage img)
Perform autoscaling on the given BufferedImage.
|
static BufferedImage |
autoscale(BufferedImage img,
int min,
int max)
Perform autoscaling on the given BufferedImage;
map min to 0 and max to 255.
|
static BufferedImage |
blankImage(int w,
int h,
int c,
int type)
Creates a blank image with the given dimensions and transfer type.
|
private static boolean |
canUseBankDataDirectly(WritableRaster r,
int transferType,
Class<? extends DataBuffer> dataBufferClass)
Whether we can return the data buffer's bank data
without performing any copy or conversion operations.
|
static BufferedImage |
constructImage(int c,
int type,
int w,
int h,
boolean interleaved,
boolean banded,
DataBuffer buffer)
Creates an image with the given DataBuffer.
|
static BufferedImage |
constructImage(int c,
int type,
int w,
int h,
boolean interleaved,
boolean banded,
DataBuffer buffer,
ColorModel colorModel)
Creates an image with the given DataBuffer.
|
static BufferedImage |
convertRenderedImage(RenderedImage img)
Converts a java.awt.image.RenderedImage into a
java.awt.image.BufferedImage.
|
static BufferedImage |
copyScaled(BufferedImage source,
BufferedImage target,
Object hint)
Copies the source image into the target, applying scaling.
|
static byte[][] |
get8BitLookupTable(ColorModel model)
Converts an IndexColorModel to a 2D byte array.
|
static byte[][] |
getBytes(BufferedImage image)
Extracts pixel data as arrays of unsigned bytes, one per channel.
|
static byte[] |
getBytes(BufferedImage img,
boolean separated)
Get the bytes from an image, merging the channels as necessary.
|
static byte[][] |
getBytes(WritableRaster r)
Extracts pixel data as arrays of unsigned bytes, one per channel.
|
static byte[][] |
getBytes(WritableRaster r,
int x,
int y,
int w,
int h)
Extracts pixel data as arrays of unsigned bytes, one per channel.
|
static GraphicsConfiguration |
getDefaultConfiguration()
Gets the default graphics configuration for the environment.
|
static double[][] |
getDoubles(BufferedImage image)
Extracts pixel data as arrays of doubles, one per channel.
|
static double[][] |
getDoubles(WritableRaster r)
Extracts pixel data as arrays of doubles, one per channel.
|
static double[][] |
getDoubles(WritableRaster r,
int x,
int y,
int w,
int h)
Extracts pixel data as arrays of doubles, one per channel.
|
static float[][] |
getFloats(BufferedImage image)
Extracts pixel data as arrays of floats, one per channel.
|
static float[][] |
getFloats(WritableRaster r)
Extracts pixel data as arrays of floats, one per channel.
|
static float[][] |
getFloats(WritableRaster r,
int x,
int y,
int w,
int h)
Extracts pixel data as arrays of floats, one per channel.
|
static int[][] |
getInts(BufferedImage image)
Extracts pixel data as arrays of signed integers, one per channel.
|
static int[][] |
getInts(WritableRaster r)
Extracts pixel data as arrays of signed integers, one per channel.
|
static int[][] |
getInts(WritableRaster r,
int x,
int y,
int w,
int h)
Extracts pixel data as arrays of signed integers, one per channel.
|
static short[][] |
getLookupTable(ColorModel model)
Convers an Index16ColorModel to a 2D short array.
|
static byte[][] |
getPixelBytes(BufferedImage img,
boolean little)
Return a 2D array of bytes representing the image.
|
static byte[][] |
getPixelBytes(BufferedImage img,
boolean little,
int x,
int y,
int w,
int h)
Return a 2D array of bytes representing the image.
|
static byte[][] |
getPixelBytes(WritableRaster r,
boolean little)
Return a 2D array of bytes representing the image.
|
static byte[][] |
getPixelBytes(WritableRaster r,
boolean little,
int x,
int y,
int w,
int h)
Return a 2D array of bytes representing the image.
|
static Object |
getPixels(BufferedImage image)
Gets the image's pixel data as arrays of primitives, one per channel.
|
static Object |
getPixels(BufferedImage image,
int x,
int y,
int w,
int h)
Gets the image's pixel data as arrays of primitives, one per channel.
|
static Object |
getPixels(WritableRaster raster)
Gets the raster's pixel data as arrays of primitives, one per channel.
|
static Object |
getPixels(WritableRaster raster,
int x,
int y,
int w,
int h)
Gets the raster's pixel data as arrays of primitives, one per channel.
|
static int |
getPixelType(BufferedImage image)
Gets the pixel type of the given image.
|
static short[][] |
getShorts(BufferedImage image)
Extracts pixel data as arrays of unsigned shorts, one per channel.
|
static short[][] |
getShorts(WritableRaster r)
Extracts pixel data as arrays of unsigned shorts, one per channel.
|
static short[][] |
getShorts(WritableRaster r,
int x,
int y,
int w,
int h)
Extracts pixel data as arrays of unsigned shorts, one per channel.
|
static Dimension |
getSize(Image image)
Gets the width and height of the given AWT image,
waiting for it to finish loading if necessary.
|
static BufferedImage |
getSubimage(BufferedImage image,
boolean littleEndian,
int x,
int y,
int w,
int h)
Returns a subimage of the specified image.
|
static BufferedImage |
indexedToRGB(BufferedImage img,
boolean le)
Converts an indexed color BufferedImage to an RGB BufferedImage.
|
static boolean |
loadImage(Image image)
Ensures the given AWT image is fully loaded.
|
static BufferedImage |
makeBuffered(Image image)
Creates a buffered image from the given AWT image object.
|
static BufferedImage |
makeBuffered(Image image,
ColorModel cm)
Creates a buffered image possessing the given color model,
from the specified AWT image object.
|
static ColorModel |
makeColorModel(int c,
int dataType)
Gets a color model for the given number of color components.
|
static ColorSpace |
makeColorSpace(int c)
Gets a color space for the given number of color components.
|
static BufferedImage |
makeCompatible(BufferedImage image,
GraphicsConfiguration gc)
Creates a buffered image compatible with the given graphics
configuration, using the given buffered image as a source.
|
static BufferedImage |
makeImage(byte[][] data,
int w,
int h,
boolean signed)
Creates an image from the given byte data.
|
static BufferedImage |
makeImage(byte[][] data,
int w,
int h,
int bpp,
boolean fp,
boolean little,
boolean signed)
Creates an image from the given raw byte array,
performing any necessary type conversions.
|
static BufferedImage |
makeImage(byte[] data,
boolean interleaved,
MetadataRetrieve meta,
int series)
Creates an image from the given raw byte array, obtaining the
dimensional parameters from the specified metadata object.
|
static BufferedImage |
makeImage(byte[] data,
int w,
int h,
boolean signed)
Creates an image from the given single-channel byte data.
|
static BufferedImage |
makeImage(byte[] data,
int w,
int h,
int c,
boolean interleaved,
boolean signed)
Creates an image from the given byte data.
|
static BufferedImage |
makeImage(byte[] data,
int w,
int h,
int c,
boolean interleaved,
int bpp,
boolean fp,
boolean little,
boolean signed)
Creates an image from the given raw byte array,
performing any necessary type conversions.
|
static BufferedImage |
makeImage(double[][] data,
int w,
int h)
Creates an image from the given double-precision floating point data.
|
static BufferedImage |
makeImage(double[] data,
int w,
int h)
Creates an image from the given single-channel double data.
|
static BufferedImage |
makeImage(double[] data,
int w,
int h,
int c,
boolean interleaved)
Creates an image from the given double data.
|
static BufferedImage |
makeImage(float[][] data,
int w,
int h)
Creates an image from the given single-precision floating point data.
|
static BufferedImage |
makeImage(float[] data,
int w,
int h)
Creates an image from the given single-channel float data.
|
static BufferedImage |
makeImage(float[] data,
int w,
int h,
int c,
boolean interleaved)
Creates an image from the given float data.
|
static BufferedImage |
makeImage(int[][] data,
int w,
int h,
boolean signed)
Creates an image from the given int data.
|
static BufferedImage |
makeImage(int[] data,
int w,
int h,
boolean signed)
Creates an image from the given single-channel int data.
|
static BufferedImage |
makeImage(int[] data,
int w,
int h,
int c,
boolean interleaved,
boolean signed)
Creates an image from the given int data.
|
static BufferedImage |
makeImage(short[][] data,
int w,
int h,
boolean signed)
Creates an image from the given short data.
|
static BufferedImage |
makeImage(short[] data,
int w,
int h,
boolean signed)
Creates an image from the given single-channel short data.
|
static BufferedImage |
makeImage(short[] data,
int w,
int h,
int c,
boolean interleaved,
boolean signed)
Creates an image from the given short data.
|
static BufferedImage |
makeRGBImage(byte[][] data,
int w,
int h) |
static BufferedImage |
makeRGBImage(byte[] data,
int c,
int w,
int h,
boolean interleaved) |
static BufferedImage |
makeUnsigned(BufferedImage img)
Converts the given BufferedImage into an image with unsigned pixel data.
|
static BufferedImage |
mergeChannels(BufferedImage[] images)
Merges the given images into a single multi-channel image.
|
static BufferedImage |
openImage(byte[] buf,
IFormatReader r,
int w,
int h)
Creates an image from the given byte array, using the given
IFormatReader to retrieve additional information.
|
static BufferedImage |
padImage(BufferedImage img,
int width,
int height)
Pads (or crops) the image to the given width and height.
|
static BufferedImage |
scale(BufferedImage source,
int width,
int height,
boolean pad)
Scales the image using the most appropriate API, with the resultant image
having the same color model as the original image.
|
static BufferedImage |
scale2D(BufferedImage image,
int width,
int height,
Object hint,
ColorModel cm)
Scales the image using the Java2D API, with the
resultant image having the given color model.
|
static BufferedImage |
scale2D(BufferedImage image,
int width,
int height,
Object hint,
GraphicsConfiguration gc)
Scales the image using the Java2D API, with the resultant
image optimized for the given graphics configuration.
|
static Image |
scaleAWT(BufferedImage source,
int width,
int height,
int hint)
Scales the image using the AWT Image API.
|
static BufferedImage[] |
splitChannels(BufferedImage image)
Splits the given multi-channel image into single-channel images.
|
protected static final Component OBS
private static final org.slf4j.Logger LOGGER
public static BufferedImage makeImage(byte[] data, int w, int h, boolean signed)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.signed
- Whether the byte values should be treated as signed
(-128 to 127) instead of unsigned (0 to 255).public static BufferedImage makeImage(short[] data, int w, int h, boolean signed)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.signed
- Whether the short values should be treated as signed
(-32768 to 32767) instead of unsigned (0 to 65535).public static BufferedImage makeImage(int[] data, int w, int h, boolean signed)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.signed
- Whether the int values should be treated as signed
(-2^31 to 2^31-1) instead of unsigned (0 to 2^32-1).public static BufferedImage makeImage(float[] data, int w, int h)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.public static BufferedImage makeImage(double[] data, int w, int h)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.public static BufferedImage makeImage(byte[] data, int w, int h, int c, boolean interleaved, boolean signed)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.c
- Number of channels.interleaved
- If set, the channels are assumed to be interleaved;
otherwise they are assumed to be sequential.
For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved,
while "RRR...GGG...BBB..." is sequential.signed
- Whether the byte values should be treated as signed
(-128 to 127) instead of unsigned (0 to 255).public static BufferedImage makeImage(short[] data, int w, int h, int c, boolean interleaved, boolean signed)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.c
- Number of channels.interleaved
- If set, the channels are assumed to be interleaved;
otherwise they are assumed to be sequential.
For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved,
while "RRR...GGG...BBB..." is sequential.signed
- Whether the short values should be treated as signed
(-32768 to 32767) instead of unsigned (0 to 65535).public static BufferedImage makeImage(int[] data, int w, int h, int c, boolean interleaved, boolean signed)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.c
- Number of channels.interleaved
- If set, the channels are assumed to be interleaved;
otherwise they are assumed to be sequential.
For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved,
while "RRR...GGG...BBB..." is sequential.signed
- Whether the int values should be treated as signed
(-2^31 to 2^31-1) instead of unsigned (0 to 2^32-1).public static BufferedImage makeImage(float[] data, int w, int h, int c, boolean interleaved)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.c
- Number of channels.interleaved
- If set, the channels are assumed to be interleaved;
otherwise they are assumed to be sequential.
For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved,
while "RRR...GGG...BBB..." is sequential.public static BufferedImage makeImage(double[] data, int w, int h, int c, boolean interleaved)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.c
- Number of channels.interleaved
- If set, the channels are assumed to be interleaved;
otherwise they are assumed to be sequential.
For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved,
while "RRR...GGG...BBB..." is sequential.public static BufferedImage makeImage(byte[][] data, int w, int h, boolean signed)
data
- Array containing image data.
It is assumed that each channel corresponds to one element of the array.
For example, for RGB data, data[0] is R, data[1] is G, and data[2] is B.w
- Width of image plane.h
- Height of image plane.signed
- Whether the byte values should be treated as signed
(-128 to 127) instead of unsigned (0 to 255).public static BufferedImage makeImage(short[][] data, int w, int h, boolean signed)
data
- Array containing image data.
It is assumed that each channel corresponds to one element of the array.
For example, for RGB data, data[0] is R, data[1] is G, and data[2] is B.w
- Width of image plane.h
- Height of image plane.signed
- Whether the short values should be treated as signed
(-32768 to 32767) instead of unsigned (0 to 65535).public static BufferedImage makeImage(int[][] data, int w, int h, boolean signed)
data
- Array containing image data.
It is assumed that each channel corresponds to one element of the array.
For example, for RGB data, data[0] is R, data[1] is G, and data[2] is B.w
- Width of image plane.h
- Height of image plane.signed
- Whether the int values should be treated as signed
(-2^31 to 2^31-1) instead of unsigned (0 to 2^32-1).public static BufferedImage makeImage(float[][] data, int w, int h)
data
- Array containing image data.
It is assumed that each channel corresponds to one element of the array.
For example, for RGB data, data[0] is R, data[1] is G, and data[2] is B.w
- Width of image plane.h
- Height of image plane.public static BufferedImage makeImage(double[][] data, int w, int h)
data
- Array containing image data.
It is assumed that each channel corresponds to one element of the array.
For example, for RGB data, data[0] is R, data[1] is G, and data[2] is B.w
- Width of image plane.h
- Height of image plane.public static BufferedImage makeImage(byte[] data, boolean interleaved, MetadataRetrieve meta, int series) throws FormatException
data
- Array containing image data.interleaved
- If set, the channels are assumed to be interleaved;
otherwise they are assumed to be sequential.
For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved,
while "RRR...GGG...BBB..." is sequential.meta
- Metadata object containing dimensional parameters.series
- Relevant image series number of metadata object.FormatException
public static BufferedImage makeImage(byte[] data, int w, int h, int c, boolean interleaved, int bpp, boolean fp, boolean little, boolean signed)
data
- Array containing image data.w
- Width of image plane.h
- Height of image plane.c
- Number of channels.interleaved
- If set, the channels are assumed to be interleaved;
otherwise they are assumed to be sequential.
For example, for RGB data, the pattern "RGBRGBRGB..." is interleaved,
while "RRR...GGG...BBB..." is sequential.bpp
- Denotes the number of bytes in the returned primitive type
(e.g. if bpp == 2, we should return an array of type short).fp
- If set and bpp == 4 or bpp == 8, then return floats or doubles.little
- Whether byte array is in little-endian order.signed
- Whether the data values should be treated as signed
instead of unsigned.public static BufferedImage makeImage(byte[][] data, int w, int h, int bpp, boolean fp, boolean little, boolean signed)
data
- Array containing image data, one channel per element.w
- Width of image plane.h
- Height of image plane.bpp
- Denotes the number of bytes in the returned primitive type
(e.g. if bpp == 2, we should return an array of type short).fp
- If set and bpp == 4 or bpp == 8, then return floats or doubles.little
- Whether byte array is in little-endian order.signed
- Whether the data values should be treated as signed
instead of unsigned.public static BufferedImage makeRGBImage(byte[] data, int c, int w, int h, boolean interleaved)
public static BufferedImage makeRGBImage(byte[][] data, int w, int h)
public static BufferedImage blankImage(int w, int h, int c, int type)
w
- Width of image plane.h
- Height of image plane.c
- Number of channels.type
- One of the following types:public static BufferedImage constructImage(int c, int type, int w, int h, boolean interleaved, boolean banded, DataBuffer buffer)
public static BufferedImage constructImage(int c, int type, int w, int h, boolean interleaved, boolean banded, DataBuffer buffer, ColorModel colorModel)
public static BufferedImage openImage(byte[] buf, IFormatReader r, int w, int h) throws FormatException, IOException
FormatException
IOException
public static Object getPixels(BufferedImage image)
public static Object getPixels(BufferedImage image, int x, int y, int w, int h)
public static Object getPixels(WritableRaster raster)
public static Object getPixels(WritableRaster raster, int x, int y, int w, int h)
public static byte[][] getBytes(BufferedImage image)
public static byte[][] getBytes(WritableRaster r)
public static byte[][] getBytes(WritableRaster r, int x, int y, int w, int h)
public static short[][] getShorts(BufferedImage image)
public static short[][] getShorts(WritableRaster r)
public static short[][] getShorts(WritableRaster r, int x, int y, int w, int h)
public static int[][] getInts(BufferedImage image)
public static int[][] getInts(WritableRaster r)
public static int[][] getInts(WritableRaster r, int x, int y, int w, int h)
public static float[][] getFloats(BufferedImage image)
public static float[][] getFloats(WritableRaster r)
public static float[][] getFloats(WritableRaster r, int x, int y, int w, int h)
public static double[][] getDoubles(BufferedImage image)
public static double[][] getDoubles(WritableRaster r)
public static double[][] getDoubles(WritableRaster r, int x, int y, int w, int h)
private static boolean canUseBankDataDirectly(WritableRaster r, int transferType, Class<? extends DataBuffer> dataBufferClass)
public static byte[][] getPixelBytes(BufferedImage img, boolean little)
public static byte[][] getPixelBytes(WritableRaster r, boolean little)
public static byte[][] getPixelBytes(BufferedImage img, boolean little, int x, int y, int w, int h)
public static byte[][] getPixelBytes(WritableRaster r, boolean little, int x, int y, int w, int h)
public static int getPixelType(BufferedImage image)
public static BufferedImage convertRenderedImage(RenderedImage img)
public static byte[] getBytes(BufferedImage img, boolean separated)
public static BufferedImage makeUnsigned(BufferedImage img)
public static BufferedImage getSubimage(BufferedImage image, boolean littleEndian, int x, int y, int w, int h)
public static BufferedImage[] splitChannels(BufferedImage image)
public static BufferedImage mergeChannels(BufferedImage[] images)
public static BufferedImage padImage(BufferedImage img, int width, int height)
public static BufferedImage autoscale(BufferedImage img)
public static BufferedImage autoscale(BufferedImage img, int min, int max)
public static BufferedImage copyScaled(BufferedImage source, BufferedImage target, Object hint)
public static BufferedImage scale2D(BufferedImage image, int width, int height, Object hint, GraphicsConfiguration gc)
public static BufferedImage scale2D(BufferedImage image, int width, int height, Object hint, ColorModel cm)
public static Image scaleAWT(BufferedImage source, int width, int height, int hint)
public static BufferedImage scale(BufferedImage source, int width, int height, boolean pad)
public static BufferedImage makeBuffered(Image image)
public static BufferedImage makeBuffered(Image image, ColorModel cm)
public static boolean loadImage(Image image)
public static Dimension getSize(Image image)
public static BufferedImage makeCompatible(BufferedImage image, GraphicsConfiguration gc)
public static GraphicsConfiguration getDefaultConfiguration()
public static ColorSpace makeColorSpace(int c)
public static ColorModel makeColorModel(int c, int dataType)
public static BufferedImage indexedToRGB(BufferedImage img, boolean le)
public static byte[][] get8BitLookupTable(ColorModel model)
public static short[][] getLookupTable(ColorModel model)
Copyright © 2015 Open Microscopy Environment