测试pixel类,平滑处理有关ppm文件
/*--------------------------------------------------------------*/
/*读取一个ASCII数字图像并对其进行平滑处理,保存到另个文件中。图 */
/*像文件的格式为PPM,可以使用pic2pic pro这个软件实现转化,文件头*/
/*包括P3,图像的宽度和高度,颜色的最大值,如:
P3
176 220
255
148 164 163 143 156 156 170 181 181 235 236 236 这一个行为RGB值
如第一个像素的RGB值为(148 164 163),平滑处理为每一个像素用和周
围四个相邻像素(上下左右)总和的平均值来代替这个像素的值。
*/
#include "pixel.h"
#include <string>
#include <vector>
#include <fstream>
using namespace std;
/*
//测试pixel的主函数
int main(){
pixel p1, p2(100,200,100), p3(10,20,30);
// p1 = p2 + p3; //valid.两个运算数都是pixel的对象
// p1 = p2 + 100; //valid.100调用具有整数变量的构造函数,
// 把100变为(100,100,100)的pixel对象
// p1 = 100 + p2; //Invalid!第一个运算数是一个整数而不是pixel对象
//调用友元函数+
p1 = 100 + p2;
cout <<"P1: "<< p1 <<"P2: " << p2 << endl;
return 0;
}*/
void read_header(istream& fin,ostream& fout,
int& width,int& height,int& max);
void smooth(vector<vector<pixel> >&image,int w,int h);
int main(){
int height,width,max,i,j;
string filename;
ifstream fin;
ofstream fout;
cout<<"Enter name of input file ";
cin >>filename;
fin.open(filename.c_str());
if (fin.fail()) {
cerr << "Error opening input file\n";
}
else{
//Open new file for output.
filename = "smoothed_"+filename;
fout.open(filename.c_str());
//Read and write header information.
read_header(fin,fout,width,height,max);
// cout << "width: " <<width <<"height: "<<height <<"max: "<< max <<endl;
//Declare image array.
vector<vector<pixel> >image(height, vector<pixel>(width));
//Read the image
for(i=0;i<height;i++)
for(j=0;j<width;j++){
fin>>image[i][j];
}
//Smooth the image.
smooth(image,width,height);
//Write modified image to new file.
for(i=0;i<height;i++)
for(j=0;j<width;j++){
fout<<image[i][j]<<' ';
/*
if ((j+1)%5==0) {
fout <<endl;
}*/
}
}//end of else
return 0;
}
//读取ppm文件头函数
void read_header(istream& fin,ostream& fout,int& width,
int& height,int& max){
char header[100];
char ch;
fin.getline(header,100);
fout<<header<<endl;
//get all comment lines and write to new file.
fin >> ch;
while (ch=='#') {
fin.getline(header,100);
fout<<ch<<header<<endl;
fin>> ch;
}
fin.putback(ch);//putback是将字符放回到输入流中
//input width and height of image.
fin >> width >> height;
fout << width <<' ' <<height<<endl;
//Input maximum color value.
fin>> max;
fout <<max <<endl;
return ;//关闭流
}
void smooth(vector<vector<pixel> >&image,int w,int h){
for(int i = 1;i<h-1;i++)
for(int j=1;j<w-1;j++){
image[i][j]=(image[i][j]+image[i-1][j]+image
[i+1][j]+image[i][j-1]+image[i][j+1])/5;
}
}
/*读取一个ASCII数字图像并对其进行平滑处理,保存到另个文件中。图 */
/*像文件的格式为PPM,可以使用pic2pic pro这个软件实现转化,文件头*/
/*包括P3,图像的宽度和高度,颜色的最大值,如:
P3
176 220
255
148 164 163 143 156 156 170 181 181 235 236 236 这一个行为RGB值
如第一个像素的RGB值为(148 164 163),平滑处理为每一个像素用和周
围四个相邻像素(上下左右)总和的平均值来代替这个像素的值。
*/
#include "pixel.h"
#include <string>
#include <vector>
#include <fstream>
using namespace std;
/*
//测试pixel的主函数
int main(){
pixel p1, p2(100,200,100), p3(10,20,30);
// p1 = p2 + p3; //valid.两个运算数都是pixel的对象
// p1 = p2 + 100; //valid.100调用具有整数变量的构造函数,
// 把100变为(100,100,100)的pixel对象
// p1 = 100 + p2; //Invalid!第一个运算数是一个整数而不是pixel对象
//调用友元函数+
p1 = 100 + p2;
cout <<"P1: "<< p1 <<"P2: " << p2 << endl;
return 0;
}*/
void read_header(istream& fin,ostream& fout,
int& width,int& height,int& max);
void smooth(vector<vector<pixel> >&image,int w,int h);
int main(){
int height,width,max,i,j;
string filename;
ifstream fin;
ofstream fout;
cout<<"Enter name of input file ";
cin >>filename;
fin.open(filename.c_str());
if (fin.fail()) {
cerr << "Error opening input file\n";
}
else{
//Open new file for output.
filename = "smoothed_"+filename;
fout.open(filename.c_str());
//Read and write header information.
read_header(fin,fout,width,height,max);
// cout << "width: " <<width <<"height: "<<height <<"max: "<< max <<endl;
//Declare image array.
vector<vector<pixel> >image(height, vector<pixel>(width));
//Read the image
for(i=0;i<height;i++)
for(j=0;j<width;j++){
fin>>image[i][j];
}
//Smooth the image.
smooth(image,width,height);
//Write modified image to new file.
for(i=0;i<height;i++)
for(j=0;j<width;j++){
fout<<image[i][j]<<' ';
/*
if ((j+1)%5==0) {
fout <<endl;
}*/
}
}//end of else
return 0;
}
//读取ppm文件头函数
void read_header(istream& fin,ostream& fout,int& width,
int& height,int& max){
char header[100];
char ch;
fin.getline(header,100);
fout<<header<<endl;
//get all comment lines and write to new file.
fin >> ch;
while (ch=='#') {
fin.getline(header,100);
fout<<ch<<header<<endl;
fin>> ch;
}
fin.putback(ch);//putback是将字符放回到输入流中
//input width and height of image.
fin >> width >> height;
fout << width <<' ' <<height<<endl;
//Input maximum color value.
fin>> max;
fout <<max <<endl;
return ;//关闭流
}
void smooth(vector<vector<pixel> >&image,int w,int h){
for(int i = 1;i<h-1;i++)
for(int j=1;j<w-1;j++){
image[i][j]=(image[i][j]+image[i-1][j]+image
[i+1][j]+image[i][j-1]+image[i][j+1])/5;
}
}