wok view sane-backends/stuff/tazpanel/sane.cgi @ rev 18245

Add tazpanel-extra
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jul 31 23:33:17 2015 +0200 (2015-07-31)
parents 4ea96657834e
children a705d8e80b1c
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 ARGS=""
41 for i in x y l t ; do
42 j=$(inrange $(xPOST geometry_$i) $(xPOST ${i}_min) $(xPOST ${i}_max))
43 eval "geometry_$i=$j"
44 CMD="$CMD -$i $j"
45 ARGS="$ARGS $j"
46 done
47 for i in mode source contrast brightness ; do
48 [ "$(xPOST $i)" ] && CMD="$CMD --$i '$(xPOST $i)'"
49 done
50 resolution=${1:-0}
51 if [ $resolution -eq 0 ]; then
52 resolution=$(xPOST res_min)
53 width=$(GET width)
54 [ ${geometry_x:-0} -le 0 ] && geometry_x=$(xPOST x_max)
55 while [ $((${resolution:=150} * ${geometry_x%.*})) -lt ${width:-8192} ]; do
56 resolution=$(($resolution * 2))
57 done
58 fi
59 case "$(xPOST mode)" in
60 *lack*|*ineart*) mode="-monochrome" ;;
61 *ray*) mode="-colorspace gray" ;;
62 *) mode="" ;;
63 esac
64 [ -d tmp ] || ln -s /tmp tmp
65 case "$device" in
66 fake*) f=/usr/share/images/slitaz-banner.png
67 c="$(echo $ARGS $(identify $f | sed \
68 's/.* \([0-9]*\)x.*/\1/') $(GET width) $(POST x_max) | awk '
69 function a(x) { return int(($x * $5)/ $7); }
70 { printf "%dx%d+%d+%d -resize %dx%d",a(1),a(2),a(3),a(4),$6,int(($2*$6)/$1)}
71 ')"
72 cat <<EOT
73 if convert -crop $c $mode $f /tmp/sane$$.png 2> /dev/null ; then
74 cat /tmp/sane$$.png
75 rm -f /tmp/sane$$.png
76 else
77 cat $f
78 fi
79 EOT
80 ;;
81 *) echo -n "scanimage -d '$(echo $device | sed 's/,.*//')' --resolution '$(inrange $resolution $(xPOST res_min) $(xPOST res_max))dpi'$CMD"
82 if [ -z "$1" -a "$(which convert)" ]; then
83 echo -n "> /tmp/sane$$.pnm ; convert -resize "
84 echo $ARGS $(GET width) | awk '{ printf "%dx%d",$5,int(($2*$5)/$1)}'
85 echo -n " /tmp/sane$$.pnm /tmp/sane$$.png ;"
86 echo -n "cat /tmp/sane$$.png ; rm -f /tmp/sane$$.pn?"
87 fi
88 esac
89 }
91 imgformat() {
92 tmp=$(mktemp -u -t tazsane.XXXXXX)
93 while read key name type exe pkg cmd ; do
94 case "$key" in
95 \#*) continue
96 esac
97 case "$1" in
98 list)
99 echo -n "<option value=\"$key\""
100 [ "$(which $exe 2> /dev/null)" ] ||
101 echo -n " disabled title=\"$exe not found: install $pkg\""
102 [ "$key" == "pnm" ] &&
103 echo -n " title=\"not supported by most browsers\""
104 echo ">$key" ;;
105 *)
106 case "$key" in
107 $(xPOST format)|'*')
108 case "$HTTP_USER_AGENT" in
110 # Tazweb has no download support
111 TazWe*) rm -f /tmp/$name
112 eval "$(getgeometry $(xPOST resolution)) $cmd >/tmp/$name" 2> $tmp.err
113 if [ -s /tmp/$name ]; then
114 info="Stored in /tmp/$name ($(stat -c %s /tmp/$name) bytes)."
115 else
116 error="$(sed 's|$|<br />|' $tmp.err)"
117 [ "$error" ] || error="I/O error"
118 fi
119 rm -f $tmp.* ;;
121 # Others should work
122 *) header "Content-Type: $type" \
123 "Content-Disposition: attachment; filename=$name" \
125 eval "$(getgeometry $(xPOST resolution)) $cmd"
126 rm -f $tmp.*
127 exit ;;
128 esac ;;
129 esac ;;
130 esac
131 done <<EOT
132 png tazsane.png image/png convert imagemagick > $tmp.pnm; convert $tmp.pnm png:-
133 jpeg tazsane.jpg image/jpeg convert imagemagick > $tmp.pnm; convert $tmp.pnm jpg:-
134 jpeg2000 tazsane.jp2 image/jpeg2000-image convert imagemagick > $tmp.pnm; convert $tmp.pnm jp2:-
135 tiff tazsane.tiff image/tiff convert imagemagick > $tmp.pnm; convert $tmp.pnm tiff:-
136 ps tazsane.ps application/postscript convert imagemagick > $tmp.pnm; convert -page A4+0+0 $tmp.pnm ps:-
137 pdf tazsane.pdf image/pdf convert imagemagick > $tmp.pnm; convert $tmp.pnm pdf:-
138 ocr1 tazsane-OCR1.txt text/plain gocr gocr | gocr -
139 ocr2 tazsane-OCR2.txt text/plain tesseract tesseract-ocr | tesseract stdin stdout
140 pnm tazsane.pnm image/pnm true slitaz
141 EOT
142 }
144 xPOST() {
145 [ "$preview" == "reset" ] || POST $@
146 }
148 tmpreview="$(POST tmpreview)"
149 find tmp/ -name 'tazsane*' -mmin +60 -prune -exec rm -f {} \;
151 device="$(POST device)"
152 preview="$(POST preview)"
153 info=""
154 error=""
156 case " $(POST) " in
157 *\ reset\ *)
158 unset device tmpreview
159 preview="reset" ;;
160 *\ preview\ *)
161 [ "$tmpreview" ] || tmpreview=$(mktemp -u -t tazsane.XXXXXX).png
162 tmp=$(mktemp -u -t tazsane.XXXXXX)
163 eval "$(getgeometry)" > $tmp.pnm 2> $tmp.err
164 if [ -s "$tmp.pnm" ]; then
165 convert $tmp.pnm $tmpreview > /dev/null 2>&1 ||
166 cp $tmp.pnm $tmpreview
167 else
168 error="$(sed 's|$|<br />|' $tmp.err)"
169 rm -f $tmpreview
170 fi
171 rm -f $tmp.pnm $tmp.err ;;
172 *\ scan\ *)
173 imgformat download ;;
174 esac
176 header
177 xhtml_header
178 [ -n "$error" ] && msg warn "$error"
179 [ -n "$info" ] && msg tip "$info"
180 if [ -z "$device" ]; then
181 all="$(scanimage -f '%d,%v %m|'|cat - sane-fake.log|sed 's/|/\n/g')"
182 case "$(echo "$all" | wc -l)" in
183 1) if [ -z "$all" ]; then
184 msg warn "$(_ "No scanner found")"
185 msg tip "$(_ "You can test this GUI with ")\
186 <a href=\"/user/pkgs.cgi?info=fake-sane\">fake-sane</a>"
187 xhtml_footer
188 exit 0
189 fi
190 device="${all%|}" ;;
191 *)
192 suggested=""
193 while read exe pkg msg; do
194 [ "$(which $exe 2> /dev/null)" ] && continue
195 suggested="$suggested
196 <li><a href=\"/user/pkgs.cgi?info=$pkg\">$pkg</a>&nbsp;$msg</li>"
197 done <<EOT
198 convert imagemagick $(_ "to preview images and support more image formats")
199 gocr gocr $(_ "a basic optical character recognition")
200 tesseract tesseract-ocr $(_ "a better optical character recognition")
201 EOT
202 [ "$suggested" ] &&
203 msg tip "$(_ "You may need to install:") <ol>$suggested</ol>"
204 cat <<EOT
205 <section>
206 <header>
207 <form name="scanner" method="post">
208 Scanner
209 <select name="device" size=1>
210 EOT
211 echo "$all" | awk -F, '{ if (NF > 0) print "<option value=\"" $0 "\">" 1+i++ " - " $2 }'
212 cat <<EOT
213 </select>
214 <button data-icon="start">$(_ "Continue")</button>
215 </form>
216 </header>
217 </section>
218 EOT
219 xhtml_footer
220 exit 0 ;;
221 esac
222 fi
224 cat <<EOT
225 <section>
226 <form name="parameters" method="post">
227 <script language="JavaScript" type="text/javascript">
228 <!--
229 function new_width() {
230 document.parameters.action = "?width="+(document.width-30)
231 }
233 window.onresize = new_width
234 new_width()
235 -->
236 </script>
238 <header>
239 $(echo $device | sed 's/.*,//')
240 <div class="float-right">
241 <button name="scan" data-icon="start">$(_ "Scan")</button>
242 <button name="reset" data-icon="refresh">$(_ "Reset")</button>
243 <button name="preview" data-icon="view">$(_ "Preview")</button>
244 </div>
245 </header>
247 <table class="wide" border="2" cellpadding="8" cellspacing="0">
248 <tr>
249 <td>Format
250 <select name="format" size=1>
251 $(imgformat list)
252 </select>
253 </td>
254 EOT
256 if [ "$(xPOST params)" ]; then
257 params="$(xPOST params | uudecode)"
258 else
259 params="$({
260 cat "$(echo $device | sed 's/,.*//').log" 2> /dev/null ||
261 scanimage --help -d "$(echo $device | sed 's/,.*//')"
262 } | awk '
263 function minmax()
264 {
265 if (match($2,"[0-9]")) {
266 i=$2; sub(/\.\..*/,"",i)
267 j=$2; sub(/.*\.\./,"",j)
268 sub(/\..*/,"",j); sub(/[dm%].*/,"",j)
269 k=$0; sub(/.* \[/,"",k); sub(/\].*/,"",k)
270 print $1 " " int(k) " " int(i) " " int(j)
271 }
272 }
274 function enum()
275 {
276 i=$0
277 if (index(i,"|")) {
278 sub(/^ *--*[a-z]* */,"",i)
279 sub(/dpi .*/,"",i); gsub(/ \[.*\].*/,"",i)
280 k=$0; sub(/.* \[/,"",k); sub(/\].*/,"",k)
281 gsub(/ /,"=",k)
282 print $1 " " k " enum " i
283 }
284 else minmax()
285 }
287 /Options specific to device/ { parse=1 }
288 {
289 if (parse != 1) next
290 if (/\[inactive\]/) next
291 if (match("-l-t-x-y", $1)) minmax()
292 if (match("--resolution--brightness--contrast--source--mode", $1)) enum()
293 }
294 ')"
295 fi
296 output="$(n=$(echo "$params" | wc -l); echo "$params" | \
297 while read name def min max ; do
298 name="${name##*-}"
299 def="${def//=/ }"
300 if [ "$min" == "enum" ]; then
301 res_min=1000000
302 res_max=0
303 echo "<td>$name"
304 echo -n "<select name=\"$name\" size=1"
305 [ "$name" == "resolution" ] && echo -n " onchange=showGeometry()"
306 echo ">"
307 IFS="|"; set -- $max ; unset IFS
308 while [ "$1" ]; do
309 echo -n "<option value=\"$1\""
310 [ "$(xPOST $name)" == "$1" ] && echo -n " selected"
311 [ -z "$(xPOST $name)" -a "$def" == "$1" ] && echo -n " selected"
312 echo ">$(_ "$1")"
313 if [ "$name" == "resolution" ]; then
314 [ $res_max -lt $1 ] && res_max=$1
315 [ $res_min -gt $1 ] && res_min=$1
316 fi
317 shift
318 done
319 echo "</select>"
320 else
321 [ "$(xPOST $name)" ] && def=$(xPOST $name)
322 [ $def -lt $min ] && def=$min
323 [ $def -gt $max ] && def=$max
324 f="$(_ "$name") <input name=\"$name\" value=\"$def\""
325 u=""
326 case "$name" in
327 x|y|l|t) cat <<EOT
328 :${name}_max=$max
329 <input type="hidden" name="${name}_min" value="$min">
330 <input type="hidden" name="${name}_max" value="$max">
331 EOT
332 while read name2 n2 id val; do
333 [ "$name" == "$name2" ] || continue
334 [ "$(xPOST geometry_$name)" ] &&
335 val="$(xPOST geometry_$name)"
336 f="$(_ "$n2")<input name=\"geometry_$name\" id=\"$id\" value=\"$val\""
337 u="&nbsp;mm"
338 break
339 done <<EOT
340 l X-Offset x 0
341 t Y-Offset y 0
342 x Width width $max
343 y Height height $max
344 EOT
345 esac
346 [ "$name" == "resolution" ] && f="$f onchange=showGeometry()"
347 echo "<td>$f type=\"text\" title=\"$min .. $max\" size=4 maxlength=4>$u"
348 res_min=$min
349 res_max=$max
350 fi
351 case "$name" in
352 resolution) cat <<EOT
353 <input type="hidden" name="res_min" value="$res_min">
354 <input type="hidden" name="res_max" value="$res_max">
355 &nbsp;dpi
356 EOT
357 esac
358 echo "</td>"
359 n=$(($n - 2))
360 case "$n" in
361 1|2) echo "</tr><tr>"
362 esac
363 done)"
364 echo "$output" | sed '/^:/d'
366 org_x=$(xPOST geometry_x); [ "$org_x" ] || org_x=$(echo "$output" | sed '/^:x_max=/!d;s/.*=//')
367 org_y=$(xPOST geometry_y); [ "$org_y" ] || org_y=$(echo "$output" | sed '/^:y_max=/!d;s/.*=//')
369 cat <<EOT
370 </tr>
371 </table>
372 <input type="hidden" name="tmpreview" value="$tmpreview">
373 <input type="hidden" name="device" value="$device">
374 <input type="hidden" name="params" value="$(echo "$params" | uuencode -m -)">
375 <script language="JavaScript" type="text/javascript">
376 <!--
377 function setGeometry(x,y) {
378 document.parameters.geometry_x.value = x;
379 document.parameters.geometry_y.value = y;
380 cropSetFrameByInput();
381 }
383 function showGeometry() {
384 var resolution = document.parameters.resolution.value;
385 if (resolution) {
386 resolution /= 25.4;
387 var x = Math.floor(document.parameters.geometry_x.value * resolution);
388 var y = Math.floor(document.parameters.geometry_y.value * resolution);
389 alert((Math.round(x * y / 100000)/10) + ' Mpixels\n' + x + 'x' + y);
390 }
391 }
392 -->
393 </script>
395 <footer align="center">
396 EOT
397 awk -vox=$org_x -voy=$org_y 'END {
398 x=210*4; y=297*4; n=0; cnt=0;
399 while (cnt < 9) {
400 if (ox +1 >= x && oy +1 >= y) {
401 print "&nbsp;<a href=\"javascript:setGeometry(" x "," y ")\">DIN-A" n "</a>"
402 cnt++
403 }
404 if (ox +1 >= x && oy +1 >= y) {
405 print "&nbsp;<a href=\"javascript:setGeometry(" y "," x ")\">DIN-A" n "L</a>"
406 cnt++
407 }
408 tmp=x; x=y/2; y=tmp
409 n++
410 }
411 }' < /dev/null
413 cat <<EOT
414 </footer>
415 </form>
416 </section>
417 EOT
419 [ -s "$tmpreview" ] && cat <<EOT
420 <div margin="15px" style="overflow-x: auto">
421 <script type="text/javascript" src="lib/crop.js"></script>
422 <img src="$tmpreview" onload=cropInit(this,'x','y','width','height')>
423 </div>
424 EOT
425 xhtml_footer