-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcolorreduction.js
65 lines (60 loc) · 2.22 KB
/
colorreduction.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
"use strict";
/*
* 2017/03/17- (c) [email protected]
*/
document.addEventListener("DOMContentLoaded", function(event) {
main();
});
function main() {
// console.debug("main");
var srcCanvas = document.getElementById("srcCanvas");
var dstCanvas = document.getElementById("dstCanvas");
var srcImage = new Image(srcCanvas.width, srcCanvas.height);
dropFunction(document, function(dataURL) {
srcImage = new Image();
srcImage.onload = function() {
drawSrcImageAndColorReduction(srcImage, srcCanvas);
}
srcImage.src = dataURL;
}, "DataURL");
bindFunction({"maxWidthHeightRange":"maxWidthHeightText",
"quantizeMethod":null},
function() {
drawSrcImageAndColorReduction(srcImage, srcCanvas);
} );
}
var worker = null;
function drawSrcImageAndColorReduction(srcImage, srcCanvas) {
var srcCtx = srcCanvas.getContext("2d");
var dstCtx = dstCanvas.getContext("2d");
var quantizeMethod = document.getElementById("quantizeMethod").value;
var maxWidthHeight = parseFloat(document.getElementById("maxWidthHeightRange").value);
document.getElementById("nColorSrc").value = "";
document.getElementById("nColorDst").value = "";
var srcImageData = srcCanvas.getContext("2d").getImageData(0, 0, srcCanvas.width, srcCanvas.height);
drawSrcImage(srcImage, srcCanvas, maxWidthHeight);
var srcImageData = srcCtx.getImageData(0, 0, srcCanvas.width,
srcCanvas.height);
document.getElementById("nColorSrc").value = getColorNum(srcImageData);
if (worker) {
worker.terminate();
}
var div = loadingStart();
worker = new Worker("worker/colorreduction.js");
worker.onmessage = function(e) {
var [dstImageData, palette] = [e.data.image, e.data.palette];
var dstWidth = dstImageData.width;
var dstHeight = dstImageData.height;
dstCanvas.width = dstWidth;
dstCanvas.height = dstHeight;
dstCtx.putImageData(dstImageData, 0, 0, 0, 0, dstWidth, dstHeight);
//
var paletteCanvas = document.getElementById("paletteCanvas");
drawPalette(paletteCanvas, palette);
document.getElementById("nColorDst").value = getColorNum(dstImageData);
loadingEnd(div);
worker = null;
}
worker.postMessage({image:srcImageData, method:quantizeMethod},
[srcImageData.data.buffer]);
}