-
Notifications
You must be signed in to change notification settings - Fork 0
/
msxArrayTools.html
370 lines (311 loc) · 10 KB
/
msxArrayTools.html
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<head>
<script>
var colorDistanceMethod=0;//0,1,2,
msxColors=[
[0,0,0],[1,1,1],
[62,184,73],[116,208,125],//green
[89,85,224],[128,118,241],//blue
[185,94,81],//red
[101,219,239],//blue
[219,101,89],[255,137,125],//red
[204,195,94],[222,208,135],//yellow
[58,162,65],//green
[183,102,181],//mag
[204,204,204],//gray
[255,255,255],//white
];
function findSkips(a,v){//array,value
var c=[0];
var l=a.length*a[0].length;
var i=-1;
var j=0;
var s=false;var p;
if (l>32725){
l=32725;
errors.overflow=true;
}//avoid overflow
while(++i<l){
p=a[Math.floor(i/a[0].length)][i%a[0].length]
if(s == (p==v))
c[j]++;
else
{s=!s;c[++j]=0;i--;}
}
if (c.length>1 && c.length%2==1)c.pop();
return c;
}
function buildSkips(c,v,length){
var s=false;
var j=0;
var a=[];
var i;
i=-1;while(c.length>0){
i++;
if (a[Math.floor(i/length)]===undefined)a[Math.floor(i/length)]=[];
if(c[0]>0)
{c[0]--;a[Math.floor(i/length)][i%length]=s?v:0;}
else
{c.shift();s=!s;i--;}
}
return a;
}
function arraysToMSX(a,vals,w=128,h=128,max=0){
var result="";
code.config="5000 'size:"+(w*h)+"*"+(vals.length-1)+" Max is 32725*"+"\r"+
"10 h="+w+":w="+h+":q="+(vals.length-1)+":m="+max+"\r";
var i=-1;var n=101;while(++i<a.length){
if(a[i].length<2)continue;
result+=(i+n)+" DATA "+a[i].length+","+vals[i]+"\r"
var A=a[i];
while(A.length>0){
n++;
result+=(i+n)+" DATA "+A.splice(0,36).toString()+"\r";
}
n++;
//a[i].toString()+"<br>";
}
code.data=result;
return result;
}
function download(){
var name_="";
var file = new File([code.config+code.recolor+code.decoder+code.data], "x001msx", {
type: "text/plain",
});
var a=document.getElementById("download");
a.href= URL.createObjectURL(file);
a.download = getFileName(true);
if(document.getElementById("autodownload").checked)
a.click();
}
function loadImage(){
code.recolor="";
var f=URL.createObjectURL(document.getElementById("file").files[0]);
var img=new Image();
img.onload=function(){
render(img);
};
img.src=f;
}
var _img;
var errors={}
function render(img,W,H){
errors={};
W=W||(Number(document.getElementById("W").value)/100);
H=H||(Number(document.getElementById("H").value)/100);
img=_img=img||_img;
var w=Math.ceil(W*img.width),h=Math.ceil(H*img.height);
document.getElementById("info").innerHTML=" "+w+"X"+h;
c.width=w;
c.height=h;
cx.imageSmoothingEnabled = false;
cx.drawImage(img,0,0,w,h);
autoColor(cx.getImageData(0,0,w,h).data);
if(document.getElementById("cr").checked){
code.recolor="31 ";
var i,ii;
//copy colors
for(i=0;i<16;i++){
code.recolor+="color=("+i;
for ( ii=0;ii<3;ii++){
msxColors[i][ii]=xhex(i,ii);
code.recolor+=","+Math.round(msxColors[i][ii]/255*7)+(ii<2?"":"):"+(i==8?"\r32 ":"") );
}
}
code.recolor+="\r";
}
var a=readCanvas(0,0,w,h);
var j=dropMax(compileArray(a));
max=j[1];a=j[0];
arraysToMSX(a[0],a[1],w,h,max);
document.getElementById("error").innerHTML="<b><i>";//no errors yet
for (var i in errors){
document.getElementById("error").innerHTML+=i+"<br>";
}
download();
}
function dropMax(a){
var A=a[0];var max=[0,0];
for (var i=0;i<A.length;i++)
if (A[i].length>max[1]){max=[i,A[i].length]};
a[0][max[0]]=[0];//.splice(max[0],1);
//a[1].splice(max[0],1);
return [a,a[1][max[0]]];
}
function readCanvas(x,y,w,h){
var i,j;
var a=[];for (i=0;i<w;i++)a[i]=[];
var data=cx.getImageData(x,y,w,h).data;
//find colors
var c={};
for (i=0;i<data.length;i+=4){
j=data[0]+","+data[1]+","+data[2];
c[j]=[data[0],data[1],data[2]];
}
var I;
for (i=0;i<data.length;i+=4){
I=i/4;
j=matchingColor([data[i],data[i+1],data[i+2]]);
a[I%w][Math.floor(I/w)]=j;
}
return a;
}
//find msxColor mathcing provided color
function matchingColor(c){
var dp=999999;var r=0;
var d;
for (var i=0;i<msxColors.length;i++){
var C=msxColors[i];
d=colorDistance(c,C,colorDistanceMethod)//Math.pow(c[0]-C[0],2)+Math.pow(c[1]-C[1],2)+Math.pow(c[2]-C[2],2);
if(d<=dp){dp=d;r=i;}
}
return r;
}
function colorDistance(c1,c2,method=0){
var c=c1;var C=c2;
switch (method){
case 1:
return Math.abs(c[0]-C[0])+Math.abs(c[1]-C[1])+Math.abs(c[2]-C[2]);
break;
case 2:
return Math.sqrt(Math.abs(c[0]-C[0]))+
Math.sqrt(Math.abs(c[1]-C[1]))+
Math.sqrt(Math.abs(c[2]-C[2]));
break;
case 3:
//return d;
break;
default:
return Math.pow(c[0]-C[0],2)+Math.pow(c[1]-C[1],2)+Math.pow(c[2]-C[2],2);
}
}
function compileArray(a){
var r=[];var vals=[];
for (var i=msxColors.length-1;i>=0;i--){
var s=findSkips(a,i);
if(s.length>1){
r.push(s);
vals.push(i);
}
}
return [r,vals];
};
var code={
recolor:"",
config:"\r\
10 w=4:h=4:q=2\r\
11 data 5,1 ,4,3,5,2,2\r\
12 data 4,5 ,1,3,4,1\r\
"
,
decoder:"\r\
15 x=0:y=0:sc=1\r\
20 l=w*h-1'defint a-z\r\
30 screen 5'\r\
40 line (x,y)-(x+(h-1)*sc,y+(w-1)*sc),m,BF'\r\
50 i=0:j=-1:read s,v'\r\
60 for j=0 to s-1:read c:k=(j and 1)*v''\r\
80 if c=0 then goto 90'\r\
81 if k=0 then i=i+c:goto 90'\r\
83 w2=i mod w:w1=i/w'\r\
84 if (c+w2>w) then goto 86'\r\
85 line(x+sc*w1,y+sc*w2)-(x+sc*w1+sc-1,y+sc*(w2+c-1)),v,bf:i=i+c:goto 90\r\
86 line(x+sc*w1,y+sc*w2)-(x+sc*w1+sc-1,y+sc*(w-1)),v,bf:c=c-w+w2:i=i+w-w2:if c>0 then goto 83\r\
90 next: q=q-1:if q>0 then goto 50'\r\
100 goto 100\r"
,
data:""
}
//hex to integer from color
function xhex(c_num,index){
var aa=document.getElementById("c"+c_num);
return parseInt(aa.value.substr(1+index*2,2),16);
}
function autoColor(data){
if(!document.getElementById("ca").checked)
return;
var all=[];
var c,d;
var ii=15;
//console.log(">>"+data.length);
for (var i=0;i<data.length;i+=4){
c=((
Math.round(data[i]/7)*7
)*256
+
Math.round(data[i+1]/7)*7
)*256
+
Math.round(data[i+2]/7)*7
;
c="#"+c.toString(16)
if(!all.includes(c)){
all.push(c);
document.getElementById("c"+ii).value=c;
console.log(c,data[i],data[i+1],data[i+2]);
if(--ii<0)
break;
}
}
}
var fCount=-1;
function getFileName(countup=false){
if(fCount<0)
fCount=Number(document.getElementById("fnum").value);
var res=""+fCount;
while(res.length<document.getElementById("fnum").value.length)
res="0"+res;
res=document.getElementById("fname").value+res;
if(countup)
fCount++;
var ex=document.getElementById("fext").value;
if(ex!="")res+="."+ex;
return res;
}
</script>
</head>
<body>
<input type="file" id="file" onchange="loadImage()" />
<canvas id="canvas" style="image-rendering: pixelated;"></canvas>
<br><a href="#" id="download">DOWNLOAD</a><div id="info"></div><div id="error"></div>
<br>
<input id="cr" type="checkbox" checked/>Recolor
<input id="ca" type="checkbox"/>AutoColor
<input id="autodownload" type="checkbox"/>Auto Download
<br>
<input id="c0" type="color"/><button onclick="render();">Refresh</button><br>
<input id="c1" type="color"/>
<input id="c2" type="color"/>
<input id="c3" type="color"/>
<input id="c4" type="color"/>
<input id="c5" type="color"/><br>
<input id="c6" type="color"/>
<input id="c7" type="color"/>
<input id="c8" type="color"/>
<input id="c9" type="color"/>
<input id="c10" type="color"/><br>
<input id="c11" type="color"/>
<input id="c12" type="color"/>
<input id="c13" type="color"/>
<input id="c14" type="color"/>
<input id="c15" type="color"/><br>
Width %<input id="W" type="number" value="100"/>
Height%<input id="H" type="number" value="100"/><br>
File Name:<input id="fname" value="X" size="8" />
File Number:<input id="fnum" onchange="fCount=-1;" value="00" size="3" />
File Ext:<input id="fext" value="" size="3" />
</body>
<script>
var c=document.getElementById("canvas");
var cx=c.getContext('2d');
function init0(){
for (var i=0;i<=15;i++){
var aa=document.getElementById("c"+i);
aa.value="#"+
msxColors[i][0].toString(16)+
msxColors[i][1].toString(16)+
msxColors[i][2].toString(16)
}
}
init0();
</script>