Out.fatalException(e);
}
}
private static BNetIcon[] readIconsDotBni(File f) {
try (BNetInputStream is = new BNetInputStream(new FileInputStream(f))) {
Out.debug(IconsDotBniReader.class, "Reading " + f.getName());
is.skip(4); //int headerSize = is.readDWord();
int bniVersion = is.readWord();
is.skip(2); // Alignment Padding (unused)
int numIcons = is.readDWord();
is.skip(4); //int dataOffset = is.readDWord();
if(bniVersion != 1)
throw new Exception("Unknown BNI version");
Out.debug(IconsDotBniReader.class, "Reading " + numIcons + " icons in format " + bniVersion);
BNetIcon[] icons = new BNetIcon[numIcons];
for(int i = 0; i < numIcons; i++) {
BNetIcon icon = new BNetIcon();
icon.flags = is.readDWord();
icon.xSize = is.readDWord();
icon.ySize = is.readDWord();
if(icon.flags != 0)
icon.sortIndex = i;
else
icon.sortIndex = numIcons;
int numProducts;
int products[] = new int[32];
//Read in up to 32 products; stop if we see a null
for(numProducts = 0; numProducts < 32; numProducts++) {
products[numProducts] = is.readDWord();
if(products[numProducts] == 0)
break;
}
if(numProducts > 0) {
icon.products = new int[numProducts];
for(int j = 0; j < numProducts; j++)
icon.products[j] = products[j];
} else
icon.products = null;
icons[i] = icon;
Out.debug(IconsDotBniReader.class, icon.toString());
}
//Image in targa format
byte infoLength = is.readByte();
is.skip(1); // ColorMapType
byte imageType = is.readByte(); // run-length true-color image types = 0x0A
is.skip(5); // ColorMapSpecification - color map data
is.skip(2); //int xOrigin = is.readWord();
is.skip(2); //int yOrigin = is.readWord();
int width = is.readWord();
int height = is.readWord();
byte depth = is.readByte(); // 24 bit depth is good
/*byte descriptor =*/ is.readByte(); // bits 5 and 4 (00110000) specify the corner to start coloring pixels - 00=bl, 01=br, 10=tl, 11=tr
is.skip(infoLength); //String info = is.readFixedLengthString(infoLength);
if(imageType != 0x0A)
throw new Exception("Unknown image type");
if(depth != 24)
throw new Exception("Unknown depth");
//Pixel data
int[] pixelData = new int[width*height];
int currentPixel = 0;
while(currentPixel < pixelData.length) {
byte packetHeader = is.readByte(); // if bit 7 (0x80) is set, run-length packet;
int len = (packetHeader & 0x7F) + 1;
if((packetHeader & 0x80) != 0) {
//Run-length packet
int blue = is.readByte() & 0xFF;
int green = is.readByte() & 0xFF;
int red = is.readByte() & 0xFF;
int col = new Color(red, green, blue).getRGB();
for(int i = 0; i < len; i++)
pixelData[getRealPixelPosition(currentPixel++, height, width)] = col;
} else {
for(int i = 0; i < len; i++) {
int blue = is.readByte() & 0xFF;
int green = is.readByte() & 0xFF;
int red = is.readByte() & 0xFF;
int col = new Color(red, green, blue).getRGB();
pixelData[getRealPixelPosition(currentPixel++, height, width)] = col;
}
}
}