summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/ovision/img.cc
blob: 4fcc7b94640bbb16c11637668124386388cd4259 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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
    }
}

/// Convertit un tableau de donnees YUV en RGB
void Img::YUVtoRGB()
{
    unsigned char y,u,v;

    yuv = false;
    
    // Parcours du tableau et conversion des valeurs YUV en RGB
    for (unsigned long i=0; i<nbPixels; i++) {
	y = tabData[i*3];
	u = tabData[i*3+1];
	v = tabData[i*3+2];

	tabData[i*3] = (unsigned char)(y + (1.4075 * (v - 128)));
        tabData[i*3+1] = (unsigned char)(y + ((0.3455 * (u - 128)) - (0.7169 * (v - 128))));
	tabData[i*3+2] = (unsigned char)(y + (1.7790 * (u - 128)));
    }
}


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

/// Charge les valeurs RGB dans un fichier
void Img::LoadRGB(char *filename, int mode, int width, int height) {

    this->width = width;
    this->height = height;
    nbPixels = width * height;
    delete[] tabData;
    tabData = new unsigned char[nbPixels*3];
    yuv = mode;

    FILE *file;
    file = fopen(filename, "r");

    fread(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;
}