Init from working directory of svn repository.
#include <tcl.h>
#include <tk.h>
#include <stdlib.h>
#include <string.h>
#include "link.h"
#include "tcl_interface.h"
#include "stdio.h"
#include "Page.h"
extern Page* global_page;
extern Page* active_page;
extern Page* zoned_page;
extern Tcl_Interp* TCL_ip;
extern Tk_Window main_window;
extern double SCALE_FACTOR;
extern int DISPLAY_SPELLING_MISTAKES;
extern TclMode mode;
static int page_currently_open = 0;
Component * curCompSelected;
Component * prevCompSelected;
void scale(int& coordinate)
{
coordinate = (int)(coordinate * SCALE_FACTOR);
}
void scale(int& coordinate, double scaleFactor)
{
coordinate = (int)(coordinate * scaleFactor);
}
int error(char* s)
{
/* would like to make this take var num args */
printf("Error: %s", s);
}
int quit_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
printf("Thank you for using OCRchie.\n");
exit(0);
}
int get_linenum_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
// argv[1] - x coord argv[2] - y coord
global_page->get_linenum(atoi (argv[1]), atoi (argv[2]));
return TCL_OK;
}
int add_equation_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
// argv[1] starting line of equation, [2] startcol [3] endline [4] endcol
global_page->addEquation(atoi (argv[1]), atoi (argv[2]), atoi(argv[3]),
atoi(argv[4]));
return TCL_OK;
}
int delete_equation_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
global_page->deleteEquation(atoi(argv[1]), atoi(argv[2]));
return TCL_OK;
}
int auto_zone_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
((ZonedPage *) global_page)->autoZone(atoi(argv[1]), atoi(argv[2]));
return TCL_OK;
}
int add_zone_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
// first we need to unscale from scaled immage
int ulx = (int)(atof(argv[1])/ZONING_SCALE_FACTOR);
int uly = (int)(atof(argv[2])/ZONING_SCALE_FACTOR);
int lrx = (int)(atof(argv[3])/ZONING_SCALE_FACTOR);
int lry = (int)(atof(argv[4])/ZONING_SCALE_FACTOR);
Point ul = Point(ulx,uly);
Point lr = Point(lrx, lry);
if ( ul != lr)
{
Zone * newzone = new Zone(ul,lr);
newzone->buildPage((ZonedPage *) global_page);
((ZonedPage *)global_page)->zones()->Append(newzone);
}
return TCL_OK;
}
int remove_zone_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
int x = (int)(atof(argv[1])/ZONING_SCALE_FACTOR);
int y = (int)(atof(argv[2])/ZONING_SCALE_FACTOR);
Zones * z = ((ZonedPage *) global_page)->zones();
z->removeElement(z->findZone(x,y));
return TCL_OK;
}
int select_comp_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
prevCompSelected = curCompSelected;
//argv[1] is x coord argv[2] is y
curCompSelected = global_page->compAt(Point(atoi(argv[1]), atoi(argv[2])));
if (curCompSelected == NULL)
{
docommand("set validComponent 0");
}
else
{
docommand("set validComponent 1");
docommand("set curCompId %s",curCompSelected->fasciiId);
docommand("puts \"curCompId: $curCompId\"");
}
return TCL_OK;
}
int join_comp_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
if (prevCompSelected != NULL && curCompSelected != NULL &&
prevCompSelected != curCompSelected &&
global_page->get_linenum(prevCompSelected) ==
global_page->get_linenum(curCompSelected))
{
global_page->join(prevCompSelected,curCompSelected);
printf("Joined. cur %u prev %u \n", (long) curCompSelected,
(long) prevCompSelected);
prevCompSelected = NULL;
curCompSelected = NULL;
}
else
printf("not Joined. cur %u prev %u\n", (long) curCompSelected,
(long) prevCompSelected);
{
prevCompSelected = NULL;
curCompSelected = NULL;
}
return TCL_OK;
}
int learn_comp_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
learn(curCompSelected, argv[1], 256);
return TCL_OK;
}
int split_comp_horiz_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
global_page->thinnestHorizontalSplit(curCompSelected);
return TCL_OK;
}
int get_skew_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
get_skew(global_page->rmap());
return TCL_OK;
}
int deskew_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
if(DESKEW_METHOD != -1)
{
if(global_page->deskew(DESKEW_METHOD))
global_page->rmap()->display_intervals("black");
return TCL_OK;
}
else
return TCL_OK;
}
int display_intervals_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
if (ac == 1)
global_page->rmap()->display_intervals("black");
else
global_page->rmap()->display_intervals(argv[1], atof(argv[2]), "black");
return TCL_OK;
}
int page_open_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
mode = REGULAR;
if(ac != 2)
return TCL_ERROR;
printf("Opening %s\n", argv[1]);
if(page_currently_open == 1)
{
/* should print some message about closing the current one first */
return TCL_OK;
}
global_page = new ZonedPage;
if(global_page->readMap(argv[1]) != VALID)
interp->result = "0";
else
{
interp->result = "1";
}
active_page = global_page;
page_currently_open = 1;
return TCL_OK;
}
int zoned_page_open_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
mode = ZONING;
if(ac != 2)
return TCL_ERROR;
printf("Opening %s\n", argv[1]);
if(page_currently_open == 1)
{
/* should print some message about closing the current one first */
return TCL_OK;
}
global_page = new ZonedPage;
if(global_page->readMap(argv[1]) != VALID)
interp->result = "0";
else
{
interp->result = "1";
}
zoned_page = global_page;
page_currently_open = 1;
return TCL_OK;
}
int extract_comp_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
global_page->extractComponents(MinHorizSeparation);
return TCL_OK;
}
int get_page_height_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
sprintf(interp->result, "%d", global_page->get_height());
/* printf("Interpereter height = %s\n", interp->result); */
return TCL_OK;
}
int get_page_width_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
sprintf(interp->result, "%d", global_page->get_width());
/* printf("Interpereter width = %s\n", interp->result); */
return TCL_OK;
}
int zoom_in_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
if(ac == 1)
{
SCALE_FACTOR = SCALE_FACTOR * 2;
global_page->rmap()->display_intervals("black");
}
else
{
//argv[1] window argv[2] scalefactor
global_page->rmap()->display_intervals(argv[1], atof(argv[2]),"black");
}
return TCL_OK;
}
int zoom_out_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
if (ac == 1)
{
SCALE_FACTOR = SCALE_FACTOR * 0.5;
global_page->rmap()->display_intervals("black");
}
else
global_page->rmap()->display_intervals(argv[1], atof(argv[2]),"black");
return TCL_OK;
}
int deallocate_page_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
if(page_currently_open == 0)
return TCL_OK; /* don't do anything if their isn't anything open */
delete global_page;
page_currently_open = 0;
return TCL_OK;
}
int learn_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
// argv1 is tif argv2 is txt and argv3 is a bool for word synchronization
printf("Learning from %s and %s sych %d\n", argv[1], argv[2], atoi(argv[3]));
learn(argv[1], argv[2], atoi(argv[3]));
return TCL_OK;
}
int learn_page_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
// argv1 is txt and argv2 is a bool for word synchronization
printf("Learning from %s and %s sych %d\n", argv[1], argv[2], atoi(argv[3]));
learn(global_page, argv[1], atoi(argv[2]));
return TCL_OK;
}
int learn_data_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
printf("Learning data from %s\n", argv[1]);
readLearnedGroups(argv[1]);
return TCL_OK;
}
int write_word_pos_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
global_page->writeWordPos(argv[1]);
return TCL_OK;
}
int write_wordbox_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
// arguments are output file, x offset, y offset and WbxEquationsOnly variable
global_page->writeWordbox(argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
return TCL_OK;
}
int write_equations_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
// arguments are output file, linenumber offset
global_page->writeEquations(argv[1], atoi(argv[2]));
return TCL_OK;
}
int write_learned_chars_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
writeLearnedGroups(argv[1]);
return TCL_OK;
}
int find_lines_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
deskew_cmd(clientData, interp, ac, argv);
/* printf("Calling setlines\n"); */
global_page->setLines();
return TCL_OK;
}
int recognize_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
/* just in case someone has left something sitting around */
docommand(".main_window.edit_window.text_part delete 1.0 end");
docommand("set COLORED_WORDS {}");
/* printf("Calling extractComponents\n"); */
if(global_page->components() == NULL)
global_page->extractComponents(MinHorizSeparation);
/* printf("Calling recognize\n"); */
global_page->recognize();
/* printf("Calling extractwords\n"); */
global_page->extractWords();
if(SPELLCHECK)
{
/* printf("Spellchecking\n"); */
global_page->spellcheck();
}
/* printf("Calling sendwordstotcl\n"); */
global_page->send_words_to_tcl();
return TCL_OK;
}
int find_lines_and_recognize_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
/* just in case someone has left something sitting around */
docommand(".main_window.edit_window.text_part delete 1.0 end");
docommand("set COLORED_WORDS {}");
/* printf("Calling deskew\n"); */
deskew_cmd(clientData, interp, ac, argv);
/* printf("Calling setlines\n"); */
global_page->setLines();
/* printf("Calling extractComponents\n"); */
global_page->extractComponents(MinHorizSeparation);
/* printf("Calling recognize\n"); */
global_page->recognize();
/* printf("Calling extractwords\n"); */
global_page->extractWords();
if(SPELLCHECK)
{
/* printf("Spellchecking\n"); */
global_page->spellcheck();
}
/* printf("Calling sendwordstotcl\n"); */
global_page->send_words_to_tcl();
return TCL_OK;
}
int switch_to_active_page_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
if (active_page != NULL)
global_page = active_page;
return TCL_OK;
}
int switch_to_zoned_page_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
if (zoned_page != NULL)
global_page = zoned_page;
return TCL_OK;
}
int set_active_page_cmd(ClientData clientData, Tcl_Interp *interp, int ac, char** argv)
{
int x = atoi(argv[1]);
int y = atoi(argv[2]);
active_page = ((ZonedPage *) zoned_page)->activate(x,y);
if (active_page != NULL)
{
docommand(".main_window.edit_window.text_part delete 1.0 end");
docommand("set COLORED_WORDS {}");
docommand("focus .main_window.display");
docommand("grab current .main_window.display");
global_page = active_page;
docommand("DISPLAY_INTERVALS .main_window.display.work_space $SCALE_FACTOR");
docommand("FIND_LINES");
docommand("grab release .main_window.display");
docommand("zone_message \"Active Zone at (%d,%d)\"", x, y);
}
else
{
docommand("zone_message \"No zone found here\"");
}
return TCL_OK;
}
static int
vdocommand1(char* s)
{
/* final function called to do a tcl docommand */
int code;
code = Tcl_Eval(TCL_ip, s);
if (code == TCL_ERROR)
error(TCL_ip->result);
return code;
}
void update()
{
Tk_DoOneEvent(TK_ALL_EVENTS);
}
static int
vdocommand(int record, char* fmt, va_list args)
{
/* helper for docommand */
char buf[4097];
int code;
vsprintf(buf, fmt, args);
if (strchr(buf, '\?'))
error("Huh?");
code = vdocommand1(buf);
return code;
}
int
docommand(char* fmt, ...)
{
/* do a tcl command, var number of args */
va_list args;
va_start(args, fmt);
vdocommand(0, fmt, args);
va_end(args);
}
static int
vset_status1(char* s)
{
/* final function called to do a tcl docommand */
int code;
code = Tcl_Eval(TCL_ip, s);
if (code == TCL_ERROR)
error(TCL_ip->result);
return code;
}
static int
vset_status(int record, char* fmt, va_list args)
{
/* helper for docommand */
char buf[4097];
char newbuf[4097];
int code;
vsprintf(buf, fmt, args);
sprintf(newbuf, ".main_window.button_bar.msg configure -text \"%s\"", buf);
if (strchr(buf, '\?'))
error("Huh?");
code = vset_status1(newbuf);
return code;
}
int set_status(char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vset_status(0, fmt, args);
va_end(args);
docommand("update");
}
int mispelled(char* word)
{
int result = docommand("spellcheck %s", word);
/* printf("call to spellcheck %s returned %s\n", word, TCL_ip->result); */
if(!(strcmp("MISPELLED", TCL_ip->result)))
return 1;
else
return 0;
}
int initialize_interpreter()
{
TCL_ip = Tcl_CreateInterp();
Tcl_Init(TCL_ip);
}
int load_user_interface()
{
main_window = Tk_CreateMainWindow(TCL_ip, NULL, "OCRchie", "OCRchie");
Tk_Init(TCL_ip);
Tk_MapWindow(main_window);
docommand("source new_ui.tcl");
}
int initialize_command_procs()
{
Tcl_CreateCommand(TCL_ip, "page_open", page_open_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "zoned_page_open", zoned_page_open_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "EXTRACT_COMP", extract_comp_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "get_page_height", get_page_height_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "get_page_width", get_page_width_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "LEARN", learn_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "LEARN_PAGE", learn_page_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "LEARN_DATA", learn_data_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "WRITE_WORD_POS", write_word_pos_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "WRITE_WORDBOX", write_wordbox_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "WRITE_EQUATIONS", write_equations_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "WRITE_LEARNED_CHARS", write_learned_chars_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "FIND_LINES_AND_RECOGNIZE", find_lines_and_recognize_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "FIND_LINES", find_lines_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "RECOGNIZE", recognize_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "SELECT_COMP", select_comp_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "JOIN_COMP", join_comp_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "LEARN_COMP", learn_comp_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "SPLIT_COMP_HORIZ", split_comp_horiz_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "GET_SKEW", get_skew_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "DESKEW", deskew_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "DISPLAY_INTERVALS", display_intervals_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "QUIT", quit_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "ZOOM_IN", zoom_in_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "ZOOM_OUT", zoom_out_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "DEALLOCATE_PAGE", deallocate_page_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "GET_LINENUM",get_linenum_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "ADD_EQUATION",add_equation_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "DELETE_EQUATION",delete_equation_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "AUTO_ZONE",auto_zone_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "ADD_ZONE",add_zone_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "REMOVE_ZONE",remove_zone_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "SWITCH_TO_ACTIVE_PAGE",switch_to_active_page_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "SWITCH_TO_ZONED_PAGE",switch_to_zoned_page_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
Tcl_CreateCommand(TCL_ip, "SET_ACTIVE_PAGE",set_active_page_cmd, (ClientData) NULL, (Tcl_CmdDeleteProc*) NULL);
printf("Done initializing new tcl commands\n");
}
int initialize_link_vars()
{
init_link_vars(); /* what a nice name */
docommand("source link_vars.tcl");
printf("Done initializing link variables\n");
}