1 #include "PGM.h" 2 #include "PPM.h" 3 #include <cstddef> // C++11 NULL 4 #include <vector> // NULL definition 5 6 namespace ProtoMol { 7 8 //_____________________________________________________________________ PGM ~PGM()9 PGM::~PGM(){delete [] p;} 10 PGM()11 PGM::PGM():w(0),h(0),p(NULL){ 12 } 13 PGM(int x,int y)14 PGM::PGM(int x, int y):w(x),h(y),p(new unsigned char[x*y]){ 15 clear(); 16 } 17 clear()18 void PGM::clear(){ 19 for(unsigned int i=0;i<w*h;i++) 20 p[i]= static_cast<unsigned char>(0); 21 } 22 resize(unsigned int width,unsigned int height)23 void PGM::resize(unsigned int width, unsigned int height){ 24 if(p != NULL && width*height != w*h){ 25 delete [] p; 26 p = NULL; 27 } 28 if(p == NULL && width*height>0) 29 p = new unsigned char[width*height]; 30 w = width; 31 h = height; 32 clear(); 33 } 34 operator =(const PPM & ppm)35 PGM& PGM::operator=(const PPM& ppm){ 36 resize(ppm.width(),ppm.height()); 37 unsigned char* p0 = ppm.begin(); 38 for(unsigned char* p1=begin();p1 != end();p1++){ 39 (*p1) = static_cast<unsigned char>((static_cast<unsigned int>(p0[0])+static_cast<unsigned int>(p0[1])+static_cast<unsigned int>(p0[2]))/3); 40 p0 += 3; 41 } 42 return (*this); 43 } 44 } 45