标题:
请教各位关于程序改写的问题
[打印本页]
作者:
cfanfan
时间:
2006-1-27 04:13
标题:
请教各位关于程序改写的问题
请教各位朋友,我这里的程序想改写,main函数中的东西想通过class方法实现,可是我费劲想了很久还是没有好的思路,所以请教大家帮助,感谢!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
/*** New Data Types ***/
typedef unsigned char BYTE;
int main(int argc, char* argv[])
/**************************************************************************************************
Arguments:
argv[1]: input file name
argv[2]: output file name
**************************************************************************************************/
{
ifstream infile; /* input file */
ofstream outfile_R;
ofstream outfile_G; /* output files */
ofstream outfile_B;
char inFileName[128]; /* input file name */
char outFileName_R[128];
char outFileName_G[128]; /* output file name */
char outFileName_B[128];
int width; /* width of input source */
int height; /* height of input source */
int Npixels, maxValue, i;
BYTE *pRGB;
BYTE *pR, *pG, *pB;
char buf[16];
int bufIndex;
// read the arguments
if(argc == 5)
{
strcpy(inFileName, argv[1]);
strcpy(outFileName_R, argv[2]);
strcpy(outFileName_G, argv[3]);
strcpy(outFileName_B, argv[4]);
}
else
{
cout<<"
PM(p6) converts to PGM(p5)
";
cout<<"input Filename: ";
cin>>inFileName;
cout<<"Output Filename_R: ";
cin>>outFileName_R;
cout<<"Output Filename_G: ";
cin>>outFileName_G;
cout<<"Output Filename_B: ";
cin>>outFileName_B;
}
/* open input/output file */
infile.open(inFileName, ios::binary);
outfile_R.open(outFileName_R, ios::binary);
outfile_G.open(outFileName_G, ios::binary);
outfile_B.open(outFileName_B, ios::binary);
/* check input file */
if(!infile)
{
cout<<"Cannot open input file "<< inFileName<<endl;
return 1;
}
infile.read(buf, 2); // get the magic number
buf[2]='';
if(buf[0] == 'P' && buf[1] == '6'){
infile.read(buf, 1);
while(isspace(buf[0])){ // Skip white space(s)
infile.read(buf,1);
}
if(buf[0] == '#') { // Skip the comment line
while(buf[0] != '
'){ // search end of line
infile.read(buf,1);
}
while(isspace(buf[0])){ // Skip white space(s)
infile.read(buf,1);
}
}
// get width
bufIndex = 0;
while(bufIndex < 15 && !isspace(buf[bufIndex])){
bufIndex++;
infile.read(buf+bufIndex, 1);
}
buf[bufIndex] = NULL; // null terminated string
width = atoi(buf);
// get height
infile.read(buf,1);
while(isspace(buf[0])){ // Skip white space(s)
infile.read(buf,1);
}
bufIndex = 0;
while(bufIndex < 15 && !isspace(buf[bufIndex])){
bufIndex++;
infile.read(buf+bufIndex, 1);
}
buf[bufIndex] = NULL; // null terminated string
height = atoi(buf);
// get max value
infile.read(buf,1);
while(isspace(buf[0])){ // Skip white space(s)
infile.read(buf,1);
}
bufIndex = 0;
while(bufIndex < 15 && !isspace(buf[bufIndex])){
bufIndex++;
infile.read(buf+bufIndex, 1);
}
buf[bufIndex] = NULL; // null terminated string
maxValue = atoi(buf);
if (maxValue == 255) { // 255 is the only type this program reads
Npixels = width * height;
pRGB = new BYTE [3*Npixels];
pR = new BYTE [Npixels];
pG = new BYTE [Npixels];
pB = new BYTE [Npixels];
infile.read(reinterpret_cast<char *>(pRGB), 3*Npixels);
outfile_R << "
5" << endl;
outfile_R << width << " " << height << endl;
outfile_R << maxValue << endl;
outfile_G << "
5" << endl;
outfile_G << width << " " << height << endl;
outfile_G << maxValue << endl;
outfile_B << "P5" << endl;
outfile_B << width << " " << height << endl;
outfile_B << maxValue << endl;
for (i=0; i < Npixels; i++){
pR
= pRGB[i*3];
pG
= pRGB[i*3+1];
pB
= pRGB[i*3+2];
}
outfile_R.write(reinterpret_cast<char *>(pR), Npixels);
outfile_G.write(reinterpret_cast<char *>(pG), Npixels);
outfile_B.write(reinterpret_cast<char *>(pB), Npixels);
}
else {
cout << "Error: wrong MaxGray value -- 255
";
width = height = 0;
}
}
else {
cout << "Error: wrong MagicValue -- P6
";
width = height = 0;
}
/* Delete the pointers */
delete [] pRGB;
delete [] pR;
delete [] pG;
delete [] pB;
/* Close file */
outfile_R.close();
outfile_G.close();
outfile_B.close();
infile.close();
return 0;
}
欢迎光临 编程开发论坛 (http://bbs.lihuasoft.net/)
Powered by Discuz! 6.0.0