下面列出了java.awt.image.IndexColorModel#getBlues() 实例代码,或者点击链接到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);
}
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;
}
protected IndexColorModel getIndexColorModel(BufferedImage src) {
// index color with transparent
ColorModel cm = src.getColorModel();
if (cm instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel)cm;
int mapSize = icm.getMapSize();
byte[] reds = new byte[mapSize];
byte[] greens = new byte[mapSize];
byte[] blues = new byte[mapSize];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
return new IndexColorModel(8, mapSize, reds, greens, blues, 0);
}
return null;
}
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);
}
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;
}
/** Gets the LUT from the <code>IndexColorModel</code> as an two-dimensional
* byte array.
*/
private static byte[][] getLUT(IndexColorModel icm) {
int[] comp = icm.getComponentSize();
int size = icm.getMapSize();
byte[][] lut = new byte[comp.length][size];
icm.getReds(lut[0]);
icm.getGreens(lut[1]);
icm.getBlues(lut[2]);
if (comp.length == 4)
icm.getAlphas(lut[3]);
return lut;
}
/** Converts an IndexColorModel to a 2D byte array. */
public static byte[][] get8BitLookupTable(final ColorModel model) {
if (!(model instanceof IndexColorModel)) return null;
final IndexColorModel m = (IndexColorModel) model;
final byte[][] lut = new byte[3][m.getMapSize()];
m.getReds(lut[0]);
m.getGreens(lut[1]);
m.getBlues(lut[2]);
return lut;
}
/**
* 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;
}