chiark / gitweb /
Much faster
[ypp-sc-tools.db-test.git] / pctb / pages.c
index 3eb33ce1cb402e01a630e0c29d8235edd7025628..5978f74e365d5e62866422e0d3a59edb43b48d35 100644 (file)
@@ -8,8 +8,6 @@
 #include <X11/keysym.h>
 #include <X11/Xutil.h>
 
-#include <X11/xpm.h>
-
 CanonImage *page_images[MAX_PAGES];
 int npages;
 
@@ -18,8 +16,6 @@ static Display *disp;
 static struct timeval tv_startup;
 static unsigned wwidth, wheight;
 
-static XpmAttributes xpma;
-
 static KeyCode keycode(KeySym sym) {
   return XKeysymToKeycode(disp,sym);
 }
@@ -73,7 +69,7 @@ typedef XImage Snapshot;
 
 //static size_t snapshot_alloc= 1024;
 static double last_input;
-static const double min_update_allowance= 0.5;
+static const double min_update_allowance= 0.25;
 
 static double timestamp(void) {
   struct timeval tv;
@@ -222,83 +218,55 @@ static void raise_and_set_focus(void) {
   fprintf(stderr,"PAGING raise_and_set_focus done.\n");
 }
 
-static void store_page(int pageno, Snapshot *sn) {
-  pid_t converter, paster;
-  eassert(pageno < MAX_PAGES);
-  int paste[2], results[2];
-  FILE *err;
-  int r;
+typedef struct {
+  unsigned long mask;
+  int lshift, rshift;
+} ShMask;
 
-  eassert(!fflush(stdout));
-  eassert(!fflush(stderr));
+static void compute_shift_mask(ShMask *sm, int targshift,
+                              unsigned long ximage_mask) {
+  unsigned long below;
   
-  r= pipe(paste);  eassert(!r);
-  r= pipe(results);  eassert(!r);
-  err= tmpfile();  eassert(err);
-
-  converter= fork();  eassert(converter!=-1);
-  if (!converter) {
-    r= dup2(paste[0],0);  eassert(r==0);
-    r= dup2(results[1],1);  eassert(r==1);
-    r= dup2(2,4);            eassert(r==4); /* fileno(errn) > 4, see above */
-    r= dup2(fileno(err),2);  eassert(r==2);
-    close(paste[0]);
-    close(paste[1]);
-    close(results[0]);
-    close(results[1]);
-    execlp("xpmtoppm", "xpmtoppm",(char*)0);
-    dup2(4,2);
-    eassert(!"xpmtoppm exec failure");
-  }
-
-  char *xpmdata=0;
-  r= XpmCreateBufferFromImage(disp, &xpmdata, sn, 0, &xpma);
-  eassert(!r);
-  eassert(xpmdata);
-
-  paster= fork();  eassert(paster!=-1);
-  if (!paster) {
-    FILE *f= fdopen(paste[1],"w");  eassert(f);
-    close(paste[0]);
-    close(results[0]);
-    close(results[1]);
-    r= fputs(xpmdata,f);  eassert(r!=EOF);
-    //size_t did= fwrite(sn->d, 1, sn->len, f);
-    //eassert(did==sn->len);
-    eassert(!fclose(f));
-    exit(0);
-  }
-
-  close(paste[0]);
-  close(paste[1]);
-  close(results[1]);
-
-  XpmFree(xpmdata);
+  sm->lshift= 0;
+  sm->rshift= 0;
+  sm->mask= 0xfful << targshift;
+  below= ~0ul << targshift;
   
-  FILE *f= fdopen(results[0],"r");
-  int c1= fgetc(f);
-  if (c1!=EOF) {
-    ungetc(c1,f);
-    page_images[pageno]= file_read_image(f);
-    r= fgetc(f);  eassert(r==EOF);  eassert(!ferror(f));  eassert(feof(f));
-    fclose(f);
+  for (;;) {
+    if (ximage_mask < sm->mask) {
+      sm->lshift++;  ximage_mask <<= 1;
+    } else if ((ximage_mask & ~below) > sm->mask) {
+      sm->rshift++;  ximage_mask >>= 1;
+    } else {
+      break;
+    }
+    assert(!(sm->lshift && sm->rshift));
   }
+  assert(sm->lshift < LONG_BIT);
+  assert(sm->rshift < LONG_BIT);
+}
 
-  pid_t got_conv,got_paste;
-  int st_conv, st_paste;
-
-  got_conv= waitpid(converter,&st_conv,0);  eassert(got_conv==converter);
-  got_paste= waitpid(paster,&st_paste,0);  eassert(got_paste==paster);
-
-  if (!st_conv &&
-      (!st_paste || (WIFSIGNALED(st_paste) && WTERMSIG(st_paste)==SIGPIPE))
-      && c1!=EOF) {
-    fclose(err);
-    return;
-  }
-  rewind(err); int c; while ((c=getc(err))!=EOF) fputc(c,stderr);
-  fprintf(stderr, "convert: subprocess statuses: %d %d\n", st_conv, st_paste);
-  _exit(127);
+static void store_page(int pageno, Snapshot *sn) {
+  eassert(pageno < MAX_PAGES);
+  ShMask shiftmasks[3];
+  CanonImage *im;
+
+#define COMPUTE_SHIFT_MASK(ix, targshift, rgb) \
+  compute_shift_mask(&shiftmasks[ix], targshift, sn->rgb##_mask)
+  COMPUTE_SHIFT_MASK(0, 16, red);
+  COMPUTE_SHIFT_MASK(1, 8,  green);
+  COMPUTE_SHIFT_MASK(2, 0,  blue);
+
+  CANONICALISE_IMAGE(im, sn->width, sn->height, {
+    long xrgb= XGetPixel(sn, x, y);
+    int i;
+    rgb= 0;
+    for (i=0; i<3; i++)
+      rgb |= ((xrgb << shiftmasks[i].lshift)
+             >> shiftmasks[i].rshift) & shiftmasks[i].mask;
+  });
+  
+  page_images[pageno]= im;
 }
 
 static void read_pages(void) {