example/stb-imagedumper: add -bg flag

This commit is contained in:
Nigel Tao committed 2026-05-04 17:07:35 +10:00
1 parent fdf742186d
commit ff4132b3ca
2 files changed
+22 -11

No files matched your search

+17 -9
View File
@@ -39,11 +39,19 @@ var (
encodeFlag = flag.Bool("encode", false, "")
roundtripFlag = flag.Bool("roundtrip", false, "")
bFlag = flag.String("b", "", "")
cFlag = flag.String("c", "rgb", "")
qFlag = flag.Int("q", 3, "")
bFlag = flag.String("b", "", "")
bgFlag = flag.String("bg", "", "")
cFlag = flag.String("c", "rgb", "")
qFlag = flag.Int("q", 3, "")
)
func getBFlag() string {
if *bFlag != "" {
return *bFlag
}
return *bgFlag
}
const usageStr = `handsum decodes and encodes the Handsum lossy image file format.
Usage: choose one of
@@ -61,10 +69,10 @@ 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, the default color and quality is -c=rgb -q=3 (best
quality; 147 bytes per file) but you can choose a lower setting. The -b flag
sets a background color (in case the input has transparency). For example:
quality; 147 bytes per file) but you can choose a lower setting. The -b or -bg
flag sets a background color (in case the input is transparent). For example:
handsum -encode -b=darkblue -c=rgb -q=2 foo.png > foo.handsum
handsum -encode -bg=darkblue -c=rgb -q=2 foo.png > foo.handsum
----
@@ -74,7 +82,7 @@ Flags:
-encode Whether to encode the input.
-roundtrip Whether to encode-and-decode the input.
-b Encoding background color: e.g. red, white, 9c27b0 or #ff8.
-b or -bg Encoding background color: e.g. red, white, 9c27b0 or #ff8.
The default is ff9e9e9e (for -c=1 or -c=3) or 00000000 (for -c=4).
-c Encoding color: gray (1), rgb (3, default) or rgba (4).
-q Encoding quality: 0, 1, 2 or 3 (default).
@@ -100,7 +108,7 @@ func main1() error {
}
quality := handsum.Quality(max(0, min(3, *qFlag)))
if (color <= handsum.ColorRGB) && (*bFlag == "") {
if (color <= handsum.ColorRGB) && (getBFlag() == "") {
*bFlag = "ff9e9e9e"
}
@@ -169,7 +177,7 @@ func roundtrip(inFile *os.File, color handsum.Color, quality handsum.Quality) er
}
func applyBackgroundColor(src image.Image) image.Image {
backgroundColor, _ := parsecolor.Parse(*bFlag)
backgroundColor, _ := parsecolor.Parse(getBFlag())
r16, g16, b16, a16 := backgroundColor.RGBA()
if (r16 == 0) && (g16 == 0) && (b16 == 0) && (a16 == 0) {
return src
+5 -2
View File
@@ -645,7 +645,8 @@ static const char* g_usage =
" -a or -ascii-art (use ASCII art instead of ANSI color codes)\n"
" -B or -braille-art-dark-mode (use white-on-black Braille art)\n"
" -b or -braille-art-light-mode (use black-on-white Braille art)\n"
" -c=orange or -background-color=ff9800 (set a background color)\n"
" -c=orange or -bg=f90 or -background-color=ff9800 (set a background\n"
" color, for transparent input)\n"
" -demo (dump the built-in demonstration images)\n"
" -n or -natural-size (use each image's natural size); this\n"
" overrides -resize=N\n"
@@ -696,7 +697,9 @@ parse_flags(int argc, char** argv) {
g_flags.natural_size = true;
continue;
}
if (!strncmp(arg, "c=", 2) || !strncmp(arg, "background-color=", 17)) {
if (!strncmp(arg, "c=", 2) || //
!strncmp(arg, "bg=", 3) || //
!strncmp(arg, "background-color=", 17)) {
while (*arg++ != '=') {
}
int64_t c = parse_color(arg, strlen(arg));