Debian Codegraph @

filter/braille/filters/imagetobrf.in

raw
1 #!/bin/bash
2
3 #
4 # Copyright (c) 2015-2018 Samuel Thibault <[email protected]>
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 # THE SOFTWARE.
25 #
26
27 # Make sure we have enough options
28 if [ $# != 5 -a $# != 6 ]; then
29 echo "ERROR: $0 jobid user name nb options [filename]" >&2
30 exit 1
31 fi
32
33 NB=$4
34 OPTIONS=$5
35 FILE=$6
36
37 OUTPUT_FORMAT=brf
38
39 case $0 in
40 *imagetoubrl*) OUTPUT_FORMAT=ubrl ;;
41 esac
42
43 . @CUPS_DATADIR@/braille/cups-braille.sh
44
45 checkTool convert imagemagick "embossing images"
46
47 NEGATE=$(getOption Negate)
48 case "$NEGATE" in
49 True|true) NEGATE=-negate ;;
50 False|false) NEGATE= ;;
51 *)
52 printf "ERROR: Option Negate must either True or False, got '%s'\n" "$NEGATE" >&2
53 exit 1
54 ;;
55 esac
56
57 ROTATE=$(getOption Rotate)
58 case "$ROTATE" in
59 90\>|270\>)
60 if [ "$GRAPHICWIDTH" -gt "$GRAPHICHEIGHT" ]
61 then
62 # Landscape paper, rotate to landscape instead of to portrait
63 ROTATE=${ROTATE/\>/\<}
64 fi
65 ;;
66 0|90|270|180) ;;
67 *)
68 printf "ERROR: Option Rotate must be a valid rotation value, got '%s'\n" "$ROTATE" >&2
69 exit 1
70 ;;
71 esac
72
73 MIRROR=$(getOption mirror)
74 case "$MIRROR" in
75 True|true) MIRROR="-flop" ;;
76 False|false) MIRROR="" ;;
77 *)
78 printf "ERROR: Option mirror must either True or False, got '%s'\n" "$MIRROR" >&2
79 exit 1
80 ;;
81 esac
82
83 PAGE="-page ${TOTALGRAPHICWIDTH}x${TOTALGRAPHICHEIGHT}+${GRAPHICHOFFSET}+${GRAPHICVOFFSET}"
84
85 RESIZE=$(getOption fitplot)
86 case "$RESIZE" in
87 True|true) RESIZE="-size ${GRAPHICWIDTH}x${GRAPHICHEIGHT} -resize ${GRAPHICWIDTH}x${GRAPHICHEIGHT}" ;;
88 False|false) RESIZE="-crop ${GRAPHICWIDTH}x${GRAPHICHEIGHT}+${GRAPHICHOFFSET}+${GRAPHICVOFFSET}" ;;
89 *)
90 printf "ERROR: Option fitplot must either True or False, got '%s'\n" "$RESIZE" >&2
91 exit 1
92 ;;
93 esac
94
95 EDGE=$(getOption Edge)
96 EDGEFACTOR=$(getOptionNumber EdgeFactor)
97
98 CANNY_RADIUS=$(getOptionNumber CannyRadius)
99 CANNY_SIGMA=$(getOptionNumber CannySigma)
100 CANNY_LOWER=$(getOptionNumber CannyLower)
101 CANNY_UPPER=$(getOptionNumber CannyUpper)
102
103 case $EDGE in
104 None) WORK=$NEGATE;;
105 Edge) WORK="$NEGATE -edge $EDGEFACTOR -negate" ;;
106 Canny) WORK="-canny ${CANNY_RADIUS}x${CANNY_SIGMA}+$CANNY_LOWER%+$CANNY_UPPER% -negate" ;;
107 *)
108 printf "ERROR: Unknown Edge option value '%s'\n" "$EDGE" >&2
109 exit 1
110 ;;
111 esac
112
113 RENDER_CALL="convert $WORK -rotate $ROTATE $PAGE $RESIZE $MIRROR -flatten - $OUTPUT_FORMAT:-"
114
115 # Now proceeed
116 echo "INFO: Converting image" 1>&2
117 if [ -z "$FILE" ]
118 then
119 printf "DEBUG: Calling %s from stdin\n" "$RENDER_CALL" 1>&2
120 $RENDER_CALL | sed -e '/^\(Width\|X\|Y\): [0-9]*$/,/^$/d' | addmargins
121 else
122 printf "DEBUG: Calling %s on '%s'\n" "$RENDER_CALL" "$FILE" 1>&2
123 $RENDER_CALL < "$FILE" | sed -e '/^\(Width\|X\|Y\): [0-9]*$/,/^$/d' | addmargins
124 fi
125 echo "INFO: Ready" >&2