-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.js
289 lines (246 loc) · 9.85 KB
/
common.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
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
var base_size = {width:150, height:150}; // Size of all Canvas
var base_img;
var base_canvas, src_canvas, mask_canvas, result_canvas;
var base_ctx, src_ctx, mask_ctx, result_ctx;
/*-----------------------------------------
Functions to set arbitrary button state
state: true->enabled, false->disabled
-----------------------------------------*/
function setButtonState(button_name, state) {
var button_elements = document.getElementsByName(button_name);
for(var i=0; i<button_elements.length; i++) {
button_elements[i].disabled = !state;
}
}
/*-----------------------------------------
Initial functions
1) all Canvas
2) only Result Canvas(for re-draw)
-----------------------------------------*/
function initializeCanvas() {
// To call completeInitializeCanvas()
var loaded_image_count = 0;
blend_position_offset.x = 0; blend_position_offset.y = 0;
var mask_pixels = mask_ctx.getImageData(0, 0, base_size.width, base_size.height);
for(var i=0; i<mask_pixels.data.length; i++) {
mask_pixels.data[i] = 255;
}
mask_ctx.putImageData(mask_pixels, 0, 0);
setButtonState("blendBtn", false);
setButtonState("adjustBtn", true);
setButtonState("directionBtn", false);
base_img = new Image();
base_img.src = image_set.base;
base_img.onload = function() { // Images must be Pre-loaded
if(++loaded_image_count === 2) completeInitializeCanvas();
};
var src_img = new Image();
src_img.src = image_set.src;
src_img.onload = function() {
if(++loaded_image_count === 2) completeInitializeCanvas();
};
function completeInitializeCanvas() {
base_canvas.width = base_size.width; base_canvas.height = base_size.height;
src_canvas.width = base_size.width; src_canvas.height = base_size.height;
mask_canvas.width = base_size.width; mask_canvas.height = base_size.height;
result_canvas.width = base_size.width; result_canvas.height = base_size.height;
base_ctx.drawImage(base_img, 0, 0, base_size.width, base_size.height);
src_ctx.drawImage(src_img, 0, 0, base_size.width, base_size.height);
mask_ctx.drawImage(src_img, 0, 0, base_size.width, base_size.height);
initializeResultCtx();
calMaskCanvasOffset();
mask_canvas.addEventListener("mousemove", drawMask, true);
mask_canvas.addEventListener("mousedown", startDrawing, true);
mask_canvas.addEventListener("mouseup", finishDrawing, true);
}
}
function initializeResultCtx() {
var result_pixels = result_ctx.getImageData(0, 0, base_size.width, base_size.height);
for(var i=0; i<result_pixels.data.length; i++) {
result_pixels.data[i] = 255;
}
result_ctx.putImageData(result_pixels, 0, 0);
result_ctx.drawImage(base_img, 0, 0, base_size.width, base_size.height);
}
/*-----------------------------------------
[Step1]
Function to control
value of Gradients radio button
-----------------------------------------*/
var is_mixing_gradients = false;
function changeGradients(gradients) {
if(gradients === "mixing") is_mixing_gradients = true;
else is_mixing_gradients = false;
}
/*-----------------------------------------
[Step2]
Functions for EventListener
Related to drawing mask area
-----------------------------------------*/
var mask_canvas_offset = {x:0, y:0};
var drawing = false, old_point = {x:0, y:0};
function calMaskCanvasOffset() {
var bounds = mask_canvas.getBoundingClientRect();
mask_canvas_offset.x = bounds.left;
mask_canvas_offset.y = bounds.top;
}
function drawMask(e) {
if(drawing) {
var x = e.clientX - mask_canvas_offset.x;
var y = e.clientY - mask_canvas_offset.y;
mask_ctx.strokeStyle = "rgba(0,255,0,1.0)";
mask_ctx.lineWidth = 20;
mask_ctx.lineJoin = "round";
mask_ctx.lineCap = "round";
mask_ctx.beginPath();
mask_ctx.moveTo(old_point.x, old_point.y);
mask_ctx.lineTo(x, y);
mask_ctx.stroke();
mask_ctx.closePath();
old_point.x = x;
old_point.y = y;
}
}
function startDrawing(e) {
drawing = true;
old_point.x = e.clientX - mask_canvas_offset.x;
old_point.y = e.clientY - mask_canvas_offset.y;
}
function finishDrawing(e) {
drawing = false;
}
/*-----------------------------------------
[Step3]
Adjust and move blend position
-----------------------------------------*/
var blend_position_offset = {x:0, y:0};
function adjustBlendPosition() {
var src_pixels = src_ctx.getImageData(0, 0, base_size.width, base_size.height);
var mask_pixels = mask_ctx.getImageData(0, 0, base_size.width, base_size.height);
var result_pixels = result_ctx.getImageData(0, 0, base_size.width, base_size.height);
for(var y=1; y<base_size.height-1; y++) {
for(var x=1; x<base_size.width-1; x++) {
var p = (y*base_size.width+x)*4;
if(mask_pixels.data[p+0]==0 && mask_pixels.data[p+1]==255 &&
mask_pixels.data[p+2]==0 && mask_pixels.data[p+3]==255) {
var p_offseted = p + 4*((blend_position_offset.y)*base_size.width+blend_position_offset.x);
for(var rgb=0; rgb<3; rgb++) {
result_pixels.data[p_offseted+rgb] = src_pixels.data[p+rgb];
}
}
}
}
result_ctx.putImageData(result_pixels, 0, 0);
mask_canvas.removeEventListener("mousemove", drawMask, true);
mask_canvas.removeEventListener("mousedown", startDrawing, true);
mask_canvas.removeEventListener("mouseup", finishDrawing, true);
setButtonState("adjustBtn", false);
setButtonState("blendBtn", true);
setButtonState("directionBtn", true);
}
function moveBlendPosition(direction) {
var mask_pixels = mask_ctx.getImageData(0, 0, base_size.width, base_size.height);
var max = {x:base_size.width-2, y:base_size.height-2}, min = {x:0, y:0};
if(direction === "up") {
blend_position_offset.y-=10;
} else if(direction === "right") {
blend_position_offset.x+=10;
} else if(direction === "down") {
blend_position_offset.y+=10;
} else if(direction === "left") {
blend_position_offset.x-=10;
}
for(var y=1; y<base_size.height-1; y++) {
for(var x=1; x<base_size.width-1; x++) {
var p = (y*base_size.width+x)*4;
if(mask_pixels.data[p+0]==0 && mask_pixels.data[p+1]==255 &&
mask_pixels.data[p+2]==0 && mask_pixels.data[p+3]==255) {
if((x+blend_position_offset.x)>max.x || (x+blend_position_offset.x)<min.x ||
(y+blend_position_offset.y)>max.y || (y+blend_position_offset.y)<min.y) {
if(direction === "up") {
blend_position_offset.y+=10;
} else if(direction === "right") {
blend_position_offset.x-=10;
} else if(direction === "down") {
blend_position_offset.y-=10;
} else if(direction === "left") {
blend_position_offset.x+=10;
}
return false;
}
}
}
}
initializeResultCtx();
adjustBlendPosition();
}
/*-----------------------------------------
Blend Images
g : src_pixels (using mask_pixels)
f*: base_pixels
---> Blend result is result_pixels
-----------------------------------------*/
function blendImages() {
initializeResultCtx();
var base_pixels = base_ctx.getImageData(0, 0, base_size.width, base_size.height);
var src_pixels = src_ctx.getImageData(0, 0, base_size.width, base_size.height);
var mask_pixels = mask_ctx.getImageData(0, 0, base_size.width, base_size.height);
var result_pixels = result_ctx.getImageData(0, 0, base_size.width, base_size.height);
var dx, absx, previous_epsilon=1.0;
var cnt=0;
do {
dx=0; absx=0;
for(var y=1; y<base_size.height-1; y++) {
for(var x=1; x<base_size.width-1; x++) {
// p is current pixel
// rgba r=p+0, g=p+1, b=p+2, a=p+3
var p = (y*base_size.width+x)*4;
// Mask area is painted rgba(0,255,0,1.0)
if(mask_pixels.data[p+0]==0 && mask_pixels.data[p+1]==255 &&
mask_pixels.data[p+2]==0 && mask_pixels.data[p+3]==255) {
var p_offseted = p + 4*(blend_position_offset.y*base_size.width+blend_position_offset.x);
// q is array of connected neighbors
var q = [((y-1)*base_size.width+x)*4, ((y+1)*base_size.width+x)*4,
(y*base_size.width+(x-1))*4, (y*base_size.width+(x+1))*4];
var num_neighbors = q.length;
for(var rgb=0; rgb<3; rgb++) {
var sum_fq = 0;
var sum_vpq = 0;
var sum_boundary = 0;
for(var i=0; i<num_neighbors; i++) {
var q_offseted = q[i] + 4*(blend_position_offset.y*base_size.width+blend_position_offset.x);
if(mask_pixels.data[q[i]+0]==0 && mask_pixels.data[q[i]+1]==255 &&
mask_pixels.data[q[i]+2]==0 && mask_pixels.data[q[i]+3]==255) {
sum_fq += result_pixels.data[q_offseted+rgb];
} else {
sum_boundary += base_pixels.data[q_offseted+rgb];
}
if(is_mixing_gradients && Math.abs(base_pixels.data[p_offseted+rgb]-base_pixels.data[q_offseted+rgb]) >
Math.abs(src_pixels.data[p+rgb]-src_pixels.data[q[i]+rgb])) {
sum_vpq += base_pixels.data[p_offseted+rgb]-base_pixels.data[q_offseted+rgb];
} else {
sum_vpq += src_pixels.data[p+rgb]-src_pixels.data[q[i]+rgb];
}
}
var new_value = (sum_fq+sum_vpq+sum_boundary)/num_neighbors;
dx += Math.abs(new_value-result_pixels.data[p_offseted+rgb]);
absx += Math.abs(new_value);
result_pixels.data[p_offseted+rgb] = new_value;
}
}
}
}
cnt++;
var epsilon = dx/absx;
if(!epsilon || previous_epsilon-epsilon === 0) break; // convergence
else previous_epsilon = epsilon;
} while(true);
result_ctx.putImageData(result_pixels, 0, 0);
mask_canvas.addEventListener("mousemove", drawMask, true);
mask_canvas.addEventListener("mousedown", startDrawing, true);
mask_canvas.addEventListener("mouseup", finishDrawing, true);
setButtonState("adjustBtn", true);
setButtonState("blendBtn", false);
setButtonState("directionBtn", false);
alert(cnt+" times iterated.");
}