Add new toBufferedOutline method to SpritePixels

Add method that returns item image with outline to SpritePixels.

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-05-20 00:29:40 +02:00
committed by Adam
parent 315a08051d
commit af7e76502b
2 changed files with 81 additions and 0 deletions

View File

@@ -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);
}

View File

@@ -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);
}
}