This repository has been 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 39
/
chromakeying.js
339 lines (294 loc) · 11.2 KB
/
chromakeying.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* globals MarvinColorModelConverter AlphaBoundary MarvinImage Seriously initRemoteBuzzerFromDOM rotaryController photoboothTools */
/* exported setBackgroundImage */
let mainImage;
let mainImageWidth;
let mainImageHeight;
let backgroundImage;
let isPrinting = false;
let seriously;
let target;
let chroma;
let seriouslyimage;
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) {
if (config.keying.variant === 'marvinj') {
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();
};
});
} else {
const image = new Image();
image.src = imgSrc;
image.onload = function () {
mainImageWidth = image.width;
mainImageHeight = image.height;
// create tmpcanvas and size it to image size
const tmpCanvas = document.createElement('canvas');
tmpCanvas.width = mainImageWidth;
tmpCanvas.height = mainImageHeight;
tmpCanvas.id = 'tmpimageout';
// append Canvas for Seriously to chromakey the image
// eslint-disable-next-line no-unused-vars
const body = document.getElementsByTagName('body')[0];
document.body.appendChild(tmpCanvas);
seriously = new Seriously();
target = seriously.target('#tmpimageout');
seriouslyimage = seriously.source(image);
chroma = seriously.effect('chroma');
chroma.source = seriouslyimage;
target.source = chroma;
const color = config.keying.seriouslyjs_color;
const r = parseInt(color.substr(1, 2), 16) / 255;
const g = parseInt(color.substr(3, 2), 16) / 255;
const b = parseInt(color.substr(5, 2), 16) / 255;
photoboothTools.console.logDev('Chromakeying color:', color);
photoboothTools.console.logDev('Red:', r, 'Green:', g, 'Blue:', b);
chroma.screen = [r, g, b, 1];
seriously.go();
mainImage = new Image();
mainImage.src = tmpCanvas.toDataURL('image/png');
mainImage.onload = function () {
drawCanvas();
};
};
image.src = imgSrc;
}
}
// 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) {
if (config.keying.variant === 'marvinj') {
ctx.drawImage(mainImage, 0, 0);
} else {
//important to fetch tmpimageout
ctx.drawImage(document.getElementById('tmpimageout'), 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 = photoboothTools.getTranslation('error');
if (isPrinting) {
photoboothTools.console.log('Printing already: ' + isPrinting);
} else {
isPrinting = true;
setTimeout(function () {
$.ajax({
method: 'GET',
url: 'api/print.php',
data: {
filename: filename
},
success: (data) => {
photoboothTools.console.log('Picture processed: ', data);
if (data.error) {
photoboothTools.console.log('An error occurred: ', data.error);
photoboothTools.modal.empty('#print_mesg');
$('#print_mesg').html(
'<div class="modal__body"><span style="color:red">' + data.error + '</span></div>'
);
}
setTimeout(function () {
photoboothTools.modal.close('#print_mesg');
if (data.error) {
photoboothTools.modal.empty('#print_mesg');
$('#print_mesg').html(
'<div class="modal__body"><span>' +
photoboothTools.getTranslation('printing') +
'</span></div>'
);
}
cb();
isPrinting = false;
}, config.print.time);
},
error: (jqXHR, textStatus) => {
photoboothTools.console.log('An error occurred: ', textStatus);
photoboothTools.modal.empty('#print_mesg');
$('#print_mesg').html(
'<div class="modal__body"><span style="color:red">' + errormsg + '</span></div>'
);
setTimeout(function () {
photoboothTools.modal.close('#print_mesg');
photoboothTools.modal.empty('#print_mesg');
$('#print_mesg').html(
'<div class="modal__body"><span>' +
photoboothTools.getTranslation('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();
photoboothTools.modal.open('#print_mesg');
setTimeout(function () {
saveImage((data) => {
if (!data.success) {
return;
}
printImage(data.filename, () => {
$('#print-btn').blur();
});
});
}, 1000);
}
function saveImageHandler(ev) {
ev.preventDefault();
photoboothTools.modal.open('#save_mesg');
setTimeout(function () {
saveImage(() => {
setTimeout(function () {
photoboothTools.modal.close('#save_mesg');
$('#save-chroma-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.print.from_chromakeying && config.print.key && parseInt(config.print.key, 10) === ev.keyCode) {
if (isPrinting) {
photoboothTools.console.log('Printing already in progress!');
} else {
$('#print-btn').trigger('click');
}
}
});
$(document).ready(function () {
$('#save-chroma-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');
}
$('.canvasWrapper').removeClass('initial');
initRemoteBuzzerFromDOM();
rotaryController.focusSet('.chromawrapper');
});