ImageTypeSpecifierpublic 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. |
Fields Summary |
---|
protected ColorModel | colorModelThe ColorModel to be used as a prototype. | protected SampleModel | sampleModelA SampleModel to be used as a prototype. | private static ImageTypeSpecifier[] | BISpecifierCached 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.
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.
if (image == null) {
throw new IllegalArgumentException("image == null!");
}
colorModel = image.getColorModel();
sampleModel = image.getSampleModel();
|
Methods Summary |
---|
public static javax.imageio.ImageTypeSpecifier | createBanded(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.
return new ImageTypeSpecifier.Banded(colorSpace,
bankIndices,
bandOffsets,
dataType,
hasAlpha,
isAlphaPremultiplied);
| public java.awt.image.BufferedImage | createBufferedImage(int width, int height)Creates a BufferedImage with a given width and
height according to the specification embodied in this object.
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.ColorModel | createComponentCM(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.ImageTypeSpecifier | createFromBufferedImageType(int bufferedImageType)Returns an ImageTypeSpecifier that encodes
one of the standard BufferedImage types
(other than TYPE_CUSTOM ).
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.ImageTypeSpecifier | createFromRenderedImage(java.awt.image.RenderedImage image)Returns an ImageTypeSpecifier that encodes the
layout of a RenderedImage (which may be a
BufferedImage ).
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.ImageTypeSpecifier | createGrayscale(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.
return new ImageTypeSpecifier.Grayscale(bits,
dataType,
isSigned,
false,
false);
| public static javax.imageio.ImageTypeSpecifier | createGrayscale(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.
return new ImageTypeSpecifier.Grayscale(bits,
dataType,
isSigned,
true,
isAlphaPremultiplied);
| public static javax.imageio.ImageTypeSpecifier | createIndexed(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.
return new ImageTypeSpecifier.Indexed(redLUT,
greenLUT,
blueLUT,
alphaLUT,
bits,
dataType);
| public static javax.imageio.ImageTypeSpecifier | createInterleaved(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.
return new ImageTypeSpecifier.Interleaved(colorSpace,
bandOffsets,
dataType,
hasAlpha,
isAlphaPremultiplied);
| public static javax.imageio.ImageTypeSpecifier | createPacked(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.
return new ImageTypeSpecifier.Packed(colorSpace,
redMask,
greenMask,
blueMask,
alphaMask, // 0 if no alpha
transferType,
isAlphaPremultiplied);
| public boolean | equals(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.
if ((o == null) || !(o instanceof ImageTypeSpecifier)) {
return false;
}
ImageTypeSpecifier that = (ImageTypeSpecifier)o;
return (colorModel.equals(that.colorModel)) &&
(sampleModel.equals(that.sampleModel));
| public int | getBitsPerBand(int band)Return the number of bits used to represent samples of the given band.
if (band < 0 | band >= getNumBands()) {
throw new IllegalArgumentException("band out of range!");
}
return sampleModel.getSampleSize(band);
| public int | getBufferedImageType()Returns an int containing one of the enumerated constant values
describing image formats from BufferedImage .
BufferedImage bi = createBufferedImage(1, 1);
return bi.getType();
| public java.awt.image.ColorModel | getColorModel()Returns the ColorModel specified by this object.
return colorModel;
| public int | getNumBands()Return the number of bands
specified by this object. This is the same value as returned by
SampleModel.getNumBands
return sampleModel.getNumBands();
| public int | getNumComponents()Return the number of color components
specified by this object. This is the same value as returned by
ColorModel.getNumComponents
return colorModel.getNumComponents();
| public java.awt.image.SampleModel | getSampleModel()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 sampleModel;
| public java.awt.image.SampleModel | getSampleModel(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.
if ((long)width*height > Integer.MAX_VALUE) {
throw new IllegalArgumentException
("width*height > Integer.MAX_VALUE!");
}
return sampleModel.createCompatibleSampleModel(width, height);
| public int | hashCode()Returns the hash code for this ImageTypeSpecifier.
return (9 * colorModel.hashCode()) + (14 * sampleModel.hashCode());
|
|