bos.write4Bytes(AlphaMask);
bos.write(RestOfFile);
bos.flush();
ByteArrayInputStream bmpInputStream = new ByteArrayInputStream(baos.toByteArray());
BufferedImage bmpImage = new BmpImageParser().getBufferedImage(bmpInputStream, null);
// Transparency map is optional with 32 BPP icons, because they already have
// an alpha channel, and Windows only uses the transparency map when it has to
// display the icon on a < 32 BPP screen. But it's still used instead of alpha
// if the image would be completely transparent with alpha...
int t_scanline_size = (Width + 7) / 8;
if ((t_scanline_size % 4) != 0)
t_scanline_size += 4 - (t_scanline_size % 4); // pad scanline to 4 byte size.
int tcolor_map_size_bytes = t_scanline_size * (Height/2);
byte[] transparency_map = null;
try
{
transparency_map = this.readByteArray("transparency_map",
tcolor_map_size_bytes, bmpInputStream, "Not a Valid ICO File");
}
catch (IOException ioEx)
{
if (BitCount != 32)
throw ioEx;
}
boolean allAlphasZero = true;
if (BitCount == 32)
{
for (int y = 0; allAlphasZero && y < bmpImage.getHeight(); y++)
{
for (int x = 0; x < bmpImage.getWidth(); x++)
{
if ((bmpImage.getRGB(x, y) & 0xff000000) != 0)
{
allAlphasZero = false;
break;
}
}
}
}
BufferedImage resultImage;
if (allAlphasZero)
{
resultImage = new BufferedImage(bmpImage.getWidth(), bmpImage.getHeight(),
BufferedImage.TYPE_INT_ARGB);
for (int y = 0; y < resultImage.getHeight(); y++)
{
for (int x = 0; x < resultImage.getWidth(); x++)
{
int alpha = 0xff;
if (transparency_map != null)
{
int alpha_byte = 0xff & transparency_map[t_scanline_size
* (bmpImage.getHeight() - y - 1) + (x / 8)];
alpha = 0x01 & (alpha_byte >> (7 - (x % 8)));
alpha = (alpha == 0) ? 0xff : 0x00;
}
resultImage.setRGB(x, y, (alpha << 24) | (0xffffff & bmpImage.getRGB(x, y)));
}
}
}
else
resultImage = bmpImage;