chiark / gitweb /
WIP check commodity, before delete pixel searcher
[ypp-sc-tools.web-live.git] / pctb / yppsc-ocr-resolver
1 #!/usr/bin/wish
2
3 # helper program for OCR in PCTB upload client
4
5 # This is part of ypp-sc-tools, a set of third-party tools for assisting
6 # players of Yohoho Puzzle Pirates.
7 #
8 # Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23 # Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24 # are used without permission.  This program is not endorsed or
25 # sponsored by Three Rings.
26
27
28 # invocation:
29 #  run this without args
30 #  then on stdin write
31 #     one line which is a Tcl list for unk_{l,r} unk_contexts glyphsdone etc.
32 #     the xpm in the format expected
33 #  then expect child to exit 0, or write a single 0 byte to fd 4
34 #  if it wrote a byte to fd 4, it can take another question
35
36
37 proc manyset {list args} {
38     foreach val $list var $args {
39         upvar 1 $var my
40         set my $val
41     }
42 }
43
44
45 #---------- display core ----------
46
47 set mul 6
48 set inter 1
49
50 set gotsh 20
51 set csrh 20
52 set ctxh 20
53
54 proc init_widgets {} {
55     # idempotent
56     global csrh gotsh ctxh
57
58     if {[winfo exists .d]} return
59     
60     frame .d
61
62     image create bitmap image/main
63     label .d.mi -image image/main -borderwidth 0
64
65     frame .d.csr -bg black -height $csrh
66     frame .d.got -bg black -height $gotsh
67     frame .d.ctx -bg black
68
69     image create bitmap image/cursor -data \
70 {#define csr_width 11
71 #define csr_height 11
72 static unsigned char csr_bits[] = {
73    0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x21, 0x04, 0x22, 0x02, 0x25, 0x05,
74    0xaa, 0x02, 0x74, 0x01, 0xa8, 0x00, 0x70, 0x00, 0x20, 0x00};
75 }
76
77     frame .d.csr.csr
78     label .d.csr.csr.l -image image/cursor -compound left
79     entry .d.csr.csr.e -bd 0
80     pack .d.csr.csr.l -side left
81
82     frame .d.mi.csr_0 -bg white -width 1
83     frame .d.mi.csr_1 -bg white -width 1
84
85     pack .d.csr .d.mi .d.got .d.ctx -side top
86     pack .d
87
88     frame .help
89     pack .help
90 }
91
92 proc show_context {maxhv x ctxs} {
93     global mul
94     upvar 1 $maxhv maxh
95     set w .d.ctx.at$x
96     if {[llength $ctxs]==1} { set fg blue } { set fg yellow }
97     label $w -bg black -fg $fg -text [join $ctxs "/\n"] -justify left
98     place $w -x [expr {($x-1)*$mul}] -y 0
99     set wh [winfo reqheight $w]
100     if {$wh > $maxh} { set maxh $wh }
101 }
102
103 proc resize_widgets {} {
104     global mulcols mulrows csrh gotsh ctxh glyphsdone
105     global unk_l unk_contexts
106     
107     foreach w {.d.csr .d.got .d.ctx} {
108         $w configure -width $mulcols
109     }
110     #.d configure -height [expr {$csrh+$mulrows+$gotsh+$ctxh}]
111     foreach w {0 1} {
112         .d.mi.csr_$w configure -height $mulrows
113     }
114
115     eval destroy [winfo children .d.ctx]
116
117     set maxh 0
118     foreach {min max contexts got} $glyphsdone {
119         show_context maxh $min $contexts
120     }
121     show_context maxh $unk_l $unk_contexts
122     .d.ctx configure -height $maxh
123 }
124
125
126 #---------- xpm input processor ----------
127
128 proc read_xpm {f} {
129     global glyphsdone mul inter rhsmost_max unk_l unk_r mulcols mulrows
130     global cols rows wordmap
131     
132     set o {}
133     set y -3
134     while 1 {
135         if {[gets $f l] < 0} { error "huh? "}
136         if {![regexp {^"(.*)",$} $l dummy l]} {
137             append o "$l\n"
138             if {[regexp {^\}\;$} $l]} break
139             continue
140         }
141         if {$y==-3} {
142             manyset $l cols rows colours cpp
143             if {$colours!=2 || $cpp!=1} { error "$l ?" }
144
145             set chop_l [expr {$unk_l - 80}]
146             set chop_r [expr {$cols - $unk_l - 100}]
147             if {$chop_l<0} { set chop_l 0 }
148
149             set unk_l [expr {$unk_l - $chop_l}]
150             set unk_r [expr {$unk_r - $chop_l}]
151             set ngd {}
152             foreach {min max contexts got} $glyphsdone {
153                 lappend ngd \
154                     [expr {$min-$chop_l}] \
155                     [expr {$max-$chop_l}] \
156                     $contexts $got
157             }
158             set glyphsdone $ngd
159
160             set realcols $cols
161             set cols [expr {$cols - $chop_l - $chop_r}]
162             debug "NOW cols=$cols chop_l,r=$chop_l,$chop_r rows=$rows\
163                 $unk_l $unk_r $ngd"
164             
165             set mulcols [expr {$cols*$mul+$inter}]
166             set mulrows [expr {$rows*$mul+$inter}]
167             append o "\"$mulcols $mulrows 9 1\",\n"
168             for {set x 0} {$x<$cols} {incr x} { set wordmap($x) 0 }
169         } elseif {$y==-2} { # first pixel
170             append o \
171 "\"+ c #111\",
172 \"a c #800\",
173 \"A c #fcc\",
174 \"b c #00c\",
175 \"B c #fff\",
176 \"u c #000\",
177 \"U c #ff0\",
178 \"q c #000\",
179 \"Q c #ff0\",\n"
180         } elseif {$y==-1} { # 2nd pixel but we've already printed ours
181         } else {
182             set ybit [expr {1<<$y}]
183             set x 0
184             set ol "\"+"
185             set olh $ol
186             if {$chop_r>=0} {
187                 set l [string range $l $chop_l end-$chop_r]
188             } else {
189                 set l [string range $l $chop_l end]
190                 append l [string repeat " " [expr -$chop_r]]
191             }
192             foreach c [split $l ""] {
193                 set how "u"
194                 if {$x >= $unk_l && $x <= $unk_r} {
195                     set how q
196                 } else {
197                     set ab 0
198                     foreach {min max contexts got} $glyphsdone {
199                         set rhsmost_max $max
200                         if {$x >= $min && $x <= $max} {
201                             set how [lindex {a b} $ab]
202                             break
203                         }
204                         set ab [expr {!$ab}]
205                     }
206                 }
207                 switch -exact $c {
208                     " " { set p $how }
209                     "o" {
210                         set p [string toupper $how]
211                         incr wordmap($x) $ybit
212                     }
213                     default { error "$c ?" }
214                 }
215                 append ol "[string repeat $p [expr {$mul-$inter}]][
216                          string repeat + $inter]"
217                 append olh [string repeat + $mul]
218                 incr x
219             }
220             set ole "\",\n"
221             append ol $ole
222             append olh $ole
223             set olhn [string repeat $olh $inter]
224             if {!$y} { append o $olhn }
225             append o [string repeat $ol [expr {$mul-1}]]
226             append o $olhn
227         }
228         incr y
229     }
230     set data [exec xpmtoppm << $o]
231     image create photo image/main -data $data
232 }
233
234
235 #---------- per-invocation display ----------
236
237 proc draw_glyphsdone {} {
238     global glyphsdone mul inter
239     eval destroy [winfo children .d.got]
240     foreach {min max contexts got} $glyphsdone {
241         frame .d.got.m$min -bd 0 -background \#888
242         label .d.got.m$min.l -text "$got" -fg white -bg black -bd 0
243         pack .d.got.m$min.l -padx 1 -pady 1
244         place .d.got.m$min -x [expr {$min*$mul+$inter}] -y 0
245     }
246 }
247
248 proc startup_cursor {} {
249     global cur_already cur_mode cur_0 cur_1 last_ht
250     global glyphsdone unk_l unk_r
251     
252     set cur_already [expr {[llength $glyphsdone]/4-1}]
253     set cur_mode 1 ;# one of:   0 1 already text
254
255     set cur_0 $unk_l
256     set cur_1 [expr {$unk_r+1}]
257     set last_ht {}
258
259     recursor
260 }
261
262
263 #---------- runtime display and keystroke handling ----------
264
265 proc helptext {t} {
266     global last_ht
267     if {![string compare $t $last_ht]} return
268     eval destroy [grid slaves .help]
269     set y 0; foreach l $t {
270         set x 0; foreach c $l {
271             set w .help.at${x}x${y}
272             label $w -text $c
273             grid $w -row $y -column $x -padx 5
274             incr x
275         }
276         incr y
277     }
278     set last_ht $t
279 }
280
281 proc recursor/0 {} { recursor//01 0 }
282 proc recursor/1 {} { recursor//01 1 }
283 proc recursor//01 {z1} {
284     global mul rhsmost_max cols glyphsdone
285     upvar #0 cur_$z1 cur
286     .d.csr.csr.l configure -text {adjust}
287     place .d.csr.csr -x [expr {$cur*$mul - 7}]
288     bind_key space { othercursor }
289     bind_leftright_q cur_$z1 0 [expr {$cols-1}]
290     if {[llength $glyphsdone]} {
291         bind_key Tab { set cur_mode already; recursor }
292     } else {
293         bind_key Tab {}
294     }
295     bind_key Return {
296         if {$cur_0 != $cur_1} {
297             .d.csr.csr.e delete 0 end
298             set cur_mode text
299             recursor
300         }
301     }
302     helptext {
303         {{<- ->}   {move cursor, adjusting area to define}}
304         {Space     {switch to moving other cursor}}
305         {Return    {confirm location, enter letter(s)}}
306         {Tab       {switch to correcting earlier ocr}}
307         {Q         {quit and abandon OCR run}}
308     }
309 }
310 proc othercursor {} {
311     global cur_mode
312     set cur_mode [expr {!$cur_mode}]
313     recursor
314 }
315
316 proc recursor/text {} {
317     helptext {
318         {Return   {confirm entry of new glyph}}
319         {Escape   {abandon entry}}
320     }
321     unbind_all_keys
322     .d.csr.csr.l configure -text {define:}
323     pack .d.csr.csr.e -side left
324     focus .d.csr.csr.e
325     bind_key Return {
326         set strq [.d.csr.csr.e get]
327         if {[regexp {^(?:[!-[]|[]-~]|\\\\|\\x[0-9a-f]{2})+} $strq]} {
328             RETURN_RESULT DEFINE "$cur_0 $cur_1 $strq"
329         }
330     }
331     bind_key Escape {
332         bind_key Escape {}
333         pack forget .d.csr.csr.e
334         set cur_mode 1
335         recursor
336     }
337 }
338
339 proc recursor/already {} {
340     global mul
341     global glyphsdone
342     global cur_already mul
343     global glyphsdone cur_already mul
344     .d.csr.csr.l configure -text {correct}
345     set rmax [lindex $glyphsdone [expr {$cur_already*4}]]
346     place .d.csr.csr -x [expr {$rmax*$mul-3}]
347     bind_key Return {}
348     bind_key space {}
349     bind_leftright_q cur_already 0 [expr {[llength $glyphsdone]/4-1}]
350     bind_key Tab { bind_key Delete {}; set cur_mode 1; recursor }
351     bind_key Delete {
352         RETURN_RESULT DELETE [lrange $glyphsdone \
353                                   [expr $cur_already*4] \
354                                   [expr $cur_already*4+2]]
355     }
356     helptext {
357         {{<- ->}   {move cursor, selecting glyph to correct}}
358         {Del       {clear this glyph from the recognition database}}
359         {Tab       {switch to selecting area to define as new glyph}}
360         {Q         {quit and abandon OCR run}}
361     }
362 }
363
364 proc bind_key {k proc} {
365     global keybindings
366     bind . <Key-$k> $proc
367     set keybindings($k) [expr {!![string length $proc]}]
368 }
369 proc unbind_all_keys {} {
370     global keybindings
371     foreach k [array names keybindings] { bind_key $k {} }
372 }
373
374 proc bind_leftright_q {var min max} {
375     bind_key Left  [list leftright $var $min $max -1]
376     bind_key Right [list leftright $var $min $max +1]
377     bind_key q     {
378         puts stderr "\nCharacter resolver quitting as you requested."
379         exit 1
380     }
381 }
382 proc leftright {var min max inc} {
383     upvar #0 $var v
384     set vnew $v
385     incr vnew $inc
386     if {$vnew < $min || $vnew > $max} return
387     set v $vnew
388     recursor
389 }
390
391 proc recursor {} {
392     global csrh cur_mode cur_0 cur_1 mul
393     foreach z1 {0 1} {
394         place .d.mi.csr_$z1 -y 0 -x [expr {[set cur_$z1] * $mul}]
395     }
396     recursor/$cur_mode
397 }
398
399
400 #---------- database read and write ----------
401
402 # database format:
403 # series of glyphs:
404 #   <context> <ncharacters> <hex>...
405 #   width
406 #   <hex-bitmap>
407
408 # $database($context 0x<bits> 0x<bits>...) = $hex
409
410 set database_header {# ypp-sc-tools pctb font v1}
411
412 proc db_getsl {f} {
413     if {[gets $f l] < 0} { error "unexpected db eof" }
414     return $l
415 }
416     
417 proc read_database {} {
418     global database database_header rows database_fn
419     catch { unset database }
420     set database_fn ./charset-$rows.txt
421     if {![file exists $database_fn]} return
422     set f [open $database_fn r]
423     if {[string compare [db_getsl $f] $database_header]} { error "$l ?" }
424     if {([db_getsl $f])+0 != $rows} { error "wrong h ?" }
425     while 1 {
426         set context [db_getsl $f]
427         if {![string length $context]} continue
428         if {[regexp {^\#} $context]} continue
429         if {![string compare . $context]} break
430
431         set bm $context
432         set strq [db_getsl $f]
433         while 1 {
434             set l [db_getsl $f]
435             if {![string length $l]} break
436             lappend bm [format %x 0x$l]
437         }
438         set database($bm) $strq
439     }
440     close $f
441 }
442
443 proc write_database {} {
444     global database rows database_fn database_header
445     set ol {}
446     foreach bm [array names database] {
447         set strq $database($bm)
448         set o "[lindex $bm 0]\n$strq\n"
449         foreach x [lrange $bm 1 end] { append o "$x\n" }
450         
451         lappend ol $o
452     }
453     set f [open $database_fn.new w]
454     puts $f "$database_header\n$rows\n"
455     foreach o [lsort $ol] {
456         puts $f $o
457     }
458     puts $f "."
459     close $f
460     file rename -force $database_fn.new $database_fn
461 }
462
463 proc dbkey {ctx l r} {
464     global wordmap
465     set bm $ctx
466     for {set x $l} {$x <= $r} {incr x} {
467         lappend bm [format %x $wordmap($x)]
468     }
469     return $bm
470 }
471
472 proc update_database/DEFINE {c0 c1 strq} {
473     global glyphsdone unk_l unk_contexts wordmap database
474     if {$c0 > $c1} { manyset [list $c0 $c1] c1 c0 }
475     if {$c0 == $unk_l} {
476         set ncontexts $unk_contexts
477     } else {
478         foreach {l r contexts got} $glyphsdone {
479             if {$l==$c0} { set ncontexts $contexts; break }
480         }
481         if {![info exists ncontexts]} {
482             puts stderr "must start at letter LHS!"
483             return
484         }
485     }
486     incr c1 -1
487     foreach c $ncontexts {
488         set bm [dbkey $c $c0 $c1]
489         set database($bm) $strq
490     }
491     write_database
492 }
493
494 proc update_database/DELETE {l r ctxs} {
495     global database
496     foreach ctx $ctxs {
497         set bm [dbkey $ctx $l $r]
498         catch { unset database($bm) }
499     }
500     write_database
501 }
502     
503
504 proc RETURN_RESULT {how what} {
505     global mainkind
506     place forget .d.csr.csr
507     pack forget .d.csr.csr.e
508     helptext {{{ Processing }}}
509     unbind_all_keys
510     update idletasks
511     debug "$how $what"
512     eval update_database/$how $what
513     done/$mainkind
514 }
515
516 #---------- main progrm ----------
517
518 proc main/default {} {
519     puts stderr "Do not run this program directly."
520     exit 12
521     
522     global glyphsdone unk_l unk_r unk_contexts
523
524     set glyphsdone {
525         7 11 1 M
526         13 17 0 a
527         19 23 0 n
528     }
529     set unk_l 25
530     set unk_r 29
531     set unk_contexts Test
532
533     set f [open text.xpm]
534     read_xpm $f
535     close $f
536
537     read_database
538     resize_widgets
539     draw_glyphsdone
540     startup_cursor
541 }
542 proc done/default {} {
543 }
544
545 proc required {} {
546     global glyphsdone unk_l unk_r unk_contexts
547     
548     if {[gets stdin l]<0} {
549         if {[eof stdin]} { fconfigure stdin -blocking yes; exit 0 }
550         return
551     }
552     init_widgets
553     manyset [lrange $l 0 3] unk_l unk_r unk_contexts
554     set glyphsdone [lrange $l 3 end]
555     debug "GOT $l"
556
557     fileevent stdin readable {}
558     fconfigure stdin -blocking yes
559
560     read_xpm stdin
561     resize_widgets
562     read_database
563     draw_glyphsdone
564     startup_cursor
565 }
566
567 proc main/automatic {} {
568     fconfigure stdin -blocking no
569     fileevent stdin readable required
570 }
571 proc done/automatic {} {
572     exec sh -c {printf \\0 >&4}
573     main/automatic
574 }
575
576 proc debug {m} { }
577
578 set mainkind default
579 foreach arg $argv {
580     switch -exact -- $arg {
581         {--debug}        { proc debug {m} { puts stderr "SHOW-THING $m" } }
582         {--noop-arg}     { }
583         {--automatic-1}  { set mainkind automatic }
584         {--automatic*}   { error "incompatible versions - install problem" }
585         default          { error "huh $argv ?" }
586     }
587 }
588
589 main/$mainkind