#include <stdio.h>
#include <stdlib.h>
#include "aa2pgm.h"
#include "pgm_font.h"

/* 
 * Function Prototypes 
 */
int read_ascii(FILE *afptr, char ascii_data[]);
int read_ascii_art(char *afile, char ascii_data[]);
pgm_header_t init_pgm_header(char ascii_data[], int size);
void write_pgm_header(FILE *fptr, pgm_header_t *ph);
void ascii2pgm(pgm_header_t *ph, char ascii_data[], int filesize, int pgmdata[][PGM_WIDTH]);
void write_pgm_data(FILE *fptr, int pgmdata[][PGM_WIDTH], pgm_header_t *ph);

/*
 * Main() function
 */
int main(int argc, char *argv[])
{
	char *afile;				/* ascii file name */
	char *pfile;				/* PGM file name */
	char *ffile;				/* font file name */
	FILE *pfptr;
	int filesize;
	char ascii_data[MAXCHARS];  		/* chars from file */
	int pgmdata[PGM_HEIGHT][PGM_WIDTH];  	/* raster values for pgm file */
	pgm_header_t ph;			/* PGM Header */

	/* commandline params */
	if( /* ADD CODE HERE */ )
	{
		/* ADD CODE HERE */
	}
	else
	{
		printf("Usage: aa2pgm ascii_file pgm_file font_file\n");
		return -1;
	}

	/* Initialize font */
	initialize_font(ffile); 

	/* Read in Ascii art */
	filesize = read_ascii_art(afile, ascii_data);

	/* Open pgm file (output only) */
	pfptr = fopen(pfile, "w");
	if( pfptr == NULL)
	{
		perror("Openning PGM File:");
		exit(-1);
	}

	/* write header to file */
	ph = init_pgm_header(ascii_data, filesize);
	write_pgm_header(pfptr, &ph);

	/* convert ascii to pgm */
	ascii2pgm(&ph, ascii_data, filesize, pgmdata);

	/* write rows and colums to file */
	write_pgm_data(pfptr, pgmdata, &ph);

	/* close PGM file */
	fclose(pfptr);

	return 0;
}



/*
 * Function Implementations
 */

int read_ascii_art(char *afile, char ascii_data[])
/*
 * PURPOSE : Read the ascii art from the supplied file and
 * store it in the supplied array.
 *
 * RETURN :  The number of characters in the file.
 *
 * NOTES :   Depends on "read_ascii".
 */
{
	/* ADD CODE HERE */
}

int read_ascii(FILE *afptr, char ascii_data[])
/*
 * PURPOSE : Reads each char from the file and stores
 * in the given array.
 *
 * RETURN :  The number of characters in the file.
 *
 * NOTES :   Called from read_ascii_art()
 */
{
	char c;
	int i=0;

	while( fscanf(afptr, "%c", &c) != EOF )
	{
		ascii_data[i] = c;
		i++;
	}

	return i;
}

void write_pgm_data(FILE *fptr, int pgmdata[][PGM_WIDTH], pgm_header_t *ph)
/*
 * PURPOSE : Writes the data in pgmdata to the ".pgm" file (fptr).
 *
 * RETURN :  void
 *
 * NOTES :   Assumes "fptr" is a file stream that has already been opened
 * for writing.
 */
{
	/* ADD CODE HERE */
}

pgm_header_t init_pgm_header(char ascii_data[], int size)
/*
 * PURPOSE : Reads through the ascii data to determine the width and
 * height of the pgm data (needed to write the PGM header).
 *
 * RETURN :  A structure that contains all the data necessary to fill
 * out the PGM header.
 *
 * NOTES :   PGM files written in ascii values have a magic number "P2".
 */
{
	pgm_header_t ph;
	int row=0;
	int width=0;

	/* ADD CODE HERE */

	ph.magic_num[0] = 'P';
	ph.magic_num[1] = '2';
	ph.width = width;
	ph.height = row;
	ph.maxval = MAXVAL; 

	return ph;
}

void write_pgm_header(FILE * fptr, pgm_header_t *ph)
/*
 * PURPOSE : Writes the PGM header information to the PGM file.
 *
 * RETURN :  void
 *
 * NOTES :   None.
 */
{

	/* See "man pgm" for a discussion of the header fields. */

	/* ADD CODE HERE */

	return;
}

void fill_pgm( int pgm_data[][PGM_WIDTH], int row, int col, char c)
/*
 * PURPOSE : Write the ascii characters to the pgm_data array.  Remember,
 * one character is a CROWxCCOL grid of pgm data.
 *
 * RETURN :  void
 *
 * NOTES :   none.
 */
{
	int i, j;
	int ch = c;
	
	for(i=0; i<CROW; i++)
	{
		for(j=0; j<CCOL; j++)
		{
			/* write ascii character to the given section of the grid */
			pgm_data[row+i][col+j] = pgm_font[ch][i][j];		
		}
	}

	return;
}

void ascii2pgm(pgm_header_t *ph, char ascii_data[], int filesize, int pgmdata[][PGM_WIDTH])
/*
 * PURPOSE : This does the real work.  For every character in the ascii art, this function
 * looks up it's representation in the pgm font array and writes the corresponding CROWxCCOL
 * data to the pgm array.
 *
 * RETURN :  void
 *
 * NOTES :   This function assumes that each line in the ascii art is the same length.
 */
{
	int i,j;
	int row,col;
	int idx;
	
	/* initialize the pgm picture to have a black background */
	for(i=0; i<PGM_HEIGHT; i++)
	{
		for(j=0; j<PGM_WIDTH; j++)
		{
			pgmdata[i][j] = 0;
		}
	}


	/* For every character, write it to the PGM array */

	/* ADD CODE HERE */

	return;
}