rendered paste body#include <image.h>#include <string.h>#include <iostream>#include <string>#include <GL/gl.h>using namespace std;Image :: Image() : width(0), height(0), depth(0), offset(0), buffer(NULL){}Image :: Image(int _w, int _h, int _d) : width(_w), height(_h), depth(_d), offset(1){ buffer = new unsigned char[width * height * depth];}Image :: Image(const char * _filename) : width(0), height(0), depth(0), offset(0), buffer(NULL){ load(_filename);}Image :: Image(const Image & _i) : width(_i.width), height(_i.height), depth(_i.depth), offset(_i.offset){ memcpy(buffer, _i.buffer, width * height * depth * offset * sizeof(unsigned char));}Image :: ~Image(){ clear();}void Image :: clear(){ if(buffer != NULL) { if(offset == 1) delete [] (unsigned char *)buffer; else delete [] (unsigned short *)buffer; buffer = NULL; } width = 0; height = 0; depth = 0; offset = 0;}bool Image :: loadPNM(ifstream & _file){ string magic, line; int type; _file >> magic; type = atoi(&magic[1]); if(magic[0] != 'P' && type < 1 && type > 6) { cerr << "[Image::loadPNG] Erreur. Le fichier n'est pas au format PNM (format : " << magic << "). Abandon du chargement." << endl; return false; } _file >> std::ws; while(_file.peek() == '#') getline(_file, line); _file >> width; _file >> height; _file >> offset; switch(type) { case 3 : case 6 : depth = 3; break; default : depth = 1; break; } if(width * height <= 0) { cerr << "[Image::loadPNG] Erreur. Les dimensions de l'image sont incorrectes (x : " << width << ", y : " << height << "). Abandon du chargement." << endl; return false; } if(offset <= 256) offset = 1; else offset = 2; if(offset == 1) { buffer = new unsigned char[width * height * depth * offset * sizeof(char)]; for(int j(height - 1); j > 0; j--) _file.read((char *)&(((unsigned char *)buffer)[j * width * depth]), width * depth * offset * sizeof(char)); } else { buffer = new unsigned short[width * height * depth * offset * sizeof(char)]; for(int j(height - 1); j > 0; j--) _file.read((char *)&(((unsigned short *)buffer)[j * width * depth]), width * depth * offset * sizeof(char)); } return true;}bool Image :: load(const char * _filename){ char * extension = strrchr(_filename, '.'); ifstream file(_filename, ios::binary); if(file.is_open() == false) { cerr << "[Image::load] Erreur. Le fichier \"" << _filename << "\" est introuvable." << endl; return false; } if(strcmp(extension, ".pgm") == 0) return loadPNM(file); if(strcmp(extension, ".ppm") == 0) return loadPNM(file); if(strcmp(extension, ".pnm") == 0) return loadPNM(file); file.close(); cerr << "[Image::load] Erreur. Le format du fichier \"" << _filename << "\" est inconnu." << endl; return false;}void Image :: display() const{ glRasterPos2i(0, 0); glDrawPixels(width, height, (depth == 1) ? GL_LUMINANCE : GL_RGB, (offset == 1) ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT, buffer);}int Image :: getWidth() const{ return width;}int Image :: getHeight() const{ return height;}