-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
afre.gmic
1037 lines (941 loc) · 50.4 KB
/
afre.gmic
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#@gmic
#
# File : afre.gmic
# ( G'MIC commands file )
#
# Description : G'MIC commands and filters by afre.
#
# Copyright : Alan Kwan
# ( https://discuss.pixls.us/u/afre )
#
# License : CeCILL v2.0
# ( http://cecill.info/licences/Licence_CeCILL_V2-en.html )
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
#
#@gui _<b>Colors</b>
#-------------------
#@gui Brightness : afre_brightness, afre_brightness_preview(1)+
#@gui : note = note("<strong>Enhance luminance brightness. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020-2021 Jan8.</strong>\n\n")
#@gui : Amount = int(50,-300,300)
#@gui : Smooth = int(0,0,50)
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_brightness : -300<=amount<=300,0<=smooth<=50
#@cli : Enhance luminance brightness.
#@cli : Default values: 'amount=50' and 'smooth=0'.
afre_brightness : check "${1=50}>=-300 && ${2=0}>=0
&& $1<=300 && $2<=50"
if !$1 return fi e[] "[afre]^_^) brightness: Enhance luminance brightness of image$? using amount=$1 and smooth=$2."
repeat $! l[$>] r={[im,iM]} . if $2>0 +afre_gui0_fast. 1,1e10
+>.. {100-$2}% blend_fade[1,2] . k[0,1] fi
^.. {$1>0?0.5^($1/300):1} n.. $r afre_orien.. 1
+n. 0,1 f. "begin(const P=2^($1/100)); 1-(1-i)^P" n. $r
+n.. 0,1 f. "begin(const A=0.5; const P=0.05); if(i<=A,
A-A*(i/A)^P,-A+A*((1-i)/A)^P)" n. 0,1 blend_fade[1,2] . k[0,1]
afre_y50. 1 * c $r done done
afre_brightness_preview :
afre_brightness $*
#@gui Contrast : afre_contrast, afre_contrast_preview(1)+
#@gui : note = note("<strong>Enhance luminance contrast. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020 Jan9.</strong>\n\n")
#@gui : Amount = int(50,-200,200)
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_contrast : -200<=amount<=200
#@cli : Enhance luminance contrast.
#@cli : Default value: 'amount=50'.
afre_contrast : check "${1=50}>=-200 && $1<=200"
if !$1 return fi e[] "[afre]^_^) contrast: Enhance luminance contrast of image$? using amount=$1."
repeat $! l[$>] r={[im,iM]} . afre_orien.. 1
. +n. 0,1 f. "begin(const A=ia; const P=2^($1/100));
if(i<A,A*(i/A)^P,1-(1-A)*((1-i)/(1-A))^P)" n. $r
+n.. 0,1 f. gauss(i-0.5) n. 0,1 blend_fade[2,3] . k[0-2]
+n.. 0,1 f. "begin(const A=0.5; const P=0.05); if(i<=A,
A-A*(i/A)^P,-A+A*((1-i)/A)^P)" n. 0,1 blend_fade[1,2] . k[0,1]
afre_y50. 1 * n $r done done
afre_contrast_preview :
afre_contrast $*
#@gui Local Contrast : afre_localcontrast, afre_localcontrast_preview(1)+
#@gui : note = note("<strong>Enhance local contrast. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020 Jul28-Sep5.</strong>\n\n")
#@gui : Radius = int(1,1,10)
#@gui : Amount = int(50,-100,100)
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_localcontrast : 1<=radius<=10,-100<=_amount<=100
#@cli : Enhance local contrast.
#@cli : Default values: 'radius=1' and 'amount=50'.
afre_localcontrast : check "${1=1}>=1 && ${2=50}>=-100 && $1<=10 && $2<=100"
if !$2 return fi rd={int($1)} e[] "[afre]^_^) localcontrast: Enhance local contrast of image$? using radius="$rd" and amount=$2."
repeat $! l[$>] r0={[im,iM]} n 0,255 rgb2jzazbz 0 sh 0 r1={[im,iM]}
. +afre_gui0_fast. $1,1e0 n. 0,1 f. "begin(const boundary=1;
const D="$rd"*2+1; const P=4^($2/50));
ref(crop(x-"$rd",y-"$rd",0,c,D,D,1,1),N); A=avg(i,med(N));
if(i<A,A*(i/A)^P,1-(1-A)*((1-i)/(1-A))^P)" n. $r1
+n.. 0,1 f. gauss(i) n. 0,1 blend_fade[2,3] . k[0-2]
+n.. 0,1 f. "begin(const A=0.5; const P=0.05); if(i<=A,
A-A*(i/A)^P,-A+A*((1-i)/A)^P)" n. 0,1 blend_fade[1,2] . k[0]
jzazbz2rgb 0 n $r0 done done
afre_localcontrast_preview :
afre_localcontrast $*
#@gui Softlight : afre_softlight, afre_softlight_preview(1)+
#@gui : note = note("<strong>Blend image with itself or another image using softlight. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2019-2020 Sept9.</strong>\n\n")
#@gui : Amount = int(50,-100,100)
#@gui : Reverse Order = bool(0)
#@gui : note = note("\n\n<strong>Hint</strong>\n\nUse the <code>Input</code> <code>layers</code> menu to select the layer(s) you would like to blend.")
#@gui : note = note("\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_softlight : -100<=amount<=100,_reverse_order={ 0 | 1 }
#@cli : Blend image with itself or another image using softlight.
#@cli : Default values: 'amount=50' and 'reverse_order=0'.
afre_softlight : check "${1=50}>=-100 && isbool(${2=0}) && $1<=100"
if !$1 return fi
if $!>2 error[] "\n[afre]>_<) softlight: Select 1-2 images."
elif $!==2&&$2 rv e0=" in reverse order"
elif $!==1 . e1=" itself and"
elif w#0!=w||h#0!=h||s#0!=s error[] "\n[afre]>_<) softlight: Select images with the same dimensions." fi
if s#0!=1?s#0!=3 error[] "\n[afre]>_<) softlight: Select image(s) with 1 or 3 channels (I or RGB)." fi
if $1<0 negate. fi
e[] "[afre]^_^) softlight: Blend image$?"${e0}" using"${e1}" amount=$1."
iM={0,iM} / $iM f.. "begin(const A=0.5*abs($1)/100+0.5);
i#0^((A*2)^(A*(1-2*i#1)))" *.. $iM rm.
afre_softlight_preview :
afre_softlight $*
#@gui Dark Sky: afre_darksky, afre_darksky_preview(1)+
#@gui : note = note("<strong>Enhance landscape by darkening the sky. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2017-2020 Sep9.</strong>\n\n")
#@gui : Blend = choice("Softlight","Overlay")
#@gui : Contrast = int(0,-100,100)
#@gui : Smooth = choice(1,"Fast (approx.)","Slow (accurate)")
#@gui : Radius = int(0,0,3)
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.\n\nE.g. <code>Smooth(fast)</code> may appear to cause artifacts when in fact it does not.")
#@cli afre_darksky : blend={ 0=softlight | 1=overlay },-100<=_contrast<=100,_smooth_method={ 0=fast_approx | 1=slow_accurate },0<=_smooth_radius<=3
#@cli : Enhance landscape by darkening the sky.
#@cli : Default values: 'blend=0', 'contrast=0', 'smooth_method=0' and 'smooth_radius=0'.
afre_darksky : check "isbool(${1=0}) && ${2=0}>=-100 && isbool(${3=0}) && ${4=0}>=0 && $2<=100 && $4<=3"
b=${arg\ 1+$1,softlight,overlay} m=${arg\ 1+$3,fast,slow} rd={int($4)} e[] "[afre]^_^) darksky: Darken sky of image$? using blend="$b", contrast=$2, smooth_method="$m" and smooth_radius="$rd"."
repeat $! l[$>] if s<3||s>4 error[] "\n[afre]>_<) darksky: Select RGB images." fi done done
repeat $! l[$>] r0={[im,iM]} n 0,255 +channels 1 +channels.. 2 -[^0]
if $rd&&$3 afre_gui0_fast. $rd,1e0 elif $rd r1={[im,iM]}
median. {$rd*2+1} n. $r1 fi c. 0%,0 +. 127 afre_contrast. {$2}
+afre_y50.. 1 r1={[im,iM]} afre_orien[0] 1 if $1 blend. ..,$b rm..
else l[-2--1] rv afre_softlight 100 done fi n. $r1 *
n $r0 done done
afre_darksky_preview :
afre_darksky $*
#@gui _<b>Contours</b>
#---------------------
#@gui Edge : afre_edge, afre_edge_preview(0)+
#@gui : note = note("<strong>Compute edge. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2019-2020 May3.</strong>\n\n")
#@gui : Method = choice(0,"Gradient","Standard Deviation")
#@gui : Thinning = int(1,1,10)
#@gui : Recovery = float(1,0.5,4)
#@gui : Brightness = float(1,0.5,4)
#@gui : Details = float(1,0.5,4)
#@cli afre_edge : method={ 0=gradient | 1=standard_deviation },1<=thinning<=10,0.5<=_recovery<=4,0.5<=_brightness<=4,0.5<=_details<=4
#@cli : Compute edge of selected images.
#@cli : Default values: 'method=0', 'thinning=1', 'recovery=1', 'brightness=1' and 'details=1'.
afre_edge : check "${2=1}>=1 && ${3=1}>=0.5 && ${4=1}>=0.5 && ${5=1}>=0.5 &&
$2<=10 && $3<=4 && $4<=4 && $5<=4"
e[] "[afre]^_^) edge: Compute edge of image$? using method=$1, thinning=$2, recovery=$3, brightness=$4 and details=$5."
repeat $! l[$>] if $1 m "method : afre_sdpatch 1" else m "method : afre_gnorm" fi
method im,ic,iM={[im,ic,iM]} ^ {1/$5} n $im,$iM
repeat $2 +afre_box 1 method. r={[im,iM]} ^. $3 n. $r - done
c $ic,$iM ^ {1/$4} n 0,$iM
done done um method
afre_edge_preview :
afre_edge $*
#@gui _<b>Details</b>
#--------------------
#@gui Texture : afre_texture, afre_texture_preview(1)+
#@gui : note = note("<strong>Enhance texture with detail scales. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020 Aug10-18.</strong>\n\n")
#@gui : note = note("<strong>Detail Scale</strong>")
#@gui : Coarse = int(0,-100,100)
#@gui : Medium = int(0,-100,100)
#@gui : Fine = int(0,-100,100)
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_texture : -100<=coarse<=100,-100<=_medium<=100,-100<=_fine<=100
#@cli : Enhance texture with detail scales.
#@cli : Default values: 'coarse=0', 'medium=0' and 'fine=0'.
afre_texture : check "${1=0}>=-100 && ${2=0}>=-100 && ${3=0}>=-100
&& $1<=100 && $2<=100 && $3<=100"
if !$1&&!$2&&!$3 return fi e[] "[afre]^_^) texture: Enhance texture of image$? using coarse=$1, medium=$2 and fine=$3 detail scales."
repeat $! l[$>] r={[im,iM]} +afre_orien 1 +afre_y50.. 1 to_rgb.
afre_details. 4 w1={20^($1/130)} w2={20^($2/100)} w3={20^($3/140)}
if $1>0 *[3] $w1 elif $1<0 f[3] 0 median[2] {abs($1)/100*5} fi
if $2>0 *[4] $w2 elif $2<0 f[4] 0 median[2] {abs($2)/100*9} fi
if $3>0 *[5] $w3 elif $3<0 f[5] 0 median[2] {abs($3)/100*13} fi
+[2--1] c. $r *[^0] c. $r
+n.. 0,1 f. "begin(const M=0.5; const G=0.05);
ref(if(i<=M,M-M*(i/M)^G,-M+M*((1-i)/M)^G),A)" n. 0,1
blend_fade[0,1] . k[0]
n $r done done
afre_texture_preview :
afre_texture $*
#@gui _<b>Repair</b>
#-------------------
#@gui Clean Text : afre_cleantext, afre_cleantext_preview(0)+
#@gui : note = note("<strong>Clean scanned text. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2019 Jun8.</strong>\n\n")
#@gui : Clean = int(8,0,10)
#@gui : Range = float(1,0.2,1)
#@gui : Black = int(80,0,100)
#@gui : White = int(95,0,100)
#@cli afre_cleantext : 0<=clean<=10,0.2<=_range<=1,0<=_black<=100,0<=_white<=100
#@cli : Clean scanned text.
#@cli : Default values: 'clean=8', 'range=1', 'black=80' and 'white=95'.
afre_cleantext : check "${1=8}>=0 && ${2=1}>=0.2 && ${3=80}>=0 && ${4=95}>=0 &&
$1<=10 && $2<=1 && $3<=100 && $4<=100"
e[] "[afre]^_^) cleantext: Clean scanned text using clean=$1, range=$2, black=$3 and white=$4."
foreach {
c:=int(11-$1)*2+1 +dilate $c erode. $c - n 0,255
retinex , norm n 0,1 f 1-gauss(i,$2)
if $3!=$4 c $3%,$4% else c $3%,{$4+0.5}% fi n 0,255 round
}
afre_cleantext_preview :
afre_cleantext $*
#@gui Denoise Smooth : afre_denoisesmooth, afre_denoisesmooth_preview(1)+
#@gui : note = note("<strong>Denoise noisy image, or smooth low noise image. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020 Oct4.</strong>\n\n
#@gui : Filter is slow when the <i>radius</i> and <i>amount</i> are high. Start small and increase as appropriate.\n\n")
#@gui : Radius = int(3,1,10)
#@gui : Amount = int(10,1,1000)
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_denoisesmooth : radius>=1,_amount>=1
#@cli : Denoise noisy image, or smooth low noise image.
#@cli : \ \ Command is slow when the 'radius' and 'amount' are high.
#@cli : \ \ Start small and increase as appropriate.\n
#@cli : Default values: 'radius=3 and amount=25'.
afre_denoisesmooth : check "${1=3}>=1 && ${2=25}>=1"
e[] "[afre]^_^) denoisesmooth: Denoise noisy image, or smooth low noise image$? using radius=$1 and amount=$2.\n\ * Command is slow when the 'radius' and 'amount' are high.\n\ \ \ Start small and increase as appropriate."
repeat $! l[$>] r={[im,iM]}
. repeat $1 +normp. 1 afre_gui1_fast.. .,{$>+1} rm. done
repeat $2 afre_gui1_fast.. . done rm.
c $r done done
afre_denoisesmooth_preview :
afre_denoisesmooth $*
#@gui Denoise Smooth Alt: afre_denoisesmooth_alt, afre_denoisesmooth_alt_preview(1)+
#@gui : note = note("<strong>Denoise noisy image, or smooth low noise image. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020 Oct4-11.</strong>\n\n
#@gui : Filter is slow when the <i>radius</i> and <i>amount</i> are high. Start small and increase as appropriate.\n\n")
#@gui : Radius = int(3,1,10)
#@gui : Amount = int(10,1,1000)
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_denoisesmooth_alt : radius>=1,_amount>=1
#@cli : Denoise noisy image, or smooth low noise image.
#@cli : \ \ Command is slow when the 'radius' and 'amount' are high.
#@cli : \ \ Start small and increase as appropriate.\n
#@cli : Default values: 'radius=3 and amount=25'.
afre_denoisesmooth_alt : check "${1=3}>=1 && ${2=25}>=1"
e[] "[afre]^_^) denoisesmooth_alt: Denoise noisy image, or smooth low noise image$? using radius=$1 and amount=$2.\n\ * Command is slow when the 'radius' and 'amount' are high.\n\ \ \ Start small and increase as appropriate."
repeat $! l[$>] r={[im,iM]}
. repeat $1 +normp. 1 afre_gui1_fast.. .,{$>+1},,0 rm. done
repeat $2 afre_gui1_fast.. .,,,0 done rm.
c $r done done
afre_denoisesmooth_alt_preview :
afre_denoisesmooth_alt $*
#@gui _<b>Testing</b>
#--------------------
#@gui afre
#@gui Vigrect : afre_vigrect, afre_vigrect_preview(0)+
#@gui : note = note("<strong>Add rectangular vignette. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2017-2019.</strong>\n<hr />
#@gui : <strong>Instructions:</strong>\n\n
#@gui : * Negative strength brightens the periphery.\n\n")
#@gui : Size = int(50,1,100)
#@gui : Strength = int(75,-500,500)
#@gui : Blur = int(10,1,50)
#@gui : Left-Right = int(50,0,100)
#@gui : Up-Down = int(50,0,100)
#@cli afre_vigrect : 1<=size<=100,-500<=_strength<=500,1<=_blur<=50,0<=_position_x<=100,0<=_position_y<=100
#@cli : Add rectangular vignette of selected images. Negative strength will brighten periphery.
#@cli : Default values: 'size=50', 'strength=75', 'blur=10', 'position_x=50' and 'position_y=50'.
afre_vigrect : check "${1=50}>=1 && ${2=75}>=-500 && ${3=10}>=1 && ${4=50}>=0 && ${5=50}>=0 &&
$1<=100 && $2<=500 && $3<=50 && $4<=100 && $5<=100"
e[] "[afre]^_^) vigrect: Add rectangular vignette of image$? using size=$1, strength=$2, blur=$3, position_x=$4 and position_y=$5."
repeat $! l[$>] iM={iM} min={min(w,h)} s={$min/100*(50.5-$1/2)}
tx={$min/100*($4-50)} ty={$min/100*($5-50)}
+f x<$s+$tx||x>w-$s+$tx||y<$s+$ty||y>h-$s+$ty
distance. 0 b. {$min/100*$3} -. {im} *. {$2/100} -
c 0,$iM done done
afre_vigrect_preview :
afre_vigrect $*
#@gui Vigcirc : afre_vigcirc, afre_vigcirc_preview(0)+
#@gui : note = note("<strong>Add circular vignette. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2019.</strong>\n<hr />
#@gui : <strong>Instructions:</strong>\n\n
#@gui : * Negative strength brightens the periphery.\n\n")
#@gui : Size = int(90,50,150)
#@gui : Strength = int(75,-500,500)
#@gui : Left-Right = int(50,0,100)
#@gui : Up-Down = int(50,0,100)
#@cli afre_vigcirc : 50<=size<=150,-500<=_strength<=500,0<=_position_x<=100,0<=_position_y<=100
#@cli : Add circular vignette of selected images. Negative strength will brighten periphery.
#@cli : Default values: 'size=90', 'strength=75', 'position_x=50' and 'position_y=50'.
afre_vigcirc : check "${1=90}>=50 && ${2=75}>=-500 && ${3=50}>=0 && ${4=50}>=0 &&
$1<=150 && $2<=500 && $3<=100 && $4<=100"
e[] "[afre]^_^) vigcirc: Add circular vignette of image$? using size=$1, strength=$2, position_x=$3 and position_y=$4."
repeat $! l[$>] iM={iM} min={min(w,h)}
+f 1 circle. $3%,$4%,{($min/2-2)/100*$1}
distance. 0 b. 1 -. {im} *. {$2/100} -
c 0,$iM done done
afre_vigcirc_preview :
afre_vigcirc $*
#@gui Sharpen FFT : afre_sharpenfft, afre_sharpenfft_preview(0)+
#@gui : note = note("<strong>Sharpen with Fourier transform. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2019-2020 Jan18.</strong>\n\n")
#@gui : Strength = int(15,1,50)
#@gui : Size = int(1,1,10)
#@cli afre_sharpenfft : 1<=strength<=50,1<=_size<=10
#@cli : Sharpen selected images with Fourier transform.
#@cli : Default values: 'strength=15' and 'size=1'.
afre_sharpenfft : check "${1=15}>=1 && ${2=1}>=1 && $1<=50 && $2<=10"
e[] "[afre]^_^) sharpenfft: Sharpen image$? with Fourier transform using strength=$1 and size=$2."
repeat $! l[$>] iM={iM} +periodize_poisson -.. . l. +l
fftpolar +gaussian.. {100/$2}% negate. *[^1] ifftpolar * $1
done + c 0,$iM done + c 0,$iM done done
afre_sharpenfft_preview :
afre_sharpenfft $*
#@gui Contrast FFT : afre_contrastfft, afre_contrastfft_preview(0)+
#@gui : note = note("<strong>Enhance contrast with Fourier transform. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2019.</strong>\n\n")
#@gui : Strength = int(75,1,100)
#@gui : Amount = int(50,0,100)
#@gui : Iterations = int(1,1,10)
#@cli afre_contrastfft : 1<=strength<=100,0<=_amount<=100,1<=_iterations<=10
#@cli : Enhance contrast for selected images with Fourier transform.
#@cli : Default values: 'strength=75', 'amount=50' and 'iterations=1'.
afre_contrastfft : check "${1=75}>=1 && ${2=50}>=0 && ${3=1}>=1 &&
$1<=100 && $2<=100 && $3<=10"
e[] "[afre]^_^) contrastfft: Enhance contrast for image$? with Fourier transform using strength=$1, amount=$2 and iterations=$3."
repeat $! l[$>] repeat $3
+fftpolar ^.. {($1-0.5)/100} ifftpolar[^0] afre_softlight $2
done done done
afre_contrastfft_preview :
afre_contrastfft $*
#@gui Gamify : fx_gamify, fx_gamify_preview(0)+
#@gui : Lightness = int(50,50,60)
#@gui : Chroma = float(2,1.1,5)
#@gui : sep = separator(), Normalize = bool(1)
#@gui : Contrast = float(1,1,1.5)
#@gui : sep = separator(), Preview type = choice("Full","Forward horizontal","Forward vertical","Backward horizontal","Backward vertical","Duplicate top","Duplicate left","Duplicate bottom","Duplicate right","Duplicate horizontal","Duplicate vertical","Checkered","Checkered inverse")
#@gui : sep = separator(), note = note("Filter by <i>afre</i>. Latest update: <i>2017-01-05</i>.")
fx_gamify :
if $3 n 0,255 fi
afre_jchz
s c n... $1,100 *.. $2 a c
afre_ijchz
split_details 2
l.. adjust_colors 0,{-$4/1.5*20} done
*. $4
+ c 0,255
fx_gamify_preview :
gui_split_preview "fx_gamify $*",$-1
#@gui Hessian Norm : fx_hnorm, fx_hnorm_preview(0)+
#@gui : Strength = float(1,0.5,1.5)
#@gui : Contrast = int(50,1,99)
#@gui : Invert = bool(0)
#@gui : sep = separator(), note = note("Filter by <i><a href="https://discuss.pixls.us/u/afre">afre</a></i>. Latest update: <i>2018-05-09</i>.")
fx_hnorm :
afre_hnorm ^ $1
c 0,$2%
if $3 negate fi
n 0,255
fx_hnorm_preview :
fx_hnorm $*
#@gui Gleam : afre_gleam, afre_gleam_preview(0)+
#@gui : note = note("<strong>Add gleam effect.</strong> Hint: Try different combinations of smooth and threshold. <strong>Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2019.</strong>\n\n
#@gui : * Experimental filter.\n\n")
#@gui : Smooth = int(3,2,100)
#@gui : Threshold = int(50,10,90)
#@cli afre_gleam : 2<=smooth<=100,10<=_threshold<=90
#@cli : Add gleam effect to selected images.
#@cli : Default values: 'smooth=3' and 'threshold=50'.
afre_gleam : check "${1=3}>=2 && ${2=50}>=10 && $1<=100 && $2<=90"
e[] "[afre]^_^) gleam: Add gleam effect to image$? using smooth=$1 and threshold=$2."
repeat $! l[$>]
+afre_y50 1 >. {100-$2}% b. $1% sqrt. +afre_softlight 100 rm.. max
done done
afre_gleam_preview :
afre_gleam $*
#@gui Half & half : afre_halfhalf, afre_halfhalf_preview(0)+
#@gui : note = note("<strong>Stitch left and right halves of 2 images, respectively. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020-Feb21.</strong>\n\n
#@gui : * Remember to set <strong>Input layers</strong>.\n
#@gui : * <strong>Match size</strong> doesn't work in Krita.\n\n")
#@gui : Match size = bool(0)
#@cli afre_halfhalf : match_size={ 0 | 1 }
#@cli : Stitch left and right halves of 2 images, respectively.
#@cli : Default value: 'match_size=0'.
afre_halfhalf : check isbool(${1=0})
if $!!=2 error[] "\n[afre]>_<) halfhalf: Please select 2 images." fi
e[] "[afre]^_^) halfhalf: Stitch left and right halves of 2 images using match_size=$1."
+f[{w#0>w#1?0:1}] 0 mv. 0 s[1,2] x,2 rm[2,3]
if w#1<w#2&&$1 rs.. {w#2}
elif $1 rs. {w#1} elif w#1<w#2 r.. .,0,0,1,0.5 fi
j[0] [1],0,0.5~ j[0] [2],50%,0.5~ k[0]
afre_halfhalf_preview :
afre_halfhalf $*
#
#@gui Portrait Montage : afre_portraitmontage, afre_portraitmontage_preview(1)+
#@gui : note = note("<strong>Generate portrait montage with resizing. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020-2021 Apr18.</strong>\n\n
#@gui : - Set <strong>Input layers</strong> to <strong>All</strong>.\n
#@gui : - Centre subjects for best results.\n\n")
#@gui : Size = choice(1,"Small","Large")
#@gui : Spacing = int(1,1,20)
#@gui : Matte Shape = choice("None","Circle","Polygon","Star")
#@gui : Matte Colour = color(230,255,230)
#@cli afre_portraitmontage : size={ 0=small | 1=large },_spacing>=1,_matte_shape={ 0=none | 1=circle | 2=polygon | 3=star },0<=_matte_colour={[R,G,B]}<=255
#@cli : Generate portrait montage with resizing.
#@cli : Default values: 'size=1','spacing=1', 'matte_shape=0', 'matte_colour=230,255,230'.\n
#@cli : \- Centre subjects for best results.
afre_portraitmontage : check "isbool(${1=1}) && ${2=1}>=1 && inrange(${3=0},0,3) && inrange(${4=230},0,255) && inrange(${5=255},0,255) && inrange(${6=230},0,255)"
if $3==0 s=none elif $3<2 s=circle elif $3<3 s=polygon t=8
elif $3<4 s=star t=24,0.9 fi
e[] "[afre]^_^) portraitmontage: Generate portrait montage of resized image$? using size=$1, spacing=$2, matte_shape="$s" and matte_colour=(${4-6})."
rr2d {`$1?'${-max_w},${-max_h},':'${-min_w},${-min_h},1'`},2 to_rgb
repeat $! l[$>] if $3 +f. -1 shape_$s {min(w,h)},$t to_rgb.
ri. ..,0,0,0.5,0.5 negate. j... ..,0,0,0,0,1,. k... autocrop fi
+ 1 done done f=min(${-max_w},${-max_h}) frame xy,{$2*$f*0.01},0
append_tiles 0 frame xy,{$2*$f*0.01},0 - 1 replace_color 0,0,-1,-1,-1,$4,$5,$6
afre_portraitmontage_preview :
afre_portraitmontage $*
#
#@gui Montage X : afre_montagex, afre_montagex_preview(1)+
#@gui : note = note("<strong>Generate montage without resizing. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2021 Apr16.</strong>\n\n
#@gui : - Set <strong>Input layers</strong> to <strong>All</strong>.\n\n")
#@gui : Max Per Row = int(5,1,25)
#@gui : Spacing = int(1,0,10)
#@gui : Matte Colour = color(230,255,230)
#@cli afre_montagex : max_per_row>=1,_spacing>=0,0<=_matte_colour={[R,G,B]}<=255
#@cli : Generate montage without resizing.
#@cli : Default values: 'max_per_row=5', 'spacing=1', 'matte_colour=230,255,230'.
afre_montagex : check "${1=5}>=1 && ${2=1}>=0 && inrange(${3=230},0,255) && inrange(${4=255},0,255) && inrange(${5=230},0,255)"
e[] "[afre]^_^) montagex: Generate montage without resizing image$? using max_per_row=$1, spacing=$2 and matte_colour=(${3-5})."
to_colormode 0 a=$1 b,c=int($!/$a),{$!%$a}
+ 1 frame xy,{$2*min(${-max_w},${-max_h})*0.01},0
repeat $b a[{$a*$<}-{$a*$<+($a-1)}] x,0.5 done
if $c a[-$c--1] x,0.5 fi a y,0.5
- 1 replace_color 0,0,-1,-1,-1,$3,$4,$5
afre_montagex_preview :
afre_montagex $*
#@gui Details : afre_details, afre_details_preview(2)+
#@gui : note = note("<strong>Split image into detail scales. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2020 Aug10.</strong>\n\n")
#@gui : Scales = int(2,2,6)
#@gui : note = note("\n\n<strong>Instructions</strong>\n\nDrag preview image to examine different regions of interest.")
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_details : n_scales>=2
#@cli : Split image into detail scales.
#@cli : Default value: 'n_scales=2'.
afre_details : check ${1=2}>=2
e[] "[afre]^_^) details: Split image$? into $1 detail scales."
repeat $! l[$<]
repeat $1-1 +afre_gui0_fast. 1,1e10 -.. . rv[-2,-1] done
done done
afre_details_preview :
afre_details $*
n 0,255 append_tiles 0
#@gui Query Primary : afre_queryprimary, afre_queryprimary_preview(1)+
#@gui : note = note("<strong>Query pixel minima, maxima or medians. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2021 Jul14.</strong>\n\n")
#@gui : Mode = choice(1,"Minima","Maxima","Medians")
#@gui : Skip Last Channel = bool(1)
#@gui : note = note("* Disable if image does not have an alpha or transparency channel.\n\n")
#@cli afre_queryprimary : mode={ 0=min | 1=max | 2=med },_skip_last_channel={ 0 | 1 }
#@cli : Query pixel minima, maxima or medians of selected images.
#@cli : Default values: 'mode=1' and 'skip_last_channel=0'.
afre_queryprimary : check "inrange(${1=1},0,2) && isbool(${2=0})"
mode,s={`$1>1?'med,medians':$1>0?'max,maxima':'min,minima'`} if $2 skip=", skipping the last channel" fi e[] "[afre]^_^) queryprimary: Query pixel "$s" of image$?"$skip"."
repeat $! l[$>] if $2 sh 0,{s-2} fi l.
+s c $mode[^0] +== rm.. *
done if $2 rm. fi done done
afre_queryprimary_preview :
afre_queryprimary $*
#@gui Denoise [afre] : afre_denoise, afre_denoise_preview(1)+
#@gui : note = note("<strong>Denoise. Filter by <a href="https://discuss.pixls.us/u/afre">afre</a> 2022 Feb9-17.</strong>\n\n")
#@gui : Radius = int(1,1,10)
#@gui : note = note("\n\n<strong>Note</strong>\n\nG'MIC's preview is inaccurate. Apply the filter and review the results in your host editor.")
#@cli afre_denoise : radius>=1
#@cli : Denoise selected images.
#@cli : Default value: 'radius=1'.
afre_denoise : check ${1=1}>=1
e[] "[afre]^_^) denoise: Denoise image$? using radius=$1."
repeat $! l[$>] r={[im,iM]}
. repeat 4
. repeat $1 afre_gui0_fast. {$>+1},1e0 done n. {0,[im,iM]}
+laplacian.. abs. n. 0,1
^. {.2+.2*($>+1)} blend_fade[1,2] . k[0,1]
done
afre_gui1_fast. ..,1,0 rm..
c $r done done
afre_denoise_preview :
afre_denoise $*
#@cli ::Features
#---------------
#
#@cli afre_norm : _keep_channels={ 0 | 1 }
#@cli : Compute normalized norms of selected images.
#@cli : Default value: 'keep_channels=1'.
afre_norm : skip ${1=1}
l[] if isnum("$1") k={$1?1} elif ["'$1'"]!=',' noarg k=1 fi done
s1="s" e[] "[afre]^_^) norm: Compute normalized norm"${s{$!>1}}" of image$? using keep_channels="$k"."
repeat $! l[$>] s={s} if $s>1
sqr s c + sqrt / {sqrt($s)} if $k r 100%,100%,100%,$s fi
fi done done
#@cli afre_y50 : colour_space={ 0=Rec.709 | 1=Rec.2020 }
#@cli : Compute luminance (D50) of selected images.
#@cli : Default value: 'colour_space=1'.
afre_y50 : check isbool(${1=1})
if $1 Y="Rec.2020" else Y="Rec.709" fi
e[] "[afre]^_^) y50: Compute luminance (D50 "$Y") of image$?."
repeat $! l[$>]
if s==3 sh 0 sh[0] 1 sh[0] 2
if $1 *[1] 0.2790177 *[2] 0.6753402 *[3] 0.0456377 # Rec.2020
else *[1] 0.22248840 *[2] 0.71690369 *[3] 0.06060791 # Rec.709
fi +[1-3] rm[1] channels 0
elif s!=1 r={[im,iM]} norm n $r fi
done done
#
#@cli afre_orien : _mode={ 0=norm | 1=Rec.709 | 2=Rec.2020 }
#@cli : Compute orientation of selected images.
#@cli : Default value: 'mode=0'.
afre_orien : skip ${1=0}
l[] if isnum("$1") m={inrange($1,0,2)?int($1):0} elif ["'$1'"]!=',' noarg m=0 fi done
s0,s$m="norm" s1="luminance Rec.709 D50" s2="luminance Rec.2020 D50" e[] "[afre]^_^) orien: Compute orientation of image$? using "${s$m}"."
repeat $! if $m +afre_y50[$>] {$m-1} else +afre_norm[$>] fi
replace. 0,1 /[$>,-1] done
#
#@cli afre_gradientnorm : _keep_channels={ 0 | 1 },_keep_range={ 0 | 1 }
#@cli : Compute normalized gradient norms of selected images.
#@cli : Default values: 'keep_channels=1' and 'keep_range=0'.
afre_gradientnorm : skip ${1=1},${2=0}
l[] if isnum("$1")&&isnum("$2") k={$1?1} r={$2?1} elif ["'$1'"]!=',' noarg k,r=1,0 fi done
s1="s" e[] "[afre]^_^) gradientnorm: Compute normalized gradient norm"${s{$!>1}}" of image$? using keep_channels="$k" and keep_range="$r"."
repeat $! l[$>] iM,s={[iM,s]}
g sqr s c + sqrt / {iM/($r?$iM:1)} if $k r 100%,100%,100%,$s fi
done done
#
#@cli afre_hessiannorm : _keep_channels={ 0 | 1 },_keep_range={ 0 | 1 }
#@cli : Compute normalized hessian norms of selected images.
#@cli : Default values: 'keep_channels=1' and 'keep_range=0'.
afre_hessiannorm : skip ${1=1},${2=0}
l[] if isnum("$1")&&isnum("$2") k={$1?1} r={$2?1} elif ["'$1'"]!=',' noarg k,r=1,0 fi done
s1="s" e[] "[afre]^_^) hessiannorm: Compute normalized hessian norm"${s{$!>1}}" of image$? using keep_channels="$k" and keep_range="$r"."
repeat $! l[$>] iM,s={[iM,s]}
hessian sqr s c + sqrt / {iM/($r?$iM:1)} if $k r 100%,100%,100%,$s fi
done done
#@cli afre_gnorm
#@cli : Compute custom gradient norm of selected images.
afre_gnorm :
e[] "[afre]^_^) gnorm: Compute custom gradient norm of image$?."
repeat $! l[$>] nm={b}
+gradient_norm r={[im,iM]} +afre_hnorm[0] afre_gui1_fast[2] [0] rm[0]
c. 20%,100% n 0,1 *. 0.75 max n $r
nm $nm done done
#@cli afre_hnorm
#@cli : Compute Hessian norm of selected images.
afre_hnorm :
e[] "[afre]^_^) hnorm: Compute Hessian norm of image$?."
repeat $! l[$>] hessian sqr s c + sqrt done done
#@cli afre_sdpatch : radius>=1
#@cli : Compute local standard deviation of selected images.
#@cli : Default value: 'radius=1'.
afre_sdpatch : skip ${1=1}
rd={int($1)} e[] "[afre]^_^) sdpatch: Compute local standard deviation of image$? using radius="$rd"."
repeat $! l[$>] +gradient_norm r={[im,iM]} rm.
+sqr afre_box_fast $rd sqr.. rv - max 0 sqrt
n $r done done
#@cli afre_jchz
#@cli : Convert selected images from RGB to JzCzhz.
afre_jchz :
e[] "[afre]^_^) jchz: Convert image$? from RGB to JzCzhz."
rgb2jzazbz 0 r 100%,100%,100%,3 repeat $! l[$>]
s c complex2polar[-2,-1] a c
done done
#@cli afre_ijchz
#@cli : Convert selected images from JzCzhz to RGB.
afre_ijchz :
e[] "[afre]^_^) ijchz: Convert image$? from JzCzhz to RGB."
r 100%,100%,100%,3 repeat $! l[$>]
s c polar2complex[-2,-1] a c
done done jzazbz2rgb 0
#@cli ::Filtering
#----------------
#@cli afre_conv : [kernel]
#@cli : Convolve selected images with a custom kernel.
afre_conv : check ${is_image_arg\ $1}
repeat $! pass$1 0
e[] "[afre]^_^) conv: Convolve image$? using ["${"-pass$1 -1"}"]."
l[$>,-1] +f[0] 1 mv. 1
+rows. 50% columns.. 50% +convolve[1] [2] convolve[4] [3]
convolve[0] [2] convolve[0] [3] k[0,4] / done
done
#@cli afre_box : radius>=1,_gaussian={ 0 | 1 }
#@cli : Blur selected images with a soft box filter.
#@cli : Default values: 'radius=1' and 'gaussian=1'.
afre_box : check "${1=1}>=1 && isbool(${2=1})"
rd={int($1)} if $2 g="soft box" else g="box" fi w={$rd*2+1}
e[] "[afre]^_^) box: Blur image$? with a "${w}"x"${w}" "$g" filter."
repeat $! l[$>]
$w,$w f. 1 if $2 gaussian. {100/3}% fi afre_conv.. . rm.
done done
#@cli afre_box_fast : radius>=1
#@cli : Blur selected images with a soft box filter. Faster version.
#@cli : Default value: 'radius=1'.
afre_box_fast : check ${1=1}>=1
rd={int($1)} w={$rd*2+1} e[] "[afre]^_^) box: Blur image$? with a "${w}"x"${w}" soft box filter."
repeat $! l[$>] boxfilter $w,0,1,3 done done
#@cli afre_gui0 : radius>=1,_smoothing>=0,0<=_structure<=10
#@cli : Blur selected images with a custom self-guided filter.
#@cli : Default values: 'radius=1', 'smoothing=1e-2' and 'structure=10'.\n
#@cli : - Use orders of magnitude for 'smoothing'.
#@cli : - Reduce 'structure' to soften edges and artifacts.
#@cli : * Improvement of "K He, J Sun, X Tang. Guided image filtering. TPAMI, 35(6):1397-1409, 2013".
afre_gui0 : check "${1=1}>=1 && ${2=1e-2}>=0 && ${3=10}>=0 && $3<=10"
rd={int($1)} kwh={sqr($rd*2+1)} str={1e{-$3+2}}
e[] "[afre]^_^) gui0: Blur image$? with a custom self-guided filter using radius="$rd", smoothing=$2 and structure=$3."
repeat $! l[$>] nm={b} iM={iM} / $iM
. +sqr. afre_box[^0] $rd nm I,mI,mII
+sqr[mI] *. -1 +. [mII] rm[mII] => vI
+l[vI] abs s c + done *. $kwh +. $str ^. -1 *. $str => W
++[vI] {vI,$2*ia+1e-6} /[vI] . rm. =>[vI] a
+*[a] [mI] *. -1 +. [mI] rm[mI] => b
*[a,b] [W] afre_box[^0] $rd =>[^0] ma,mW,mb
*[I] [ma] +[I] [mb] /[I] [mW] k[I]
* $iM nm $nm done done
#@cli afre_gui1 : [guide],_radius>=1,_smoothing>=0,0<=_structure<=10
#@cli : Blur selected images with a custom guided filter.
#@cli : Default values: 'radius=1', 'smoothing=0' and 'structure=5'.\n
#@cli : - Use orders of magnitude for 'smoothing'.
#@cli : - Reduce 'structure' to soften edges and artifacts.
#@cli : * Improvement of "K He, J Sun, X Tang. Guided image filtering. TPAMI, 35(6):1397-1409, 2013".
afre_gui1 : check ${is_image_arg\ $1}" && ${2=1}>=1 && ${3=0}>=0 && ${4=5}>=0 && $4<=10"
rd={int($2)} kwh={sqr($rd*2+1)} str={1e{-$4+2}}
e[] "[afre]^_^) gui1: Blur image$? with a custom guided filter using guide=["${"-pass$1 -1"}"], radius="$rd", smoothing=$3 and structure=$4."
pass$1 0 to_colormode 0
repeat $! if $>!=$! nm$>={$>,b} fi iM$>={$>,iM} /[$>] ${iM$>} done
repeat $!-1 l[$>,-1]
if w#0!=w#1||h#0!=h#1||d#0!=d#1 error[] "\n[afre]>_<) gui1: Image$? and guide ["${"-pass$1 -1"}"] must have the same width, height and depth." fi
. rv +*[1] [2] +sqr[1] afre_box[^0] $rd nm I,mI,mp,mIp,mII
+*[mI] [mp] *. -1 +. [mIp] rm[mIp] => cIp
+sqr[mI] *. -1 +. [mII] rm[mII] => vI
+normp[vI] 1 *. $kwh +. $str ^. -1 *. $str => W
++[vI] {vI,$3*ia+1e-6} /[cIp] . rm[vI,-1] =>[cIp] a
+*[a] [mI] *. -1 +. [mp] rm[mI,mp] => b
*[a,b] [W] afre_box[^0] $rd =>[^0] ma,mW,mb
*[ma] [I] +[ma] [mb] /[ma] [mW] k[I,ma]
=>[ma] ${nm$>} rv
done done rm.
repeat $! *[$>] ${iM$>} done
#@cli afre_gui0c : [kernel],_smoothing>=0,0<=_structure<=10
#@cli : Convolve selected images with a custom self-guided filter.
#@cli : Default values: 'smoothing=1e-2' and 'structure=10'.\n
#@cli : - Use orders of magnitude for 'smoothing'.
#@cli : - Reduce 'structure' to soften edges and artifacts.
#@cli : * Improvement of "K He, J Sun, X Tang. Guided image filtering. TPAMI, 35(6):1397-1409, 2013".
afre_gui0c : check ${is_image_arg\ $1}" && ${2=1e-2}>=0 && ${3=10}>=0 && $3<=10"
e[] "[afre]^_^) gui0c: Convolve image$? with a custom self-guided filter using kernel=["${"-pass$1 -1"}"], smoothing=$2 and structure=$3."
str={1e{-$3+2}}
repeat $! pass$1 0 kwh={wh} l[$>,-1] nm={0,b} iM={0,iM} /[0] $iM
[0] +sqr. afre_conv[^0-1] [1] nm I,k,mI,mII
+sqr[mI] *. -1 +. [mII] rm[mII] => vI
+l[vI] abs s c + done *. $kwh +. $str ^. -1 *. $str => W
++[vI] {vI,$2*ia+1e-6} /[vI] . rm. =>[vI] a
+*[a] [mI] *. -1 +. [mI] rm[mI] => b
*[a,b] [W] afre_conv[^0-1] [k] =>[^0-1] ma,mW,mb
*[I] [ma] +[I] [mb] /[I] [mW] k[I]
* $iM nm $nm done done
#@cli afre_gui1c : [guide],[kernel],_smoothing>=0,0<=_structure<=10
#@cli : Convolve selected images with a custom guided filter.
#@cli : Default values: 'smoothing=0' and 'structure=5'.\n
#@cli : - Use orders of magnitude for 'smoothing'.
#@cli : - Reduce 'structure' to soften edges and artifacts.
#@cli : * Improvement of "K He, J Sun, X Tang. Guided image filtering. TPAMI, 35(6):1397-1409, 2013".
afre_gui1c : check ${is_image_arg\ $1}" && "${is_image_arg\ $2}" && ${3=0}>=0 && ${4=5}>=0 && $4<=10"
e[] "[afre]^_^) gui1c: Convolve image$? with a custom guided filter using guide=["${"-pass$1 -1"}"], kernel=["${"-pass$2 -1"}"], smoothing=$3 and structure=$4."
pass$1 0 to_colormode 0 str={1e{-$4+2}}
repeat $! if $>!=$! nm$>={$>,b} fi iM$>={$>,iM} /[$>] ${iM$>} done
repeat $!-1 pass$2 0 kwh={wh} l[$>,-2--1]
if w#0!=w#1||h#0!=h#1||d#0!=d#1 error[] "\n[afre]>_<) gui1c: Image$? and guide ["${"-pass$1 -1"}"] must have the same width, height and depth." fi
[1] rv +*[2] [3] +sqr[2] afre_conv[^0-1] [1] nm I,k,mI,mp,mIp,mII
+*[mI] [mp] *. -1 +. [mIp] rm[mIp] => cIp
+sqr[mI] *. -1 +. [mII] rm[mII] => vI
+normp[vI] 1 *. $kwh +. $str ^. -1 *. $str => W
++[vI] {vI,$3*ia+1e-6} /[cIp] . rm[vI,-1] =>[cIp] a
+*[a] [mI] *. -1 +. [mp] rm[mI,mp] => b
*[a,b] [W] afre_conv[^0-1] [k] =>[^0-1] ma,mW,mb
*[ma] [I] +[ma] [mb] /[ma] [mW] k[I,ma]
=>[ma] ${nm$>} rv
done done rm.
repeat $! *[$>] ${iM$>} done
#@cli afre_gui0_fast : radius>=1,_smoothing>=0,0<=_structure<=10
#@cli : Blur selected images with a custom self-guided filter. Faster version.
#@cli : Default values: 'radius=1', 'smoothing=1e-2' and 'structure=10'.\n
#@cli : - Use orders of magnitude for 'smoothing'.
#@cli : - Reduce 'structure' to soften edges and artifacts.
#@cli : * Improvement of "K He, J Sun, X Tang. Guided image filtering. TPAMI, 35(6):1397-1409, 2013".
afre_gui0_fast : check "${1=1}>=1 && ${2=1e-2}>=0 && ${3=10}>=0 && $3<=10"
rd={int($1)} kwh={sqr($rd*2+1)} str={1e{-$3+2}}
e[] "[afre]^_^) gui0_fast: Blur image$? with a custom self-guided filter using radius="$rd", smoothing=$2 and structure=$3. Faster version."
repeat $! l[$>] nm={b} iM={iM} / $iM
. +sqr. afre_box_fast[^0] $rd nm I,mI,mII
+sqr[mI] *. -1 +. [mII] rm[mII] => vI
+l[vI] abs s c + done *. $kwh +. $str ^. -1 *. $str => W
++[vI] {vI,$2*ia+1e-6} /[vI] . rm. =>[vI] a
+*[a] [mI] *. -1 +. [mI] rm[mI] => b
*[a,b] [W] afre_box_fast[^0] $rd =>[^0] ma,mW,mb
*[I] [ma] +[I] [mb] /[I] [mW] k[I]
* $iM nm $nm done done
#@cli afre_gui1_fast : [guide],_radius>=1,_smoothing>=0,0<=_structure<=10
#@cli : Blur selected images with a custom guided filter. Faster version.
#@cli : Default values: 'radius=1', 'smoothing=0' and 'structure=5'.\n
#@cli : - Use orders of magnitude for 'smoothing'.
#@cli : - Reduce 'structure' to soften edges and artifacts.
#@cli : * Improvement of "K He, J Sun, X Tang. Guided image filtering. TPAMI, 35(6):1397-1409, 2013".
afre_gui1_fast : check ${is_image_arg\ $1}" && ${2=1}>=1 && ${3=0}>=0 && ${4=5}>=0 && $4<=10"
rd={int($2)} kwh={sqr($rd*2+1)} str={1e{-$4+2}}
e[] "[afre]^_^) gui1_fast: Blur image$? with a custom guided filter using guide=["${"-pass$1 -1"}"], radius="$rd", smoothing=$3 and structure=$4. Faster version."
pass$1 0 to_colormode 0
repeat $! if $>!=$! nm$>={$>,b} fi iM$>={$>,iM} /[$>] ${iM$>} done
repeat $!-1 l[$>,-1]
if w#0!=w#1||h#0!=h#1||d#0!=d#1 error[] "\n[afre]>_<) gui1_fast: Image$? and guide ["${"-pass$1 -1"}"] must have the same width, height and depth." fi
. rv +*[1] [2] +sqr[1] afre_box_fast[^0] $rd nm I,mI,mp,mIp,mII
+*[mI] [mp] *. -1 +. [mIp] rm[mIp] => cIp
+sqr[mI] *. -1 +. [mII] rm[mII] => vI
+normp[vI] 1 *. $kwh +. $str ^. -1 *. $str => W
++[vI] {vI,$3*ia+1e-6} /[cIp] . rm[vI,-1] =>[cIp] a
+*[a] [mI] *. -1 +. [mp] rm[mI,mp] => b
*[a,b] [W] afre_box_fast[^0] $rd =>[^0] ma,mW,mb
*[ma] [I] +[ma] [mb] /[ma] [mW] k[I,ma]
=>[ma] ${nm$>} rv
done done rm.
repeat $! *[$>] ${iM$>} done
#
#@cli afre_dilateg : [mask],size>=2
#@cli : Compute geodesic dilation of selected images.
#@cli : Default value: 'size=3'.
afre_dilateg : check ${is_image_arg\ $1}" && ${2=3}>=2"
e[] "[afre]^_^) dilateg: Compute geodesic dilation of image$? using mask=["${"pass$1\ -1"}"] and size=$2."
pass$1 0 repeat $!-1 l[$>,-1] repeat 1000
ia={ia} +dilate. $2 +min[0,-1] rm[2--2] if $ia==ia break fi
done rm[0] rv done done rm.
#
#@cli afre_erodeg : [mask],size>=2
#@cli : Compute geodesic erosion of selected images.
#@cli : Default value: 'size=3'.
afre_erodeg : check ${is_image_arg\ $1}" && ${2=3}>=2"
e[] "[afre]^_^) erodeg: Compute geodesic erosion of image$? using mask=["${"pass$1\ -1"}"] and size=$2."
pass$1 0 repeat $!-1 l[$>,-1] repeat 1000
ia={ia} +erode. $2 +max[0,-1] rm[2--2] if $ia==ia break fi
done rm[0] rv done done rm.
#
#@cli afre_openg : size>=2
#@cli : Compute geodesic opening of selected images.
#@cli : Default value: 'size=3'.
afre_openg : check ${1=3}>=2
e[] "[afre]^_^) openg: Compute geodesic opening of image$? using size=$1."
+erode $1 repeat 1000
ia={ia} dilate. $1 +min rm.. if $ia==ia break fi
done k.
#
#@cli afre_closeg : size>=2
#@cli : Compute geodesic closing of selected images.
#@cli : Default value: 'size=3'.
afre_closeg : check ${1=3}>=2
e[] "[afre]^_^) closeg: Compute geodesic closing of image$? using size=$1."
+dilate $1 repeat 1000
ia={ia} erode. $1 +max rm.. if $ia==ia break fi
done k.
#@cli ::Utilities
#----------------
#
#@cli afre_maxmin
#@cli : Return the maximum of channel minimums.
afre_maxmin :
repeat s sh. $> im.={`$>?','`}{im} rm. done u {max($im)}
#
#@cli afre_minmax
#@cli : Return the minimum of channel maximums.
afre_minmax :
repeat s sh. $> iM.={`$>?','`}{iM} rm. done u {min($iM)}
#@cli afre_compare : crop={ 1=once | 2=twice },_layout={ x | y | z | tile },_normalize={ 0 | 1 }
#@cli : Crop then compare selected images interactively.
#@cli : Default values: 'crop=1', 'layout=x' and 'normalize=1'.\n
#@cli : 1. Interactive Crop
#@cli : \ - Click and drag to select crop.
#@cli : \ - Use scroll button to browse images.
#@cli : \ * Click once or close window to exit command.\n
#@cli : 2. Comparison Window
#@cli : \ - Regular display behaviour.
#@cli : \ - Close window to make another comparison.
afre_compare : check "${1=1}>=1 && $1<=2 && (s='${2=x}';s=='x'||s=='y'||s=='z'||s=='tile') && "isbool(${3=1})
n={$!-1} if $n<1 error[] "\n[afre]>_<) compare: Please select 2 or more images." fi
repeat $!-1 if w#0!=w#{$>+1}||h#0!=h#{$>+1} error[] "\n[afre]>_<) compare: Please select images with the same width and height." fi done
repeat $! if d#$>!=1 e[] "[afre]>_<) compare: *Slices in image ["$>"] will be considered separate images." fi done
e[] "[afre]^_^) compare: Crop then compare image$? interactively."
e[] "\n 1. Interactive Crop\n
\ - Click and drag to select crop.\n
\ - Use scroll button to browse images.\n
\ * Click once or close window to exit command.\n\n
2. Comparison Window\n
\ - Regular display behaviour.\n
\ - Close window to make another comparison.\n"
to_colormode 0 if $3 n 0,255 fi
do +l =>[0] Compare
a z repeat $1 xz b={w} if $b==1 break fi done
b={w} if '$2'!='z' s z if '$2'!='tile' a $2 else append_tiles 0 fi fi
if $b==1 break else v - d v + fi
done k[0-$n] while $b!=1 v - d[]
#@cli afre_reorder : order,_order2,...
#@cli : Reposition images according to a list of values of any length.
afre_reorder :
e[] "[afre]^_^) reorder: Reposition images according to list $*."
z 0,0,0,100%,100%,1 mirror z f z==0?1e6:i
l ($*) onfail error[] "\n[afre]>_<) reorder: Parameter must be a list of numbers; e.g. 0,2,4,1,3." done
repeat min(w,$!-1) f[$>] z==0?{i($>)}:i done
sort_list[0-{min(w,$!-1)-1}] + rm. z 0,0,1,100%,100%,1
#@cli afre_log2 : direction={ 0=inverse | 1=forward }
#@cli : Transform selected images into or back from log2 space.
#@cli : Default value: 'direction=1'.
afre_log2 : check isbool(${1=1})
if $1 d="into" else d="back from" fi
e[] "[afre]^_^) log2: Transform image$? "$d" log2 space."
if $1 + 1 log2 else f exp(i*log(2)) - 1 fi
#@cli afre_section : 1<=xy_sections<=50
#@cli : Section selected images into numbered regions.
#@cli : Default value: 'xy_sections=20'.
afre_section : check "${1=20}>=1 && $1<=50"
m=min(w,h) err=int($m/25) if $err<$1 error[] "\n[afre]>_<) section: Section image$? into no more than "{max(sqr($err),1)}" ("{max($err,1)}"x"{max($err,1)}") regions." fi
e[] "[afre]^_^) section: Section image$? into "{sqr($1)}" ($1x$1) numbered regions."
repeat $! l[$>]
im,iM={[im,iM]} s yx,$1 repeat $!
t[$>] {$>+1},0.5~,0.5~,{$m*0.55},0.9,$im t[$>] {$>+1},0.5~,0.5~,{$m*0.56},0.9,$iM
done append_tiles $1
grid {100/$1}%,{100/$1}%,,,0.2,$im
grid {100/$1}%,{100/$1}%,-1,-1,0.2,$iM
done done
#@cli afre_cleanphoto : 2<=size<=10,1<=_recovery<=100,-50<=_xy_sections<=50,_maskN>=1,...
#@cli : Clean dust and scratches from photos.
#@cli : Default values: 'size=3', 'recovery=10', 'xy_sections=1' and 'maskN=1'.\n
#@cli : - 'xy_sections' sections image into MxM labeled regions.
#@cli : \ \ \ For global processing, set 'xy_sections' to 1.
#@cli : \ \ \ To visualize these regions with labels, use a negative number.
#@cli : - 'maskN' is a comma separated list of regions to be processed.
afre_cleanphoto : check "${1=3}>=2 && ${2=10}>=1 && ${3=1}>=-50 &&
$1<=10 && $2<=100 && $3<=50" skip ${4=1}
m=min(w,h) err=int($m/25) if $err<$3 error[] "\n[afre]>_<) cleanphoto: Section image$? into no more than "{max(sqr($err),1)}" ("{max($err,1)}"x"{max($err,1)}") regions." fi
m "ip : p=$1,,,{iM}
+erode_threshold $p dilate_threshold. $p afre_gui1_fast. .. c. 0,100%
+afre_edge.. 1,1,4,4 afre_box. 1 c. 0,$2% n. 0,1 +blend_fade[0,1] . k[0,-1]
+gaussian. {[w,h]*0.3} c. 0,50% n. 0,1 blend_fade[0,1] . k[0]"
e[] "[afre]^_^) cleanphoto: Clean dust and scratches from image$? using size=$1, recovery=$2, xy_sections=$3 and masks=${4--1}."
repeat $! l[$>] if $3>1
iM={iM} w0,h0={[w,h]} w,h={[w,h]/$3} 100%,100%,1,3 l. (${4--1})
c. 1,{sqr($3)} round. sort. discard. unroll. x
repeat w v={i($>)} i={$v%$3?$v%$3:$3} j={($v+$3-$i)/$3}
r={[$w*$i-$w,$h*$j-$h,$w*$i-1,$h*$j-1]} rectangle[0] $r,1,$iM
done rm. label_fg 0 r {[$w0,$h0]+2},1,3,0,0,0.5,0.5
repeat iM +==[0] {$>+1} coord=${"
w={w} h={h} val={i(w-1,h-1)} +=. {1+$val},100%,100%,100%
autocrop. =. $val,100%,100%,100% x0={$w-w} y0={$h-h} rm.
+autocrop. x1={$x0+w-1} y1={$y0+h-1} rm. u $x0,$y0,$x1,$y1
"} rm. ($coord) done
a[^0] y done r[0] {[$w0,$h0]+2},1,3,0,0,0.5,0.5
repeat h x0,y0,x1,y1={@{[0,1,2,3]+4*$>}} +z[0,1] $x0,$y0,$x1,$y1
+ip.. f. i#4?i#5:i#3 j[0] .,$x0,$y0 rm[-3--1]
done k[0] r {[$w0,$h0]},1,3,0,0,0.5,0.5
else ip
fi done done um ip
#@cli afre_cleanphoto1 : 1<=radius<=10,1<=_xy_sections<=50,_maskN>=1,...
#@cli : Clean dust and scratches from photos.
#@cli : Default values: 'radius=3', 'xy_sections=1' and 'maskN=1'.\n
#@cli : - 'xy_sections' sections image into MxM labeled regions.
#@cli : \ \ For global processing, set 'xy_sections' to 1.
#@cli : - 'maskN' is a comma separated list of regions to be processed.
#@cli : \ \ Use 'afre_section' to determine region labels.
afre_cleanphoto1 : check "${1=3}>=1 && ${2=1}>=1 &&
$1<=10 && $2<=50" skip ${3=1}
rd={int($1)} m=min(w,h) err=int($m/25) if $err<$2 error[] "\n[afre]>_<) cleanphoto: Section image$? into no more than "{max(sqr($err),1)}" ("{max($err,1)}"x"{max($err,1)}") regions." fi
m "ip : s={"$rd"*2+1} +l +erode_oct $s dilate_oct. $s - c 0,100% ^ 2
c 5%,10% dilate_oct 3 n 0,1 done +inpaint_pde.. . rm..
+gaussian. {[w,h]*0.3} c. 0,50% n. 0,1 blend_fade[0,1] . k[0]"
e[] "[afre]^_^) cleanphoto: Clean dust and scratches from image$? using radius="$rd", xy_sections=$2 and maskN=${3--1}."
repeat $! l[$>] if $2>1
iM={iM} w0,h0={[w,h]} w,h={[w,h]/$2} 100%,100%,1,3 l. (${3--1})
c. 1,{sqr($2)} round. sort. discard. unroll. x
repeat w v={i($>)} i={$v%$2?$v%$2:$2} j={($v+$2-$i)/$2}
r={[$w*$i-$w,$h*$j-$h,$w*$i-1,$h*$j-1]} rectangle[0] $r,1,$iM
done rm. label_fg 0 r {[$w0,$h0]+2},1,3,0,0,0.5,0.5
repeat iM +==[0] {$>+1} coord=${"
w={w} h={h} val={i(w-1,h-1)} +=. {1+$val},100%,100%,100%
autocrop. =. $val,100%,100%,100% x0={$w-w} y0={$h-h} rm.
+autocrop. x1={$x0+w-1} y1={$y0+h-1} rm. u $x0,$y0,$x1,$y1
"} rm. ($coord) done
a[^0] y done r[0] {[$w0,$h0]+2},1,3,0,0,0.5,0.5
repeat h x0,y0,x1,y1={@{[0,1,2,3]+4*$>}} +z[0,1] $x0,$y0,$x1,$y1
+ip.. f. i#4?i#5:i#3 j[0] .,$x0,$y0 rm[-3--1]
done k[0] r {[$w0,$h0]},1,3,0,0,0.5,0.5
else ip
fi done done um ip
#@cli search_dichotomic : "increasing_fn",target_y,_precision>0
#@cli : Find parameter for function such that 'target_y' is met in image.
#@cli : Default value: 'precision=1e-3'.\n
#@cli : - Return 'nan' if search fails.
#@cli : * Credit: David Tschumperle.
search_dichotomic : check ${3=1e-3}>0
m "_sdc : $1" target_value,epsilon=$2,$3 mpos,nb_attempts=-1,20
do mvalue=${_sdc\ $mpos} if $mvalue<$target_value break fi mpos*=2
nb_attempts-=1 while $nb_attempts if !$nb_attempts u nan return fi
Mpos,nb_attempts=1,20
do Mvalue=${_sdc\ $Mpos} if $Mvalue>$target_value break fi Mpos*=2
nb_attempts-=1 while $nb_attempts if !$nb_attempts u nan return fi
nb_attempts=100
do
cpos={($mpos+$Mpos)/2} cvalue=${_sdc\ $cpos}
if abs($cvalue-$target_value)<$epsilon u $cpos return
elif $cvalue<$target_value mpos=$cpos else Mpos=$cpos fi
nb_attempts-=1
while $nb_attempts u {$nb_attempts?$cpos:nan}
# List of Commands and Filters
#---------53-----------23-----
# GUI CLI 20
# Colors 5
# afre_brightness afre_contrast afre_localcontrast afre_softlight
# afre_darksky
# Contours 1