Skip to content
Snippets Groups Projects
Image.cpp 3.78 KiB
Newer Older
Jean-Marc's avatar
Jean-Marc committed
#include "Image.h"
#include <iostream>
#include <cassert>
Jean-Marc's avatar
Jean-Marc committed
#include <fstream>
Jean-Marc's avatar
Jean-Marc committed

using namespace std;

Image::Image()
{
Jean-Marc's avatar
Jean-Marc committed
    cout<<"tab alloué : "<<tab<<endl;
Jean-Marc's avatar
Jean-Marc committed
    tab = nullptr;
Jean-Marc's avatar
Jean-Marc committed
    dimx = 0;
    dimy = 0;
Jean-Marc's avatar
Jean-Marc committed

Jean-Marc's avatar
Jean-Marc committed
}

Jean-Marc's avatar
Jean-Marc committed
Image::Image(unsigned int dimensionX, unsigned int dimensionY)
Jean-Marc's avatar
Jean-Marc committed
{
Jean-Marc's avatar
Jean-Marc committed
    assert (dimensionX>0 && dimensionY>0);
Jean-Marc's avatar
Jean-Marc committed

Jean-Marc's avatar
Jean-Marc committed
    dimx = dimensionX;
    dimy = dimensionY;
Jean-Marc's avatar
Jean-Marc committed
    tab = new Pixel [dimx*dimy];
Jean-Marc's avatar
Jean-Marc committed
    for(unsigned int i = 0; i<dimx*dimy; i++)
    {
        tab[i] = Pixel(0,0,0);
    }

Jean-Marc's avatar
Jean-Marc committed
}

Image::~Image()
{
    dimx = 0;
    dimy = 0;
Jean-Marc's avatar
Jean-Marc committed
    if (tab != nullptr)
    {
        delete [] tab;
        tab = nullptr;
    }
    
    
Jean-Marc's avatar
Jean-Marc committed
    cout<<"tab détruit : "<<tab<<endl;
Jean-Marc's avatar
Jean-Marc committed
Pixel & Image::getPix(unsigned int x, unsigned int y)
Jean-Marc's avatar
Jean-Marc committed
{
Jean-Marc's avatar
Jean-Marc committed
    assert (x>=0 && y>=0);
Jean-Marc's avatar
Jean-Marc committed
    assert (x<dimx && y<dimy);
Jean-Marc's avatar
Jean-Marc committed

Jean-Marc's avatar
Jean-Marc committed
    return tab[y*dimx+x];
Jean-Marc's avatar
Jean-Marc committed
}

Jean-Marc's avatar
Jean-Marc committed
Pixel Image::getPix2(unsigned int x, unsigned int y) const
Jean-Marc's avatar
Jean-Marc committed
{
Jean-Marc's avatar
Jean-Marc committed
    assert (x>=0 && y>=0); 
Jean-Marc's avatar
Jean-Marc committed
    assert (x<dimx && y<dimy);
Jean-Marc's avatar
Jean-Marc committed

    Pixel ret = tab[y*dimx+x];

    return ret;
}

Jean-Marc's avatar
Jean-Marc committed
void Image::setPix(const unsigned int &x, const unsigned int &y, const Pixel &couleur)
Jean-Marc's avatar
Jean-Marc committed
{
    tab[y*dimx+x] = couleur;
}

Jean-Marc's avatar
Jean-Marc committed
void Image::dessinerRectangle(unsigned int Xmin, unsigned int Ymin, unsigned int Xmax, unsigned int Ymax, const Pixel & couleur)
Jean-Marc's avatar
Jean-Marc committed
{
Jean-Marc's avatar
Jean-Marc committed
    unsigned int i,j;
Jean-Marc's avatar
Jean-Marc committed

Jean-Marc's avatar
Jean-Marc committed
    for(i=Xmin;i<Xmax;i++)
Jean-Marc's avatar
Jean-Marc committed
    {
Jean-Marc's avatar
Jean-Marc committed
        for(j=Ymin;j<Ymax;j++)
Jean-Marc's avatar
Jean-Marc committed
        {
            setPix(i,j,couleur);

        }
    }
}

Jean-Marc's avatar
Jean-Marc committed
void Image::effacer (const Pixel & couleur)
Jean-Marc's avatar
Jean-Marc committed
{
    dessinerRectangle(0,0,dimx,dimy,couleur);
}
Jean-Marc's avatar
Jean-Marc committed

Jean-Marc's avatar
Jean-Marc committed
void Image::testRegression()
{
    cout<<"test de regression : "<<endl;
    Image im1;
    assert (im1.dimx == 0);
    assert (im1.dimy == 0);

    Image im2(50,50);
    assert (im2.dimx == 50);
    assert (im2.dimy == 50);

    Pixel p1 = im2.getPix(14,26);
    assert(p1.r == 0);
    assert(p1.g == 0);
    assert(p1.b == 0);

    Pixel p2(145,78,153);
    im2.dessinerRectangle(10,10,30,30,p2);
    p1 = im2.getPix(18,22);
    cout<<int(p1.r)<<" "<<int(p1.g)<<" "<<int(p1.b)<<endl;

    im2.effacer(Pixel(0,0,0));
    p2 = im2.getPix(18,22);
    cout<<int(p2.r)<<" "<<int(p2.g)<<" "<<int(p2.b)<<endl;



    

    cout<<"fin test"<<endl;
}

Jean-Marc's avatar
Jean-Marc committed
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;
Jean-Marc's avatar
Jean-Marc committed
    for (unsigned int y = 0; y < dimy; y++)
        for (unsigned int x = 0; x < dimx; x++)
Jean-Marc's avatar
Jean-Marc committed
        {
Jean-Marc's avatar
Jean-Marc committed
            Pixel pix = getPix2(x, y);
Jean-Marc's avatar
a  
Jean-Marc committed
            fichier << int(pix.r) << " " << int(pix.g) << " " << int(pix.b) << " ";
Jean-Marc's avatar
Jean-Marc committed
            //cout<<x<<" , "<<y<<endl;
Jean-Marc's avatar
Jean-Marc committed
        }
    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());
Jean-Marc's avatar
Jean-Marc committed
    unsigned char r, g, b;
Jean-Marc's avatar
Jean-Marc committed
    string mot;
    dimx = dimy = 0;
    fichier >> mot >> dimx >> dimy >> mot;
Jean-Marc's avatar
Jean-Marc committed
    cout<<dimx<<","<<dimy<<endl;
Jean-Marc's avatar
Jean-Marc committed
    assert(dimx > 0 && dimy > 0);
    if (tab != nullptr)
        delete[] tab;
    tab = new Pixel[dimx * dimy];
Jean-Marc's avatar
Jean-Marc committed
    for (unsigned int y = 0; y < dimy; y++)
        for (unsigned int x = 0; x < dimx; x++)
Jean-Marc's avatar
Jean-Marc committed
        {
Jean-Marc's avatar
Jean-Marc committed
            fichier >> r >> g >> b;
            //cout<<r<<" , "<<g<<" , "<<b<<endl;
Jean-Marc's avatar
a  
Jean-Marc committed
            getPix(x, y).r = char(r);
            getPix(x, y).g = char(g);
            getPix(x, y).b = char(b);
Jean-Marc's avatar
Jean-Marc committed
        }
    fichier.close();
    cout << "Lecture de l'image " << filename << " ... OK\n";
}
Jean-Marc's avatar
Jean-Marc committed
/*
Jean-Marc's avatar
Jean-Marc committed
void Image::afficherConsole()const
Jean-Marc's avatar
Jean-Marc committed
{
    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);
Jean-Marc's avatar
Jean-Marc committed
            cout << pix.r << " " << pix.g << " " << pix.b << " ";
Jean-Marc's avatar
Jean-Marc committed
        }
        cout << endl;
    }
}
Jean-Marc's avatar
Jean-Marc committed
    */
Jean-Marc's avatar
Jean-Marc committed

Jean-Marc's avatar
Jean-Marc committed
Pixel * Image::getTab()const
{
    return tab;
}