-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
268 lines (244 loc) · 6.67 KB
/
script.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
var backup = null;
var backup2 = null;
function upfor() {
can1 = document.getElementById("can1");
forg = document.getElementById("forg");
fimg = new SimpleImage(forg);
backup = new SimpleImage(forg);
fimg.drawTo(can1);
}
function upback() {
can2 = document.getElementById("can2");
back = document.getElementById("back");
bimg = new SimpleImage(back);
backup2 = new SimpleImage(back);
bimg.drawTo(can2);
}
function checkf() {
if (backup == null || !backup.complete()) {
alert("Foreground image not loaded yet");
return false;
}
}
function checkb() {
if (backup2 == null || !backup2.complete()) {
alert("Background image not loaded yet");
return false;
}
}
function reset() {
can3 = document.getElementById("can3");
var ct = can3.getContext("2d");
ct.beginPath();
ct.save();
ct.setTransform(1, 0, 0, 1, 0, 0);
ct.clearRect(0, 0, can3.width, can3.height);
ct.restore();
}
function makegreens(forgim, backgim) {
greensout = new SimpleImage(forgim.getWidth(), forgim.getHeight());
for (var px of forgim.values()) {
var x = px.getX();
var y = px.getY();
var bpx = backgim.getPixel(x, y);
if (px.getGreen() >= 200 && px.getRed() <= 200 && px.getBlue() <= 200) {
greensout.setPixel(x, y, bpx);
} else {
greensout.setPixel(x, y, px);
}
}
return greensout;
}
function dogreens() {
checkf();
checkb();
var greenf = backup;
var greeb = backup2;
reset();
makegreens(greenf, greeb);
greensout.drawTo(can3);
}
function makegrays(image) {
grayout = new SimpleImage(image.getWidth(), image.getHeight());
for (var px of image.values()) {
var opx = grayout.getPixel(px.getX(), px.getY());
var avg = (px.getRed() + px.getGreen() + px.getBlue()) / 3;
opx.setRed(avg);
opx.setGreen(avg);
opx.setBlue(avg);
}
return grayout;
}
function dograys() {
checkf();
var gsimg = backup;
reset();
makegrays(gsimg);
grayout.drawTo(can3);
}
function makered(image) {
redout = new SimpleImage(image.getWidth(), image.getHeight());
for (var px of image.values()) {
var avg = (px.getRed() + px.getGreen() + px.getBlue()) / 3;
var opx = redout.getPixel(px.getX(), px.getY());
if (avg >= 128) {
opx.setRed(255);
opx.setGreen(2 * avg - 255);
opx.setBlue(2 * avg - 255);
} else {
opx.setRed(2 * avg);
opx.setGreen(0);
opx.setBlue(0);
}
}
return redout;
}
function dored() {
checkf();
var redimg = backup;
reset();
makered(redimg);
redout.drawTo(can3);
}
function makebw(image) {
bwout = new SimpleImage(image.getWidth(), image.getHeight());
for (var px of image.values()) {
var opx = bwout.getPixel(px.getX(), px.getY());
var avg = (px.getRed() + px.getGreen() + px.getBlue()) / 3;
opx.setRed(2 * avg);
opx.setGreen(2 * avg);
opx.setBlue(2 * avg);
}
return bwout;
}
function dobw() {
checkf();
var bwimg = backup;
reset();
makebw(bwimg);
bwout.drawTo(can3);
}
function makeblur(image) {
blout = new SimpleImage(image.getWidth(), image.getHeight());
for (var px of image.values()) {
var rnd = Math.random();
var x = px.getX();
var y = px.getY();
var nx = x + Math.floor((Math.random() * 10) + 1);
var ny = y + Math.floor((Math.random() * 10) + 1);
if (nx > image.getWidth() - 1) {
nx = image.getWidth() - 1;
}
if (nx < 0) {
nx = 0;
}
if (ny > image.getHeight() - 1) {
ny = image.getHeight() - 1;
}
if (ny < 0) {
ny = 0;
}
var npx = image.getPixel(nx, ny);
if (rnd < 0) {
blout.setPixel(x, y, px);
} else {
blout.setPixel(nx, ny, npx);
}
}
return blout;
}
function doblur() {
checkf();
var blimg = backup;
reset();
makeblur(blimg);
blout.drawTo(can3);
}
function crop(hide, hidein) {
cropout = new SimpleImage(hide.getWidth(), hide.getHeight());
var hiw = hidein.getWidth();
var hw = hide.getWidth();
var hih = hidein.getHeight();
var hh = hide.getHeight();
if (hw < hiw) {
if (hh < hih) {
hidein.setSize(hw, hh);
}
}
if (hw > hiw) {
if (hh > hih) {
hidein.setSize(hw, hh);
}
}
for (var px of cropout.values()) {
var hipx = hidein.getPixel(px.getX(), px.getY());
px.setRed(hipx.getRed());
px.setGreen(hipx.getGreen());
px.setBlue(hipx.getBlue());
}
return cropout;
}
function clearbits(colorval) {
var color = Math.floor(colorval / 16) * 16;
return color;
}
function prephidein(hidein) {
prepout = new SimpleImage(hidein.getWidth(), hidein.getHeight());
for (var px of prepout.values()) {
var hipx = hidein.getPixel(px.getX(), px.getY());
px.setRed(clearbits(hipx.getRed()));
px.setGreen(clearbits(hipx.getGreen()));
px.setBlue(clearbits(hipx.getBlue()));
}
return prepout;
}
function shiftcolor(hide) {
shiftout = new SimpleImage(hide.getWidth(), hide.getHeight());
for (var px of shiftout.values()) {
var hpx = hide.getPixel(px.getX(), px.getY());
px.setRed(hpx.getRed() / 16);
px.setGreen(hpx.getGreen() / 16);
px.setBlue(hpx.getBlue() / 16);
}
return shiftout;
}
function makesteg(hide, hidein) {
stegout = new SimpleImage(hide.getWidth(), hide.getHeight());
for (var px of stegout.values()) {
var hpx = hide.getPixel(px.getX(), px.getY());
var hipx = hidein.getPixel(px.getX(), px.getY());
px.setRed(hipx.getRed() + hpx.getRed());
px.setGreen(hipx.getGreen() + hpx.getGreen());
px.setBlue(hipx.getBlue() + hpx.getBlue());
}
return stegout;
}
function dosteg() {
checkf();
checkb();
var hide = backup;
var hidein = backup2;
reset();
crop(hide, hidein);
prephidein(cropout);
shiftcolor(hide);
makesteg(shiftout, prepout);
stegout.drawTo(can3);
}
function unsteg(hidden) {
unstegout = new SimpleImage(hidden.getWidth(), hidden.getHeight());
for (var px of unstegout.values()) {
var hpx = hidden.getPixel(px.getX(), px.getY());
px.setRed(hpx.getRed() % 16 * 16);
px.setGreen(hpx.getGreen() % 16 * 16);
px.setBlue(hpx.getBlue() % 16 * 16);
}
return unstegout;
}
function dounsteg() {
checkf();
var hidden = backup;
reset();
unsteg(hidden);
unstegout.drawTo(can3);
}