chiark / gitweb /
do not try to rsync new pixmap if dict-local-only
[ypp-sc-tools.web-live.git] / pctb / rgbimage.c
index 634ead1362a2bacb0870f891e769c623d1327e12..0e89892306605d0b9f5e14773125197f7445e234 100644 (file)
 
 #include "convert.h"
 
-static int identify(const RgbImage *base, Rect portion,
-                   char result[MAXIMGIDENT], const char *what) {
-  sysassert( dbfile_open("pixmaps.txt") );
+static int identify1(const RgbImage *base, Rect portion,
+                   char result[MAXIMGIDENT], const char *what,
+                   const char *which) {
+  char *dbfile_name= masprintf("#%s-pixmap#.txt",which);
+  if (!dbfile_open(dbfile_name))
+    goto not_found;
 
 #define FGETSLINE (dbfile_getsline(result,MAXIMGIDENT,__FILE__,__LINE__))
 
@@ -86,7 +89,9 @@ static int identify(const RgbImage *base, Rect portion,
          int c;
          dbassert( dbfile_scanf("%d",&c) == 1);
          dbassert(c>=0 && c<=255);
-         diff |= (c != RI_PIXEL(base, portion.tl.x + x, portion.tl.y + y)[i]);
+         int px= portion.tl.x + x, py= portion.tl.y + y;
+         diff |= px > portion.br.x || py > portion.br.y ||
+                 (c != RI_PIXEL(base,px,py)[i]);
        }
       }
     }
@@ -95,31 +100,75 @@ static int identify(const RgbImage *base, Rect portion,
       goto found;
     }
   }
+ not_found:
   result[0]= 0;
 
-found:
+ found:
   dbfile_close();
+  free(dbfile_name);
   return !!result[0];
 }
+
+static int identify(const RgbImage *base, Rect portion,
+                   char result[MAXIMGIDENT], const char *what) {
+  return identify1(base,portion,result,what, "master") ||
+         identify1(base,portion,result,what, "local");
+}
+
+void fwrite_ppmraw(FILE *f, const RgbImage *ri) {
+  fprintf(f,
+         "P6\n"
+         "%d %d\n"
+         "255\n", ri->w, ri->h);
+  int count= ri->w * ri->h * 3;
+  sysassert( fwrite(ri->data, 1, count, f) == count );
+  sysassert(!ferror(f));
+  sysassert(!fflush(f));
+}
+
+static void fwrite_ppm(FILE *f, const RgbImage *base, Rect portion) {
+  int x,y,i;
+  fprintf(f,"P3\n%d %d\n255\n", RECT_W(portion), RECT_H(portion));
+  for (y=portion.tl.y; y<=portion.br.y; y++) {
+    for (x=portion.tl.x; x<=portion.br.x; x++) {
+      putc(' ',f);
+      for (i=0; i<3; i++)
+       fprintf(f," %3d", RI_PIXEL(base,x,y)[i]);
+    }
+    putc('\n',f);
+  }
+  sysassert(!ferror(f));
+  sysassert(!fflush(f));
+}
+
 void identify_rgbimage(const RgbImage *base, Rect portion,
                       char result[MAXIMGIDENT], const char *what) {
-  int ok= identify(base, portion, result, what);
-  if (ok) return;
+  static int synced;
 
-  if (DEBUGP(pixmap)) {
-    int x,y,i;
-    fprintf(stderr,"P3\n%d %d\n255\n", RECT_W(portion), RECT_H(portion));
-    for (y=portion.tl.y; y<=portion.br.y; y++) {
-      for (x=portion.tl.x; x<=portion.br.x; x++) {
-       putc(' ',stderr);
-       for (i=0; i<3; i++)
-         fprintf(stderr," %3d", RI_PIXEL(base,x,y)[i]);
-      }
-      putc('\n',stderr);
-    }
+  if (!synced) {
+    if (o_flags & ff_dict_fetch)
+      fetch_with_rsync("pixmap");
+    synced++;
+  }
+  
+  for (;;) {
+    int ok= identify(base, portion, result, what);
+    if (ok) return;
+    
+    if (DEBUGP(pixmap))
+      fwrite_ppm(stderr,base,portion);
+
+    FILE *resolver= resolve_start();
+    if (!resolver)
+       fatal("Image recognition failed - unrecognised island.\n"
+             "See FIXME.FIXME\n");
+
+    fprintf(resolver, "pixmap\n" "%s\n", what);
+    fwrite_ppm(resolver, base, portion);
+    putc('\n',resolver);
+
+    resolve_finish();
   }
-  fatal("Unrecognised pixmap for %s.", what);
 }
 
 RgbImage *alloc_rgb_image(int w, int h) {