G'MIC exercises

After seeing a mention of pyramid based inpainting in a darktable highlights recovery thread

I thought I’d give it a try in g’mic. Some “insiders” have seen a version already, but it had a problem: upscaling with the resize command in g’mic - in bilinear mode - is not pixel aligned with a downscale. I’ve hopefully solved this by using a 2x2 box filter (that’s the convolve part) after nearest-neighbour scaling.

gcd_pyramid_inpaint : check ${"is_image_arg $1"}
  pass$1 0 ge. 1 store. msk
  repeat $! l[$>]
    wh={[w,h]-1}
    L={ceil(log(max(w,h))/log(2))} # find required number of levels
    i $msk z 0,0,{D=2^$L;[D,D]-1},1 # force 2^n dimensions
    eq. 0 mul.. .
    repeat $L I,M={[$>*2,$>*2+1]}
      +r[$I,$M] 50%,50%,100%,100%,2
      +eq. 0 add. .. div[-3,-1] gt. 0
    done
    i[0] (0.0625,0.125,0.0625;0.125,0.25,0.125;0.0625,0.125,0.0625)
    repeat $L
      l[0,-4--1] rm.
        r. [1],[1],[1],[1],1 convolve. [0]
        eq[2] 0 j[1] .,0,0,0,0,1,[2] rm.
      endl
    done
    rm[0,-1] z 0,0,$wh # restore dimensions
  endl done

gmic sp leaf 100%,100%,100%,1 ellipse. 50%,50%,"{[w/4,h/3]}",0,1,1 negate. gcd_pyramid_inpaint.. .
leafinpaint

On this laptop, it takes 0.072s

2 Likes