lib/handsum: add Potato, Gray variants

This commit is contained in:
Nigel Tao committed 2026-04-12 15:56:00 +10:00
1 parent 50b62c7704
commit cbdc4d5f64
2 files changed
+300 -68

No files matched your search

+32 -6
View File
@@ -17,9 +17,11 @@ import (
"bytes"
"errors"
"flag"
"fmt"
"image"
"image/png"
"os"
"strings"
"github.com/google/wuffs/lib/handsum"
@@ -35,6 +37,7 @@ var (
decodeFlag = flag.Bool("decode", false, "whether to decode the input")
encodeFlag = flag.Bool("encode", false, "whether to encode the input")
roundtripFlag = flag.Bool("roundtrip", false, "whether to encode-and-decode the input")
variantFlag = flag.String("variant", "hvpc", "which variant to encode to: hvpc, hvpg, hvxc, hvxg")
)
const usageStr = `handsum decodes and encodes the Handsum lossy image file format.
@@ -52,6 +55,15 @@ The output image (in Handsum or PNG format) is written to stdout.
Decode inputs Handsum and outputs PNG.
Encode inputs BMP, GIF, JPEG, PNG, TIFF or WEBP and outputs Handsum.
Roundtrip is equivalent to encode (to an ephemeral file) and then decode.
For encode or roundtrip, you can also choose a quality and color variant
-variant=hvpc to use Handsum Variant Potato Color (the default)
-variant=hvpg to use Handsum Variant Potato Gray
-variant=hvxc to use Handsum Variant ExtremelyPotato Color
-variant=hvxg to use Handsum Variant ExtremelyPotato Gray
For example: handsum -encode -variant=hvpg foo.png > foo.hvpg.handsum
`
func main() {
@@ -65,6 +77,20 @@ func main1() error {
flag.Usage = func() { os.Stderr.WriteString(usageStr) }
flag.Parse()
variant := handsum.Variant(0)
switch strings.ToLower(*variantFlag) {
case "hvpc":
variant = handsum.VariantPotatoColor
case "hvpg":
variant = handsum.VariantPotatoGray
case "hvxc":
variant = handsum.VariantExtremelyPotatoColor
case "hvxg":
variant = handsum.VariantExtremelyPotatoGray
default:
return fmt.Errorf("bad -variant flag value %q", *variantFlag)
}
inFile := os.Stdin
switch flag.NArg() {
case 0:
@@ -84,10 +110,10 @@ func main1() error {
return decode(inFile)
}
if !*decodeFlag && *encodeFlag && !*roundtripFlag {
return encode(inFile)
return encode(inFile, variant)
}
if !*decodeFlag && !*encodeFlag && *roundtripFlag {
return roundtrip(inFile)
return roundtrip(inFile, variant)
}
return errors.New("must specify exactly one of -decode, -encode, -roundtrip or -help")
}
@@ -100,21 +126,21 @@ func decode(inFile *os.File) error {
return png.Encode(os.Stdout, src)
}
func encode(inFile *os.File) error {
func encode(inFile *os.File, variant handsum.Variant) error {
src, _, err := image.Decode(inFile)
if err != nil {
return err
}
return handsum.Encode(os.Stdout, src, nil)
return handsum.Encode(os.Stdout, src, &handsum.EncodeOptions{Variant: variant})
}
func roundtrip(inFile *os.File) error {
func roundtrip(inFile *os.File, variant handsum.Variant) error {
src, _, err := image.Decode(inFile)
if err != nil {
return err
}
buf := &bytes.Buffer{}
err = handsum.Encode(buf, src, nil)
err = handsum.Encode(buf, src, &handsum.EncodeOptions{Variant: variant})
if err != nil {
return err
}