C Programming Code Explanation

Discussion in 'Programmer's Corner' started by smarch, May 6, 2010.

  1. smarch

    Thread Starter Active Member

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

    }
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Help us out - what is the code supposed to do?
     
  3. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    That's why it's important to comment your code.
     
  4. ELECTRONERD

    Senior Member

    May 26, 2009
    1,146
    16
    I would also put your code within the CODE tags for an easier reading format.

    Austin
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
    Code ( (Unknown Language)):
    1. #include <stdio.h>
    2.  
    3. #define XSIZE 256
    4. #define YSIZE 128
    5. #define RED 0
    6. #define GREEN 1
    7. #define BLUE 2
    8.  
    9. int image[YSIZE][XSIZE][3];
    10.  
    11. main()
    12. {
    13.     int x, y;
    14.  
    15.     int r1 = 255;
    16.     int r2 = 0;
    17.     int g1 = 60;
    18.     int g2 = 30;
    19.     int b1 = 0;
    20.     int b2 = 255;
    21.  
    22.  
    23.     for(x=0; x<XSIZE; x++)
    24.     {
    25.         for(y=0; y<YSIZE; y++)
    26.         {
    27.             image[y][x][RED] = r1 + (int)(x * (r2 - r1)/XSIZE);
    28.             image[y][x][GREEN] = g1 + (int)(x * (g2 - g1)/XSIZE);
    29.             image[y][x][BLUE] = b1 + (int)(x * (b2 - b1)/XSIZE);
    30.         }
    31.     }
    32.     FILE *pfile = NULL;
    33.     int x,y;
    34.     pfile = fopen("myfile.ppm", "w");
    35.     fprintf(pfile, "P3\n# %s\n%d %d\n255\n", "Myfile.ppm", XSIZE, YSIZE);
    36.     for(y = 0; y < YSIZE; y++)
    37.     {
    38.         for(x = 0; x < XSIZE; x++)
    39.         {
    40.             fprintf(pfile,"%d %d %d\n",image[y][x][0],image[y][x][1],image[y][x][2]);
    41.         }
    42.     }
    43.     fclose(pfile);
    44.     system("myfile.ppm");
    45.    
    46.  
    47. }
    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?
     
  6. bassplayer142

    Active Member

    Jan 2, 2007
    89
    0
    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.
     
Loading...