下面列出了java.awt.image.IndexColorModel#getTransparentPixel() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/** Create a ColorLUT from a <code>IndexColorModel</code>. */
public ColorLUT(IndexColorModel icm) {
this.indexColorModel = icm;
for (int a = 0; a < icm.getMapSize(); a++) {
if (a != icm.getTransparentPixel()) {
int red = icm.getRed(a);
int green = icm.getGreen(a);
int blue = icm.getBlue(a);
addRGB(red, green, blue, a);
}
}
}
/**
* Returns {@code true} if the given color models are equal. The {@link ColorModel} class
* defines an {@code equals} method, but as of Java 6 that method does not compare every
* attributes. For example it does not compare the color space and the transfer type, so
* we have to compare them here.
*
* @param cm1 the first color model.
* @param cm2 the second color model.
* @return {@code true} if the two color models are equal.
*/
private static boolean equals(final ColorModel cm1, final ColorModel cm2) {
if (cm1 == cm2) {
return true;
}
if (cm1 != null && cm1.equals(cm2) &&
cm1.getClass().equals(cm2.getClass()) &&
cm1.getTransferType() == cm2.getTransferType() &&
Objects.equals(cm1.getColorSpace(), cm2.getColorSpace()))
{
if (cm1 instanceof IndexColorModel) {
final IndexColorModel icm1 = (IndexColorModel) cm1;
final IndexColorModel icm2 = (IndexColorModel) cm2;
final int size = icm1.getMapSize();
if (icm2.getMapSize() == size &&
icm1.getTransparentPixel() == icm2.getTransparentPixel() &&
Objects.equals(icm1.getValidPixels(), icm2.getValidPixels()))
{
for (int i=0; i<size; i++) {
if (icm1.getRGB(i) != icm2.getRGB(i)) {
return false;
}
}
}
if (cm1 instanceof MultiBandsIndexColorModel) {
final MultiBandsIndexColorModel micm1 = (MultiBandsIndexColorModel) cm1;
final MultiBandsIndexColorModel micm2 = (MultiBandsIndexColorModel) cm2;
if (micm1.numBands != micm2.numBands || micm1.visibleBand != micm2.visibleBand) {
return false;
}
}
}
return true;
}
return false;
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == Transparency.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == Transparency.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
@Override
public void writeImage(OutputStream out, BufferedImage image,
int frameDurationInCentiseconds, IndexColorModel globalColorModel,
boolean writeLocalColorTable) throws IOException {
if (frameDurationInCentiseconds >= 0
|| globalColorModel.getTransparentPixel() != -1) {
if (frameDurationInCentiseconds < 0)
frameDurationInCentiseconds = 0;
GifGraphicControlExtension gce = new GifGraphicControlExtension(
frameDurationInCentiseconds,
DisposalMethod.RESTORE_BACKGROUND,
globalColorModel.getTransparentPixel());
gce.write(out);
}
int localColorSize = 0;
if (writeLocalColorTable) {
localColorSize = globalColorModel.getMapSize();
int k = 2;
while (localColorSize > k) {
k *= 2;
}
if (k > 256)
throw new IllegalArgumentException("Illegal number of colors ("
+ localColorSize + "). There can only be 256 at most.");
localColorSize = k;
}
Dimension d = new Dimension(image.getWidth(), image.getHeight());
GifImageDescriptor id = new GifImageDescriptor(0, 0, d.width, d.height,
false, localColorSize);
id.write(out);
if (localColorSize > 0) {
GifLocalColorTable ct = new GifLocalColorTable(globalColorModel);
ct.write(out);
}
GifImageDataBlock dataBlock = new GifImageDataBlock(image,
globalColorModel);
dataBlock.write(out);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}
public void setColorModel(ColorModel model) {
if (src != null) {
src.checkSecurity(null, false);
}
srcModel = model;
// Check to see if model is INT_RGB
if (model instanceof IndexColorModel) {
if (model.getTransparency() == model.TRANSLUCENT) {
// REMIND:
// Probably need to composite anyway so force ARGB
cmodel = ColorModel.getRGBdefault();
srcLUT = null;
}
else {
IndexColorModel icm = (IndexColorModel) model;
numSrcLUT = icm.getMapSize();
srcLUT = new int[Math.max(numSrcLUT, 256)];
icm.getRGBs(srcLUT);
srcLUTtransIndex = icm.getTransparentPixel();
cmodel = model;
}
}
else {
if (cmodel == null) {
cmodel = model;
srcLUT = null;
}
else if (model instanceof DirectColorModel) {
// If it is INT_RGB or INT_ARGB, use the model
DirectColorModel dcm = (DirectColorModel) model;
if ((dcm.getRedMask() == 0xff0000) &&
(dcm.getGreenMask() == 0xff00) &&
(dcm.getBlueMask() == 0x00ff)) {
cmodel = model;
srcLUT = null;
}
}
}
isSameCM = (cmodel == model);
}