2D image generator using C and nxview

Click For Summary
SUMMARY

The discussion focuses on creating a 2D image generator using C programming and the PPM file format, specifically for viewing with the nxview application. The user is attempting to generate a 256x128 image using arrays and loops but encounters issues with file writing and image data initialization. Key points include the importance of correctly formatting the PPM header, using unsigned char for pixel values, and ensuring proper spacing in the output file to comply with PPM specifications. Suggestions for implementing a color gradient through linear interpolation between RGB components were also provided.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with PPM file format specifications
  • Knowledge of arrays and loops in programming
  • Basic concepts of color representation in RGB format
NEXT STEPS
  • Implement proper PPM header writing in C for image files
  • Learn about using unsigned char data types for pixel values in C
  • Research linear interpolation techniques for color gradients
  • Explore file I/O operations in C for writing image data
USEFUL FOR

This discussion is beneficial for C programmers, graphic developers, and anyone interested in image processing and file generation using programming languages.

1alph1
Messages
5
Reaction score
0
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

P3
# a 3x3 RGB image.ppm
3 3
100
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 isn't 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;
}
//printf("*\n");
//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]);
}
printf("\n");
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);

fclose(ppmfile);*/
}

I also want to generate a smooth transtiton/graident between two colours across the image.
any help tips of info would be great, thanks
 
Technology news on Phys.org
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.
 
Vampyre747 said:
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.

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!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K