| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #!/bin/env bash
- export GDK_BACKEND=x11
- # Specify device to avoid startup delays and scanning slowness.
- export SANE_DEFAULT_DEVICE=$(scanimage --formatted-device-list %d%n | grep libusb)
- # See if file exists, add a number to name if needed.
- function check_filename {
- path=$1
- folder=$(dirname "$path")
- file=$(basename "$path")
- extension="${file##*.}"
- filename="${file%.*}"
- if [[ -e "$path" ]]; then
- i=2
- while [[ -e "$folder/$filename-$i.$extension" ]]; do
- let i++
- done
- filename=$filename-$i
- fi
- echo "$folder/$filename.$extension"
- }
- export -f check_filename # Make available to run_scan's command substitution
- # Run scanner. arguments:
- # 1-folder 2-prefix 3-timestamp 4-format 5-papersize
- # 6-resolution 7-iccprofile 8-colourmode
- function run_scan {
- format=$4
- res=$6
- colmode=$8
- icc=$7
- # Paper sizes.
- landscape=false
- case $5 in
- A4)
- x=210 y=297
- ;;
- A4-R)
- x=210 y=297 landscape=true
- ;;
- A5)
- x=148 y=210
- ;;
- A5-R)
- x=210 y=148
- # Force A4 instead, and cut later, if 150DPI..
- # Sane epson backend throws an out of memory error at ~A5R 150DPI.
- if [[ $res == "150" ]]; then
- x=210 y=297
- fi
- ;;
- B5)
- x=176 y=250
- ;;
- B5-R)
- x=176 y=250 landscape=true
- ;;
- LTR)
- x=216 y=279
- ;;
- LTR-R)
- x=216 y=279 landscape=true
- ;;
- esac
- filename=$1/$2-$(date +"%Y-%m-%d-%H%M%S").$format
- filename=$(check_filename $filename)
- userid=$(id -u $USER)
- tempname="/var/run/user/$userid/temp-$(date +%s).tif"
- echo "$colmode $res $x $y $filename"
- scanimage --mode=$colmode --resolution=$res -x $x -y $y --format=tiff --output-file=$tempname | \
- yad --progress --text="Scanning (TIFF)..." --width=300 --no-buttons --auto-close
- options=""
- [[ $landscape == true ]] && options="${options} -rotate 90"
- [[ $icc == "Profile-ax" ]] && options="${options} -profile /home/cinaeco/scan-dialog/scanner-ax.icm"
- [[ $icc == "Profile-al" ]] && options="${options} -profile /home/cinaeco/scan-dialog/scanner-al.icm"
- [[ $icc == "Profile-as" ]] && options="${options} -profile /home/cinaeco/scan-dialog/scanner-as.icm"
- echo "Options: $options"
- convert $options $tempname $tempname | \
- yad --progress --text="Modifying..." --width=300 --no-buttons --auto-close
- case $format in
- png)
- convert $tempname $filename | \
- yad --progress --text="Converting to $format..." --width=300 --no-buttons --auto-close
- ;;
- jpg)
- convert -quality 85 $tempname $filename | \
- yad --progress --text="Converting to $format..." --width=300 --no-buttons --auto-close
- ;;
- pdf)
- if [[ $colmode == "Lineart" ]]; then
- # PNGs compress BW images better.
- tempname2="${tempname%.tif}.png"
- convert $tempname $tempname2 | \
- yad --progress --text="Converting to png..." --width=300 --no-buttons --auto-close
- rm $tempname
- tempname=$tempname2
- else
- tempname2="${tempname%.tif}.jpg"
- convert -quality 85 $tempname $tempname2 | \
- yad --progress --text="Converting to jpg..." --width=300 --no-buttons --auto-close
- rm $tempname
- tempname=$tempname2
- fi
- convert $tempname $filename | \
- yad --progress --text="Converting to $format..." --width=300 --no-buttons --auto-close
- ;;
- tiff)
- cp $tempname $filename
- ;;
- esac
- size_raw=$(stat -c%s $filename)
- size_kb=$((size_raw/1024))
- yad --picture --title="Filesize: $size_kb KB ($format)" --size=fit --width=750 --height=1000 --filename=$tempname
- rm $tempname
- }
- export -f run_scan # Make function available to yad button command.
- # Display scan-dialog.
- yad --form --orient=hor --maximized --width=750 --height=250 --columns=3 --focus-field=5 \
- --title="Scanning Options" \
- --field="Scan Folder:SFL" /mnt/public/scans \
- --field="Filename Prefix:SFL" scan \
- --field="Timestamp:CB" Yes\!No \
- --field="File format:CB" jpg\!png\!pdf\!tiff \
- --field="Paper Size:CB" A4\!A4-R\!A5\!A5-R\!B5\!B5-R\!LTR\!LTR-R\! \
- --field="Resolution (DPI):CB" 75\!150\!^300\!600\!1200 \
- --field="ICC Profile:CB" Profile-ax\!Profile-al\!Profile-as\!None \
- --field="Scan Buttons:BTN" "" \
- --field="Colour:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 %6 %7 Color"' \
- --field="Grayscale:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 %6 %7 Gray"' \
- --field="B&W:FBTN" 'bash -c "run_scan %1 %2 %3 %4 %5 %6 %7 Lineart"' \
- --field="...:BTN" "" \
- --field="...:BTN" "" \
- --field="...:BTN" "" \
- --field="Presets:BTN" "" \
- --field="Art (Colour PNG):FBTN" 'bash -c "run_scan %1 art %3 png %5 %6 %7 Color"' \
- --field="Art (Colour JPG):FBTN" 'bash -c "run_scan %1 art %3 jpg %5 %6 %7 Color"' \
- --field="Doc (Colour PDF):FBTN" 'bash -c "run_scan %1 doc %3 pdf %5 %6 %7 Color"' \
- --field="Doc (Gray PDF):FBTN" 'bash -c "run_scan %1 doc %3 pdf %5 %6 %7 Gray"' \
- --field="Doc (B&W PDF):FBTN" 'bash -c "run_scan %1 doc %3 pdf %5 %6 %7 Lineart"' \
- --field="...:BTN" "" \
- --no-buttons --buttons-layout=center
|