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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bitmapencoder;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.*;
/**
*
* @author Rodot
*/
public class BitmapEncoder {
private BufferedImage inputImage;
private BufferedImage outputImage;
private String bitmapName = "myBitmap";
private boolean hexFormatting = false;
private boolean wrapping = true;
private static File[] filesToConvert;
private static int imageCounter;
private static int fileCount;
protected void BitmapEncoder() {
}
protected void open(File file) {
try {
inputImage = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
if (inputImage != null) {
if ((inputImage.getWidth() > 200) || (inputImage.getHeight() > 200)) {
inputImage = null;
return;
}
outputImage = deepCopy(inputImage);
}
}
protected void threshold(int thres) {
if (inputImage == null) {
return;
}
for (int y = 0; y < inputImage.getHeight(); y++) {
for (int x = 0; x < inputImage.getWidth(); x++) {
int rgb = inputImage.getRGB(x, y);
int red = (rgb >> 16) & 0x000000FF;
int green = (rgb >> 8) & 0x000000FF;
int blue = (rgb) & 0x000000FF;
int value = red + green + blue;
if (value > thres) {
outputImage.setRGB(x, y, 0x00FFFFFF);
} else {
outputImage.setRGB(x, y, 0);
}
}
}
}
protected String generateOutput(int thres) {
if (inputImage == null) {
return "";
}
String output = "";
output = output.concat("const byte ");
output = output.concat(bitmapName);
output = output.concat("[] PROGMEM = {");
int width = ((inputImage.getWidth() - 1) / 8 + 1) * 8; //round to the closest larger multiple of 8
output = output.concat(width + "," + inputImage.getHeight() + ",");
if (wrapping) {
output = output.concat("\n");
}
for (int y = 0; y < inputImage.getHeight(); y++) {
for (int x = 0; x < inputImage.getWidth(); x += 8) {
if (hexFormatting) {
output = output.concat("0x");
} else {
output = output.concat("B");
}
int thisByte = 0;
for (int b = 0; b < 8; b++) {
int value = 0xFFFF;
if (x + b < inputImage.getWidth()) {
int rgb = inputImage.getRGB(x + b, y);
int red = (rgb >> 16) & 0x000000FF;
int green = (rgb >> 8) & 0x000000FF;
int blue = (rgb) & 0x000000FF;
value = red + green + blue;
}
if (hexFormatting) {
thisByte *= 2;
if (value < thres) {
thisByte++;
}
} else {//binary formattning
if (value < thres) {
output = output.concat("1");
} else {
output = output.concat("0");
}
}
}
if (hexFormatting) {
output = output.concat(Integer.toString(thisByte, 16).
toUpperCase());
}
output = output.concat(",");
}
if (wrapping) {
output = output.concat("\n");
}
}
output = output.concat("};");
return output;
}
protected static BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
protected static int countFiles(File[] list, boolean recursed) {
if (list == null) {
return 0;
}
if (!recursed) {
fileCount = 0;
}
for (File f : list) {
if (f.isDirectory()) {
countFiles(f.listFiles(), true);
} else {
if (isImage(f)) {
fileCount++;
}
}
}
return fileCount;
}
protected static boolean isImage(File f) {
boolean isImage = false;
if (f.getName().endsWith(".bmp") || f.getName().endsWith(".png")
|| f.getName().endsWith(".jpeg")
|| f.getName().endsWith(".jpg")) {
isImage = true;
}
return isImage;
}
protected static File[] processSelectedFiles(File[] list, boolean recursed) {
if (!recursed) {
filesToConvert = new File[countFiles(list, false)];
imageCounter = 0;
}
if (list == null) {
return null;
}
for (File f : list) {
if (f.isDirectory()) {
processSelectedFiles(f.listFiles(), true);
} else {
if (isImage(f)) {
filesToConvert[imageCounter] = f;
imageCounter++;
}
}
}
return filesToConvert;
}
protected BufferedImage getInputImage() {
return inputImage;
}
protected BufferedImage getOutputImage() {
return outputImage;
}
protected String getBitmapName() {
return bitmapName;
}
protected boolean isHexFormatting() {
return hexFormatting;
}
protected boolean isWrapping() {
return wrapping;
}
protected void setInputImage(BufferedImage inputImage) {
this.inputImage = inputImage;
}
protected void setOutputImage(BufferedImage outputImage) {
this.outputImage = outputImage;
}
protected void setBitmapName(String bitmapName) {
this.bitmapName = bitmapName;
}
protected void setHexFormatting(boolean hexFormatting) {
this.hexFormatting = hexFormatting;
}
protected void setWrapping(boolean wrapping) {
this.wrapping = wrapping;
}
}
|