I'm trying to create a horizontal line using a predefined colour between any two points on an image, which is 256 wide by 128 high, which is printed to a .ppm file that is then viewed using XnView. This is created by using a for loop which also contains code to create a vertical line, which works fine. I have been advised to use only one for loop on one of the variables, and then insert the other into the for loop. However, it is only printing out vertical lines and will print only one pixel for the horizontal line. Could anyone offer any tips on how to progress? The code is included below.
Code:
#include <stdio.h>
#include <math.h>
#define I_WIDE 256 // Define row with a value of 256
#define I_HIGH 128 // Define column with a value of 128
#define RANGE 255 // Defines the range that the colour can take
void main (void)
{
unsigned char image [I_WIDE] [I_HIGH] [3]; // Array of Predefined Values as shown above
// Declare variables before the code, Intialise variables to 0
int x = 0, y = 0;
int xpos = 0, ypos = 5, end_xpos = 5, end_ypos = 5;
int r1 = 55, g1 = 210, b1 = 38;
FILE*ppmfile = NULL;
for (y = ypos;y <= end_ypos; y++)
{
x = xpos; xpos < end_xpos; xpos++;
image[x][y][0] = (unsigned char)r1;
image[x][y][1] = (unsigned char)g1;
image[x][y][2] = (unsigned char)b1;
}
ppmfile = fopen("test.ppm","w"); // Opens the file to be written
fprintf(ppmfile, "P3\n"); // Magic Number at the top of the document
fprintf(ppmfile, "#test.ppm\n"); // Name of the document (Commented out)
fprintf(ppmfile, "%d %d\n", I_WIDE, I_HIGH); // Inserts the size of the image into the document
fprintf(ppmfile, "%d\n", RANGE); // number of colour variations in the document
for (y = 0; y < I_HIGH; y++)
{
for (x = 0; x < I_WIDE; x++)
{
fprintf(ppmfile, "%4u %4u %4u \n", image[x][y][0], image[x][y][1], image[x][y][2]); // prints all of the different values
}
}
fclose(ppmfile);
}
Thanks in advance.