File: shell.html

package info (click to toggle)
jpegqs 1.20210408-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 428 kB
  • sloc: ansic: 4,369; makefile: 176; sh: 42
file content (225 lines) | stat: -rw-r--r-- 7,006 bytes parent folder | download
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
<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>JPEG Quant Smooth (WebAssembly)</title>
	</head>
	<body>
		<div id=droparea align=center>
			<table>
			<tr align=center><td colspan=2>
				<button id=load disabled style="float: left;">Load</button>
				<progress id=progress hidden=true></progress>
				<button id=save disabled style="float: right;">Save</button>
			</td></tr>
			<tr><td>Options:&nbsp;</td>
				<td><input id=cmdline size=40 value="--optimize --info 8 --quality 3"></td></tr>
			<tr><td>Filename:&nbsp;</td>
				<td><input id=filename size=40 value="" disabled></td></tr>
			</table>
			<br>
			<div id=status>You need to enable JavaScript to run this app.</div>
			<br>
			<textarea id=output cols=80 rows=24 readonly style="resize: none;"></textarea>
		</div>

		<script type='text/javascript'>

var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var outputElement = document.getElementById('output');
var loadElement = document.getElementById('load');
var saveElement = document.getElementById('save');
outputElement.value = '';
loadElement.disabled = true;
saveElement.disabled = true;

/* var */ Module = {
	preRun: [],
	postRun: function() {
		initDrop();
		loadElement.onclick = cbLoad;
		saveElement.onclick = cbSave;
		loadElement.disabled = false;
	},
	print: function(text) {
		if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
		console.log(text);
		outputElement.value += text + "\n";
		outputElement.scrollTop = outputElement.scrollHeight; // focus on bottom
	},
	printErr: function(text) {
		if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
		// console.error(text);
		console.log(text);
		outputElement.value += "!!! " + text + "\n";
		outputElement.scrollTop = outputElement.scrollHeight; // focus on bottom
	},
	setStatus: function(text) {
		if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
		if (text === Module.setStatus.last.text) return;
		var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
		var now = Date.now();
		if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
		Module.setStatus.last.time = now;
		Module.setStatus.last.text = text;
		if (m) {
			text = m[1];
			progressElement.value = parseInt(m[2])*100;
			progressElement.max = parseInt(m[4])*100;
			progressElement.hidden = false;
		} else {
			progressElement.hidden = true;
		}
		statusElement.innerHTML = text;
	},
	totalDependencies: 0,
	monitorRunDependencies: function(left) {
		this.totalDependencies = Math.max(this.totalDependencies, left);
		Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
	}
};
Module.setStatus('Downloading...');
window.onerror = function() {
	Module.setStatus('Exception thrown, see JavaScript console');
	Module.setStatus = function(text) {
		if (text) Module.printErr('[post-exception status] ' + text);
	};
};
/*============================================================================*/
var output_data = null;
var prm_addr = null;

Module["wasm_progress"] = function(percent) {
	progressElement.hidden = false;
	progressElement.value = percent;
	progressElement.max = 100;
}

function cbProcess(input) {
	loadElement.disabled = true;
	saveElement.disabled = true;
	output_data = null;
	var cmdline = document.getElementById('cmdline').value;
	var cmd_size = cmdline.length;

	var inp_size = input.byteLength;
	prm_addr = Module._malloc(5*8 + inp_size + cmd_size + 1);
	Module.HEAPU8.set(new Uint8Array(5*8), prm_addr);
	var inp_addr = prm_addr + 5*8;
	var cmd_addr = prm_addr + 5*8 + inp_size;

	Module.HEAPU8.set(new Uint8Array(input), inp_addr);

	for (var i = 0; i < cmd_size; i++) {
		var c = cmdline.charCodeAt(i);
		if (c > 127) c = '?'.charCodeAt(0);
		Module.HEAPU8[cmd_addr + i] = c; 
	}
	Module.HEAPU8[cmd_addr + cmd_size] = 0;

	Module.print("Input size: " + inp_size);
	Module.print("Processing...");

	Module.HEAPU32[prm_addr/4] = cmd_addr;
	Module.HEAPU32[prm_addr/4 + 1*2] = inp_addr;
	Module.HEAPU32[prm_addr/4 + 2*2] = inp_size;
	Module._web_main(prm_addr);
}

Module["wasm_return"] = function() {
	var out_addr = Module.HEAPU32[prm_addr/4 + 3*2];
	var out_size = Module.HEAPU32[prm_addr/4 + 4*2];

	Module.print("Output size: " + out_size);
	Module._free(prm_addr);
	if (out_addr) {
		output_data = Module.HEAPU8.slice(out_addr, out_addr + out_size);
		Module._free(out_addr);
		saveElement.disabled = false;
	}
	loadElement.disabled = false;
	progressElement.hidden = true;
}

function initDrop() {
	var dropArea = document; // .getElementById('droparea');
	function preventDefaults(e) { e.preventDefault(); e.stopPropagation(); }
	['dragenter', 'dragover', 'dragleave', 'drop'].forEach(function(eventName) {
		dropArea.addEventListener(eventName, preventDefaults, false);
	})
	function handleDrop(e) {
		var list = e.dataTransfer.files;
		if (list.length == 1) readFile(list[0]);
		else alert("Multiple file drop unsupported.");
	}
	dropArea.addEventListener('drop', handleDrop, false);
}

function readFile(file) {
	Module.print("Loading \"" + file.name + "\"");
	document.getElementById('filename').value = file.name;
	document.getElementById('filename').disabled = false;

	var reader = new FileReader();
	reader.onload = function(e) {
		var input_data = e.target.result;
		cbProcess(input_data);
	};
	reader.readAsArrayBuffer(file);
}

function cbLoad() {
	var input = document.createElement('input');
	input.type = 'file';
	input.accept = 'image/jpeg';
	input.onchange = function(e) { 
		var file = e.target.files[0]; 
		readFile(file);
	}
	input.click();
}

function cbSave() {
	if (output_data !== null) {
		var blob = new Blob([output_data], {type: "application/unknown"});
		var link = document.createElement('a');
		link.href = (window.URL || window.webkitURL).createObjectURL(blob);
		link.download = document.getElementById('filename').value;
		link.click();
	} else {
		alert("data undefined");
	}
}

function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
	var expires = "expires=" + d.toUTCString();
	document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1);
		if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
	}
	return null;
}

{
	var options = getCookie('options');
	if (options) document.getElementById('cmdline').value = options;
	window.onbeforeunload = function() {
		var options = document.getElementById('cmdline').value;
		setCookie('options', options, 90);
	}
}
/*============================================================================*/
		</script>
		{{{ SCRIPT }}}
	</body>
</html>