Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

2D image generator and modification using C, updated part 2 (drawing a line)

  1. Jan 18, 2009 #1
    hello, Part of a little project of mine consists of writing C code to build a image file in .ppm format which can be viewed by the simple program xnview. The image size is going to be 256 x 128.

    My code below generates a image with a smooth transtiton/graident between very dark red to light red, across the image. I have added comments to the code so you know what i am doing where.

    This is only part of my plan however

    #define IWIDE 256 // defines the width of the image
    #define IHIGH 128 // defines the height of the image
    #define RANGE 255 // defines the range that the colour can take

    void main(void)
    unsigned char image[IWIDE][IHIGH][3];
    int x, y;// loop control variables
    int red1 = 25, red2 = 233;
    float redgrad = 0.0;
    float redval = 0.0;
    //char redval = (unsigned char)128;
    FILE * ppmfile = NULL; // clears the file
    ppmfile = fopen("image.ppm","w"); // Opens the file to be written
    // What is to be written to the file
    fprintf(ppmfile, "P3\n");
    fprintf(ppmfile, "# feep.ppm\n");
    fprintf(ppmfile, "%d %d\n",IWIDE , IHIGH );
    fprintf(ppmfile, "%d\n\n",RANGE);
    redgrad = (float) (red2 - red1) / IWIDE;
    for (y = 0; y < IHIGH; y ++)
    for (x = 0; x < IWIDE; x ++)
    redval = red1 + redgrad*x ;
    image[x][y][0] = (unsigned char) redval; image[x][y][1] = 0; image[x][y][2] = 0; /* sets all of the colour values to zero */
    fprintf(ppmfile, "%4u %4d %4d \n", image[x][y][0], image[x][y][1], image[x][y][2]); /* prints all of the different values */

    what i want to be able to do now is....

    (1) i want my final version to allow the user to supply the colours as 2 sets of 3 values (red, green and blue) between 0 and 255. Range checking should be implemented to ensure that colours outside this range are rejected.

    i have forgotten how and where to do the user input?? and how to range check to make sure the value is between 0 and 255? majorly stuck on this. :(

    next i want to...

    (2) write into the same piece of code a way of drwaing a line in a given colour between any two points in the 256 wide by 128 high image that i have created in the first part. The colour and the coordinates should be entered by the user. In addition the program should ask after each line if the user wants to input another.

    i have no idea how i am going to do this!

    ANY help, tips code or advice would be great, many thanks, i really appreciate your time and effort!!!!
  2. jcsd
  3. Jan 19, 2009 #2
    I see that you have the scanf line commented out. You can look at a reference for scanf such as this page. There's more than one way you could do this. You could use the %u, %x or %s format specifiers. If you use %s, you will have to use something like atoi to convert it to a number. After you retrieve the user's value, you have to do range checking. You could just do a do/while loop for this - just terminate the loop when the values are valid. Something like this would work:

    do {
    printf("Enter the red component's value:");
    scanf(%u, &red);
    } while (red <= 255);

    You can just Google line-drawing algorithms. One of the first pages that pops up is http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html. If you are going to delve much deeper into graphics code, you probably want to check out some books specifically on graphics programming.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: 2D image generator and modification using C, updated part 2 (drawing a line)