-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbin-graph-merged.sh
More file actions
executable file
·179 lines (157 loc) · 5.4 KB
/
bin-graph-merged.sh
File metadata and controls
executable file
·179 lines (157 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
#
# Copyright 2025 8dcc. All Rights Reserved.
#
# This file is part of bin-graph.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------------
#
# Simple script for generating multiple binary graphs from the same file.
set -e
BIN_GRAPH='bin-graph'
if [ $# -lt 2 ]; then
echo "Usage: $(basename "$0") [OPTION...] INPUT OUTPUT.png" 1>&2
exit 1
fi
if [ ! "$(command -v "$BIN_GRAPH")" ]; then
echo "$(basename "$0"): The '$BIN_GRAPH' command is not installed." 1>&2
exit
fi
if [ ! "$(command -v 'magick')" ] &&
{ [ ! "$(command -v 'identify')" ] || [ ! "$(command -v 'convert')" ]; }; then
echo "$(basename "$0"): The '$BIN_GRAPH' command is not installed." 1>&2
exit
fi
get_width() {
local image="$1"
if [ -n "$(command -v 'magick')" ]; then
magick "$image" -format "%w" info:
else
identify -format "%w" "$image"
fi
}
get_height() {
local image="$1"
if [ -n "$(command -v 'magick')" ]; then
magick "$image" -format "%h" info:
else
identify -format "%h" "$image"
fi
}
merge_images() {
local total_width="$1"
local total_height="$2"
local output_path="$3"
local args=("${@:4}") # ('path1' '+x+y' 'path2' '+x+y' ...)
local magick_args=()
i=0
for arg in "${args[@]}"; do
if [ $((i % 2)) -eq 0 ]; then
magick_args+=("$arg") # File name (even)
else
magick_args+=(-geometry "$arg" -composite) # Position (odd)
fi
i=$((i+1))
done
if [ -n "$(command -v 'magick')" ]; then
magick -background transparent \
-size "${total_width}x${total_height}" \
xc:transparent "${magick_args[@]}" "$output_path"
else
convert -background transparent \
-size "${total_width}x${total_height}" \
xc:transparent "${magick_args[@]}" "$output_path"
fi
}
max() {
local max="$1"
for num in "$@"; do
if (( num > max )); then
max="$num"
fi
done
echo "$max"
}
bin_graph_args=("${@:1:$#-2}")
input_path="${*: -2:1}"
output_path="${*: -1:1}"
input_name="$(basename "$input_path")"
tmp_dir="$(mktemp --tmpdir --directory "bin-graph-merged.XXXXX")"
"$BIN_GRAPH" "${bin_graph_args[@]}" --mode 'grayscale' --width 256 "$input_path" "${tmp_dir}/${input_name}.grayscale.png"
"$BIN_GRAPH" "${bin_graph_args[@]}" --mode 'entropy-histogram' --width 64 --block-size 256 "$input_path" "${tmp_dir}/${input_name}.entropy-histogram.png"
"$BIN_GRAPH" "${bin_graph_args[@]}" --mode 'ascii' --width 256 --transform-hilbert 8 "$input_path" "${tmp_dir}/${input_name}.ascii.png"
"$BIN_GRAPH" "${bin_graph_args[@]}" --mode 'entropy' --width 256 --transform-hilbert 8 --block-size 256 "$input_path" "${tmp_dir}/${input_name}.entropy.png"
"$BIN_GRAPH" "${bin_graph_args[@]}" --mode 'histogram' --width 256 "$input_path" "${tmp_dir}/${input_name}.histogram.png"
"$BIN_GRAPH" "${bin_graph_args[@]}" --mode 'bigrams' "$input_path" "${tmp_dir}/${input_name}.bigrams.png"
# +-----+-+-----+-----+---+
# | | | | | 5 |
# | | | | |---+
# | 1 |2| 3 | 4 | 6 |
# | | | | |---+
# | | | | |
# +-----+-+-----+-----+
#
# Target layout:
#
# 1. Grayscale.
# 2. Entropy histogram.
# 3. Hilbert ASCII.
# 4. Hilbert entropy.
# 5. Histogram.
# 6. Bigrams.
padding=5 # px
image1="${tmp_dir}/${input_name}.grayscale.png"
image2="${tmp_dir}/${input_name}.entropy-histogram.png"
image3="${tmp_dir}/${input_name}.ascii.png"
image4="${tmp_dir}/${input_name}.entropy.png"
image5="${tmp_dir}/${input_name}.histogram.png"
image6="${tmp_dir}/${input_name}.bigrams.png"
width1="$(get_width "$image1")"
width2="$(get_width "$image2")"
width3="$(get_width "$image3")"
width4="$(get_width "$image4")"
width5="$(get_width "$image5")"
width6="$(get_width "$image6")"
height1="$(get_height "$image1")"
height2="$(get_height "$image2")"
height3="$(get_height "$image3")"
height4="$(get_height "$image4")"
height5="$(get_height "$image5")"
height6="$(get_height "$image6")"
x1="$padding"
x2="$((x1 + width1 + padding))"
x3="$((x2 + width2 + padding))"
x4="$((x3 + width3 + padding))"
x5="$((x4 + width4 + padding))"
x6="$x5"
y1="$padding"
y2="$y1"
y3="$y2"
y4="$y3"
y5="$y4"
y6="$((y5 + height5 + padding))"
total_width="$((x5 + $(max "$width5" "$width6") + padding))"
total_height="$((y1 + $(max "$height1" "$height2" "$height3" "$height4" $((height5 + padding + height6))) + padding))"
merge_images "$total_width" "$total_height" "$output_path" \
"$image1" "+${x1}+${y1}" \
"$image2" "+${x2}+${y2}" \
"$image3" "+${x3}+${y3}" \
"$image4" "+${x4}+${y4}" \
"$image5" "+${x5}+${y5}" \
"$image6" "+${x6}+${y6}"
if [ -d "$tmp_dir" ]; then
rm -r "$tmp_dir"
fi