Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.Arrays;
- import javax.imageio.ImageIO;
- public class ImgToLua {
- public static void main(String[] args) throws IOException {
- StringBuilder result = new StringBuilder("local img = {");
- String newLine = ",\n ";
- BufferedImage img = ImageIO.read(new File("")); // Change this to the file location for the image to convert
- for (int i = 0; i < img.getWidth(); i++) {
- if (i != 0)
- result.append(newLine);
- readLine(img, i, result);
- }
- result.append("}");
- System.out.println(result.toString());
- }
- public static void readLine(BufferedImage src, int row, StringBuilder dest) {
- StringBuilder line = new StringBuilder("{");
- for (int i = 0; i < src.getHeight(); i++) {
- if (i != 0)
- line.append(",");
- line.append(convert(src.getRGB(row, i)));
- }
- line.append("}");
- dest.append(line);
- }
- public static String convert(int rgb) {
- return "0x" + pad(Integer.toHexString(rgb & 0xFFFFFF), 6).toUpperCase();
- }
- public static String pad(String hex, int digits) {
- if (hex.length() >= digits)
- return hex;
- char[] c = new char[digits - hex.length()];
- Arrays.fill(c, '0');
- return new String(c) + hex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement