cmd/handsum: append '\n' to base64 output

This commit is contained in:
Nigel Tao committed 2026-06-25 22:22:26 +10:00
1 parent ba25980637
commit 9c6d1dacad
3 files changed
+80 -7

No files matched your search

+1
View File
@@ -203,6 +203,7 @@ func encode(w io.Writer, b64e *base64.Encoding, r io.Reader, color handsum.Color
if b64e != nil {
enc := b64e.AppendEncode(nil, buf.Bytes())
enc = append(enc, '\n')
if _, err := w.Write(enc); err != nil {
return err
}
+71
View File
@@ -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
}
+8 -7
View File
@@ -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".