forked from lvgl/lv_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_conv_core.php
244 lines (189 loc) · 7.33 KB
/
img_conv_core.php
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
<?php
$offline = 0;
if (!isset($_SERVER["HTTP_HOST"])) {
parse_str($argv[1], $_POST);
$offline = 1;
}
if($offline == 0) {
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$img_file = $_FILES["img_file"]["tmp_name"];
$img_file_name = $_FILES["img_file"]["name"];
$output_name = $_POST["name"];
$transp = $_POST["transp"];
$format = $_POST["format"];
}
else {
if(isset($_POST["name"])) {
$output_name = $_POST["name"];
} else {
echo("Mising Name\n");
exit(0);
}
if(isset($_POST["img"])) {
$img_file = $_POST["img"];
$img_file_name = $_POST["img"];
} else {
echo("Mising image file\n");
exit(0);
}
if(isset($_POST["format"])) {
$format = $_POST["format"];
} else {
$format = "c_array";
}
if(isset($_POST["transp"])) {
$transp = $_POST["transp"];
} else {
$transp = "none";
}
}
$w = 0;
$h = 0;
$size = getimagesize($img_file);
$w = $size[0];
$h = $size[1];
$ext = pathinfo($img_file_name, PATHINFO_EXTENSION);
if($ext == "png") $img = imagecreatefrompng($img_file);
else if($ext == "bmp") $img = imagecreatefrombmp($img_file);
else if($ext == "jpg") $img = imagecreatefromjpeg($img_file);
else if($ext == "jpeg") $img = imagecreatefromjpeg($img_file);
else echo("$ext is a not supported image type. use png, jpg, jpeg or bmp");
if($format == "c_array") conv_c_src();
else conv_bin_rgb();
function conv_c_src()
{
global $w;
global $h;
global $output_name;
global $img;
global $transp;
global $c_src;
$c_src .= "#include <stdint.h>
#include \"lv_conf.h\"
#include \"lvgl/lv_draw/lv_draw.h\"
static const uint8_t " . $output_name . "_pixel_map[] = {\n";
$c_src8 = "\n#if LV_COLOR_DEPTH == 1 || LV_COLOR_DEPTH == 8\n";
if ($transp != "alpha") $c_src8 .= "/*Pixel format: Red: 3 bit, Green: 3 bit, Blue: 2 bit*/\n";
if ($transp == "alpha") $c_src8 .= "/*Pixel format: Alpha 8 bit, Red: 3 bit, Green: 3 bit, Blue: 2 bit*/\n";
$c_src16 = "\n\n#elif LV_COLOR_DEPTH == 16\n";
if ($transp != "alpha") $c_src16 .= "/*Pixel format: Red: 5 bit, Green: 6 bit, Blue: 5 bit*/\n";
if ($transp == "alpha") $c_src16 .= "/*Pixel format: Alpha 8 bit, Red: 5 bit, Green: 6 bit, Blue: 5 bit*/\n";
$c_src24 = "\n\n#elif LV_COLOR_DEPTH == 24\n";
if ($transp != "alpha") $c_src24 .= "/*Pixel format: Fix 0xFF: 8 bit, Red: 8 bit, Green: 8 bit, Blue: 8 bit*/\n";
if ($transp == "alpha") $c_src24 .= "/*Pixel format: Alpha 8 bit, Red: 8 bit, Green: 8 bit, Blue: 8 bit*/\n";
$a_str = "";
for($y = 0; $y < $h; $y++) {
$c_src8 .= "\n ";
$c_src16 .= "\n ";
$c_src24 .= "\n ";
for($x = 0; $x < $w; $x++) {
$c = imagecolorat($img, $x, $y);
if($transp == "alpha") {
$a = ($c & 0xff000000) >> 23; /*Alpha is 7 bit*/
if($a & 0x02) $a |= 0x01; /*Repeate the last bit: 0000000 -> 00000000; 1111110 -> 11111111*/
$a = 255 - $a;
$a_str = "0x" . str_pad(dechex($a), 2, '0', STR_PAD_LEFT) . ", ";
}
$r = ($c & 0x00ff0000) >> 16;
$g = ($c & 0x0000ff00) >> 8;
$b = ($c & 0x000000ff) >> 0;
$c8 = ($r & 0xE0) | (($g & 0xE0) >> 3) | ($b >> 6); //RGB332
$c_src8 .= "0x" . str_pad(dechex($c8), 2, '0', STR_PAD_LEFT). ", ";
$c_src8 .= $a_str;
$c16 = (($r & 0xF8) << 8) | (($g & 0xFC) << 3) | (($b & 0xF8) >> 3); //RGR565
$c_src16 .= "0x" . str_pad(dechex(($c16 & 0x00FF)), 2, '0', STR_PAD_LEFT). ", ";
$c_src16 .= "0x" . str_pad(dechex(($c16 & 0xFF00) >> 8), 2, '0', STR_PAD_LEFT). ", ";
$c_src16 .= $a_str;
$c24 = ($r << 16) | ($g << 8) | ($b); //RGR888
$c_src24 .= "0x" . str_pad(dechex(($c24 & 0x0000FF)), 2, '0', STR_PAD_LEFT). ", ";
$c_src24 .= "0x" . str_pad(dechex(($c24 & 0x00FF00) >> 8), 2, '0', STR_PAD_LEFT). ", ";
$c_src24 .= "0x" . str_pad(dechex(($c24 & 0xFF0000) >> 16), 2, '0', STR_PAD_LEFT). ", ";
if($transp == "alpha") $c_src24 .= $a_str;
else $c_src24 .= "0xff, "; /*Padding*/
}
}
$c_src .= $c_src8;
$c_src .= $c_src16;
$c_src .= $c_src24;
$c_src .= "
#else
#error \"$output_name " . "image :invalid color depth (check LV_COLOR_DEPTH in lv_conf.h)\"
#endif
};\n\n";
$c_src .= "
const lv_img_t $output_name = {
.header.w = $w,\t\t\t/*Image width in pixel count*/
.header.h = $h,\t\t\t/*Image height in pixel count*/\n";
if($transp == "alpha") $c_src .= " .header.alpha_byte = 1,\t\t/*Alpha byte added to every pixel*/\n";
else $c_src .= " .header.alpha_byte = 0,\t\t/*No alpha byte*/\n";
if($transp == "chroma") $c_src .= " .header.chroma_keyed = 1,\t/*LV_COLOR_TRANSP (lv_conf.h) pixels will be transparent*/\n";
else $c_src .= " .header.chroma_keyed = 0,\t/*No chroma keying*/\n";
$c_src .= " .header.format = LV_IMG_FORMAT_INTERNAL_RAW,\t/*It's a variable compiled into the code*/\n";
$c_src .= " .pixel_map = " . $output_name . "_pixel_map\t/*Pointer the array of image pixels.*/
};\n\n";
download($output_name, $c_src);
}
function conv_bin_rgb()
{
global $w;
global $h;
global $output_name;
global $img;
global $transp;
global $format;
$header = pack("v", $w << 20) | pack("v", $h << 8);
//echo($transp . "<br><br><br><br>");
if($transp == "chroma") $chroma = 1;
if($transp == "alpha") $alpha = 1;
if($format == "bin_rgb332") $img_format = 2;
if($format == "bin_rgb565") $img_format = 3;
if($format == "bin_rgb888") $img_format = 4;
$header = pack("V", $chroma | $alpha << 1 | $img_format << 2 | $w << 8 | $h << 20);
$bin = $header;
$a = 255;
for($y = 0; $y < $h; $y++) {
for($x = 0; $x < $w; $x++) {
$c = imagecolorat($img, $x, $y);
$r = ($c & 0x00ff0000) >> 16;
$g = ($c & 0x0000ff00) >> 8;
$b = ($c & 0x000000ff) >> 0;
if($transp == "alpha") {
$a = ($c & 0xff000000) >> 23; /*Alpha is 7 bit*/
if($a & 0x02) $a |= 0x01; /*Repeate the last bit: 0000000 -> 00000000; 1111110 -> 11111111*/
$a = 255 - $a;
}
if($format == "bin_rgb332") {
$c8 = ($r & 0xE0) | (($g & 0xE0) >> 3) | ($b >> 6); //RGB332
$bin .= pack("C", $c8);
if($transp == "alpha") $bin .= pack("C", $a);
} else if($format == "bin_rgb565") {
$c16 = (($r & 0xF8) << 8) | (($g & 0xFC) << 3) | (($b & 0xF8) >> 3); //RGR565
$bin .= pack("v", $c16);
if($transp == "alpha") $bin .= pack("C", $a);
} else if($format == "bin_rgb888") {
$c24 = ($a << 24) | ($r << 16) | ($g << 8) | ($b); //RGR888
$bin .= pack("V", $c24);
}
}
}
$output_name .= ".bin";
download($output_name, $bin);
}
function download($name, $content)
{
global $offline;
$file_name = $name.'.c';
if($offline) {
$file = fopen($file_name, "w");
fwrite($file, $content);
fclose($file);
} else {
header('Content-Type: application/text');
header('Content-disposition: attachment; filename='.$file_name);
header('Content-Length: ' . strlen($content));
echo($content);
}
}
?>