#include #include #include #include #include #include #include #include "imagePGM.h" using namespace std; imagePGM::imagePGM () { width = 0; height = 0; data = NULL; dataGL = NULL; } imagePGM::imagePGM (int width, int height) { this->width = width; this->height = height; data = new int[width * height]; dataGL = new GLubyte[width * height]; } imagePGM::imagePGM (char *fileName) { int x, y; ifstream input (fileName, ios::in); string buffer; size_t temp = 0; // Vérification de l'ouverture du fichier if (!input) { cout << "ERREUR Ouverture du fichier \"" << fileName << "\"\n"; exit (-1); } // Lecture MagicValue getline (input, buffer); temp = buffer.find ("P5"); if (temp < 0 || temp >= buffer.length ()) { cout << "ERREUR MagicValue : " << buffer << " != P5\n"; input.close (); exit (-1); } // On saute les ligne commencant par un '#' do { buffer.clear (); getline (input, buffer); } while (buffer[0] == '#'); // Lecture width height { istringstream bufferTemp (buffer); bufferTemp >> width >> height; } // On saute les ligne commencant par un '#' do { buffer.clear (); getline (input, buffer); } while (buffer[0] == '#'); // Lecture maxgrey (On ne tient pas compte de maxgrey, qui est presque toujours 255) { int maxgrey; istringstream bufferTemp (buffer); bufferTemp >> maxgrey; } // Allocation ImagePGM data = new int[width * height]; dataGL = new GLubyte[width * height]; buffer.clear (); // Lecture ImagePGM for (y = 0; y < height; y++) { unsigned char temp; for (x = 0; x < width; x++) { input >> temp; data[y * width + x] = temp; } } name = fileName; input.close (); } void imagePGM::save () { int x, y; ofstream output (name, ios::out); // Vérification de l'ouverture du fichier if (!output) { cout << "ERREUR Ouverture du fichier \"" << name << "\"\n"; exit (-1); } // Ecriture du header output << "P5\n"; output << "# width height\n"; output << width << " " << height << "\n"; output << "255\n"; // Ecriture du tableau for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { cout << data[y * width + x]; if (x != width - 1) { cout << " "; } else { cout << "\n"; } } } output.close (); } imagePGM::~imagePGM () { delete data; delete dataGL; } int imagePGM::write (int x, int y, int value) // Affecte la case (x,y) de l'image avec la valeur "value" en vérifiant si x et y sont bien dans l'image { if (x < width && y < height && x >= 0 && y >= 0) { data[y * width + x] = value; return 0; } return -1; } int imagePGM::read (int x, int y) // Renvoie la valeur d'une case de l'image aux coordonnées (x,y) { if (x < width && y < height && x >= 0 && y >= 0) { return data[y * width + x]; } return -1; } void imagePGM::init (int value) { for (int temp = 0; temp < height; temp++) { data[temp] = value; } } int imagePGM::rockImage () { int x, y; for (int temp = 0; temp < width * height; temp++) { if (data[temp] < 0 || data[temp] > 255) { return -1; } } for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { dataGL[y * width + x] = (GLubyte)(data[(height - 1 - y) * width + x]); } } return 0; } int imagePGM::readMirror (int x, int y) { int correction = 0; if (width * height == 1) { return data[0]; } if (x < 0) { x *= -1; correction++; } if (x >= width) { x = 2 * (width - 1) - x + 1; correction++; } if (y < 0) { y *= -1; correction++; } if (y >= height) { y = 2 * (height - 1) - y + 1; correction++; } if (correction) { return readMirror (x, y); } return data[y * width + x]; } imagePGM & imagePGM::operator = (const imagePGM & image) { if (this != &image) { int temp; // Effacement des tableaux dynamiques delete data; delete dataGL; // Copie des variables width = image.width; height = image.height; // Création des tableaux dynamiques data = new int[width * height]; dataGL = new GLubyte[width * height]; // Copie du contenue des tableaux for (temp = 0; temp < width * height; temp++) { data[temp] = image.data[temp]; dataGL[temp] = image.dataGL[temp]; } } return * this; } int imagePGM::getHeight () { return height; } int imagePGM::getWidth () { return width; } int imagePGM::getSize () { return width * height; }