cmd/handsum: append '\n' to base64 output
diff --git a/cmd/handsum/main.go b/cmd/handsum/main.go index 84842d8..980fb91 100644 --- a/cmd/handsum/main.go +++ b/cmd/handsum/main.go
@@ -203,6 +203,7 @@ if b64e != nil { enc := b64e.AppendEncode(nil, buf.Bytes()) + enc = append(enc, '\n') if _, err := w.Write(enc); err != nil { return err }
diff --git a/lib/handsum/example_test.go b/lib/handsum/example_test.go new file mode 100644 index 0000000..8d5da05 --- /dev/null +++ b/lib/handsum/example_test.go
@@ -0,0 +1,71 @@ +// Copyright 2026 The Wuffs Authors. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, 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 + +package handsum_test + +import ( + "bytes" + "encoding/base64" + "fmt" + "image/png" + "log" + "os" + + "github.com/google/wuffs/lib/handsum" +) + +func Example_decode() { + srcBytes, err := base64.URLEncoding.DecodeString("" + + "_tdrdX33KcrhdVU1hFi3h2mtY0qqd2dswzeX9jmJckmUxD15c1Vqk1iGdjmYoknlZDl5dmmZ" + + "xEczhliahXRDpC3JFoiItjAtQ2vJpjBsUziLiXivgimZqDq6skhwhkxIsimmragKTLa-_Hmn" + + "C5rf_acAvXR4zebYyq-Qc2SVtVnIA_mX9zVlEmn_Ul9pIzwHaFBb") + if err != nil { + log.Fatalf("base64.URLEncoding.DecodeString: %v", err) + } + + img, err := handsum.Decode(bytes.NewReader(srcBytes)) + if err != nil { + log.Fatalf("handsum.Decode: %v", err) + } + fmt.Printf("%T %v\n", img, img.Bounds()) + + // Output: + // *image.RGBA (0,0)-(16,12) +} + +func Example_encode() { + f, err := os.Open("../../test/data/bricks-color.png") + if err != nil { + log.Fatalf("os.Open: %v", err) + } + defer f.Close() + + srcImage, err := png.Decode(f) + if err != nil { + log.Fatalf("png.Decode: %v", err) + } + + buf := &bytes.Buffer{} + err = handsum.Encode(buf, srcImage, nil) + if err != nil { + log.Fatalf("handsum.Encode: %v", err) + } + + s := base64.URLEncoding.EncodeToString(buf.Bytes()) + for ; len(s) > 72; s = s[72:] { + fmt.Println(s[:72]) + } + fmt.Println(s) + + // Output: + // _tdrdX33KcrhdVU1hFi3h2mtY0qqd2dswzeX9jmJckmUxD15c1Vqk1iGdjmYoknlZDl5dmmZ + // xEczhliahXRDpC3JFoiItjAtQ2vJpjBsUziLiXivgimZqDq6skhwhkxIsimmragKTLa-_Hmn + // C5rf_acAvXR4zebYyq-Qc2SVtVnIA_mX9zVlEmn_Ul9pIzwHaFBb +}
diff --git a/lib/handsum/handsum.go b/lib/handsum/handsum.go index ea8933f..2a964bb 100644 --- a/lib/handsum/handsum.go +++ b/lib/handsum/handsum.go
@@ -47,9 +47,9 @@ // 4.59375 bits per pixel for a 16×16 pixel image (a 1:1 aspect ratio), or // 6.125 bits per pixel for a 16×12 pixel image (a 4:3 aspect ratio). // -// A Handsum file's starts with a 3 byte header: a 15-bit magic signature, a -// 2-bit color (Gray, RGB or RGBA), a 2-bit quality and a 5-bit aspect ratio. -// An image's longest dimension (width or height) is 16 pixels and the aspect +// Handsum files start with a 3 byte header: a 15-bit magic signature, a 2-bit +// color (Gray, RGB or RGBA), a 2-bit quality and a 5-bit aspect ratio. An +// image's longest dimension (width or height) is 16 pixels and the aspect // ratio gives the shorter dimension. // // The color=RGB payload, after the header, holds a scaled 16×16 pixel YCbCr @@ -79,10 +79,11 @@ // // All Handsum images use the sRGB color profile and non-premultiplied alpha. // -// The "Handsum" name was inspired by the "Thumbhash" image file format, which -// is also designed for very small thumbnails (or very compact representations -// of image placeholders). Handsum files are bigger (but better quality) than -// Thumbhash. "Handsum" also sounds like "handsome", meaning "good looking". +// The "Handsum" name was inspired by the "Thumbhash" image file format (which +// was in turn inspired by "Blurhash"), which is also designed for very small +// thumbnails (or very compact representations of image placeholders). Handsum +// files are bigger (but better quality) than Thumbhash (and Blurhash). +// "Handsum" also sounds like "handsome", meaning "good looking". // // Other techniques and image formats, similar to Thumbhash, can be found by // search for "LQIP" or "Low Quality Image Placeholders".