chiark / gitweb /
WIP island determination; pixmap handling in progress
[ypp-sc-tools.main.git] / pctb / rgbimage.c
diff --git a/pctb/rgbimage.c b/pctb/rgbimage.c
new file mode 100644 (file)
index 0000000..e49044f
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * Handling of colour (RGB) images
+ */
+/*
+ *  This is part of ypp-sc-tools, a set of third-party tools for assisting
+ *  players of Yohoho Puzzle Pirates.
+ * 
+ *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
+ * 
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ * 
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ * 
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * 
+ *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
+ *  are used without permission.  This program is not endorsed or
+ *  sponsored by Three Rings.
+ */
+
+#include "convert.h"
+
+/*
+ *
+ *
+ * widgets.txt
+ * format
+ *
+ *  <magic>
+ *
+ *  <context>
+ *  <string>
+ *  ppmnoraw depth 8
+ *
+ *  <context>
+ *  <string>
+ *  ppmnoraw depth 8
+ *  .
+ *
+ */
+
+#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;
+
+#define FGETSLINE (fgetsline(f,result,MAXIMGIDENT))
+
+  FGETSLINE;
+fprintf(stderr,">%s<\n",result);
+  dbassert(!strcmp(result,"# ypp-sc-tools pctb pixmaps v1"));
+  
+  void *row= 0;
+  size_t rowa= 0;
+  
+  for (;;) {
+    FGETSLINE;
+    if (!result || 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);
+
+    if (rowa < inpam.width) {
+      rowa= inpam.width;
+      row= mrealloc(row, rowa*3);
+    }
+    int diff=
+      inpam.width  != RECT_W(portion) ||
+      inpam.height != 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);
+    }
+    if (!diff)
+      return 1;
+  }
+  return 0;
+}
+void identify_rgbimage(const RgbImage *base, Rect portion,
+                      char result[MAXIMGIDENT]) {
+  int ok= identify(base, portion, result);
+  if (ok) return;
+
+  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);
+    }
+  }
+  fatal("Unrecognised pixmap.");
+}
+
+RgbImage *alloc_rgb_image(int w, int h) {
+  RgbImage *ri;
+  ri= mmalloc(sizeof(*ri) + w*h*3);
+  ri->w= w;
+  ri->h= h;
+  return ri;
+}