Code:
#include <stdio.h> //includes all header files, both from source code and our own files
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "prototypes.h"
#include "prototype2h.h"
pixel_colour floor_colour;
pixel_colour line_colour;
#define Layer 3
extern int x,y;
extern int xs, xe, ys, ye;
extern int debug=1;
extern int ***image ;
extern int High, Wide;
extern float m;
static number=-1;
int counter, hold, z, k=1;
long size;
int *pnum = NULL;
char filename[120], file[20], command[]={"ab"};
user input[1000000];
char opt[] ={"SZ"};
char opt1[] ={"FL"};
char opt2[] ={"FI"};
char opt3[] ={"LI"};
char opt4[] ={"CH"};
char opt5[] ={"RE"};
char opt6[] ={"EX"};
char opt7[] ={"HP"};
void swap(char *opt1, char *opt2)
{
char *temp = (char *)malloc((strlen(opt1)+1)*sizeof(char));
strcpy(temp, opt1);
strcpy(opt1, opt2);
strcpy(opt2, temp);
free(temp);
}
int FloodImage(pixel_colour flood_colour)
{
flood_colour.red=*(pnum);
flood_colour.green=*(pnum+1);
flood_colour.blue=*(pnum+2);
for (y = 0; y < High; y++)
{
for(x = 0; x < Wide; x++)
{
image[y][x][0]=flood_colour.red;
image[y][x][1]=flood_colour.green;
image[y][x][2]=flood_colour.blue;
}
}
}
int Draw_Line_pen(int xs,int xe, int ys, int ye, pixel_colour line_colour)
{
xs=*(pnum);
xe=*(pnum+1);
ys=*(pnum+2);
ye=*(pnum+3);
line_colour.red=*(pnum+4);
line_colour.green=*(pnum+5);
line_colour.blue=*(pnum+6);
if (xs>=0 && xe<= Wide && ys>=0 && ye<=High)
{
goto drawline;
return(0);
}
else if (xs<0 && ys<0 && xe<=Wide && ye<=High)
{
printf("The starting point of the line is not within the image, please type in new co-ordinates for the line\n");
return(1);
}
else if (xe>Wide && ye>High && xs>=0 && ys>=0)
{
printf("The finishing point of the line is not within the image, please type in new co-ordinates for the line\n");
return(2);
}
else if((xs<0 && xe>Wide && ys>=0 && ye<=High) || (xs>=0 && xe<=Wide && ys<0 && ye>High))
{
printf("A line co-ordinate is not within the image, please type in new co-ordinates for the line\n");
return(3);
}
else if(xs<0 && ys<0 && xe>Wide && ye>High)
{
printf("None of the line co-ordinates are within the image, pleae type in new co-ordinates for the line\n");
return(4);
}
else
{
printf("Incorrect line point try again");
return(5);
}
drawline:
if (abs(xe - xs) > abs(ye - ys)) //check gradient less than one
{
m = (ye - ys)/(float)(xe - xs); //Calculate gradient
if (xs > xe)
{
m = (xe - xs)/(float)(ye - ys); //Calculate gradient
if (ys > ye)
{
int temp; //swap xs and xe and ys and ye
temp = xs; xs = xe; xe = temp;
temp = ys; ys = ye; ye = temp;
}
for (y = ys; y < ye; y++)
{
x = (int)(xs + (m * (y - ys))); //changes array elements to draw line
image[y][x][0] = line_colour.red;
image[y][x][1] = line_colour.green;
image[y][x][2] = line_colour.blue;
}
}
else {
int temp; //swap xs and xe and ys and ye, similar to swap function above
temp = xs; xs = xe; xe = temp;
temp = ys; ys = ye; ye = temp;
}
for (x = xs; x < xs; x++)
{
y = (int)(ys + (m * (x - xs)));
image[y][x][0] = line_colour.red; //changes array elements to draw line
image[y][x][1] = line_colour.green;
image[y][x][2] = line_colour.blue;
} // gradient greater than one if here
}
}
int RefreshImage( )
{
FILE *pfile=NULL;
sprintf(filename,"Image%04d.ppm",number);
pfile=fopen(filename,"w");
fprintf(pfile,"P3\n #Image%i.ppm\n%d %d\n255\n", number, Wide, High);
drawline:
for (y=0 ; y<High; y++)
{
for (x = 0; x<Wide; x++)
{
fprintf(pfile,"%4i ",image[y][x][0]); //prints line array information to the .ppm file
fprintf(pfile,"%4i ",image[y][x][1]);
fprintf(pfile,"%4i ",image[y][x][2]);
}
}
fclose(pfile);
}
int Menu( )
{
printf("\n****************************************************");
printf("\n***************COMMAND LIST*************************");
printf("\n* Ryan Segal and Luke Coomber *");
printf("\n* *");
printf("\n* A list of commands are shown below: *");
printf("\n* *");
printf("\n* SZ x y - Sets the height and width of the image.*");
printf("\n* FL r g b - Fills background with chosen colour. *");
printf("\n* FI filename - Reads commands from file in dir. *");
printf("\n* LI xs xe ys ye r g b - Draws a line from (xs,ys)*");
printf("\n* to (xe, ye) in the *");
printf("\n* specified colour. *");
printf("\n* If error occurs, use the below troubleshooter: *");
printf("\n* 0: Line is working. *");
printf("\n* 1: Starting point is invalid. *");
printf("\n* 2: Ending point is invalid. *");
printf("\n* 3: Both start and end points are invalid.*");
printf("\n* 4: All points of the line are invalid. *");
printf("\n* CH - Saves a copy whilst writting all commands *");
printf("\n* to a new image. *");
printf("\n* RE - Saves a copy whilst keeping current image *");
printf("\n* open for further editing. *");
printf("\n* HP - Displays the current menu again. *");
printf("\n* EX - Closes the program *");
printf("\n****************************************************");
printf("\n****************************************************");
}
int CheckpointImage( )
{
number=number+1;
RefreshImage( );
}
int getfile( )
{
FILE *pfile;
printf("Open the file: %s\n", file);
pfile=fopen(file,"rt");
*(pnum) = 0; //predefines required pointers
*(pnum+1) = 0;
*(pnum+2) = 0;
*(pnum+3) = 0;
*(pnum+4) = 0;
*(pnum+5) = 0;
*(pnum+6) = 0;
if (pfile)
{
while(!feof(pfile))
{
fgets(input[z].code, 30, (pfile));
z++;
}
hold = z;
}
else
{
printf("Error");
}
z = 0;
line:
counter = 0;
z =0;
for(z=0;z<hold;z++)
{
if(strncmp(input[z].code, opt, 2) == 0)
{
sscanf(input[z].code,"%s %d %d", command, &Wide, &High);
image = (int ***)malloc(sizeof(int **) * High);
for (y=0; y<High; y++)
{
image[y] = (int **)malloc(sizeof(int *) * Wide);
for (x=0; x<Wide;x++)
image[y][x] = (int *)malloc(sizeof(int) * Layer);
}
}
else if(strncmp(input[z].code, opt3, 2) == 0)
{
rerun:
if(strncmp(input[z-1].code, opt3, 2) == 0)
{
goto jump;
}
else if(strncmp(input[z+counter].code, opt1, 2) == 0)
{
swap(input[z].code, input[z+counter].code);
if(strncmp(input[z+1].code, opt3, 2) == 0)
{
do
{
swap(input[z+k].code, input[z+counter].code);
k++;
}
while((z+k)<(z+counter));
goto line;
}
else
{
goto jump;
}
}
else
{
if(strncmp(input[z+counter].code, opt4, 2) == 0)
{
counter = 0;
goto jump;
}
else
{
counter++;
goto rerun;
}
}
jump:
sscanf(input[z].code,"%s %d %d %d %d %d %d %d", command, pnum, (pnum+1), (pnum+2), (pnum+3), (pnum+4), (pnum+5), (pnum+6));
Draw_Line_pen(xs, ys, xe, ye, line_colour);
}
else if(strncmp(input[z].code, opt1, 2) == 0) //Compares to and runs flood image function
{
sscanf(input[z].code,"%s %d %d %d",command, pnum, (pnum+1), (pnum+2));
FloodImage(flood_colour);
}
else if(strncmp(input[z].code, opt2, 2) == 0)
{
call: //Compares to and runs call_file function
sscanf(input[z].code,"%s",command);
getfile( );
}
else if(strncmp(input[z].code, opt5, 2) == 0)
{
sscanf(input[z].code,"%s",command); //Compares to and runs Rrefresh image function
RefreshImage( );
}
else if (strcmp(command, opt7) == 0)
{
Menu( ); //Compares to and runs help function
}
else if(strncmp(input[z].code, opt4, 2) == 0) //Compares to and runs checkpoint image function
{
sscanf(input[z].code,"%s",command);
CheckpointImage( );
k = 1;
}
}
line1:
fclose(pfile); //closes file
}
int Input( )
{
pnum=(int*)malloc(7*sizeof(int)); //reserves memory for pnum pointers
printf("Create an image using commands followed found in the help command\n");
z = 1;
*(pnum) = 0; //declares pointers
*(pnum+1) = 0;
*(pnum+2) = 0;
*(pnum+3) = 0;
*(pnum+4) = 0;
*(pnum+5) = 0;
*(pnum+6) = 0;
High = 128;
Wide = 256;
rturn:
z=0;
do
{
scanf("%s",command);
if(strcmp(command,opt) == 0)
{
scanf("%d %d", &Wide, &High);
image = (int ***)malloc(sizeof(int **) * High);
for (y=0; y<High; y++)
{
image[y] = (int **)malloc(sizeof(int *) * Wide);
for (x=0; x<Wide;x++)
image[y][x] = (int *)malloc(sizeof(int) * Layer);
}
}
else if(strcmp(command, opt3) == 0)
{
scanf("%d%d%d%d%d%d%d",pnum, (pnum+1), (pnum+2), (pnum+3), (pnum+4), (pnum+5), (pnum+6));
sprintf(input[z].code,"%s %d %d %d %d %d %d %d",command, *(pnum), *(pnum+1), *(pnum+2), *(pnum+3), *(pnum+4), *(pnum+5), *(pnum+6));
if(!(*(pnum)>=0 && *(pnum+2)<=Wide && *(pnum+1)>=0 && *(pnum+3)<=High))
{
z = z-1;
DrawLine(xs, ys, xe, ye, line_colour);
}
}
else if (strcmp(command,opt2) == 0)
{
scanf("%s",file);
sprintf(input[z].code,"%s",command);
goto call;
}
else if (strcmp(command , opt1) == 0)
{
scanf("%d%d%d",pnum, (pnum+1), (pnum+2));
sprintf(input[z].code,"%s %d %d %d",command, *(pnum), *(pnum+1), *(pnum+2));
if(*(pnum)> 255 || *(pnum+1)>255 || *(pnum+2)> 255 || *(pnum)< 0 || *(pnum+1)<0 || *(pnum+2)<0)
{
printf("The colour component is out of range. Try again\n");
z=z-1;
}
}
else if (strcmp(command , opt5) == 0)
{
sprintf(input[z].code,"%s",command);
printf("[Image has been drawn]\n");
printf("[All points within image.]\n");
goto line;
}
else if (strcmp(command , opt4) == 0)
{
sprintf(input[z].code,"%s",command);
printf("[Image has been drawn]\n");
printf("[All points within image.]\n");
goto line;
}
else if (strcmp(command , opt6) == 0)
{
sprintf(input[z].code,"%s",command);
exit(1);
return 0;
}
else if (strcmp(command, opt7) == 0)
{
Menu( );
}
else
{
z=z-1;
printf("Unknown command. Please Try again, use help command if needed\n");
}
z++;
}
while(z < 9000);
line:
counter = 0;
z =0;
for(z=0;z<9000;z++)
{
if(strncmp(input[z].code, opt3, 2) == 0)
{
rerun:
if(strncmp(input[z-1].code, opt3, 2) == 0)
{
goto jump;
}
else if(strncmp(input[z+counter].code, opt1, 2) == 0)
{
swap(input[z].code, input[z+counter].code);
if(strncmp(input[z+1].code, opt3, 2) == 0)
{
do
{
swap(input[z+k].code, input[z+counter].code);
k++;
}
while((z+k)<(z+counter));
goto line;
}
else
{
goto jump;
}
}
else
{
if(strncmp(input[z+counter].code, opt4, 2) == 0)
{
counter = 0;
goto jump;
}
else
{
counter++;
goto rerun;
}
}
jump:
sscanf(input[z].code,"%s %d %d %d %d %d %d %d", command, pnum, (pnum+1), (pnum+2), (pnum+3), (pnum+4), (pnum+5), (pnum+6));
DrawLine(xs, ys, xe, ye, line_colour);
}
else if(strncmp(input[z].code, opt1, 2) == 0)
{
sscanf(input[z].code,"%s %d %d %d",command, pnum, (pnum+1), (pnum+2));
FloodImage(flood_colour);
}
else if(strncmp(input[z].code, opt2, 2) == 0)
{
call:
sscanf(input[z].code,"%s",command);
getfile( );
goto rturn;
}
else if(strncmp(input[z].code, opt5, 2) == 0)
{
sscanf(input[z].code,"%s",command);
RefreshImage( );
goto rturn;
}
else if(strncmp(input[z].code, opt4, 2) == 0)
{
sscanf(input[z].code,"%s",command);
CheckpointImage( );
k = 1;
goto rturn;
}
}
}