-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg2lcd.c
345 lines (310 loc) · 12.4 KB
/
img2lcd.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* filename: img2lcd.c
* Author: KaliAssistant
* Github: https://github.com/KaliAssistant
*
* */
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STBI_NO_PSD
#define STBI_NO_GIF
#define STBI_NO_PIC
#define STBI_NO_PNM
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> // For getopt()
#include <stdbool.h>
#include "stb_image.h"
#include "stb_image_write.h"
unsigned short* convert_rgb565(int width, int height, bool a2bg, const unsigned char *rgba_data);
unsigned char* convert_rgb888(int width, int height, bool a2bg, const unsigned char *rgba_data);
unsigned char* convert_xbm_1bit(int width, int height, bool a2bg, const unsigned char *rgba_data);
int save_rgb565_data(const char* output_file, int width, int height, unsigned short* rgb565_data);
int save_rgb565_bin(const char* output_file, int width, int height, unsigned short* rgb565_data);
int save_xbm_1bit_xbm(const char* output_file, int width, int height, unsigned char* xbm_data);
int save_rgb888_data(const char* output_file, int width, int height, unsigned char* rgb888_data);
int save_rgb888_bin(const char* output_file, int width, int height, unsigned char* rgb888_data);
int main(int argc, char* argv[]) {
char *input_file = NULL;
char *output_file = NULL;
char *color_format = NULL;
char *output_format = NULL;
char *alpha_2_bgcolor = NULL;
int opt;
while ((opt = getopt(argc, argv, "i:o:b:f:F:")) != -1) {
switch (opt) {
case 'i':
input_file = optarg;
break;
case 'o':
output_file = optarg;
break;
case 'b':
alpha_2_bgcolor = optarg;
break;
case 'f':
color_format = optarg;
break;
case 'F':
output_format = optarg;
break;
default:
fprintf(stderr, "Usage: %s -i <input image> -o output -b <alpha to bg color> [BLACK|WHITE] -f [RGB565|RGB888|XBM] -F [bin|h|xbm]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (!input_file || !output_file || !color_format || !output_format || !alpha_2_bgcolor) {
fprintf(stderr, "Missing required arguments.\n");
exit(EXIT_FAILURE);
}
int width, height, channels;
unsigned char *rgb_data = stbi_load(input_file, &width, &height, &channels, 4);
if (!rgb_data) {
fprintf(stderr, "Cannot load file:%s\n", input_file);
exit(EXIT_FAILURE);
}
bool a2bg;
if (strcmp(alpha_2_bgcolor, "WHITE") ==0) {
a2bg = true;
} else if (strcmp(alpha_2_bgcolor, "BLACK") ==0) {
a2bg = false;
} else {
fprintf(stderr, "Unsupported bgcolor format : %s\n", alpha_2_bgcolor);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
if (strcmp(color_format, "XBM") ==0) {
if (strcmp(output_format, "xbm") ==0) {
if (save_xbm_1bit_xbm(output_file, width, height, convert_xbm_1bit(width, height, a2bg, rgb_data)) == -1) {
fprintf(stderr, "Cannot write file:%s\n", output_file);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
printf("Conversion completed: %s\n", output_file);
stbi_image_free(rgb_data);
} else {
fprintf(stderr, "Unsupported output format: %s\n", output_format);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
} else if (strcmp(color_format, "RGB565") ==0 ) {
if (strcmp(output_format, "h") ==0) {
if (save_rgb565_data(output_file, width, height, convert_rgb565(width, height, a2bg, rgb_data)) == -1) {
fprintf(stderr, "Cannot write file:%s\n", output_file);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
printf("Conversion completed: %s\n", output_file);
stbi_image_free(rgb_data);
} else if (strcmp(output_format, "bin") ==0) {
if (save_rgb565_bin(output_file, width, height, convert_rgb565(width, height, a2bg, rgb_data)) == -1) {
fprintf(stderr, "Cannot write file:%s\n", output_file);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
printf("Conversion completed: %s\n", output_file);
stbi_image_free(rgb_data);
} else {
fprintf(stderr, "Unsupported output format: %s\n", output_format);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
} else if (strcmp(color_format, "RGB888") ==0) {
if (strcmp(output_format, "h") ==0) {
if (save_rgb888_data(output_file, width, height, convert_rgb888(width, height, a2bg, rgb_data)) == -1) {
fprintf(stderr, "Cannot write file:%s\n", output_file);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
printf("Conversion completed: %s\n", output_file);
stbi_image_free(rgb_data);
} else if (strcmp(output_format, "bin") ==0) {
if (save_rgb888_bin(output_file, width, height, convert_rgb888(width, height, a2bg, rgb_data)) == -1) {
fprintf(stderr, "Cannot write file:%s\n", output_file);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
printf("Conversion completed: %s\n", output_file);
stbi_image_free(rgb_data);
} else {
fprintf(stderr, "Unsupported output format: %s\n", output_format);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
} else {
fprintf(stderr, "Unsupported color format: %s\n", color_format);
stbi_image_free(rgb_data);
exit(EXIT_FAILURE);
}
return 0;
}
unsigned char* convert_xbm_1bit(int width, int height, bool a2bg, const unsigned char *rgba_data) {
unsigned char* xbm_data = malloc((width + 7) / 8 * height);
for (int y = 0; y < height; ++y ) {
for (int x = 0; x < width; ++x) {
int index = y * width + x;
int byte_index = y * ((width + 7) / 8) + (x / 8);
int bit_index = x % 8;
unsigned char r = rgba_data[index * 4];
unsigned char g = rgba_data[index * 4 + 1];
unsigned char b = rgba_data[index * 4 + 2];
unsigned char a = rgba_data[index * 4 + 3];
// 將透明像素設置為白色,不透明像素設置為黑色
if (a2bg) {
if (a > 128 && (r + g + b) / 3 < 128) {
xbm_data[byte_index] |= (1 << bit_index);
} else {
xbm_data[byte_index] |= (0 << bit_index);
}
} else {
if (a > 128 && (r + g + b) / 3 < 128) {
xbm_data[byte_index] |= (0 << bit_index);
} else {
xbm_data[byte_index] |= (1 << bit_index);
}
}
}
}
return xbm_data;
}
unsigned short* convert_rgb565(int width, int height, bool a2bg, const unsigned char *rgba_data) {
unsigned short* rgb565_data = malloc(width * height *2);
// unsigned char *raw_rgb_data = malloc(width * height * 3);
int _px_count_ = 0;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int src_index = (y * width + x) * 4;
//int dest_index = (y * width + x) * 3;
unsigned char r = rgba_data[src_index];
unsigned char g = rgba_data[src_index + 1];
unsigned char b = rgba_data[src_index + 2];
unsigned char a = rgba_data[src_index + 3];
if (a2bg) {
unsigned char cr = r * a / 255 + 255 * (255 - a) / 255;
unsigned char cg = g * a / 255 + 255 * (255 - a) / 255;
unsigned char cb = b * a / 255 + 255 * (255 - a) / 255;
rgb565_data[_px_count_++] = ((cr & 0xF8) << 8) | ((cg & 0xFC) << 3) | (cb >> 3);
} else {
rgb565_data[_px_count_++] = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
}
}
return rgb565_data;
}
unsigned char* convert_rgb888(int width, int height, bool a2bg, const unsigned char *rgba_data) {
unsigned char* rgb888_data = malloc(width * height * 3);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int src_index = (y * width + x) * 4;
int dest_index = (y * width + x) * 3;
unsigned char r = rgba_data[src_index];
unsigned char g = rgba_data[src_index + 1];
unsigned char b = rgba_data[src_index + 2];
unsigned char a = rgba_data[src_index + 3];
if (a2bg) {
rgb888_data[dest_index] = r * a / 255 + 255 * (255 - a) / 255;
rgb888_data[dest_index + 1] = g * a / 255 + 255 * (255 - a) / 255;
rgb888_data[dest_index + 2] = b * a / 255 + 255 * (255 - a) / 255;
} else {
rgb888_data[dest_index] = r;
rgb888_data[dest_index + 1] = g;
rgb888_data[dest_index + 2] = b;
}
}
}
return rgb888_data;
}
int save_xbm_1bit_xbm(const char* output_file, int width, int height, unsigned char* xbm_data) {
FILE *file = fopen(output_file, "w");
if (!file) {
fprintf(stderr, "Could not open output file: %s\n", output_file);
return -1;
}
fprintf(file, "#define XBM_WIDTH %d\n", width);
fprintf(file, "#define XBM_HEIGHT %d\n", height);
fprintf(file, "const unsigned char xbm_data[] PROGMEM = {\n");
size_t out_data_size = (width + 7) / 8 * height;
for (size_t i = 0; i < out_data_size; ++i) {
if (i % 16 == 0 && i != 0) {
fprintf(file, "\n");
}
fprintf(file, "0x%02X", xbm_data[i]);
if (i != out_data_size) {
fprintf(file, ", ");
}
}
fprintf(file, "\n};\n");
fclose(file);
free(xbm_data);
return 0;
}
int save_rgb565_data(const char* output_file, int width, int height, unsigned short* rgb565_data) {
FILE *file = fopen(output_file, "w");
if (!file) {
fprintf(stderr, "Could not open output file: %s\n", output_file);
return -1;
}
fprintf(file, "#define IMAGE_WIDTH %d\n", width);
fprintf(file, "#define IMAGE_HEIGHT %d\n", height);
fprintf(file, "const unsigned short image_data[] PROGMEM = {\n");
size_t out_data_size = width * height;
for (size_t i = 0; i < out_data_size; ++i) {
if (i % 16 == 0 && i != 0) {
fprintf(file, "\n");
}
fprintf(file, "0x%04X", rgb565_data[i]);
if (i != out_data_size) {
fprintf(file, ", ");
}
}
fprintf(file, "\n};\n");
fclose(file);
free(rgb565_data);
return 0;
}
int save_rgb888_data(const char* output_file, int width, int height, unsigned char* rgb888_data) {
FILE *file = fopen(output_file, "wb");
if (!file) {
fprintf(stderr, "Could not open output file: %s\n", output_file);
return -1;
}
fprintf(file, "#define IMAGE_WIDTH %d\n", width);
fprintf(file, "#define IMAGE_HEIGHT %d\n", height);
fprintf(file, "const unsigned char image_data_24bit[] PROGMEM = {\n");
size_t out_data_size = width * height *3;
for (size_t i = 0; i < out_data_size; i+=3) {
if (i % 16 == 0 && i != 0) {
fprintf(file, "\n");
}
fprintf(file, "0x%08X", (rgb888_data[i] << 16) | (rgb888_data[i+1] << 8) | rgb888_data[i+2]);
if (i != out_data_size) {
fprintf(file, ", ");
}
}
fprintf(file, "\n};\n");
fclose(file);
free(rgb888_data);
return 0;
}
int save_rgb565_bin(const char* output_file, int width, int height, unsigned short* rgb565_data) {
FILE *file = fopen(output_file, "wb");
if (!file) {
fprintf(stderr, "Could not open output file: %s\n", output_file);
return -1;
}
fwrite(rgb565_data, sizeof(unsigned short), width * height, file);
fclose(file);
free(rgb565_data);
return 0;
}
int save_rgb888_bin(const char* output_file, int width, int height, unsigned char* rgb888_data) {
FILE *file = fopen(output_file, "wb");
if (!file) {
fprintf(stderr, "Could not open output file: %s\n", output_file);
return -1;
}
fwrite(rgb888_data, sizeof(unsigned char), width * height *3, file);
fclose(file);
free(rgb888_data);
return 0;
}