UnsharpMask returns a copy of the image with its high-frequency components amplified. Parameter radius corresponds to the radius to be samples per pixel. Parameter amount is the normalized strength of the effect. A value of 0.0 will leave the image untouched and a value of 1.0 will fully apply the u
(img image.Image, radius, amount float64)
| 150 | // Parameter amount is the normalized strength of the effect. A value of 0.0 will leave |
| 151 | // the image untouched and a value of 1.0 will fully apply the unsharp mask. |
| 152 | func UnsharpMask(img image.Image, radius, amount float64) *image.RGBA { |
| 153 | amount = f64.Clamp(amount, 0, 10) |
| 154 | |
| 155 | blurred := blur.Gaussian(img, 5*radius) // scale radius by matching factor |
| 156 | |
| 157 | bounds := img.Bounds() |
| 158 | src := clone.AsRGBA(img) |
| 159 | dst := image.NewRGBA(bounds) |
| 160 | w, h := bounds.Dx(), bounds.Dy() |
| 161 | |
| 162 | parallel.Line(h, func(start, end int) { |
| 163 | for y := start; y < end; y++ { |
| 164 | for x := 0; x < w; x++ { |
| 165 | pos := y*dst.Stride + x*4 |
| 166 | |
| 167 | r := float64(src.Pix[pos+0]) |
| 168 | g := float64(src.Pix[pos+1]) |
| 169 | b := float64(src.Pix[pos+2]) |
| 170 | a := float64(src.Pix[pos+3]) |
| 171 | |
| 172 | rBlur := float64(blurred.Pix[pos+0]) |
| 173 | gBlur := float64(blurred.Pix[pos+1]) |
| 174 | bBlur := float64(blurred.Pix[pos+2]) |
| 175 | aBlur := float64(blurred.Pix[pos+3]) |
| 176 | |
| 177 | r = r + (r-rBlur)*amount |
| 178 | g = g + (g-gBlur)*amount |
| 179 | b = b + (b-bBlur)*amount |
| 180 | a = a + (a-aBlur)*amount |
| 181 | |
| 182 | dst.Pix[pos+0] = uint8(f64.Clamp(r, 0, 255)) |
| 183 | dst.Pix[pos+1] = uint8(f64.Clamp(g, 0, 255)) |
| 184 | dst.Pix[pos+2] = uint8(f64.Clamp(b, 0, 255)) |
| 185 | dst.Pix[pos+3] = uint8(f64.Clamp(a, 0, 255)) |
| 186 | } |
| 187 | } |
| 188 | }) |
| 189 | |
| 190 | return dst |
| 191 | } |
| 192 | |
| 193 | // Sobel returns an image emphasising edges using an approximation to the Sobel–Feldman operator. |
| 194 | func Sobel(src image.Image) *image.RGBA { |