2014-01-29 146 views
0
#include <iostream> 
#include <fstream> 
#include <iomanip> 
using namespace std; 

//Function Prototypes *************************** 
bool openFile(fstream &inFile); 
void validateFileData(fstream &inFile, int& numRows, int& numCols); 
double getGrayValue(fstream &inFile, int numRows, int numCols); 

/************************************************ 
***************************** M A I N ********** 
************************************************/ 
int main() { 
//Var Dictionary **************************** 
int numRows, numCols; //size of our ppm image 
bool isGoodFile;  //tells if file opened OK 
double grayValue;  //grayscale: avg of all pixels in img 
fstream inFile;   //file holding ppm image 

//1.0 Init 
isGoodFile = openFile(inFile); 

//2.0 Process File ************************** 
if (isGoodFile) {//we have a file to process 
    validateFileData(inFile, numRows, numCols); 

    if ((numRows>0) && (numCols>0)) {//we have proper PPM file 
     grayValue = getGrayValue(inFile, numRows, numCols); 
     cout << "\nThe average value for this image is: " << grayValue << endl; 
    } else //file did not match PPM spec's 
     cout<<"The file was not in proper ASCII PPM format\n\n"; 

}//isGoodFile 
else //file failed to open properly 
    cout<<"File failed to open correctly\n"; 

//3.0 Finish up ***************************** 
inFile.close(); //must always CLOSE all files we open 
cout<<"\n\n\n\t<<< Normal Termination >>>\n\n"; 
    return 0; 
    } 

/************************************************ 
***************************** openFile ********** 
************************************************/ 
bool openFile (fstream &inFile) { 
//Receives fstream input file 
//Prompts user for path/name & attempts to open file 
//Returns T/F if open succeeded or not 

//var dictionary 
char fileName[80]; //path & name of PPM file 

//1.0) Prompt user & get filename 
cout<<"\nPlease enter the name of your PPM file: "; 
cin.getline(fileName,80); 
cout<<"\nFile name is: "<< fileName << endl; //data echo 

//2.0) Attempt to open file 
inFile.open(fileName); 

return (inFile.good()); //true if file OK 
}//openFile 

/************************************************ 
***************************** validateFileData ** 
************************************************/ 
void validateFileData(fstream &inFile, int& numRows, int& numCols){ 
//Receives fstream input file (already opened) 
//Processes (and verifies) file is ASCII PPM format 
//Returns with numCols (width) and numRows (height) of image 
// and the file pointer at the first pixel of file 
// If file is not in PPM format, will print errMsg 
// and return -1 for numRows/numCols. 

//var dictionary 
int width, height, maxColor; //PPM characteristics 
bool isGoodPPM = true;   //flag for our tests 
char myLine[80];    //lines of PPM file (text) 

//1.0) Test 1st line: "P3" 
cout<<"\nData echo of file input:\n"; 
inFile.getline(myLine, 80); //grab whole 1st line 
cout<<myLine<<endl;   //echo it back to human 

if ((myLine[0]!='P') || (myLine[1]!='3')) //test it 
    isGoodPPM = false; //we have bad file 

//2.0 Test 2nd line (if needed): "# any old comment" 
if (isGoodPPM) {//keep testing ONLY if file still alive 
    inFile.getline(myLine,80); //grab whole 2nd line 
    cout<<myLine<<endl;   //echo it back to human 

    if (myLine[0]!='#') //test for comment 
     isGoodPPM = false; //bad file 
} 

//3.0 Test 3rd & 4th lines: "<width> <height> <255> 
if (isGoodPPM) { 
    inFile >> width >> height >> maxColor; //grab 3 integers 
    cout << width << " " << height << endl //echo back 
     << maxColor << endl; 

    //test our numbers 
    if ((width<=0) || (height<=0) || (maxColor!=255)) 
     isGoodPPM = false; //bad file 
} 

//4.0 Return results 
if (isGoodPPM) { //file passed inspection 
    numCols = width; numRows = height; 
} else { 
    numCols = numRows = -1; //bad file 
} 

}//validateFileData 

/************************************************ 
***************************** getAvgPixel ******* 
************************************************/ 
double getGrayValue(fstream &inFile, int numRows, int numCols){ 
//Receives fstream input file, with pointer at first pixel value 
//   and the numRows (height) and numCols (width) of image 
//Reads all pixels (RGB Values) 
//Returns the average (double) of all the pixels (grayScale value) 

double average = 0.0; //variable to return the average 
const int MAXROW = numRows;//variable for max amount of rows, added one so i could use it in a loop better 
const int MAXCOL = numCols;//variable for max amount of rows, added one so i could use it in a loop better 
char myLine[80];//set up array to store the values in 
int temp=0;//variable to temporarily store numbers in 
int adding=0;//variable to store the added numbers in 
int total=0;//variable to store the total amount of numbers in 
int column=0;//variable to store column position 
int row=0;//variable to store column position 
const int MYLINEMAX =81; 

inFile.getline(myLine, 80); 
while((column<MAXCOL) && (row<MAXROW)){//set up a loop for the row 
    for(int i=0;i<MYLINEMAX;i++){ 
     temp=adding; 
     adding=temp+myLine[i]; 
     total++; 
    } 
    average=adding/total; 
} 




return average; 
}//getGrayValue 

IM如何做的最後一部分數學極其混亂,檢查工作正常,但我似乎無法做數學題,即使我啓動'average'as 0在我的命令提示符中沒有打印出任何內容獲取灰度顏色的平均從ASCII圖像

回答

0

getGrayFile()永不改變列或行,所以while循環永遠不會終止。