* client: add BufferedImage -> IndexedSprite converter to ImageUtil

* client: change ClanManager to use ImageUtil image converter

* emojis: add emoji images

* client: add emoji plugin

Replaces various emoticons entered in public/friends/private chats with their corresponding emoji images.

* added plugin type to emojiplugin
This commit is contained in:
vanni
2019-05-12 20:33:06 -04:00
committed by Kyleeld
parent b69578d93d
commit 4a94c2fecb
52 changed files with 347 additions and 54 deletions

View File

@@ -30,10 +30,6 @@ import com.google.common.cache.LoadingCache;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@@ -152,8 +148,8 @@ public class ClanManager
for (int i = 0; i < CLANCHAT_IMAGES.length; i++, curPosition++)
{
final int resource = CLANCHAT_IMAGES[i];
clanChatImages[i] = rgbaToIndexedBufferedImage(clanChatImageFromSprite(spriteManager.getSprite(resource, 0)));
newModIcons[curPosition] = createIndexedSprite(client, clanChatImages[i]);
clanChatImages[i] = clanChatImageFromSprite(spriteManager.getSprite(resource, 0));
newModIcons[curPosition] = ImageUtil.getImageIndexedSprite(clanChatImages[i], client);
}
client.setModIcons(newModIcons);
@@ -166,54 +162,6 @@ public class ClanManager
return cleaned.replace('\u00A0', ' ');
}
private static IndexedSprite createIndexedSprite(final Client client, final BufferedImage bufferedImage)
{
final IndexColorModel indexedCM = (IndexColorModel) bufferedImage.getColorModel();
final int width = bufferedImage.getWidth();
final int height = bufferedImage.getHeight();
final byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
final int[] palette = new int[indexedCM.getMapSize()];
indexedCM.getRGBs(palette);
final IndexedSprite newIndexedSprite = client.createIndexedSprite();
newIndexedSprite.setPixels(pixels);
newIndexedSprite.setPalette(palette);
newIndexedSprite.setWidth(width);
newIndexedSprite.setHeight(height);
newIndexedSprite.setOriginalWidth(width);
newIndexedSprite.setOriginalHeight(height);
newIndexedSprite.setOffsetX(0);
newIndexedSprite.setOffsetY(0);
return newIndexedSprite;
}
private static BufferedImage rgbaToIndexedBufferedImage(final BufferedImage sourceBufferedImage)
{
final BufferedImage indexedImage = new BufferedImage(
sourceBufferedImage.getWidth(),
sourceBufferedImage.getHeight(),
BufferedImage.TYPE_BYTE_INDEXED);
final ColorModel cm = indexedImage.getColorModel();
final IndexColorModel icm = (IndexColorModel) cm;
final int size = icm.getMapSize();
final byte[] reds = new byte[size];
final byte[] greens = new byte[size];
final byte[] blues = new byte[size];
icm.getReds(reds);
icm.getGreens(greens);
icm.getBlues(blues);
final WritableRaster raster = indexedImage.getRaster();
final int pixel = raster.getSample(0, 0, 0);
final IndexColorModel resultIcm = new IndexColorModel(8, size, reds, greens, blues, pixel);
final BufferedImage resultIndexedImage = new BufferedImage(resultIcm, raster, sourceBufferedImage.isAlphaPremultiplied(), null);
resultIndexedImage.getGraphics().drawImage(sourceBufferedImage, 0, 0, null);
return resultIndexedImage;
}
private static BufferedImage clanChatImageFromSprite(final BufferedImage clanSprite)
{
final BufferedImage clanChatCanvas = ImageUtil.resizeCanvas(clanSprite, CLANCHAT_IMAGE_DIMENSION.width, CLANCHAT_IMAGE_DIMENSION.height);

View File

@@ -0,0 +1,109 @@
/*
* Copyright (c) 2019, Lotto <https://github.com/devLotto>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.emojis;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import net.runelite.client.util.ImageUtil;
public enum Emoji
{
SLIGHT_SMILE(":)"),
JOY("=')"),
COWBOY("3:)"),
BLUSH("^_^"),
SMILE(":D"),
GRINNING("=D"),
WINK(";)"),
STUCK_OUT_TONGUE_CLOSED_EYES("X-P"),
STUCK_OUT_TONGUE(":P"),
YUM("=P~"),
HUGGING("<gt>:D<lt>"), // >:D<
TRIUMPH(":<gt>"), // :>
THINKING(":-?"),
CONFUSED(":/"),
NEUTRAL_FACE("=|"),
EXPRESSIONLESS(":|"),
UNAMUSED(":-|"),
SLIGHT_FROWN(":("),
FROWNING2("=("),
CRY(":'("),
SOB(":_("),
FLUSHED(":$"),
ZIPPER_MOUTH(":-#"),
PERSEVERE("<gt>_<lt>"), // >_<
SUNGLASSES("8-)"),
INNOCENT("O:)"),
SMILING_IMP("<gt>:)"), // >:)
RAGE("<gt>:("), // >:(
HUSHED(":-O"),
OPEN_MOUTH(":O"),
SCREAM(":-@"),
SEE_NO_EVIL("X_X"),
DANCER("\\:D/"),
OK_HAND("(Ok)"),
THUMBSUP("(Y)"),
THUMBSDOWN("(N)"),
HEARTS("<lt>3"), // <3
BROKEN_HEART("<lt>/3"), // </3
ZZZ("Zzz"),
FISH("<lt><gt><lt>"), // <><
CAT(":3"),
DOG("=3"),
CRAB("V(;,;)V"),
FORK_AND_KNIFE("--E"),
COOKING("--(o)"),
PARTY_POPPER("@@@");
private static final Map<String, Emoji> emojiMap = new HashMap<>();
private final String trigger;
static
{
final Emoji[] emojis = values();
for (final Emoji emoji : emojis)
{
emojiMap.put(emoji.trigger, emoji);
}
}
Emoji(String trigger)
{
this.trigger = trigger;
}
public static Emoji getEmoji(String trigger)
{
return emojiMap.get(trigger);
}
public BufferedImage loadImage()
{
return ImageUtil.getResourceStreamFromClass(getClass(), this.name().toLowerCase() + ".png");
}
}

View File

@@ -0,0 +1,161 @@
/*
* Copyright (c) 2019, Lotto <https://github.com/devLotto>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.emojis;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import javax.inject.Inject;
import joptsimple.internal.Strings;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.GameState;
import net.runelite.api.IndexedSprite;
import net.runelite.api.MessageNode;
import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.GameStateChanged;
import net.runelite.client.chat.ChatMessageManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.PluginType;
import net.runelite.client.util.ImageUtil;
@PluginDescriptor(
name = "Emojis",
description = "Replaces common emoticons such as :) with their corresponding emoji in the chat",
enabledByDefault = false,
type = PluginType.UTILITY
)
@Slf4j
public class EmojiPlugin extends Plugin
{
@Inject
private Client client;
@Inject
private ChatMessageManager chatMessageManager;
private int modIconsStart = -1;
@Override
protected void startUp()
{
}
@Override
protected void shutDown()
{
}
@Subscribe
public void onGameStateChanged(GameStateChanged gameStateChanged)
{
if (gameStateChanged.getGameState() == GameState.LOGGED_IN
&& modIconsStart == -1)
{
loadEmojiIcons();
}
}
private void loadEmojiIcons()
{
final Emoji[] emojis = Emoji.values();
final IndexedSprite[] modIcons = client.getModIcons();
final IndexedSprite[] newModIcons = Arrays.copyOf(modIcons, modIcons.length + emojis.length);
modIconsStart = modIcons.length;
for (int i = 0; i < emojis.length; i++)
{
final Emoji emoji = emojis[i];
try
{
final BufferedImage image = emoji.loadImage();
final IndexedSprite sprite = ImageUtil.getImageIndexedSprite(image, client);
newModIcons[modIconsStart + i] = sprite;
}
catch (Exception ex)
{
log.warn("Failed to load the sprite for emoji " + emoji, ex);
}
}
client.setModIcons(newModIcons);
}
@Subscribe
public void onChatMessage(ChatMessage chatMessage)
{
if (client.getGameState() != GameState.LOGGED_IN)
{
return;
}
switch (chatMessage.getType())
{
case PUBLICCHAT:
case MODCHAT:
case FRIENDSCHAT:
case PRIVATECHAT:
case PRIVATECHATOUT:
break;
default:
return;
}
final String message = chatMessage.getMessage();
final String[] messageWords = message.split(" ");
boolean editedMessage = false;
for (int i = 0; i < messageWords.length; i++)
{
final Emoji emoji = Emoji.getEmoji(messageWords[i]);
if (emoji == null)
{
continue;
}
final int emojiId = modIconsStart + emoji.ordinal();
messageWords[i] = "<img=" + emojiId + ">";
editedMessage = true;
}
// If we haven't edited the message any, don't update it.
if (!editedMessage)
{
return;
}
final String newMessage = Strings.join(messageWords, " ");
final MessageNode messageNode = chatMessage.getMessageNode();
messageNode.setRuneLiteFormatMessage(newMessage);
chatMessageManager.update(messageNode);
client.refreshChat();
}
}

View File

@@ -24,6 +24,7 @@
*/
package net.runelite.client.util;
import com.google.common.primitives.Ints;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
@@ -34,12 +35,15 @@ import java.awt.image.DirectColorModel;
import java.awt.image.PixelGrabber;
import java.awt.image.RescaleOp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.GrayFilter;
import java.util.function.Predicate;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.IndexedSprite;
import net.runelite.api.SpritePixels;
/**
@@ -448,4 +452,74 @@ public class ImageUtil
return client.createSpritePixels(pixels, image.getWidth(), image.getHeight());
}
/**
* Converts an image into an {@code IndexedSprite} instance.
*
* The passed in image can only have at max 255 different colors.
*
* @param image The image to be converted
* @param client Current client instance
* @return The image as an {@code IndexedSprite}
*/
public static IndexedSprite getImageIndexedSprite(BufferedImage image, Client client)
{
final byte[] pixels = new byte[image.getWidth() * image.getHeight()];
final List<Integer> palette = new ArrayList<>();
/*
When drawing the indexed sprite, palette idx 0 is seen as fully transparent,
so pad the palette out so that our colors start at idx 1.
*/
palette.add(0);
final int[] sourcePixels = image.getRGB(0, 0,
image.getWidth(), image.getHeight(),
null, 0, image.getWidth());
/*
Build a color palette and assign the pixels to positions in the palette.
*/
for (int j = 0; j < sourcePixels.length; j++)
{
final int argb = sourcePixels[j];
final int a = (argb >> 24) & 0xFF;
final int rgb = argb & 0xFF_FF_FF;
// Default to not drawing the pixel.
int paletteIdx = 0;
// If the pixel is fully opaque, draw it.
if (a == 0xFF)
{
paletteIdx = palette.indexOf(rgb);
if (paletteIdx == -1)
{
paletteIdx = palette.size();
palette.add(rgb);
}
}
pixels[j] = (byte) paletteIdx;
}
if (palette.size() > 256)
{
throw new RuntimeException("Passed in image had " + (palette.size() - 1)
+ " different colors, exceeding the max of 255.");
}
final IndexedSprite sprite = client.createIndexedSprite();
sprite.setPixels(pixels);
sprite.setPalette(Ints.toArray(palette));
sprite.setWidth(image.getWidth());
sprite.setHeight(image.getHeight());
sprite.setOriginalWidth(image.getWidth());
sprite.setOriginalHeight(image.getHeight());
sprite.setOffsetX(0);
sprite.setOffsetY(0);
return sprite;
}
}

View File

@@ -0,0 +1 @@
The images in this folder are derivatives of "twemoji" (https://github.com/twitter/twemoji) by "Twitter, Inc", used under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). The derivatives are licensed under the same license.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB