Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Generating transition between two colours but an error occurs

$
0
0
Hi, basically I have this code and it builds and I type in my values, but when I try and open it on irfanview it says 'invalid or unsupported PNM file' not quite sure why it happens - is there something wrong with my code

[
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define H 128
#define W 256
#define R 0
#define G 1
#define B 2

int main(void)

{
    FILE*pfile = NULL;
	int image[H][W][3];
	int x,y;
        float r1,r2;
	float g1,g2;
	float b1,b2;
	float CS;
	float CC;
	
	for(y=0; y<H; y++)
		for (x=0; x<W; x++)
		{
			image[y][x][R]=0;
			image[y][x][G]=0;
			image[y][x][B]=0;
		}

	printf("Enter First Colour Components\n\n"); 
	do
	{
		printf("Enter First Red Value: \n");
		scanf("%f", &r1);
	}
	while((r1<0)&&(r1>255));
	do
	{
		printf("Enter First Green Value: \n");
		scanf("%f", &g1);
	}
	while((g1<0)&&(g1>255));
	do
	{
		printf("Enter First Blue Value: \n");
		scanf("%f", &b1);
	}
	while((b1<0)&&(b1>255));

	if((r1<255)&&(g1<255)&&(b1<255))
		{
			printf("In Range\n");
	    }
	else
	    {
		printf("Out of Range. Try Again\n");
	    }

	printf("\nEnter Second Colour Components\n\n");
	do
	{
		printf("Enter Second Red Value: \n");
		scanf("%f", &r2);
	}
	while((r2<0)&&(r2>255));
	do
	{
		printf("Enter Second Green Value: \n");
		scanf("%f", &g2);
	}
	while((g2<0)&&(g2>255));
	do
	{
		printf("Enter Second Blue Value:\n");
		scanf("%f", &b2);
	}
	while((b2<0)&&(b2>255));

	if ((r2<255)&&(g2<255)&&(b2<255))
		{
			printf("In Range\n");
	    }
	else
	    {
			printf("Out of Range. Try Again\n");
	    }

	for (y=0; y<H; y++)
		for (x=0; x<W; x++)
		{
			CC=(float)x;
			CS=(float)x/255;
			image[y][x][R]=(r1+((CS)*(r2-r1)));
			image[y][x][G]=(g1+((CS)*(g2-g1)));
			image[y][x][B]=(b1+((CS)*(b2-b1)));
		}

		pfile=fopen("image2.ppm","w");
		fprintf(pfile,"P3\n");
		fprintf(pfile,"#smooth transition\n");
		fprintf(pfile,"%d %d", H,W);
		fprintf(pfile,"255\n");
		for (y=0; y<H; y++)
			 for (x=0; x<W; x++)
			 {
				 fprintf(pfile,"%d %d %d\n", image[y][x][R], image[y][x][G], image[y][x][B]);
				 fprintf(pfile,"%d %d %d\n", image[y][x][R], image[y][x][G], image[y][x][B]);
				 fprintf(pfile,"%d %d %d\n", image[y][x][R], image[y][x][G], image[y][x][B]);
			 }
		fclose(pfile);
                scanf("%*s");
                return 0;
}
]

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>