qubes.GetImageRGBA 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/bin/sh
  2. set -e
  3. read -r 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" ] && \
  29. ( [ "$w" -gt "$forcemaxsize" ] || [ "$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. if [ -n "${tmpfile}" ]; then
  41. rm -f "${tmpfile}"
  42. fi
  43. if [ -n "${tmpfile2}" ]; then
  44. rm -f "${tmpfile2}"
  45. fi
  46. # vim: ft=sh ts=4 sw=4 et