Read image length and width in C
Seeing a length, width program written by using ASP, feeling a bit, so I also wrote one with C .
#include
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 retturn 1;} else f ((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 f ((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 :: os :: 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 | (S1 [0]); m_height = (unsigned int = (unsigned int = (S2 [1]) << 8 | (S2 [0]); Break; Case 2: // jpg ffin.seekg (164); FFIN.READ (S1, 2); FFIN.READ S2, 2); m_width = (unsigned int) (S1 [1]) << 8 | (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 | (S1 [0]); m_height = (unsigned int) (S2 [1]) << 8 | (S2 [0]) Break; case 4: // bmp ffin.seekg (18); ffin.read (S1, 2); ffin.seekg (2, std :: os :: cur); ffin.read (S2, 2); M_WIDTH = (unsigned int) (S1 [1]) << 8 | (S1 [0]); m_height = (unsigned int) (S2 [1]) << 8 | (unsigned int) (S2 [0) ]); Break; Default: cout << "no" << endl; break;} ffin.close ();