-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
123 lines (119 loc) · 5.14 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>TPT CGI Generator</title>
</head>
<body>
<!--608 x 380-->
Instructions: Download the script and run it in the lua console with <code>dofile("<path to script here>")</code><br>
<input type="number" step="0.05" onchange="draw(document.getElementById('filein'),this.value)" id="scale" />
<input type="file" onchange="draw(this,1);document.getElementById('scale').value = 1;" id="filein" />
<button onclick="generate()">Generate script [BAN WARNING]</button>
<br>
<a id="result" href="javascript:alert('No script generated')" download="image.lua">Save script</a>
<br>
<div id="progress"></div>
<canvas width="608" height="380" id="canvas" style="border:1px solid black"></canvas>
<script id="worker1">
function RGB2TPTINT(r, g, b, a) {
let color = a
color = color * 256 + r
color = color * 256 + g
color = color * 256 + b
return color;
}
self.onmessage = (d) => {
if (!(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)){
throw "error: not running as worker"
}
let data = d.data
let pixel = data[0]
let w = data[1]
let h = data[2]
let script = `local data="`
let currentvalue = 0;
let NextRGBValue = () => pixel[currentvalue++];
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
let red = NextRGBValue()
let green = NextRGBValue()
let blue = NextRGBValue()
let alpha = NextRGBValue()
red = Math.floor((alpha / 255) * red)
green = Math.floor((alpha / 255) * green)
blue = Math.floor((alpha / 255) * blue)
let color = RGB2TPTINT(red, green, blue, 255)
script+=`${color},`
}
self.postMessage(`PROGRESSProgress: ${y+1}/${h} Pixel: ${y*w+w}/${h*w}`)
}
script+=`"`
script+=[
``,
`local w = ${w}`,
`local h = ${h}`,
`local pos = 1`,
`function NextValue()`,
` local value = 0`,
` local char = '0'`,
` while char~=',' do`,
` value = value*10 + tonumber(char)`,
` char = data:sub(pos,pos)`,
` pos = pos + 1`,
` end`,
` return value`,
`end`,
`for y=4,h+3 do`,
` for x=4,w+3 do`,
` local color = NextValue()`,
` if color==0xff000000 then`,
` goto continue`,
` end`,
` tpt.set_property("dcolor", color, tpt.create(x,y,"DMND"))`,
` ::continue::`,
` end`,
`end`
].join("\n")
self.postMessage(new Blob([script]))
}
</script>
<script>
var w = 0
var h = 0
var canvas = document.getElementById("canvas").getContext("2d")
var lastObjectURL = ""
function draw(elem, s) {
let image = new Image
image.onload = () => {
canvas.clearRect(0, 0, 608, 380)
w = Math.min(Math.ceil(image.width * s),608)
h = Math.min(Math.ceil(image.height * s),380)
canvas.drawImage(image, 0, 0, image.width * s, image.height * s)
URL.revokeObjectURL(image.src)
}
image.src = URL.createObjectURL(elem.files[0])
}
var blob = new Blob([document.querySelector('#worker1').textContent], {
type: "text/javascript"
})
var worker = new Worker(window.URL.createObjectURL(blob))
function generate() {
let res = document.getElementById("result")
let pixel = canvas.getImageData(0, 0, w, h, {colorSpace: "srgb"}).data
worker.postMessage([pixel,w,h])
worker.onmessage = (d)=>{
let data = d.data
if (typeof data == "string"){
document.getElementById("progress").innerText = data.slice(8)
} else{
// handle BLOB
if (lastObjectURL!="")
URL.revokeObjectURL(lastObjectURL)
lastObjectURL = URL.createObjectURL(data)
res.href = lastObjectURL
}
}
}
</script>
</body>
</html>