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

In summary, the conversation discusses a project involving writing C code to create an image file in .ppm format. The code generates a smooth gradient between dark and light red, and the author plans to add user input for color values and a line-drawing feature. They are seeking help with implementing user input and range checking, as well as finding resources for line-drawing algorithms.
  • #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!
 
Technology news on Phys.org
  • #2
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:
  • #3


Hello,

Thank you for sharing your project with me. It seems like you have made good progress so far in generating your 2D image using C. I can offer some suggestions and advice for the next steps in your project.

For your first goal, which is to allow the user to supply the colors as 2 sets of 3 values (red, green, and blue) between 0 and 255, you can use the scanf() function to take in user input. For example, you can prompt the user to enter the red, green, and blue values separately and use scanf() to assign those values to variables. Then, you can use if statements or other methods to range check the values and reject them if they are outside the range of 0 to 255.

For your second goal, which is to draw a line in a given color between any two points in the 256 wide by 128 high image, you will need to use a mathematical algorithm to calculate the coordinates of the line and then use a loop to assign the given color to each point on the line. There are many resources available online that can guide you through this process.

Additionally, you can use a while loop to continuously ask the user if they want to input another line after each line is drawn. This will allow the user to draw multiple lines and create a more complex image.

I hope this helps and gives you some direction for your project. Good luck!
 

1. How does the 2D image generator work?

The 2D image generator uses C programming language to create lines and other shapes on a two-dimensional plane. It works by manipulating pixels on a canvas to form the desired image.

2. Can I modify the existing code to draw other shapes?

Yes, the code can be modified to draw various shapes like circles, triangles, and squares by using different mathematical equations for plotting the points.

3. Is it possible to change the color of the lines?

Yes, the color of the lines can be changed by modifying the code to use different RGB values for the pixels. This allows for a wide range of color options.

4. How can I make the lines thicker or thinner?

The thickness of the lines can be adjusted by changing the number of pixels that are plotted for each line. A higher number of pixels will result in a thicker line, while a lower number will create a thinner line.

5. Is there a limit to the size of the image that can be generated?

The size of the image that can be generated is dependent on the memory of the computer or device running the code. However, larger images may take longer to generate and may require more memory to store.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
9
Views
3K
Back
Top