From cd124e1675276f26b594f23367da63637673698f Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 6 Jun 2009 15:48:29 +0100 Subject: [PATCH] Check we're on the right page --- pctb/convert.c | 6 ++++-- pctb/ocr.h | 2 ++ pctb/pages.c | 43 +++++++++++++++++++++++++++++-------------- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/pctb/convert.c b/pctb/convert.c index 4784c65..4fb18f4 100644 --- a/pctb/convert.c +++ b/pctb/convert.c @@ -109,7 +109,9 @@ static void debug_rect(const char *what, int whati, Rect rr) { eassert((point).coord != (last)+(increm)); \ } while(0) -static void find_structure(void) { +void find_structure(CanonImage *im) { + cim= im; + Rect whole = { {0,0}, {cim->w-1,cim->h-1} }; WALK_UNTIL_MUST(mainr.tl, x,-1, whole.tl.x, '*'); @@ -283,7 +285,7 @@ int main_test(void) { int tryrect, colno; load_image_and_canonify(); - find_structure(); + find_structure(cim); rd= ocr_init(text_h); for (tryrect= +cim->h; tryrect >= -cim->h; tryrect--) { diff --git a/pctb/ocr.h b/pctb/ocr.h index 7921104..8e1263d 100644 --- a/pctb/ocr.h +++ b/pctb/ocr.h @@ -54,6 +54,8 @@ OcrResultGlyph *ocr(OcrReader *rd, OcrCellType, int w, Pixcol cols[]); void debug_flush(void); +void find_structure(CanonImage *im); + #define eassert assert #define debug stdout diff --git a/pctb/pages.c b/pctb/pages.c index 5978f74..9b05fcd 100644 --- a/pctb/pages.c +++ b/pctb/pages.c @@ -11,10 +11,12 @@ CanonImage *page_images[MAX_PAGES]; int npages; +static XWindowAttributes attr; static Window id; static Display *disp; static struct timeval tv_startup; static unsigned wwidth, wheight; +static int wxpos, wypos; static KeyCode keycode(KeySym sym) { return XKeysymToKeycode(disp,sym); @@ -184,14 +186,13 @@ static void wait_for_stability(Snapshot **output, fprintf(stderr,"PAGING wait_for_stability done.\n"); } -static void raise_and_set_focus(void) { +static void raise_and_get_details(void) { int r; - XWindowAttributes attr; - int xpos,ypos, evbase,errbase,majver,minver; + int evbase,errbase,majver,minver; unsigned bd,depth; Window dummy; - fprintf(stderr,"PAGING raise_and_set_focus\n"); + fprintf(stderr,"PAGING raise_and_get_details\n"); r= XTestQueryExtension(disp, &evbase,&errbase,&majver,&minver); eassert(r==True); @@ -200,16 +201,21 @@ static void raise_and_set_focus(void) { r= XGetWindowAttributes(disp, id, &attr); eassert(r); r= XGetGeometry(disp,id, &attr.root, - &xpos,&ypos, &wwidth,&wheight, + &wxpos,&wypos, &wwidth,&wheight, &bd,&depth); eassert(r); - r= XTranslateCoordinates(disp, id,attr.root, 160,160, &xpos,&ypos, + r= XTranslateCoordinates(disp, id,attr.root, 160,160, &wxpos,&wypos, &dummy); eassert(r); +} +static void set_focus(void) { int screen= XScreenNumberOfScreen(attr.screen); - XTestFakeMotionEvent(disp,screen, xpos, ypos, 0); + + fprintf(stderr,"PAGING set_focus\n"); + + XTestFakeMotionEvent(disp,screen, wxpos,wypos, 0); XTestFakeButtonEvent(disp,1,1, 50); XTestFakeButtonEvent(disp,1,0, 50); @@ -246,8 +252,7 @@ static void compute_shift_mask(ShMask *sm, int targshift, assert(sm->rshift < LONG_BIT); } -static void store_page(int pageno, Snapshot *sn) { - eassert(pageno < MAX_PAGES); +static CanonImage *convert_page(Snapshot *sn) { ShMask shiftmasks[3]; CanonImage *im; @@ -265,23 +270,33 @@ static void store_page(int pageno, Snapshot *sn) { rgb |= ((xrgb << shiftmasks[i].lshift) >> shiftmasks[i].rshift) & shiftmasks[i].mask; }); - - page_images[pageno]= im; + + return im; } static void read_pages(void) { Snapshot *current=0, *last=0; + CanonImage *test; - raise_and_set_focus(); + /* find the window and check it's on the right kind of screen */ + raise_and_get_details(); + snapshot(¤t); + test= convert_page(current); + find_structure(test); + free(test); /* page to the top - keep pressing page up until the image stops changing */ + set_focus(); wait_for_stability(¤t,0, send_pgup_many); /* now to actually page down */ for (;;) { - fprintf(stderr,"PAGING page %d\n",npages); - store_page(npages, current); + fprintf(stderr,"paging page %d\n",npages); + + eassert(npages < MAX_PAGES); + page_images[npages]= convert_page(current); free_snapshot(&last); last=current; current=0; + fprintf(stderr,"PAGING page %d converted\n",npages); wait_for_stability(¤t,last, 0); -- 2.30.2