cimg.cxx 3.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,508 @@
1
+ /*
2
+ #
3
+ # File : inpaint.h
4
+ # ( C++ header file - CImg plug-in )
5
+ #
6
+ # Copyright : David Tschumperlé
7
+ #
8
+ # Description :
9
+ #
10
+ # This plug-in implements the patch-based inpainting algorithm for 2d images, as
11
+ # described in the two following publications :
12
+ #
13
+ # "A Smarter Examplar-based Inpainting Algorithm using Local and Global Heuristics
14
+ # for more Geometric Coherence."
15
+ # (M. Daisy, P. Buyssens, D. Tschumperlé, O. Lezoray).
16
+ # IEEE International Conference on Image Processing (ICIP'14), Paris/France, Oct. 2014
17
+ #
18
+ # and
19
+ #
20
+ # "A Fast Spatial Patch Blending Algorithm for Artefact Reduction in Pattern-based
21
+ # Image Inpainting."
22
+ # (M. Daisy, D. Tschumperlé, O. Lezoray).
23
+ # SIGGRAPH Asia 2013 Technical Briefs, Hong-Kong, November 2013.
24
+ #
25
+ # Licenses : This file is 'dual-licensed', you have to choose one
26
+ # of the two licenses below to apply.
27
+ #
28
+ # CeCILL-C
29
+ # The CeCILL-C license is close to the GNU LGPL.
30
+ # ( http://cecill.info/licences/Licence_CeCILL-C_V1-en.html )
31
+ #
32
+ # or CeCILL v2.1
33
+ # The CeCILL license is compatible with the GNU GPL.
34
+ # ( http://cecill.info/licences/Licence_CeCILL_V2.1-en.html )
35
+ #
36
+ # This software is governed either by the CeCILL or the CeCILL-C license
37
+ # under French law and abiding by the rules of distribution of free software.
38
+ # You can use, modify and or redistribute the software under the terms of
39
+ # the CeCILL or CeCILL-C licenses as circulated by CEA, CNRS and INRIA
40
+ # at the following URL: "http://cecill.info".
41
+ #
42
+ # As a counterpart to the access to the source code and rights to copy,
43
+ # modify and redistribute granted by the license, users are provided only
44
+ # with a limited warranty and the software's author, the holder of the
45
+ # economic rights, and the successive licensors have only limited
46
+ # liability.
47
+ #
48
+ # In this respect, the user's attention is drawn to the risks associated
49
+ # with loading, using, modifying and/or developing or reproducing the
50
+ # software by the user in light of its specific status of free software,
51
+ # that may mean that it is complicated to manipulate, and that also
52
+ # therefore means that it is reserved for developers and experienced
53
+ # professionals having in-depth computer knowledge. Users are therefore
54
+ # encouraged to load and test the software's suitability as regards their
55
+ # requirements in conditions enabling the security of their systems and/or
56
+ # data to be ensured and, more generally, to use and operate it in the
57
+ # same conditions as regards security.
58
+ #
59
+ # The fact that you are presently reading this means that you have had
60
+ # knowledge of the CeCILL and CeCILL-C licenses and that you accept its terms.
61
+ #
62
+ */
63
+ #ifndef cimg_plugin_inpaint
64
+ #define cimg_plugin_inpaint
65
+
66
+ template<typename t>
67
+ CImg<T>& inpaint_patch(const CImg<t>& mask, const unsigned int patch_size=11,
68
+ const unsigned int lookup_size=22, const float lookup_factor=1,
69
+ const int lookup_increment=1,
70
+ const unsigned int blend_size=0, const float blend_threshold=0.5f,
71
+ const float blend_decay=0.02, const unsigned int blend_scales=10,
72
+ const bool is_blend_outer=false) {
73
+ if (depth()>1)
74
+ throw CImgInstanceException(_cimg_instance
75
+ "inpaint_patch(): Instance image is volumetric (should be 2d).",
76
+ cimg_instance);
77
+ if (!is_sameXYZ(mask))
78
+ throw CImgArgumentException(_cimg_instance
79
+ "inpaint_patch() : Sizes of instance image and specified mask "
80
+ "(%u,%u,%u,%u) do not match.",
81
+ cimg_instance,
82
+ mask._width,mask._height,mask._depth,mask._spectrum);
83
+ if (!patch_size)
84
+ throw CImgArgumentException(_cimg_instance
85
+ "inpaint_patch() : Specified patch size is 0, must be strictly "
86
+ "positive.",
87
+ cimg_instance);
88
+ if (!lookup_size)
89
+ throw CImgArgumentException(_cimg_instance
90
+ "inpaint_patch() : Specified lookup size is 0, must be strictly "
91
+ "positive.",
92
+ cimg_instance);
93
+ if (lookup_factor<0)
94
+ throw CImgArgumentException(_cimg_instance
95
+ "inpaint_patch() : Specified lookup factor %g is negative, must be "
96
+ "positive.",
97
+ cimg_instance,
98
+ lookup_factor);
99
+ if (!lookup_increment)
100
+ throw CImgArgumentException(_cimg_instance
101
+ "inpaint_patch() : Specified lookup increment is 0, must be "
102
+ "strictly positive.",
103
+ cimg_instance);
104
+ if (blend_decay<0)
105
+ throw CImgArgumentException(_cimg_instance
106
+ "inpaint_patch() : Specified blend decay %g is negative, must be "
107
+ "positive.",
108
+ cimg_instance,
109
+ blend_decay);
110
+
111
+ // Find (dilated by 2) bounding box for the inpainting mask.
112
+ unsigned int xm0 = _width, ym0 = _height, xm1 = 0, ym1 = 0;
113
+ bool is_mask_found = false;
114
+ cimg_forXY(mask,x,y) if (mask(x,y)) {
115
+ is_mask_found = true;
116
+ if (x<(int)xm0) xm0 = (unsigned int)x;
117
+ if (x>(int)xm1) xm1 = (unsigned int)x;
118
+ if (y<(int)ym0) ym0 = (unsigned int)y;
119
+ if (y>(int)ym1) ym1 = (unsigned int)y;
120
+ }
121
+ if (!is_mask_found) return *this;
122
+ xm0 = xm0>2?xm0 - 2:0;
123
+ ym0 = ym0>2?ym0 - 2:0;
124
+ xm1 = xm1<_width - 3?xm1 + 2:_width - 1;
125
+ ym1 = ym1<_height - 3?ym1 + 2:_height - 1;
126
+ int ox = xm0, oy = ym0;
127
+ unsigned int dx = xm1 - xm0 + 1U, dy = ym1 - ym0 + 1U;
128
+
129
+ // Construct normalized version of the mask.
130
+ CImg<ucharT> nmask(dx,dy);
131
+ unsigned char *ptrM = nmask.data();
132
+ cimg_for_inXY(mask,xm0,ym0,xm1,ym1,x,y) *(ptrM++) = mask(x,y)?0:1;
133
+ xm0 = ym0 = 0; xm1 = dx - 1; ym1 = dy - 1;
134
+
135
+ // Start patch filling algorithm.
136
+ const int p2 = (int)patch_size/2, p1 = (int)patch_size - p2 - 1;
137
+ const unsigned int patch_size2 = patch_size*patch_size;
138
+ unsigned int _lookup_size = lookup_size, nb_lookups = 0, nb_fails = 0, nb_saved_patches = 0;
139
+ bool is_strict_search = true;
140
+ const float one = 1;
141
+
142
+ CImg<floatT> confidences(nmask), priorities(dx,dy,1,2,-1), pC;
143
+ CImg<unsigned int> saved_patches(4,256), is_visited(width(),height(),1,1,0);
144
+ CImg<ucharT> pM, pN; // Pre-declare patch variables (avoid iterative memory alloc/dealloc)
145
+ CImg<T> pP, pbest;
146
+ CImg<floatT> weights(patch_size,patch_size,1,1,0);
147
+ weights.draw_gaussian((float)p1,(float)p1,patch_size/15.0f,&one)/=patch_size2;
148
+ unsigned int target_index = 0;
149
+
150
+ while (true) {
151
+
152
+ // Extract mask border points and compute priorities to find target point.
153
+ unsigned int nb_border_points = 0;
154
+ float target_confidence = -1, target_priority = -1;
155
+ int target_x = -1, target_y = -1;
156
+ CImg_5x5(M,unsigned char);
157
+
158
+ cimg_for_in5x5(nmask,xm0,ym0,xm1,ym1,x,y,0,0,M,unsigned char)
159
+ if (!Mcc && (Mcp || Mcn || Mpc || Mnc)) { // Found mask border point
160
+
161
+ float confidence_term = -1, data_term = -1;
162
+ if (priorities(x,y)>=0) { // If priority has already been computed
163
+ confidence_term = priorities(x,y,0);
164
+ data_term = priorities(x,y,1);
165
+ } else { // If priority must be computed/updated
166
+
167
+ // Compute smoothed normal vector.
168
+ const float
169
+ // N = smoothed 3x3 neighborhood of M.
170
+ Npc = (4.0f*Mpc + 2.0f*Mbc + 2.0f*Mcc + 2.0f*Mpp + 2.0f*Mpn + Mbp + Mbn + Mcp + Mcn)/16,
171
+ Nnc = (4.0f*Mnc + 2.0f*Mac + 2.0f*Mcc + 2.0f*Mnp + 2.0f*Mnn + Map + Man + Mcp + Mcn)/16,
172
+ Ncp = (4.0f*Mcp + 2.0f*Mcb + 2.0f*Mcc + 2.0f*Mpp + 2.0f*Mnp + Mpb + Mnb + Mpc + Mnc)/16,
173
+ Ncn = (4.0f*Mcn + 2.0f*Mca + 2.0f*Mcc + 2.0f*Mpn + 2.0f*Mnn + Mpa + Mna + Mpc + Mnc)/16,
174
+ _nx = 0.5f*(Nnc - Npc),
175
+ _ny = 0.5f*(Ncn - Ncp),
176
+ nn = std::sqrt(1e-8f + _nx*_nx + _ny*_ny),
177
+ nx = _nx/nn,
178
+ ny = _ny/nn;
179
+
180
+ // Compute confidence term.
181
+ nmask._inpaint_patch_crop(x - p1,y - p1,x + p2,y + p2,1).move_to(pM);
182
+ confidences._inpaint_patch_crop(x - p1,y - p1,x + p2,y + p2,1).move_to(pC);
183
+ confidence_term = 0;
184
+ const unsigned char *ptrM = pM.data();
185
+ cimg_for(pC,ptrC,float) confidence_term+=*ptrC**(ptrM++);
186
+ confidence_term/=patch_size2;
187
+ priorities(x,y,0) = confidence_term;
188
+
189
+ // Compute data term.
190
+ _inpaint_patch_crop(ox + x - p1,oy + y - p1,ox + x + p2,oy + y + p2,2).move_to(pP);
191
+ float mean_ix2 = 0, mean_ixiy = 0, mean_iy2 = 0;
192
+
193
+ CImg_3x3(I,T);
194
+ CImg_3x3(_M, unsigned char);
195
+ cimg_forC(pP,c) cimg_for3x3(pP,p,q,0,c,I,T) {
196
+ // Compute weight-mean of structure tensor inside patch.
197
+ cimg_get3x3(pM,p,q,0,0,_M,unsigned char);
198
+ const float
199
+ ixf = (float)(_Mnc*_Mcc*(Inc - Icc)),
200
+ iyf = (float)(_Mcn*_Mcc*(Icn - Icc)),
201
+ ixb = (float)(_Mcc*_Mpc*(Icc - Ipc)),
202
+ iyb = (float)(_Mcc*_Mcp*(Icc - Icp)),
203
+ ix = cimg::abs(ixf)>cimg::abs(ixb)?ixf:ixb,
204
+ iy = cimg::abs(iyf)>cimg::abs(iyb)?iyf:iyb,
205
+ w = weights(p,q);
206
+ mean_ix2 += w*ix*ix;
207
+ mean_ixiy += w*ix*iy;
208
+ mean_iy2 += w*iy*iy;
209
+ }
210
+ const float // Compute tensor-directed data term
211
+ ux = mean_ix2*(-ny) + mean_ixiy*nx,
212
+ uy = mean_ixiy*(-ny) + mean_iy2*nx;
213
+ data_term = std::sqrt(ux*ux + uy*uy);
214
+ priorities(x,y,1) = data_term;
215
+ }
216
+ const float priority = confidence_term*data_term;
217
+ if (priority>target_priority) {
218
+ target_priority = priority; target_confidence = confidence_term;
219
+ target_x = ox + x; target_y = oy + y;
220
+ }
221
+ ++nb_border_points;
222
+ }
223
+ if (!nb_border_points) break; // No more mask border points to inpaint!
224
+
225
+ // Locate already reconstructed neighbors (if any), to get good origins for patch lookup.
226
+ CImg<unsigned int> lookup_candidates(2,256);
227
+ unsigned int nb_lookup_candidates = 0, *ptr_lookup_candidates = lookup_candidates.data();
228
+ const unsigned int *ptr_saved_patches = saved_patches.data();
229
+ const int
230
+ x0 = target_x - (int)patch_size, y0 = target_y - (int)patch_size,
231
+ x1 = target_x + (int)patch_size, y1 = target_y + (int)patch_size;
232
+ for (unsigned int k = 0; k<nb_saved_patches; ++k) {
233
+ const unsigned int
234
+ src_x = *(ptr_saved_patches++), src_y = *(ptr_saved_patches++),
235
+ dest_x = *(ptr_saved_patches++), dest_y = *(ptr_saved_patches++);
236
+ if ((int)dest_x>=x0 && (int)dest_y>=y0 && (int)dest_x<=x1 && (int)dest_y<=y1) {
237
+ const int off_x = target_x - dest_x, off_y = target_y - dest_y;
238
+ *(ptr_lookup_candidates++) = src_x + off_x;
239
+ *(ptr_lookup_candidates++) = src_y + off_y;
240
+ if (++nb_lookup_candidates>=lookup_candidates._height)
241
+ lookup_candidates.resize(2,-200,1,1,0);
242
+ }
243
+ }
244
+ // Add also target point as a center for the patch lookup.
245
+ *(ptr_lookup_candidates++) = target_x;
246
+ *(ptr_lookup_candidates++) = target_y;
247
+ ++nb_lookup_candidates;
248
+
249
+ // Divide size of lookup regions if several lookup sources have been detected.
250
+ unsigned int final_lookup_size = _lookup_size;
251
+ if (nb_lookup_candidates>1) {
252
+ const unsigned int
253
+ _final_lookup_size = (unsigned int)cimg::round(_lookup_size*lookup_factor/
254
+ std::sqrt((float)nb_lookup_candidates),1,1);
255
+ final_lookup_size = _final_lookup_size + 1 - (_final_lookup_size%2);
256
+ }
257
+ const int l2 = (int)final_lookup_size/2, l1 = (int)final_lookup_size - l2 - 1;
258
+
259
+ #ifdef inpaint_debug
260
+ CImg<ucharT> visu(*this,false);
261
+ for (unsigned int C = 0; C<nb_lookup_candidates; ++C) {
262
+ const int
263
+ xl = lookup_candidates(0,C),
264
+ yl = lookup_candidates(1,C);
265
+ visu.draw_rectangle(xl - l1,yl - l1,xl + l2,yl + l2,CImg<ucharT>::vector(0,255,0).data(),0.2f);
266
+ }
267
+ visu.draw_rectangle(target_x - p1,target_y - p1,target_x + p2,target_y + p2,
268
+ CImg<ucharT>::vector(255,0,0).data(),0.5f);
269
+ static int foo = 0;
270
+ if (!(foo%1)) {
271
+ // visu.save("video.ppm",foo);
272
+ static CImgDisplay disp_debug;
273
+ disp_debug.display(visu).set_title("DEBUG");
274
+ }
275
+ ++foo;
276
+ #endif // #ifdef inpaint_debug
277
+
278
+ // Find best patch candidate to fill target point.
279
+ _inpaint_patch_crop(target_x - p1,target_y - p1,target_x + p2,target_y + p2,0).move_to(pP);
280
+ nmask._inpaint_patch_crop(target_x - ox - p1,target_y - oy - p1,target_x - ox + p2,target_y - oy + p2,0).
281
+ move_to(pM);
282
+ ++target_index;
283
+ const unsigned int
284
+ _lookup_increment = (unsigned int)(lookup_increment>0?lookup_increment:
285
+ nb_lookup_candidates>1?1:-lookup_increment);
286
+ float best_ssd = cimg::type<float>::max();
287
+ int best_x = -1, best_y = -1;
288
+ for (unsigned int C = 0; C<nb_lookup_candidates; ++C) {
289
+ const int
290
+ xl = (int)lookup_candidates(0,C),
291
+ yl = (int)lookup_candidates(1,C),
292
+ x0 = std::max(p1,xl - l1), y0 = std::max(p1,yl - l1),
293
+ x1 = std::min(width() - 1 - p2,xl + l2), y1 = std::min(height() - 1 - p2,yl + l2);
294
+ for (int y = y0; y<=y1; y+=_lookup_increment)
295
+ for (int x = x0; x<=x1; x+=_lookup_increment) if (is_visited(x,y)!=target_index) {
296
+ if (is_strict_search) mask._inpaint_patch_crop(x - p1,y - p1,x + p2,y + p2,1).move_to(pN);
297
+ else nmask._inpaint_patch_crop(x - ox - p1,y - oy - p1,x - ox + p2,y - oy + p2,0).move_to(pN);
298
+ if ((is_strict_search && pN.sum()==0) || (!is_strict_search && pN.sum()==patch_size2)) {
299
+ _inpaint_patch_crop(x - p1,y - p1,x + p2,y + p2,0).move_to(pC);
300
+ float ssd = 0;
301
+ const T *_pP = pP._data;
302
+ const float *_pC = pC._data;
303
+ cimg_for(pM,_pM,unsigned char) { if (*_pM) {
304
+ cimg_forC(pC,c) {
305
+ ssd+=cimg::sqr((Tfloat)*_pC - (Tfloat)*_pP); _pC+=patch_size2; _pP+=patch_size2;
306
+ }
307
+ if (ssd>=best_ssd) break;
308
+ _pC-=pC._spectrum*patch_size2;
309
+ _pP-=pC._spectrum*patch_size2;
310
+ }
311
+ ++_pC; ++_pP;
312
+ }
313
+ if (ssd<best_ssd) { best_ssd = ssd; best_x = x; best_y = y; }
314
+ }
315
+ is_visited(x,y) = target_index;
316
+ }
317
+ }
318
+
319
+ if (best_x<0) { // If no best patch found
320
+ priorities(target_x - ox,target_y - oy,0)/=10; // Reduce its priority (lower data_term)
321
+ if (++nb_fails>=4) { // If too much consecutive fails :
322
+ nb_fails = 0;
323
+ _lookup_size+=_lookup_size/2; // Try to expand the lookup size
324
+ if (++nb_lookups>=3) {
325
+ if (is_strict_search) { // If still fails, switch to non-strict search mode
326
+ is_strict_search = false;
327
+ _lookup_size = lookup_size;
328
+ nb_lookups = 0;
329
+ }
330
+ else return *this; // Pathological case, probably a weird mask
331
+ }
332
+ }
333
+ } else { // Best patch found -> reconstruct missing part on the target patch
334
+ _lookup_size = lookup_size;
335
+ nb_lookups = nb_fails = 0;
336
+ _inpaint_patch_crop(best_x - p1,best_y - p1,best_x + p2,best_y + p2,0).move_to(pbest);
337
+ nmask._inpaint_patch_crop(target_x - ox - p1,target_y - oy - p1,target_x - ox + p2,target_y - oy + p2,1).
338
+ move_to(pM);
339
+ cimg_for(pM,ptr,unsigned char) *ptr=1 - *ptr;
340
+ draw_image(target_x - p1,target_y - p1,pbest,pM,1,1);
341
+ confidences.draw_image(target_x - ox - p1,target_y - oy - p1,pC.fill(target_confidence),pM,1,1);
342
+ nmask.draw_rectangle(target_x - ox - p1,target_y - oy - p1,0,0,target_x - ox + p2,target_y - oy + p2,0,0,1);
343
+ priorities.draw_rectangle(target_x - ox - (int)patch_size,
344
+ target_y - oy - (int)patch_size,0,0,
345
+ target_x - ox + 3*p2/2,
346
+ target_y - oy + 3*p2/2,0,0,-1);
347
+ // Remember patch positions.
348
+ unsigned int *ptr_saved_patches = saved_patches.data(0,nb_saved_patches);
349
+ *(ptr_saved_patches++) = best_x;
350
+ *(ptr_saved_patches++) = best_y;
351
+ *(ptr_saved_patches++) = target_x;
352
+ *ptr_saved_patches = target_y;
353
+ if (++nb_saved_patches>=saved_patches._height) saved_patches.resize(4,-200,1,1,0);
354
+ }
355
+ }
356
+ nmask.assign(); // Free some unused memory resources
357
+ priorities.assign();
358
+ confidences.assign();
359
+ is_visited.assign();
360
+
361
+ // Blend inpainting result (if requested), using multi-scale blending algorithm.
362
+ if (blend_size && blend_scales) {
363
+ const float _blend_threshold = std::max(0.0f,std::min(1.0f,blend_threshold));
364
+ saved_patches._height = nb_saved_patches;
365
+
366
+ // Re-crop image and mask if outer blending is activated.
367
+ if (is_blend_outer) {
368
+ const int
369
+ b2 = (int)blend_size/2, b1 = (int)blend_size - b2 - 1,
370
+ xb0 = std::max(0,ox - b1),
371
+ yb0 = std::max(0,oy - b1),
372
+ xb1 = std::min(_width - 1,xb0 + dx + b1 + b2),
373
+ yb1 = std::min(_height - 1,yb0 + dy + b1 + b2);
374
+ ox = xb0; oy = yb0; dx = xb1 - xb0 + 1U, dy = yb1 - yb0 + 1U;
375
+ }
376
+
377
+ // Generate map of source offsets.
378
+ CImg<unsigned int> offsets(dx,dy,1,2);
379
+ unsigned int *ptr = saved_patches.end();
380
+ cimg_forY(saved_patches,i) {
381
+ const unsigned int yd = *(--ptr), xd = *(--ptr), ys = *(--ptr), xs = *(--ptr);
382
+ for (int l = -p1; l<=p2; ++l)
383
+ for (int k = -p1; k<=p2; ++k) {
384
+ const int xdk = xd + k, ydl = yd + l;
385
+ if (xdk>=0 && xdk<=width() - 1 && ydl>=0 && ydl<=height() - 1 && mask(xd + k,yd + l)) {
386
+ offsets(xd - ox + k,yd - oy + l,0) = xs + k;
387
+ offsets(xd - ox + k,yd - oy + l,1) = ys + l;
388
+ }
389
+ }
390
+ }
391
+ unsigned int *ptrx = offsets.data(0,0,0,0), *ptry = offsets.data(0,0,0,1);
392
+ cimg_forXY(offsets,x,y) {
393
+ if (!mask(x + ox,y + oy)) { *ptrx = x + ox; *ptry = y + oy; }
394
+ ++ptrx; ++ptry;
395
+ }
396
+
397
+ // Generate map of local blending amplitudes.
398
+ CImg<floatT> blend_map(dx,dy,1,1,0);
399
+ CImg_3x3(I,float);
400
+ cimg_for3XY(offsets,x,y) if (mask(x + ox,y + oy)) {
401
+ const float
402
+ iox = std::max((float)offsets(_n1x,y,0) - offsets(x,y,0),
403
+ (float)offsets(x,y,0) - offsets(_p1x,y,0)),
404
+ ioy = std::max((float)offsets(x,_n1y,1) - offsets(x,y,1),
405
+ (float)offsets(x,y,1) - offsets(x,_p1y,1)),
406
+ ion = std::sqrt(iox*iox + ioy*ioy);
407
+ float iin = 0;
408
+ cimg_forC(*this,c) {
409
+ cimg_get3x3(*this,x,y,0,c,I,float);
410
+ const float
411
+ iix = (float)std::max(Inc - Icc,Icc - Ipc),
412
+ iiy = (float)std::max(Icn - Icc,Icc - Icp);
413
+ iin+=std::log(1 + iix*iix + iiy*iiy);
414
+ }
415
+ iin/=_spectrum;
416
+ blend_map(x,y) = ion*iin;
417
+ }
418
+ blend_map.threshold(blend_map.max()*_blend_threshold).distance(1);
419
+ cimg_forXY(blend_map,x,y) blend_map(x,y) = 1/(1 + blend_decay*blend_map(x,y));
420
+ blend_map.quantize(blend_scales + 1,false);
421
+ float bm, bM = blend_map.max_min(bm);
422
+ if (bm==bM) blend_map.fill((float)blend_scales);
423
+
424
+ // Generate blending scales.
425
+ CImg<T> result = _inpaint_patch_crop(ox,oy,ox + dx - 1,oy + dy - 1,0);
426
+ for (unsigned int blend_iter = 1; blend_iter<=blend_scales; ++blend_iter) {
427
+ const unsigned int
428
+ _blend_width = blend_iter*blend_size/blend_scales,
429
+ blend_width = _blend_width?_blend_width + 1 - (_blend_width%2):0;
430
+ if (!blend_width) continue;
431
+ const int b2 = (int)blend_width/2, b1 = (int)blend_width - b2 - 1;
432
+ CImg<floatT>
433
+ blended = _inpaint_patch_crop(ox,oy,ox + dx - 1,oy + dy - 1,0),
434
+ cumul(dx,dy,1,1);
435
+ weights.assign(blend_width,blend_width,1,1,0).
436
+ draw_gaussian((float)b1,(float)b1,blend_width/4.0f,&one);
437
+ cimg_forXY(cumul,x,y) cumul(x,y) = mask(x + ox,y + oy)?0.0f:1.0f;
438
+ blended.mul(cumul);
439
+
440
+ cimg_forY(saved_patches,l) {
441
+ const unsigned int *ptr = saved_patches.data(0,l);
442
+ const int
443
+ xs = (int)*(ptr++),
444
+ ys = (int)*(ptr++),
445
+ xd = (int)*(ptr++),
446
+ yd = (int)*(ptr++);
447
+ if (xs - b1<0 || ys - b1<0 || xs + b2>=width() || ys + b2>=height()) { // Blend with partial patch
448
+ const int
449
+ xs0 = std::max(0,xs - b1),
450
+ ys0 = std::max(0,ys - b1),
451
+ xs1 = std::min(width() - 1,xs + b2),
452
+ ys1 = std::min(height() - 1,ys + b2);
453
+ _inpaint_patch_crop(xs0,ys0,xs1,ys1,0).move_to(pP);
454
+ weights._inpaint_patch_crop(xs0 - xs + b1,ys0 - ys + b1,xs1 - xs + b1,ys1 - ys + b1,0).move_to(pC);
455
+ blended.draw_image(xd + xs0 - xs - ox,yd + ys0 - ys - oy,pP,pC,-1);
456
+ cumul.draw_image(xd + xs0 - xs - ox,yd + ys0 - ys - oy,pC,-1);
457
+ } else { // Blend with full-size patch
458
+ _inpaint_patch_crop(xs - b1,ys - b1,xs + b2,ys + b2,0).move_to(pP);
459
+ blended.draw_image(xd - b1 - ox,yd - b1 - oy,pP,weights,-1);
460
+ cumul.draw_image(xd - b1 - ox,yd - b1 - oy,weights,-1);
461
+ }
462
+ }
463
+
464
+ if (is_blend_outer) {
465
+ cimg_forXY(blended,x,y) if (blend_map(x,y)==blend_iter) {
466
+ const float cum = cumul(x,y);
467
+ if (cum>0) cimg_forC(*this,c) result(x,y,c) = (T)(blended(x,y,c)/cum);
468
+ }
469
+ } else { cimg_forXY(blended,x,y) if (mask(x + ox,y + oy) && blend_map(x,y)==blend_iter) {
470
+ const float cum = cumul(x,y);
471
+ if (cum>0) cimg_forC(*this,c) result(x,y,c) = (T)(blended(x,y,c)/cum);
472
+ }
473
+ }
474
+ }
475
+ if (is_blend_outer) draw_image(ox,oy,result);
476
+ else cimg_forXY(result,x,y) if (mask(x + ox,y + oy))
477
+ cimg_forC(*this,c) (*this)(x + ox,y + oy,c) = (T)result(x,y,c);
478
+ }
479
+ return *this;
480
+ }
481
+
482
+ // Special crop function that supports more boundary conditions :
483
+ // 0=dirichlet (with value 0), 1=dirichlet (with value 1) and 2=neumann.
484
+ CImg<T> _inpaint_patch_crop(const int x0, const int y0, const int x1, const int y1,
485
+ const unsigned int boundary=0) const {
486
+ const int
487
+ nx0 = x0<x1?x0:x1, nx1 = x0^x1^nx0,
488
+ ny0 = y0<y1?y0:y1, ny1 = y0^y1^ny0;
489
+ CImg<T> res(1U + nx1 - nx0,1U + ny1 - ny0,1,_spectrum);
490
+ if (nx0<0 || nx1>=width() || ny0<0 || ny1>=height()) {
491
+ if (boundary>=2) cimg_forXYZC(res,x,y,z,c) res(x,y,z,c) = _atXY(nx0 + x,ny0 + y,z,c);
492
+ else res.fill((T)boundary).draw_image(-nx0,-ny0,*this);
493
+ } else res.draw_image(-nx0,-ny0,*this);
494
+ return res;
495
+ }
496
+
497
+ template<typename t>
498
+ CImg<T> get_inpaint_patch(const CImg<t>& mask, const unsigned int patch_size=11,
499
+ const unsigned int lookup_size=22, const float lookup_factor=1,
500
+ const int lookup_increment=1,
501
+ const unsigned int blend_size=0, const float blend_threshold=0.5,
502
+ const float blend_decay=0.02f, const unsigned int blend_scales=10,
503
+ const bool is_blend_outer=false) const {
504
+ return (+*this).inpaint_patch(mask,patch_size,lookup_size,lookup_factor,lookup_increment,
505
+ blend_size,blend_threshold,blend_decay,blend_scales,is_blend_outer);
506
+ }
507
+
508
+ #endif /* cimg_plugin_inpaint */