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
|
<!doctype html>
<html>
<head>
<title>Gif Dither</title>
</head>
<body>
<div>
<!-- <button id="random">random</button> -->
<button id="pattern2Lite">pattern2lite</button>
<button id="pattern3Lite">pattern3lite</button>
<button id="pattern4Lite">pattern4lite</button>
<button id="floydSteinberg">floyd-steinberg</button>
<button id="pattern4">pattern4</button>
<button id="pattern3">pattern3</button>
<!-- <button id="right">right</button> -->
<input type="text" id="url" value="1376516658960-dumpfm-DoritoWitch-TimeFLyTrans0001.png">
<button id="encode">ENCODE</button>
<button id="save">SAVE</button>
<span id="loading"></span>
</div>
<div id="images"></div>
</body>
<script type="text/javascript" src="js/vendor/gif-encode/util.js"></script>
<script type="text/javascript" src="js/vendor/gif-encode/tube.js"></script>
<script type="text/javascript" src="js/vendor/gif-encode/client.js"></script>
<script type="text/javascript" src="js/vendor/gif.js"></script>
<script type="text/javascript" src="js/vendor/FileSaver.js"></script>
<script type="text/javascript" src="js/vendor/dataUriToBlob.js"></script>
<script type="text/javascript" src="js/vendor/canvasquery.js"></script>
<script type="text/javascript" src="js/canvasquery.dither.js"></script>
<script type="text/javascript">
document.getElementById("url").onchange = load
setTimeout(init)
function init () {
bind()
// load()
}
function status (s){
var el = document.getElementById("loading")
el.innerHTML = s + "..."
}
function load(imageURL) {
document.getElementById("save").style.display = "none"
document.getElementById("encode").style.display = "none"
status("loading")
var imageURL = proxify( document.getElementById("url").value );
window.gif = window.img = null
if (imageURL.substr(-3) === "gif") {
window.gif = GIF(imageURL);
// gif.on("error", tryToLoadNextImage);
// gif.on("rendered", trackLoadTime);
gif.on("rendered", ready);
return gif.render();
} else {
window.img = new Image();
// img.addEventListener("error", tryToLoadNextImage);
img.addEventListener("load", ready);
img.crossOrigin = "anonymous";
return img.src = imageURL;
}
}
function giveImage() {
if (imageURL.substr(-3) === "gif") {
return gif.frames[gif.currentFrame()].ctx.canvas;
} else {
return img;
}
}
function proxify (url) {
if (url.indexOf("http") == 0)
return "/cgi-bin/proxy?" + url // .replace(/^https?:\/\//, "");
else
return url
};
function bind(){
var buttons = document.getElementsByTagName("button")
for (var i = 0; i < buttons.length; i++) {
(function(n){
buttons[n].onclick = function(){
algo = buttons[n].id;
build()
}
})(i)
}
document.getElementById("save").onclick = save
document.getElementById("encode").onclick = encode
}
var encoder = new GifEncoder()
encoder.on("quantized", function(url){
status("encoding")
encoder.encode()
})
encoder.on("rendered-url", function(url){
status("")
var image = new Image ()
image.src = url
document.body.appendChild(image)
lastGif = url
document.getElementById("save").style.display = "inline"
})
/*
var lastGif;
encoder.on("rendered", function(bits){
status("")
lastGif = bits
document.getElementById("save").style.display = "inline"
})
*/
function save (){
if (!lastGif) return;
var filename = document.getElementById("url").value.replace(/^.*\//,"").replace(/\.gif.*$/,"")
var blob = dataUriToBlob(lastGif); // new Blob ([lastGif], {type: "image/gif"});
saveAs(blob, filename + "-" + algo + "-" + (+new Date() % 1000) + ".gif");
}
function ready(){
if (window.gif) gif.off("rendered", ready)
status("")
document.getElementById("save").style.display = "none"
document.getElementById("encode").style.display = "inline"
build()
}
var algo = "random"
var frames = []
function build(){
status("dithering")
encoder.resetFrames()
document.getElementById("images").innerHTML = ""
if (window.gif) {
var buttons = document.getElementsByTagName("button")
var bz = []
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].id != "save" && buttons[i].id != "encode") bz.push(buttons[i].id)
}
zz = bz
gif.frames.forEach(function(frame, i){
var algo = bz[i % bz.length]
var cc = dither(frame)
encoder.addFrame(cc.canvas)
})
}
else {
var frame = { ctx: { canvas: window.img } };
var buttons = document.getElementsByTagName("button")
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].id != "save" && buttons[i].id != "encode") {
algo = buttons[i].id;
var cc = dither(frame)
encoder.addFrame(cc.canvas)
}
}
}
encode()
}
function encode(){
if (! encoder.quantized) {
status("quantizing")
encoder.quantize()
}
else {
status("encoding")
encoder.encode()
}
}
function dither(frame){
var canvas = frame.ctx.canvas
var w = canvas.width, h = canvas.height;
var cc = cq(w, h)
cc.drawImage(canvas, 0, 0, w, h);
cc[algo + "Dither"]( )
return cc
// cc.appendTo("#images")
}
</script>
</html>
|