C + + Implementation read Picture length and width _c language

Source: Internet
Author: User

See an ASP written to read the image file length, width of the program, feeling a little meaning, so with C + + also wrote a.

#include <iostream> #include <fstream> #include <string> using namespace std;
  Class CImage {Private:long m_width;
  
  Long m_height;
int get_extension (string fname);
    Public:cimage () {m_width = 0;
    
  m_height = 0;
  };
  
  void LoadImage (char* fname);
  Long Get_width () {return m_width;
  
  };
  Long Get_height () {return m_height;  
  
};

};
  int cimage::get_extension (String fname) {char c = fname.at (Fname.length ()-1);
  
  char C2 = fname.at (Fname.length ()-3);
  if (c = = ' F ') && (C2 = = ' G ')) {//File extension name is GIF return 1;
  }else if ((c = = ' G ') && (C2 = = ' J ')) {//File extension name is JPG return 2;
  }else if ((c = = ' G ') && (c2 = = ' P ')) {//File extension name is PNG return 3;
  }else if ((c = = ' P ') && (C2 = = ' B ')) {//File extension name is BMP return 4;
return 0;
    
  } void Cimage::loadimage (char *fname) {m_width = M_height = 0; IfstReam Ffin (fname, std::ios::binary); if (!ffin) {cout<< ' Can not ' open this file. '
    <<endl;
  Return
  int result = Get_extension (fname);
  
  Char s1[2] = {0}, s2[2] = {0};     
    Switch (Result) {Case 1://GIF ffin.seekg (6);
    Ffin.read (S1, 2);    
    Ffin.read (S2, 2);
    M_width = (unsigned int) (s1[1]) <<8| (unsigned int) (s1[0));  
    M_height = (unsigned int) (s2[1]) <<8| (unsigned int) (s2[0));
  Break    
    Case 2://JPG ffin.seekg (164);
    Ffin.read (S1, 2);    
    Ffin.read (S2, 2);
    M_width = (unsigned int) (s1[1]) <<8| (unsigned int) (s1[0));  
    M_height = (unsigned int) (s2[1]) <<8| (unsigned int) (s2[0));
  Break    
    Case 3://PNG FFIN.SEEKG (17);
    Ffin.read (S1, 2);
    FFIN.SEEKG (2, std::ios::cur);   
    Ffin.read (S2, 2);
    M_width = (unsigned int) (s1[1]) <<8| (unsigned int) (s1[0));  
    M_height = (unsigned int) (s2[1]) <<8| (unsigned int) (s2[0));
  Break   Case 4://BMP 
    FFIN.SEEKG (18);
    Ffin.read (S1, 2);
    FFIN.SEEKG (2, std::ios::cur);    
    Ffin.read (S2, 2);
    M_width = (unsigned int) (s1[1]) <<8| (unsigned int) (s1[0));  
    M_height = (unsigned int) (s2[1]) <<8| (unsigned int) (s2[0));
  Break
    default:cout<< "NO" <<endl;
  Break
} ffin.close ();


};
    int main (int argc, char *argv[]) {if (ARGC < 2) {printf ("Usage:program imagefilename/n");
  return 0;
  } CImage test; Test.
  LoadImage (argv[1]);
  cout<< "width:" <<test.get_width () <<endl;
   
  cout<< "Height:" <<test.get_height () <<endl;
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.