From 10e13c033d7ffb18f2c20ad8cbcd4ff65f95f57c Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 28 Oct 2025 14:15:41 -0400 Subject: [PATCH] Started a filters package to place my filters. First one is upscale. --- filters/upscale.go | 39 +++++++++++++++++++++++++++++++++++++++ main.go | 31 ++++--------------------------- 2 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 filters/upscale.go diff --git a/filters/upscale.go b/filters/upscale.go new file mode 100644 index 0000000..4c8e878 --- /dev/null +++ b/filters/upscale.go @@ -0,0 +1,39 @@ +package filters + +import ( + "image" + "image/draw" + "image/color" + "github.com/disintegration/gift" +) + +type UpscaleFilter struct { + Size image.Rectangle + PixelWidth int +} + +func UpscaleImage(size image.Rectangle, width int) gift.Filter { + return UpscaleFilter{size, width} +} + +func (filter UpscaleFilter) Bounds(src image.Rectangle) (dst_bounds image.Rectangle) { + return filter.Size +} + +func (filter UpscaleFilter) Draw(dst draw.Image, src image.Image, _ *gift.Options) { + var from_x, from_y int + var from color.Color + bounds := filter.Size + + for y := bounds.Min.Y; y < bounds.Max.Y; y++ { + for x := bounds.Min.X; x < bounds.Max.X; x++ { + if x % filter.PixelWidth == 0 { + from_x = x / filter.PixelWidth + from_y = y / filter.PixelWidth + from = src.At(from_x, from_y) + } + + dst.Set(x, y, from) + } + } +} diff --git a/main.go b/main.go index 05e4606..43716f6 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,13 @@ package main import ( - // "fmt" "os" "image" "log" "image/png" - "image/color" "github.com/disintegration/gift" - // "math" "flag" + "lcthw.dev/go/jankifier/filters" ) func LoadImage(filename string) image.Image { @@ -36,26 +34,6 @@ func SaveImage(filename string, img image.Image) { } } -func UpscaleImage(bounds image.Rectangle, smaller *image.NRGBA, pixel_width int) *image.NRGBA { - upscale := image.NewNRGBA(bounds) - var from_x, from_y int - var from color.Color - - for y := bounds.Min.Y; y < bounds.Max.Y; y++ { - for x := bounds.Min.X; x < bounds.Max.X; x++ { - if x % pixel_width == 0 { - from_x = x / pixel_width - from_y = y / pixel_width - from = smaller.At(from_x, from_y) - } - - upscale.Set(x, y, from) - } - } - - return upscale -} - type Opts struct { InFile string OutFile string @@ -80,12 +58,11 @@ func main() { bounds := src.Bounds() resize := gift.Resize(bounds.Max.X / opts.PixelWidth, 0, gift.NearestNeighborResampling) + upscale := filters.UpscaleImage(bounds, opts.PixelWidth) - g := gift.New(resize) + g := gift.New(resize, upscale) smaller := image.NewNRGBA(g.Bounds(bounds)) g.Draw(smaller, src) - upscale := UpscaleImage(bounds, smaller, opts.PixelWidth) - - SaveImage(opts.OutFile, upscale) + SaveImage(opts.OutFile, smaller) }