summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/see/img.cc
blob: fa38e4ab778025d59e8341350891d5b905f4f96f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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 <iostream>
#include <fstream>
#include <stdlib.h>

/// Constructeur
Img::Img (void)
    : tabY_ (0), tabData_ (0), tabSegm_ (0)
{
}

/// Destructeur
Img::~Img (void)
{
    delete [] tabData_;
    delete [] tabSegm_;
    delete [] tabY_;
}

/// 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 ();
}

/// Cr�e le tableau Y
void
Img::doTabY ()
{
    delete [] tabY_;
    tabY_ = new int[height_];
    for (int i=0; i<height_; ++i)
	tabY_[i] = i*width_;
}

/// 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_;
    doTabY ();
    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<std::string> &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_;
    doTabY ();
    this->colorMode_ = colorMode_;
    // Initialise tabData_
    delete[] tabData_;
    tabData_ = new unsigned char[nbPixels_*3];
    unsigned char *t = tabData_;
    // Ouvre les images et stocke les donn�es dans tabData_
    for (std::vector<std::string>::const_iterator iter=imgList.begin(); iter != imgList.end(); iter++)
      {
	std::ifstream file (iter->c_str (), std::ios::in);
	if (file)
	  {
	    file.read ((char *)t, nbPixels_*3);
	    t += 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_);
    doTabY ();
    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 ();
}