func runBlur(cmd *hadfield.Command, args []string) { var style blur.Style switch blurStyle { case "clamp": style = blur.CLAMP case "ignore": style = blur.IGNORE case "wrap": style = blur.WRAP default: utils.Warn("--style must be one of 'clamp', 'ignore' or 'wrap'") os.Exit(2) } i, data := utils.ReadStdin() if blurBox { i = blur.Box(i, blurRadius, style) } else { i = blur.Gaussian(i, blurRadius, blurGaussian, style) } utils.WriteStdout(i, data) }
// UnsharpMask sharpens the given Image using the unsharp mask technique. // Basically the image is blurred, then subtracted from the original for // differences above the threshold value. func UnsharpMask(in image.Image, radius int, sigma, amount, threshold float64) image.Image { blurred := blur.Gaussian(in, radius, sigma, blur.IGNORE) bounds := in.Bounds() out := image.NewRGBA(bounds) // Absolute difference between a and b, returns float64 between 0 and 1. diff := func(a, b float64) float64 { if a > b { return a - b } return b - a } for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for x := bounds.Min.X; x < bounds.Max.X; x++ { ar, ag, ab, aa := utils.RatioRGBA(in.At(x, y)) br, bg, bb, _ := utils.RatioRGBA(blurred.At(x, y)) if diff(ar, br) >= threshold { ar = amount*(ar-br) + ar } if diff(ag, bg) >= threshold { ag = amount*(ag-bg) + ag } if diff(ab, bb) >= threshold { ab = amount*(ab-bb) + ab } out.Set(x, y, color.NRGBA{ uint8(utils.Truncatef(ar * 255)), uint8(utils.Truncatef(ag * 255)), uint8(utils.Truncatef(ab * 255)), uint8(aa * 255), }) } } return out }