background_reshape 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/ash
  2. #Barry Kauler 2009
  3. #the background image is at /usr/share/backgrounds/default.jpg
  4. #this may have the wrong dimensions to suit either a 'normal' or a 'widescreen' monitor.
  5. #for now, keep this simple, assume image is suitable for normal screen, truncate vertically
  6. #if widescreen.
  7. #v424 fix maths.
  8. #v424 move ORIGINAL-* images to /usr/share/backgrounds_original
  9. #110831 support png images.
  10. #111013 Karl Godt: 'dc' can return , instead of . in numeric values, for non-english locale.
  11. #130326 Karl Godt: Added MODE option to be able to swith between math by BK and math by /bin/sh.
  12. #130327 Doubble Quotes around Files and Directories to support Spaces.
  13. #130327 Karl Godt: Logfile is /tmp/backg.log.
  14. #130327 wjaguar: Rewrote unnecessarily tortuous math & string handling
  15. LOG=/tmp/backg.log
  16. exec 1>>$LOG 2>&1
  17. : << 'COMMENTED_OUT' # Mode not used anymore
  18. [ "$DEBUG" ] && echo "DEBUG=$DEBUG"
  19. #below set this if wanted (un-comment by deleting the prceeding # (number-sign):
  20. #MODE=old
  21. [ "$MODE" ] || {
  22. case "$1" in
  23. "-m" ) MODE=$2 ; shift 2 ;;
  24. "-m"* ) MODE=${1#-m} ; shift 1 ;;
  25. "--mode="* ) MODE=${1#--mode=} ; shift 1 ;;
  26. esac
  27. FILENAME=$1
  28. }
  29. echo "MODE=$MODE"
  30. echo "FILENAME=$FILENAME"
  31. COMMENTED_OUT
  32. #accept optional image file as passed param...
  33. [ -f /usr/share/backgrounds/default.png ] && IMAGEFILE="/usr/share/backgrounds/default.png"
  34. [ -f /usr/share/backgrounds/default.jpg ] && IMAGEFILE="/usr/share/backgrounds/default.jpg"
  35. #[ $1 ] && IMAGEFILE="$1"
  36. [ "$*" ] && IMAGEFILE="$@"
  37. [ "$IMAGEFILE" = "" ] && exit
  38. echo "IMAGEFILE=$IMAGEFILE"
  39. #v423 precaution, if someone has chosen the backup original...
  40. [ "$IMAGEFILE" != "${IMAGEFILE#ORIGINAL}" ] && exit
  41. [ -f "$IMAGEFILE" ] || { echo "$0: $IMAGEFILE is not a file ..?"; exit; }
  42. IMAGEPATH=${IMAGEFILE%/*}
  43. [ "$IMAGEPATH" = "$IMAGEFILE" ] && IMAGEPATH=`pwd`
  44. mkdir -p "${IMAGEPATH}_original" #v424
  45. IMAGEBASE=${IMAGEFILE##*/}
  46. case $IMAGEBASE in
  47. *.jpg|*.jpeg|*.JPG|*.JPEG) IMAGEPNM='jpegtopnm'
  48. PNMIMAGE="pnmtojpeg -quality=85" ;;
  49. *.png|*.PNG) IMAGEPNM='pngtopnm'
  50. PNMIMAGE="pnmtopng" ;;
  51. *) echo "$0: Only support for PNG and JPG files"
  52. exit ;;
  53. esac
  54. echo "IMAGEPATH=$IMAGEPATH IMAGEBASE=$IMAGEBASE IMAGEPNM=$IMAGEPNM"
  55. # Safety check
  56. which $IMAGEPNM >/dev/null 2>&1 || { echo "No $IMAGEPNM utility!" ; exit ; }
  57. which ${PNMIMAGE%% *} >/dev/null 2>&1 || { echo "No $PNMIMAGE utility!" ; exit ; }
  58. #first boot, maybe trying different video modes, so this causes reset each time...
  59. if [ ! -f "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" ];then #v424
  60. cp -f "$IMAGEFILE" "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" #v424
  61. else
  62. cp -f "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" "$IMAGEFILE" #restore. v424
  63. fi
  64. read IMAGEHORIZ IMAGEDIMS IMAGEVERT IMAGEDIMS << EOF
  65. `$IMAGEPNM "$IMAGEFILE" 2>/dev/null | pamfile 2>&1 | grep 'stdin:' | cut -f 2 -d ','`
  66. EOF
  67. echo "IMAGEHORIZ=$IMAGEHORIZ IMAGEVERT=$IMAGEVERT"
  68. read ROOTDIMS ROOTHORIZ ROOTVERT << EOF
  69. `xwininfo -root | grep ' \-geometry ' | cut -f 1 -d '+' | tr 'x' ' '`
  70. EOF
  71. #v423 some math gymnastics involved here..
  72. IMGVERTSCALED=$(( ( $ROOTHORIZ * $IMAGEVERT ) / $IMAGEHORIZ ))
  73. NEWVERT=$(( ( $IMAGEHORIZ * $ROOTVERT ) / $ROOTHORIZ )) #v424 fix.
  74. echo "IMGVERTSCALED=$IMGVERTSCALED NEWVERT=$NEWVERT" #TEST
  75. #this method cuts equal amount from top and bottom...
  76. if [ "$NEWVERT" -gt "0" ] && [ $NEWVERT -lt $IMAGEVERT ] #precaution
  77. then
  78. PAMCUT=pamcut
  79. which $PAMCUT >/dev/null 2>&1 || PAMCUT=pnmcut #older netpbm.
  80. which $PAMCUT >/dev/null 2>&1 || exit # no netpbm at all
  81. CUTVERT=$(( $IMAGEVERT - $NEWVERT ))
  82. $IMAGEPNM "${IMAGEPATH}_original/ORIGINAL-$IMAGEBASE" | \
  83. $PAMCUT -left=0 -top=$(( $CUTVERT / 2 )) -height=$NEWVERT | \
  84. $PNMIMAGE > "$IMAGEFILE"
  85. fi
  86. ###END###