-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-to-lua.js
165 lines (124 loc) · 3.67 KB
/
image-to-lua.js
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
$ = function (el) { return document.getElementById(el); }
$hide = function (el) { el.style.display = 'none'; };
$show = function (el) { el.style.display = 'block'; };
$('init_code_1').value = `function init()
this:init_draw(img_width, img_height)
for x=1, img_width do
for y=1, img_height do
local c = colors[x+((y-1)*img_width)]
this:set_sprite_texel(base_x+x-1, base_y+img_height-y, c[1]/255, c[2]/255, c[3]/255, (c[4] ~= nil and c[4]/255 or 1.0))
end
end
end`;
$('init_code_2').value = `function init()
this:init_draw(img_width, img_height)
local j = 0
for i=1, #colors do
local ca = colors[i]
for n=1, ca[2] do
local c = ca[1]
this:set_sprite_texel(base_x + (j % img_width), base_y + img_height-math.floor(j / img_width)-1, c[1]/255, c[2]/255, c[3]/255, (c[4] ~= nil and c[4]/255 or 1.0))
j = j + 1
end
end
end`;
$('draw_image').value = `function step()
this:draw_sprite(0, 0, 0, 5, 5, base_x, base_y, base_x+img_width, base_y+img_height)
end`;
function image_to_lua() {
var el = $('temp_upload');
var canvas = $('temp_canvas');
let img = $('temp_img');
const include_alpha = $('include_alpha').checked;
$show($('loading'));
img.src = '';
if (el.files.length != 1)
return;
var f = el.files[0];
if (!f.type.match(/image.*/))
return;
var reader = new FileReader();
reader.onload = function (e) { img.src = e.target.result; }
reader.readAsDataURL(f);
img.onload = function (e) {
var w = img.width;
var h = img.height;
$('img_res').innerHTML = `${w}x${h}`;
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 512, 512);
ctx.drawImage(img, 0, 0);
var img_data = ctx.getImageData(0, 0, w, h);
var data = img_data.data;
var str_1 = str_2 = str_3 = '';
var vars = '';
var prev_ic = -1;
var var_counter = 0;
var colors = [];
var counter = 0;
var varname = '';
var s = 0;
for (var i = 0, n = data.length; i < n; i += 4) {
var red = data[i];
var green = data[i + 1];
var blue = data[i + 2];
var alpha = 0;
var color_str = '';
if (include_alpha) {
alpha = data[i + 3];
color_str = '{' + red + ',' + green + ',' + blue + ',' + alpha + '}';
} else
color_str = '{' + red + ',' + green + ',' + blue + '}';
var ic = red + (green << 8) + (blue << 16) + (alpha << 24);
var c = colors[ic];
if (ic == prev_ic || prev_ic == -1)
var_counter = var_counter + 1;
else {
// push changes
str_3 += '{' + varname + ',' + var_counter + '},';
var_counter = 1;
}
if (c === undefined) {
varname = 'c' + ++counter;
colors[ic] = varname;
vars += varname + '=' + color_str + ';';
} else
varname = c;
str_1 += color_str + ',';
str_2 += varname + ',';
prev_ic = ic;
console.log(s % w && s > w);
if (s++ % w == w - 1) {
/*
str_1 += '\n';
str_2 += '\n';
*/
}
}
str_3 += '{' + varname + ',' + var_counter + '},';
const based = `img_width=${w};img_height=${h}
base_x=${$('base_x').value};base_y=${$('base_y').value};\n`;
str_1 = based + '\ncolors={ ' + str_1 + '}';
str_2 = based + vars + '\ncolors={ ' + str_2 + '}';
str_3 = based + vars + '\ncolors={ ' + str_3 + '}';
var best_str = '';
var best_str_len = 0;
if (str_1.length > str_2.length) {
best_str = str_2;
best_str_len = str_2.length;
} else {
best_str = str_1;
best_str_len = str_1.length;
}
$('code_1').value = best_str;
$('v1_chars').innerHTML = best_str_len;
$('code_2').value = str_3;
$('v2_chars').innerHTML = str_3.length;
console.log('str1: ' + str_1.length);
console.log('str2: ' + str_2.length);
console.log('str3: ' + str_3.length);
$show($('results'));
$hide($('loading'));
}
}