// Generate random images from a DAG quadtree.
// Except this is doing the iteration inside-out, with a very interesting effect

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

enum { n_levels = 9, n_nodes = 8 };

static unsigned char tree[n_levels][n_nodes][4];
/* static unsigned char palette[256][3]; */

static void
randomize()
{
  fread(tree, sizeof(tree), 1, stdin);
  /* for (size_t ii = 0; ii < sizeof(tree); ii++) */
  /*   ((char*)tree)[ii] = random(); */

  /* for (size_t ii = 0; ii < sizeof(palette); ii++) */
  /*   ((char*)palette)[ii] = random(); */
}

static int
pixel(int zorder)
{
  int nn = 0;
  for (int ii = 0; ii < n_levels; ii++)
    nn = tree[ii][nn & (n_nodes - 1)][zorder >> 2 * ii & 3];
  return nn;
}

// Copied and pasted from Hacker's Delight:
// Perfect outer shuffle coded as swaps [GLS] (30 Brisc instructions).

unsigned shuffle2(unsigned x) {
   unsigned t;
   t = (x ^ (x >> 8)) & 0x0000FF00;  x = x ^ t ^ (t << 8);
   t = (x ^ (x >> 4)) & 0x00F000F0;  x = x ^ t ^ (t << 4);
   t = (x ^ (x >> 2)) & 0x0C0C0C0C;  x = x ^ t ^ (t << 2);
   t = (x ^ (x >> 1)) & 0x22222222;  x = x ^ t ^ (t << 1);
   return x;
}


static void 
generate_ppm()
{
  printf("P6\n%d %d\n255\n", 1 << n_levels, 1 << n_levels);
  for (int yy = 0; yy < 1 << n_levels; yy++)
    for (int xx = 0; xx < 1 << n_levels; xx++) {
      int pix = pixel(shuffle2(yy << 16 | xx));
      putchar(pix); putchar(pix); putchar(pix);
      /* fwrite(&palette[pix], 3, 1, stdout); */
    }
}

int
main(int argc, char **argv)
{
  srandom((argc == 1) ? getpid() : atoi(argv[1]));
  randomize();
  generate_ppm();
  return 0;
}


-- 
To unsubscribe: http://lists.canonical.org/mailman/listinfo/kragen-hacks

Reply via email to