- #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
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