-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMAGETOLED.JAVA
69 lines (61 loc) · 2.26 KB
/
IMAGETOLED.JAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class ImageToLED { // To be used with a 64x32 LED panel code - Turns 64x32 pngs into arrays of RGB values.
static String toString(int x) {
String s = Integer.toString(x, 16);
while (s.length() < 2) s = "0" + s;
return "0x" + s;
}
static String inputName = "TEST.png"; // Input file image
static String outputName = "output.txt"; // Output text file (contains formatted arrays of RGB components)
public static void main(String[] args) throws IOException {
BufferedImage bi = ImageIO.read(new File(inputName));
if (bi.getWidth() != 64) System.err.println("INVALID WIDTH");
if (bi.getHeight() != 32) System.err.println("INVALID HEIGHT");
FileWriter pw = new FileWriter(outputName); // Prints 3 java array codes
Color[][] grid = new Color[64][32];
for (int i = 0; i < 64; i++) {
for (int j = 0; j < 32; j++) {
grid[i][j] = new Color(bi.getRGB(i, j));
}
}
pw.write("uint8_t red[n][m] = {");
for (int i = 0; i < 32; i++) {
if (i > 0) pw.write(", \n");
pw.write("{");
for (int j = 0; j < 64; j++) {
if (j > 0) pw.write(", ");
pw.write(toString(grid[j][i].getRed()));
}
pw.write("}");
}
pw.write("};\n\n");
pw.write("uint8_t green[n][m] = {");
for (int i = 0; i < 32; i++) {
if (i > 0) pw.write(", \n");
pw.write("{");
for (int j = 0; j < 64; j++) {
if (j > 0) pw.write(", ");
pw.write(toString(grid[j][i].getGreen()));
}
pw.write("}");
}
pw.write("};\n\n");
pw.write("uint8_t blue[n][m] = {");
for (int i = 0; i < 32; i++) {
if (i > 0) pw.write(", \n");
pw.write("{");
for (int j = 0; j < 64; j++) {
if (j > 0) pw.write(", ");
pw.write(toString(grid[j][i].getBlue()));
}
pw.write("}");
}
pw.write("};\n\n");
pw.close();
}
}