From 42431a756e6b9b0bcf97d8e230158fc80c22f2f5 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 30 Oct 2025 10:57:41 -0400 Subject: [PATCH] Almost working. Only thing is the color is off when indexed. --- config.json | 4 +-- processing.go | 70 ++++++++++++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/config.json b/config.json index 53c85e5..8faf9cb 100644 --- a/config.json +++ b/config.json @@ -10,8 +10,8 @@ "Target": "temp", "Include": [ ".png", ".jpg" ], "Exceptions": { - "temp/painted_*.png": { - "PixelWidth": 8, + "images/painted_.*.png": { + "PixelWidth": 2, "ColorDepth": 16, "DitherType": 2, "Width": 256, diff --git a/processing.go b/processing.go index 85ccd3d..a97f5eb 100644 --- a/processing.go +++ b/processing.go @@ -41,15 +41,36 @@ func SaveImage(filename string, img image.Image) { func JankImage(settings Conversion, in_file string, out_file string) { src := LoadImage(in_file) - bounds := src.Bounds() + src_bounds := src.Bounds() + target_bounds := src_bounds - resize := gift.Resize(bounds.Max.X / settings.PixelWidth, 0, gift.NearestNeighborResampling) + if settings.Width > 0 { + target_bounds.Max.X = settings.Width + } + + if settings.Height > 0 { + target_bounds.Max.Y = settings.Height + } + + fmt.Println("final size is: ", target_bounds.Max.X, target_bounds.Max.Y) + + // only done when shrinking/growing + presize := gift.Resize(target_bounds.Max.X, target_bounds.Max.Y, gift.NearestNeighborResampling) + + resize := gift.Resize(target_bounds.Max.X / settings.PixelWidth, 0, gift.NearestNeighborResampling) posterize := filters.Posterize(uint16(settings.ColorDepth), settings.DitherType) - upscale := filters.Upscale(bounds, settings.PixelWidth) + upscale := filters.Upscale(target_bounds, settings.PixelWidth) sharpen := gift.UnsharpMask(1, 1, 0) - g := gift.New(resize, posterize, upscale, sharpen) - out_img := image.NewNRGBA(g.Bounds(bounds)) + var g *gift.GIFT + + if target_bounds.Max.X != src_bounds.Max.X || target_bounds.Max.Y != src_bounds.Max.Y { + g = gift.New(presize, resize, posterize, upscale, sharpen) + } else { + g = gift.New(resize, posterize, upscale, sharpen) + } + + out_img := image.NewNRGBA(g.Bounds(target_bounds)) g.Draw(out_img, src) SaveImage(out_file, out_img) @@ -113,6 +134,7 @@ func Included(config Settings, path string) bool { } func GetConversion(config Settings, path string) Conversion { + path = RePrefixPath(path, "") for key, conversion := range config.Exceptions { match, err := regexp.MatchString(key, path) if err != nil { log.Fatalf("problem matching regex %s: %v", key, err) } @@ -141,30 +163,32 @@ func HasChanged(old_info fs.FileInfo, new_path string) bool { func RenderImages(config Settings, force bool) error { err := filepath.WalkDir(config.Source, func(path string, d fs.DirEntry, err error) error { - if !d.IsDir() && Included(config, path) { - target := RePrefixPath(path, config.Target) + path = filepath.ToSlash(path) - err = MkdirPath(target) - if err != nil { - log.Fatalf("failed to make path %s: %v", path, err) - } + if !d.IsDir() && Included(config, path) { + target := RePrefixPath(path, config.Target) - fmt.Println("FILE: ", path, "TARGET:", target) - convert := GetConversion(config, path) + err = MkdirPath(target) + if err != nil { + log.Fatalf("failed to make path %s: %v", path, err) + } - path_info, err := d.Info() - if err != nil { - log.Fatalf("failed to stat %s: %v", path, err) - } + fmt.Println("FILE: ", path, "TARGET:", target) + convert := GetConversion(config, path) + + path_info, err := d.Info() + if err != nil { + log.Fatalf("failed to stat %s: %v", path, err) + } - if force || HasChanged(path_info, target) { - JankImage(convert, path, target) + if force || HasChanged(path_info, target) { + JankImage(convert, path, target) + } + } else { + fmt.Println("SKIP:", path) } - } else { - fmt.Println("SKIP:", path) - } - return nil + return nil }) return err