Started a filters package to place my filters. First one is upscale.

master
Zed A. Shaw 1 month ago
parent d738eca230
commit 10e13c033d
  1. 39
      filters/upscale.go
  2. 31
      main.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)
}
}
}

@ -1,15 +1,13 @@
package main package main
import ( import (
// "fmt"
"os" "os"
"image" "image"
"log" "log"
"image/png" "image/png"
"image/color"
"github.com/disintegration/gift" "github.com/disintegration/gift"
// "math"
"flag" "flag"
"lcthw.dev/go/jankifier/filters"
) )
func LoadImage(filename string) image.Image { 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 { type Opts struct {
InFile string InFile string
OutFile string OutFile string
@ -80,12 +58,11 @@ func main() {
bounds := src.Bounds() bounds := src.Bounds()
resize := gift.Resize(bounds.Max.X / opts.PixelWidth, 0, gift.NearestNeighborResampling) 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)) smaller := image.NewNRGBA(g.Bounds(bounds))
g.Draw(smaller, src) g.Draw(smaller, src)
upscale := UpscaleImage(bounds, smaller, opts.PixelWidth) SaveImage(opts.OutFile, smaller)
SaveImage(opts.OutFile, upscale)
} }

Loading…
Cancel
Save