Program

This is a discussion on Program within the C Programming forums, part of the General Programming Boards category; I am trying to write a program that makes a ppm file. This gets gradient from one colour to another ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    15

    Question Program

    I am trying to write a program that makes a ppm file. This gets gradient from one colour to another then adds a line of set position and colour
    So far I have this
    Code:
    #include <stdio.h>
    #define RED 0
    #define GREEN 1
    #define BLUE 2
    #define ysize 128
    #define xsize 256
    #define zsize 3
    void main()
    {
    int colour[xsize][ysize][zsize];
    int red[256]; /* Red Array*/
    int green[256]; /* Green Array */
    int blue[256]; /* Blue Array */
    int lred,lgreen,lblue;
    int z = 0, a =0;	//Declaring variables required as integer
    int x1, x2, y1, y2;
    char reply;
    float /*rgrad, ggrad, bgrad,*/ m = 0, grad = 0;	//Declaring gradient variables as floating point numbers
    int red1 = 0; /* Declaring 1st red value */
    int green1 = 0; /* Declaring 1st green value */
    int blue1 = 0; /* Declaring 1st blue value */
    int red2 = 0; /* Declaring 2nd red value*/
    int green2 = 0; /* Declaring 2nd green value*/
    int blue2 = 0; /* Declaring 2nd blue value*/
    int n; /* Number of steps */
    int counter; /* Holds current value of x */
    int x = 0; /* Loop control variable on width */
    int y = 0; /* Loop control variable for the height */
    FILE *pfile = NULL; /* Declares the file pointer */
    
    
    printf ("Input values between 0 and 255\n"); /* */
    do
    {
    printf ("\nInput first value of red: ");
    scanf ("%i", &red1); /* Input of 1st Red Colour Component */
    }
    while ((red1<0) || (red1>255)); /* Checking the 1st red colour is in a valid range */
    do
    {
    printf ("\nInput first value of green: ");
    scanf ("%i", &green1); /* Input of 1st Green Colour Component */
    }
    while ((green1<0) || (green1>255)); /* Checking the 1st green colour is in a valid range */
    do
    {
    printf ("\nInput first value of blue: ");
    scanf ("%i", &blue1); /* Input of 1st Blue Colour Component */
    }
    while ((blue1<0) || (blue1>255)); /* Checking the 1st blue colour is in a valid range */
    do
    {
    printf ("\nInput second value of red: ");
    scanf ("%i", &red2); /* Input of 2nd Red ColourComponent */
    }
    while ((red2<0) || (red2>255)); /* Checking the 2nd blue red is in a valid range */
    do
    {
    printf ("\nInput second value of green: "); /* Input of 2nd Green Colour Component */
    scanf ("%i", &green2);
    }
    while((green2<0) || (green2>255)); /* Checking the 2nd green colour is in a valid range */
    do
    {
    printf ("\nInput second value of blue: ");
    scanf ("%i", &blue2); /* Input of 2nd Blue Colour Component */
    }
    while ((blue2<0) || (blue2>255)); /* Checking the 2nd blue colour is in a valid range */
    
    
    
    	line:
    	printf ("\nEnter Colour Component between 0 & 255 for the line colour:\n");
    	do
    	{
    		printf("Enter value for red colour : ");
    		scanf("%i", &lred);
    	}
    	while (lred<0|| lred>=256);
    	do
    	{
    		printf("Enter value for green colour : ");
    		scanf("%i", &lgreen);
    	}
    	while (lgreen<0 || lgreen>=256);
    	do
    	{
    		printf("Enter value for blue colour : ");
    		scanf("%i", &lblue);	
    	}
    	while (lblue<0 || lblue>=256);
    
    	do
    	{
    		printf("Enter value for X Co-ordinate start of line : ");
    		scanf("%i", &x1);	
    	}
    	while (x1<0 || x1>=256);
    	do
    	{
    		printf("Enter value for Y Co-ordinate start of line : ");
    		scanf("%i", &y1);
    	}
    	while (y1<0 || y1>=128);
    	do
    	{
    		printf("Enter value for X Co-ordinate end of line : ");
    		scanf("%i", &x2);
    	}
    	while (x2<0 || x2>=256);
    	do
    	{
    		printf("Enter value for Y Co-ordinate end of line : ");
    		scanf("%i", &y2);
    	}
    	while (y2<0 || y2>=128);
    
    	grad = (float)(y2-y1)/(x2-x1);
    
    	printf("Gradient: %f \n", grad);
    
    	for(x = x1; x<=x2; x++)
    	{
    	
    		y = (grad*(x-x1)+y1);
    
    		colour[x][y][0] = 0;
    		colour[x][y][1] = 0;
    		colour[x][y][2] = 222;
    	}
    
    printf ("\n\nThe colour gradient has been produced.\nTo view it, open the .ppm file in the current directory with Infranview.\n\n");
    
    pfile = fopen("Gradients.ppm","w"); /* Opens the location of the saved file */
    
    fprintf (pfile, "P3\n #Gradients.ppm\n256 128\n255\n");
    
    x = 0; /* Loop control variable */
    while (x <256)
    {
    counter = (int)x;
    n = counter/255;
    red[x] = (red1+((n)*(red2-red1)));
    green[x] = (green1+((n)*(green2-green1)));
    blue[x] = (blue1+((n)*(blue2-blue1)));
    x = x + 1;
    }
    
    
    
    y=0;
    while (y < 128) /* Loop control for the Height */
    {
    x = 0;
    while (x <256) /* Loop control for the Width */
    {
    fprintf (pfile, "%3.0i %3.0i %3.0i ",red[x], green[x], blue[x]); /* Write the arrays into the file Gradients.ppm */
    x++;
    }
    y++;
    }
    
    fclose(pfile); /* Closes the file */
    printf("\n\nType close and hit enter to exit.\n");
    scanf("%*s"); /* Prevents the command window from closing */
    
    }
    however it is not working for me. Any help will be appreciated. Thank you

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For troubleshooting, remove the scanf()'s, and either assign the values directly through the program for testing, or read the values from a file.

    Testing a program that requires a lot of manual input, is just a PITA, and a time waster. Also, you CAN be a lot more expressive about WHAT your problem is, EXACTLY.

    Being cryptic about the symptoms, will not help your doctor diagnose your disease - same with this program. Tell us what it's doing that's wrong.
    Last edited by Adak; 01-29-2011 at 10:50 PM.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Before I added the line part, it would make a gradient from one user defined colour to the next, after adding the part for the line, the output file is producing either a block of solid colour or a block of half black and a horizontal line where everything above is lines randomly coming down with the occasional coloured vertical line.

    What I am trying to get it all to do is input the 3 component values for RGB then second for the colour it turns into, then put a line over that image from defined points of a user defined colour.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If n is an int (which it is), then n/255 is pretty much always zero (except for the very last line where it is 1).

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jonmartin View Post
    Before I added the line part, it would make a gradient from one user defined colour to the next, after adding the part for the line, the output file is producing either a block of solid colour or a block of half black and a horizontal line where everything above is lines randomly coming down with the occasional coloured vertical line.

    What I am trying to get it all to do is input the 3 component values for RGB then second for the colour it turns into, then put a line over that image from defined points of a user defined colour.
    I have no idea what " ... then second for the colour it turns into, then put a line over that image from defined points of a user defined colour.

    means. I'm quite sure it's English, however.

    Pretend that I know nothing about your type of file or image. Now, show me an example of what you want, with the variable names in your program.

    I need the input from the user, and the output your program should produce, from that input.

    Sorry to be difficult, but your program deals with precise discrete variables, not with a general description. So far, the two ends of this "bridge", are not close enough that I can see from one end of the "bridge", to the other end of this "bridge" we're building.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    I have seperated the code now so I have atleast the gradient being drawn
    Code:
    #include <stdio.h>
    
    void main()
    {
    	float red[256]; /* Red Array*/
    	float green[256]; /* Green Array */
    	float blue[256]; /* Blue Array */
    	float red1 = 0; /* Declaring 1st red value */
    	float green1 = 0; /* Declaring 1st green value */
    	float blue1 = 0; /* Declaring 1st blue value */
    	float red2 = 0; /* Declaring 2nd red value*/
    	float green2 = 0; /* Declaring 2nd green value*/
    	float blue2 = 0; /* Declaring 2nd blue value*/
    	float n; /* Number of steps */
    	float counter; /* Holds current value of x */
    	int x = 0; /* Loop control variable on width */
    	int y = 0; /* Loop control variable for the height */
    	FILE *pfile = NULL; /* Declares the file pointer */
    
    
    	printf ("Input values between 0 and 255\n"); /*  */
    	do
    	{
    		printf ("\nInput first value of red: ");
    		scanf ("%f", &red1); /* Input of 1st Red Colour Component */
    		do
    		{
    			printf ("\nInput first value of green: ");
    			scanf ("%f", &green1); /* Input of 1st Green Colour Component */
    			do
    			{
    				printf ("\nInput first value of blue: ");
    				scanf ("%f", &blue1); /* Input of 1st Blue Colour Component */
    				do
    				{
    					printf ("\nInput second value of red: ");
    					scanf ("%f", &red2); /* Input of 2nd Red ColourComponent */
    					do
    					{
    						printf ("\nInput second value of green: "); /* Input of 2nd Green Colour Component */
    						scanf ("%f", &green2);
    						do
    						{
    							printf ("\nInput second value of blue: ");
    							scanf ("%f", &blue2); /* Input of 2nd Blue Colour Component */
    						}
    						while ((blue2<0) || (blue2>255)); /* Checking the 2nd blue colour is in a valid range */
    					}
    					while((green2<0) || (green2>255)); /* Checking the 2nd green colour is in a valid range */
    				}
    				while ((red2<0) || (red2>255)); /* Checking the 2nd blue red is in a valid range */
    			}
    			while ((blue1<0) || (blue1>255)); /* Checking the 1st blue colour is in a valid range */
    		}
    		while ((green1<0) || (green1>255)); /* Checking the 1st green colour is in a valid range */
    	}
    	while ((red1<0) || (red1>255)); /* Checking the 1st red colour is in a valid range */
    
    	printf ("\n\nThe colour gradient has been produced.\nTo view it, open the .ppm file in the current directory with Infranview.\n\n");
    
    	pfile = fopen("Gradients.ppm","w"); /* Opens the location of the saved file */
    	fprintf (pfile, "P3\n #Gradients.ppm\n256 128\n255\n");
    
    	x = 0; /* Loop control variable */
    	while (x <256)
    	{
    		counter = (int)x;
    		n = counter/255;
    		red[x] = (red1+((n)*(red2-red1)));
    		green[x] = (green1+((n)*(green2-green1)));
    		blue[x] = (blue1+((n)*(blue2-blue1)));
    		x = x + 1;
    	}
    
    
    
    	y=0;
    	while (y < 128) /* Loop control for the Height */
    	{
    		x = 0;
    		while (x <256) /* Loop control for the Width */
    		{
    			fprintf (pfile, "%3.0f %3.0f %3.0f ",red[x], green[x], blue[x]); /* Write the arrays into the file Gradients.ppm */
    			x++;
    		}
    		y++;
    	}
    
    	fclose(pfile); /* Closes the file */
    	printf("\n\nType close and hit enter to exit.\n");
    	scanf("%*s"); /* Prevents the command window from closing */
    
    }
    Now I just have to get the line drawn in. Gradient part is all working fine.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Quote Originally Posted by Adak View Post
    I have no idea what " ... then second for the colour it turns into, then put a line over that image from defined points of a user defined colour.

    means. I'm quite sure it's English, however.

    Pretend that I know nothing about your type of file or image. Now, show me an example of what you want, with the variable names in your program.

    I need the input from the user, and the output your program should produce, from that input.

    Sorry to be difficult, but your program deals with precise discrete variables, not with a general description. So far, the two ends of this "bridge", are not close enough that I can see from one end of the "bridge", to the other end of this "bridge" we're building.
    When I say one colour into another, I mean something like this http://www.techotopia.com/images/3/3...d_gradient.jpg
    I then want to be able to type in 2 coordinates for the start and finish of a line to be drawn. The file format I am writing this in is called PPM which uses the format as shown by this page PPM Format Specification
    The user inputs the component values of RGB for the left hand side then the 2nd value is for the right hand side to merge into (i.e in the previous picture, red on left, blue on right). The output file looks like something similar to that shown on the PPM format description website however I am using much larger arrays so many more numbers.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So your logic is:

    Have a color gradient data file, that will print up a visual onto the screen.

    Looking at the color gradient, the user will select what color he wants on the line.

    Clear the screen, and show the line above the color gradient, OR leave room above the color gradient, for the line, and print it up after getting the user's input.

    Output the line AND the color gradient data, into a PPM file, OR just the line, or what??

    Closer?

    I've made hundreds of thousands of color shades in a program, but nothing using PPM file format.
    Last edited by Adak; 01-30-2011 at 08:53 AM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want to overlay the line, you're going to have to decide which pixels to change color. Line drawing algorithm - Wikipedia, the free encyclopedia

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Ok well I have tried putting the gradient code into multidimensional arrays to make it look a lot neater and simpler however its not working any more. It just gets stuck in a loop when it asks for the first input.
    Code:
    #include <stdio.h>
    
     
    
    int main()
    
    {
          float myArray[256][128][3];
          float r1,r2,g1,g2,b1,b2;
          float r[255],g[255],b[255];
     //     float R,G,B;
          int x,y,z;
    	  int counter;
     //     float co11,co12,co13;
     //     float i1,i2,j1,j2;
    //      float slope,jF;     
          FILE *pfile = NULL;
          for(x=0;x<256;x++);
          {
                for(y=0;y<128;y++);
                {
                      for(z=0;z<3;z++);
                      {
                            myArray[x][y][z]=0;     
                      }
                }
          }
          printf("Input values between 0 and 255\n"); 
          do
          {
                printf("\nInput first value: ");
                scanf("%f,%f,%f", &r1, &g1, &b1); 
          }
          while (r1<0||r1>255||g1<0||g1>255||b1<0||b1>255); 
          do
          {
                printf("\nInput last value: ");
                scanf("%f,%f,%f", &r2, &g2, &b2);
          }
          while (r2<0||r2>255||g2<0||g2>255||b2<0||b2>255);
    
          printf("\n\nThe colour gradient has been produced.\nTo view it, open the .ppm file in the current directory with Infranview.\n\n");
          pfile = fopen("Gradients.ppm","w"); /* Opens the location of the saved file */
          fprintf(pfile, "P3\n #Gradients.ppm\n256 128\n255\n");
          x = 0; /* Loop control variable */
          while (x <256)
          {
                counter = (int)x;
                z = counter/255;
                r[x] = (r1+((z)*(r2-r1)));
                g[x] = (g1+((z)*(g2-g1)));
                b[x] = (b1+((z)*(b2-b1)));
                x = x + 1;
          }
          y=0;
          while (y < 128) /* Loop control for the Height */
          {
                x = 0;
                while (x <256) /* Loop control for the Width */
                {
                      fprintf(pfile, ("%3.0f %3.0f %3.0f "),r[x], g[x], b[x]); /* Write the arrays into the file Gradients.ppm */
                      x++;
                }
                y++;
          }
          fclose(pfile); /* Closes the file */
          scanf("%*s"); /* Prevents the command window from closing */
     
    
    }

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you put commas in your format string, you're going to have to type in commas when you type in the input.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    Ok I have gotten the gradient to work and I am trying to get the line to be plotted on top however it isnt. I want the colour of the line which is defined by the user to overwrite the pixels which have been drawn from the gradient. This is what I have so far
    Code:
    #include <stdio.h>
    #define RED 0
    #define GREEN 1
    #define BLUE 2
    #define xsize 256
    #define ysize 128
    #define zsize 3
    
    void main()
    {
    	int myArray[xsize][ysize][zsize];
    	float r[256],g[256],b[256]; /* Arrays*/
    	float r1,r2,g1,g2,b1,b2,r3,g3,b3; /* Declaring 1st red value */
    	int red,green,blue=0;
    	float counter; /* Holds current value of x */
    	float n; /* Number of steps */	
    	int x,y,z; /* Loop control variable on width */
    	int x1,x2,y1,y2;
    	float gradient = 0;
    
    
    
    	FILE *pfile = NULL; /* Declares the file pointer */
    
    	for (x=0;x<256;x++)
    	{
    		for (y=0;y<128;y++)
    		{
    			for (z=0;z<3;z++)
    			{
    				myArray[x][y][z]=0;
    			}
    		}
    	}
    
    
          printf ("Input values between 0 and 255\n"); 
          do
          {
                printf ("\nInput first value: ");
                scanf ("%f %f %f", &r1, &g1, &b1); 
          }
          while (r1<0||r1>255||g1<0||g1>255||b1<0||b1>255); 
          do
          {
                printf ("\nInput last value: ");
                scanf ("%f %f %f", &r2, &g2, &b2);
          }
    	  while (r2<0||r2>255||g2<0||g2>255||b2<0||b2>255); 
    
    line:
    	  do
    	  {
    		  printf("\nInput colour components of line: ");
    		  scanf("%f %f %f", &r3, &g3, &b3);
    	  }
    	  while (r3<0||r3>255||g3<0||g3>255||b3<0||b3>255); 
    	  do
    	  {
    		  printf("\nInput first x co-ordinate then last e.g 20 140: ");
    		  scanf("%i %i", &x1, &x2);
    	  }
    	  while (x1<0||x1>255||x2<0||x2>255); 
    	  do
    	  {
    		  printf("\nInput first y co-ordinate then last e.g 20 98: ");
    		  scanf("%i %i", &y1, &y2);
    	  }
    	  while (y1<0||y1>255||y2<0||y2>255); 
    	  
    	  gradient = (float)(y2-y1)/(float)(x2-x1);
    	  printf("\nGradient: %f", gradient);
    
    
    
    
    
    
    	printf ("\n\nThe colour gradient has been produced.\nTo view it, open the .ppm file in the current directory with Infranview.\n\n");
    
    	pfile = fopen("Gradients.ppm","w"); /* Opens the location of the saved file */
    	fprintf (pfile, "P3\n #Gradients.ppm\n256 128\n255\n");
    
          x = 0; /* Loop control variable */
          while (x <256)
          {
                counter = (int)x;
                n = counter/255;
                r[x] = (r1+((n)*(r2-r1)));
                g[x] = (g1+((n)*(g2-g1)));
                b[x] = (b1+((n)*(b2-b1)));
                x = x + 1;
          }
    
    
    
    	for(x = x1; x<=x2; x++)
    	{
    	
    		y = (gradient*(x-x1)+y1);
    
    		myArray[x][y][0] = 0;
    		myArray[x][y][1] = 0;
    		myArray[x][y][2] = 222;
    	}
    
    
          y=0;
          while (y < 128) /* Loop control for the Height */
          {
                x = 0;
                while (x <256) /* Loop control for the Width */
                {
                      fprintf(pfile, ("%3.0f %3.0f %3.0f "),r[x], g[x], b[x]); /* Write the arrays into the file Gradients.ppm */
                      x++;
                }
                y++;
          }
    
    
    
    
    	//  y=0;
       //   while (y < 128) /* Loop control for the Height */
       //   {
       //         x = 0;
       //         while (x <256) /* Loop control for the Width */
       //         {
        //             fprintf(pfile,"%i ",myArray[x][y][RED]); 
    	//			 fprintf(pfile,"%i ",myArray[x][y][GREEN]); 
    	//			 fprintf(pfile,"%i ",myArray[x][y][BLUE]); 
         //             x++;
         //       }
         //       y++;
      //    }
    
    
    
    	fclose(pfile); /* Closes the file */
    	printf("\n\nType close and hit enter to exit.\n");
    	scanf("%*s"); /* Prevents the command window from closing */
    
    }

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand the "line to be plotted" bit.

    You have the array for the gradient. Now you want to put some different values into the top of the array for the gradient.

    Then you want to reprint that part (only), of the gradient array, so the line is displayed.

    You know the height of the line you want, you know the width of the line you want, and you know the values for the array gradient that you want to put into the array.

    And of course, you know how to print the array gradient, although here, you only need to reprint a small part of the array gradient (which part has the line data now in it).

    What are you stuck on? Seems like you have everything you need.

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    15
    I define the components of the background gradient. Then I define the colour and position of the line. I cant get the line to be plotted ontop of the gradient. I want something that ends up looking like this
    http://img97.imageshack.us/img97/3408/colourp.jpg

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't you want to overwrite the gradient's value, in the specific parts of the gradient's array where the line needs to be drawn?

    Do you need the background gradient to show through the line itself?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21