gKit2 light
image.h
Go to the documentation of this file.
1 
2 #ifndef _IMAGE_H
3 #define _IMAGE_H
4 
5 #include <cmath>
6 #include <cassert>
7 #include <vector>
8 #include <algorithm>
9 
10 #include "color.h"
11 
12 
15 
18 
20 class Image
21 {
22 protected:
23  std::vector<Color> m_pixels;
24  int m_width;
25  int m_height;
26 
27 public:
28  Image( ) : m_pixels(), m_width(0), m_height(0) {}
29  Image( const int w, const int h, const Color& color= Black() ) : m_pixels(w*h, color), m_width(w), m_height(h) {}
30 
40  Color& operator() ( const int x, const int y )
41  {
42  return m_pixels[offset(x, y)];
43  }
44 
46  Color operator() ( const int x, const int y ) const
47  {
48  return m_pixels[offset(x, y)];
49  }
50 
51  Color& operator() ( const unsigned offset )
52  {
53  assert(offset < m_pixels.size());
54  return m_pixels[offset];
55  }
56 
57  Color operator() ( const unsigned offset ) const
58  {
59  assert(offset < m_pixels.size());
60  return m_pixels[offset];
61  }
62 
64  Color sample( const float x, const float y ) const
65  {
66  // interpolation bilineaire
67  float u= x - std::floor(x);
68  float v= y - std::floor(y);
69  int ix= x;
70  int iy= y;
71  return (*this)(ix, iy) * ((1 - u) * (1 - v))
72  + (*this)(ix+1, iy) * (u * (1 - v))
73  + (*this)(ix, iy+1) * ((1 - u) * v)
74  + (*this)(ix+1, iy+1) * (u * v);
75  }
76 
78  Color texture( const float x, const float y ) const
79  {
80  return sample(x * m_width, y * m_height);
81  }
82 
84  const void *data( ) const
85  {
86  assert(!m_pixels.empty());
87  return &m_pixels.front();
88  }
89 
91  void *data( )
92  {
93  assert(!m_pixels.empty());
94  return &m_pixels.front();
95  }
96 
98  int width( ) const { return m_width; }
100  int height( ) const { return m_height; }
102  unsigned size( ) const { return m_width * m_height; }
103 
105  unsigned offset( const int x, const int y ) const
106  {
107  int px= x;
108  if(px < 0) px= 0;
109  if(px > m_width-1) px= m_width-1;
110  int py= y;
111  if(py < 0) py= 0;
112  if(py > m_height-1) py= m_height-1;
113 
114  assert(py * m_width + px < int(m_pixels.size()));
115  return py * m_width + px;
116  }
117 
126  static Image& error( )
127  {
128  static Image image;
129  return image;
130  }
131 
133  bool operator== ( const Image& im ) const
134  {
135  // renvoie vrai si im ou l'objet est la sentinelle
136  return (this == &im);
137  }
138 };
139 
141 #endif
representation d'une image.
Definition: image.h:21
int height() const
renvoie la hauteur de l'image.
Definition: image.h:100
void * data()
renvoie un pointeur sur le stockage des couleurs des pixels.
Definition: image.h:91
const void * data() const
renvoie un pointeur sur le stockage des couleurs des pixels.
Definition: image.h:84
Color & operator()(const int x, const int y)
Definition: image.h:40
unsigned size() const
renvoie le nombre de pixels de l'image.
Definition: image.h:102
bool operator==(const Image &im) const
comparaison avec la sentinelle.
Definition: image.h:133
Color texture(const float x, const float y) const
renvoie la couleur interpolee aux coordonnees normalisees (x, y) [0 .. 1]x[0 .. 1].
Definition: image.h:78
static Image & error()
Definition: image.h:126
unsigned offset(const int x, const int y) const
renvoie l'indice du pixel.
Definition: image.h:105
Color sample(const float x, const float y) const
renvoie la couleur interpolee a la position (x, y) [0 .. width]x[0 .. height].
Definition: image.h:64
int width() const
renvoie la largeur de l'image.
Definition: image.h:98
Color Black()
utilitaire. renvoie une couleur noire.
Definition: color.cpp:31
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:14