use of net.runelite.cache.definitions.SpriteDefinition in project runelite by runelite.
the class SpriteLoader method load.
public SpriteDefinition[] load(int id, byte[] b) {
InputStream is = new InputStream(b);
is.setOffset(is.getLength() - 2);
int spriteCount = is.readUnsignedShort();
SpriteDefinition[] sprites = new SpriteDefinition[spriteCount];
// 2 for size
// 5 for width, height, palette length
// + 8 bytes per sprite for offset x/y, width, and height
is.setOffset(is.getLength() - 7 - spriteCount * 8);
// max width and height
int width = is.readUnsignedShort();
int height = is.readUnsignedShort();
int paletteLength = is.readUnsignedByte() + 1;
for (int i = 0; i < spriteCount; ++i) {
sprites[i] = new SpriteDefinition();
sprites[i].setId(id);
sprites[i].setFrame(i);
sprites[i].setMaxWidth(width);
sprites[i].setMaxHeight(height);
}
for (int i = 0; i < spriteCount; ++i) {
sprites[i].setOffsetX(is.readUnsignedShort());
}
for (int i = 0; i < spriteCount; ++i) {
sprites[i].setOffsetY(is.readUnsignedShort());
}
for (int i = 0; i < spriteCount; ++i) {
sprites[i].setWidth(is.readUnsignedShort());
}
for (int i = 0; i < spriteCount; ++i) {
sprites[i].setHeight(is.readUnsignedShort());
}
// same as above + 3 bytes for each palette entry, except for the first one (which is transparent)
is.setOffset(is.getLength() - 7 - spriteCount * 8 - (paletteLength - 1) * 3);
int[] palette = new int[paletteLength];
for (int i = 1; i < paletteLength; ++i) {
palette[i] = is.read24BitInt();
if (palette[i] == 0) {
palette[i] = 1;
}
}
is.setOffset(0);
for (int i = 0; i < spriteCount; ++i) {
SpriteDefinition def = sprites[i];
int spriteWidth = def.getWidth();
int spriteHeight = def.getHeight();
int dimension = spriteWidth * spriteHeight;
byte[] pixelPaletteIndicies = new byte[dimension];
byte[] pixelAlphas = new byte[dimension];
def.pixelIdx = pixelPaletteIndicies;
def.palette = palette;
int flags = is.readUnsignedByte();
if ((flags & FLAG_VERTICAL) == 0) {
// read horizontally
for (int j = 0; j < dimension; ++j) {
pixelPaletteIndicies[j] = is.readByte();
}
} else {
// read vertically
for (int j = 0; j < spriteWidth; ++j) {
for (int k = 0; k < spriteHeight; ++k) {
pixelPaletteIndicies[spriteWidth * k + j] = is.readByte();
}
}
}
// read alphas
if ((flags & FLAG_ALPHA) != 0) {
if ((flags & FLAG_VERTICAL) == 0) {
// read horizontally
for (int j = 0; j < dimension; ++j) {
pixelAlphas[j] = is.readByte();
}
} else {
// read vertically
for (int j = 0; j < spriteWidth; ++j) {
for (int k = 0; k < spriteHeight; ++k) {
pixelAlphas[spriteWidth * k + j] = is.readByte();
}
}
}
} else {
// everything non-zero is opaque
for (int j = 0; j < dimension; ++j) {
int index = pixelPaletteIndicies[j];
if (index != 0)
pixelAlphas[j] = (byte) 0xFF;
}
}
int[] pixels = new int[dimension];
// build argb pixels from palette/alphas
for (int j = 0; j < dimension; ++j) {
int index = pixelPaletteIndicies[j] & 0xFF;
pixels[j] = palette[index] | (pixelAlphas[j] << 24);
}
def.setPixels(pixels);
}
return sprites;
}
use of net.runelite.cache.definitions.SpriteDefinition in project runelite by runelite.
the class MapImageDumper method drawMapIcons.
private void drawMapIcons(Graphics2D graphics, Region region, int z, int drawBaseX, int drawBaseY) {
for (Location location : region.getLocations()) {
int localZ = location.getPosition().getZ();
if (z != 0 && localZ != z) {
// draw all icons on z=0
continue;
}
ObjectDefinition od = findObject(location.getId());
assert od != null;
int localX = location.getPosition().getX() - region.getBaseX();
int localY = location.getPosition().getY() - region.getBaseY();
int drawX = drawBaseX + localX;
int drawY = drawBaseY + (Region.Y - 1 - localY);
if (od.getMapAreaId() != -1) {
AreaDefinition area = areas.getArea(od.getMapAreaId());
assert area != null;
int spriteId = area.spriteId;
SpriteDefinition sprite = sprites.findSprite(spriteId, 0);
assert sprite != null;
BufferedImage iconImage = sprites.getSpriteImage(sprite);
graphics.drawImage(iconImage, drawX * MAP_SCALE, drawY * MAP_SCALE, null);
}
}
}
use of net.runelite.cache.definitions.SpriteDefinition in project runelite by runelite.
the class MapImageDumper method loadSprites.
private void loadSprites() throws IOException {
Storage storage = store.getStorage();
Index index = store.getIndex(IndexType.SPRITES);
final int mapsceneHash = Djb2.hash("mapscene");
for (Archive a : index.getArchives()) {
byte[] contents = a.decompress(storage.loadArchive(a));
SpriteLoader loader = new SpriteLoader();
SpriteDefinition[] sprites = loader.load(a.getArchiveId(), contents);
for (SpriteDefinition sprite : sprites) {
if (sprite.getHeight() <= 0 || sprite.getWidth() <= 0) {
continue;
}
if (a.getNameHash() == mapsceneHash) {
BufferedImage spriteImage = new BufferedImage(sprite.getWidth(), sprite.getHeight(), BufferedImage.TYPE_INT_ARGB);
spriteImage.setRGB(0, 0, sprite.getWidth(), sprite.getHeight(), sprite.getPixels(), 0, sprite.getWidth());
// scale image down so it fits
Image scaledImage = spriteImage.getScaledInstance(MAPICON_MAX_WIDTH, MAPICON_MAX_HEIGHT, 0);
assert scaledMapIcons.containsKey(sprite.getFrame()) == false;
scaledMapIcons.put(sprite.getFrame(), scaledImage);
}
}
}
}
use of net.runelite.cache.definitions.SpriteDefinition in project runelite by runelite.
the class SpriteManager method load.
public void load() throws IOException {
Storage storage = store.getStorage();
Index index = store.getIndex(IndexType.SPRITES);
for (Archive a : index.getArchives()) {
byte[] contents = a.decompress(storage.loadArchive(a));
SpriteLoader loader = new SpriteLoader();
SpriteDefinition[] defs = loader.load(a.getArchiveId(), contents);
for (SpriteDefinition sprite : defs) {
sprites.put(sprite.getId(), sprite);
}
}
}
Aggregations