#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void main()
{


	//decleration
	ifstream source;
	ofstream target;
	char * box;
	int width, height, numlevel;
	string s;
	int amount= 100;

	//--------------------

	source.open("1.ppm",ios::binary);
	while(source.fail())
	{
		cout<<"can not open the file"<<endl;    // to make sure the file is opened
	}

	source>> s;
	if ( s != "P6" )
	cout<<" the file is invailid " <<endl;

	source>> width>>height >>numlevel;

	source.get();

	// start reading into the array

	box= new char[width*height*3];
	source.read(box,width*height*3);
	source.close();

	for ( int i=0; i<height; i++ )
		for( int j=0; j<width; j++ )
	{
		unsigned char red= box[i*width*3 + j*3 ]; 
		unsigned char green= box[i*width*3 + j*3 + 1];
		unsigned char blue= box[i*width*3 + j*3 + 2];

		if ( (red + amount > 255) )
			red = 255;
		else if( red + amount < 0 )
		red = 0;
		else red+=amount;

		if ( (green + amount > 255) )
			green = 255;
		else if( green + amount < 0 )
		green = 0;
		else green+=amount;

		if ( (blue + amount > 255) )
			blue = 255;
		else if( blue + amount < 0 )
		blue = 0;
		else blue+=amount;

		box[i*width*3 + j*3] = red ;
	    box[i*width*3 + j*3 + 1] = green;
	    box[i*width*3 + j*3 + 2] = blue;

		//---------------------
		//writing to new file

		target.open("new.ppm");

		target << s << width<<height<< numlevel;
		
		for( int k=0; k<width*height*3; k++)
		target<<box[k];

		
		while(target.fail() )
			cout<<"can't write"<<endl;
		target.close();







}

cout<<" hello"<<endl;





}

Recommended Answers

All 3 Replies

I have a problem with this code. The program never does the goal. it never reaches the "cout<<"" line thats line 92 i waited like 5 minutes but nothing happened. moreover i think the code which writes to ppm file is missing somthing. but i don't know what. may be spaces between s height, width and numlevel.

pleas anyone give me a help

Its kind of hard to help because I don't know what your input files look like but I noticed two things. Lines 21 and 80 are

while (source.fail())

Those should be if statements because it will continuously loop if the file open process fails. Also, have you put a couple 'cout' statements for height and width to make sure they are correct? If they are not, your for loops will not act the way you want.

well, thank u for helping

the ppm file are simple and easy to process.
the patern look like this
P6 100 100 897 977%98$238FD@$....
the P6 indicates that the file is ppm. the first number i.e 100 in this case stands for the width and the second for the height while the third is called num level ( i no nothing about it ) after that is the pixels. the appear each in a sequence of three numbers. eg 255 255 255. these nums are one pixel and they are red green blue.

the qustion is that when i rewrite the brightend image should i consider writing spaces between the height and the width P6 ..etc in order for the image viewer to read them or not. thats why i feel my code is wrong. i mean the one for writing the ppm file
thank u in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.