#!/usr/bin/env bash
### USAGE
# ./convertToIconset
# Example
# ./convertToIconset ~/sample.png ~/Desktop/converted.iconset
# exit the shell script on error immediately
set -e
make_iconset_imagemagick() {
local file iconset
file="${1}"
iconset="${2}"
mkdir "$iconset"
for size in {16,32,64,128,256,512}; do
$CONVERT "${file}" -define png:big-depth=16 -define png:color-type=6 -sample "${size}x${size}" "${iconset}/icon_${size}x${size}.png"
$CONVERT "${file}" -define png:big-depth=16 -define png:color-type=6 -sample "$((size * 2))x$((size * 2))" "${iconset}/icon_${size}x${size}@2x.png"
done
}
make_iconset_sips() {
local file iconset
file="${1}"
iconset="${2}"
mkdir "$iconset"
for size in {16,32,64,128,256,512}; do
sips --setProperty format png --resampleHeightWidth "${size}" "${size}" "${file}" --out "${iconset}/icon_${size}x${size}.png" &> /dev/null
sips --setProperty format png --resampleHeightWidth "$((size * 2))" "$((size * 2))" "${file}" --out "${iconset}/icon_${size}x${size}@2x.png" &> /dev/null
done
}
# Parameters
SOURCE="$1"
DEST="$2"
# Check source and destination arguments
if [ -z "${SOURCE}" ]; then
echo >&2 "No source image specified"; exit 1
fi
if [ -z "${DEST}" ]; then
echo >&2 "No destination specified"; exit 1
fi
HAVE_IMAGEMAGICK=
HAVE_SIPS=
HAVE_GRAPHICSMAGICK=
CONVERT=
type gm &>/dev/null && gm version | grep GraphicsMagick &>/dev/null && HAVE_GRAPHICSMAGICK=true && CONVERT="gm convert"
type convert &>/dev/null && HAVE_IMAGEMAGICK=true && CONVERT="convert"
type sips &>/dev/null && HAVE_SIPS=true
if [[ -n "$HAVE_IMAGEMAGICK" || -n "$HAVE_GRAPHICSMAGICK" ]]; then
PNG_PATH="$(mktemp -d -t nativefier-iconset-XXXXXX)/icon.png"
"${BASH_SOURCE%/*}/convertToPng" "${SOURCE}" "${PNG_PATH}"
make_iconset_imagemagick "${PNG_PATH}" "${DEST}"
elif [[ -n "$HAVE_SIPS" ]]; then
make_iconset_sips "${SOURCE}" "${DEST}"
else
echo >&2 "Cannot find convert or sips executables"; exit 1;
fi