-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgfx2gf.sh
executable file
·277 lines (245 loc) · 7.8 KB
/
gfx2gf.sh
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash
###########################################################################
# #
# gfx2gf.sh v1.1.2 (20141010) #
# by Garrett Hyde #
# #
# This bash script is based upon Jeremy Williams Window's batch file. #
# *** You must have ImageMagick (www.imagemagick.org) installed! *** #
# You also might need to install Ghostscript. #
# #
###########################################################################
USAGE="$0 [-f] [-h] [-n] [-o $(tput smul)directory$(tput rmul)] [-v] $(tput smul)file$(tput rmul)..."
# Check if FFmpeg is installed
if ! type ffmpeg > /dev/null; then
echo "You need to install FFmpeg first!" >&2
exit 1
fi
# Check if ImageMagick is installed
if ! type convert > /dev/null; then
echo "You need to install ImageMagick first!" >&2
exit 1
fi
# Check if Ghostscript is installed
if ! type gs > /dev/null; then
echo "You need to install Ghostscript first!" >&2
exit 1
fi
# Check usage
if [ "$#" -lt 1 ]; then
echo $USAGE >&2
exit 1
fi
# Parse options
base_output=""
force=0
verbose=0
nearest_neighbor=0
while getopts ":fnho:v" opt; do
case $opt in
f)
force=1
;;
h)
echo $USAGE
exit 0
;;
o)
base_output="$OPTARG"
;;
v)
verbose=1
;;
n)
nearest_neighbor=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
# Process graphic files
for input_path in "$@"; do
# Establish output directory
input_file="$(basename "$input_path")"
output_dir="$(basename "$input_path" .${input_path##*.})"
if [ -n "$base_output" ]; then
output_path="$base_output/$output_dir"
else
output_path="$(dirname "$input_path")/$output_dir"
fi
# Check if output directory already exists
if [ -e "$output_path" ]; then
# Check if '-f' option is enabled
if [ "$force" -eq 1 ]; then
if [ "$verbose" -eq 1 ]; then
echo "Deleting previous \"$output_path\""
fi
rm -fr "$output_path"
else
echo "Cannot create output directory \"$output_path\" because it already exists." >&2
continue
fi
fi
# Make output directory
mkdir -p "$output_path"
# Grab information about source file (height, width, delay)
file_info=$(identify -format "%h %w %T" "$input_path"[0])
height=$(echo "$file_info" | awk '{print $1}')
width=$(echo "$file_info" | awk '{print $2}')
delay=$(echo "$file_info" | awk '{print $3}')
# Convert graphic into BMP files
if [ "$verbose" -eq 1 ]; then
echo "Converting \"$input_file\" to Game Frame format..."
echo "-----"
fi
ffmpeg $(if [ "$verbose" -eq 0 ]; then echo "-loglevel error"; fi) \
-an -r 1 -i "$input_path" -r 1 -f image2 -pix_fmt bgr24 -s 16x16 $(if [ "$nearest_neighbor" -ne 0 ]; then echo "-sws_flags neighbor"; fi) \
"$output_path/frame_%06d.bmp"
if [ "$verbose" -eq 1 ]; then
echo "-----"
fi
# Count the number of frames
frames=$(ls "$output_path" | grep "frame_" | wc -l)
if [ "$verbose" -eq 1 ]; then
echo "$frames 16x16 frame(s) created."
fi
if [ "$frames" -gt 196000 ]; then
echo "File has more than 196000 frames. This might not work." >&2
fi
# Check if need to use nested folders
if [ "$frames" -gt 1999 ]; then
nested=1
else
nested=0
fi
# Combine multiple files into one long filmstrip
if [ "$verbose" -eq 1 ]; then
echo "Creating filmstrip..."
fi
montage "$output_path/frame_*.bmp" -mode concatenate -tile 1x \
-type truecolor "$output_path/0.bmp"
if [ "$frames" -gt 1 ]; then
# Create multiframe animation
if [ "$verbose" -eq 1 ]; then
echo "Filmstrip conversion finished!"
fi
if [ "$delay" -lt 4 ]; then
delay=4
fi
delay=$(($delay * 10))
# Write config.ini using stored delay value
echo "# All of these settings are optional.
# If this file does't exist, or if
# settings don't appear in this file,
# Game Frame will use default values.
.
[animation]
.
# milliseconds to hold each frame
# (1000 = 1 sec; lower is faster)
hold = $delay
.
# should the animation loop? If false,
# system will progress to next folder
# after the last frame is displayed.
loop = true
.
[translate]
.
# move the animation across the screen
# this many pixels per frame. Experiment
# with positive and negative numbers for
# different directions.
moveX = 0
moveY = 16
.
# should the movement loop?
$(if [ "$nested" -eq 0 ]; then
echo "loop = true"
else
echo "loop = false"
fi)
.
# begin/end scroll off screen?
panoff = false
.
# optionally dictate the next animation
# EXAMPLE: nextFolder = mspacman
# nextFolder = defend1" > "$output_path/config.ini"
if [ "$verbose" -eq 1 ]; then
echo "File \"config.ini\" written!"
fi
# Generate preview GIF
if [ "$verbose" -eq 1 ]; then
echo "Generating preview..."
fi
delay=$(($delay / 10))
if [ "$frames" -le 250 ]; then
convert -delay $delay -filter box -resize 128x128 \
"$output_path/frame_*.bmp" \
"$output_path"/"$output_dir"_preview.gif
else
if [ "$verbose" -eq 1 ]; then
echo "Long animation; only previewing the first 250 frames..."
fi
convert -delay $delay -filter box -resize 128x128 \
"$output_path/frame_%06d.bmp[1-250]" \
"$output_path"/"$output_dir"_preview.gif
fi
if [ "$verbose" -eq 1 ]; then
echo "Preview created!"
fi
else
# Create single frame animation
if [ "$verbose" -eq 1 ]; then
echo "Graphic conversion finished!"
echo "Generating preview..."
fi
convert -filter box -resize 128x128 "$output_path/frame_*.bmp" \
"$output_path"/"$output_dir"_preview.gif
if [ "$verbose" -eq 1 ]; then
echo "Preview created!"
fi
fi
# Cleanup
find "$output_path" -name 'frame_*.bmp' -delete
# Build nested folders
if [ "$nested" -eq 1 ]; then
if [ "$verbose" -eq 1 ]; then
echo "Long video detected; nesting folders..."
fi
# Generate filmstrips
convert -crop 16x32000x0x0 "$output_path/0.bmp" \
"$output_path/strip_%01d.bmp"
strips=$(ls "$output_path" | grep "strip_" | wc -l)
if [ "$verbose" -eq 1 ]; then
echo "$strips filmstrips created."
fi
# Create nested folders and move filmstrips into those folders
if [ "$verbose" -eq 1 ]; then
echo "Creating nested folders"
fi
for i in $(seq 0 $(($strips - 1))); do
mkdir "$output_path/$i"
mv "$output_path/strip_$i.bmp" "$output_path/$i/0.bmp"
cp "$output_path/config.ini" "$output_path/$i/"
done
# Cleanup
rm -f "$output_path/config.ini"
rm -f "$output_path/0.bmp"
if [ "$verbose" -eq 1 ]; then
echo "Nested folders created!"
fi
fi
if [ "$verbose" -eq 1 ]; then
echo "Process finished!"
fi
done
exit 0