This repository was archived by the owner on Aug 27, 2022. It is now read-only.
forked from andreknieriem/photobooth
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathchromakeying.js
More file actions
278 lines (238 loc) · 8.58 KB
/
chromakeying.js
File metadata and controls
278 lines (238 loc) · 8.58 KB
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
/* globals MarvinColorModelConverter AlphaBoundary MarvinImage i18n */
/* exported setBackgroundImage */
let mainImage;
let mainImageWidth;
let mainImageHeight;
let backgroundImage;
let isPrinting = false;
function greenToTransparency(imageIn, imageOut) {
for (let y = 0; y < imageIn.getHeight(); y++) {
for (let x = 0; x < imageIn.getWidth(); x++) {
const color = imageIn.getIntColor(x, y);
const hsv = MarvinColorModelConverter.rgbToHsv([color]);
if (hsv[0] >= 60 && hsv[0] <= 200 && hsv[1] >= 0.2 && hsv[2] >= 0.2) {
imageOut.setIntColor(x, y, 0, 127, 127, 127);
} else {
imageOut.setIntColor(x, y, color);
}
}
}
}
function reduceGreen(image) {
for (let y = 0; y < image.getHeight(); y++) {
for (let x = 0; x < image.getWidth(); x++) {
const r = image.getIntComponent0(x, y);
const g = image.getIntComponent1(x, y);
const b = image.getIntComponent2(x, y);
const color = image.getIntColor(x, y);
const hsv = MarvinColorModelConverter.rgbToHsv([color]);
if (hsv[0] >= 60 && hsv[0] <= 130 && hsv[1] >= 0.15 && hsv[2] >= 0.15) {
if (r * b != 0 && (g * g) / (r * b) > 1.5) {
image.setIntColor(x, y, 255, r * 1.4, g, b * 1.4);
} else {
image.setIntColor(x, y, 255, r * 1.2, g, b * 1.2);
}
}
}
}
}
function alphaBoundary(imageOut, radius) {
const ab = new AlphaBoundary();
for (let y = 0; y < imageOut.getHeight(); y++) {
for (let x = 0; x < imageOut.getWidth(); x++) {
ab.alphaRadius(imageOut, x, y, radius);
}
}
}
function setMainImage(imgSrc) {
const image = new MarvinImage();
image.load(imgSrc, function () {
mainImageWidth = image.getWidth();
mainImageHeight = image.getHeight();
const imageOut = new MarvinImage(image.getWidth(), image.getHeight());
//1. Convert green to transparency
greenToTransparency(image, imageOut);
// 2. Reduce remaining green pixels
reduceGreen(imageOut);
// 3. Apply alpha to the boundary
alphaBoundary(imageOut, 6);
const tmpCanvas = document.createElement('canvas');
tmpCanvas.width = mainImageWidth;
tmpCanvas.height = mainImageHeight;
imageOut.draw(tmpCanvas);
mainImage = new Image();
mainImage.src = tmpCanvas.toDataURL('image/png');
mainImage.onload = function () {
drawCanvas();
};
});
}
// eslint-disable-next-line no-unused-vars
function setBackgroundImage(url) {
backgroundImage = new Image();
backgroundImage.src = url;
backgroundImage.onload = function () {
drawCanvas();
};
}
function drawCanvas() {
const canvas = document.getElementById('mainCanvas');
if (typeof mainImage !== 'undefined' && mainImage !== null) {
canvas.width = mainImage.width;
canvas.height = mainImage.height;
} else if (typeof backgroundImage !== 'undefined' && backgroundImage !== null) {
canvas.width = backgroundImage.width;
canvas.height = backgroundImage.height;
}
const ctx = canvas.getContext ? canvas.getContext('2d') : null;
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (typeof backgroundImage !== 'undefined' && backgroundImage !== null) {
if (typeof mainImage !== 'undefined' && mainImage !== null) {
const size = calculateAspectRatioFit(
backgroundImage.width,
backgroundImage.height,
mainImage.width,
mainImage.height
);
ctx.drawImage(backgroundImage, 0, 0, size.width, size.height);
} else {
ctx.drawImage(backgroundImage, 0, 0, backgroundImage.width, backgroundImage.height);
}
}
if (typeof mainImage !== 'undefined' && mainImage !== null) {
ctx.drawImage(mainImage, 0, 0);
}
}
function calculateAspectRatioFit(srcWidth, srcHeight, maxWidth, maxHeight) {
const ratio = Math.max(maxWidth / srcWidth, maxHeight / srcHeight);
return {
width: srcWidth * ratio,
height: srcHeight * ratio
};
}
function printImage(filename, cb) {
const errormsg = i18n('error');
if (isPrinting) {
console.log('Printing already: ' + isPrinting);
} else {
isPrinting = true;
setTimeout(function () {
$.ajax({
method: 'GET',
url: 'api/print.php',
data: {
filename: filename
},
success: (data) => {
console.log('Picture processed: ', data);
if (data.error) {
console.log('An error occurred: ', data.error);
$('#print_mesg').empty();
$('#print_mesg').html(
'<div class="modal__body"><span style="color:red">' + data.error + '</span></div>'
);
}
setTimeout(function () {
$('#print_mesg').removeClass('modal--show');
if (data.error) {
$('#print_mesg').empty();
$('#print_mesg').html(
'<div class="modal__body"><span>' + i18n('printing') + '</span></div>'
);
}
cb();
isPrinting = false;
}, config.printing_time);
},
error: (jqXHR, textStatus) => {
console.log('An error occurred: ', textStatus);
$('#print_mesg').empty();
$('#print_mesg').html(
'<div class="modal__body"><span style="color:red">' + errormsg + '</span></div>'
);
setTimeout(function () {
$('#print_mesg').removeClass('modal--show');
$('#print_mesg').empty();
$('#print_mesg').html('<div class="modal__body"><span>' + i18n('printing') + '</span></div>');
cb();
isPrinting = false;
}, 5000);
}
});
}, 1000);
}
}
function saveImage(cb) {
const canvas = document.getElementById('mainCanvas');
const dataURL = canvas.toDataURL('image/png');
$.post(
'api/chromakeying/save.php',
{
imgData: dataURL
},
function (data) {
if (cb) {
cb(data);
}
}
);
}
function printImageHandler(ev) {
ev.preventDefault();
$('#print_mesg').addClass('modal--show');
setTimeout(function () {
saveImage((data) => {
if (!data.success) {
return;
}
printImage(data.filename, () => {
$('#print-btn').blur();
});
});
}, 1000);
}
function saveImageHandler(ev) {
ev.preventDefault();
$('#save_mesg').addClass('modal--show');
setTimeout(function () {
saveImage(() => {
setTimeout(function () {
$('#save_mesg').removeClass('modal--show');
$('#save-btn').blur();
}, 2000);
});
}, 1000);
}
function closeHandler(ev) {
ev.preventDefault();
if (document.referrer) {
window.location = document.referrer;
} else {
window.history.back();
}
}
$(document).on('keyup', function (ev) {
if (config.use_print_chromakeying && config.print_key && parseInt(config.print_key, 10) === ev.keyCode) {
if (isPrinting) {
console.log('Printing already in progress!');
} else {
$('#print-btn').trigger('click');
}
}
});
$(document).ready(function () {
$('#save-btn').on('click', saveImageHandler);
$('#print-btn').on('click', printImageHandler);
$('#close-btn').on('click', closeHandler);
setTimeout(function () {
setMainImage($('body').attr('data-main-image'));
}, 100);
// we don't want to scroll on small or horizontal devices
const windowHeight = $(window).innerHeight();
const bottomLine = $('.chroma-control-bar').position().top + $('.chroma-control-bar').outerHeight(true);
const diff = bottomLine - windowHeight;
if (diff > 0) {
const canvasHeight = $('#mainCanvas').height();
$('#mainCanvas').css('height', canvasHeight - diff + 'px');
}
});