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

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