2D image generator and modification using C, updated part 2 (drawing a line)

AI Thread Summary
The discussion revolves around a project to create a .ppm image file using C code, specifically generating a gradient from dark red to light red in a 256 x 128 pixel format. The user seeks assistance in enhancing the code to allow for user input of color values (red, green, blue) with proper range checking to ensure values are between 0 and 255. Suggestions include using `scanf` for input and implementing a loop for validation. Additionally, the user wants to add functionality for drawing lines in specified colors between two points, with user-defined coordinates. Recommendations include researching line-drawing algorithms and considering graphics programming resources for deeper understanding.
1alph1
Messages
5
Reaction score
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!
 
Technology news on Phys.org
I see that you have the scanf line commented out. You can look at a reference for scanf such as http://www.cplusplus.com/reference/clibrary/cstdio/scanf.html". There's more than one way you could do this. You could use the %u, %x or %s format specifiers. If you use %s, you will have to use something like atoi to convert it to a number. After you retrieve the user's value, you have to do range checking. You could just do a do/while loop for this - just terminate the loop when the values are valid. Something like this would work:

do {
printf("Enter the red component's value:");
scanf(%u, &red);
} while (red <= 255);

You can just Google line-drawing algorithms. One of the first pages that pops up is http://www.cs.unc.edu/~mcmillan/comp136/Lecture6/Lines.html". If you are going to delve much deeper into graphics code, you probably want to check out some books specifically on graphics programming.
 
Last edited by a moderator:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
2
Views
4K
Replies
17
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
9
Views
3K
Replies
75
Views
6K
Back
Top