chiark / gitweb /
get pirate name right
[ypp-sc-tools.web-live.git] / pctb / structure.c
index fc077c1f31a72cd4d5d9df6734bb9562aad46f77..d768acd406151a7fccf55a510cbd2d1c9ede4af9 100644 (file)
@@ -77,7 +77,7 @@ static void require_rectangle_r(Rect rr, const char *ok) {
 }
 
 static void debug_rect(const char *what, int whati, Rect rr) {
-#ifdef DEBUG_RECTANGLES
+  if (!DEBUGP(rect)) return;
   int y,r,w;
   fprintf(debug, "%s %d: %d,%d..%d,%d:\n", what, whati,
          rr.tl.x,rr.tl.y, rr.br.x,rr.br.y);
@@ -89,7 +89,6 @@ static void debug_rect(const char *what, int whati, Rect rr) {
     fputc('|',debug);
     fputc('\n',debug);
   }
-#endif
   debug_flush();
 }
 
@@ -152,17 +151,17 @@ void find_structure(CanonImage *im) {
   down.y++;
   WALK_UNTIL_MUST(down, y,+1, mainr.br.y, '+');
 
-#ifdef DEBUG_RECTANGLES
-  int xscaleunit, y,x;
-  for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
-    fprintf(debug,"     ");
-    for (x=0; x<=cim->w; x++) {
-      if (x % xscaleunit) fputc(' ',debug);
-      else fprintf(debug,"%d",(x / xscaleunit)%10);
+  if (DEBUGP(rect)) {
+    int xscaleunit, y,x;
+    for (y=0, xscaleunit=1; y<4; y++, xscaleunit*=10) {
+      fprintf(debug,"     ");
+      for (x=0; x<=cim->w; x++) {
+       if (x % xscaleunit) fputc(' ',debug);
+       else fprintf(debug,"%d",(x / xscaleunit)%10);
+      }
+      fputc('\n',debug);
     }
-    fputc('\n',debug);
   }
-#endif
 
   commbasey= up.y;
   comminty= down.y - up.y + 2;
@@ -208,7 +207,7 @@ CanonImage *alloc_canon_image(int w, int h) {
   return im;
 }
 
-CanonImage *file_read_image_ppm(FILE *f) {
+static void file_read_image_ppm(FILE *f) {
   struct pam inpam;
   unsigned char rgb_buf[3];
   CanonImage *im;
@@ -226,17 +225,39 @@ CanonImage *file_read_image_ppm(FILE *f) {
                       (rgb_buf[2]);
   });
 
-  return im;
+  eassert(!ferror(screenshots_file));
+  eassert(!feof(screenshots_file));
+
+  eassert(npages < MAX_PAGES);
+  page_images[npages++]= im;
 }
 
-void read_screenshots(void) {
-  int c;
-  while ((c= fgetc(screenshots_file) != EOF)) {
-    ungetc(c, screenshots_file);
+void read_one_screenshot(void) {
+  progress("reading screenshot...");
+  file_read_image_ppm(screenshots_file);
+  progress_log("read screenshot.");
+}
 
-    eassert(npages < MAX_PAGES);
-    page_images[npages++]= file_read_image_ppm(screenshots_file);
+void read_screenshots(void) {
+  struct stat stab;
+  int r;
+  
+  r= fstat(fileno(screenshots_file), &stab);  eassert(!r);
+  
+  for (;;) {
+    if (S_ISREG(stab.st_mode)) {
+      long pos= ftell(screenshots_file);
+      if (pos == stab.st_size) break;
+    } else {
+      int c= fgetc(screenshots_file);
+      if (c==EOF) break;
+      ungetc(c, screenshots_file);
+    }
+    progress("reading screenshot %d...",npages);
+    file_read_image_ppm(screenshots_file);
   }
+  eassert(!ferror(screenshots_file));
+  progress_log("read %d screenshots.",npages);
 }
 
 static void find_commodity(int offset, Rect *rr) {
@@ -244,13 +265,14 @@ static void find_commodity(int offset, Rect *rr) {
   rr->tl.y= commbasey - offset*comminty;
   rr->br.y= rr->tl.y + comminty-2;
   if (rr->tl.y < mainr.tl.y || rr->br.y > mainr.br.y) { rr->tl.x=-1; return; }
+  
+  rr->tl.x= mainr.tl.x;
+  rr->br.x= mainr.br.x;
+
   if (rr->tl.y > mainr.tl.y)
     require_rectangle(rr->tl.x,rr->tl.y-1, rr->br.x,rr->tl.y-1, "+");
   if (rr->br.y < mainr.tl.y)
     require_rectangle(rr->tl.x,rr->br.y+1, rr->br.x,rr->br.y+1, "+");
-  
-  rr->tl.x= mainr.tl.x;
-  rr->br.x= mainr.br.x;
 }
 
 static void find_table_entry(Rect commod, int colno, Rect *cellr) {