use of net.runelite.cache.definitions.SpriteDefinition in project runelite by runelite.
the class SpriteManager method export.
public void export(File outDir) throws IOException {
for (SpriteDefinition sprite : sprites.values()) {
// I don't know why this happens
if (sprite.getHeight() <= 0 || sprite.getWidth() <= 0) {
continue;
}
SpriteExporter exporter = new SpriteExporter(sprite);
File png = new File(outDir, sprite.getId() + "-" + sprite.getFrame() + ".png");
exporter.exportTo(png);
}
}
use of net.runelite.cache.definitions.SpriteDefinition in project runelite by runelite.
the class CacheController method getItemImage.
@RequestMapping(path = "item/{itemId}/image", produces = "image/png")
public ResponseEntity<byte[]> getItemImage(@PathVariable int itemId, @RequestParam(defaultValue = "1") int quantity, @RequestParam(defaultValue = "1") int border, @RequestParam(defaultValue = "3153952") int shadowColor) throws IOException {
final CacheEntry cache = cacheService.findMostRecent();
ItemProvider itemProvider = new ItemProvider() {
@Override
public ItemDefinition provide(int itemId) {
try {
return getItem(itemId);
} catch (IOException ex) {
log.warn(null, ex);
return null;
}
}
};
ModelProvider modelProvider = new ModelProvider() {
@Override
public ModelDefinition provide(int modelId) throws IOException {
IndexEntry indexEntry = cacheService.findIndexForCache(cache, IndexType.MODELS.getNumber());
ArchiveEntry archiveEntry = cacheService.findArchiveForIndex(indexEntry, modelId);
byte[] archiveData = Container.decompress(cacheService.getArchive(archiveEntry), null).data;
return new ModelLoader().load(modelId, archiveData);
}
};
SpriteProvider spriteProvider = new SpriteProvider() {
@Override
public SpriteDefinition provide(int spriteId, int frameId) {
try {
IndexEntry indexEntry = cacheService.findIndexForCache(cache, IndexType.SPRITES.getNumber());
ArchiveEntry archiveEntry = cacheService.findArchiveForIndex(indexEntry, spriteId);
byte[] archiveData = Container.decompress(cacheService.getArchive(archiveEntry), null).data;
SpriteDefinition[] defs = new SpriteLoader().load(spriteId, archiveData);
return defs[frameId];
} catch (Exception ex) {
log.warn(null, ex);
return null;
}
}
};
TextureProvider textureProvider2 = new TextureProvider() {
@Override
public TextureDefinition[] provide() {
try {
IndexEntry indexEntry = cacheService.findIndexForCache(cache, IndexType.TEXTURES.getNumber());
ArchiveEntry archiveEntry = cacheService.findArchiveForIndex(indexEntry, 0);
ArchiveFiles archiveFiles = cacheService.getArchiveFiles(archiveEntry);
TextureLoader loader = new TextureLoader();
TextureDefinition[] defs = new TextureDefinition[archiveFiles.getFiles().size()];
int i = 0;
for (FSFile file : archiveFiles.getFiles()) {
TextureDefinition def = loader.load(file.getFileId(), file.getContents());
defs[i++] = def;
}
return defs;
} catch (Exception ex) {
log.warn(null, ex);
return null;
}
}
};
BufferedImage itemImage = ItemSpriteFactory.createSprite(itemProvider, modelProvider, spriteProvider, textureProvider2, itemId, quantity, border, shadowColor, false);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
ImageIO.write(itemImage, "png", bao);
return ResponseEntity.ok(bao.toByteArray());
}
use of net.runelite.cache.definitions.SpriteDefinition in project runelite by runelite.
the class SpriteService method getImage.
public BufferedImage getImage(int spriteId, int frameId) throws IOException {
SpriteDefinition sprite = getSprite(spriteId, frameId);
if (sprite == null) {
return null;
}
BufferedImage bufferedImage = getSpriteImage(sprite);
return bufferedImage;
}
use of net.runelite.cache.definitions.SpriteDefinition in project runelite by runelite.
the class SpriteService method getSprite.
public SpriteDefinition getSprite(int spriteId, int frameId) throws IOException {
CacheEntry cache = cacheService.findMostRecent();
if (cache == null) {
return null;
}
IndexEntry index = cacheService.findIndexForCache(cache, IndexType.SPRITES.getNumber());
if (index == null) {
return null;
}
ArchiveEntry archive = cacheService.findArchiveForIndex(index, spriteId);
if (archive == null) {
return null;
}
ArchiveFiles files = cacheService.getArchiveFiles(archive);
if (files == null) {
return null;
}
FSFile file = files.getFiles().get(0);
byte[] contents = file.getContents();
SpriteDefinition[] sprite = new SpriteLoader().load(archive.getArchiveId(), contents);
if (frameId < 0 || frameId >= sprite.length) {
return null;
}
return sprite[frameId];
}
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;
}
Aggregations