summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/img.cc
blob: 60469add05fb914b824eff85aa49a2a2013477be (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
// img.cc - Classe Image
// nono - Programme du robot Efrei Robotique I1-I2 2004
// Copyright (C) 2004 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.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
using namespace std;
/// Renvoie le minimum entre 2 nombres
inline unsigned char min(unsigned char a, unsigned char b) {
    if (a<b) return a;
    else return b;
}

/// Constructeur
Img::Img (void) {
    // Initialisation des variables
    tabData = NULL;
    tabSegm = NULL;

    yuv = hsi = false;

}

/// Destructeur
Img::~Img (void) {
    //free tabData
    delete [] tabData;
}



/// Convertit un tableau de donnees RGB en YUV
void Img::RGBtoYUV() {
    unsigned char r,g,b;

    yuv = true;

    // Parcours du tableau et conversion des valeurs RBG en YUV
    for (unsigned long i=0; i<nbPixels; i++) {
	r = tabData[i*3];
	g = tabData[i*3+1];
	b = tabData[i*3+2];

	tabData[i*3] = (unsigned char)(0.299*r + 0.587*g + 0.114*b); // Y
        tabData[i*3+1] = (unsigned char)(0-0.147*r - 0.289*g + 0.437*b + 0.5); // U
	tabData[i*3+2] = (unsigned char)(00.615*r - 0.515*g - 0.100*b + 0.5); // V
    }
}

/// Convertit un tableau de donnees RGB en YUV
void Img::RGBtoHSI() {
    unsigned char r,g,b;

    hsi = true;

    // Parcours du tableau et conversion des valeurs RBG en HSI
    for (unsigned long i=0; i<nbPixels; i++) {
	r = tabData[i*3];
	g = tabData[i*3+1];
	b = tabData[i*3+2];
	
	tabData[i*3] = (unsigned char)(acos((r-0.5f*g-0.5f*b)/(sqrt(1.0f*(r-g)*(r-g)+(r-b)*(g-b))))); // H
	tabData[i*3+1] = (unsigned char)(1-min(min(r,g),b)); // S
	tabData[i*3+2] = (unsigned char)(0.33f*(r+g+b)); // I
    }
}

/// Ecrit les valeurs RGB dans un fichier
void Img::WriteRGB(char *filename) {

    FILE *file;
    file = fopen(filename, "w+");

    fwrite(tabData, 3, width*height, file);
    
    fclose(file);
}

/// Lit une image depuis un ImageLoader.
void
Img::load (ImageLoader &loader)
{
    delete[] tabData;
    loader.getSize (width, height);
    nbPixels = width * height;
    tabData = new unsigned char[loader.getBufSize ()];
    loader.read (tabData, loader.getBufSize ());
    yuv = loader.getColorSpace () == ImageLoader::yuv;
}