gKit3
Loading...
Searching...
No Matches
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
20class Image
21{
22protected:
23 std::vector<Color> m_pixels;
24 int m_width;
25 int m_height;
26
27public:
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
52 Color& operator() ( const size_t offset )
53 {
54 assert(offset < m_pixels.size());
55 return m_pixels[offset];
56 }
57
59 Color operator() ( const size_t offset ) const
60 {
61 assert(offset < m_pixels.size());
62 return m_pixels[offset];
63 }
64
66 Color sample( const float x, const float y ) const
67 {
68 // interpolation bilineaire
69 float u= x - std::floor(x);
70 float v= y - std::floor(y);
71 int ix= x;
72 int iy= y;
73 return (*this)(ix, iy) * ((1 - u) * (1 - v))
74 + (*this)(ix+1, iy) * (u * (1 - v))
75 + (*this)(ix, iy+1) * ((1 - u) * v)
76 + (*this)(ix+1, iy+1) * (u * v);
77 }
78
80 Color texture( const float x, const float y ) const
81 {
82 return sample(x * m_width, y * m_height);
83 }
84
86 const float *data( ) const
87 {
88 assert(!m_pixels.empty());
89 return (const float *) m_pixels.data();
90 }
91
93 float *data( )
94 {
95 assert(!m_pixels.empty());
96 return (float *) m_pixels.data();
97 }
98
100 int width( ) const { return m_width; }
102 int height( ) const { return m_height; }
104 unsigned size( ) const { return m_width * m_height; }
105
108 unsigned offset( const int x, const int y ) const
109 {
110 int px= x;
111 if(px < 0) px= 0;
112 if(px > m_width-1) px= m_width-1;
113 int py= y;
114 if(py < 0) py= 0;
115 if(py > m_height-1) py= m_height-1;
116
117 unsigned p= py * m_width + px;
118 assert(p < m_pixels.size());
119 return p;
120 }
121};
122
124#endif
representation d'une image.
Definition image.h:21
int height() const
renvoie la hauteur de l'image.
Definition image.h:102
unsigned size() const
renvoie le nombre de pixels de l'image.
Definition image.h:104
const float * data() const
renvoie un const pointeur sur le stockage des couleurs des pixels.
Definition image.h:86
float * data()
renvoie un pointeur sur le stockage des couleurs des pixels.
Definition image.h:93
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:80
Color & operator()(const int x, const int y)
Definition image.h:40
unsigned offset(const int x, const int y) const
Definition image.h:108
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:66
int width() const
renvoie la largeur de l'image.
Definition image.h:100
Color Black()
utilitaire. renvoie une couleur noire.
Definition color.cpp:18
representation d'une couleur (rgba) transparente ou opaque.
Definition color.h:14