summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/image/image.hh
blob: 2496ef7ce5994a8419a84c20cb260e8778979c28 (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
#ifndef image_hh
#define image_hh
// image.hh
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Nicolas Schodet
//
// Robot APB Team/Efrei 2005.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}

#include <stdint.h>

class ImageReader;
class ImageWriter;

/// Classe contenant une image. Le but est de faciliter l'�change des images
/// entre diff�rentes classes, et de minimiser les copies. Il y a un comptage
/// de r�f�rence et un copy-on-write, ce qui veut dire que si l'on copie cet
/// objet, il n'y a pas de copie des pixels tant que l'on essaye pas de la
/// modifier.
///
/// La classe n'est pas thread-safe � cause du comptage de r�f�rence
/// impl�ment� sans lock.
class Image
{
  public:
    enum PixelFormat { rgb, bgr, yuv422, yuv, hsi };
    typedef uint32_t pixel_t;
  public:
    /// Construit une image vide.
    Image (void);
    /// Construit une image aux pixels non initialis�e.
    Image (int width, int length, PixelFormat pixelFormat);
    /// Constructeur de copie, copy-on-write.
    Image (const Image &image);
    /// Construit une image directement � partir d'un ImageReader.
    Image (ImageReader &reader);
    /// Destructeur.
    ~Image (void);
    /// Op�rateur d'affectation.
    Image &operator= (const Image &rhs);
    /// R�cup�re la taille de l'image.
    void getSize (int &width, int &height) const;
    /// Retourne le format des pixels.
    PixelFormat getPixelFormat (void) const;
    /// Retourne le nombre d'octets par pixels.
    unsigned getBytePerPixel (void) const;
    /// Retourne le nombre d'octets par pixels pour un format donn�e.
    static unsigned getBytePerPixel (PixelFormat pixelFormat);
    /// Retourne la taille du tampon m�moire.
    unsigned getBufSize (void) const;
    /// Retourne la taille du tampon m�moire pour une image donn�e.
    static unsigned getBufSize (int width, int height, PixelFormat pixelFormat);
    /// R�cup�re un pointeur vers l'image.
    const uint8_t *getBuf (void) const;
    /// R�cup�re un pointeur mutable vers l'image. D�clenche �ventuellement
    /// une copie.
    uint8_t *getMutableBuf (void);
    /// Lit une image.
    void read (ImageReader &reader);
    /// �crit une image.
    void write (ImageWriter &writer) const;
  private:
    struct Rep
      {
	int width_;
	int height_;
	PixelFormat pixelFormat_;
	unsigned bpp_;
	unsigned bufSize_;
	int references_;
      public:
	/// Cr�e un Rep, avec de la place pour l'image.
	static Rep *create (int width, int height, PixelFormat pixelFormat);
	/// Copie un Rep, par r�f�rence (ne fait qu'incr�menter le nombre de
	/// r�f�rences).
	Rep *copyRef (void);
	/// Copie un Rep, vrai copie.
	Rep *copy (void);
	/// Lib�re le Rep, detruit si necessaire.
	void release (void);
	/// D�truit le Rep.
	void destroy (void);
	/// R�cup�re le buffer.
	uint8_t *getBuf (void);
      private:
	/// Constructeur, interdit.
	Rep (void);
	/// Constructeur de copie, interdit.
	Rep (const Rep &rep);
      };
    mutable Rep *rep_;
};

#include "image.tcc"

#endif // image_hh