chiark / gitweb /
Much faster
[ypp-sc-tools.db-test.git] / pctb / convert.c
index 3a2b79fed91f6d50ed7141ddf262fd395c0bc440..4784c653994e8e4bd0c675389075cca391b23cf4 100644 (file)
@@ -1,11 +1,6 @@
 
 #include "ocr.h"
 
-typedef struct {
-  unsigned long rgb; /* on screen */
-  char c; /* canonical */
-} CanonColourInfo;
-
 static CanonImage *cim;
 
 void debug_flush(void) {
@@ -38,7 +33,7 @@ static int colrightx[INTERESTING_COLUMNS];
 static int text_h;
 static OcrReader *rd;
 
-static const CanonColourInfo canoncolourinfos[]= {
+const CanonColourInfo canoncolourinfos[]= {
   { 0x475A5E, '*' }, /* edge */
   { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
   { 0x7D9094, '+' }, /* interbox */
@@ -221,44 +216,33 @@ static void find_table_entry(Rect commod, int colno, Rect *cellr) {
   require_rectangle_r(*cellr, " o");
 }
 
+CanonImage *alloc_canon_image(int w, int h) {
+  CanonImage *im= malloc(sizeof(CanonImage) + w*h);
+  eassert(im);
+  im->w= w;
+  im->h= h;
+  memset(im->d,'?',w*h);
+  return im;
+}
+
 CanonImage *file_read_image(FILE *f) {
   struct pam inpam;
-  unsigned char rgb[3];
-  int x,y,r;
-  const CanonColourInfo *cci;
+  unsigned char rgb_buf[3];
+  CanonImage *im;
 
   pnm_readpaminit(f, &inpam, sizeof(inpam));
   eassert(inpam.maxval == 255);
   eassert(inpam.bytes_per_sample == 1);
 
-  CanonImage *im= malloc(sizeof(CanonImage) + inpam.width*inpam.height);
-  eassert(im);
-  im->h= inpam.height;
-  im->w= inpam.width;
-
-  memset(im->d,'?',inpam.width*inpam.height);
-
-  for (y=0; y<inpam.height; y++) {
-    for (x=0; x<inpam.width; x++) {
-      r= fread(&rgb,1,3,f);  eassert(r==3);
-      unsigned long rgb_l=
-       ((unsigned long)rgb[0]<<16) |
-       ((unsigned long)rgb[1]<<8) |
-                      (rgb[2]);
-      for (cci=canoncolourinfos; cci->c; cci++)
-       if (cci->rgb == rgb_l) {
-         im->d[y*inpam.width + x]= cci->c;
-         break;
-       }
-    }
-#ifdef DEBUG_RECTANGLES
-    fprintf(debug, "%4d ",y);
-    r= fwrite(im->d + y*inpam.width, 1,inpam.width, debug);
-    eassert(r==inpam.width);
-    fputc('\n',debug);
-#endif
-  }
-  debug_flush();
+  CANONICALISE_IMAGE(im, inpam.width, inpam.height, {
+    r= fread(&rgb_buf,1,3,f);  eassert(r==3);
+
+    rgb=
+       ((unsigned long)rgb_buf[0]<<16) |
+       ((unsigned long)rgb_buf[1]<<8) |
+                      (rgb_buf[2]);
+  });
+
   return im;
 }