summarylogtreecommitdiffstats
path: root/hqx.sh
blob: 18baa9d8ad019b30ffd4d712f6ae10a56d627d43 (plain)
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
#!/bin/bash
usage() {
        echo "Usage: hqx [-s scaleBy] input output"
        exit 1
}

scale=4

while getopts ":s:" option; do
        case $option in
                s)
                        scale="$OPTARG"
                        if [ "$scale" -ne 2 ] && [ "$scale" -ne 3 ] && [ "$scale" -ne 4 ]; then
                                echo "Only scale factors of 2, 3 and 4 are supported"
                                exit 1
                        fi
                ;;
                \?)
                        echo "Invalid option: -$OPTARG" >&2
                ;;
                :)
                        echo "hqx: option requires an argument -- '$OPTARG'"
                        usage
                ;;
        esac
done
shift $((OPTIND-1))

inputfile="$1"
outputfile="$2"

if [ -z "$inputfile" ] || [ -z "$outputfile" ]; then
        usage
fi

filetype="$(identify -format "%m" "$inputfile" 2>&1)"
input_exists="$?"

if ! [ -s "$inputfile" ] || [ $input_exists -ne 0 ]; then
        echo "ERROR: can't load '$inputfile'"
        exit 1
fi

if ! [ "$filetype" = "PNG" ]; then
        convert "$inputfile" "._temp_png.png"
        inputfile="._temp_png.png"
fi

pixelscale "$inputfile" "$outputfile" "hq${scale}x"

if [ -e "._temp_png.png" ]; then
        rm "._temp_png.png"
fi