qubes.GetImageRGBA 1.4 KB

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