2D image generator using C and nxview

In summary, the conversation discusses using arrays, loops, and writing files in C to create an image file in .ppm format. The goal is to create a large image of 256 x 128 pixels, but there are some difficulties in using C for this function. The conversation also includes a code attempt and suggestions for creating a smooth gradient between two colors in the image.
  • #1
1alph1
7
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
  • #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.
 
  • #3
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!
 

FAQ: 2D image generator using C and nxview

How does the 2D image generator work?

The 2D image generator works by utilizing the programming language C and the nxview library. The C code is used to generate the image data, while the nxview library is used to display the image on a 2D plane. The C code can be modified to create different types of images, and the nxview library provides various functions for manipulating the image.

What are the advantages of using C for the 2D image generator?

C is a powerful and versatile programming language that is commonly used for image processing. It allows for efficient manipulation of image data and provides a wide range of functions and libraries for creating complex images. Additionally, C is a high-level language, meaning that it can easily be translated into machine code for faster execution.

Can the image generator create 3D images?

No, the 2D image generator using C and nxview is specifically designed for creating 2D images. However, with some modifications to the code and the use of additional libraries, it is possible to create 3D images using C.

Is the nxview library difficult to use?

The nxview library is relatively easy to use, especially for those familiar with C programming. It provides a simple interface for displaying images on a 2D plane, and the documentation is comprehensive and easy to understand. However, some programming knowledge is required to effectively utilize the library.

Can I use the 2D image generator for commercial purposes?

Yes, you can use the 2D image generator for commercial purposes. However, you must ensure that you have the necessary licenses and permissions for any libraries or code used in your image generator. Additionally, you may need to credit the creators of the nxview library and any other resources used in your project.

Similar threads

Back
Top