summaryrefslogtreecommitdiffstats
path: root/daw_base/build/gen_wallpaper_from_bitmap.sh
blob: 6116efb5ad7a5e9fa40f5ee0c1dc4c538ae811a0 (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
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
#!/bin/bash
#
# generate a Plasma5 Wallpaper collection from a single image

# Parameter '-b': bitmap file of JPG or PNG format.
#   Aspect ratio of the image *must* be 16:9 (1920x1080 px or higher res).
# Parameter 2: name of the wallpaper directory (single word)
# Parameter 3: descriptive long name for the wallpaper (between quotes)
#
# Example:
#  ./gen_wallpaper_from_bitmap.sh -p sample.png -d LivingFlame -n "The living flame" -a "John Doe" -e "foo@fum.info"
# ---------------------------------------------------------------------------

CWD=$(cd $(dirname $0); pwd)

# Attribution - this will be used in the wallpaper metadata file:
WP_AUTHOR=${WP_AUTHOR:-"Eric Hameleers"}
WP_EMAIL=${WP_EMAIL:-"alien@slackware.com"}

# More default values:
WP_ROOT=${WP_ROOT:-"$CWD"}

BN=$(basename $0)
SHOWHELP=0

# ---------------------------------------------------------------------------
while getopts "a:b:d:e:hn:r:" Option
do
  case $Option in
    a ) WP_AUTHOR="${OPTARG}"
        ;;
    b ) WP_BITMAP="${OPTARG}"
        ;;
    d ) WP_DIR="${OPTARG}"
        ;;
    e ) WP_EMAIL="${OPTARG}"
        ;;
    h ) SHOWHELP=1
        ;;
    n ) WP_NAME="${OPTARG}"
        ;;
    r ) WP_ROOT="${OPTARG}"
        ;;
    * ) ;;   # DEFAULT
  esac
done

# End of option parsing.
shift $(($OPTIND - 1))

#  $1 now references the first non option item supplied on the command line
#  if one exists.
# ---------------------------------------------------------------------------

if [ $SHOWHELP -eq 1 ]; then
  echo "[$BN:] Parameters are:"
  echo "  -a <author>   Name of the wallpaper author, put inbetween quotes."
  echo "  -b <bitmap>   Bitmap file in JPG or PNG format."
  echo "                Aspect ratio of the image *must* be 16:9"
  echo "                (1920x1080 px or higher res)."
  echo "  -d <dirname>  Directory name for the wallpaper."
  echo "  -e <email>    Email address of the wallpaper author."
  echo "  -h            This help."
  echo "  -n <name>     Descriptive name for the wallpaper, inbetween quotes."
  echo "  -r <rootdir>  Root directory to create the wallpaper"
  echo "                directory structure in."
  exit 0
fi

# Some house-keeping with regard to the supplied image:
WP_REALBITMAP="$(readlink -f ${WP_BITMAP} 2>&1)"
WP_REALBASE="$(echo $(basename ${WP_REALBITMAP}) |rev |cut -d. -f2- |rev)"
WP_BITMBASE="$(echo $(basename ${WP_BITMAP}) |rev |cut -d. -f2- |rev)"

# If wallpaper name / description were not given, we use the bitmap basename:
WP_DIR=${WP_DIR:-"$WP_REALBASE"}
WP_NAME=${WP_NAME:-"$WP_BITMBASE"}

# Exit immediately if the image file is not found:
if ! readlink -f "${WP_BITMAP}" 1>/dev/null 2>&1 1>/dev/null 2>&1 || ! file "${WP_REALBITMAP}" | grep -q "image data" ; then
  echo "-- No wallpaper image found."
  exit 1
fi

# Exit if the target directory does not exist or is not writable:
if [ ! -d "${WP_ROOT}" ]; then
  echo "-- '${WP_ROOT}' not a directory."
  exit 2
fi
# Get the full path:
WP_ROOT="$(cd ${WP_ROOT}; pwd)"
if [ ! -w "${WP_ROOT}" ]; then
  echo "-- Directory '${WP_ROOT}' not writable."
  exit 2
fi

# Exit if the wallpaper already exists:
if [ -d "${WP_ROOT}/${WP_DIR}" ]; then
  echo "-- Wallpaper '${WP_ROOT}/${WP_DIR}' already exists."
  exit 3
fi

# Create the wallpaper for Plasma5;
echo "-- Normalizing background image."
# First convert our image into a JPG with known filename:
TMPDIR=$(mktemp -d -t wp_img.XXXXXX)
convert "${WP_REALBITMAP}" ${TMPDIR}/background.jpg

# The toplevel directory WP_DIR is your wallpaper!
# It needs to be placed in /usr/share/wallpapers/ on the target system:
mkdir -p ${WP_ROOT}/${WP_DIR}/contents/images

# Create set of images for common aspect ratios like 16:9, 16:10 and 4:3:
# Aspect Ratio 16:9 :
convert ${TMPDIR}/background.jpg \
  -resize 1920x1080 \
  ${WP_ROOT}/${WP_DIR}/contents/images/1920x1080.jpg
convert ${TMPDIR}/background.jpg \
  -resize 5120x2880 \
  ${WP_ROOT}/${WP_DIR}/contents/images/5120x2880.jpg
# Aspect Ratio 16:10 :
convert  ${TMPDIR}/background.jpg \
  -resize 5120x - | \
  convert - -geometry 1920x1200^ -gravity center -crop 1920x1200+0+0 \
  ${WP_ROOT}/${WP_DIR}/contents/images/1920x1200.jpg
convert  ${TMPDIR}/background.jpg \
  -resize 5120x - | \
  convert - -geometry 1280x800^ -gravity center -crop 1280x800+0+0 \
  ${WP_ROOT}/${WP_DIR}/contents/images/1280x800.jpg
# Aspect Ratio 4:3 :
convert  ${TMPDIR}/background.jpg \
  -resize 5120x - | \
  convert - -geometry 1024x768^ -gravity center -crop 1024x768+0+0 \
  ${WP_ROOT}/${WP_DIR}/contents/images/1024x768.jpg

# Create the required wallpaper screenshot of 400x225 px (16:9 aspect ratio):
convert ${TMPDIR}/background.jpg \
  -resize 400x225 \
  ${WP_ROOT}/${WP_DIR}/contents/screenshot.png

# Add wallpaper description:
cat <<EOT >${WP_ROOT}/${WP_DIR}/metadata.desktop
[Desktop Entry]
Name=${WP_NAME}

X-KDE-PluginInfo-Name=${WP_DIR}
X-KDE-PluginInfo-Author=${WP_AUTHOR}
X-KDE-PluginInfo-Email=${WP_EMAIL}
X-KDE-PluginInfo-License=CC-BY-SA-4.0
EOT

# Inform user:
echo "-- Wallpaper '${WP_NAME}' created in '${WP_ROOT}/${WP_DIR}'."

# Cleanup:
rm -fr $TMPDIR