// img.cc - Classe Image // robert - Programme du robot APBteam // Copyright (C) 2005 Olivier Gaillard /// @file img.cc Chargement des images, conversion en YUV, HSI, detection des contours, transformation d'une image segmentee en RGB, ecriture de l'image sur le disque #include "img.hh" #include #include #include /// Constructeur Img::Img (void) : tabData_ (0), tabSegm_ (0) { } /// Destructeur Img::~Img (void) { delete [] tabData_; delete [] tabSegm_; } /// Ecrit les valeurs RGB dans un fichier void Img::writeRaw (const char *filename, unsigned char *tab) const { if (!tab) tab = tabData_; std::ofstream file (filename, std::ios::out | std::ios::trunc); if (file) file.write ((char *)tab, nbPixels_*3); else std::cerr << "Img::writeRaw Erreur d'ouverture de fichier" << std::endl; file.close (); } /// Charge les valeurs RGB dans un fichier void Img::loadRaw (const char *filename, const Image::PixelFormat colorMode_, const int width_, const int height_) { // Enregistre les paramètres de l'image this->width_ = width_; this->height_ = height_; nbPixels_ = width_ * height_; this->colorMode_ = colorMode_; // Initialise tabData_ delete[] tabData_; tabData_ = new unsigned char[nbPixels_*3]; // Ouvre l'image std::ifstream file (filename, std::ios::in); if (file) file.read ((char *)tabData_, nbPixels_*3); else std::cerr << "Img::loadRaw Erreur d'ouverture de fichier " << filename << std::endl; file.close (); } /// Charge plusieurs fichiers void Img::loadMultipeRaw (const std::vector &imgList, const Image::PixelFormat colorMode_, const int width_, const int height_) { // Enregistre les paramètres de l'image this->width_ = width_; this->height_ = height_ * imgList.size (); nbPixels_ = width_ * height_; this->colorMode_ = colorMode_; // Initialise tabData_ delete[] tabData_; tabData_ = new unsigned char[nbPixels_*3]; // Ouvre les images et stocke les données dans tabData_ for (std::vector::const_iterator iter=imgList.begin(); iter != imgList.end(); iter++) { std::ifstream file (iter->c_str (), std::ios::in); if (file) file.read ((char *)tabData_, nbPixels_*3); else std::cerr << "Img::loadRaw Erreur d'ouverture de fichier " << *iter << std::endl; file.close (); } } /// Lit une image depuis un Image. void Img::load (ImageReader &loader) { delete[] tabData_; // Ouvre l'image Image image; image.read (loader); loader.getParam (width_, height_, colorMode_); nbPixels_ = width_ * height_; tabData_ = new unsigned char[image.getBufSize ()]; memcpy (tabData_, image.getBuf (), image.getBufSize ()); // Récupère le format de couleur colorMode_ = image.getPixelFormat (); }