Class AWTImageTools


  • public final class AWTImageTools
    extends Object
    A utility class with convenience methods for manipulating images in BufferedImage form. To work with images in primitive array form, use the ome.codecs.ImageTools class. Much code was stolen and adapted from DrLaszloJamf's posts on the Java forums.
    Author:
    Curtis Rueden ctrueden at wisc.edu
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static Component OBS
      ImageObserver for working with AWT images.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      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 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 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 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 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 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 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, 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)  
    • Field Detail

      • OBS

        protected static final Component OBS
        ImageObserver for working with AWT images.
    • Method Detail

      • makeImage

        public static BufferedImage makeImage​(byte[] data,
                                              int w,
                                              int h,
                                              boolean signed)
        Creates an image from the given single-channel byte data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(short[] data,
                                              int w,
                                              int h,
                                              boolean signed)
        Creates an image from the given single-channel short data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(int[] data,
                                              int w,
                                              int h,
                                              boolean signed)
        Creates an image from the given single-channel int data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(float[] data,
                                              int w,
                                              int h)
        Creates an image from the given single-channel float data.
        Parameters:
        data - Array containing image data.
        w - Width of image plane.
        h - Height of image plane.
      • makeImage

        public static BufferedImage makeImage​(double[] data,
                                              int w,
                                              int h)
        Creates an image from the given single-channel double data.
        Parameters:
        data - Array containing image data.
        w - Width of image plane.
        h - Height of image plane.
      • makeImage

        public static BufferedImage makeImage​(byte[] data,
                                              int w,
                                              int h,
                                              int c,
                                              boolean interleaved,
                                              boolean signed)
        Creates an image from the given byte data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(short[] data,
                                              int w,
                                              int h,
                                              int c,
                                              boolean interleaved,
                                              boolean signed)
        Creates an image from the given short data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(int[] data,
                                              int w,
                                              int h,
                                              int c,
                                              boolean interleaved,
                                              boolean signed)
        Creates an image from the given int data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(float[] data,
                                              int w,
                                              int h,
                                              int c,
                                              boolean interleaved)
        Creates an image from the given float data.
        Parameters:
        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.
      • makeImage

        public static BufferedImage makeImage​(double[] data,
                                              int w,
                                              int h,
                                              int c,
                                              boolean interleaved)
        Creates an image from the given double data.
        Parameters:
        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.
      • makeImage

        public static BufferedImage makeImage​(byte[][] data,
                                              int w,
                                              int h,
                                              boolean signed)
        Creates an image from the given byte data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(short[][] data,
                                              int w,
                                              int h,
                                              boolean signed)
        Creates an image from the given short data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(int[][] data,
                                              int w,
                                              int h,
                                              boolean signed)
        Creates an image from the given int data.
        Parameters:
        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).
      • makeImage

        public static BufferedImage makeImage​(float[][] data,
                                              int w,
                                              int h)
        Creates an image from the given single-precision floating point data.
        Parameters:
        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.
      • makeImage

        public static BufferedImage makeImage​(double[][] data,
                                              int w,
                                              int h)
        Creates an image from the given double-precision floating point data.
        Parameters:
        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.
      • makeImage

        public 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.
        Parameters:
        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.
      • makeImage

        public 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.
        Parameters:
        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.
      • makeRGBImage

        public static BufferedImage makeRGBImage​(byte[] data,
                                                 int c,
                                                 int w,
                                                 int h,
                                                 boolean interleaved)
      • makeRGBImage

        public static BufferedImage makeRGBImage​(byte[][] data,
                                                 int w,
                                                 int h)
      • constructImage

        public static BufferedImage constructImage​(int c,
                                                   int type,
                                                   int w,
                                                   int h,
                                                   boolean interleaved,
                                                   boolean banded,
                                                   DataBuffer buffer)
        Creates an image with the given DataBuffer.
      • constructImage

        public 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.
      • getPixels

        public static Object getPixels​(BufferedImage image)
        Gets the image's pixel data as arrays of primitives, one per channel. The returned type will be either byte[][], short[][], int[][], float[][] or double[][], depending on the image's transfer type.
      • getPixels

        public 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. The returned type will be either byte[][], short[][], int[][], float[][] or double[][], depending on the image's transfer type.
      • getPixels

        public static Object getPixels​(WritableRaster raster)
        Gets the raster's pixel data as arrays of primitives, one per channel. The returned type will be either byte[][], short[][], int[][], float[][] or double[][], depending on the raster's transfer type.
      • getPixels

        public 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. The returned type will be either byte[][], short[][], int[][], float[][] or double[][], depending on the raster's transfer type.
      • getBytes

        public static byte[][] getBytes​(BufferedImage image)
        Extracts pixel data as arrays of unsigned bytes, one per channel.
      • getBytes

        public static byte[][] getBytes​(WritableRaster r)
        Extracts pixel data as arrays of unsigned bytes, one per channel.
      • getBytes

        public static byte[][] getBytes​(WritableRaster r,
                                        int x,
                                        int y,
                                        int w,
                                        int h)
        Extracts pixel data as arrays of unsigned bytes, one per channel.
      • getShorts

        public static short[][] getShorts​(BufferedImage image)
        Extracts pixel data as arrays of unsigned shorts, one per channel.
      • getShorts

        public static short[][] getShorts​(WritableRaster r)
        Extracts pixel data as arrays of unsigned shorts, one per channel.
      • getShorts

        public static short[][] getShorts​(WritableRaster r,
                                          int x,
                                          int y,
                                          int w,
                                          int h)
        Extracts pixel data as arrays of unsigned shorts, one per channel.
      • getInts

        public static int[][] getInts​(BufferedImage image)
        Extracts pixel data as arrays of signed integers, one per channel.
      • getInts

        public static int[][] getInts​(WritableRaster r)
        Extracts pixel data as arrays of signed integers, one per channel.
      • getInts

        public static int[][] getInts​(WritableRaster r,
                                      int x,
                                      int y,
                                      int w,
                                      int h)
        Extracts pixel data as arrays of signed integers, one per channel.
      • getFloats

        public static float[][] getFloats​(BufferedImage image)
        Extracts pixel data as arrays of floats, one per channel.
      • getFloats

        public static float[][] getFloats​(WritableRaster r)
        Extracts pixel data as arrays of floats, one per channel.
      • getFloats

        public static float[][] getFloats​(WritableRaster r,
                                          int x,
                                          int y,
                                          int w,
                                          int h)
        Extracts pixel data as arrays of floats, one per channel.
      • getDoubles

        public static double[][] getDoubles​(BufferedImage image)
        Extracts pixel data as arrays of doubles, one per channel.
      • getDoubles

        public static double[][] getDoubles​(WritableRaster r)
        Extracts pixel data as arrays of doubles, one per channel.
      • getDoubles

        public static double[][] getDoubles​(WritableRaster r,
                                            int x,
                                            int y,
                                            int w,
                                            int h)
        Extracts pixel data as arrays of doubles, one per channel.
      • getPixelBytes

        public static byte[][] getPixelBytes​(BufferedImage img,
                                             boolean little)
        Return a 2D array of bytes representing the image. If the transfer type is something other than DataBuffer.TYPE_BYTE, then each pixel value is converted to the appropriate number of bytes. In other words, if we are given an image with 16-bit data, each channel of the resulting array will have width * height * 2 bytes.
      • getPixelBytes

        public static byte[][] getPixelBytes​(WritableRaster r,
                                             boolean little)
        Return a 2D array of bytes representing the image. If the transfer type is something other than DataBuffer.TYPE_BYTE, then each pixel value is converted to the appropriate number of bytes. In other words, if we are given an image with 16-bit data, each channel of the resulting array will have width * height * 2 bytes.
      • getPixelBytes

        public static byte[][] getPixelBytes​(BufferedImage img,
                                             boolean little,
                                             int x,
                                             int y,
                                             int w,
                                             int h)
        Return a 2D array of bytes representing the image. If the transfer type is something other than DataBuffer.TYPE_BYTE, then each pixel value is converted to the appropriate number of bytes. In other words, if we are given an image with 16-bit data, each channel of the resulting array will have width * height * 2 bytes.
      • getPixelBytes

        public static byte[][] getPixelBytes​(WritableRaster r,
                                             boolean little,
                                             int x,
                                             int y,
                                             int w,
                                             int h)
        Return a 2D array of bytes representing the image. If the transfer type is something other than DataBuffer.TYPE_BYTE, then each pixel value is converted to the appropriate number of bytes. In other words, if we are given an image with 16-bit data, each channel of the resulting array will have width * height * 2 bytes.
      • getBytes

        public static byte[] getBytes​(BufferedImage img,
                                      boolean separated)
        Get the bytes from an image, merging the channels as necessary.
      • makeColorSpace

        public static ColorSpace makeColorSpace​(int c)
        Gets a color space for the given number of color components.
      • makeColorModel

        public static ColorModel makeColorModel​(int c,
                                                int dataType)
        Gets a color model for the given number of color components.