From e7dd8f6520c72a6571d9adda02c3b9a5df7dee05 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 10 May 2026 21:17:50 +1000 Subject: [PATCH] script: add handsum-tilify.go --- script/handsum-tilify.go | 73 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 script/handsum-tilify.go diff --git a/script/handsum-tilify.go b/script/handsum-tilify.go new file mode 100644 index 00000000..8d40efe7 --- /dev/null +++ b/script/handsum-tilify.go @@ -0,0 +1,73 @@ +// Copyright 2026 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +// +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//go:build ignore +// +build ignore + +package main + +// handsum-tilify.go decodes JPEG or PNG from stdin, partitions it into a grid +// of 16×16 tiles, round-trips each tile through the Handsum codec and writes +// the resultant PNG to stdout. +// +// Usage: go run handsum-tilify.go < input.png > output.png + +import ( + "bytes" + "flag" + "image" + "image/draw" + "image/png" + "os" + + "github.com/google/wuffs/lib/handsum" + + _ "image/jpeg" +) + +var qFlag = flag.Int("q", 4, "Encoding quality: 1, 2, 3 or 4 (default)") + +func main() { + if err := main1(); err != nil { + os.Stderr.WriteString(err.Error() + "\n") + os.Exit(1) + } +} + +func main1() error { + flag.Parse() + quality := handsum.Quality(max(1, min(4, *qFlag))) + + src0, _, err := image.Decode(os.Stdin) + if err != nil { + return err + } + bounds := src0.Bounds() + src1, _ := src0.(interface { + SubImage(image.Rectangle) image.Image + }) + dst := image.NewRGBA(bounds) + buf := &bytes.Buffer{} + + for y := bounds.Min.Y; y < bounds.Max.Y; y += 16 { + for x := bounds.Min.X; x < bounds.Max.X; x += 16 { + buf.Reset() + rect := image.Rect(x, y, x+16, y+16) + tile := src1.SubImage(rect) + handsum.Encode(buf, tile, &handsum.EncodeOptions{ + Quality: quality, + }) + roundTripped, _ := handsum.Decode(buf) + draw.Draw(dst, rect, roundTripped, roundTripped.Bounds().Min, draw.Src) + } + } + + return png.Encode(os.Stdout, dst) +}