#!/bin/sh -u # # Construct a wallpaper image by resizing a source image and padding. # Pulled from rootbg and updated to use GraphicsMagick. # Writes new image to a persistent cache in PNG format and prints the pathname. # - Cameron Simpson 11sep2003 # : ${BG:=black} : ${TMPDIR:=/tmp} : ${WALLPAPER_SHADE:=''} : ${XINERAMA:=''} : ${X11_COMPOSITE:=''} gm=gm [ -n "${PREFER_IMAGEMAGICK:-}" ] && gm= cmd=`basename "$0"` usage="Usage: $cmd [-g geom] [-bg bg-colour] [-sh shade] imagefiles... -g geom Use this target geometry instead of the display pixel size. -bg bg-colour Background colour to use for padding. Default is black, overridding by the envvar \$BG. -sh shading Shade (dim) the wallpaper to this percentage of full brightness. Default 100 on \$X11_COMPOSITE displays and 50 on others. Overridden by the envvar \$WALLPAPER_SHADE." badopts= geom= shade=$WALLPAPER_SHADE while [ $# -gt 0 ] do case "$1" in -bg)BG=$2; shift ;; -sh)shade=$2; shift ;; -g) geom=$2; shift ;; --) shift; break ;; -?*)echo "$cmd: unrecognised option: $1" >&2; badopts=1 ;; *) break ;; esac shift done if [ $# = 0 ] then echo "$cmd: missing imagefiles" >&2 badopts=1 fi [ $badopts ] && { echo "$usage" >&2; exit 2; } set -ue if [ -z "$shade" ] then if [ $X11_COMPOSITE ] then shade=100 else shade=50 fi fi if [ $# = 1 ] then # replicate, one per monitor imagefile=$1 set -- for width in $X11_MONITOR_WIDTHS do set -- ${1+"$@"} "$imagefile" done fi ########################################## # Multiple images - assemble into montage. if [ $# -gt 1 ] then wide=0 for width in $X11_MONITOR_WIDTHS do wide=`expr $wide + $width` done append="$gm convert +append" ## -resize '${wide}x${X11_Y}'" n=0 for width in $X11_MONITOR_WIDTHS do n=`expr $n + 1` eval "imagefile=\$$n" newimage=`X11_MONITOR_WIDTHS=$width "$0" -sh 100 "$imagefile"` append="$append '$newimage'" done tmpf=$TMPDIR/$cmd$$.png eval "$append 'png:$tmpf'" imagefile=`filecache "$tmpf"` rm "$tmpf" exec ls -d -- "$imagefile" fi ########################################## # Single image - scale to fit. imagefile=$1 shift bgx=`set -- $X11_MONITOR_WIDTHS; echo "$1"` bgy=$X11_Y # if stdin-ish name, fetch and convert to PNG case "$imagefile" in X:*|-|*:-|\|* | http://* | ftp://* ) imagefile=`fileof "$imagefile"` || exit 1 imagefile=`pngof "$imagefile"` || exit 1 ;; esac # get image size ix= iy= eval `imsize-gm "$imagefile" | awk '{print "ix="$1 " iy="$2}'` [ -n "$ix" -a -n "$iy" ] \ || { echo "$cmd: can't deduce source image size" >&2; exit 1; } convname=pngof set -- $gm convert "$imagefile" convname=$convname/sh$shade if [ "x$shade" != x100 ] then ## -modulate does the wrong thing ## set "$@" -modulate "$shade" case "$shade" in [0-9]) mulshade=0.0$shade ;; [0-9][0-9]*)mulshade=0.$shade ;; *) echo "$cmd: upsupported shade \"$shade\", expected 0-99" >&2 exit 1 ;; esac set "$@" \ -normalize \ -operator red Multiply "$mulshade" \ -operator green Multiply "$mulshade" \ -operator blue Multiply "$mulshade" fi convname=$convname/${bgx}x${bgy} bx=0 by=0 if [ "x${bgx}x${bgy}" != "x${ix}x${iy}" ] then # must rescale # compute width of borders awkf="BEGIN { ix=$ix; iy=$iy; bgx=$bgx; bgy=$bgy "' w=bgx; h=bgy } END { if (ix > bgx || iy > bgy) # too big - shrink if (ix/bgx > iy/bgy) # shrink X more than Y h=int(iy * (bgx/ix)) else # shrink Y more than X w=int(ix * (bgy/iy)) else # smaller image - scale up if (bgx/ix < bgy/iy) # scale X less - use that h=iy*bgx/ix else # scale Y less w=ix*bgy/iy w+=w%2 h+=h%2 bx=int((bgx-w)/2) by=int((bgy-h)/2) print "bx="bx " by="by }' eval `awk "$awkf"