- #1
1alph1
- 7
- 0
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
#include<stdio.h>
#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 */
}
//scanf("%*s");
}
fclose(ppmfile);
}
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!
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
#include<stdio.h>
#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 */
}
//scanf("%*s");
}
fclose(ppmfile);
}
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!