Corrigé du TD 2 : points et images

Classe Point

Fichier Point.h

#ifndef Point_h
#define Point_h
 
class Point
{
public :
    Point();
    Point(int _x, int _y);
    Point(Point const & p);
 
    int  getX() const;
    void setX(int _x);
 
    int  getY() const;
    void setY(int _y);
 
private :
    int x;
    int y;
};
 
#endif

Fichier Point.cpp

#include "Point.h"
 
Point::Point()
{
    // Rien à faire
}
 
Point::Point(int _x, int _y)
: x(_x), y(_y)
{
    // Rien d'autre
}
 
Point::Point(Point const & p)
: x(p.x), y(p.y)
{
    // Rien d'autre
}
 
int
Point::getX() const
{
    return x;
}
 
void
Point::setX(int _x)
{
    x = _x;
}
 
int
Point::getY() const
{
    return y;
}
 
void
Point::setY(int _y)
{
    y = _y;
}

Image 2D

Ordre lexicographique

int pointVersIndice(Point const & p, Point const & taille)
{
    return p.getX() + p.getY()*taille.getY();
}
 
Point indiceVersPoint(int indice, Point const & taille)
{
    Point p;
    p.setX(indice % taille.getX());
    p.setY((indice - p.getX()) / taille.getX());
 
    return p;
}

Classe Image

Fichier Image.h

#ifndef Image_h
#define Image_h
 
#include "Point.h"
 
class Image
{
public :
    Image();
    Image(Point const & _taille);
    Image(Image const & i);
    ~Image();
    Image const & operator=(Image const & i);
 
    unsigned char getValeur(Point const & p) const;
    void setValeur(Point const & p, unsigned char valeur);
 
    unsigned char const & operator()(Point const & p) const;
    unsigned char       & operator()(Point const & p);
private :
    void alloueTableau(Point const & taille);
    void desalloueTableau();
    void copieTableau(unsigned char* t);
 
    unsigned char* tab;
    Point taille;
};
 
#endif

Fichier Image.cpp

#include "Image.h"
 
Image::Image()
: tab(0), taille(0)
{
}
 
Image::Image(Point const & _taille)
: taille(_taille)
{
    alloueTableau(taille);
}
 
Image::Image(Image const & i)
: taille(i.taille)
{
    alloueTableau(taille);
    copieTableau(i.tab);
}
 
Image::~Image()
{
    desalloueTableau();
}
 
Image const &
Image::operator=(Image const & i)
{
    if(this != &i)
    {
        desalloueTableau();
        taille = i.taille;
        alloueTableau(taille);
        copieTableau(i.tab);
    }
 
    return (*this);
}
 
unsigned char
Image::getValeur(Point const & p) const
{
    return tab[pointVersIndice(p, taille)];
}
 
void
Image::setValeur(Point const & p, unsigned char valeur)
{
    tab[pointVersIndice(p, taille)] = valeur;
}
 
unsigned char const &
Image::operator()(Point const & p) const
{
    return tab[pointVersIndice(p, taille)];
}
 
unsigned char &
Image::operator()(Point const & p)
{
    return tab[pointVersIndice(p, taille)];
}
 
void
Image::alloueTableau(Point const & taille)
{
    tab = new unsigned char[taille.getX()*taille.getY()];
}
 
void
Image::desalloueTableau()
{
    delete[] tab;
}
 
void
Image::copieTableau(unsigned char* t)
{
    for(int i=0; i<taille.getX()*taille.getY(); ++i)
    {
        tab[i] = t[i];
    }
}

Fichier main.cpp

#include <iostream>
#include "Image.h"
#include "Point.h"
 
int main()
{
    Image image;
 
    Point p1(10,10);
    Point p2(20,20);
 
    Point taille(100, 100);
    Image image1(taille);
    image1.setValeur(p1, 128);
    std::cout << image1.getValeur(p1) << "n";
 
    image = image1;
    image(p2) = 255;
    std::cout << image(p2) << "n";
}

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>