summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ovision/see/colorTable.cc
blob: 6d0780f64dda9bea137cb526783149544a36f6a2 (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
// colorTable.cc - Classe tableau de couleur
// robert - Programme du robot APBteam
// Copyright (C) 2005 Olivier Gaillard

/// @file colorTable.cc Gestion du tableau de couleur : compression/d�compression, �criture, ... 
#include <iostream>
#include <fstream>
#include <zlib.h>

#include "segmLearn.hh"
#include "colorTable.hh"

/// Constructor ColorTable
ColorTable::ColorTable (const char *filename) 
    : data_ (0)
{
    load (filename);
}


/// Destructor ColorTable
ColorTable::~ColorTable ()
{
    delete [] data_;
}

/// Cree un tableau des couleurs segment�es pour ne plus faire de calcul
/// et augmenter la rapidit�
/// @param testOutputMax choix de l'utilisation d'un sueil maxi pour la sortie pour �viter qu'une couleur trop diff�rente soit attribuer � une autre couleur
void
ColorTable::create (const int nbOutput_, const bool testOutputMax)
{
    SegmLearn segm;
    segm.buildNN (nbOutput_, Segm::loadFromFile);
    std::cout << "ColorTable:: Creation de la table" << std::endl;
    unsigned char x[3];
    delete [] data_;
    data_ = new unsigned char[colorTabSizeTotal];
    // Parcours de toutes les valeurs possibles
    for (int i=0; i<colorTabSize; i++)
	for (int j=0; j<colorTabSize; j++)
	    for (int k=0; k<colorTabSize; k++)
	      {
		// Remplissage de la table
		x[0]=i; x[1]=j; x[2]=k;	
		data_[ i*colorTabSize2 + j*colorTabSize +k ] = segm.giveColor (x , testOutputMax);
	      }
}

/// Cree un fichier de ColorTable
void 
ColorTable::save (const char *filename) const
{
    std::cout << "ColorTable:: Ecriture de la table" << std::endl;
    gzFile file = gzopen (filename, "w+");
    if (file)
      {
	gzwrite (file, (char*)data_, colorTabSizeTotal);
	gzclose (file);
      }
    else
	std::cerr << "ColorTable::CreateColorTableFile Erreur d'ouverture de fichier" << std::endl;

}

/// Charge un fichier de ColorTable
bool
ColorTable::load (const char *filename)
{
    /// Initialisation de data
    delete [] data_;
    data_ = new unsigned char[colorTabSizeTotal];
    /// Ouverture du fichier de couleurs
    gzFile file = gzopen (filename, "w+");
    if (file)
      {
	std::cout << "ColorTable:: Chargement de la table" << std::endl;
	gzread (file, (char*)data_, colorTabSizeTotal);
	gzclose (file);
      }
    else
      {
	std::cerr << "ColorTable::CreateColorTableFile Erreur d'ouverture de fichier" << std::endl;
	return false;
      }
    return true;
}