Search in sources :

Example 6 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)

Example 7 with SpriteDefinition

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

Example 8 with SpriteDefinition

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);
            }
        }
    }
}
Also used : Storage(net.runelite.cache.fs.Storage) Archive(net.runelite.cache.fs.Archive) SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition) Index(net.runelite.cache.fs.Index) SpriteLoader(net.runelite.cache.definitions.loaders.SpriteLoader) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage)

Example 9 with SpriteDefinition

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);
        }
    }
}
Also used : Storage(net.runelite.cache.fs.Storage) Archive(net.runelite.cache.fs.Archive) SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition) Index(net.runelite.cache.fs.Index) SpriteLoader(net.runelite.cache.definitions.loaders.SpriteLoader)

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