Pgm Image processing in c++.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Youmair
    New Member
    • Feb 2012
    • 2

    Pgm Image processing in c++.

    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.



    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];
                                      
                            
                            }
                    }
         
    }
    Last edited by weaknessforcats; Mar 11 '12, 04:22 PM. Reason: added code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I don't believe you will find anyone to debug ths code. There are no comments and no statement of what the code is to do and how it is to do it. Understanding a problem from the code only is like looking at an elephant from two inches. You only see that it is gray.

    Have you stepped through each function with your debugger to confirm that the function works?

    Comment

    • Youmair
      New Member
      • Feb 2012
      • 2

      #3
      Thanks, for your advice i debugged my functions and found that error.Next time i'll care for debugging.

      Comment

      Working...