wok view sane-backends/stuff/tazpanel/sane.cgi @ rev 18237
sane-backends: add tazpanel/sane.cgi
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sat Jul 25 13:48:18 2015 +0200 (2015-07-25) |
parents | |
children | 4ea96657834e |
line source
1 #!/bin/sh
2 #
3 # Scanner CGI interface - Scan documents via a browser
4 #
5 # (C) 2015 SliTaz GNU/Linux - BSD License
6 #
8 # Common functions from libtazpanel
9 . lib/libtazpanel
10 get_config
12 #------
13 # menu
14 #------
16 case "$1" in
17 menu)
18 TEXTDOMAIN_original=$TEXTDOMAIN
19 export TEXTDOMAIN='sane'
21 cat <<EOT
22 <li><a data-icon="text" href="sane.cgi"$(groups | grep -q scanner ||
23 echo ' data-root')>$(_ 'Scanner')</a></li>
24 EOT
25 export TEXTDOMAIN=$TEXTDOMAIN_original
26 exit
27 esac
29 TITLE="$(_ 'TazPanel - Hardware') - $(_ 'Scanner')"
31 inrange() {
32 local n=$1
33 [ $1 -lt $2 ] && n=$2
34 [ $1 -gt $3 ] && n=$3
35 echo $n
36 }
38 getgeometry() {
39 CMD=""
40 for i in l t x y ; do
41 j=$(inrange $(xPOST geometry_$i) $(xPOST ${i}_min) $(xPOST ${i}_max))
42 eval "geometry_$i=$j"
43 CMD="$CMD -$i $j"
44 # convert -crop XxY+L+T -resize XxY
45 done
46 for i in mode source contrast brightness ; do
47 [ "$(xPOST $i)" ] && CMD="$CMD --$i '$(xPOST $i)'"
48 done
49 resolution=${1:-0}
50 if [ $resolution -eq 0 ]; then
51 resolution=$(xPOST res_min)
52 width=$(GET width)
53 [ ${geometry_x:-0} -le 0 ] && geometry_x=$(xPOST x_max)
54 while [ $((${resolution:=150} * ${geometry_x%.*})) -lt ${width:-8192} ]; do
55 resolution=$(($resolution * 2))
56 done
57 fi
58 [ -d tmp ] || ln -s /tmp tmp
59 case "$device" in
60 fake*) echo "cat /usr/share/images/slitaz-background.jpg" ;;
61 *) echo "scanimage -d '$(echo $device | sed 's/,.*//')' --resolution '$(inrange $resolution $(xPOST res_min) $(xPOST res_max))dpi'$CMD"
62 esac
63 }
65 imgformat() {
66 tmp=$(mktemp -u -t tazsane.XXXXXX)
67 while read key name type exe pkg cmd ; do
68 case "$key" in
69 \#*) continue
70 esac
71 case "$1" in
72 list)
73 echo -n "<option value=\"$key\""
74 [ "$(which $exe 2> /dev/null)" ] ||
75 echo -n " disabled title=\"$exe not found: install $pkg\""
76 [ "$key" == "pnm" ] &&
77 echo -n " title=\"not supported by most browsers\""
78 echo ">$key" ;;
79 *)
80 case "$key" in
81 $(xPOST format)|'*')
82 case "$HTTP_USER_AGENT" in
84 # Tazweb has no download support
85 TazWe*) rm -f /tmp/$name
86 eval "$(getgeometry $(xPOST resolution)) $cmd >/tmp/$name" 2> $tmp.err
87 if [ -s /tmp/$name ]; then
88 info="Stored in /tmp/$name ($(stat -c %s /tmp/$name) bytes)."
89 else
90 error="$(sed 's|$|<br />|' $tmp.err)"
91 [ "$error" ] || error="I/O error"
92 fi
93 rm -f $tmp.* ;;
95 # Others should work
96 *) header "Content-Type: $type" \
97 "Content-Disposition: attachment; filename=$name" \
99 eval "$(getgeometry $(xPOST resolution)) $cmd"
100 rm -f $tmp.*
101 exit ;;
102 esac ;;
103 esac ;;
104 esac
105 done <<EOT
106 png tazsane.png image/png convert imagemagick > $tmp.pnm; convert $tmp.pnm png:-
107 jpeg tazsane.jpg image/jpeg convert imagemagick > $tmp.pnm; convert $tmp.pnm jpg:-
108 jpeg2000 tazsane.jp2 image/jpeg2000-image convert imagemagick > $tmp.pnm; convert $tmp.pnm jp2:-
109 tiff tazsane.tiff image/tiff convert imagemagick > $tmp.pnm; convert $tmp.pnm tiff:-
110 ps tazsane.ps application/postscript convert imagemagick > $tmp.pnm; convert -page A4+0+0 $tmp.pnm ps:-
111 pdf tazsane.pdf image/pdf convert imagemagick > $tmp.pnm; convert $tmp.pnm pdf:-
112 ocr1 tazsane-OCR1.txt text/plain gocr gocr | gocr -
113 ocr2 tazsane-OCR2.txt text/plain tesseract tesseract-ocr | tesseract stdin stdout
114 pnm tazsane.pnm image/pnm true slitaz
115 EOT
116 }
118 xPOST() {
119 [ "$preview" == "reset" ] || POST $@
120 }
122 tmpreview="$(POST tmpreview)"
123 find tmp/ -name 'tazsane*' -mmin +60 -prune -exec rm -f {} \;
125 device="$(POST device)"
126 preview="$(POST preview)"
127 info=""
128 error=""
130 case " $(POST) " in
131 *\ reset\ *)
132 unset device tmpreview
133 preview="reset" ;;
134 *\ preview\ *)
135 [ "$tmpreview" ] || tmpreview=$(mktemp -u -t tazsane.XXXXXX).png
136 tmp=$(mktemp -u -t tazsane.XXXXXX)
137 eval "$(getgeometry)" > $tmp.pnm 2> $tmp.err
138 if [ -s "$tmp.pnm" ]; then
139 convert $tmp.pnm $tmpreview > /dev/null 2>&1 ||
140 cp $tmp.pnm $tmpreview
141 else
142 error="$(sed 's|$|<br />|' $tmp.err)"
143 rm -f $tmpreview
144 fi
145 rm -f $tmp.pnm $tmp.err ;;
146 *\ scan\ *)
147 imgformat download ;;
148 esac
150 header
151 xhtml_header
152 [ -n "$error" ] && msg warn "$error"
153 [ -n "$info" ] && msg tip "$info"
154 if [ -z "$device" ]; then
155 [ -s sane-fake.log ] && all="$(sed 's/|/\n/g' sane-fake.log)" ||
156 all="$(scanimage -f '%d,%v %m|' | sed 's/|/\n/g')"
157 case "$(echo "$all" | wc -l)" in
158 1) if [ -z "$all" ]; then
159 msg warn 'No scanner found'
160 xhtml_footer
161 exit 0
162 fi
163 device="${all%|}" ;;
164 *) cat <<EOT
165 <section>
166 <header>
167 <form name="scanner" method="post">
168 Scanner
169 <select name="device" size=1>
170 EOT
171 echo "$all" | awk -F, '{ if (NF > 0) print "<option value=\"" $0 "\">" 1+i++ " - " $2 }'
172 cat <<EOT
173 </select>
174 <button data-icon="start">$(_ "Continue")</button>
175 </form>
176 </header>
177 </section>
178 EOT
179 xhtml_footer
180 exit 0 ;;
181 esac
182 fi
184 cat <<EOT
185 <section>
186 <form name="parameters" method="post">
187 <script language="JavaScript" type="text/javascript">
188 <!--
189 function new_width() {
190 document.parameters.action = "?width="+document.width
191 }
193 window.onresize = new_width
194 new_width()
195 -->
196 </script>
198 <header>
199 $(echo $device | sed 's/.*,//')
200 <div class="float-right">
201 <button name="scan" data-icon="start">$(_ "Scan")</button>
202 <button name="reset" data-icon="stop">$(_ "Reset")</button>
203 <button name="preview" data-icon="view">$(_ "Preview")</button>
204 </div>
205 </header>
207 <table class="wide" border="2" cellpadding="8" cellspacing="0">
208 <tr>
209 <td>Format
210 <select name="format" size=1>
211 $(imgformat list)
212 </select>
213 </td>
214 EOT
216 if [ "$(xPOST params)" ]; then
217 params="$(xPOST params | uudecode)"
218 else
219 params="$({
220 cat "$(echo $device | sed 's/,.*//').log" 2> /dev/null ||
221 scanimage --help -d "$(echo $device | sed 's/,.*//')"
222 } | awk '
223 function minmax()
224 {
225 if (match($2,"[0-9]")) {
226 i=$2; sub(/\.\..*/,"",i)
227 j=$2; sub(/.*\.\./,"",j)
228 sub(/\..*/,"",j); sub(/[dm%].*/,"",j)
229 k=$0; sub(/.* \[/,"",k); sub(/\].*/,"",k)
230 print $1 " " int(k) " " int(i) " " int(j)
231 }
232 }
234 function enum()
235 {
236 i=$0
237 if (index(i,"|")) {
238 sub(/^ *--*[a-z]* */,"",i)
239 sub(/dpi .*/,"",i); gsub(/ \[.*\].*/,"",i)
240 k=$0; sub(/.* \[/,"",k); sub(/\].*/,"",k)
241 gsub(/ /,"=",k)
242 print $1 " " k " enum " i
243 }
244 else minmax()
245 }
247 /Options specific to device/ { parse=1 }
248 {
249 if (parse != 1) next
250 if (/\[inactive\]/) next
251 if (match("-l-t-x-y", $1)) minmax()
252 if (match("--resolution--brightness--contrast--source--mode", $1)) enum()
253 }
254 ')"
255 fi
256 output="$(echo "$params" | while read name def min max ; do
257 name="${name##*-}"
258 def="${def//=/ }"
259 if [ "$min" == "enum" ]; then
260 res_min=1000000
261 res_max=0
262 echo "<td>$name"
263 echo -n "<select name=\"$name\" size=1"
264 [ "$name" == "resolution" ] && echo -n " onchange=showGeometry()"
265 echo ">"
266 IFS="|"; set -- $max ; unset IFS
267 while [ "$1" ]; do
268 echo -n "<option value=\"$1\""
269 [ "$(xPOST $name)" == "$1" ] && echo -n " selected"
270 [ -z "$(xPOST $name)" -a "$def" == "$1" ] && echo -n " selected"
271 echo ">$(_ "$1")"
272 if [ "$name" == "resolution" ]; then
273 [ $res_max -lt $1 ] && res_max=$1
274 [ $res_min -gt $1 ] && res_min=$1
275 fi
276 shift
277 done
278 echo "</select>"
279 else
280 [ "$(xPOST $name)" ] && def=$(xPOST $name)
281 [ $def -lt $min ] && def=$min
282 [ $def -gt $max ] && def=$max
283 f="$(_ "$name")<input name=\"$name\" value=\"$def\""
284 u=""
285 case "$name" in
286 x|y|l|t) cat <<EOT
287 :${name}_max=$max
288 <input type="hidden" name="${name}_min" value="$min">
289 <input type="hidden" name="${name}_max" value="$max">
290 EOT
291 while read name2 n2 id val; do
292 [ "$name" == "$name2" ] || continue
293 [ "$(xPOST geometry_$name)" ] &&
294 val="$(xPOST geometry_$name)"
295 f="$(_ "$n2")<input name=\"geometry_$name\" id=\"$id\" value=\"$val\""
296 u=" mm"
297 break
298 done <<EOT
299 l X-Offset x 0
300 t Y-Offset y 0
301 x Width width $max
302 y Height height $max
303 EOT
304 [ "$newline" ] || echo "</tr><tr>"
305 newline=$name
306 esac
307 [ "$name" == "resolution" ] && f="$f onchange=showGeometry()"
308 echo "<td>$f type=\"text\" title=\"$min .. $max\" size=4 maxlength=4>$u"
309 res_min=$min
310 res_max=$max
311 fi
312 case "$name" in
313 resolution) cat <<EOT
314 <input type="hidden" name="res_min" value="$res_min">
315 <input type="hidden" name="res_max" value="$res_max">
316 dpi
317 EOT
318 esac
319 echo "</td>"
320 done)"
321 echo "$output" | sed '/^:/d'
323 org_x=$(xPOST geometry_x); [ "$org_x" ] || org_x=$(echo "$output" | sed '/^:x_max=/!d;s/.*=//')
324 org_y=$(xPOST geometry_y); [ "$org_y" ] || org_y=$(echo "$output" | sed '/^:y_max=/!d;s/.*=//')
326 cat <<EOT
327 </tr>
328 </table>
329 <input type="hidden" name="tmpreview" value="$tmpreview">
330 <input type="hidden" name="device" value="$device">
331 <input type="hidden" name="params" value="$(echo "$params" | uuencode -m -)">
332 <script language="JavaScript" type="text/javascript">
333 <!--
334 function setGeometry(x,y) {
335 document.parameters.geometry_x.value = x;
336 document.parameters.geometry_y.value = y;
337 cropSetFrameByInput();
338 }
340 function showGeometry() {
341 var resolution = document.parameters.resolution.value;
342 if (resolution) {
343 resolution /= 25.4;
344 var x = Math.floor(document.parameters.geometry_x.value * resolution);
345 var y = Math.floor(document.parameters.geometry_y.value * resolution);
346 alert((Math.round(x * y / 100000)/10) + ' Mpixels\n' + x + 'x' + y);
347 }
348 }
349 -->
350 </script>
352 <footer align="center">
353 EOT
354 awk -vox=$org_x -voy=$org_y 'END {
355 x=210*4; y=297*4; n=0; cnt=0;
356 while (cnt < 9) {
357 if (ox +1 >= x && oy +1 >= y) {
358 print " <a href=\"javascript:setGeometry(" x "," y ")\">DIN-A" n "</a>"
359 cnt++
360 }
361 if (ox +1 >= x && oy +1 >= y) {
362 print " <a href=\"javascript:setGeometry(" y "," x ")\">DIN-A" n "L</a>"
363 cnt++
364 }
365 tmp=x; x=y/2; y=tmp
366 n++
367 }
368 }' < /dev/null
370 cat <<EOT
371 </footer>
372 </form>
373 </section>
374 EOT
376 [ -s "$tmpreview" ] && cat <<EOT
377 <div margin="15px" style="overflow-x: auto">
378 <script type="text/javascript" src="lib/crop.js"></script>
379 <img src="$tmpreview" onload=cropInit(this,'x','y','width','height')>
380 </div>
381 EOT
382 xhtml_footer