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 using C and nxview

  1. Jan 6, 2009 #1
    hello, Part of a little project of mine consists of using arrays, loops and writing files, in C 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.

    I can use notepad to create simple images of flags ect. but am struggling on using C for this function.

    Here is a simple .ppm image constructed in notepad

    # a 3x3 RGB image.ppm
    3 3
    0 100 0 100 0 0 0 0 100
    0 100 0 100 0 0 0 0 100
    0 100 0 100 0 0 0 0 100

    As you can see its in the format RGB and to create a large image like 256x128 would take too long.

    Researching I found the best way was to...

    Declare an approprite sized array and build the required image in that using loops.
    Write this to a file in .ppm format and view in nxview.

    Here is one of my attempts but something isnt working.

    #include <stdio.h>
    #define WIDTH 25
    #define HEIGHT 12

    void main(void)
    int i,j,k; //loop control variables
    int width = 15,height = 16, grad = 3;
    char outchar = '*';
    char image[HEIGHT][WIDTH][3];

    for ( i = 0; i < HEIGHT; i++)
    for ( j = 0; j < WIDTH; j++)
    image[j][0] = i;
    image[j][1] = j;
    image[j][2] = 2*i;
    //if (i==4) width = 4;

    for ( i = 0; i < HEIGHT; i++)
    for ( j = 0; j < WIDTH; j++)
    printf ("%d ",image[j][0]);
    printf ("%d ",image[j][1]);
    printf ("%d\n",image[j][2]);
    if (i==4) width = 4;

    char image[128][256][3];
    FILE * ppmfile = NULL;
    ppmfile = fopen("myfile.ppm","w");
    printf ("%c", image[j][0]);

    fprintf(ppmfile, "%3d %3d %3d \n",image[10][20][0], 0, 0);
    fprintf(stdout, "%3d %3d %3d \n",0, 0, 0);


    I also want to generate a smooth transtiton/graident between two colours across the image.
    any help tips of info would be great, thanks
  2. jcsd
  3. Jan 13, 2009 #2
    How exactly is it not working? Is xnview not liking your input? I'm assuming that you are putting the header in manually, since it's not in your code. Everything looks fine up until your redefinition of the image variable. I'm also assuming you had that part commented out, since there is still the */ after the fclose. If you did everything the same as you had it up until then but with fprintf after opening a file, it should be fine. Of course, you would need to write the header before your second loop. In the second part that looks like you commented it out, your code looks okay, but you are using uninitialized data. I don't see a reason that you would have to use the %3d format specifier over %d, since unsigned chars are never going to be more than 3 characters wide. You probably also want to use an unsigned char for your array, since image values usually go from 0->255. If you assigned the value 128 or higher, as a regular char, the output would be a negative number (not defined in the ppm specification as far as I can remember).

    The PPM specs say that you can have as much white space as you want between each component. I notice that you have a \n after every pixel and an extra \n after each line, which may not be fine by the specs. There is the possibility that some image viewers may expect spaces in between pixels, not \n. Usually, you write each line of data with spaces and then put a \n at the end. However, if your data runs longer than 70 characters, you have to wrap it around to the next line.

    To do a gradient between two colors, you can just do a linear interpolation between each component of the color separately. For instance, you can calculate an interpolated value between the red of the color on your left and the red of the color on your right to determine the red value of a pixel. Then do that for the green and blue components.

    I hope that helps. Just let me know if you have anymore trouble with it.
  4. Jan 18, 2009 #3
    Thanks so much for the help, i was wondering if you could check out my updated version where i took your advise its also in programming and is called 2D image generator and modification using C, updated part 2 (drawing a line) thanks again!!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook