-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEspColorTransform.java
More file actions
100 lines (74 loc) · 3.06 KB
/
EspColorTransform.java
File metadata and controls
100 lines (74 loc) · 3.06 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import javax.imageio.ImageIO;
public class EspColorTransform {
static int color565(int r, int g, int b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
static void test(int r, int g, int b) {
String hex = Integer.toHexString( color565(r, g, b) );
for(int i=hex.length(); i < 4; i++) { hex = "0"+hex; }
System.out.println( "0x"+hex.toUpperCase() );
}
public static final int MODE_64K = 1;
static void treatImage(String filename) throws Exception {
File f = new File(filename);
String outFname = f.getName().toUpperCase();
outFname = outFname.substring(0, outFname.lastIndexOf("."));
outFname += ".PCT";
int mode = MODE_64K; // 64K picture
System.out.println(outFname);
PrintStream fout = new PrintStream( new FileOutputStream(new File("./data", outFname)) );
if ( mode == MODE_64K ) {
// 40967 bytes for 160x128px 64K file -> 7 bytes header (64K + WW + HH)
fout.print("64K");
BufferedImage img = ImageIO.read(f);
BufferedImage workingCopy = img;
int wwidth = workingCopy.getWidth();
int wheight = workingCopy.getHeight();
if ( img.getWidth() > 160 ) {
wwidth = 160;
wheight = 128;
Image scaled = img.getScaledInstance(wwidth, wheight, Image.SCALE_SMOOTH);
workingCopy = new BufferedImage(wwidth, wheight, BufferedImage.TYPE_INT_ARGB);
workingCopy.getGraphics().drawImage(scaled, 0, 0, null);
}
fout.write( (wwidth/256) );
fout.write( (wwidth%256) );
fout.write( (wheight/256) );
fout.write( (wheight%256) );
int[] rgb = new int[ wwidth * wheight ];
workingCopy.getRGB(0, 0, wwidth, wheight, rgb, 0, wwidth);
int cpt = 0;
for(int y=0; y < wheight; y++) {
for(int x=0; x < wwidth; x++) {
int rp = rgb[ (y*wwidth)+x ];
int a = (rp >> 24) & 0xFF; // should always be 255
int r = (rp >> 16) & 0xFF;
int g = (rp >> 8) & 0xFF;
int b = (rp) & 0xFF;
int c64k = color565(r, g, b);
if ( cpt == 0 ) {
System.out.println("-> "+a+", "+r+", "+g+", "+b);
System.out.println( Integer.toHexString(c64k) );
}
fout.write( c64k / 256 );
fout.write( c64k % 256 );
cpt++;
}
}
}
fout.flush();
fout.close();
}
public static void main(String[] args) throws Exception {
test( 0,0,255 );
test( 128,128,128 );
if ( args != null && args.length == 1 ) {
treatImage(args[0]);
}
}
}