Added the start of a simple posterize effect.

master
Zed A. Shaw 1 month ago
parent 10e13c033d
commit a4746a3e73
  1. 43
      filters/posterize.go
  2. 2
      filters/upscale.go
  3. 5
      main.go

@ -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)
}
}
}

@ -12,7 +12,7 @@ type UpscaleFilter struct {
PixelWidth int
}
func UpscaleImage(size image.Rectangle, width int) gift.Filter {
func Upscale(size image.Rectangle, width int) gift.Filter {
return UpscaleFilter{size, width}
}

@ -58,9 +58,10 @@ func main() {
bounds := src.Bounds()
resize := gift.Resize(bounds.Max.X / opts.PixelWidth, 0, gift.NearestNeighborResampling)
upscale := filters.UpscaleImage(bounds, opts.PixelWidth)
posterize := filters.Posterize(16)
upscale := filters.Upscale(bounds, opts.PixelWidth)
g := gift.New(resize, upscale)
g := gift.New(posterize, resize, upscale)
smaller := image.NewNRGBA(g.Bounds(bounds))
g.Draw(smaller, src)

Loading…
Cancel
Save