qubes.GetImageRGBA 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/bin/sh
  2. set -e
  3. read filename
  4. ICON_MAXSIZE=512
  5. if [ "${filename%%:*}" = xdgicon ]; then
  6. filename="$(/usr/lib/qubes/xdg-icon "${filename#*:}" "$ICON_MAXSIZE")"
  7. forcemaxsize="$ICON_MAXSIZE"
  8. [ -n "${filename}" ]
  9. elif [ "${filename}" = "-" ] || [ "${filename##*:}" = "-" ]; then
  10. tmpfile="$(mktemp /tmp/qimg-XXXXXXXX)"
  11. cat > "${tmpfile}"
  12. if [ "${filename##*:}" = "-" ]; then
  13. filename="${filename%:*}:${tmpfile}"
  14. else
  15. filename="${tmpfile}"
  16. fi
  17. elif ! [ -r "${filename}" ]; then
  18. exit 1
  19. fi
  20. s="$(identify -format '%w %h %m' "$filename")"
  21. w="$(echo "$s"|cut -d " " -f 1)"
  22. h="$(echo "$s"|cut -d " " -f 2)"
  23. m="$(echo "$s"|cut -d " " -f 3)"
  24. if [ "$m" = SVG ]; then
  25. tmpfile2="$(mktemp /tmp/qimg-XXXXXXXX.png)"
  26. rsvg-convert -o "$tmpfile2" "$filename"
  27. # downscale the image if necessary
  28. if [ -n "$forcemaxsize" -a \
  29. \( "$w" -gt "$forcemaxsize" -o "$h" -gt "$forcemaxsize" \) ]; then
  30. convert "$tmpfile2" -scale "${forcemaxsize}x${forcemaxsize}" "$tmpfile2"
  31. # read the size again, because icon may not be a square
  32. s="$(identify -format '%w %h' "$tmpfile2")"
  33. w="$(echo "$s"|cut -d " " -f 1)"
  34. h="$(echo "$s"|cut -d " " -f 2)"
  35. fi
  36. filename="$tmpfile2"
  37. fi
  38. echo "$w $h"
  39. convert -depth 8 -size "${w}x${h}" "$filename" rgba:-
  40. [ -n "${tmpfile}" ] && rm -f "${tmpfile}" || true
  41. [ -n "${tmpfile2}" ] && rm -f "${tmpfile2}" || true
  42. # vim: ft=sh ts=4 sw=4 et