From a4746a3e730531f5ebec49b684d9737ccb2a4f3b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 28 Oct 2025 15:00:48 -0400 Subject: [PATCH] Added the start of a simple posterize effect. --- filters/posterize.go | 43 +++++++++++++++++++++++++++++++++++++++++++ filters/upscale.go | 2 +- main.go | 5 +++-- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 filters/posterize.go diff --git a/filters/posterize.go b/filters/posterize.go new file mode 100644 index 0000000..98e2ecd --- /dev/null +++ b/filters/posterize.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) + } + } +} diff --git a/filters/upscale.go b/filters/upscale.go index 4c8e878..712865f 100644 --- a/filters/upscale.go +++ b/filters/upscale.go @@ -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} } diff --git a/main.go b/main.go index 43716f6..0a2dbc9 100644 --- a/main.go +++ b/main.go @@ -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)