-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
586 lines (487 loc) · 24.8 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<!-- MIT License
Copyright (c) 2020 Henry Kang
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>pictoon</title>
<link rel="icon" href="hk_logo.png">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
.container {
display: flex;
position: relative;
}
.frame {
margin: 20px;
padding: 20px;
box-shadow: 5px 10px 18px #888888;
}
canvas {
/* border: solid rgb(240, 240, 240); */
/* box-shadow: inset 0 0 10px #000000; */
}
.dragBox {
margin: 20px 10px;
padding: 20px 20px;
position: absolute;
bottom: 0;
text-align: center;
font-weight: bold;
line-height: 2rem;
color: #92b0b3;
border: 2px dashed #91b0b3;
transition: transform 0.3s;
}
.dragBox input[type="file"] {
position: absolute;
height: 100%;
width: 100%;
opacity: 0;
top: 0;
left: 0;
}
.dragBox:hover, .dragBox.dragover {
background: #ecf0f5;
}
.dragBox .fa.fa-upload {
display: block;
margin: 0;
padding: 0;
}
</style>
</head>
<body style="background-color:rgb(255, 255, 255)" id="body">
<div class="container m-0 p-0">
<div class="frame">
<canvas id="canvas"></canvas>
</div>
<div>
<div class="dragBox" >
<i class="fa fa-upload" aria-hidden="true"></i>
<span class="content">Choose an image file or drag it here</span>
<input type="file" id="uploadFile">
</div>
</div>
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "./build/three.module.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import * as dat from './jsm/libs/dat.gui.module.js';
import { FBO } from './jsm/postprocessing/hkFBO.js';
import { Shader } from './jsm/postprocessing/hkShader.js';
import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
import { hkGaussShader } from './jsm/shaders/hkGaussShader.js';
import { hkGrayShader } from './jsm/shaders/hkGrayShader.js';
import { hkRenderShader } from './jsm/shaders/hkRenderShader.js';
import { hkBilateralShader } from './jsm/shaders/hkBilateralShader.js';
import { hkFBLShader } from './jsm/shaders/hkFBLShader.js';
import { hkDBLShader } from './jsm/shaders/hkDBLShader.js';
import { hkBinarizeShader } from './jsm/shaders/hkBinarizeShader.js';
import { hkTangentShader } from './jsm/shaders/hkTangentShader.js';
import { hkTensorShader } from './jsm/shaders/hkTensorShader.js';
import { hkLICShader } from './jsm/shaders/hkLICShader.js';
import { hkDirdogShader } from './jsm/shaders/hkDirdogShader.js';
import { hkFdogShader } from './jsm/shaders/hkFdogShader.js';
import { hkLevelShader } from './jsm/shaders/hkLevelShader.js';
import { hkLevelColShader } from './jsm/shaders/hkLevelColShader.js';
import { hkQuantShader } from './jsm/shaders/hkQuantLabShader.js';
import { hkSaturateShader } from './jsm/shaders/hkSaturateShader.js';
import { hkOverlayShader } from './jsm/shaders/hkOverlayShader.js';
import { Vector2 } from 'three';
let strDownloadMime = "image/octet-stream";
let camera, renderer, scene;
let plane, loader;
let ori, bilat, gray, gray2, out, tangent, tensor, fdog;
let w, h; // canvas resolution
// let pRatio = window.devicePixelRatio;
let pRatio = 2.5;
let bilateralShader, fblShader, dblShader, grayShader, gaussShader, binarizeShader;
let quantShader, tensorShader, tangentShader, licShader, dirdogShader, fdogShader, renderShader;
let saturateShader, levelShader, levelcolShader, overlayShader;
let config = {
IMAGE: 1, // 0: ori, 1: toon
TENSOR: 10, // [1, 10] structure tensor smoothing kernel size
FDOG: {
LIC: 10, // pre-smoothing sigma
DIRDOG: 3.5, // directional dog sigma
FLOWDOG: 5.0, // flowdog sigma
THRES: 0.6, // binarization threshold
},
LEVEL: {
LOWER: 0.01,
UPPER: 0.99,
},
BILATERAL: {
SPACE: 3.0,
RANGE: 0.3,
},
};
function start_gui () {
gui = new dat.GUI({
width: 300
});
gui.add(config, 'IMAGE', {
'source': 0,
'toon': 1,
}).name('image').onFinishChange(updateImage);
gui.add(config, 'TENSOR', 1, 20).name('tensor').onFinishChange(render);
let fdog = gui.addFolder('FDoG');
fdog.open();
fdog.add(config.FDOG, 'LIC', 3, 20).name('lic').onFinishChange(render);
fdog.add(config.FDOG, 'DIRDOG', 1, 10).name('dirdog').onFinishChange(render);
fdog.add(config.FDOG, 'FLOWDOG', 3, 20).name('flowdog').onFinishChange(render);
fdog.add(config.FDOG, 'THRES', 0.5, 0.9999).name('thres').onFinishChange(render);
let level = gui.addFolder('Level');
level.open();
level.add(config.LEVEL, 'LOWER', 0.01, 0.5).name('lower').onFinishChange(render);
level.add(config.LEVEL, 'UPPER', 0.5, 0.99).name('upper').onFinishChange(render);
gui.add({ fun: resetParameters }, 'fun').name('Reset Parameters');
gui.add({ fun: saveAsImage }, 'fun').name('Take Screenshot');
gui.add({ fun : () => {
window.open('https://twitter.com/henrykangio');
} }, 'fun').name('<i class="fa-brands fa-twitter"></i>  Twitter');
gui.add({ fun : () => {
window.open('https://www.instagram.com/henrykangio');
} }, 'fun').name('<i class="fa-brands fa-instagram"></i>  Instagram');
}
let gui;
start_gui();
init();
const uploadElement = document.getElementById("uploadFile");
uploadElement.addEventListener("change", dragNdrop, false);
uploadElement.addEventListener("dragover", drag, false);
uploadElement.addEventListener("drop", drop, false);
function dragNdrop() {
var fileName = URL.createObjectURL(event.target.files[0]);
console.log("fileName = " + fileName);
let texture = textureLoad(loader, fileName);
plane.material = new THREE.MeshBasicMaterial( { map: texture } );
}
function drag() {
document.getElementById('uploadFile').parentNode.className = 'dragover dragBox';
}
function drop() {
document.getElementById('uploadFile').parentNode.className = 'dragBox';
}
function init() {
scene = new THREE.Scene();
const geometry = new THREE.PlaneGeometry( 1, 1 );
const material = new THREE.MeshBasicMaterial();
plane = new THREE.Mesh(geometry, material);
scene.add(plane);
w = window.innerWidth;
h = window.innerHeight;
camera = new THREE.OrthographicCamera( w / - 2, w / 2, h / 2, h / - 2, 1, 1000 );
camera.position.z = 2;
camera.lookAt( scene.position );
scene.add( camera );
renderer = new THREE.WebGLRenderer({
canvas: document.getElementById("canvas"), // canvas already created
antialias: true,
preserveDrawingBuffer: true
});
renderer.setPixelRatio( pRatio ); // 2.5
// renderer.setPixelRatio( 1.0 );
renderer.setSize( w, h ); // set renderer resolution
// document.body.appendChild( renderer.domElement ); // renderer.domElement = <canvas>
bilateralShader = new Shader( hkBilateralShader );
fblShader = new Shader( hkFBLShader );
dblShader = new Shader( hkDBLShader );
grayShader = new Shader( hkGrayShader );
levelShader = new Shader( hkLevelShader );
levelcolShader = new Shader( hkLevelColShader );
gaussShader = new Shader( hkGaussShader );
binarizeShader = new Shader( hkBinarizeShader );
tensorShader = new Shader( hkTensorShader );
tangentShader = new Shader( hkTangentShader );
licShader = new Shader( hkLICShader );
dirdogShader = new Shader( hkDirdogShader );
fdogShader = new Shader( hkFdogShader );
quantShader = new Shader( hkQuantShader );
saturateShader = new Shader( hkSaturateShader );
overlayShader = new Shader( hkOverlayShader );
renderShader = new Shader( hkRenderShader );
window.addEventListener( 'resize', onWindowResize );
loader = new THREE.TextureLoader();
let fileName = 'eagle_500.png';
let texture = textureLoad(loader, fileName);
plane.material = new THREE.MeshBasicMaterial( { map: texture } );
}
function textureLoad(loader, fileName) {
let texture = loader.load(
fileName,
function ( tex ) {
w = tex.image.width; // image width
h = tex.image.height; // image height
// const _cap = 960; // internal size
const _cap = 512; // internal size
// const cap = 640; // display size
const cap = 512; // display size
// const cap = 1024; // visual optimization needed for device
const capRatio = _cap / cap;
// console.log("cap = " + cap);
let ar = w / h; // aspect ratio of image
if (ar >= 1) { // landscape image
w = _cap;
h = Math.round(w / ar);
}
else { // portrait image
h = _cap;
w = Math.round(h * ar);
}
// match orthographic camera's screen window to image resolution
camera.left = w / -2;
camera.right = w / 2;
camera.top = h / 2;
camera.bottom = h / -2;
camera.updateProjectionMatrix();
plane.scale.x = w;
plane.scale.y = h;
renderer.setSize( w, h ); // update renderer resolution
let canvas = renderer.domElement;
let canvas_w = Math.round(w / capRatio); // restrict the canvas size
let canvas_h = Math.round(h / capRatio); // restrict the canvas size
canvas.style.width = canvas_w + "px";
canvas.style.height = canvas_h + "px";
if (!ori) ori = new FBO(renderer); else ori.reset();
if (!bilat) bilat = new FBO(renderer); else bilat.reset();
if (!gray) gray = new FBO(renderer); else gray.reset();
if (!gray2) gray2 = new FBO(renderer); else gray2.reset();
if (!out) out = new FBO(renderer); else out.reset();
if (!tangent) tangent = new FBO(renderer); else tangent.reset();
if (!tensor) tensor = new FBO(renderer); else tensor.reset();
if (!fdog) fdog = new FBO(renderer); else fdog.reset();
// initial rendering of input image
renderer.setRenderTarget( ori.writeBuffer );
renderer.render( scene, camera );
ori.swapBuffers();
render();
},
);
return texture;
}
function onWindowResize() {
render();
}
function render() {
hkBilateral(ori, bilat, 3, 0.3);
hkGray(bilat, gray);
hkTensor(bilat, tensor);
hkGauss(tensor, tensor, config.TENSOR);
hkTangent(tensor, tangent);
hkLIC(gray, tangent, fdog, config.FDOG.LIC);
hkDirdog(fdog, tangent, fdog, config.FDOG.DIRDOG);
hkFdog(fdog, tangent, fdog, config.FDOG.FLOWDOG);
hkBinarize(fdog, fdog, config.FDOG.THRES);
hkLevelCol(bilat, bilat, config.LEVEL.LOWER, config.LEVEL.UPPER);
hkDBL(bilat, tangent, bilat, 3.5, 0.3);
hkFBL(bilat, tangent, bilat, 10.0, 0.3);
hkSaturate(bilat, bilat, 1.3);
hkQuant(bilat, bilat, 2);
hkOverlay(bilat, fdog, bilat);
updateImage();
}
function updateImage() {
if (config.IMAGE == "0") hkRender(ori, null);
if (config.IMAGE == "1") hkRender(bilat, null);
}
function resetParameters() {
config.IMAGE = "1"; // set to bilat
gui.__controllers[0].updateDisplay(); // image
config.TENSOR = "10";
gui.__controllers[1].updateDisplay(); // tensor scale
config.FDOG.LIC = "10";
config.FDOG.DIRDOG = "3.5";
config.FDOG.FLOWDOG = "5.0";
config.FDOG.THRES = "0.6";
gui.__folders.FDoG.__controllers[0].updateDisplay(); // lic
gui.__folders.FDoG.__controllers[1].updateDisplay(); // dirdog
gui.__folders.FDoG.__controllers[2].updateDisplay(); // flowdog
gui.__folders.FDoG.__controllers[3].updateDisplay(); // thres
config.LEVEL.LOWER = "0.01";
config.LEVEL.UPPER = "0.99";
gui.__folders.Level.__controllers[0].updateDisplay(); // lower
gui.__folders.Level.__controllers[1].updateDisplay(); // upper
render();
}
function hkBilateral(src, dst, half, range) {
let shader = bilateralShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'resolution' ].value = new Vector2(w * pRatio, h * pRatio);
shader.uniforms[ 'u_half' ].value = half;
shader.uniforms[ 'u_range' ].value = range;
shader.render( renderer, dst );
}
function hkDBL(src, tangent, dst, half, range) {
let shader = dblShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_tangent' ].value = tangent.readBuffer.texture;
shader.uniforms[ 'resolution' ].value = new Vector2(w * pRatio, h * pRatio);
shader.uniforms[ 'u_half' ].value = half;
shader.uniforms[ 'u_range' ].value = range;
shader.render( renderer, dst );
}
function hkFBL(src, tangent, dst, half, range) {
let shader = fblShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_tangent' ].value = tangent.readBuffer.texture;
shader.uniforms[ 'resolution' ].value = new Vector2(w * pRatio, h * pRatio);
shader.uniforms[ 'u_half' ].value = half;
shader.uniforms[ 'u_range' ].value = range;
shader.render( renderer, dst );
}
function hkQuant(src, dst, bits) {
let shader = quantShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_bits' ].value = bits;
shader.render( renderer, dst );
}
function hkSaturate(src, dst, sat) {
let shader = saturateShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_sat' ].value = sat;
shader.render( renderer, dst );
}
function hkGray(src, dst) {
let shader = grayShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.render( renderer, dst );
}
function hkLevel(src, dst, x1, y1, x2, y2) {
let shader = levelShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_pivot1' ].value = new Vector2(x1, y1);
shader.uniforms[ 'u_pivot2' ].value = new Vector2(x2, y2);
shader.render( renderer, dst );
}
function hkLevelCol(src, dst, lower, upper) {
let shader = levelcolShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_lower' ].value = lower;
shader.uniforms[ 'u_upper' ].value = upper;
shader.render( renderer, dst );
}
function hkGauss(src, dst, half) {
let shader = gaussShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'resolution' ].value = new Vector2(w * pRatio, h * pRatio);
shader.uniforms[ 'u_half' ].value = half;
shader.render( renderer, dst );
}
function hkBinarize(src, dst, thres) {
let shader = binarizeShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_thres' ].value = thres;
shader.render( renderer, dst );
}
function hkTensor(src, dst) {
let shader = tensorShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'resolution' ].value = new Vector2(w * pRatio, h * pRatio);
shader.render( renderer, dst );
}
function hkTangent(src, dst) {
let shader = tangentShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.render( renderer, dst );
}
function hkLIC(src, tangent, dst, half) {
let shader = licShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_tangent' ].value = tangent.readBuffer.texture;
shader.uniforms[ 'resolution' ].value = new Vector2(w * pRatio, h * pRatio);
shader.uniforms[ 'u_half' ].value = half;
shader.render( renderer, dst );
}
function hkDirdog(src, tangent, dst, half) {
let shader = dirdogShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_tangent' ].value = tangent.readBuffer.texture;
shader.uniforms[ 'resolution' ].value = new Vector2(w * pRatio, h * pRatio);
shader.uniforms[ 'u_half' ].value = half;
shader.render( renderer, dst );
}
function hkFdog(src, tangent, dst, half) {
let shader = fdogShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_tangent' ].value = tangent.readBuffer.texture;
shader.uniforms[ 'resolution' ].value = new Vector2(w * pRatio, h * pRatio);
shader.uniforms[ 'u_half' ].value = half;
shader.render( renderer, dst );
}
function hkRender(src, dst) {
let shader = renderShader;
shader.uniforms[ 'u_image' ].value = src.readBuffer.texture;
if (dst) { // renderTarget = fbo
shader.render( renderer, dst );
}
else { // renderTarget = screen
shader.render( renderer, null );
}
}
function hkOverlay(src, overlay, dst) {
let shader = overlayShader;
shader.uniforms[ 'u_src' ].value = src.readBuffer.texture;
shader.uniforms[ 'u_overlay' ].value = overlay.readBuffer.texture;
// plane.material = shader.material;
let material = plane.material;
plane.material = shader.material;
material.dispose();
renderer.setRenderTarget( dst.writeBuffer );
renderer.render( scene, camera );
dst.swapBuffers();
}
function saveAsImage() {
var imgData, imgNode;
try {
var strMime = "image/png";
imgData = renderer.domElement.toDataURL(strMime); // renderer.domElement = canvas
saveFile(imgData.replace(strMime, strDownloadMime), "out.png");
} catch (e) {
console.log(e);
return;
}
}
var saveFile = function (strData, filename) {
var link = document.createElement('a');
if (typeof link.download === 'string') {
document.body.appendChild(link); // Firefox requires the link to be in the body
link.download = filename;
link.href = strData;
link.click();
document.body.removeChild(link); // remove the link when done
} else {
location.replace(uri);
}
}
</script>
</body>
</html>