-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgmic.cpp
14890 lines (14024 loc) · 691 KB
/
gmic.cpp
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
/*
#
# File : gmic.cpp
# ( C++ source file )
#
# Description : GREYC's Magic for Image Computing - G'MIC core
# ( https://gmic.eu )
#
# Copyright : David Tschumperlé
# ( https://tschumperle.users.greyc.fr/ )
#
# Licenses : This file is 'dual-licensed', you have to choose one
# of the two licenses below to apply.
#
# CeCILL-C
# The CeCILL-C license is close to the GNU LGPL.
# ( http://cecill.info/licences/Licence_CeCILL-C_V1-en.html )
#
# or CeCILL v2.1
# The CeCILL license is compatible with the GNU GPL.
# ( http://cecill.info/licences/Licence_CeCILL_V2.1-en.html )
#
# This software is governed either by the CeCILL or the CeCILL-C 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 or CeCILL-C licenses 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 and CeCILL-C licenses and that you accept its terms.
#
*/
// Add G'MIC-specific methods to the CImg<T> class of the CImg library.
//----------------------------------------------------------------------
#if defined(cimg_plugin)
template<typename t>
static CImg<T> copy_rounded(const CImg<t>& img) {
if (!cimg::type<t>::is_float() || cimg::type<T>::is_float()) return img;
CImg<T> res(img._width,img._height,img._depth,img._spectrum);
const t *ptrs = img._data;
cimg_for(res,ptrd,T) *ptrd = (T)cimg::round(*(ptrs++));
return res;
}
static CImg<T> copy_rounded(const CImg<T>& img) {
return CImg<T>(img,true);
}
static const char *storage_type(const CImgList<T>& images, const bool allow_bool) {
T im = cimg::type<T>::max(), iM = cimg::type<T>::min();
bool is_int = true;
for (unsigned int l = 0; l<images.size() && is_int; ++l) {
cimg_for(images[l],p,T) {
const T val = *p;
if (!(val==(T)(int)val)) { is_int = false; break; }
if (val<im) im = val;
if (val>iM) iM = val;
}
}
if (is_int) {
if (allow_bool && im==0 && iM==1) return "bool";
else if (im>=0) {
if (iM<(1U<<8)) return "uint8";
else if (iM<(1U<<16)) return "uint16";
else if (iM<((cimg_uint64)1<<32)) return "uint32";
} else {
if (im>=-(1<<7) && iM<(1<<7) && cimg::type<char>::min()<0) return "int8";
else if (im>=-(1<<15) && iM<(1<<15)) return "int16";
else if (im>=-((cimg_int64)1<<31) && iM<((cimg_int64)1<<31)) return "int32";
}
}
return pixel_type();
}
static CImg<T> append_CImg3d(const CImgList<T>& images) {
if (!images) return CImg<T>();
if (images.size()==1) return +images[0];
CImg<charT> error_message(1024);
unsigned int g_nbv = 0, g_nbp = 0;
ulongT siz = 0;
cimglist_for(images,l) {
const CImg<T>& img = images[l];
if (!img.is_CImg3d(false,error_message))
throw CImgArgumentException("append_CImg3d(): image [%d] (%u,%u,%u,%u,%p) "
"is not a CImg3d (%s).",
l,img._width,img._height,img._depth,img._spectrum,img._data,
error_message.data());
siz+=img.size() - 8;
g_nbv+=cimg::float2uint((float)img[6]);
g_nbp+=cimg::float2uint((float)img[7]);
}
CImg<T> res(1,siz + 8);
const T **const ptrs = new const T*[images.size()];
T *ptrd = res._data;
*(ptrd++) = (T)('C' + 0.5f); *(ptrd++) = (T)('I' + 0.5f); // Create object header
*(ptrd++) = (T)('m' + 0.5f); *(ptrd++) = (T)('g' + 0.5f);
*(ptrd++) = (T)('3' + 0.5f); *(ptrd++) = (T)('d' + 0.5f);
*(ptrd++) = (T)cimg::uint2float(g_nbv);
*(ptrd++) = (T)cimg::uint2float(g_nbp);
cimglist_for(images,l) { // Merge object points
const CImg<T>& img = images[l];
const unsigned int nbv = cimg::float2uint((float)img[6]);
std::memcpy(ptrd,img._data + 8,3*nbv*sizeof(T));
ptrd+=3*nbv;
ptrs[l] = img._data + 8 + 3*nbv;
}
ulongT poff = 0;
cimglist_for(images,l) { // Merge object primitives
const unsigned int
nbv = cimg::float2uint((float)images[l][6]),
nbp = cimg::float2uint((float)images[l][7]);
for (unsigned int p = 0; p<nbp; ++p) {
const unsigned int
nbi = cimg::float2uint((float)*(ptrs[l]++)),
_nbi = nbi<5?nbi:nbi==5?2:nbi/3;
*(ptrd++) = (T)cimg::uint2float(nbi);
for (unsigned int i = 0; i<_nbi; ++i)
*(ptrd++) = (T)cimg::uint2float(cimg::float2uint((float)*(ptrs[l]++)) + poff);
for (unsigned int i = nbi - _nbi; i; --i)
*(ptrd++) = *(ptrs[l]++);
}
poff+=nbv;
}
ulongT voff = 0;
cimglist_for(images,l) { // Merge object colors
const unsigned int nbc = cimg::float2uint((float)images[l][7]);
for (unsigned int c = 0; c<nbc; ++c)
if (*(ptrs[l])==(T)-128) {
*(ptrd++) = *(ptrs[l]++);
const unsigned int
w = (unsigned int)cimg::float2uint((float)*(ptrs[l]++)),
h = (unsigned int)*(ptrs[l]++),
s = (unsigned int)*(ptrs[l]++);
if (!h && !s) { *(ptrd++) = (T)cimg::uint2float((unsigned int)(w + voff)); *(ptrd++) = 0; *(ptrd++) = 0; }
else {
*(ptrd++) = (T)w; *(ptrd++) = (T)h; *(ptrd++) = (T)s;
const ulongT whs = (ulongT)w*h*s;
std::memcpy(ptrd,ptrs[l],whs*sizeof(T));
ptrs[l]+=whs; ptrd+=whs;
}
} else { *(ptrd++) = *(ptrs[l]++); *(ptrd++) = *(ptrs[l]++); *(ptrd++) = *(ptrs[l]++); }
voff+=nbc;
}
voff = 0;
cimglist_for(images,l) { // Merge object opacities
const unsigned int nbo = cimg::float2uint((float)images[l][7]);
for (unsigned int o = 0; o<nbo; ++o)
if (*(ptrs[l])==(T)-128) {
*(ptrd++) = *(ptrs[l]++);
const unsigned int
w = (unsigned int)cimg::float2uint((float)*(ptrs[l]++)),
h = (unsigned int)*(ptrs[l]++),
s = (unsigned int)*(ptrs[l]++);
if (!h && !s) { *(ptrd++) = (T)cimg::uint2float((unsigned int)(w + voff)); *(ptrd++) = 0; *(ptrd++) = 0; }
else {
*(ptrd++) = (T)w; *(ptrd++) = (T)h; *(ptrd++) = (T)s;
const ulongT whs = (ulongT)w*h*s;
std::memcpy(ptrd,ptrs[l],whs*sizeof(T));
ptrs[l]+=whs; ptrd+=whs;
}
} else *(ptrd++) = *(ptrs[l]++);
voff+=nbo;
}
delete[] ptrs;
return res;
}
CImg<T>& append_string_to(CImg<T>& img, T* &ptrd) const {
if (!_width) return img;
if (ptrd + _width>=img.end()) {
CImg<T> tmp(std::max(8U,2*img._width + _width + 1));
std::memcpy(tmp,img,img._width*sizeof(T));
ptrd = tmp._data + (ptrd - img._data);
tmp.move_to(img);
}
std::memcpy(ptrd,_data,_width*sizeof(T));
ptrd+=_width;
return img;
}
static CImg<T>& append_string_to(const char c, CImg<T>& img, T* &ptrd) {
if (ptrd + 1>=img.end()) {
CImg<T> tmp(std::max(8U,2*img._width + 1));
std::memcpy(tmp,img,img._width*sizeof(T));
ptrd = tmp._data + (ptrd - img._data);
tmp.move_to(img);
}
*(ptrd++) = c;
return img;
}
// Return a copymarked version of an image name.
// This method has no 'in-place' version, at it is always better to call the new instance version.
CImg<T> get_copymark() const {
if (is_empty() || !*_data) return CImg<T>::string("_c1");
const char *pe = _data + _width - 1, *ext = cimg::split_filename(_data);
if (*ext) pe = --ext;
unsigned int num = 0, fact = 1;
if (pe>_data + 2) { // Try to find ending number if any
const char *ppe = pe - 1;
while (ppe>_data && *ppe>='0' && *ppe<='9') { num+=fact*(*(ppe--) - '0'); fact*=10; }
if (ppe>_data && ppe!=pe - 1 && *(ppe - 1)=='_' && *ppe=='c' && ppe[1]!='0') {
pe = ppe - 1;
}
else num = 0;
}
++num;
const unsigned int
ndigits = (unsigned int)std::max(1.,std::ceil(std::log10(num + 1.))),
lbase = (unsigned int)(pe - _data),
lext = _data + _width - ext - 1;
CImg<T> res(lbase + 2 + ndigits + lext + 1);
std::memcpy(res,_data,lbase);
cimg_snprintf(res._data + lbase,res._width - lbase,"_c%u%s",num,ext);
return res;
}
CImg<T> get_draw_ellipse(const int x, const int y, const float r0, const float r1,
const float angle, const T *const col, const float opacity) const {
return (+*this).draw_ellipse(x,y,r0,r1,angle,col,opacity);
}
CImg<T> get_draw_ellipse(const int x, const int y, const float r0, const float r1,
const float angle, const T *const col, const float opacity,
const unsigned int pattern) const {
return (+*this).draw_ellipse(x,y,r0,r1,angle,col,opacity,pattern);
}
CImg<T> get_draw_fill(const int x, const int y, const int z,
const T *const col, const float opacity,
const float tolerance, const bool is_high_connectivity) const {
return (+*this).draw_fill(x,y,z,col,opacity,tolerance,is_high_connectivity);
}
CImg<T>& gmic_draw_image(const float x, const float y, const float z, const float c,
const char sepx, const char sepy, const char sepz, const char sepc,
const CImg<T>& sprite, const CImg<T>& mask, const float opacity,
const float max_opacity_mask) {
const float
fx = sepx=='~'?x*(width() - sprite.width()):sepx=='%'?x*(width() - 1)/100:x,
fy = sepy=='~'?y*(height() - sprite.height()):sepy=='%'?y*(height() - 1)/100:y,
fz = sepz=='~'?y*(depth() - sprite.depth()):sepz=='%'?z*(depth() - 1)/100:z,
fc = sepc=='~'?c*(spectrum() - sprite.spectrum()):sepc=='%'?c*(spectrum() - 1)/100:c;
return draw_image((int)cimg::round(fx),(int)cimg::round(fy),
(int)cimg::round(fz),(int)cimg::round(fc),
sprite,mask,opacity,max_opacity_mask);
}
CImg<T> get_gmic_draw_image(const float x, const float y, const float z, const float c,
const char sepx, const char sepy, const char sepz, const char sepc,
const CImg<T>& sprite, const CImg<T>& mask, const float opacity,
const float max_opacity_mask) const {
return (+*this).gmic_draw_image(x,y,z,c,sepx,sepy,sepz,sepc,sprite,mask,opacity,max_opacity_mask);
}
CImg<T>& gmic_draw_image(const float x, const float y, const float z, const float c,
const char sepx, const char sepy, const char sepz, const char sepc,
const CImg<T>& sprite, const float opacity) {
const float
fx = sepx=='~'?x*(width() - sprite.width()):sepx=='%'?x*(width() - 1)/100:x,
fy = sepy=='~'?y*(height() - sprite.height()):sepy=='%'?y*(height() - 1)/100:y,
fz = sepz=='~'?y*(depth() - sprite.depth()):sepz=='%'?z*(depth() - 1)/100:z,
fc = sepc=='~'?c*(spectrum() - sprite.spectrum()):sepc=='%'?c*(spectrum() - 1)/100:c;
return draw_image((int)cimg::round(fx),(int)cimg::round(fy),
(int)cimg::round(fz),(int)cimg::round(fc),
sprite,opacity);
}
CImg<T> get_gmic_draw_image(const float x, const float y, const float z, const float c,
const char sepx, const char sepy, const char sepz, const char sepc,
const CImg<T>& sprite, const float opacity) const {
return (+*this).gmic_draw_image(x,y,z,c,sepx,sepy,sepz,sepc,sprite,opacity);
}
CImg<T> get_draw_line(const int x0, const int y0, const int x1, const int y1, const T *const col,
const float opacity, const unsigned int pattern) const {
return (+*this).draw_line(x0,y0,x1,y1,col,opacity,pattern);
}
template<typename tp, typename tf, typename tc, typename to>
CImg<T> get_draw_object3d(const float x0, const float y0, const float z0,
const CImg<tp>& vertices, const CImgList<tf>& primitives,
const CImgList<tc>& colors, const CImgList<to>& opacities,
const unsigned int render_mode, const bool double_sided,
const float focale,
const float light_x, const float light_y,const float light_z,
const float specular_lightness, const float specular_shininess,
const float g_opacity, CImg<floatT>& zbuffer) const {
return (+*this).draw_object3d(x0,y0,z0,vertices,primitives,colors,opacities,render_mode,
double_sided,focale,light_x,light_y,light_z,specular_lightness,
specular_shininess,g_opacity,zbuffer);
}
CImg<T> get_draw_point(const int x, const int y, const int z, const T *const col,
const float opacity) const {
return (+*this).draw_point(x,y,z,col,opacity);
}
template<typename t>
CImg<T> get_draw_polygon(const CImg<t>& points, const T *const color, const float opacity) const {
return (+*this).draw_polygon(points,color,opacity);
}
template<typename t>
CImg<T> get_draw_polygon(const CImg<t>& points,
const T *const color, const float opacity, const unsigned int pattern,
const bool is_closed) const {
return (+*this).draw_polygon(points,color,opacity,pattern,is_closed);
}
CImg<T>& gmic_blur(const float sigma_x, const float sigma_y, const float sigma_z, const float sigma_c,
const unsigned int boundary_conditions, const bool is_gaussian) {
if (is_empty()) return *this;
if (is_gaussian) {
if (_width>1) vanvliet(sigma_x,0,'x',boundary_conditions);
if (_height>1) vanvliet(sigma_y,0,'y',boundary_conditions);
if (_depth>1) vanvliet(sigma_z,0,'z',boundary_conditions);
if (_spectrum>1) vanvliet(sigma_c,0,'c',boundary_conditions);
} else {
if (_width>1) deriche(sigma_x,0,'x',boundary_conditions);
if (_height>1) deriche(sigma_y,0,'y',boundary_conditions);
if (_depth>1) deriche(sigma_z,0,'z',boundary_conditions);
if (_spectrum>1) deriche(sigma_c,0,'c',boundary_conditions);
}
return *this;
}
CImg<Tfloat> get_gmic_blur(const float sigma_x, const float sigma_y, const float sigma_z, const float sigma_c,
const unsigned int boundary_conditions, const bool is_gaussian) const {
return CImg<Tfloat>(*this,false).gmic_blur(sigma_x,sigma_y,sigma_z,sigma_c,boundary_conditions,is_gaussian);
}
CImg<T>& gmic_blur_box(const float sigma_x, const float sigma_y, const float sigma_z, const float sigma_c,
const unsigned int order, const unsigned int boundary_conditions,
const unsigned int nb_iter) {
if (is_empty()) return *this;
if (_width>1) boxfilter(sigma_x,order,'x',boundary_conditions,nb_iter);
if (_height>1) boxfilter(sigma_y,order,'y',boundary_conditions,nb_iter);
if (_depth>1) boxfilter(sigma_z,order,'z',boundary_conditions,nb_iter);
if (_spectrum>1) boxfilter(sigma_c,order,'c',boundary_conditions,nb_iter);
return *this;
}
CImg<Tfloat> get_gmic_blur_box(const float sigma_x, const float sigma_y, const float sigma_z, const float sigma_c,
const unsigned int order, const unsigned int boundary_conditions,
const unsigned int nb_iter) const {
return CImg<Tfloat>(*this,false).gmic_blur_box(sigma_x,sigma_y,sigma_z,sigma_c,order,boundary_conditions,nb_iter);
}
CImg<T>& gmic_blur_box(const float sigma, const unsigned int order, const unsigned int boundary_conditions,
const unsigned int nb_iter) {
const float nsigma = sigma>=0?sigma:-sigma*cimg::max(_width,_height,_depth)/100;
return gmic_blur_box(nsigma,nsigma,nsigma,0,order,boundary_conditions,nb_iter);
}
CImg<Tfloat> get_gmic_blur_box(const float sigma, const unsigned int order, const unsigned int boundary_conditions,
const unsigned int nb_iter) const {
return CImg<Tfloat>(*this,false).gmic_blur_box(sigma,order,boundary_conditions,nb_iter);
}
CImg<T>& gmic_discard(const char *const axes) {
for (const char *s = axes; *s; ++s) discard(*s);
return *this;
}
CImg<T> get_gmic_discard(const char *const axes) const {
return (+*this).gmic_discard(axes);
}
template<typename t>
CImg<T>& gmic_discard(const CImg<t>& values, const char *const axes) {
if (is_empty() || !values || !axes || !*axes) return *this;
for (const char *s = axes; *s; ++s) discard(values,*s);
return *this;
}
template<typename t>
CImg<T> get_gmic_discard(const CImg<t>& values, const char *const axes) const {
return (+*this).gmic_discard(values,axes);
}
CImg<T>& gmic_draw_text(const float x, const float y,
const char sepx, const char sepy,
const char *const text, const T *const col,
const int bg, const float opacity, const unsigned int siz,
const unsigned int nb_cols) {
float fx = 0, fy = 0;
if (is_empty()) {
const T one[] = { (T)1 };
fx = sepx=='%' || sepx=='~'?0:x;
fy = sepy=='%' || sepy=='~'?0:y;
draw_text((int)cimg::round(fx),(int)cimg::round(fy),"%s",one,0,opacity,siz,text).resize(-100,-100,1,nb_cols);
cimg_forC(*this,c) if (col[c]!=1) get_shared_channel(c)*=col[c];
return *this;
}
if (sepx=='~' || sepy=='~') {
const unsigned char one[] = { 1 };
CImg<ucharT> foo;
foo.draw_text(0,0,"%s",one,0,1,siz,text);
fx = sepx=='~'?x*(width() - foo.width()):sepx=='%'?x*(width() - 1)/100:x;
fy = sepy=='~'?y*(height() - foo.height()):sepy=='%'?y*(height() - 1)/100:y;
} else {
fx = sepx=='%'?x*(width() - 1)/100:x;
fy = sepy=='%'?y*(height() - 1)/100:y;
}
return draw_text((int)cimg::round(fx),(int)cimg::round(fy),"%s",col,bg,opacity,siz,text);
}
CImg<T> get_gmic_draw_text(const float x, const float y,
const char sepx, const char sepy,
const char *const text, const T *const col,
const int bg, const float opacity, const unsigned int siz,
const unsigned int nb_cols) const {
return (+*this).gmic_draw_text(x,y,sepx,sepy,text,col,bg,opacity,siz,nb_cols);
}
CImg<T>& gmic_draw_text(const float x, const float y,
const char sepx, const char sepy,
const char *const text, const T *const col,
const int bg, const float opacity, const CImgList<T>& font,
const unsigned int nb_cols) {
float fx = 0, fy = 0;
if (is_empty()) {
const T one[] = { (T)1 };
fx = sepx=='%' || sepx=='~'?0:x;
fy = sepy=='%' || sepy=='~'?0:y;
draw_text((int)cimg::round(fx),(int)cimg::round(fy),"%s",one,0,opacity,&font,text).resize(-100,-100,1,nb_cols);
cimg_forC(*this,c) get_shared_channel(c)*=col[c];
return *this;
}
if (sepx=='~' || sepy=='~') {
const unsigned char one[] = { 1 };
CImg<ucharT> foo;
foo.draw_text(0,0,"%s",one,0,1,&font,text);
fx = sepx=='~'?x*(width() - foo.width()):sepx=='%'?x*(width() - 1)/100:x;
fy = sepy=='~'?y*(height() - foo.height()):sepy=='%'?y*(height() - 1)/100:y;
} else {
fx = sepx=='%'?x*(width() - 1)/100:x;
fy = sepy=='%'?y*(height() - 1)/100:y;
}
return draw_text((int)cimg::round(fx),(int)cimg::round(fy),"%s",col,bg,opacity,&font,text);
}
CImg<T> get_gmic_draw_text(const float x, const float y,
const char sepx, const char sepy,
const char *const text, const T *const col,
const int bg, const float opacity, const CImgList<T>& font,
const unsigned int nb_cols) const {
return (+*this).gmic_draw_text(x,y,sepx,sepy,text,col,bg,opacity,font,nb_cols);
}
CImg<T>& gmic_invert_endianness(const char *const stype) {
#define _gmic_invert_endianness(svalue_type,value_type) \
if (!std::strcmp(stype,svalue_type)) \
if (pixel_type()==cimg::type<value_type>::string()) invert_endianness(); \
else CImg<value_type>(*this).invert_endianness().move_to(*this);
if (!std::strcmp(stype,"bool") ||
!std::strcmp(stype,"uint8") ||
!std::strcmp(stype,"int8")) return *this;
_gmic_invert_endianness("uint16",cimg_uint16)
else _gmic_invert_endianness("int16",cimg_int16)
else _gmic_invert_endianness("uint32",cimg_uint32)
else _gmic_invert_endianness("int32",cimg_int32)
else _gmic_invert_endianness("uint64",cimg_uint64)
else _gmic_invert_endianness("int64",cimg_int64)
else _gmic_invert_endianness("float32",cimg_float32)
else _gmic_invert_endianness("float64",cimg_float64)
else invert_endianness();
return *this;
}
CImg<T> get_gmic_invert_endianness(const char *const stype) const {
return (+*this).gmic_invert_endianness(stype);
}
CImg<T>& gmic_matchpatch(const CImg<T>& patch_image,
const unsigned int patch_width,
const unsigned int patch_height,
const unsigned int patch_depth,
const unsigned int nb_iterations,
const unsigned int nb_randoms,
const float patch_penalization,
const bool is_score,
const CImg<T> *const initialization) {
return get_gmic_matchpatch(patch_image,patch_width,patch_height,patch_depth,
nb_iterations,nb_randoms,patch_penalization,is_score,initialization).move_to(*this);
}
CImg<T> get_gmic_matchpatch(const CImg<T>& patch_image,
const unsigned int patch_width,
const unsigned int patch_height,
const unsigned int patch_depth,
const unsigned int nb_iterations,
const unsigned int nb_randoms,
const float patch_penalization,
const bool is_score,
const CImg<T> *const initialization) const {
CImg<floatT> score, res;
res = _matchpatch(patch_image,patch_width,patch_height,patch_depth,
nb_iterations,nb_randoms,patch_penalization,
initialization?*initialization:CImg<T>::const_empty(),
is_score,is_score?score:CImg<floatT>::empty());
const unsigned int s = res._spectrum;
if (score) res.resize(-100,-100,-100,s + 1,0).draw_image(0,0,0,s,score);
return res;
}
const CImg<T>& gmic_print(const char *const title, const bool is_debug,
const bool is_valid) const {
cimg::mutex(29);
CImg<doubleT> st;
if (is_valid && !is_empty()) get_stats().move_to(st);
const ulongT siz = size(), msiz = siz*sizeof(T), siz1 = siz - 1,
mdisp = msiz<8*1024?0U:msiz<8*1024*1024?1U:2U,
wh = _width*_height, whd = _width*_height*_depth,
w1 = _width - 1, wh1 = _width*_height - 1, whd1 = _width*_height*_depth - 1;
std::fprintf(cimg::output(),"%s%s%s%s:\n %ssize%s = (%u,%u,%u,%u) [%lu %s of %s%s].\n %sdata%s = %s",
cimg::t_magenta,cimg::t_bold,title,cimg::t_normal,
cimg::t_bold,cimg::t_normal,_width,_height,_depth,_spectrum,
(unsigned long)(mdisp==0?msiz:(mdisp==1?(msiz>>10):(msiz>>20))),
mdisp==0?"b":(mdisp==1?"Kio":"Mio"),
_is_shared?"shared ":"",
pixel_type(),
cimg::t_bold,cimg::t_normal,
is_debug?"":"(");
if (is_debug) std::fprintf(cimg::output(),"%p = (",(void*)_data);
if (is_valid) {
if (is_empty()) std::fprintf(cimg::output(),") [%s].\n",
pixel_type());
else {
cimg_foroff(*this,off) {
std::fprintf(cimg::output(),cimg::type<T>::format_s(),cimg::type<T>::format(_data[off]));
if (off!=siz1) std::fprintf(cimg::output(),"%s",
off%whd==whd1?" ^ ":
off%wh==wh1?"\\":
off%_width==w1?";":",");
if (off==11 && siz>24) { off = siz1 - 12; std::fprintf(cimg::output(),"(...),"); }
}
std::fprintf(cimg::output(),")%s.\n %smin%s = %g, %smax%s = %g, %smean%s = %g, "
"%sstd%s = %g, %scoords_min%s = (%u,%u,%u,%u), "
"%scoords_max%s = (%u,%u,%u,%u).\n",
_is_shared?" [shared]":"",
cimg::t_bold,cimg::t_normal,st[0],
cimg::t_bold,cimg::t_normal,st[1],
cimg::t_bold,cimg::t_normal,st[2],
cimg::t_bold,cimg::t_normal,std::sqrt(st[3]),
cimg::t_bold,cimg::t_normal,(int)st[4],(int)st[5],(int)st[6],(int)st[7],
cimg::t_bold,cimg::t_normal,(int)st[8],(int)st[9],(int)st[10],(int)st[11]);
}
} else std::fprintf(cimg::output(),"%s%sinvalid pointer%s) [shared %s].\n",
cimg::t_red,cimg::t_bold,cimg::t_normal,
pixel_type());
std::fflush(cimg::output());
cimg::mutex(29,0);
return *this;
}
CImg<T>& gmic_set(const double value,
const int x, const int y, const int z, const int v) {
(*this).atXYZC(x,y,z,v,(T)0) = (T)value;
return *this;
}
CImg<T> get_gmic_set(const double value,
const int x, const int y, const int z, const int v) const {
return (+*this).gmic_set(value,x,y,z,v);
}
CImg<T>& gmic_shift(const float delta_x, const float delta_y=0, const float delta_z=0, const float delta_c=0,
const unsigned int boundary_conditions=0, const bool interpolation=false) {
if (is_empty()) return *this;
const int
idelta_x = (int)cimg::round(delta_x), idelta_y = (int)cimg::round(delta_y),
idelta_z = (int)cimg::round(delta_z), idelta_c = (int)cimg::round(delta_c);
if (!interpolation ||
(delta_x==(float)idelta_x && delta_y==(float)idelta_y && delta_z==(float)idelta_z && delta_c==(float)idelta_c))
return shift(idelta_x,idelta_y,idelta_z,idelta_c,boundary_conditions); // Integer displacement
return _gmic_shift(delta_x,delta_y,delta_z,delta_c,boundary_conditions).move_to(*this);
}
CImg<T> get_gmic_shift(const float delta_x, const float delta_y=0, const float delta_z=0, const float delta_c=0,
const unsigned int boundary_conditions=0, const bool interpolation=false) const {
if (is_empty()) return CImg<T>::empty();
const int
idelta_x = (int)cimg::round(delta_x), idelta_y = (int)cimg::round(delta_y),
idelta_z = (int)cimg::round(delta_z), idelta_c = (int)cimg::round(delta_c);
if (!interpolation ||
(delta_x==(float)idelta_x && delta_y==(float)idelta_y && delta_z==(float)idelta_z && delta_c==(float)idelta_c))
return (+*this).shift(idelta_x,idelta_y,idelta_z,idelta_c,boundary_conditions); // Integer displacement
return _gmic_shift(delta_x,delta_y,delta_z,delta_c,boundary_conditions);
}
CImg<T> _gmic_shift(const float delta_x, const float delta_y=0, const float delta_z=0, const float delta_c=0,
const unsigned int boundary_conditions=0) const {
CImg<T> res(_width,_height,_depth,_spectrum);
if (delta_c!=0) // 4D shift
switch (boundary_conditions) {
case 3 : { // Mirror
const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth(), s2 = 2.f*spectrum();
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forXYZC(res,x,y,z,c) {
const float
mx = cimg::mod(x - delta_x,w2),
my = cimg::mod(y - delta_y,h2),
mz = cimg::mod(z - delta_z,d2),
mc = cimg::mod(c - delta_c,s2);
res(x,y,z,c) = _linear_atXYZC(mx<width()?mx:w2 - mx - 1,
my<height()?my:h2 - my - 1,
mz<depth()?mz:d2 - mz - 1,
mc<spectrum()?mc:s2 - mc - 1);
}
} break;
case 2 : // Periodic
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forXYZC(res,x,y,z,c) res(x,y,z,c) = _linear_atXYZC_p(x - delta_x,y - delta_y,z - delta_z,c - delta_c);
break;
case 1 : // Neumann
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forXYZC(res,x,y,z,c) res(x,y,z,c) = _linear_atXYZC(x - delta_x,y - delta_y,z - delta_z,c - delta_c);
break;
default : // Dirichlet
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forXYZC(res,x,y,z,c) res(x,y,z,c) = linear_atXYZC(x - delta_x,y - delta_y,z - delta_z,c - delta_c,(T)0);
}
else if (delta_z!=0) // 3D shift
switch (boundary_conditions) {
case 3 : { // Mirror
const float w2 = 2.f*width(), h2 = 2.f*height(), d2 = 2.f*depth();
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forC(res,c) cimg_forXYZ(res,x,y,z) {
const float
mx = cimg::mod(x - delta_x,w2),
my = cimg::mod(y - delta_y,h2),
mz = cimg::mod(z - delta_z,d2);
res(x,y,z,c) = _linear_atXYZ(mx<width()?mx:w2 - mx - 1,
my<height()?my:h2 - my - 1,
mz<depth()?mz:d2 - mz - 1,c);
}
} break;
case 2 : // Periodic
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forC(res,c) cimg_forXYZ(res,x,y,z) res(x,y,z,c) = _linear_atXYZ_p(x - delta_x,y - delta_y,z - delta_z,c);
break;
case 1 : // Neumann
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forC(res,c) cimg_forXYZ(res,x,y,z) res(x,y,z,c) = _linear_atXYZ(x - delta_x,y - delta_y,z - delta_z,c);
break;
default : // Dirichlet
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forC(res,c) cimg_forXYZ(res,x,y,z) res(x,y,z,c) = linear_atXYZ(x - delta_x,y - delta_y,z - delta_z,c,(T)0);
}
else if (delta_y!=0) // 2D shift
switch (boundary_conditions) {
case 3 : { // Mirror
const float w2 = 2.f*width(), h2 = 2.f*height();
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forZC(res,z,c) cimg_forXY(res,x,y) {
const float
mx = cimg::mod(x - delta_x,w2),
my = cimg::mod(y - delta_y,h2);
res(x,y,z,c) = _linear_atXY(mx<width()?mx:w2 - mx - 1,
my<height()?my:h2 - my - 1,z,c);
}
} break;
case 2 : // Periodic
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forZC(res,z,c) cimg_forXY(res,x,y) res(x,y,z,c) = _linear_atXY_p(x - delta_x,y - delta_y,z,c);
break;
case 1 : // Neumann
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forZC(res,z,c) cimg_forXY(res,x,y) res(x,y,z,c) = _linear_atXY(x - delta_x,y - delta_y,z,c);
break;
default : // Dirichlet
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forZC(res,z,c) cimg_forXY(res,x,y) res(x,y,z,c) = linear_atXY(x - delta_x,y - delta_y,z,c,(T)0);
}
else // 1D shift
switch (boundary_conditions) {
case 3 : { // Mirror
const float w2 = 2.f*width();
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forYZC(res,y,z,c) cimg_forX(res,x) {
const float mx = cimg::mod(x - delta_x,w2);
res(x,y,z,c) = _linear_atX(mx<width()?mx:w2 - mx - 1,y,z,c);
}
} break;
case 2 : // Periodic
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forYZC(res,y,z,c) cimg_forX(res,x) res(x,y,z,c) = _linear_atX_p(x - delta_x,y,z,c);
break;
case 1 : // Neumann
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forYZC(res,y,z,c) cimg_forX(res,x) res(x,y,z,c) = _linear_atX(x - delta_x,y,z,c);
break;
default : // Dirichlet
cimg_pragma_openmp(parallel for cimg_openmp_collapse(3) cimg_openmp_if_size(res.size(),4096))
cimg_forYZC(res,y,z,c) cimg_forX(res,x) res(x,y,z,c) = linear_atX(x - delta_x,y,z,c,(T)0);
}
return res;
}
template<typename t>
const CImg<T>& gmic_symmetric_eigen(CImg<t>& val, CImg<t>& vec) const {
if (spectrum()!=3 && spectrum()!=6) return symmetric_eigen(val,vec);
val.assign(width(),height(),depth(),spectrum()==3?2:3);
vec.assign(width(),height(),depth(),spectrum()==3?2:6);
CImg<t> _val, _vec;
cimg_forXYZ(*this,x,y,z) {
get_tensor_at(x,y,z).symmetric_eigen(_val,_vec);
val.set_vector_at(_val,x,y,z);
if (spectrum()==3) {
vec(x,y,z,0) = _vec(0,0);
vec(x,y,z,1) = _vec(0,1);
} else {
vec(x,y,z,0) = _vec(0,0);
vec(x,y,z,1) = _vec(0,1);
vec(x,y,z,2) = _vec(0,2);
vec(x,y,z,3) = _vec(1,0);
vec(x,y,z,4) = _vec(1,1);
vec(x,y,z,5) = _vec(1,2);
}
}
return *this;
}
template<typename t>
CImg<T>& inpaint(const CImg<t>& mask, const unsigned int method) {
if (!is_sameXYZ(mask))
throw CImgArgumentException("CImg<%s>::inpaint(): Invalid mask (%u,%u,%u,%u,%p) for "
"instance image (%u,%u,%u,%u,%p).",
pixel_type(),mask._width,mask._height,mask._depth,
mask._spectrum,mask._data,
_width,_height,_depth,_spectrum,_data);
CImg<t> _mask(mask,false), _nmask(mask,false);
bool is_pixel = false;
do {
is_pixel = false;
if (depth()==1) { // 2D image
CImg_3x3(M,t);
CImg_3x3(I,T);
switch (method) {
case 0: // Average 2D (low-connectivity)
cimg_for3x3(_mask,x,y,0,0,M,t) if (Mcc && (!Mcp || !Mpc || !Mnc || !Mcn)) {
is_pixel = true;
const unsigned int wcp = Mcp?0U:1U, wpc = Mpc?0U:1U, wnc = Mnc?0U:1U, wcn = Mcn?0U:1U,
sumw = wcp + wpc + wnc + wcn;
cimg_forC(*this,k) {
cimg_get3x3(*this,x,y,0,k,I,T);
(*this)(x,y,k) = (T)((wcp*Icp + wpc*Ipc + wnc*Inc + wcn*Icn)/(float)sumw);
}
_nmask(x,y) = 0;
}
break;
case 1: // Average 2D (high-connectivity)
cimg_for3x3(_mask,x,y,0,0,M,t) if (Mcc && (!Mpp || !Mcp || !Mnp || !Mpc || !Mnc || !Mpn || !Mcn || !Mnn)) {
is_pixel = true;
const unsigned int
wpp = Mpp?0U:1U, wcp = Mcp?0U:2U, wnp = Mnp?0U:1U,
wpc = Mpc?0U:2U, wnc = Mnc?0U:2U,
wpn = Mpn?0U:1U, wcn = Mcn?0U:2U, wnn = Mnn?0U:1U,
sumw = wpp + wcp + wnp + wpc + wnc + wpn + wcn + wnn;
cimg_forC(*this,k) {
cimg_get3x3(*this,x,y,0,k,I,T);
(*this)(x,y,k) = (T)((wpp*Ipp + wcp*Icp + wnp*Inp + wpc*Ipc +
wnc*Inc + wpn*Ipn + wcn*Icn + wnn*Inn)/(float)sumw);
}
_nmask(x,y) = 0;
}
break;
case 2: { // Median 2D (low-connectivity)
T J[4];
cimg_for3x3(_mask,x,y,0,0,M,t)
if (Mcc && (!Mcp || !Mpc || !Mnc || !Mcn)) {
is_pixel = true;
cimg_forC(*this,k) {
cimg_get3x3(*this,x,y,0,k,I,T);
unsigned int ind = 0;
if (!Mcp) J[ind++] = Icp;
if (!Mpc) J[ind++] = Ipc;
if (!Mnc) J[ind++] = Inc;
if (!Mcn) J[ind++] = Icn;
(*this)(x,y,k) = CImg<T>(J,ind,1,1,1,true).kth_smallest(ind>>1);
}
_nmask(x,y) = 0;
}
} break;
default: // Median 2D (high-connectivity)
T J[8];
cimg_for3x3(_mask,x,y,0,0,M,t)
if (Mcc && (!Mpp || !Mcp || !Mnp || !Mpc || !Mnc || !Mpn || !Mcn || !Mnn)) {
is_pixel = true;
cimg_forC(*this,k) {
cimg_get3x3(*this,x,y,0,k,I,T);
unsigned int ind = 0;
if (!Mpp) J[ind++] = Ipp;
if (!Mcp) J[ind++] = Icp;
if (!Mnp) J[ind++] = Inp;
if (!Mpc) J[ind++] = Ipc;
if (!Mnc) J[ind++] = Inc;
if (!Mpn) J[ind++] = Ipn;
if (!Mcn) J[ind++] = Icn;
if (!Mnn) J[ind++] = Inn;
(*this)(x,y,k) = CImg<T>(J,ind,1,1,1,true).kth_smallest(ind>>1);
}
_nmask(x,y) = 0;
}
}
} else { // 3D image
CImg_3x3x3(M,t);
CImg_3x3x3(I,T);
switch (method) {
case 0: // Average 3D (low-connectivity)
cimg_for3x3x3(_mask,x,y,z,0,M,t)
if (Mccc && (!Mccp || !Mcpc || !Mpcc || !Mncc || !Mcnc || !Mccn)) {
is_pixel = true;
const unsigned int
wccp = Mccp?0U:1U, wcpc = Mcpc?0U:1U, wpcc = Mpcc?0U:1U,
wncc = Mncc?0U:1U, wcnc = Mcnc?0U:1U, wccn = Mccn?0U:1U,
sumw = wcpc + wpcc + wccp + wncc + wcnc + wccn;
cimg_forC(*this,k) {
cimg_get3x3x3(*this,x,y,z,k,I,T);
(*this)(x,y,z,k) = (T)((wccp*Iccp + wcpc*Icpc + wpcc*Ipcc +
wncc*Incc + wcnc*Icnc + wccn*Iccn)/(float)sumw);
}
_nmask(x,y,z) = 0;
}
break;
case 1: // Average 3D (high-connectivity)
cimg_for3x3x3(_mask,x,y,z,0,M,t)
if (Mccc && (!Mppp || !Mcpp || !Mnpp || !Mpcp || !Mccp || !Mncp || !Mpnp || !Mcnp ||
!Mnnp || !Mppc || !Mcpc || !Mnpc || !Mpcc || !Mncc || !Mpnc || !Mcnc ||
!Mnnc || !Mppn || !Mcpn || !Mnpn || !Mpcn || !Mccn || !Mncn || !Mpnn ||
!Mcnn || !Mnnn)) {
is_pixel = true;
const unsigned int
wppp = Mppp?0U:1U, wcpp = Mcpp?0U:2U, wnpp = Mnpp?0U:1U,
wpcp = Mpcp?0U:2U, wccp = Mccp?0U:4U, wncp = Mncp?0U:2U,
wpnp = Mpnp?0U:1U, wcnp = Mcnp?0U:2U, wnnp = Mnnp?0U:1U,
wppc = Mppc?0U:2U, wcpc = Mcpc?0U:4U, wnpc = Mnpc?0U:2U,
wpcc = Mpcc?0U:4U, wncc = Mncc?0U:4U,
wpnc = Mpnc?0U:2U, wcnc = Mcnc?0U:4U, wnnc = Mnnc?0U:2U,
wppn = Mppn?0U:1U, wcpn = Mcpn?0U:2U, wnpn = Mnpn?0U:1U,
wpcn = Mpcn?0U:2U, wccn = Mccn?0U:4U, wncn = Mncn?0U:2U,
wpnn = Mpnn?0U:1U, wcnn = Mcnn?0U:2U, wnnn = Mnnn?0U:1U,
sumw = wppp + wcpp + wnpp + wpcp + wccp + wncp + wpnp + wcnp + wnnp +
wppc + wcpc + wnpc + wpcc + wncc + wpnc + wcnc + wnnc +
wppn + wcpn + wnpn + wpcn + wccn + wncn + wpnn + wcnn + wnnn;
cimg_forC(*this,k) {
cimg_get3x3x3(*this,x,y,z,k,I,T);
(*this)(x,y,z,k) = (T)((wppp*Ippp + wcpp*Icpp + wnpp*Inpp +
wpcp*Ipcp + wccp*Iccp + wncp*Incp +
wpnp*Ipnp + wcnp*Icnp + wnnp*Innp +
wppc*Ippc + wcpc*Icpc + wnpc*Inpc +
wpcc*Ipcc + wncc*Incc +
wpnc*Ipnc + wcnc*Icnc + wnnc*Innc +
wppn*Ippn + wcpn*Icpn + wnpn*Inpn +
wpcn*Ipcn + wccn*Iccn + wncn*Incn +
wpnn*Ipnn + wcnn*Icnn + wnnn*Innn)/(float)sumw);
}
_nmask(x,y,z) = 0;
}
break;
case 2: { // Median 3D (low-connectivity)
T J[6];
cimg_for3x3x3(_mask,x,y,z,0,M,t)
if (Mccc && (!Mccp || !Mcpc || !Mpcc || !Mncc || !Mcnc || !Mccn)) {
is_pixel = true;
cimg_forC(*this,k) {
cimg_get3x3x3(*this,x,y,z,k,I,T);
unsigned int ind = 0;
if (!Mccp) J[ind++] = Iccp;
if (!Mcpc) J[ind++] = Icpc;
if (!Mpcc) J[ind++] = Ipcc;
if (!Mncc) J[ind++] = Incc;
if (!Mcnc) J[ind++] = Icnc;
if (!Mccn) J[ind++] = Iccn;
(*this)(x,y,z,k) = CImg<T>(J,ind,1,1,1,true).kth_smallest(ind>>1);
}
_nmask(x,y,z) = 0;
}
} break;
default: { // Median 3D (high-connectivity)
T J[26];
cimg_for3x3x3(_mask,x,y,z,0,M,t)
if (Mccc && (!Mppp || !Mcpp || !Mnpp || !Mpcp || !Mccp || !Mncp || !Mpnp || !Mcnp ||
!Mnnp || !Mppc || !Mcpc || !Mnpc || !Mpcc || !Mncc || !Mpnc || !Mcnc ||
!Mnnc || !Mppn || !Mcpn || !Mnpn || !Mpcn || !Mccn || !Mncn || !Mpnn ||
!Mcnn || !Mnnn)) {
is_pixel = true;
cimg_forC(*this,k) {
cimg_get3x3x3(*this,x,y,z,k,I,T);
unsigned int ind = 0;
if (!Mppp) J[ind++] = Ippp;
if (!Mcpp) J[ind++] = Icpp;
if (!Mnpp) J[ind++] = Inpp;
if (!Mpcp) J[ind++] = Ipcp;
if (!Mccp) J[ind++] = Iccp;
if (!Mncp) J[ind++] = Incp;
if (!Mpnp) J[ind++] = Ipnp;
if (!Mcnp) J[ind++] = Icnp;
if (!Mnnp) J[ind++] = Innp;
if (!Mppc) J[ind++] = Ippc;
if (!Mcpc) J[ind++] = Icpc;
if (!Mnpc) J[ind++] = Inpc;
if (!Mpcc) J[ind++] = Ipcc;
if (!Mncc) J[ind++] = Incc;
if (!Mpnc) J[ind++] = Ipnc;
if (!Mcnc) J[ind++] = Icnc;
if (!Mnnc) J[ind++] = Innc;
if (!Mppn) J[ind++] = Ippn;
if (!Mcpn) J[ind++] = Icpn;
if (!Mnpn) J[ind++] = Inpn;
if (!Mpcn) J[ind++] = Ipcn;
if (!Mccn) J[ind++] = Iccn;
if (!Mncn) J[ind++] = Incn;
if (!Mpnn) J[ind++] = Ipnn;
if (!Mcnn) J[ind++] = Icnn;
if (!Mnnn) J[ind++] = Innn;
(*this)(x,y,z,k) = CImg<T>(J,ind,1,1,1,true).kth_smallest(ind>>1);
}
_nmask(x,y,z) = 0;
}
} break;
}
}
_mask = _nmask;
} while (is_pixel);
return *this;
}
template<typename t>
CImg<T> get_inpaint(const CImg<t>& mask, const unsigned int method) const {
return (+*this).inpaint(mask,method);
}
template<typename t>
CImg<T>& inpaint_patch(const CImg<t>& mask, const unsigned int patch_size=11,
const unsigned int lookup_size=22, const float lookup_factor=1,
const int lookup_increment=1,
const unsigned int blend_size=0, const float blend_threshold=0.5f,
const float blend_decay=0.02f, const unsigned int blend_scales=10,
const bool is_blend_outer=false) {
if (depth()>1)
throw CImgInstanceException(_cimg_instance
"inpaint_patch(): Instance image is volumetric (should be 2D).",
cimg_instance);
if (!is_sameXYZ(mask))
throw CImgArgumentException(_cimg_instance
"inpaint_patch() : Sizes of instance image and specified mask "
"(%u,%u,%u,%u) do not match.",
cimg_instance,
mask._width,mask._height,mask._depth,mask._spectrum);
if (!patch_size)
throw CImgArgumentException(_cimg_instance
"inpaint_patch() : Specified patch size is 0, must be strictly "
"positive.",
cimg_instance);
if (!lookup_size)
throw CImgArgumentException(_cimg_instance
"inpaint_patch() : Specified lookup size is 0, must be strictly "
"positive.",
cimg_instance);
if (lookup_factor<0)
throw CImgArgumentException(_cimg_instance