From af7e76502b24ec2b81bb6db025304c0c9ece1166 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Sun, 20 May 2018 00:29:40 +0200 Subject: [PATCH] Add new toBufferedOutline method to SpritePixels Add method that returns item image with outline to SpritePixels. Signed-off-by: Tomas Slusny --- .../java/net/runelite/api/SpritePixels.java | 16 +++++ .../runelite/mixins/RSSpritePixelsMixin.java | 65 +++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/runelite-api/src/main/java/net/runelite/api/SpritePixels.java b/runelite-api/src/main/java/net/runelite/api/SpritePixels.java index 9eec87c833..4ebdbdba1c 100644 --- a/runelite-api/src/main/java/net/runelite/api/SpritePixels.java +++ b/runelite-api/src/main/java/net/runelite/api/SpritePixels.java @@ -24,6 +24,7 @@ */ package net.runelite.api; +import java.awt.Color; import java.awt.image.BufferedImage; /** @@ -76,4 +77,19 @@ public interface SpritePixels * @throws IllegalArgumentException if the width or height do not match */ void toBufferedImage(BufferedImage img) throws IllegalArgumentException; + + /** + * Writes the contents of the SpritePixels with chosen outline to the BufferedImage + * + * @param color target color + */ + BufferedImage toBufferedOutline(Color color); + + /** + * Writes the contents of the SpritePixels with chosen outline to the BufferedImage + * + * @param img target image + * @param color target color + */ + void toBufferedOutline(BufferedImage img, int color); } diff --git a/runelite-mixins/src/main/java/net/runelite/mixins/RSSpritePixelsMixin.java b/runelite-mixins/src/main/java/net/runelite/mixins/RSSpritePixelsMixin.java index 7511df70d7..b351a3d768 100644 --- a/runelite-mixins/src/main/java/net/runelite/mixins/RSSpritePixelsMixin.java +++ b/runelite-mixins/src/main/java/net/runelite/mixins/RSSpritePixelsMixin.java @@ -24,6 +24,7 @@ */ package net.runelite.mixins; +import java.awt.Color; import java.awt.image.BufferedImage; import net.runelite.api.mixins.Inject; import net.runelite.api.mixins.Mixin; @@ -68,4 +69,68 @@ public abstract class RSSpritePixelsMixin implements RSSpritePixels img.setRGB(0, 0, width, height, transPixels, 0, width); } + + @Inject + @Override + public BufferedImage toBufferedOutline(Color color) + { + BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); + + toBufferedOutline(img, color.getRGB()); + + return img; + } + + @Inject + @Override + public void toBufferedOutline(BufferedImage img, int color) + { + int width = getWidth(); + int height = getHeight(); + + if (img.getWidth() != width || img.getHeight() != height) + { + throw new IllegalArgumentException("Image bounds do not match SpritePixels"); + } + + int[] pixels = getPixels(); + int[] newPixels = new int[width * height]; + int pixelIndex = 0; + + for (int y = 0; y < height; ++y) + { + for (int x = 0; x < width; ++x) + { + int pixel = pixels[pixelIndex]; + if (pixel == 16777215 || pixel == 0) + { + // W + if (x > 0 && pixels[pixelIndex - 1] != 0) + { + pixel = color; + } + // N + else if (y > 0 && pixels[pixelIndex - width] != 0) + { + pixel = color; + } + // E + else if (x < width - 1 && pixels[pixelIndex + 1] != 0) + { + pixel = color; + } + // S + else if (y < height - 1 && pixels[pixelIndex + width] != 0) + { + pixel = color; + } + newPixels[pixelIndex] = pixel; + } + + pixelIndex++; + } + } + + img.setRGB(0, 0, width, height, newPixels, 0, width); + } }