reference/ocr-simple/tcl_interface.cc
changeset 0 6b8091ca909a
equal deleted inserted replaced
-1:000000000000 0:6b8091ca909a
       
     1 #include <tcl.h>
       
     2 #include <tk.h>
       
     3 #include <string.h>
       
     4 #include "link.h"
       
     5 #include "tcl_interface.h"
       
     6 #include "stdio.h"
       
     7 #include "Page.h"
       
     8 
       
     9 extern Page* global_page;
       
    10 extern Tcl_Interp* TCL_ip;
       
    11 extern Tk_Window main_window;
       
    12 extern double SCALE_FACTOR;
       
    13 extern int DISPLAY_SPELLING_MISTAKES;
       
    14 
       
    15 static int page_currently_open = 0;
       
    16 
       
    17 void scale(int& coordinate)
       
    18 {
       
    19   coordinate = (int)(coordinate * SCALE_FACTOR);
       
    20 }
       
    21 
       
    22 int error(char* s)
       
    23 {
       
    24 /* would like to make this take var num args */
       
    25   printf("Error: %s", s);
       
    26 }
       
    27 
       
    28 int quit_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    29 {
       
    30   printf("Thank you for using OCRchie.\n");
       
    31   exit(0);
       
    32 }
       
    33 
       
    34 int get_skew_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    35 {
       
    36   get_skew(global_page->rmap());
       
    37   return TCL_OK;
       
    38 }
       
    39 
       
    40 int deskew_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    41 {
       
    42   if(DESKEW_METHOD != -1)
       
    43     {
       
    44       if(global_page->deskew(DESKEW_METHOD))
       
    45 	global_page->rmap()->display_intervals("black");
       
    46       return TCL_OK;
       
    47     }
       
    48   else
       
    49     return TCL_OK;
       
    50 }
       
    51 
       
    52 int display_intervals_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    53 {
       
    54   global_page->rmap()->display_intervals("black");
       
    55   return TCL_OK;
       
    56 }
       
    57 
       
    58 int page_open_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    59 {
       
    60   if(ac != 2)
       
    61     return TCL_ERROR;
       
    62   printf("Opening %s\n", argv[1]);
       
    63   if(page_currently_open == 1)
       
    64     {
       
    65       /* should print some message about closing the current one first */
       
    66       return TCL_OK;
       
    67     }
       
    68   global_page = new Page;
       
    69   if(global_page->readMap(argv[1]) != VALID)
       
    70     interp->result = "0";
       
    71   else
       
    72     interp->result = "1";
       
    73   page_currently_open = 1;
       
    74   return TCL_OK;
       
    75 }
       
    76 
       
    77 int get_page_height_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    78 {
       
    79   sprintf(interp->result, "%d", global_page->get_height());
       
    80   /*  printf("Interpereter height = %s\n", interp->result); */
       
    81   return TCL_OK;
       
    82 }
       
    83 
       
    84 int get_page_width_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    85 {
       
    86   sprintf(interp->result, "%d",  global_page->get_width());
       
    87   /* printf("Interpereter width = %s\n", interp->result); */
       
    88   return TCL_OK;
       
    89 }
       
    90 
       
    91 int zoom_in_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    92 {
       
    93   SCALE_FACTOR = SCALE_FACTOR * 2;
       
    94   global_page->rmap()->display_intervals("black");
       
    95   return TCL_OK;
       
    96 }
       
    97 
       
    98 int zoom_out_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
    99 {
       
   100   SCALE_FACTOR = SCALE_FACTOR * 0.5;
       
   101   global_page->rmap()->display_intervals("black");
       
   102   return TCL_OK;
       
   103 }
       
   104 
       
   105 int deallocate_page_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
   106 {
       
   107   if(page_currently_open == 0)
       
   108     return TCL_OK;  /* don't do anything if their isn't anything open */
       
   109   delete global_page;
       
   110   page_currently_open = 0;
       
   111   return TCL_OK;
       
   112 }
       
   113 
       
   114 int learn_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
   115 {
       
   116   printf("Learning from %s and %s\n", argv[1], argv[2]);
       
   117   learn(argv[1], argv[2]);
       
   118   return TCL_OK;
       
   119 }
       
   120 
       
   121 int learn_data_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
   122 {
       
   123   printf("Learning data from %s\n", argv[1]);
       
   124   readLearnedGroups(argv[1]);
       
   125   return TCL_OK;
       
   126 }
       
   127 
       
   128 int write_word_pos_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
   129 {
       
   130   global_page->writeWordPos(argv[1]);
       
   131   return TCL_OK;
       
   132 }
       
   133 
       
   134 int write_learned_chars_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
   135 {
       
   136   writeLearnedGroups(argv[1]);
       
   137   return TCL_OK;
       
   138 }
       
   139   
       
   140 int recognize_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
       
   141 
       
   142 {
       
   143   /* just in case someone has left something sitting around */
       
   144   docommand(".main_window.edit_window.text_part delete 1.0 end");
       
   145   docommand("set COLORED_WORDS {}");
       
   146   /* printf("Calling deskew\n"); */
       
   147   deskew_cmd(clientData, interp, ac, argv);
       
   148   /*  printf("Calling setlines\n"); */
       
   149   global_page->setLines();
       
   150   /* printf("Calling extractComponents\n"); */
       
   151   global_page->extractComponents();
       
   152   /* printf("Calling recognize\n"); */
       
   153   global_page->recognize();
       
   154   /* printf("Calling extractwords\n"); */
       
   155   global_page->extractWords();
       
   156   if(SPELLCHECK)
       
   157     {
       
   158       /* printf("Spellchecking\n"); */
       
   159       global_page->spellcheck();
       
   160     }	     
       
   161   /* printf("Calling sendwordstotcl\n"); */
       
   162   global_page->send_words_to_tcl();
       
   163   return TCL_OK;
       
   164 }
       
   165 
       
   166 static int
       
   167 vdocommand1(char* s)
       
   168 {
       
   169 /* final function called to do a tcl docommand */
       
   170   int code;
       
   171 
       
   172   code = Tcl_Eval(TCL_ip, s);      
       
   173   if (code == TCL_ERROR)
       
   174     error(TCL_ip->result);
       
   175   return code;
       
   176 }
       
   177 
       
   178 void update()
       
   179 {
       
   180   Tcl_DoOneEvent(TCL_DONT_WAIT);   
       
   181 }
       
   182 
       
   183 static int
       
   184 vdocommand(int record, char* fmt, va_list args)
       
   185 {
       
   186 /* helper for docommand */
       
   187   char buf[4097];
       
   188   int code;
       
   189 
       
   190   vsprintf(buf, fmt, args);
       
   191   if (strchr(buf, '\?'))
       
   192     error("Huh?");
       
   193   code = vdocommand1(buf);
       
   194   return code;
       
   195 }
       
   196 
       
   197 
       
   198 int
       
   199 docommand(char* fmt, ...)
       
   200 {
       
   201 /* do a tcl command, var number of args */
       
   202   va_list args;
       
   203 
       
   204   va_start(args, fmt);
       
   205   vdocommand(0, fmt, args);
       
   206   va_end(args);
       
   207 }
       
   208 
       
   209 static int
       
   210 vset_status1(char* s)
       
   211 {
       
   212 /* final function called to do a tcl docommand */
       
   213   int code;
       
   214 
       
   215 	code = Tcl_Eval(TCL_ip, s);      
       
   216 	if (code == TCL_ERROR)
       
   217 	  error(TCL_ip->result);
       
   218 	return code;
       
   219 }
       
   220 
       
   221 static int
       
   222 vset_status(int record, char* fmt, va_list args)
       
   223 {
       
   224 /* helper for docommand */
       
   225   char buf[4097];
       
   226   char newbuf[4097];
       
   227   int code;
       
   228 
       
   229   vsprintf(buf, fmt, args);
       
   230   sprintf(newbuf, ".main_window.button_bar.msg configure -text \"%s\"", buf); 
       
   231   if (strchr(buf, '\?'))
       
   232     error("Huh?");
       
   233   code = vset_status1(newbuf);
       
   234   return code;
       
   235 }
       
   236 
       
   237 int set_status(char* fmt, ...)
       
   238 {
       
   239   va_list args;
       
   240 
       
   241   va_start(args, fmt);
       
   242   vset_status(0, fmt, args);
       
   243   va_end(args);
       
   244   docommand("update");
       
   245 }
       
   246      
       
   247 int mispelled(char* word)
       
   248 {
       
   249   int result = docommand("spellcheck %s", word);
       
   250   /*  printf("call to spellcheck %s returned %s\n", word, TCL_ip->result); */
       
   251   if(!(strcmp("MISPELLED", TCL_ip->result)))    
       
   252     return 1;
       
   253   else 
       
   254     return 0;
       
   255 }
       
   256 
       
   257 
       
   258 int initialize_interpreter()
       
   259 {
       
   260   TCL_ip = Tcl_CreateInterp();
       
   261   Tcl_Init(TCL_ip);
       
   262 
       
   263 }
       
   264 
       
   265 int load_user_interface()
       
   266 {
       
   267   // main_window = Tk_CreateMainWindow(TCL_ip, NULL, "OCRchie", "OCRchie");
       
   268  
       
   269  
       
   270   Tk_Init(TCL_ip);
       
   271   main_window = Tk_CreateWindow(TCL_ip, NULL, "OCRchie", "OCRchie");
       
   272   //  Tk_MapWindow(main_window);   /6/21/00
       
   273   docommand("source new_ui.tcl");
       
   274 }
       
   275 
       
   276 int initialize_command_procs()
       
   277 {
       
   278   Tcl_CreateCommand(TCL_ip, "page_open", page_open_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   279   Tcl_CreateCommand(TCL_ip, "get_page_height", get_page_height_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   280   Tcl_CreateCommand(TCL_ip, "get_page_width", get_page_width_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   281   Tcl_CreateCommand(TCL_ip, "LEARN", learn_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   282   Tcl_CreateCommand(TCL_ip, "LEARN_DATA", learn_data_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   283   Tcl_CreateCommand(TCL_ip, "WRITE_WORD_POS", write_word_pos_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   284   Tcl_CreateCommand(TCL_ip, "WRITE_LEARNED_CHARS", write_learned_chars_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   285   Tcl_CreateCommand(TCL_ip, "FIND_LINES_AND_RECOGNIZE", recognize_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   286   Tcl_CreateCommand(TCL_ip, "GET_SKEW", get_skew_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   287   Tcl_CreateCommand(TCL_ip, "DESKEW", deskew_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   288   Tcl_CreateCommand(TCL_ip, "DISPLAY_INTERVALS", display_intervals_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   289   Tcl_CreateCommand(TCL_ip, "QUIT", quit_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   290   Tcl_CreateCommand(TCL_ip, "ZOOM_IN", zoom_in_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   291   Tcl_CreateCommand(TCL_ip, "ZOOM_OUT", zoom_out_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   292   Tcl_CreateCommand(TCL_ip, "DEALLOCATE_PAGE", deallocate_page_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
       
   293   printf("Done initializing new tcl commands\n");
       
   294 }
       
   295 
       
   296 int initialize_link_vars()
       
   297 {
       
   298   init_link_vars();  /* what a nice name */
       
   299   docommand("source link_vars.tcl");
       
   300   printf("Done initializing link variables\n");
       
   301 }