chiark / gitweb /
New colour canonification based on tree
[ypp-sc-tools.main.git] / pctb / structure.c
index cb79f9996606b8209f0913267d0a3aed250a7370..f0de5fa964d05f0c9e135b17fb99965200fff10b 100644 (file)
@@ -51,7 +51,12 @@ char *archipelago, *island;
 #define OTHERCOORD_x y
 #define OTHERCOORD_y x
 
-const CanonColourInfo canoncolourinfos[]= {
+typedef struct {
+  Rgb rgb; /* on screen */
+  char c; /* canonical */
+} CanonColourInfo;
+
+const CanonColourInfo canoncolourinfo_table[]= {
   { 0x475A5E, '*' }, /* edge */
   { 0x2C5F7A, '*' }, /* edge just under box heading shadow */
   { 0xC5C7AE, '*' }, /* blank area of partial commodities list */
@@ -89,6 +94,31 @@ const CanonColourInfo canoncolourinfos[]= {
   { 0,0 }
 };
 
+CanonColourInfoReds canoncolourinfo_tree;
+
+void canon_colour_prepare(void) {
+  const CanonColourInfo *cci;
+  for (cci=canoncolourinfo_table; cci->c; cci++) {
+    unsigned char r= cci->rgb >> 16;
+    unsigned char g= cci->rgb >>  8;
+    unsigned char b= cci->rgb;
+
+    CanonColourInfoGreens *greens= canoncolourinfo_tree.red2[r];
+    if (!greens) {
+      greens= canoncolourinfo_tree.red2[r]= mmalloc(sizeof(*greens));
+      FILLZERO(*greens);
+    }
+
+    CanonColourInfoBlues *blues= greens->green2[g];
+    if (!blues) {
+      blues= greens->green2[g]= mmalloc(sizeof(*blues));
+      memset(blues, '?', sizeof(blues));
+    }
+
+    blues->blue2[b]= cci->c;
+  }
+}
+
 
 static void mustfail1(const char *file, int line, const char *what) {
   fprintf(stderr,
@@ -390,15 +420,13 @@ static void file_read_image_ppm(FILE *f) {
     fatal("PNM screenshot(s) file must be 8bpp 1 byte-per-sample RGB raw");
 
   CANONICALISE_IMAGE(im, inpam.width, inpam.height, {
-    int r= fread(&rgb_buf,1,3,f);
+    int rr= fread(&rgb_buf,1,3,f);
     sysassert(!ferror(f));
-    if (r!=3) fatal("PNM screenshot(s) file ends unexpectedly");
-
-    rgb=
-       ((unsigned long)rgb_buf[0]<<16) |
-       ((unsigned long)rgb_buf[1]<<8) |
-                      (rgb_buf[2]);
+    if (rr!=3) fatal("PNM screenshot(s) file ends unexpectedly");
 
+    r= rgb_buf[0];
+    g= rgb_buf[1];
+    b= rgb_buf[2];
   });
 
   sysassert(!ferror(screenshot_file));