下面列出了java.awt.image.IndexColorModel#getReds() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void write(ImageOutputStream ios,
JPEGImageWriter writer) throws IOException {
super.write(ios, writer); // width and height
// Write the palette (must be 768 bytes)
byte [] palette = new byte[768];
IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
byte [] reds = new byte [256];
byte [] greens = new byte [256];
byte [] blues = new byte [256];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
for (int i = 0; i < 256; i++) {
palette[i*3] = reds[i];
palette[i*3+1] = greens[i];
palette[i*3+2] = blues[i];
}
ios.write(palette);
writePixels(ios, writer);
}
static IndexColorModel createIndexColorModel() {
BufferedImage ex = new BufferedImage(1, 1,
BufferedImage.TYPE_BYTE_INDEXED);
IndexColorModel icm = (IndexColorModel) ex.getColorModel();
int SIZE = 256;
byte[] r = new byte[SIZE];
byte[] g = new byte[SIZE];
byte[] b = new byte[SIZE];
byte[] a = new byte[SIZE];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
java.util.Arrays.fill(a, (byte) 255);
r[0] = g[0] = b[0] = a[0] = 0; // transparent
return new IndexColorModel(8, SIZE, r, g, b, a);
}
private void writePLTE() throws IOException {
if (!(getColorModel() instanceof IndexColorModel)) return;
final IndexColorModel model = (IndexColorModel) getColorModel();
final byte[][] lut = new byte[3][256];
model.getReds(lut[0]);
model.getGreens(lut[1]);
model.getBlues(lut[2]);
getHandle().writeInt(768);
final byte[] b = new byte[772];
b[0] = 'P';
b[1] = 'L';
b[2] = 'T';
b[3] = 'E';
for (int i = 0; i < lut[0].length; i++) {
for (int j = 0; j < lut.length; j++) {
b[i * lut.length + j + 4] = lut[j][i];
}
}
getHandle().write(b);
getHandle().writeInt(crc(b));
}
/**
* Returns an index color model with specified opacities (alphas).
* @param icm an index color model from which to copy RGBs.
* @param alpha array of opacities in the range [0.0,1.0].
* @return the index color model with alphas.
*/
public static IndexColorModel setAlpha(IndexColorModel icm, float[] alpha) {
int bits = icm.getPixelSize();
int size = icm.getMapSize();
byte[] r = new byte[size];
byte[] g = new byte[size];
byte[] b = new byte[size];
byte[] a = new byte[size];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
int n = min(size,alpha.length);
for (int i=0; i<n; ++i)
a[i] = (byte)(255.0f*alpha[i]+0.5f);
return new IndexColorModel(bits,size,r,g,b,a);
}
void write(ImageOutputStream ios,
JPEGImageWriter writer) throws IOException {
super.write(ios, writer); // width and height
// Write the palette (must be 768 bytes)
byte [] palette = new byte[768];
IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
byte [] reds = new byte [256];
byte [] greens = new byte [256];
byte [] blues = new byte [256];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
for (int i = 0; i < 256; i++) {
palette[i*3] = reds[i];
palette[i*3+1] = greens[i];
palette[i*3+2] = blues[i];
}
ios.write(palette);
writePixels(ios, writer);
}
private static synchronized byte[] getDefaultPalette() {
if (defaultPalette == null) {
BufferedImage img = new BufferedImage(1, 1,
BufferedImage.TYPE_BYTE_INDEXED);
IndexColorModel icm = (IndexColorModel) img.getColorModel();
final int size = icm.getMapSize();
byte[] r = new byte[size];
byte[] g = new byte[size];
byte[] b = new byte[size];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
defaultPalette = new byte[size * 3];
for (int i = 0; i < size; i++) {
defaultPalette[3 * i + 0] = r[i];
defaultPalette[3 * i + 1] = g[i];
defaultPalette[3 * i + 2] = b[i];
}
}
return defaultPalette;
}
void write(ImageOutputStream ios,
JPEGImageWriter writer) throws IOException {
super.write(ios, writer); // width and height
// Write the palette (must be 768 bytes)
byte [] palette = new byte[768];
IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
byte [] reds = new byte [256];
byte [] greens = new byte [256];
byte [] blues = new byte [256];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
for (int i = 0; i < 256; i++) {
palette[i*3] = reds[i];
palette[i*3+1] = greens[i];
palette[i*3+2] = blues[i];
}
ios.write(palette);
writePixels(ios, writer);
}
void write(ImageOutputStream ios,
JPEGImageWriter writer) throws IOException {
super.write(ios, writer); // width and height
// Write the palette (must be 768 bytes)
byte [] palette = new byte[768];
IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
byte [] reds = new byte [256];
byte [] greens = new byte [256];
byte [] blues = new byte [256];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
for (int i = 0; i < 256; i++) {
palette[i*3] = reds[i];
palette[i*3+1] = greens[i];
palette[i*3+2] = blues[i];
}
ios.write(palette);
writePixels(ios, writer);
}
private static synchronized byte[] getDefaultPalette() {
if (defaultPalette == null) {
BufferedImage img = new BufferedImage(1, 1,
BufferedImage.TYPE_BYTE_INDEXED);
IndexColorModel icm = (IndexColorModel) img.getColorModel();
final int size = icm.getMapSize();
byte[] r = new byte[size];
byte[] g = new byte[size];
byte[] b = new byte[size];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
defaultPalette = new byte[size * 3];
for (int i = 0; i < size; i++) {
defaultPalette[3 * i + 0] = r[i];
defaultPalette[3 * i + 1] = g[i];
defaultPalette[3 * i + 2] = b[i];
}
}
return defaultPalette;
}
void write(ImageOutputStream ios,
JPEGImageWriter writer) throws IOException {
super.write(ios, writer); // width and height
// Write the palette (must be 768 bytes)
byte [] palette = new byte[768];
IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
byte [] reds = new byte [256];
byte [] greens = new byte [256];
byte [] blues = new byte [256];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
for (int i = 0; i < 256; i++) {
palette[i*3] = reds[i];
palette[i*3+1] = greens[i];
palette[i*3+2] = blues[i];
}
ios.write(palette);
writePixels(ios, writer);
}
private static synchronized byte[] getDefaultPalette() {
if (defaultPalette == null) {
BufferedImage img = new BufferedImage(1, 1,
BufferedImage.TYPE_BYTE_INDEXED);
IndexColorModel icm = (IndexColorModel) img.getColorModel();
final int size = icm.getMapSize();
byte[] r = new byte[size];
byte[] g = new byte[size];
byte[] b = new byte[size];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
defaultPalette = new byte[size * 3];
for (int i = 0; i < size; i++) {
defaultPalette[3 * i + 0] = r[i];
defaultPalette[3 * i + 1] = g[i];
defaultPalette[3 * i + 2] = b[i];
}
}
return defaultPalette;
}
void write(ImageOutputStream ios,
JPEGImageWriter writer) throws IOException {
super.write(ios, writer); // width and height
// Write the palette (must be 768 bytes)
byte [] palette = new byte[768];
IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
byte [] reds = new byte [256];
byte [] greens = new byte [256];
byte [] blues = new byte [256];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
for (int i = 0; i < 256; i++) {
palette[i*3] = reds[i];
palette[i*3+1] = greens[i];
palette[i*3+2] = blues[i];
}
ios.write(palette);
writePixels(ios, writer);
}
/**
* Returns an index color model with specified opacity (alpha).
* @param icm an index color model from which to copy RGBs.
* @param alpha opacity in the range [0.0,1.0].
* @return the index color model with alpha.
*/
public static IndexColorModel setAlpha(IndexColorModel icm, double alpha) {
int bits = icm.getPixelSize();
int size = icm.getMapSize();
byte[] r = new byte[size];
byte[] g = new byte[size];
byte[] b = new byte[size];
byte[] a = new byte[size];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
byte ia = (byte)(255.0*alpha+0.5);
for (int i=0; i<size; ++i)
a[i] = ia;
return new IndexColorModel(bits,size,r,g,b,a);
}
/**
* Create a color table from the image ColorModel and SampleModel.
*/
private static byte[] createColorTable(ColorModel colorModel,
SampleModel sampleModel)
{
byte[] colorTable;
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
/**
* The GIF image format assumes that size of image palette
* is power of two. We will use closest larger power of two
* as size of color table.
*/
int ctSize = getGifPaletteSize(mapSize);
byte[] reds = new byte[ctSize];
byte[] greens = new byte[ctSize];
byte[] blues = new byte[ctSize];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
/**
* fill tail of color component arrays by replica of first color
* in order to avoid appearance of extra colors in the color table
*/
for (int i = mapSize; i < ctSize; i++) {
reds[i] = reds[0];
greens[i] = greens[0];
blues[i] = blues[0];
}
colorTable = new byte[3*ctSize];
int idx = 0;
for (int i = 0; i < ctSize; i++) {
colorTable[idx++] = reds[i];
colorTable[idx++] = greens[i];
colorTable[idx++] = blues[i];
}
} else if (sampleModel.getNumBands() == 1) {
// create gray-scaled color table for single-banded images
int numBits = sampleModel.getSampleSize()[0];
if (numBits > 8) {
numBits = 8;
}
int colorTableLength = 3*(1 << numBits);
colorTable = new byte[colorTableLength];
for (int i = 0; i < colorTableLength; i++) {
colorTable[i] = (byte)(i/3);
}
} else {
// We do not have enough information here
// to create well-fit color table for RGB image.
colorTable = null;
}
return colorTable;
}
/**
* Create a color table from the image ColorModel and SampleModel.
*/
private static byte[] createColorTable(ColorModel colorModel,
SampleModel sampleModel)
{
byte[] colorTable;
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
/**
* The GIF image format assumes that size of image palette
* is power of two. We will use closest larger power of two
* as size of color table.
*/
int ctSize = getGifPaletteSize(mapSize);
byte[] reds = new byte[ctSize];
byte[] greens = new byte[ctSize];
byte[] blues = new byte[ctSize];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
/**
* fill tail of color component arrays by replica of first color
* in order to avoid appearance of extra colors in the color table
*/
for (int i = mapSize; i < ctSize; i++) {
reds[i] = reds[0];
greens[i] = greens[0];
blues[i] = blues[0];
}
colorTable = new byte[3*ctSize];
int idx = 0;
for (int i = 0; i < ctSize; i++) {
colorTable[idx++] = reds[i];
colorTable[idx++] = greens[i];
colorTable[idx++] = blues[i];
}
} else if (sampleModel.getNumBands() == 1) {
// create gray-scaled color table for single-banded images
int numBits = sampleModel.getSampleSize()[0];
if (numBits > 8) {
numBits = 8;
}
int colorTableLength = 3*(1 << numBits);
colorTable = new byte[colorTableLength];
for (int i = 0; i < colorTableLength; i++) {
colorTable[i] = (byte)(i/3);
}
} else {
// We do not have enough information here
// to create well-fit color table for RGB image.
colorTable = null;
}
return colorTable;
}
/**
* Create a color table from the image ColorModel and SampleModel.
*/
private static byte[] createColorTable(ColorModel colorModel,
SampleModel sampleModel)
{
byte[] colorTable;
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
/**
* The GIF image format assumes that size of image palette
* is power of two. We will use closest larger power of two
* as size of color table.
*/
int ctSize = getGifPaletteSize(mapSize);
byte[] reds = new byte[ctSize];
byte[] greens = new byte[ctSize];
byte[] blues = new byte[ctSize];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
/**
* fill tail of color component arrays by replica of first color
* in order to avoid appearance of extra colors in the color table
*/
for (int i = mapSize; i < ctSize; i++) {
reds[i] = reds[0];
greens[i] = greens[0];
blues[i] = blues[0];
}
colorTable = new byte[3*ctSize];
int idx = 0;
for (int i = 0; i < ctSize; i++) {
colorTable[idx++] = reds[i];
colorTable[idx++] = greens[i];
colorTable[idx++] = blues[i];
}
} else if (sampleModel.getNumBands() == 1) {
// create gray-scaled color table for single-banded images
int numBits = sampleModel.getSampleSize()[0];
if (numBits > 8) {
numBits = 8;
}
int colorTableLength = 3*(1 << numBits);
colorTable = new byte[colorTableLength];
for (int i = 0; i < colorTableLength; i++) {
colorTable[i] = (byte)(i/3);
}
} else {
// We do not have enough information here
// to create well-fit color table for RGB image.
colorTable = null;
}
return colorTable;
}
/**
* Create a color table from the image ColorModel and SampleModel.
*/
private static byte[] createColorTable(ColorModel colorModel,
SampleModel sampleModel)
{
byte[] colorTable;
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
/**
* The GIF image format assumes that size of image palette
* is power of two. We will use closest larger power of two
* as size of color table.
*/
int ctSize = getGifPaletteSize(mapSize);
byte[] reds = new byte[ctSize];
byte[] greens = new byte[ctSize];
byte[] blues = new byte[ctSize];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
/**
* fill tail of color component arrays by replica of first color
* in order to avoid appearance of extra colors in the color table
*/
for (int i = mapSize; i < ctSize; i++) {
reds[i] = reds[0];
greens[i] = greens[0];
blues[i] = blues[0];
}
colorTable = new byte[3*ctSize];
int idx = 0;
for (int i = 0; i < ctSize; i++) {
colorTable[idx++] = reds[i];
colorTable[idx++] = greens[i];
colorTable[idx++] = blues[i];
}
} else if (sampleModel.getNumBands() == 1) {
// create gray-scaled color table for single-banded images
int numBits = sampleModel.getSampleSize()[0];
if (numBits > 8) {
numBits = 8;
}
int colorTableLength = 3*(1 << numBits);
colorTable = new byte[colorTableLength];
for (int i = 0; i < colorTableLength; i++) {
colorTable[i] = (byte)(i/3);
}
} else {
// We do not have enough information here
// to create well-fit color table for RGB image.
colorTable = null;
}
return colorTable;
}
/**
* Create a color table from the image ColorModel and SampleModel.
*/
private static byte[] createColorTable(ColorModel colorModel,
SampleModel sampleModel)
{
byte[] colorTable;
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
/**
* The GIF image format assumes that size of image palette
* is power of two. We will use closest larger power of two
* as size of color table.
*/
int ctSize = getGifPaletteSize(mapSize);
byte[] reds = new byte[ctSize];
byte[] greens = new byte[ctSize];
byte[] blues = new byte[ctSize];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
/**
* fill tail of color component arrays by replica of first color
* in order to avoid appearance of extra colors in the color table
*/
for (int i = mapSize; i < ctSize; i++) {
reds[i] = reds[0];
greens[i] = greens[0];
blues[i] = blues[0];
}
colorTable = new byte[3*ctSize];
int idx = 0;
for (int i = 0; i < ctSize; i++) {
colorTable[idx++] = reds[i];
colorTable[idx++] = greens[i];
colorTable[idx++] = blues[i];
}
} else if (sampleModel.getNumBands() == 1) {
// create gray-scaled color table for single-banded images
int numBits = sampleModel.getSampleSize()[0];
if (numBits > 8) {
numBits = 8;
}
int colorTableLength = 3*(1 << numBits);
colorTable = new byte[colorTableLength];
for (int i = 0; i < colorTableLength; i++) {
colorTable[i] = (byte)(i/3);
}
} else {
// We do not have enough information here
// to create well-fit color table for RGB image.
colorTable = null;
}
return colorTable;
}
/**
* Create a color table from the image ColorModel and SampleModel.
*/
private static byte[] createColorTable(ColorModel colorModel,
SampleModel sampleModel)
{
byte[] colorTable;
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
/**
* The GIF image format assumes that size of image palette
* is power of two. We will use closest larger power of two
* as size of color table.
*/
int ctSize = getGifPaletteSize(mapSize);
byte[] reds = new byte[ctSize];
byte[] greens = new byte[ctSize];
byte[] blues = new byte[ctSize];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
/**
* fill tail of color component arrays by replica of first color
* in order to avoid appearance of extra colors in the color table
*/
for (int i = mapSize; i < ctSize; i++) {
reds[i] = reds[0];
greens[i] = greens[0];
blues[i] = blues[0];
}
colorTable = new byte[3*ctSize];
int idx = 0;
for (int i = 0; i < ctSize; i++) {
colorTable[idx++] = reds[i];
colorTable[idx++] = greens[i];
colorTable[idx++] = blues[i];
}
} else if (sampleModel.getNumBands() == 1) {
// create gray-scaled color table for single-banded images
int numBits = sampleModel.getSampleSize()[0];
if (numBits > 8) {
numBits = 8;
}
int colorTableLength = 3*(1 << numBits);
colorTable = new byte[colorTableLength];
for (int i = 0; i < colorTableLength; i++) {
colorTable[i] = (byte)(i/3);
}
} else {
// We do not have enough information here
// to create well-fit color table for RGB image.
colorTable = null;
}
return colorTable;
}
/**
* Create a color table from the image ColorModel and SampleModel.
*/
private static byte[] createColorTable(ColorModel colorModel,
SampleModel sampleModel)
{
byte[] colorTable;
if (colorModel instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
/**
* The GIF image format assumes that size of image palette
* is power of two. We will use closest larger power of two
* as size of color table.
*/
int ctSize = getGifPaletteSize(mapSize);
byte[] reds = new byte[ctSize];
byte[] greens = new byte[ctSize];
byte[] blues = new byte[ctSize];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
/**
* fill tail of color component arrays by replica of first color
* in order to avoid appearance of extra colors in the color table
*/
for (int i = mapSize; i < ctSize; i++) {
reds[i] = reds[0];
greens[i] = greens[0];
blues[i] = blues[0];
}
colorTable = new byte[3*ctSize];
int idx = 0;
for (int i = 0; i < ctSize; i++) {
colorTable[idx++] = reds[i];
colorTable[idx++] = greens[i];
colorTable[idx++] = blues[i];
}
} else if (sampleModel.getNumBands() == 1) {
// create gray-scaled color table for single-banded images
int numBits = sampleModel.getSampleSize()[0];
if (numBits > 8) {
numBits = 8;
}
int colorTableLength = 3*(1 << numBits);
colorTable = new byte[colorTableLength];
for (int i = 0; i < colorTableLength; i++) {
colorTable[i] = (byte)(i/3);
}
} else {
// We do not have enough information here
// to create well-fit color table for RGB image.
colorTable = null;
}
return colorTable;
}