#include "Image.h" #include <iostream> #include <cassert> #include <fstream> using namespace std; Image::Image() { tab = new Pixel [1]; cout<<"tab alloué : "<<tab<<endl; dimx = 0; dimy = 0; } Image::Image(const unsigned int &x, const unsigned int &y) { assert (x>0 && y>0); dimx = x; dimy = y; tab = new Pixel [dimx*dimy]; cout<<"tab alloué v2 : "<<tab<<endl; } Image::~Image() { dimx = 0; dimy = 0; tab = nullptr; delete [] tab; cout<<"tab détruit : "<<tab<<endl; } Pixel & Image::getPix(const unsigned int &x, const unsigned int &y) const { assert (x>=0 && y>=0); return tab[y*dimx+x]; } Pixel Image::getPix2(const unsigned int &x, const unsigned int &y) const { assert (x>=0 && y>=0); Pixel ret = tab[y*dimx+x]; return ret; } void Image::setPix(const unsigned int &x, const unsigned int &y, const Pixel &couleur) { tab[y*dimx+x] = couleur; } void Image::dessinerRectangle(unsigned Xmin, unsigned int Ymin, unsigned int Xmax, unsigned int Ymax, const Pixel & couleur) { unsigned i,j; for(i=Xmin;i<Xmax;i++) { for(j=Ymin;j<Ymax;j++) { setPix(i,j,couleur); } } } void Image::effacer (const Pixel & couleur) { dessinerRectangle(0,0,dimx,dimy,couleur); } void Image::sauver(const string &filename) const { ofstream fichier(filename.c_str()); assert(fichier.is_open()); fichier << "P3" << endl; fichier << dimx << " " << dimy << endl; fichier << "255" << endl; for (unsigned int y = 0; y < dimy; y++) for (unsigned int x = 0; x < dimx; x++) { Pixel &pix = getPix(x, y); fichier << int(pix.r) << " " << int(pix.g) << " " << int(pix.b) << " "; //cout<<x<<" , "<<y<<endl; } cout << "Sauvegarde de l'image " << filename << " ... OK\n"; fichier.close(); } void Image::ouvrir(const string &filename) { ifstream fichier(filename.c_str()); assert(fichier.is_open()); unsigned char r, g, b; string mot; dimx = dimy = 0; fichier >> mot >> dimx >> dimy >> mot; cout<<dimx<<","<<dimy<<endl; assert(dimx > 0 && dimy > 0); if (tab != nullptr) delete[] tab; tab = new Pixel[dimx * dimy]; for (unsigned int y = 0; y < dimy; y++) for (unsigned int x = 0; x < dimx; x++) { fichier >> r >> g >> b; //cout<<r<<" , "<<g<<" , "<<b<<endl; getPix(x, y).r = char(r); getPix(x, y).g = char(g); getPix(x, y).b = char(b); } fichier.close(); cout << "Lecture de l'image " << filename << " ... OK\n"; } void Image::afficherConsole()const { cout << dimx << " " << dimy << endl; for (unsigned int y = 0; y < dimy; ++y) { for (unsigned int x = 0; x < dimx; ++x) { Pixel &pix = getPix(x, y); cout << pix.r << " " << pix.g << " " << pix.b << " "; } cout << endl; } } Pixel * Image::getTab()const { return tab; }