#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "paulslib.h"
#include "bitmaplib.h"

// Image width and height
#define nxy 731

int main(int argc,char **argv)
{
   int i,j;
   int **a = NULL;
   int c = 2;
   BITMAP4 *image,white={255,255,255,0},black={0,0,0,0};
   char fname[64];
   FILE *fptr;

   /* Form the matrix */
   a = malloc((nxy+1)*sizeof(int *));
   for (i=0;i<=nxy;i++)
      a[i] = malloc((nxy+1)*sizeof(int));
   for (i=0;i<=nxy;i++)
      for (j=0;j<=nxy;j++)
         a[i][j] = 0;
   a[0][0] = 1;
   a[0][1] = 1;
   a[1][0] = 1;
   a[1][1] = c;

   /* Create the image */
   image = Create_Bitmap(nxy,nxy);
   Erase_Bitmap(image,nxy,nxy,white);

   /* Draw the cells of the carpet */
   for (i=2;i<nxy;i++) {
      for (j=1;j<=i;j++) {
         if (j-1 == 0)
            a[i][j-1] = 1;
         a[i][j] = (a[i][j-1] + a[i-1][j] + a[i-1][j-1]) % (c+1);
         if (a[i][j] > 0)
            Draw_Pixel(image,nxy,nxy,i,j,black);
      }
   }

   /* Write the image */
   sprintf(fname,"pascal.tga");
   if ((fptr = fopen(fname,"w")) == NULL) {
      fprintf(stderr,"Unable to open bitmap file\n");
      exit(0);
   }
   Write_Bitmap(fptr,image,nxy,nxy,12);
   fclose(fptr);

   exit(0);
}

