Advertisement
Unh0ly_Tigg

ImgToLua.java

Sep 22nd, 2013
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.Arrays;
  5.  
  6. import javax.imageio.ImageIO;
  7.  
  8.  
  9. public class ImgToLua {
  10.     public static void main(String[] args) throws IOException {
  11.         StringBuilder result = new StringBuilder("local img = {");
  12.         String newLine = ",\n             ";
  13.         BufferedImage img = ImageIO.read(new File("")); // Change this to the file location for the image to convert
  14.         for (int i = 0; i < img.getWidth(); i++) {
  15.             if (i != 0)
  16.                 result.append(newLine);
  17.             readLine(img, i, result);
  18.         }
  19.         result.append("}");
  20.         System.out.println(result.toString());
  21.     }
  22.    
  23.     public static void readLine(BufferedImage src, int row, StringBuilder dest) {
  24.         StringBuilder line = new StringBuilder("{");
  25.         for (int i = 0; i < src.getHeight(); i++) {
  26.             if (i != 0)
  27.                 line.append(",");
  28.             line.append(convert(src.getRGB(row, i)));
  29.         }
  30.         line.append("}");
  31.         dest.append(line);
  32.     }
  33.    
  34.     public static String convert(int rgb) {
  35.         return "0x" + pad(Integer.toHexString(rgb & 0xFFFFFF), 6).toUpperCase();
  36.     }
  37.    
  38.     public static String pad(String hex, int digits) {
  39.         if (hex.length() >= digits)
  40.             return hex;
  41.         char[] c = new char[digits - hex.length()];
  42.         Arrays.fill(c, '0');
  43.         return new String(c) + hex;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement