1.  #include <stdio.h>
  2.  #include <math.h>
  3.  #include <png.h>
  4.  #include "bsfr.h"
  5.  
  6.  #define BITS 8
  7.  const int ascii = 1;
  8.  
  9.  png_byte row [ 4 * (1 << BITS) ];
  10.  
  11.  /* very fast lowest common ancestor
  12.   * in binary inorder tree
  13.   */
  14.  int lca_bin (int a, int b)
  15.  {
  16.   if (a == b)
  17.   return 255;
  18.  
  19.   /* swap variables */
  20.   if (b > a)
  21.   a ^= b ^= a ^= b;
  22.  
  23.   /* GiM's lca-trick
  24.   * if smaller number is even, and bigger is in _specified_ range,
  25.   * return lower one :)
  26.   */
  27.   if (!(b & 1) && a < b + (1 << (bsf (b))))
  28.   return (b);
  29.  
  30.   return a & ~((1 << bsr (a ^ b))-1);
  31.  }
  32.  
  33.  /* let's show only diagonal and the interesting edges :) */
  34.  int lca_bin_cool (int a, int b, int mask)
  35.  {
  36.   if (a == b)
  37.   return 255&mask;
  38.  
  39.   /* swap variables */
  40.   if (b > a)
  41.   a ^= b ^= a ^= b;
  42.  
  43.   if (!(b & 1) && a < b + (1 << (bsf (b))))
  44.   return 127&mask;
  45.  
  46.   if (mask)
  47.   return 0;
  48.   else
  49.   return a & ~((1 << bsr (a ^ b))-1);
  50.  }
  51.  
  52.  void write_row_callback(png_structp png_ptr, png_uint_32 row, int pass)
  53.  {
  54.   printf (".");
  55.   return;
  56.  }
  57.  
  58.  const char *user_error_ptr = "";
  59.  static void user_error_fn(png_structp a, png_const_charp error_msg)
  60.  {
  61.   printf ("%s\n", error_msg);
  62.  }
  63.  
  64.  static void user_warning_fn(png_structp a, png_const_charp warning_msg)
  65.  {
  66.   printf ("%s\n", warning_msg);
  67.  }
  68.  
  69.  int main(int argc, char **argv)
  70.  {
  71.   FILE *fp = fopen(--argc?*++argv:"output.png", "wb");
  72.   int j, i;
  73.   png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
  74.   (png_voidp)user_error_ptr, user_error_fn, user_warning_fn);
  75.   png_infop info_ptr;
  76.   png_bytep row_pointer;
  77.   /*png_colorp palette;*/
  78.  
  79.   if (!fp)
  80.   {
  81.   printf (" [!] Failed while trying to create output file\n");
  82.   return (-1);
  83.   }
  84.   if (!png_ptr)
  85.   {
  86.   printf (" [+] png_crate_write_struct failed!\n");
  87.   return (-1);
  88.   }
  89.  
  90.   if (!(info_ptr = png_create_info_struct(png_ptr)))
  91.   {
  92.   printf (" [!] png_create_info_struct failed!\n");
  93.   png_destroy_write_struct (&png_ptr, (png_infopp)NULL);
  94.   return (-1);
  95.   }
  96.  
  97.   if (setjmp (png_jmpbuf (png_ptr)))
  98.   {
  99.   png_destroy_write_struct (&png_ptr, &info_ptr);
  100.   fclose (fp);
  101.   return (-1);
  102.   }
  103.   png_init_io (png_ptr, fp);
  104.   png_set_write_status_fn (png_ptr, write_row_callback);
  105.   /* png_set_filter (png_ptr, 0,
  106.   */
  107.  
  108.   png_set_IHDR (png_ptr, info_ptr, 1 << BITS, 1 << BITS,
  109.   8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
  110.   PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  111.   /* palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH * sizeof (png_color));
  112.   * png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
  113.   */
  114.  
  115.   png_set_invert_alpha (png_ptr);
  116.   png_write_info (png_ptr, info_ptr);
  117.  
  118.   for (j = 0; j < 1 << BITS; j++)
  119.   {
  120.   if (j)
  121.   for (i = 1; i < 1 << BITS; i++) {
  122.   row [4*i + 0] = lca_bin_cool(i, j, 0xff);
  123.   row [4*i + 1] = 0;
  124.   row [4*i + 2] = 0;
  125.   row [4*i + 3] = 0;
  126.   }
  127.   row_pointer = row;
  128.   png_write_row (png_ptr, row_pointer);
  129.   }
  130.  
  131.   png_write_end (png_ptr, info_ptr);
  132.   png_destroy_write_struct (&png_ptr, &info_ptr);
  133.   printf ("\n [+] image written!\n");
  134.  
  135.   fclose(fp);
  136.  
  137.   return 0;
  138.  }
  139.  

This document should validate, please check by clicking: Valid XHTML 1.1