// $Source: /local/data/cvs/yellowbank/tile/qtile.cc,v $
// $Revision: 1.3 $
// $State: Exp $
// $Date: 2004/07/19 03:54:07 $
// $Author: yrp001 $
// $Locker:  $

// program to draw tile patterns

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "qstset.h"
#include "qpoint.h"
#include "qedge.h"
#include "qglobals.h"
#include "gd.h"

int main( int argc, char* argv[] ) {

  //  if( argc != 2 ) {
  //    std::cerr << "Usage: qtile <filename.png>" << std::endl;
  //    std::cerr << "Expects input on stdin." << std::endl;
  //    exit( 1 );
  //  }

  if( argc != 1 ) {
    std::cerr << "Usage example: qtile < tile.spec > output.png" << std::endl;
    std::cerr << "Expects input on stdin." << std::endl;
    exit( 1 );
  }

  FILE* f_out;
  gdImagePtr im_out;
  int x, y, xt, yt, xc, yc;
  int n;
  int xsize, ysize;
  qpoint pt;
  qstset stset;

  std::cin >> xsize;
  std::cin >> ysize;
  std::cin >> stset;

  im_out = gdImageCreate( xsize, ysize );

  gdImageColorAllocate( im_out, 0, 0, 0 ); // black (background)
  gdImageColorAllocate( im_out, 255, 255, 255 ); // white

  xc = xsize / 2;
  yc = ysize / 2;

  for( y = 0; y < ysize; y++ )
    for( x = 0; x < xsize; x++ ) {
      xt = x - xc;
      //      yt = -1 * (0 - y); // invert y
      yt = y - yc;
      yt *= -1;
      //      yt -= 1;
      pt.setx( xt );
      pt.sety( yt );
      n = stset.findpoint( pt, &pt );
      gdImageSetPixel( im_out, x, y, is_odd( n ) );
    }

  //  f_out = fopen( argv[1], "wb" );
  f_out = stdout;
  gdImagePng( im_out, f_out );
  //  fclose( f_out );
  gdImageDestroy( im_out );

  //  exit( 0 ); // bombs template class detructors
}

