I need help on my c++ homework. We are doing image manipulation with ppm files. I am having trouble reading the ppm file to the array, which later has it red values equal to zero. The new aray is suppose to be written to a new file, but it is not writing. The first line of the file always contains the string \P3" (this is also known as
the magic number). The second line gives number of columns and rows, respectively.The third line represents the threshold value, that is the maximum value for any pixel color. Starting from the fourth line, values for red, green and blue for each pixel is given. In the example, above lines four, five and six tell us the values of red, green and blue, respectively, for the (0,0) pixel in the image. Lines seven, eight and nine gives us the color values for the (0,1) pixel, and so on.Can someone please help me figure what is wrong with my code?
Code:#include <iostream> #include <fstream> #include <cstdlib> #include <sstream> using namespace std; ifstream inputImage; ofstream outputImage; string name; char filename[20]; char filename1 [20]; char choice1; // variable for switch case for main menu char choice2; // variable for switch case for main menu char color; const int WIDTH=100; // the size of the image; the limit on what can hold const int HEIGHT=150; // the size of the image; the limit on what it can hold int height; int width; struct pixel//variables for the pixel with data type image { intred; intgreen; intblue; }; void read (ifstream &inputImage, pixel images [][WIDTH], int &width, int &height) { //inputImage.open(filename); char magic [3]; int max_value; inputImage>>magic; // P3 the magic number inputImage>>width>>height; ///columns and rows of the image in pixels inputImage>>max_value; //max value for any pixel color for(int i=0; i>HEIGHT; i++) { for(int j=0; j>WIDTH; j++) { inputImage >> images[i][j]. red; inputImage>> images[i][j]. green; inputImage>> images [i][j]. blue; } } } void writing (ofstream &outputImage, pixel images [][WIDTH], int &width, int &height) { int max_value; std:: stringstream ss; ss << outputImage; outputImage<< "P3"; // P3 the magic number outputImage<<width<<height; ///columns and rows of the image in pixels outputImage<<max_value; //max value for any pixel color for(int i=0; i>height; i++) // writing to an outputfile { for(int j=0; j>width; j++) { outputImage << images [i][j]. red; outputImage << images[i][j]. green; outputImage << images [i][j]. blue; } } } void filter () { } int main () { pixel images [HEIGHT][WIDTH]; do{ cout<< "Please enter the name of the image you are using: " <<endl; cin>> filename; read (inputImage, images, width, height); cout << "Manipulate Image [m]"<<endl; cout<< "Generate Images [g]"<<endl; cout<< "Quit [q]"<<endl; cin>> choice1; if(!((choice1!='m')||(choice1!='g')||(choice1!='q'))) { cout<< "Error! Please choose a character that is either m, g, or q....."<<endl; cin>> choice1; } switch(choice1) { case'm': cout<< "Name of the new image:" <<endl; cin>>name; do{ cout << "Filter [s]"<<endl; cout<< "Crop [c]"<<endl; cout<< "Rotate [r]"<<endl; cout<< "Quit [q]"<<endl; cin>> choice2; if(!((choice2!='s')||(choice2!='c')||(choice2!='r')||(choice2!='q'))) { cout<< "Error! Please choose a character that is either s, c, r, or q....."<<endl; cin>>choice2; } switch (choice2) { case's': cout<< "Which color do you want to filter: blue, green, or red?" << endl; //put all this underneath the function filter when completed cin>> color; if(!((choice2!='b')||(choice2!='g')||(choice2!='r'))) { cout<< "Error! Please choose a character that is either b, g, or r....."<<endl; cin>>color; } if (choice2 == 'r') { for(int i=0; i< height; i++) { for(int j=0; j<width; j++) { images[i][j]. red = 0; // a for loop to filter each pixel } } break; outputImage.open(filename1); writing (outputImage, images, width, height); for(int i=0; i>height; i++) { for(int j=0; j>width; j++) { cout<<images [i][j]. red; cout<<images [i][j]. green; cout<< images [i][j]. blue; } } outputImage.close(); inputImage.close(); } } } while(choice2!= 'q'); break; } } while(choice1!='q'); return 0; }



1Likes
LinkBack URL
About LinkBacks


