C Programming Code Explanation

Thread Starter

smarch

Joined Mar 14, 2009
52
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");
}

}
 

AlexR

Joined Jan 16, 2008
732
First up when you post code please use the code tags, i.e. go into the advanced editing mode and use the "#" tag to preserve your code formatting. This will make the code a lot easier to read and will probably get you many more responses. Also what formatting we can see in your code is not very good, making it even harder to read.

This is what I think it should look like.
Rich (BB code):
#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");
    

}
So what part of the program don't you understand? You will get more help if you can ask a specific question rather than a general "how does it work". After all you wrote it and you hopefully know what it's supposed to do. We don't!
That said it looks to be a program to fill a 256X128 pixel image array with a gradient and then send the result to a file.
So as I said before, what part of the program don't you understand?
 
First off before you look at the code you should look into how a picture is stored within matrices. There are three matrices augmented so that each have x and y components equal to the x and y pixel length of the picture itself. The third dimension (z) is the red, green, and blue components which are added together for the final image. Zero would be no color while 255 would be full color in the respected z dimension.
 
Top