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

Click For Summary
SUMMARY

This discussion focuses on creating a 2D image generator in C that outputs a .ppm file, specifically generating a gradient from dark red to light red. The user seeks guidance on implementing user input for RGB color values with range checking and drawing lines between specified coordinates. Key functions include file handling with fopen and fprintf, as well as user input management using scanf. The discussion also suggests researching line-drawing algorithms for further development.

PREREQUISITES
  • Understanding of C programming, particularly file I/O operations
  • Familiarity with the PPM image format specifications
  • Knowledge of user input handling in C using scanf
  • Basic concepts of color representation in RGB format
NEXT STEPS
  • Implement user input for RGB values with range checking in C
  • Research line-drawing algorithms, such as Bresenham's line algorithm
  • Explore advanced graphics programming techniques in C
  • Learn about error handling in file operations in C
USEFUL FOR

This discussion is beneficial for C programmers, graphics developers, and anyone interested in image processing and manipulation using programming techniques.

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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K