Hi, I have constructed the following code, but I have made this code with a lot of help, it is part of a lab exercise, but I am finding it difficult to understand what it does exactly at each point, is there anyone that could give me a clear understanding of how it works? As I now need to modify it to be turned into functions so it will be called in response to a user input. Any help at all would be gratfully appreciated. Thanks.
#include <stdio.h>
#define XSIZE 256
#define YSIZE 128
#define RED 0
#define GREEN 1
#define BLUE 2
int image[YSIZE][XSIZE][3];
main()
{
int x, y;
int r1 = 255;
int r2 = 0;
int g1 = 60;
int g2 = 30;
int b1 = 0;
int b2 = 255;
for(x=0; x<XSIZE; x++)
for(y=0; y<YSIZE; y++)
{
image[y][x][RED] = r1 + (int)(x * (r2 - r1)/XSIZE);
image[y][x][GREEN] = g1 + (int)(x * (g2 - g1)/XSIZE);
image[y][x][BLUE] = b1 + (int)(x * (b2 - b1)/XSIZE);
}
{
FILE *pfile = NULL;
int x,y;
pfile = fopen("myfile.ppm", "w");
fprintf(pfile, "P3\n# %s\n%d %d\n255\n", "Myfile.ppm", XSIZE, YSIZE);
for(y = 0; y < YSIZE; y++)
for(x = 0; x < XSIZE; x++)
fprintf(pfile,"%d %d %d\n",image[y][x][0],image[y][x][1],image[y][x][2]);
fclose(pfile);
system("myfile.ppm");
}
}
#include <stdio.h>
#define XSIZE 256
#define YSIZE 128
#define RED 0
#define GREEN 1
#define BLUE 2
int image[YSIZE][XSIZE][3];
main()
{
int x, y;
int r1 = 255;
int r2 = 0;
int g1 = 60;
int g2 = 30;
int b1 = 0;
int b2 = 255;
for(x=0; x<XSIZE; x++)
for(y=0; y<YSIZE; y++)
{
image[y][x][RED] = r1 + (int)(x * (r2 - r1)/XSIZE);
image[y][x][GREEN] = g1 + (int)(x * (g2 - g1)/XSIZE);
image[y][x][BLUE] = b1 + (int)(x * (b2 - b1)/XSIZE);
}
{
FILE *pfile = NULL;
int x,y;
pfile = fopen("myfile.ppm", "w");
fprintf(pfile, "P3\n# %s\n%d %d\n255\n", "Myfile.ppm", XSIZE, YSIZE);
for(y = 0; y < YSIZE; y++)
for(x = 0; x < XSIZE; x++)
fprintf(pfile,"%d %d %d\n",image[y][x][0],image[y][x][1],image[y][x][2]);
fclose(pfile);
system("myfile.ppm");
}
}