Merge duplicate ASCII renderers and trim dead options
The braille and block renderers were two near-identical awk programs sharing a verbatim PBM tokenizer, header check, and blank-line trim, differing only in the per-cell rendering. One awk with a mode variable now branches where they actually differ. The option surface served two in-repo callers, both passing constants: the duplicate positional loop behind --, the undocumented --block and --braille aliases, and the --flag=value forms all go. The two magick probes for the alpha range collapse into a single invocation. Output is byte-identical across images and flag sets for everything that remains, including both real callers' exact invocations. 353 lines to 243. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
cacd22c671
commit
b14735aacb
+64
-174
@@ -42,64 +42,30 @@ while (($# > 0)); do
|
||||
;;
|
||||
-w | --width)
|
||||
shift
|
||||
if [[ -z $1 ]]; then
|
||||
echo "Missing value for --width" >&2
|
||||
exit 1
|
||||
fi
|
||||
[[ -n $1 ]] || { echo "Missing value for --width" >&2; exit 1; }
|
||||
width="$1"
|
||||
;;
|
||||
--width=*)
|
||||
width="${1#*=}"
|
||||
;;
|
||||
-H | --height)
|
||||
shift
|
||||
if [[ -z $1 ]]; then
|
||||
echo "Missing value for --height" >&2
|
||||
exit 1
|
||||
fi
|
||||
[[ -n $1 ]] || { echo "Missing value for --height" >&2; exit 1; }
|
||||
height="$1"
|
||||
;;
|
||||
--height=*)
|
||||
height="${1#*=}"
|
||||
;;
|
||||
-m | --mode)
|
||||
shift
|
||||
if [[ -z $1 ]]; then
|
||||
echo "Missing value for --mode" >&2
|
||||
exit 1
|
||||
fi
|
||||
[[ -n $1 ]] || { echo "Missing value for --mode" >&2; exit 1; }
|
||||
mode="$1"
|
||||
;;
|
||||
--mode=*)
|
||||
mode="${1#*=}"
|
||||
;;
|
||||
--block | --blocks)
|
||||
mode=block
|
||||
;;
|
||||
--braille)
|
||||
mode=braille
|
||||
;;
|
||||
-t | --threshold)
|
||||
shift
|
||||
if [[ -z $1 ]]; then
|
||||
echo "Missing value for --threshold" >&2
|
||||
exit 1
|
||||
fi
|
||||
[[ -n $1 ]] || { echo "Missing value for --threshold" >&2; exit 1; }
|
||||
threshold="$1"
|
||||
;;
|
||||
--threshold=*)
|
||||
threshold="${1#*=}"
|
||||
;;
|
||||
--invert)
|
||||
invert=true
|
||||
;;
|
||||
--no-trim)
|
||||
trim=false
|
||||
;;
|
||||
--)
|
||||
shift
|
||||
break
|
||||
;;
|
||||
-*)
|
||||
echo "Unknown option: $1" >&2
|
||||
usage >&2
|
||||
@@ -120,46 +86,16 @@ while (($# > 0)); do
|
||||
shift
|
||||
done
|
||||
|
||||
if (($# > 0)); then
|
||||
while (($# > 0)); do
|
||||
if [[ -z $image_path ]]; then
|
||||
image_path="$1"
|
||||
elif [[ -z $output_path ]]; then
|
||||
output_path="$1"
|
||||
else
|
||||
echo "Unexpected argument: $1" >&2
|
||||
usage >&2
|
||||
exit 1
|
||||
fi
|
||||
shift
|
||||
done
|
||||
fi
|
||||
|
||||
if [[ -z $image_path || -z $output_path ]]; then
|
||||
usage >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! [[ $width =~ ^[0-9]+$ ]] || (( width < 1 )); then
|
||||
echo "Invalid width: $width" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! [[ $height =~ ^[0-9]+$ ]] || (( height < 1 )); then
|
||||
echo "Invalid height: $height" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ $mode != "braille" && $mode != "block" ]]; then
|
||||
echo "Invalid mode: $mode (expected braille or block)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! [[ $threshold =~ ^[0-9]+$ ]] || (( threshold < 0 || threshold > 100 )); then
|
||||
echo "Invalid threshold: $threshold (expected 0-100)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f $image_path ]]; then
|
||||
echo "Logo file not found: $image_path" >&2
|
||||
exit 1
|
||||
@@ -170,25 +106,19 @@ if omarchy-cmd-missing magick; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
case "$mode" in
|
||||
braille)
|
||||
if [[ $mode == "braille" ]]; then
|
||||
pixel_width=$((width * 2))
|
||||
pixel_height=$((height * 4))
|
||||
;;
|
||||
block)
|
||||
else
|
||||
pixel_width=$width
|
||||
pixel_height=$((height * 2))
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
|
||||
alpha_min=$(magick -background none "$image_path" -alpha extract -format '%[fx:minima]' info: 2>/dev/null) || {
|
||||
echo "Unable to read logo image: $image_path" >&2
|
||||
exit 1
|
||||
}
|
||||
alpha_max=$(magick -background none "$image_path" -alpha extract -format '%[fx:maxima]' info: 2>/dev/null) || {
|
||||
alpha_range=$(magick -background none "$image_path" -alpha extract -format '%[fx:minima] %[fx:maxima]' info: 2>/dev/null) || {
|
||||
echo "Unable to read logo image: $image_path" >&2
|
||||
exit 1
|
||||
}
|
||||
read -r alpha_min alpha_max <<<"$alpha_range"
|
||||
|
||||
use_alpha=false
|
||||
if awk -v min="$alpha_min" -v max="$alpha_max" 'BEGIN { exit !(min < 0.999 && max > min) }'; then
|
||||
@@ -218,36 +148,42 @@ magick_args+=(-resize "${pixel_width}x${pixel_height}" -threshold "${threshold}%
|
||||
tmp_output=$(mktemp)
|
||||
trap 'rm -f "$tmp_output"' EXIT
|
||||
|
||||
if [[ $mode == "braille" ]]; then
|
||||
if ! magick "${magick_args[@]}" | awk '
|
||||
BEGIN {
|
||||
braille_base = 10240
|
||||
dot[0,0] = 1
|
||||
dot[0,1] = 2
|
||||
dot[0,2] = 4
|
||||
dot[1,0] = 8
|
||||
dot[1,1] = 16
|
||||
dot[1,2] = 32
|
||||
dot[0,3] = 64
|
||||
dot[1,3] = 128
|
||||
}
|
||||
{
|
||||
sub(/#.*/, "")
|
||||
for (i = 1; i <= NF; i++) {
|
||||
token[++token_count] = $i
|
||||
}
|
||||
}
|
||||
END {
|
||||
if (token[1] != "P1") {
|
||||
exit 1
|
||||
}
|
||||
if ! magick "${magick_args[@]}" | awk -v mode="$mode" '
|
||||
BEGIN {
|
||||
braille_base = 10240
|
||||
dot[0,0] = 1
|
||||
dot[0,1] = 2
|
||||
dot[0,2] = 4
|
||||
dot[1,0] = 8
|
||||
dot[1,1] = 16
|
||||
dot[1,2] = 32
|
||||
dot[0,3] = 64
|
||||
dot[1,3] = 128
|
||||
|
||||
width = token[2]
|
||||
height = token[3]
|
||||
pixel_offset = 4
|
||||
block["11"] = "█"
|
||||
block["10"] = "▀"
|
||||
block["01"] = "▄"
|
||||
block["00"] = " "
|
||||
}
|
||||
{
|
||||
sub(/#.*/, "")
|
||||
for (i = 1; i <= NF; i++) {
|
||||
token[++token_count] = $i
|
||||
}
|
||||
}
|
||||
END {
|
||||
if (token[1] != "P1") {
|
||||
exit 1
|
||||
}
|
||||
|
||||
for (y = 0; y < height; y += 4) {
|
||||
line = ""
|
||||
width = token[2]
|
||||
height = token[3]
|
||||
pixel_offset = 4
|
||||
row_step = (mode == "braille") ? 4 : 2
|
||||
|
||||
for (y = 0; y < height; y += row_step) {
|
||||
line = ""
|
||||
if (mode == "braille") {
|
||||
for (x = 0; x < width; x += 2) {
|
||||
code = 0
|
||||
for (dy = 0; dy < 4; dy++) {
|
||||
@@ -267,80 +203,34 @@ if [[ $mode == "braille" ]]; then
|
||||
line = line sprintf("%c", braille_base + code)
|
||||
}
|
||||
}
|
||||
sub(/[ ]+$/, "", line)
|
||||
lines[++line_count] = line
|
||||
}
|
||||
|
||||
first = 1
|
||||
while (first <= line_count && lines[first] ~ /^[ ]*$/) {
|
||||
first++
|
||||
}
|
||||
|
||||
last = line_count
|
||||
while (last >= first && lines[last] ~ /^[ ]*$/) {
|
||||
last--
|
||||
}
|
||||
|
||||
for (i = first; i <= last; i++) {
|
||||
print lines[i]
|
||||
}
|
||||
}
|
||||
' >"$tmp_output"; then
|
||||
echo "Unable to convert logo image: $image_path" >&2
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
if ! magick "${magick_args[@]}" | awk '
|
||||
BEGIN {
|
||||
block["11"] = "█"
|
||||
block["10"] = "▀"
|
||||
block["01"] = "▄"
|
||||
block["00"] = " "
|
||||
}
|
||||
{
|
||||
sub(/#.*/, "")
|
||||
for (i = 1; i <= NF; i++) {
|
||||
token[++token_count] = $i
|
||||
}
|
||||
}
|
||||
END {
|
||||
if (token[1] != "P1") {
|
||||
exit 1
|
||||
}
|
||||
|
||||
width = token[2]
|
||||
height = token[3]
|
||||
pixel_offset = 4
|
||||
|
||||
for (y = 0; y < height; y += 2) {
|
||||
line = ""
|
||||
} else {
|
||||
for (x = 0; x < width; x++) {
|
||||
top = token[pixel_offset + (y * width) + x]
|
||||
bottom = (y + 1 < height) ? token[pixel_offset + ((y + 1) * width) + x] : 0
|
||||
line = line block[top bottom]
|
||||
}
|
||||
sub(/[ ]+$/, "", line)
|
||||
lines[++line_count] = line
|
||||
}
|
||||
|
||||
first = 1
|
||||
while (first <= line_count && lines[first] ~ /^[ ]*$/) {
|
||||
first++
|
||||
}
|
||||
|
||||
last = line_count
|
||||
while (last >= first && lines[last] ~ /^[ ]*$/) {
|
||||
last--
|
||||
}
|
||||
|
||||
for (i = first; i <= last; i++) {
|
||||
print lines[i]
|
||||
}
|
||||
sub(/[ ]+$/, "", line)
|
||||
lines[++line_count] = line
|
||||
}
|
||||
' >"$tmp_output"; then
|
||||
echo "Unable to convert logo image: $image_path" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
first = 1
|
||||
while (first <= line_count && lines[first] ~ /^[ ]*$/) {
|
||||
first++
|
||||
}
|
||||
|
||||
last = line_count
|
||||
while (last >= first && lines[last] ~ /^[ ]*$/) {
|
||||
last--
|
||||
}
|
||||
|
||||
for (i = first; i <= last; i++) {
|
||||
print lines[i]
|
||||
}
|
||||
}
|
||||
' >"$tmp_output"; then
|
||||
echo "Unable to convert logo image: $image_path" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -s $tmp_output ]]; then
|
||||
|
||||
Reference in New Issue
Block a user