public void loadChunk(Map map) throws ChunkLoadException {
MapHeader mapHeader = map.getMapHeader();
Colour[] colours = map.getColours();
int colourDepth = mapHeader.getColourDepth();
PixelData imageData[] = new PixelData[mapHeader.getNoOfImages()];
int intAlpha=0 , intRed=0, intGreen=0, intBlue=0, colourIndex;
// cache some map header values
int blockHeight = mapHeader.getBlockHeight();
int blockWidth = mapHeader.getBlockWidth();
// grab the transparent colour
Colour chromeKey = null;
if (mapHeader.getColourDepth() == 8) {
colourIndex = mapHeader.getChromeKeyIndex();
chromeKey = colours[colourIndex];
} else {
chromeKey = mapHeader.getChromeKey();
}
int transparent = chromeKey.toInt();
PixelData pixelData;
log.info("Reading [" + mapHeader.getNoOfImages() + "] images...");
for (int i=0; i < mapHeader.getNoOfImages(); i++) {
int[] intImage = new int [mapHeader.getBlockWidth() * mapHeader.getBlockHeight()];
byte[] bytImage = new byte[mapHeader.getBlockWidth() * mapHeader.getBlockHeight()];
int index = 0;
boolean hasTransparentPixels = false;
for (int y=0 ; y<blockHeight; y++) {
for (int x=0 ; x<blockWidth; x++) {
int upper, lower;
int colour;
// grab the colour of the pixel
switch (colourDepth) {
case 8:
colourIndex = super.readUByte();
intAlpha = colours[colourIndex].getAlpha();
intRed = colours[colourIndex].getRed();
intGreen = colours[colourIndex].getGreen();
intBlue = colours[colourIndex].getBlue();
bytImage[index] = (byte) colourIndex;
break;
case 15:
upper = super.readUByte();
lower = super.readUByte();
intAlpha = 0xFF;
intRed = (upper & 0x7C) << 1;
intGreen = ((upper & 0x03) << 6) | ((lower & 0xE0) >> 2);
intBlue = (lower & 0x1F) << 3;
break;
case 16:
upper = super.readUByte();
lower = super.readUByte();
intAlpha = 0xFF;
intRed = (upper & 0xF8);
intGreen = ((upper & 0x07) << 5) | ((lower & 0xE0) >> 3);
intBlue = (lower & 0x1F) << 3;
break;
case 24:
intAlpha = 0xFF;
intRed = super.readUByte();
intGreen = super.readUByte();
intBlue = super.readUByte();
break;
case 32:
intAlpha = super.readUByte();
intRed = super.readUByte();
intGreen = super.readUByte();
intBlue = super.readUByte();
transparent = 0; // prevents (semi) opaque colours being made transparent
break;
}
colour = (intAlpha << 24) | (intRed << 16) | (intGreen << 8) | intBlue;
if (colour == transparent) {
colour &= 0x00FFFFFF;
hasTransparentPixels = true;
}
// this caters for 32 bit colours
if (intAlpha != 0xFF)
hasTransparentPixels = true;
intImage[index++] = colour;
}
}
if (colourDepth == 8)
pixelData = new PixelData(intImage, bytImage, hasTransparentPixels);
else
pixelData = new PixelData(intImage, hasTransparentPixels);
imageData[i] = pixelData;
// this logging detail is far too much for normal use!
// String data = "";