Hello,
In the following code ,i am able to shrink an image using 2d pointer,but when writing the image to a file ,the file is not fully shrink(the pixels are cropped).
Could anyone please help me to sort out the problem in my code.
In the following code ,i am able to shrink an image using 2d pointer,but when writing the image to a file ,the file is not fully shrink(the pixels are cropped).
Could anyone please help me to sort out the problem in my code.
Code:
#include<iostream>
#include<fstream>
#include<string>
#include<conio.h>
using namespace std;
void ReadPGM(ifstream& ipgm, string &f_name, string &f_type ,int **&ptr,int &rows,int &columns,int &maxgray,string &comment1);
void Shrinkimage(ifstream& ipgm,int **&ptr,int &rows,int &columns,int **&ptr2);
//void writePGM(ofstream &opgm,ifstream&ipgm, string &ftype,string &f_type,int **&ptr,int &rows,int &columns,int &maxgray,string &comment1);
void writePGM(ofstream &opgm,ifstream &ipgm,string &f_type,string &comment1,int &rows,int &columns,int &maxgray,int **&ptr2);
int main (){
ifstream ipgm;
ofstream opgm;
string f_name;
string f_type;
int userinput;
int rows,columns,maxgray;
string comment1,comment2;
int **ptr;
int **ptr2;
cout<<"Enter the name of you pgm file(with extension):"<<endl;
//fflush(stdin);
getline(cin,f_name);
ReadPGM(ipgm,f_name,f_type,ptr,rows,columns,maxgray,comment1);
cout<<"Enter ""1"" if you want to shrink a file:"<<endl;
cin>>userinput;
if(userinput==1)
{
Shrinkimage(ipgm,ptr,rows,columns,ptr2);
writePGM(opgm,ipgm,f_type,comment1,rows,columns,maxgray,ptr2);
}
system("pause");
return 0;
}
void ReadPGM(ifstream& ipgm, string &f_name, string &f_type ,int **&ptr,int &rows,int &columns,int &maxgray,string &comment1){
ipgm.open(f_name.c_str());
if(!ipgm) //if file not found
{
cout<<"File not found!"<<endl;
system("pause");
}
getline(ipgm, f_type);
getline(ipgm ,comment1);
//getline(ipgm ,comment2);
ipgm>>columns; //width
ipgm>>rows; //height
ipgm>>maxgray;
cout<<f_type<<endl<<comment1<<" "<<columns<<" "<<rows<<" "<<maxgray<<endl;
ptr =new int*[rows];
for(int i=0;i<rows;i++)
{
ptr[i]=new int[columns];
}
for(int j=0; j<rows; j++)
{
for(int k=0;k<columns;k++)
{
ipgm>> ptr[j][k];
//cout << ptr[j][k];
}
}
}
void writePGM(ofstream &opgm,ifstream &ipgm,string &f_type,string &comment1,int &rows,int &columns,int &maxgray,int **&ptr2){
string type1;
int rows2=rows/2;
int columns2=columns/2;
int **ptr3;
ptr3=new int * [rows];
for(int i=0;i<rows;i++)
{
ptr3[i]=new int [columns];
}
//For shrink
opgm.open("Shrink.PGM");
opgm<<f_type<<endl;
opgm<<comment1<<endl;
opgm<<columns2<<" ";
opgm<<rows2<<endl;
opgm<<maxgray<<endl;
for(int j=0,x=0; j<rows2; j+=2,x++)
{
for(int k=0,y=0;k<columns2;k+=2,y++)
{
opgm << ptr2[x][y]<<" "<<endl;
//cout << ptr2[x][y];
}
opgm <<"\n ";
}
}
void Shrinkimage(ifstream& ipgm,int **&ptr,int &rows,int &columns,int **&ptr2){
int x=0,y=0;
int rows2=rows/2;
int columns2=columns/2;
//int **ptr2;
ptr2 = new int* [rows2];
for(int i=0; i<rows2; i++)
{
ptr2[i] = new int [columns2];
}
for(int j=0,x=0; j<rows2; j+=2,x++)
{
for(int k=0,y=0;k<columns2;k+=2,y++)
{
ptr2[x][y] = ptr[j][k];
// cout << ptr2[x][y];
}
}
}
Comment