Search in sources :

Example 1 with SpriteDefinition

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);
    }
}
Also used : SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition) File(java.io.File) SpriteExporter(net.runelite.cache.definitions.exporters.SpriteExporter)

Example 2 with SpriteDefinition

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());
}
Also used : ItemProvider(net.runelite.cache.definitions.providers.ItemProvider) TextureLoader(net.runelite.cache.definitions.loaders.TextureLoader) ModelProvider(net.runelite.cache.definitions.providers.ModelProvider) SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition) IndexEntry(net.runelite.http.service.cache.beans.IndexEntry) ArchiveEntry(net.runelite.http.service.cache.beans.ArchiveEntry) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheEntry(net.runelite.http.service.cache.beans.CacheEntry) TextureProvider(net.runelite.cache.definitions.providers.TextureProvider) IOException(java.io.IOException) NotFoundException(net.runelite.http.service.util.exception.NotFoundException) BufferedImage(java.awt.image.BufferedImage) FSFile(net.runelite.cache.fs.FSFile) ModelLoader(net.runelite.cache.definitions.loaders.ModelLoader) ArchiveFiles(net.runelite.cache.fs.ArchiveFiles) TextureDefinition(net.runelite.cache.definitions.TextureDefinition) SpriteLoader(net.runelite.cache.definitions.loaders.SpriteLoader) SpriteProvider(net.runelite.cache.definitions.providers.SpriteProvider) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with SpriteDefinition

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;
}
Also used : SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition) BufferedImage(java.awt.image.BufferedImage)

Example 4 with SpriteDefinition

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];
}
Also used : ArchiveFiles(net.runelite.cache.fs.ArchiveFiles) SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition) IndexEntry(net.runelite.http.service.cache.beans.IndexEntry) ArchiveEntry(net.runelite.http.service.cache.beans.ArchiveEntry) CacheEntry(net.runelite.http.service.cache.beans.CacheEntry) SpriteLoader(net.runelite.cache.definitions.loaders.SpriteLoader) FSFile(net.runelite.cache.fs.FSFile)

Example 5 with SpriteDefinition

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;
}
Also used : InputStream(net.runelite.cache.io.InputStream) SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition)

Aggregations

SpriteDefinition (net.runelite.cache.definitions.SpriteDefinition)9 BufferedImage (java.awt.image.BufferedImage)5 SpriteLoader (net.runelite.cache.definitions.loaders.SpriteLoader)4 Archive (net.runelite.cache.fs.Archive)2 ArchiveFiles (net.runelite.cache.fs.ArchiveFiles)2 FSFile (net.runelite.cache.fs.FSFile)2 Index (net.runelite.cache.fs.Index)2 Storage (net.runelite.cache.fs.Storage)2 ArchiveEntry (net.runelite.http.service.cache.beans.ArchiveEntry)2 CacheEntry (net.runelite.http.service.cache.beans.CacheEntry)2 IndexEntry (net.runelite.http.service.cache.beans.IndexEntry)2 Image (java.awt.Image)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 AreaDefinition (net.runelite.cache.definitions.AreaDefinition)1 ObjectDefinition (net.runelite.cache.definitions.ObjectDefinition)1 TextureDefinition (net.runelite.cache.definitions.TextureDefinition)1 SpriteExporter (net.runelite.cache.definitions.exporters.SpriteExporter)1 ModelLoader (net.runelite.cache.definitions.loaders.ModelLoader)1