There’s some similar stuff here to my “Tone Enhance” filter in G’MIC (bilateral + tone masking), so perhaps it’s useful to show a breakdown of how the gamma adjustment part functions. It can possibly be simplified further.

Edit: I must point out the example here is from a quite heavily compressed “sample” image in G’MIC.

- Median then bilateral the image, convert to luma then split to 3 tones

- Create a “gamma mask” from that with the user supplied values (an exponent value per pixel)

- Apply it (raise to that exponent), example here uses gammas 2,0.8,0.5 for shadow, mid, highlight

G’MIC code stripped down to those parts:

```
gcd_tone_simple : skip ${1=1},${2=1},${3=1},${4=128}
repeat $! l[$>]
+n 0,255 median. 3 bilateral. 2%,30 gcd_srgb2luma.
sub. $4 +abs. negate.
+max.. 0 min... 0 abs...
mul... {if(iM#-3>0,(1/$1-1)/iM#-3,1)}
mul.. {if(iM#-2>0,(1/$2-1)/iM#-2,1)}
mul. {if(iM#-1>0,(1/$3-1)/iM#-1,1)}
add[-3--1] 1 mul[-3--1]
m={0,iM} div[0] $m pow[0,-1] mul[0] $m
endl done
```

The main thing I wanted to show is that G’MIC is very good for prototyping this sort of thing (there’s also a “guided” filter in G’MIC).