parent
10e13c033d
commit
a4746a3e73
@ -0,0 +1,43 @@ |
|||||||
|
package filters |
||||||
|
|
||||||
|
import ( |
||||||
|
"image" |
||||||
|
"image/draw" |
||||||
|
"image/color" |
||||||
|
"github.com/disintegration/gift" |
||||||
|
) |
||||||
|
|
||||||
|
type PosterizeFilter struct { |
||||||
|
Depth uint8 |
||||||
|
} |
||||||
|
|
||||||
|
func Posterize(depth uint8) gift.Filter { |
||||||
|
return PosterizeFilter{depth} |
||||||
|
} |
||||||
|
|
||||||
|
func (filter PosterizeFilter) Bounds(src image.Rectangle) (dst_bounds image.Rectangle) { |
||||||
|
return src |
||||||
|
} |
||||||
|
|
||||||
|
func bin(c uint8) uint8 { |
||||||
|
if c <= 64 { |
||||||
|
return 0 |
||||||
|
} else { |
||||||
|
return 255 |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (filter PosterizeFilter) Draw(dst draw.Image, src image.Image, _ *gift.Options) { |
||||||
|
bounds := src.Bounds() |
||||||
|
|
||||||
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { |
||||||
|
for x := bounds.Min.X; x < bounds.Max.X; x++ { |
||||||
|
from := src.At(x, y) |
||||||
|
c := from.(color.NRGBA) |
||||||
|
c.R = bin(c.R) |
||||||
|
c.G = bin(c.G) |
||||||
|
c.B = bin(c.B) |
||||||
|
dst.Set(x, y, c) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue