This repository has been archived on 2019-12-04. You can view files and clone it, but cannot push or open issues or pull requests.
terrainerizer/unterrainerizer

128 lines
3.0 KiB
Bash
Executable File

#!/bin/sh
#
# Unterrainerizer v0.1
# by Anna Wiggins (annabunches@gmail.com)
# Licensed under the GPLv3, see accompanying file LICENSE for terms
#
# Takes any number of terrain RAW files and merges them into a single
# PNG file (or other file type, as configured).
#
# The input files must be named according to this convention:
#
# mapname-nxm.raw
#
# Where mapname is the same for all maps, and n and m are the column and
# row that this image occupies in the final image, starting from the
# top-left.
extension="png"
##### STOP EDITING HERE #####
#############################
# Make a unique random directory
make_tmpdir()
{
tmpdir=/tmp/unterrainerizer-`uuidgen | perl -pe 's/-.*$//'`
mkdir $tmpdir
}
clean_up()
{
echo "Cleaning up"
rm -rf $tmpdir
}
# Extract the maps into separate files
# Also sets max_x and max_y
extract_maps()
{
echo "Extracting heightmaps from RAW files"
# Find the greatest X and Y values
index=0
max_x=0
max_y=0
for i in $@; do
x=`echo $i | perl -pe 's/^.*-(.*)x.*.raw/\1/'`
y=`echo $i | perl -pe 's/^.*-.*x(.*).raw/\1/'`
if [ $x -gt $max_x ]; then max_x=$x; fi
if [ $y -gt $max_y ]; then max_y=$y; fi
out_name=${tmpdir}/${base_name}-${x}x${y}.${extension}
unterrain_commands[$index]="(un-terrain \"${i}\" \"${out_name}\")"
index+=1
done
# We have to invert the order these are added so that montage will handle them correctly later
index=0
for i in $(seq 0 $max_y); do
for j in $(seq 0 $max_x); do
extracted_maps[$index]=${tmpdir}/${base_name}-${j}x${i}.${extension}
index+=1
done
done
gimp -id -b - &> /dev/null <<EOF
(define (un-terrain src-filename dest-filename)
(let* (
(src-image (car (file-slraw-load 1 src-filename "")))
(dest-image (car (gimp-image-new 256 256 1)))
)
(gimp-edit-copy (car (gimp-image-get-active-drawable src-image)))
(let (
(layer (car (gimp-layer-new dest-image 256 256 2 "" 100 0)))
)
(gimp-image-add-layer dest-image layer -1))
(let* (
(pasteable (car (gimp-image-get-active-drawable dest-image)))
(floater (car (gimp-edit-paste pasteable 1)))
)
(gimp-floating-sel-anchor floater))
(gimp-file-save 1
dest-image
(car (gimp-image-get-active-drawable dest-image))
dest-filename
"")
dest-image))
${unterrain_commands[@]}
(gimp-quit 0)
EOF
}
# Recombine the maps into a single final image
build_heightmap()
{
echo "Combining separate pieces into a single map"
montage -geometry +0+0 -tile $(($max_x + 1))x$(($max_y + 1)) ${extracted_maps[@]} ${tmpdir}/${base_name}.${extension}
# fixme: x and y are inverted in the default sort order, apparently... how to fix? Oy! leftoffhere
}
move_heightmap()
{
echo "Moving heightmap to current directory"
mv ${tmpdir}/${base_name}.${extension} ./
}
# Extract the base name of the file. This is:
# base_name-nxm.extension
base_name=`echo $1 | perl -pe 's/^(.*)-.*x.*.raw/\1/' | perl -pe 's/^.*\/(.*)$/\1/'`
# Run the functions
make_tmpdir
extract_maps $@
build_heightmap
move_heightmap
#clean_up