-
Notifications
You must be signed in to change notification settings - Fork 8
/
imageWorker.js
780 lines (652 loc) · 33.4 KB
/
imageWorker.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
let imageBuffers = [];
console.log=(...args)=>{
for (let i = 0; i < args.length; i++) {
if(typeof args[i]=="function")
{
args[i]=args[i].name;
}
if(typeof args[i]=="object")
{
if(args[i].name)
{
args[i]=args[i].name;
}
// args[i]=JSON.stringify(args[i]);
}
}
postMessage({logMessage:args})
// setTimeout(() => {
// for (i = 0; i < args.length; i++) {
// if(typeof args[i]=="undefined")
// {
// args[i]="undefined";
// }
// if(args[i].name)
// {
// args[i]=args[i].name;
// }
// if (typeof args[i] == "object") {
// args[i] = JSON.stringify(args[i]);
// }
// args[i] = args[i] + "";
// }
// throw new Error("MESSAGE FROM WORKER " + args.join(" -- "));
// }, 0);
}
console.log("Image Service worker started")
var uDark={
background_match:/background|sprite|(?<![a-z])(bg|box|panel|fond|fundo|bck)(?![a-z])/i,
logo_match: /nav|avatar|logo|icon|alert|notif|cart|menu|tooltip|dropdown|control/i,
RGBToLightness: (r, g, b) => {
return (Math.max(r, g, b) + Math.min(r, g, b)) / 2;
},
trigger_ratio_size_number_colors: 393,
trigger_ratio_size_number_lightness_photo: 9835,
is_photo:(targetBitmap,editionStatus)=> {
console.log(targetBitmap,targetBitmap.width, targetBitmap.height, "is_photo")
const canvas = new OffscreenCanvas(targetBitmap.width, targetBitmap.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(targetBitmap, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
let pixelCount = data.length / 4;
let opaqueColorCounter = new Set();
let lightnessCounter = new Set();
let trigger_ratio_size_number_lightness_photo=uDark.trigger_ratio_size_number_lightness_photo;
let trigger_ratio_size_number_colors=uDark.trigger_ratio_size_number_colors;
if(editionStatus.isGrayscale)
{
trigger_ratio_size_number_lightness_photo=trigger_ratio_size_number_lightness_photo*2;
trigger_ratio_size_number_colors=trigger_ratio_size_number_colors*2;
}
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const a = data[i + 3];
let number= ((a << 24)) | (b << 16) | (g << 8) | r
lightnessCounter.add(number)
if(a==255){
opaqueColorCounter.add(number);
}
let is_photo = pixelCount/opaqueColorCounter.size <= trigger_ratio_size_number_colors
&& pixelCount/lightnessCounter.size <= trigger_ratio_size_number_lightness_photo;
if(is_photo)
{
return true;
}
}
return false;
},
is_photo_stat: async function(editionStatus, sourceBitmap,details) {
let targetGrid=50;
let targetGridCount=5;
let targetGridWidth=targetGrid*targetGridCount;
let resizeWidth=Math.min(sourceBitmap.width,targetGridWidth),resizeHeight=Math.min(sourceBitmap.height,targetGridWidth);
const targetBitmap = await createImageBitmap(sourceBitmap, 0, 0, sourceBitmap.width, sourceBitmap.height,{
resizeWidth,resizeHeight
});
const isGrayscaleImage = uDark.isGrayscale(targetBitmap,details);
editionStatus.isGrayscale = isGrayscaleImage;
console.log("Image","is_photo_stat isGrayscale",details.url,details.requestId,editionStatus.isGrayscale )
if(resizeHeight==targetGridWidth&&resizeWidth==targetGridWidth)
{
for (let row = 0; row < targetGridCount; row++) {
for (let col = 0; col < targetGridCount; col++) {
console.log("Image","is_photo_stat isGrayscale",details.url,details.requestId,editionStatus.isGrayscale,row,col )
let testBitmap=await createImageBitmap(targetBitmap, col*targetGrid, row*targetGrid, targetGrid, targetGrid);
let is_photo=uDark.is_photo(testBitmap,editionStatus);
if(is_photo)
{
console.log("Image","is_photo_stat isPhotoYes",details.url,details.requestId,editionStatus.isGrayscale,testBitmap )
return true;
}
}
}
}
else
{
return uDark.is_photo(targetBitmap,editionStatus);
}
// Use the targetBitmap for further processing
return false;
},
isGrayscale: (targetBitmap, tolerance=4) => {
console.log(targetBitmap, targetBitmap.width, targetBitmap.height, "isGrayscale");
const canvas = new OffscreenCanvas(targetBitmap.width, targetBitmap.height);
const ctx = canvas.getContext('2d');
ctx.drawImage(targetBitmap, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
let vmax=Math.max(r, g, b);
let vmin=Math.min(r, g, b);
if(vmax-vmin>tolerance)
{
return false;
}
}
return true;
},
check_image_lines_content: function(canvas, ctx) {
const leftBorderImageData = ctx.getImageData(0, 0, 1, canvas.height);
const rightBorderImageData = ctx.getImageData(canvas.width - 1, 0, 1, canvas.height);
const topBorderImageData = ctx.getImageData(0, 0, canvas.width, 1);
const bottomBorderImageData = ctx.getImageData(0, canvas.height - 1, canvas.width, 1);
const centerVerticalLineImageData=ctx.getImageData(canvas.width/2, 0, 1, canvas.height);
const centerHorizontalLineImageData=ctx.getImageData(0, canvas.height/2, canvas.width, 1);
let linesAchromatic = Array(10).fill(false);
let linesGradient = Array(10).fill(false);
let linesAchromaticCount = 0;
let linesAchromaticOpaqueCount = 0;
let linesGradientsCount=0;
let list_test = [leftBorderImageData, rightBorderImageData, topBorderImageData, bottomBorderImageData,centerVerticalLineImageData,centerHorizontalLineImageData]
for (let i = 0; i < list_test.length; i++) {
let imageData = list_test[i];
let theImageDataBufferTMP = new ArrayBuffer(imageData.data.length);
let theImageDataClamped8TMP = new Uint8ClampedArray(theImageDataBufferTMP);
theImageDataClamped8TMP.set(imageData.data);
let theImageDataUint32TMP = new Uint32Array(theImageDataBufferTMP)
let number = theImageDataUint32TMP[0];
var r = number & 0xff;
var g = (number >> 8) & 0xff;
var b = (number >> 16) & 0xff;
var a = (number >> 24) & 0xff;
if (theImageDataUint32TMP.every(x => x == number)) {
linesAchromatic[i] = [r, g, b, a]
linesAchromaticCount++;
if (a == 255) {
linesAchromaticOpaqueCount++;
}
}
let isGradient = true;
let currentLightness=Math.max(uDark.RGBToLightness(r, g, b),a);
for (let n = 1; n < theImageDataUint32TMP.length; n++) {
// Start to 1 as we already checked the first pixel
number = theImageDataUint32TMP[n];
r = number & 0xff;
g = (number >> 8) & 0xff;
b = (number >> 16) & 0xff;
a = (number >> 24) & 0xff;
let lightness=Math.max(uDark.RGBToLightness(r, g, b),a);
if (lightness>currentLightness+5||lightness<currentLightness-5) {
isGradient = false;
break;
}
currentLightness=lightness;
}
if(isGradient)
{
linesGradient[i]=true;
linesGradientsCount++;
}
}
return {
linesAchromaticCount,
linesGradient,
linesAchromaticOpaqueCount,
linesAchromatic,
linesGradientsCount,
linesWithSomeAlpha:linesAchromaticCount-linesAchromaticOpaqueCount,
linesFullAlphaCount:linesAchromatic.filter(x=>x[3]==0).length
}
},
logo_image_edit_hook: function(editionStatus, canvas, ctx, img, blob, details, imageURLObject, complement) {
let start_date = new Date();
console.log("Logos","Entering edition",details.url,details.requestId,uDark.logo_image_edit_hook)
let editionConfidence = 0 +(editionStatus.editionConfidenceLogo);
// Draw the image onto the canvas
ctx.drawImage(img, 0, 0);
// Get the ImageData from the canvas
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// Now you can work with the imageData object
// console.log(imageData, img.src);
// The imageData object has a data property, a Uint8ClampedArray containing the color values of each pixel in the image.
// It is easier to work with this array as 32-bit integers, so we create a new Uint32Array from the original one
let theImageDataBufferTMP = new ArrayBuffer(imageData.data.length);
let theImageDataClamped8TMP = new Uint8ClampedArray(theImageDataBufferTMP);
theImageDataClamped8TMP.set(imageData.data);
let theImageDataUint32TMP = new Uint32Array(theImageDataBufferTMP) // Id prefet o use imageData bu idont uderstand yet why in can't
// let theImageDataUint32TMP = new Uint32Array(imageData.data);
let n = theImageDataUint32TMP.length;
console.log("Logos","Entering the loop",details.url,details.requestId,"Confidence:",editionConfidence,new Date()/1-start_date/1)
if (!editionStatus.statsComplete) {
editionStatus.colorCounter = new Set();
editionStatus.opaqueColorCounter = new Set();
editionStatus.opaqueLightnessCounter = new Set();
editionStatus.lightnessCounter = new Set();
editionStatus.alphaCounter = new Set();
editionStatus.lightness_sum = 0;
editionStatus.opaqueColor_qty = 0;
editionStatus.opaqueLightness_sum = 0;
editionStatus.alpha_sum = 0;
editionStatus.alpha_qty = 0;
}
console.log("Logo HOOK", "ENTERING LOOP", editionStatus.statsComplete)
imgDataLoop: while (n--) {
if (editionStatus.is_photo && !(editionConfidence >= 100)) { // stop the loop if a photo is detected
theImageDataClamped8TMP.set(imageData.data);
break imgDataLoop;
}
var number = theImageDataUint32TMP[n];
var r = number & 0xff;
var g = (number >> 8) & 0xff;
var b = (number >> 16) & 0xff;
var a = (number >> 24) & 0xff;
{
let lightness = uDark.RGBToLightness(r, g, b);
if (!editionStatus.statsComplete) {
editionStatus.colorCounter.add(number);
let lightnessWithAlpha = Math.min(lightness, a); // Alpha kills lightness
editionStatus.lightness_sum += lightnessWithAlpha;
editionStatus.lightnessCounter.add(lightnessWithAlpha);
editionStatus.alphaCounter.add(a);
editionStatus.alpha_sum += a;
if (a == 255) {
editionStatus.opaqueColorCounter.add(number);
editionStatus.opaqueLightnessCounter.add(lightness);
editionStatus.opaqueColor_qty++
editionStatus.opaqueLightness_sum += lightness;
} else {
editionStatus.alpha_qty++;
}
editionStatus.is_photo =
theImageDataUint32TMP.length/editionStatus.opaqueColorCounter.size <= uDark.trigger_ratio_size_number_colors
&& theImageDataUint32TMP.length/ editionStatus.lightnessCounter.size <= uDark.trigger_ratio_size_number_lightness_photo;
}
// Standard way 2023 // very very very slow (1.5s for a 500 x 500 img)
// 2024 way : Go faster by finding the right caclulation for each pixel
// [r, g, b, a] = uDark.revert_rgba(r, g, b, a, (...args) => args);
let lightenUnder=127;
let edit_under_lightness=100;
if (lightness < lightenUnder && lightness<edit_under_lightness) {
// [r,g,b]=[r,g,b].map((x)=>x/2);
[r, g, b] = [r, g, b].map((x) => {
x = x + Math.pow(
(lightenUnder - lightness) // The less the lightness the more the color is lightened
, 1+.11); // Increase the lightening effect a bit
return x;
});
}
}
var newColor = ((a << 24)) | (b << 16) | (g << 8) | r;
theImageDataUint32TMP[n] = newColor;
}
if(!editionStatus.statsComplete)
{
if(n === -1)
{
editionStatus.avg_ligtness = (editionStatus.lightness_sum / theImageDataUint32TMP.length);
editionStatus.alpha_percent = editionStatus.alpha_qty / theImageDataUint32TMP.length;
editionStatus.contrast = Math.max(...editionStatus.lightnessCounter) - Math.min(...editionStatus.lightnessCounter);
editionStatus.contrast_percent = editionStatus.contrast / 255;
editionStatus.max_lightness = Math.max(...editionStatus.lightnessCounter);
editionStatus.min_lightness = Math.min(...editionStatus.lightnessCounter);
editionStatus.statsComplete = true;
}
else {
console.log("Logos","Stat not complete", details.url,details.requestId, editionStatus);
return false;
}
}
console.log("Logos",editionStatus.statsComplete,"Confidence:",editionConfidence,new Date()/1-start_date/1,editionStatus,details.requestId,imageData)
if (editionStatus.statsComplete) {
if (!(editionConfidence >= 100) && !editionStatus.is_photo) {
if (editionStatus.colorCounter.size > 1) // Not achromatic
{
if (editionStatus.alpha_percent < 0.1) {
return false; // No enough alpha to be an editable logo
}
let max_bright_trigger = 255 * 0.4;
if (editionStatus.min_lightness > max_bright_trigger) {
return false; // This is a bright image with which does not need lightening, because even the darkest pixel is bright enough
}
if (editionStatus.contrast_percent > .70) {
// ADD SHADOW TO IMAGE PARTS WITH LIGHTNESS < 25
// Note 25 is < 100-70 there is no conflict with the previous condition
theImageDataClamped8TMP.set(imageData.data);
let theImageDataUint32TMP = new Uint32Array(theImageDataBufferTMP)
let n=theImageDataUint32TMP.length;
imgDataLoop: while (n--) {
var number = theImageDataUint32TMP[n];
var r = number & 0xff;
var g = (number >> 8) & 0xff;
var b = (number >> 16) & 0xff;
var a = (number >> 24) & 0xff;
var newColor = ((a << 24)) | (b << 16) | (g << 8) | r;
let lightness=(Math.max(r, g, b,255-a) + Math.min(r, g, b, 255-a)) / 2;
// lightness=Math.min(lightness); // Alpha kills lightness
if(lightness<25){
number=0xffffffff;
}
else{
number=0x00000000;
}
theImageDataUint32TMP[n] = number;
}
imageData.data.set(theImageDataClamped8TMP);
ctx.putImageData(imageData, 0, 0);
ctx.filter = "blur(3px)";
let resource=canvas;
let options={viaBitmap:false,repetitions:5};
if(options.viaBitmap)
{
resource=canvas.transferToImageBitmap();
}
for(let i=0;i<options.repetitions;i++)
{
ctx.drawImage(resource,0,0);
}
ctx.filter = "none";
ctx.drawImage(img, 0, 0);
editionStatus.edited=true;
return true;
// let shadowImageBitmap = canvas.transferToImageBitmap();
// editionStatus.edited=true;
// return true;
// ctx.drawImage(img, 0, 0);
// // return false;
// // ctx.shadowColor = "white";
// // ctx.shadowBlur = 10;
// // ctx.filter = "contrast(0%) brightness(3) blur(3px)"; // White border no matter what
// ctx.filter=" grayscale() invert(1) brightness(0.6) contrast(900%)";
// // ctx.filter = "brightness(3) invert(1) blur(8px) grayscale() hue-rotate(0deg)"; // Can use it to shift a bit the hue, allowing better reading of the text
// for (let i = 0; i < 5; i++) {
// ctx.drawImage(img, 0, 0);
// }
// ctx.filter = "none";
// //ctx.fillStyle = "red";
// //ctx.fillRect(0, 0, canvas.width, canvas.height);
// editionStatus.edited=true;
// return true;
}
}
}
}
// Pro tip Reset the image at any time : theImageDataClamped8TMP.set(imageData.data);
console.log("Logos", details.url,details.requestId, "Logo edited in", new Date() / 1 - start_date / 1);
imageData.data.set(theImageDataClamped8TMP);
ctx.putImageData(imageData, 0, 0);
console.log("Logos", details.url,details.requestId, "Put back in context", new Date() / 1 - start_date / 1);
editionStatus.edited = true;
return true;
},
background_image_edit_hook: function(editionStatus, canvas, ctx, img, blob, details, imageURLObject, complement) {
let editionConfidence = 0 +(editionStatus.editionConfidenceBackground);
let start_date = new Date();
console.log("Background","Entering edition",details.url,details.requestId,uDark.background_image_edit_hook)
// Refuse bacground images on certain conditions
if(!uDark.disableBackgroundPostCheck)
{
if (
complement.has("width") && !complement.get("width").includes("%") && complement.get("width").startsWith(img.width) // We fetched the image with the same size as the element
||
complement.has("height") && !complement.get("height").includes("%") && complement.get("height").startsWith(img.height) // We fetched the image with the same size as the element
) {
return false;
}
if (complement.has("uDark_backgroundRepeat") && /repeat|round|space/i.test(complement.get("uDark_backgroundRepeat").replaceAll("no-repeat", "")) ||
complement.has("alt") && /background/i.test(complement.get("alt"))
) {
editionConfidence += 200;
}
}
// Draw the image onto the canvas
ctx.drawImage(img, 0, 0);
// Now you can work with the imageData object
// console.log(imageData, img.src);
// The imageData object has a data property, a Uint8ClampedArray containing the color values of each pixel in the image.
// It is easier to work with this array as 32-bit integers, so we create a new Uint32Array from the original one.
if (complement.has("uDark_cssClass") && !(editionConfidence >= 100)) {
let linesColorCheck = uDark.check_image_lines_content(canvas, ctx)
if ((linesColorCheck.linesAchromaticOpaqueCount > 2 || linesColorCheck.linesGradientsCount > 2)&&linesColorCheck.linesFullAlphaCount==0) {
editionConfidence += 100;
}
editionStatus.linesColorCheck = linesColorCheck;
}
// Get the ImageData from the canvas
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
let theImageDataBufferTMP = new ArrayBuffer(imageData.data.length);
let theImageDataClamped8TMP = new Uint8ClampedArray(theImageDataBufferTMP);
theImageDataClamped8TMP.set(imageData.data);
let theImageDataUint32TMP = new Uint32Array(theImageDataBufferTMP) // Id prefet o use imageData bu idont uderstand yet why in can't
// let theImageDataUint32TMP = new Uint32Array(imageData.data);
let n = theImageDataUint32TMP.length;
console.log("Background","Entering the loop",details.url,details.requestId,"Confidence:",editionConfidence,new Date()/1-start_date/1,)
if (!editionStatus.statsComplete) {
editionStatus.colorCounter = new Set();
editionStatus.opaqueColorCounter = new Set();
editionStatus.opaqueLightnessCounter = new Set();
editionStatus.lightnessCounter = new Set();
editionStatus.alphaCounter = new Set();
editionStatus.lightness_sum = 0;
editionStatus.opaqueLightness_sum = 0;
editionStatus.alpha_sum = 0;
editionStatus.alpha_qty = 0;
}
imgDataLoop: while (n--) {
if (editionStatus.is_photo && !(editionConfidence >= 100)) { // stop the loop if a photo is detected
theImageDataClamped8TMP.set(imageData.data);
break imgDataLoop;
}
var number = theImageDataUint32TMP[n];
var r = number & 0xff;
var g = (number >> 8) & 0xff;
var b = (number >> 16) & 0xff;
var a = (number >> 24) & 0xff;
{
let lightness = uDark.RGBToLightness(r, g, b);
editionStatus.colorCounter.add(number);
let lightnessWithAlpha = Math.min(lightness, a); // Alpha kills lightness
editionStatus.lightness_sum += lightnessWithAlpha;
editionStatus.lightnessCounter.add(lightnessWithAlpha);
editionStatus.alphaCounter.add(a);
editionStatus.alpha_sum += a;
if (a == 255) {
editionStatus.opaqueColorCounter.add(number);
editionStatus.opaqueLightnessCounter.add(lightness);
editionStatus.opaqueLightness_sum += lightness;
} else {
editionStatus.alpha_qty++;
}
// Standard way 2023 // very very very slow (1.5s for a 500 x 500 img)
// 2024 way : Go faster by finding the right caclulation for each pixel
// [r, g, b, a] = uDark.revert_rgba(r, g, b, a, (...args) => args);
if (lightness > 127) {
// [r,g,b]=[r,g,b].map((x)=>x/2);
[r, g, b] = [r, g, b].map((x) => {
x = x * Math.pow(
255 / (lightness + 127), // The more the lightness is high, the more the color is darkened
lightness / 127 * 2.8 // The more the lightness is high, the more the darkeing is strong
);
return x;
});
}
let opaqueColorCounterRatio = theImageDataUint32TMP.length/editionStatus.opaqueColorCounter.size
let ligthnessCounterRatio = theImageDataUint32TMP.length/editionStatus.lightnessCounter.size
editionStatus.is_photo = opaqueColorCounterRatio <= uDark.trigger_ratio_size_number_colors
&& ligthnessCounterRatio <= uDark.trigger_ratio_size_number_lightness_photo
&& ligthnessCounterRatio>uDark.trigger_ratio_size_number_lightness_photo*0.07;
if(editionStatus.is_photo)
{
editionConfidence--;
}
}
var newColor = ((a << 24)) | (b << 16) | (g << 8) | r;
theImageDataUint32TMP[n] = newColor;
}
if(!editionStatus.statsComplete)
{
if (n === -1) {
editionStatus.avg_ligtness = (editionStatus.lightness_sum / theImageDataUint32TMP.length);
editionStatus.alpha_percent = editionStatus.alpha_qty / theImageDataUint32TMP.length;
editionStatus.contrast = Math.max(...editionStatus.lightnessCounter) - Math.min(...editionStatus.lightnessCounter);
editionStatus.contrast_percent = editionStatus.contrast / 255;
editionStatus.max_lightness = Math.max(...editionStatus.lightnessCounter);
editionStatus.min_lightness = Math.min(...editionStatus.lightnessCounter);
editionStatus.statsComplete = true;
} else {
console.log("Background","Stat not complete", details.url,details.requestId, editionStatus);
return false;
}
}
// throw new Error(editionStatus.lightnessCounter.size+" "+details.url);
console.log("Background","Stats complete",editionStatus.statsComplete,details.requestId,"Confidence:",editionConfidence,new Date()/1-start_date/1,editionStatus,imageData)
if (editionStatus.statsComplete&&!uDark.disableBackgroundPostCheck) {
if (!(editionConfidence >= 100) && !editionStatus.is_photo) {
if (editionStatus.contrast_percent > .77 &&
editionStatus.avg_ligtness > 220 &&
editionStatus.max_lightness >= 204
) {
ctx.filter = "invert(0.9) hue-rotate(180deg)";
ctx.drawImage(img, 0, 0);
editionStatus.edited = true;
return true;
} else if (editionStatus.colorCounter.size > 1) {
if (editionStatus.alpha_percent > 0.1 && !(editionStatus.alpha_qty * 2 > theImageDataUint32TMP.length)) {
return false; // Not a background : Too much pure alpha pixels
}
let min_bright_trigger = 255 * 0.4;
if (editionStatus.avg_ligtness < min_bright_trigger && editionStatus.max_lightness < min_bright_trigger) {
editionStatus.probable_dark_background_having_contrast = true;
return false; // If its a background, i'ts already a dark one, and it has no contrast elements, no need to darken it more
}
if (editionStatus.contrast_percent > .45 && editionStatus.avg_ligtness > 127 || editionStatus.contrast_percent > .60) {
return false; // This image is bright but seems to have contrast elements, if we darken it we will loose these elements
}
}
}
}
console.log("Background", details.url,details.requestId, "Background edited in", new Date() / 1 - start_date / 1);
imageData.data.set(theImageDataClamped8TMP);
ctx.putImageData(imageData, 0, 0);
console.log("Background", details.url,details.requestId, "Put back in context", new Date() / 1 - start_date / 1);
editionStatus.edited = true;
},
edit_an_image: async function(details) {
details.requestId=details.url.split('').map(v=>v.charCodeAt(0)).reduce((a,v)=>a+((a<<7)+(a<<3))^v).toString(16);
let editionStatus = {
edited: false,
statsComplete: false,
editionConfidenceLogo:0,
editionConfidenceBackground:0,
};
// Determine some basic required things about the image
let imageURLObject = new URL(details.url);
// Determine the transformation function to use
let complementIndex = imageURLObject.hash.indexOf("_uDark")
let complement=new URLSearchParams(complementIndex==-1?"":imageURLObject.hash.slice(complementIndex+6))
let edition_order_hooks = [uDark.background_image_edit_hook,
uDark.logo_image_edit_hook
];
if(uDark.background_match.test(imageURLObject.search+ complement.get("class")+complement.get("uDark_cssClass")))
{
editionStatus.editionConfidenceBackground=100;
edition_order_hooks = [uDark.background_image_edit_hook];
}
if (
complement.has("inside_clickable")
|| uDark.logo_match.test(imageURLObject.search+ complement.get("class")+complement.get("uDark_cssClass"))
) {
edition_order_hooks = [uDark.logo_image_edit_hook];
}
if (!edition_order_hooks.length) {
return {}
}
console.log("Image", "Editing image",details.fromCache, details.url,details.requestId,details.isDataUrl);
// Do the common tasks required for all edited images
let start_date = new Date();
console.log("Image", "Filter has stopped", new Date() / 1 - start_date / 1,details.url,details.requestId);
// NOTE: TODO: Improve: If the image has a 404 state the server returns html, and canvas can't draw it and loose at least 3 seconds understanding it
if (details.statusCode && details.statusCode >= 400) {
return editionStatus;
}
// let blob = {
// arrayBuffer: x => ({
// then: x => details.dataUrl
// })
// }
let blob = (new Blob(imageBuffers));
console.log("Image","Blob created", new Date() / 1 - start_date ,details.url,details.requestId)
let imageBitmap;
try
{
imageBitmap=await createImageBitmap(blob);
}
catch(e)
{
console.log("Image","Invalid or disposed image", new Date() / 1 - start_date ,details.url,details.requestId)
// Invalid or disposed image.
return editionStatus;
}
if(uDark.experimentalAttemptImageStat)
{
editionStatus.is_photo = await uDark.is_photo_stat(editionStatus, imageBitmap,details);
}
if(editionStatus.is_photo){editionStatus.editionConfidenceBackground=-1000}
// blob.arrayBuffer().then((buffer) => {
{
console.log("Image","created ImageBitmap", new Date() / 1 - start_date ,details.url,details.requestId)
// Create an Image object
// const reader = new FileReader() // Faster but ad what cost later ?
// // 2. Add a handler for the 'onload' event
// reader.onload = (e) => {
// // 5. Get the result when the 'onload' event is triggered.
// const base64data = reader.result
// console.log("Image to base 64",new Date() / 1 - start_date,details.url,details.requestId,{base64data});
// img.src = base64data;
// }
// // 3. Add a handler for the 'onerror' event
// reader.onerror = () => {
// console.log('error')
// }
// // // 4. Call 'readAsDataURL' method
// reader.readAsDataURL(blob)
const canvas = new OffscreenCanvas(imageBitmap.width, imageBitmap.height);
const ctx = canvas.getContext('2d');
for (let imageEditionHook of edition_order_hooks) {
if (editionStatus.edited === false) {
console.log("Image edition hook",imageEditionHook, new Date() / 1 - start_date ,details.url,details.requestId)
imageEditionHook(editionStatus, canvas, ctx, imageBitmap, blob, details, imageURLObject, complement);
console.log("Image acceptance",editionStatus.edited,editionStatus.rejected, new Date() / 1 - start_date ,details.url,details.requestId)
}
}
if (editionStatus.edited === true) {
// Write the edited image to the filter and disconnect it
console.log("Image", "Going blob", new Date() / 1 - start_date / 1,details.url,details.requestId);
let editedBlobWithImageHeaders= await canvas.convertToBlob();
let buffer= await editedBlobWithImageHeaders.arrayBuffer();
console.log("Image", "Canvas to blob done", new Date() / 1 - start_date / 1,details.url,details.requestId);
console.log("Image", "Blob to arraybuffer", new Date() / 1 - start_date / 1,details.url,details.requestId);
editionStatus.editedBuffer = buffer;
console.log("Image", "Image written in filter having edited it", new Date() / 1 - start_date / 1,details.url,details.requestId);
}
return editionStatus;
// filter.write(theImageDataUint32TMP.buffer);
// filter.write(details.buffers[0]);
// filter.disconnect();
};
}
}
onmessage = async (e) => {
if(e.data.oneImageBuffer)
{
imageBuffers.push(e.data.oneImageBuffer);
}
if(e.data.filterStopped)
{
let editionResult = {};
editionResult = await uDark.edit_an_image(e.data.details);
if(editionResult.editedBuffer)
{
imageBuffers=[editionResult.editedBuffer];
}
console.log("Sending back",imageBuffers,editionResult.editedBuffer);
postMessage({editionComplete:1,buffers:imageBuffers},imageBuffers);
console.log("filter stopped");
imageBuffers=[];
}
};