FileDocCategorySizeDatePackage
ImageTypeSpecifier.javaAPI DocJava SE 5 API47643Fri Aug 26 14:57:30 BST 2005javax.imageio

ImageTypeSpecifier

public class ImageTypeSpecifier extends Object
A class that allows the format of an image (in particular, its SampleModel and ColorModel) to be specified in a convenient manner.
version
0.5

Fields Summary
protected ColorModel
colorModel
The ColorModel to be used as a prototype.
protected SampleModel
sampleModel
A SampleModel to be used as a prototype.
private static ImageTypeSpecifier[]
BISpecifier
Cached specifiers for all of the standard BufferedImage types.
Constructors Summary
private ImageTypeSpecifier()
A constructor to be used by inner subclasses only.

        ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        
        BISpecifier =
            new ImageTypeSpecifier[BufferedImage.TYPE_BYTE_INDEXED + 1];

        BISpecifier[BufferedImage.TYPE_CUSTOM] = null;

        BISpecifier[BufferedImage.TYPE_INT_RGB] =
            createPacked(sRGB,
                         0x00ff0000,
                         0x0000ff00,
                         0x000000ff,
                         0x0,
                         DataBuffer.TYPE_INT,
                         false);

        BISpecifier[BufferedImage.TYPE_INT_ARGB] =
            createPacked(sRGB,
                         0x00ff0000,
                         0x0000ff00,
                         0x000000ff,
                         0xff000000,
                         DataBuffer.TYPE_INT,
                         false);

        BISpecifier[BufferedImage.TYPE_INT_ARGB_PRE] =
            createPacked(sRGB,
                         0x00ff0000,
                         0x0000ff00,
                         0x000000ff,
                         0xff000000,
                         DataBuffer.TYPE_INT,
                         true);

        BISpecifier[BufferedImage.TYPE_INT_BGR] =
            createPacked(sRGB,
                         0x000000ff,
                         0x0000ff00,
                         0x00ff0000,
                         0x0,
                         DataBuffer.TYPE_INT,
                         false);

        int[] bOffsRGB = { 2, 1, 0 };
        BISpecifier[BufferedImage.TYPE_3BYTE_BGR] =
            createInterleaved(sRGB,
                              bOffsRGB,
                              DataBuffer.TYPE_BYTE,
                              false,
                              false);

        int[] bOffsABGR = { 3, 2, 1, 0 };
        BISpecifier[BufferedImage.TYPE_4BYTE_ABGR] =
            createInterleaved(sRGB,
                              bOffsABGR,
                              DataBuffer.TYPE_BYTE,
                              true,
                              false);
        
        BISpecifier[BufferedImage.TYPE_4BYTE_ABGR_PRE] =
            createInterleaved(sRGB,
                              bOffsABGR,
                              DataBuffer.TYPE_BYTE,
                              true,
                              true);

        BISpecifier[BufferedImage.TYPE_USHORT_565_RGB] =
            createPacked(sRGB,
                         0xF800,
                         0x07E0,
                         0x001F,
                         0x0,
                         DataBuffer.TYPE_USHORT,
                         false);

        BISpecifier[BufferedImage.TYPE_USHORT_555_RGB] =
            createPacked(sRGB,
                         0x7C00,
                         0x03E0,
                         0x001F,
                         0x0,
                         DataBuffer.TYPE_USHORT,
                         false);

        BISpecifier[BufferedImage.TYPE_BYTE_GRAY] =
            createGrayscale(8,
                            DataBuffer.TYPE_BYTE,
                            false);
        
        BISpecifier[BufferedImage.TYPE_USHORT_GRAY] =
            createGrayscale(16,
                            DataBuffer.TYPE_USHORT,
                            false);

        BISpecifier[BufferedImage.TYPE_BYTE_BINARY] =
            createGrayscale(1,
                            DataBuffer.TYPE_BYTE,
                            false);

        BufferedImage bi =
            new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_INDEXED);
        IndexColorModel icm = (IndexColorModel)bi.getColorModel();
        int mapSize = icm.getMapSize();
        byte[] redLUT = new byte[mapSize];
        byte[] greenLUT = new byte[mapSize];
        byte[] blueLUT = new byte[mapSize];
        byte[] alphaLUT = new byte[mapSize];

        icm.getReds(redLUT);
        icm.getGreens(greenLUT);
        icm.getBlues(blueLUT);
        icm.getAlphas(alphaLUT);

        BISpecifier[BufferedImage.TYPE_BYTE_INDEXED] =
            createIndexed(redLUT, greenLUT, blueLUT, alphaLUT,
                          8,
                          DataBuffer.TYPE_BYTE);
    
public ImageTypeSpecifier(ColorModel colorModel, SampleModel sampleModel)
Constructs an ImageTypeSpecifier directly from a ColorModel and a SampleModel. It is the caller's responsibility to supply compatible parameters.

param
colorModel a ColorModel.
param
sampleModel a SampleModel.
exception
IllegalArgumentException if either parameter is null.
exception
IllegalArgumentException if sampleModel is not compatible with colorModel.

        if (colorModel == null) {
            throw new IllegalArgumentException("colorModel == null!");
        }
        if (sampleModel == null) {
            throw new IllegalArgumentException("sampleModel == null!");
        }
        if (!colorModel.isCompatibleSampleModel(sampleModel)) {
            throw new IllegalArgumentException
                ("sampleModel is incompatible with colorModel!");
        }
        this.colorModel = colorModel;
        this.sampleModel = sampleModel;
    
public ImageTypeSpecifier(RenderedImage image)
Constructs an ImageTypeSpecifier from a RenderedImage. If a BufferedImage is being used, one of the factory methods createFromRenderedImage or createFromBufferedImageType should be used instead in order to get a more accurate result.

param
image a RenderedImage.
exception
IllegalArgumentException if the argument is null.

        if (image == null) {
            throw new IllegalArgumentException("image == null!");
        }
        colorModel = image.getColorModel();
        sampleModel = image.getSampleModel();
    
Methods Summary
public static javax.imageio.ImageTypeSpecifiercreateBanded(java.awt.color.ColorSpace colorSpace, int[] bankIndices, int[] bandOffsets, int dataType, boolean hasAlpha, boolean isAlphaPremultiplied)
Returns a specifier for a banded image format that will use a ComponentColorModel and a BandedSampleModel to store each channel in a separate array.

param
colorSpace the desired ColorSpace.
param
bankIndices an array of ints indicating the bank in which each band will be stored.
param
bandOffsets an array of ints indicating the starting offset of each band within its bank.
param
dataType the desired data type, as one of the enumerations from the DataBuffer class.
param
hasAlpha true if an alpha channel is desired.
param
isAlphaPremultiplied true if the color channels will be premultipled by the alpha channel.
return
an ImageTypeSpecifier with the desired characteristics.
exception
IllegalArgumentException if colorSpace is null.
exception
IllegalArgumentException if bankIndices is null.
exception
IllegalArgumentException if bandOffsets is null.
exception
IllegalArgumentException if the lengths of bankIndices and bandOffsets differ.
exception
IllegalArgumentException if bandOffsets.length does not equal the number of color space components, plus 1 if hasAlpha is true.
exception
IllegalArgumentException if dataType is not one of the legal DataBuffer.TYPE_* constants.

        return new ImageTypeSpecifier.Banded(colorSpace,
                                             bankIndices,
                                             bandOffsets,
                                             dataType,
                                             hasAlpha,
                                             isAlphaPremultiplied);
    
public java.awt.image.BufferedImagecreateBufferedImage(int width, int height)
Creates a BufferedImage with a given width and height according to the specification embodied in this object.

param
width the desired width of the returned BufferedImage.
param
height the desired height of the returned BufferedImage.
return
a new BufferedImage
exception
IllegalArgumentException if either width or height are negative or zero.
exception
IllegalArgumentException if the product of width and height is greater than Integer.MAX_VALUE, or if the number of array elements needed to store the image is greater than Integer.MAX_VALUE.

 
        try {
            SampleModel sampleModel = getSampleModel(width, height);
            WritableRaster raster =
                Raster.createWritableRaster(sampleModel,
                                            new Point(0, 0));
            return new BufferedImage(colorModel, raster,
                                     colorModel.isAlphaPremultiplied(),
                                     new Hashtable());
        } catch (NegativeArraySizeException e) {
            // Exception most likely thrown from a DataBuffer constructor
            throw new IllegalArgumentException
                ("Array size > Integer.MAX_VALUE!");
        }
    
static java.awt.image.ColorModelcreateComponentCM(java.awt.color.ColorSpace colorSpace, int numBands, int dataType, boolean hasAlpha, boolean isAlphaPremultiplied)

        int transparency =
            hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE;
        
        int[] numBits = new int[numBands];
        int bits = DataBuffer.getDataTypeSize(dataType);

        for (int i = 0; i < numBands; i++) {
            numBits[i] = bits;
        }
        
        return new ComponentColorModel(colorSpace,
                                       numBits,
                                       hasAlpha,
                                       isAlphaPremultiplied,
                                       transparency,
                                       dataType);
    
public static javax.imageio.ImageTypeSpecifiercreateFromBufferedImageType(int bufferedImageType)
Returns an ImageTypeSpecifier that encodes one of the standard BufferedImage types (other than TYPE_CUSTOM).

param
bufferedImageType an int representing one of the standard BufferedImage types.
return
an ImageTypeSpecifier with the desired characteristics.
exception
IllegalArgumentException if bufferedImageType is not one of the standard types, or is equal to TYPE_CUSTOM.
see
java.awt.image.BufferedImage
see
java.awt.image.BufferedImage#TYPE_INT_RGB
see
java.awt.image.BufferedImage#TYPE_INT_ARGB
see
java.awt.image.BufferedImage#TYPE_INT_ARGB_PRE
see
java.awt.image.BufferedImage#TYPE_INT_BGR
see
java.awt.image.BufferedImage#TYPE_3BYTE_BGR
see
java.awt.image.BufferedImage#TYPE_4BYTE_ABGR
see
java.awt.image.BufferedImage#TYPE_4BYTE_ABGR_PRE
see
java.awt.image.BufferedImage#TYPE_USHORT_565_RGB
see
java.awt.image.BufferedImage#TYPE_USHORT_555_RGB
see
java.awt.image.BufferedImage#TYPE_BYTE_GRAY
see
java.awt.image.BufferedImage#TYPE_USHORT_GRAY
see
java.awt.image.BufferedImage#TYPE_BYTE_BINARY
see
java.awt.image.BufferedImage#TYPE_BYTE_INDEXED

        if (bufferedImageType >= BufferedImage.TYPE_INT_RGB &&
            bufferedImageType <= BufferedImage.TYPE_BYTE_INDEXED) {
            return BISpecifier[bufferedImageType];
        } else if (bufferedImageType == BufferedImage.TYPE_CUSTOM) {
            throw new IllegalArgumentException("Cannot create from TYPE_CUSTOM!");
        } else {
            throw new IllegalArgumentException("Invalid BufferedImage type!");
        }
    
public static javax.imageio.ImageTypeSpecifiercreateFromRenderedImage(java.awt.image.RenderedImage image)
Returns an ImageTypeSpecifier that encodes the layout of a RenderedImage (which may be a BufferedImage).

param
image a RenderedImage.
return
an ImageTypeSpecifier with the desired characteristics.
exception
IllegalArgumentException if image is null.

        if (image == null) {
            throw new IllegalArgumentException("image == null!");
        }

        if (image instanceof BufferedImage) {
            int bufferedImageType = ((BufferedImage)image).getType();
            if (bufferedImageType != BufferedImage.TYPE_CUSTOM) {
                return BISpecifier[bufferedImageType];
            }
        }
        
        return new ImageTypeSpecifier(image);
    
public static javax.imageio.ImageTypeSpecifiercreateGrayscale(int bits, int dataType, boolean isSigned)
Returns a specifier for a grayscale image format that will pack pixels of the given bit depth into array elements of the specified data type.

param
bits the number of bits per gray value (1, 2, 4, 8, or 16).
param
dataType the desired data type, as one of the enumerations from the DataBuffer class.
param
isSigned true if negative values are to be represented.
return
an ImageTypeSpecifier with the desired characteristics.
exception
IllegalArgumentException if bits is not one of 1, 2, 4, 8, or 16.
exception
IllegalArgumentException if dataType is not one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_SHORT, or DataBuffer.TYPE_USHORT.
exception
IllegalArgumentException if bits is larger than the bit size of the given dataType.

        return new ImageTypeSpecifier.Grayscale(bits,
                                                dataType,
                                                isSigned,
                                                false,
                                                false);
    
public static javax.imageio.ImageTypeSpecifiercreateGrayscale(int bits, int dataType, boolean isSigned, boolean isAlphaPremultiplied)
Returns a specifier for a grayscale plus alpha image format that will pack pixels of the given bit depth into array elements of the specified data type.

param
bits the number of bits per gray value (1, 2, 4, 8, or 16).
param
dataType the desired data type, as one of the enumerations from the DataBuffer class.
param
isSigned true if negative values are to be represented.
param
isAlphaPremultiplied true if the luminance channel will be premultipled by the alpha channel.
return
an ImageTypeSpecifier with the desired characteristics.
exception
IllegalArgumentException if bits is not one of 1, 2, 4, 8, or 16.
exception
IllegalArgumentException if dataType is not one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_SHORT, or DataBuffer.TYPE_USHORT.
exception
IllegalArgumentException if bits is larger than the bit size of the given dataType.

        return new ImageTypeSpecifier.Grayscale(bits,
                                                dataType,
                                                isSigned,
                                                true,
                                                isAlphaPremultiplied);
    
public static javax.imageio.ImageTypeSpecifiercreateIndexed(byte[] redLUT, byte[] greenLUT, byte[] blueLUT, byte[] alphaLUT, int bits, int dataType)
Returns a specifier for an indexed-color image format that will pack index values of the given bit depth into array elements of the specified data type.

param
redLUT an array of bytes containing the red values for each index.
param
greenLUT an array of bytes containing * the green values for each index.
param
blueLUT an array of bytes containing the blue values for each index.
param
alphaLUT an array of bytes containing the alpha values for each index, or null to create a fully opaque LUT.
param
bits the number of bits in each index.
param
dataType the desired output type, as one of the enumerations from the DataBuffer class.
return
an ImageTypeSpecifier with the desired characteristics.
exception
IllegalArgumentException if redLUT is null.
exception
IllegalArgumentException if greenLUT is null.
exception
IllegalArgumentException if blueLUT is null.
exception
IllegalArgumentException if bits is not one of 1, 2, 4, 8, or 16.
exception
IllegalArgumentException if the non-null LUT parameters do not have lengths of exactly 1 << bits.
exception
IllegalArgumentException if dataType is not one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_SHORT, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT.
exception
IllegalArgumentException if bits is larger than the bit size of the given dataType.

        return new ImageTypeSpecifier.Indexed(redLUT,
                                              greenLUT,
                                              blueLUT,
                                              alphaLUT,
                                              bits,
                                              dataType);
    
public static javax.imageio.ImageTypeSpecifiercreateInterleaved(java.awt.color.ColorSpace colorSpace, int[] bandOffsets, int dataType, boolean hasAlpha, boolean isAlphaPremultiplied)
Returns a specifier for an interleaved image format that will use a ComponentColorModel and a PixelInterleavedSampleModel to store each pixel component in a separate byte, short, or int.

param
colorSpace the desired ColorSpace.
param
bandOffsets an array of ints indicating the offsets for each band.
param
dataType the desired data type, as one of the enumerations from the DataBuffer class.
param
hasAlpha true if an alpha channel is desired.
param
isAlphaPremultiplied true if the color channels will be premultipled by the alpha channel.
return
an ImageTypeSpecifier with the desired characteristics.
exception
IllegalArgumentException if colorSpace is null.
exception
IllegalArgumentException if bandOffsets is null.
exception
IllegalArgumentException if dataType is not one of the legal DataBuffer.TYPE_* constants.
exception
IllegalArgumentException if bandOffsets.length does not equal the number of color space components, plus 1 if hasAlpha is true.

        return new ImageTypeSpecifier.Interleaved(colorSpace,
                                                  bandOffsets,
                                                  dataType,
                                                  hasAlpha,
                                                  isAlphaPremultiplied);
    
public static javax.imageio.ImageTypeSpecifiercreatePacked(java.awt.color.ColorSpace colorSpace, int redMask, int greenMask, int blueMask, int alphaMask, int transferType, boolean isAlphaPremultiplied)
Returns a specifier for a packed image format that will use a DirectColorModel and a packed SampleModel to store each pixel packed into in a single byte, short, or int.

param
colorSpace the desired ColorSpace.
param
redMask a contiguous mask indicated the position of the red channel.
param
greenMask a contiguous mask indicated the position of the green channel.
param
blueMask a contiguous mask indicated the position of the blue channel.
param
alphaMask a contiguous mask indicated the position of the alpha channel.
param
transferType the desired SampleModel transfer type.
param
isAlphaPremultiplied true if the color channels will be premultipled by the alpha channel.
return
an ImageTypeSpecifier with the desired characteristics.
exception
IllegalArgumentException if colorSpace is null.
exception
IllegalArgumentException if colorSpace is not of type TYPE_RGB.
exception
IllegalArgumentException if no mask has at least 1 bit set.
exception
IllegalArgumentException if transferType if not one of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, or DataBuffer.TYPE_INT.

        return new ImageTypeSpecifier.Packed(colorSpace,
                                             redMask,
                                             greenMask,
                                             blueMask,
                                             alphaMask, // 0 if no alpha
                                             transferType,
                                             isAlphaPremultiplied);
    
public booleanequals(java.lang.Object o)
Returns true if the given Object is an ImageTypeSpecifier and has a SampleModel and ColorModel that are equal to those of this object.

param
o the Object to be compared for equality.
return
true if the given object is an equivalent ImageTypeSpecifier.

        if ((o == null) || !(o instanceof ImageTypeSpecifier)) {
            return false;
        }

        ImageTypeSpecifier that = (ImageTypeSpecifier)o;
        return (colorModel.equals(that.colorModel)) &&
            (sampleModel.equals(that.sampleModel));
    
public intgetBitsPerBand(int band)
Return the number of bits used to represent samples of the given band.

param
band the index of the band to be queried, as an int.
return
an int specifying a number of bits.
exception
IllegalArgumentException if band is negative or greater than the largest band index.

        if (band < 0 | band >= getNumBands()) {
            throw new IllegalArgumentException("band out of range!");
        }
        return sampleModel.getSampleSize(band);
    
public intgetBufferedImageType()
Returns an int containing one of the enumerated constant values describing image formats from BufferedImage.

return
an int representing a BufferedImage type.
see
java.awt.image.BufferedImage
see
java.awt.image.BufferedImage#TYPE_CUSTOM
see
java.awt.image.BufferedImage#TYPE_INT_RGB
see
java.awt.image.BufferedImage#TYPE_INT_ARGB
see
java.awt.image.BufferedImage#TYPE_INT_ARGB_PRE
see
java.awt.image.BufferedImage#TYPE_INT_BGR
see
java.awt.image.BufferedImage#TYPE_3BYTE_BGR
see
java.awt.image.BufferedImage#TYPE_4BYTE_ABGR
see
java.awt.image.BufferedImage#TYPE_4BYTE_ABGR_PRE
see
java.awt.image.BufferedImage#TYPE_USHORT_565_RGB
see
java.awt.image.BufferedImage#TYPE_USHORT_555_RGB
see
java.awt.image.BufferedImage#TYPE_BYTE_GRAY
see
java.awt.image.BufferedImage#TYPE_USHORT_GRAY
see
java.awt.image.BufferedImage#TYPE_BYTE_BINARY
see
java.awt.image.BufferedImage#TYPE_BYTE_INDEXED

        BufferedImage bi = createBufferedImage(1, 1);
        return bi.getType();
    
public java.awt.image.ColorModelgetColorModel()
Returns the ColorModel specified by this object.

return
a ColorModel.

        return colorModel;
    
public intgetNumBands()
Return the number of bands specified by this object. This is the same value as returned by SampleModel.getNumBands

return
the number of bands in the image.

        return sampleModel.getNumBands();
    
public intgetNumComponents()
Return the number of color components specified by this object. This is the same value as returned by ColorModel.getNumComponents

return
the number of components in the image.

        return colorModel.getNumComponents();
    
public java.awt.image.SampleModelgetSampleModel()
Returns a SampleModel based on the settings encapsulated within this object. The width and height of the SampleModel will be set to arbitrary values.

return
a SampleModel with arbitrary dimensions.

        return sampleModel;
    
public java.awt.image.SampleModelgetSampleModel(int width, int height)
Returns a SampleModel based on the settings encapsulated within this object. The width and height of the SampleModel will be set to the supplied values.

param
width the desired width of the returned SampleModel.
param
height the desired height of the returned SampleModel.
return
a SampleModel with the given dimensions.
exception
IllegalArgumentException if either width or height are negative or zero.
exception
IllegalArgumentException if the product of width and height is greater than Integer.MAX_VALUE

        if ((long)width*height > Integer.MAX_VALUE) {
            throw new IllegalArgumentException
                ("width*height > Integer.MAX_VALUE!");
        }
        return sampleModel.createCompatibleSampleModel(width, height);
    
public inthashCode()
Returns the hash code for this ImageTypeSpecifier.

return
a hash code for this ImageTypeSpecifier

        return (9 * colorModel.hashCode()) + (14 * sampleModel.hashCode());