chiark / gitweb /
Check we're on the right page
authorIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 6 Jun 2009 14:48:29 +0000 (15:48 +0100)
committerIan Jackson <ian@liberator.relativity.greenend.org.uk>
Sat, 6 Jun 2009 14:48:29 +0000 (15:48 +0100)
pctb/convert.c
pctb/ocr.h
pctb/pages.c

index 4784c653994e8e4bd0c675389075cca391b23cf4..4fb18f4c2973ff8f7ba8281acb9470a6feb1dd40 100644 (file)
@@ -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--) {
index 792110466f4dfe05cde96b469697321ea56b1f1d..8e1263d94495bf365cc97748d2b2e839234c0dc6 100644 (file)
@@ -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
 
index 5978f74e365d5e62866422e0d3a59edb43b48d35..9b05fcd046ae5989f25f1c9695837b66e910e619 100644 (file)
 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(&current);
+  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(&current,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(&current,last, 0);