-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwavelet.js
247 lines (207 loc) · 7.15 KB
/
wavelet.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
var wavelet = [
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ]
];
// 8x8 grid of pixels
var pixels = [
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ]
];
// 8x8 grid of divs, corresponds to the pixels above
var pixelDivs = [];
var waveletDivs = [];
// evt -> mouse event that was triggered. Need it for y position inside pixel / wavelet block
// min -> min value that can appear in block (eg. pixel should start at 0, wavelet at -128)
// max -> max value that can appear in block (eg. pixel shouldn't be more than 255)
// matrix -> 8x8 matrix that is affected, either pixel or wavelet
// transformFunc -> transformation function, either wavelet to pixels or vice versa
function triggerMouseEvent(evt, min, max, matrix, transformFunc) {
var rect = evt.target.getBoundingClientRect();
// relative y-position to element
// 0 -> very top
// 1 -> very bottom
var y = (evt.clientY - rect.top) / rect.height;
var range = max - min;
// y- and x-position of pixel can be determined through the class name
// eg. if class name is "pixel32"
// charAt(5) == 3
// charAt(6) == 2
var wY = evt.target.classList[0].charAt(5) - '0';
var wX = evt.target.classList[0].charAt(6) - '0';
matrix[wY][wX] = (1 - y) * range + min;
if(matrix[wY][wX] > max)
matrix[wY][wX] = max;
else if(matrix[wY][wX] < min)
matrix[wY][wX] = min;
transformFunc();
updateBlocks();
}
// wavelet -> pixels transformation
function waveletToPixels()
{
var tmp = [
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ]
];
for(var y = 0; y < 8; y++) {
for(var x = 0; x < 8; x++) {
pixels[y][x] = wavelet[y][x];
}
}
function iwave(size) {
var hsize = size/2;
// vertical
for(var y = 0; y < hsize; y++) {
for(var x = 0; x < size; x++) {
tmp[y*2][x] = pixels[y][x] + pixels[y+hsize][x];
tmp[y*2+1][x] = pixels[y][x] - pixels[y+hsize][x];
}
}
// horizontal
for(var y = 0; y < size; y++) {
for(var x = 0; x < hsize; x++) {
pixels[y][x*2] = tmp[y][x] + tmp[y][x+hsize];
pixels[y][x*2+1] = tmp[y][x] - tmp[y][x+hsize];
}
}
}
iwave(2);
iwave(4);
iwave(8);
for(var y = 0; y < 8; y++) {
for(var x = 0; x < 8; x++) {
pixels[y][x] += 128;
}
}
}
// pixels -> wavelet transformation
function pixelsToWavelet() {
var tmp = [
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ]
];
for(var y = 0; y < 8; y++) {
for(var x = 0; x < 8; x++) {
wavelet[y][x] = pixels[y][x] - 128;
}
}
function wave(size) {
var hsize = size/2;
// horizontal
for(var y = 0; y < size; y++) {
for(var x = 0; x < hsize; x++) {
var i = x*2;
tmp[y][x] = (wavelet[y][i] + wavelet[y][i+1]) / 2;
tmp[y][x+hsize] = (wavelet[y][i] - wavelet[y][i+1]) / 2;
}
}
// vertical
for(var y = 0; y < hsize; y++) {
for(var x = 0; x < size; x++) {
var i = y*2;
wavelet[y][x] = (tmp[i][x] + tmp[i+1][x]) / 2;
wavelet[y+hsize][x] = (tmp[i][x] - tmp[i+1][x]) / 2;
}
}
}
wave(8);
wave(4);
wave(2);
}
// deep copy 8x8 matrix
function deepCopy(from, to) {
if(from == null || to == null)
return;
for(var y = 0; y < 8; y++) {
for(var x = 0; x < 8; x++) {
to[y][x] = from[y][x];
}
}
}
function updateBlocks() {
for(var y = 0; y < 8; y++) {
for(var x = 0; x < 8; x++) {
pixelDivs[y][x].style.opacity = pixels[y][x] / 255;
var height = (100 - ((wavelet[y][x] + 128) * 100 / 256)) + '%';
var back = 'linear-gradient(to bottom, #1e5799 0%,#1e5799 ' + height + ',#7db9e8 ' + height + ',#7db9e8 100%)';
waveletDivs[y][x].style.background = back;
waveletDivs[y][x].innerHTML = Math.round(wavelet[y][x]);
}
}
}
// register mouse events, save all visible pixel blocks in our pixel array
(function() {
var mouseIsDown = false;
document.addEventListener('mousedown', function() { mouseIsDown = true; });
document.addEventListener('mouseup', function() { mouseIsDown = false; });
// option selection from dropdown list
// usually starts with smiley face
var presetElement = document.querySelector('#presetSelection');
deepCopy(presets[presetElement.value], pixels);
// initialize pixelDivs
for(var y = 0; y < 8; y++) {
pixelDivs.push([]);
waveletDivs.push([]);
for(var x = 0; x < 8; x++) {
// eg.:
// #pixels > .pixel16
var pixelDiv = document.querySelector('#pixels > .pixel' + y + x);
pixelDivs[y].push(pixelDiv);
// change pixel-values when clicking on pixel block
pixelDiv.addEventListener('mousemove', function(e) {
if(mouseIsDown) {
triggerMouseEvent(e, 0, 255, pixels, pixelsToWavelet);
}
});
pixelDiv.addEventListener('click', function(e) {
triggerMouseEvent(e, 0, 255, pixels, pixelsToWavelet);
});
// wavelet grid
var waveletDiv = document.querySelector('#wavelets > .pixel' + y + x);
waveletDivs[y].push(waveletDiv);
// change wavelet-values when clicking on wavelet block
waveletDiv.addEventListener('mousemove', function(e) {
if(mouseIsDown) {
triggerMouseEvent(e, -128, 128, wavelet, waveletToPixels);
}
});
waveletDiv.addEventListener('click', function(e) {
triggerMouseEvent(e, -128, 128, wavelet, waveletToPixels);
});
}
}
pixelsToWavelet();
updateBlocks();
presetElement.addEventListener('change', function() {
// prevent but where mouse is registered as clicked
// even though we only registered a selection change
mouseIsDown = false;
deepCopy(presets[presetElement.value], pixels);
pixelsToWavelet();
updateBlocks();
});
})();