chiark / gitweb /
WIP island determination; can recognise a pixmap!
[ypp-sc-tools.main.git] / pctb / rgbimage.c
index e49044f419f0f7ef06c530a74f8d46c10a8f0dd0..634ead1362a2bacb0870f891e769c623d1327e12 100644 (file)
 
 #include "convert.h"
 
-#define dbassert(x)                                                    \
-  ((x) ? (void)0 :                                                     \
-     fatal("Error in pixmap database.\n"                               \
-          " Requirement not met: %s:%d: %s", __FILE__,__LINE__, #x))
-
 static int identify(const RgbImage *base, Rect portion,
-                   char result[MAXIMGIDENT]) {
-  FILE *f;
-  sysassert( f= fopen("pixmaps.txt","r") );
-  struct pam inpam;
+                   char result[MAXIMGIDENT], const char *what) {
+  sysassert( dbfile_open("pixmaps.txt") );
 
-#define FGETSLINE (fgetsline(f,result,MAXIMGIDENT))
+#define FGETSLINE (dbfile_getsline(result,MAXIMGIDENT,__FILE__,__LINE__))
 
   FGETSLINE;
-fprintf(stderr,">%s<\n",result);
   dbassert(!strcmp(result,"# ypp-sc-tools pctb pixmaps v1"));
   
   void *row= 0;
@@ -70,42 +62,49 @@ fprintf(stderr,">%s<\n",result);
   
   for (;;) {
     FGETSLINE;
-    if (!result || result[0]=='#') continue;
+    if (!result[0] || result[0]=='#') continue;
     if (!strcmp(result,".")) break;
 
-    pnm_readpaminit(f, &inpam, sizeof(inpam));
-    dbassert(inpam.maxval == 255);
-    dbassert(inpam.bytes_per_sample == 1);
-    dbassert(inpam.format == PPM_FORMAT);
+    char magic[10];
+    dbfile_getsline(magic,sizeof(magic),__FILE__,__LINE__);
+
+    dbassert(!strcmp(magic,"P3"));
+    int w,h,maxval;
+    dbassert( dbfile_scanf("%d %d %d",&w,&h,&maxval) == 3);
+    dbassert(w>0); dbassert(h>0); dbassert(maxval==255);
 
-    if (rowa < inpam.width) {
-      rowa= inpam.width;
+    if (rowa < w) {
+      rowa= w;
       row= mrealloc(row, rowa*3);
     }
-    int diff=
-      inpam.width  != RECT_W(portion) ||
-      inpam.height != RECT_H(portion);
+    int diff= w != RECT_W(portion) || h != RECT_H(portion);
 
-    int y;
-    for (y=0; y<inpam.height; y++) {
-      int r= fread(row,1, 3*inpam.width, f);
-      sysassert(!ferror(f));
-      dbassert(!feof(f));
-      assert(r == 3*inpam.width);
-      if (diff) continue;
-
-      diff= memcmp(RI_PIXEL(base, portion.tl.x, portion.tl.y + y),
-                  row, 3*inpam.width);
+    int x,y,i;
+    for (y=0; y<h; y++) {
+      for (x=0; x<w; x++) {
+       for (i=0; i<3; i++) {
+         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]);
+       }
+      }
+    }
+    if (!diff) {
+      progress_log("Identified %s image: %s.",what,result);
+      goto found;
     }
-    if (!diff)
-      return 1;
   }
-  return 0;
+  result[0]= 0;
+
+found:
+  dbfile_close();
+  return !!result[0];
 }
  
 void identify_rgbimage(const RgbImage *base, Rect portion,
-                      char result[MAXIMGIDENT]) {
-  int ok= identify(base, portion, result);
+                      char result[MAXIMGIDENT], const char *what) {
+  int ok= identify(base, portion, result, what);
   if (ok) return;
 
   if (DEBUGP(pixmap)) {
@@ -120,7 +119,7 @@ void identify_rgbimage(const RgbImage *base, Rect portion,
       putc('\n',stderr);
     }
   }
-  fatal("Unrecognised pixmap.");
+  fatal("Unrecognised pixmap for %s.", what);
 }
 
 RgbImage *alloc_rgb_image(int w, int h) {