}
private BufferedImage readXpmImage(XpmHeader xpmHeader, BasicCParser cParser)
throws ImageReadException, IOException
{
ColorModel colorModel;
WritableRaster raster;
int bpp;
if (xpmHeader.palette.size() <= (1 << 8))
{
int[] palette = new int[xpmHeader.palette.size()];
for (Iterator it = xpmHeader.palette.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
PaletteEntry paletteEntry = (PaletteEntry)entry.getValue();
palette[paletteEntry.index] = paletteEntry.getBestARGB();
}
colorModel = new IndexColorModel(8, xpmHeader.palette.size(),
palette, 0, true, -1, DataBuffer.TYPE_BYTE);
raster = WritableRaster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
xpmHeader.width, xpmHeader.height, 1, null);
bpp = 8;
}
else if (xpmHeader.palette.size() <= (1 << 16))
{
int[] palette = new int[xpmHeader.palette.size()];
for (Iterator it = xpmHeader.palette.entrySet().iterator(); it.hasNext();)
{
Map.Entry entry = (Map.Entry)it.next();
PaletteEntry paletteEntry = (PaletteEntry)entry.getValue();
palette[paletteEntry.index] = paletteEntry.getBestARGB();
}
colorModel = new IndexColorModel(16, xpmHeader.palette.size(),
palette, 0, true, -1, DataBuffer.TYPE_USHORT);
raster = WritableRaster.createInterleavedRaster(DataBuffer.TYPE_USHORT,
xpmHeader.width, xpmHeader.height, 1, null);
bpp = 16;
}
else
{
colorModel = new DirectColorModel(32, 0x00ff0000, 0x0000ff00,
0x000000ff, 0xff000000);
raster = WritableRaster.createPackedRaster(DataBuffer.TYPE_INT,
xpmHeader.width, xpmHeader.height,
new int[]{0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000}, null);
bpp = 32;
}
BufferedImage image = new BufferedImage(colorModel, raster,
colorModel.isAlphaPremultiplied(), new Properties());
DataBuffer dataBuffer = raster.getDataBuffer();
StringBuilder row = new StringBuilder();
boolean hasMore = true;
for (int y = 0; y < xpmHeader.height; y++)
{