gKit3
Loading...
Searching...
No Matches
Files | Classes | Functions
utilitaires pour manipuler des images

Files

file  color.h
 
file  image.h
 

Classes

struct  Color
 representation d'une couleur (rgba) transparente ou opaque. More...
 
class  Image
 representation d'une image. More...
 

Functions

Color Black ()
 utilitaire. renvoie une couleur noire.
 
Color White ()
 utilitaire. renvoie une couleur blanche.
 
Color Red ()
 utilitaire. renvoie une couleur rouge.
 
Color Green ()
 utilitaire. renvoie une couleur verte.
 
Color Blue ()
 utilitaire. renvoie une couleur bleue.
 
Color Yellow ()
 utilitaire. renvoie une couleur jaune.
 
Color operator+ (const Color &a, const Color &b)
 
Color operator- (const Color &a, const Color &b)
 
Color operator- (const Color &c)
 
Color operator* (const Color &a, const Color &b)
 
Color operator* (const Color &c, const float k)
 
Color operator* (const float k, const Color &c)
 
Color operator/ (const Color &a, const Color &b)
 
Color operator/ (const float k, const Color &c)
 
Color operator/ (const Color &c, const float k)
 
Color operator+ (const float k, const Color &c)
 
Color operator+ (const Color &c, const float k)
 
Color srgb (const Color &color, const float g=float(2.2))
 transformation couleur : rgb lineaire vers srgb
 
Color linear (const Color &color, const float g=float(2.2))
 transformation couleur : srgb vers rgb lineaire
 
Color abs (const Color &color)
 
Image read_image (const char *filename, const bool flipY=true)
 charge une image .bmp .tga .jpeg .png ou .hdr
 
bool write_image (const Image &image, const char *filename, const bool flipY=true)
 enregistre une image au format .png
 
bool write_image_png (const Image &image, const char *filename, const bool flipY=true)
 enregistre une image au format .png
 
bool write_image_bmp (const Image &image, const char *filename, const bool flipY=true)
 enregistre une image au format .bmp
 
bool write_image_hdr (const Image &image, const char *filename, const bool flipY=true)
 enregistre une image au format .hdr
 
bool write_image_preview (const Image &image, const char *filename, const bool flipY=true)
 raccourci pour write_image_png(tone(image, range(image)), "image.png")
 
Image srgb (const Image &image)
 transformation couleur : rgb lineaire vers srgb
 
Image linear (const Image &image)
 transformation couleur : srgb vers rgb lineaire
 
float range (const Image &image)
 evalue l'exposition d'une image.
 
Image tone (const Image &image, const float saturation)
 correction de l'exposition d'une image + transformation gamma.
 

Detailed Description

Function Documentation

◆ Black()

Color Black ( )

utilitaire. renvoie une couleur noire.

Definition at line 18 of file color.cpp.

19{
20 return Color(0, 0, 0);
21}
representation d'une couleur (rgba) transparente ou opaque.
Definition color.h:14

◆ White()

Color White ( )

utilitaire. renvoie une couleur blanche.

Definition at line 23 of file color.cpp.

24{
25 return Color(1, 1, 1);
26}

◆ Red()

Color Red ( )

utilitaire. renvoie une couleur rouge.

Definition at line 28 of file color.cpp.

29{
30 return Color(1, 0, 0);
31}

◆ Green()

Color Green ( )

utilitaire. renvoie une couleur verte.

Definition at line 33 of file color.cpp.

34{
35 return Color(0, 1, 0);
36}

◆ Blue()

Color Blue ( )

utilitaire. renvoie une couleur bleue.

Definition at line 38 of file color.cpp.

39{
40 return Color(0, 0, 1);
41}

◆ Yellow()

Color Yellow ( )

utilitaire. renvoie une couleur jaune.

Definition at line 43 of file color.cpp.

44{
45 return Color(1, 1, 0);
46}

◆ operator+() [1/3]

Color operator+ ( const Color a,
const Color b 
)

Definition at line 73 of file color.cpp.

74{
75 return Color(a.r + b.r, a.g + b.g, a.b + b.b, a.a + b.a);
76}

◆ operator-() [1/2]

Color operator- ( const Color a,
const Color b 
)

Definition at line 83 of file color.cpp.

84{
85 return a + (-b);
86}

◆ operator-() [2/2]

Color operator- ( const Color c)

Definition at line 78 of file color.cpp.

79{
80 return Color(-c.r, -c.g, -c.b, -c.a);
81}

◆ operator*() [1/3]

Color operator* ( const Color a,
const Color b 
)

Definition at line 88 of file color.cpp.

89{
90 return Color(a.r * b.r, a.g * b.g, a.b * b.b, a.a * b.a);
91}

◆ operator*() [2/3]

Color operator* ( const Color c,
const float  k 
)

Definition at line 98 of file color.cpp.

99{
100 return k * c;
101}

◆ operator*() [3/3]

Color operator* ( const float  k,
const Color c 
)

Definition at line 93 of file color.cpp.

94{
95 return Color(c.r * k, c.g * k, c.b * k, c.a * k);
96}

◆ operator/() [1/3]

Color operator/ ( const Color a,
const Color b 
)

Definition at line 103 of file color.cpp.

104{
105 return Color(a.r / b.r, a.g / b.g, a.b / b.b, a.a / b.a);
106}

◆ operator/() [2/3]

Color operator/ ( const float  k,
const Color c 
)

Definition at line 108 of file color.cpp.

109{
110 return Color(k / c.r, k / c.g, k / c.b, k / c.a);
111}

◆ operator/() [3/3]

Color operator/ ( const Color c,
const float  k 
)

Definition at line 113 of file color.cpp.

114{
115 float kk= 1 / k;
116 return kk * c;
117}

◆ operator+() [2/3]

Color operator+ ( const float  k,
const Color c 
)

Definition at line 119 of file color.cpp.

120{
121 return Color(k + c.r, k + c.g, k + c.b, k + c.a);
122}

◆ operator+() [3/3]

Color operator+ ( const Color c,
const float  k 
)

Definition at line 124 of file color.cpp.

125{
126 return k + c;
127}

◆ srgb() [1/2]

Color srgb ( const Color color,
const float  g = float(2.2) 
)

transformation couleur : rgb lineaire vers srgb

Definition at line 61 of file color.cpp.

62{
63 return Color(srgb(color.r), srgb(color.g), srgb(color.b), color.a);
64
65}

◆ linear() [1/2]

Color linear ( const Color color,
const float  g = float(2.2) 
)

transformation couleur : srgb vers rgb lineaire

Definition at line 67 of file color.cpp.

68{
69 return Color(linear(color.r), linear(color.g), linear(color.b), color.a);
70}

◆ abs()

Color abs ( const Color color)

Definition at line 129 of file color.cpp.

130{
131 return Color( std::abs(color.r), std::abs(color.g), std::abs(color.b), std::abs(color.a) );
132}

◆ read_image()

Image read_image ( const char *  filename,
const bool  flipY = true 
)

charge une image .bmp .tga .jpeg .png ou .hdr

Definition at line 94 of file image_io.cpp.

95{
96 stbi_ldr_to_hdr_scale(1.0f);
97 stbi_ldr_to_hdr_gamma(2.2f);
98 stbi_set_flip_vertically_on_load(flipY);
99
100 int width, height, channels;
101 float *data= stbi_loadf(filename, &width, &height, &channels, 4);
102 if(!data)
103 {
104 printf("[error] loading '%s'...\n", filename);
105 return {};
106 }
107
108 Image image(width, height);
109 for(unsigned i= 0, offset= 0; i < image.size(); i++, offset+= 4)
110 image(i)= Color( data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
111
112 stbi_image_free(data);
113 return image;
114}
representation d'une image.
Definition image.h:21

◆ write_image()

bool write_image ( const Image image,
const char *  filename,
const bool  flipY = true 
)

enregistre une image au format .png

Definition at line 143 of file image_io.cpp.

144{
145 return write_image_png(image, filename, flipY );
146}
bool write_image_png(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .png
Definition image_io.cpp:124

◆ write_image_png()

bool write_image_png ( const Image image,
const char *  filename,
const bool  flipY = true 
)

enregistre une image au format .png

Definition at line 124 of file image_io.cpp.

125{
126 if(image.size() == 0)
127 return false;
128
129 std::vector<unsigned char> tmp(image.width()*image.height()*4);
130 for(unsigned i= 0, offset= 0; i < image.size(); i++, offset+= 4)
131 {
132 Color pixel= image(i) * 255;
133 tmp[offset ]= clamp(pixel.r, 0, 255);
134 tmp[offset +1]= clamp(pixel.g, 0, 255);
135 tmp[offset +2]= clamp(pixel.b, 0, 255);
136 tmp[offset +3]= clamp(pixel.a, 0, 255);
137 }
138
139 stbi_flip_vertically_on_write(flipY);
140 return stbi_write_png(filename, image.width(), image.height(), 4, tmp.data(), image.width() * 4) != 0;
141}
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
int width() const
renvoie la largeur de l'image.
Definition image.h:100

◆ write_image_bmp()

bool write_image_bmp ( const Image image,
const char *  filename,
const bool  flipY = true 
)

enregistre une image au format .bmp

Definition at line 148 of file image_io.cpp.

149{
150 if(image.size() == 0)
151 return false;
152
153 std::vector<unsigned char> tmp(image.width()*image.height()*4);
154 for(unsigned i= 0, offset= 0; i < image.size(); i++, offset+= 4)
155 {
156 Color pixel= image(i) * 255;
157 tmp[offset ]= clamp(pixel.r, 0, 255);
158 tmp[offset +1]= clamp(pixel.g, 0, 255);
159 tmp[offset +2]= clamp(pixel.b, 0, 255);
160 tmp[offset +3]= clamp(pixel.a, 0, 255);
161 }
162
163 stbi_flip_vertically_on_write(flipY);
164 return stbi_write_bmp(filename, image.width(), image.height(), 4, tmp.data()) != 0;
165}

◆ write_image_hdr()

bool write_image_hdr ( const Image image,
const char *  filename,
const bool  flipY = true 
)

enregistre une image au format .hdr

Definition at line 167 of file image_io.cpp.

168{
169 if(image.size() == 0)
170 return false;
171
172 stbi_flip_vertically_on_write(flipY);
173 return stbi_write_hdr(filename, image.width(), image.height(), 4, image.data()) != 0;
174}
const float * data() const
renvoie un const pointeur sur le stockage des couleurs des pixels.
Definition image.h:86

◆ write_image_preview()

bool write_image_preview ( const Image image,
const char *  filename,
const bool  flipY = true 
)

raccourci pour write_image_png(tone(image, range(image)), "image.png")

Definition at line 176 of file image_io.cpp.

177{
178 if(image.size() == 0)
179 return false;
180
181 Image tmp= tone(image, range(image));
182 return write_image_png(tmp, filename, flipY);
183}
float range(const Image &image)
evalue l'exposition d'une image.
Definition image_io.cpp:34
Image tone(const Image &image, const float saturation)
correction de l'exposition d'une image + transformation gamma.
Definition image_io.cpp:72

◆ srgb() [2/2]

Image srgb ( const Image image)

transformation couleur : rgb lineaire vers srgb

Definition at line 14 of file image_io.cpp.

15{
16 Image tmp(image.width(), image.height());
17
18 for(unsigned i= 0; i < image.size(); i++)
19 tmp(i)= srgb(image(i));
20
21 return tmp;
22}
Image srgb(const Image &image)
transformation couleur : rgb lineaire vers srgb
Definition image_io.cpp:14

◆ linear() [2/2]

Image linear ( const Image image)

transformation couleur : srgb vers rgb lineaire

Definition at line 24 of file image_io.cpp.

25{
26 Image tmp(image.width(), image.height());
27
28 for(unsigned i= 0; i < image.size(); i++)
29 tmp(i)= linear(image(i));
30
31 return tmp;
32}
Image linear(const Image &image)
transformation couleur : srgb vers rgb lineaire
Definition image_io.cpp:24

◆ range()

float range ( const Image image)

evalue l'exposition d'une image.

Definition at line 34 of file image_io.cpp.

35{
36 float gmin= FLT_MAX;
37 float gmax= 0;
38 for(unsigned i= 0; i < image.size(); i++)
39 {
40 Color color= image(i);
41 float g= color.r + color.g + color.b;
42
43 if(g < gmin) gmin= g;
44 if(g > gmax) gmax= g;
45 }
46
47 int bins[100] = {};
48 for(unsigned i= 0; i < image.size(); i++)
49 {
50 Color color= image(i);
51 float g= color.r + color.g + color.b;
52
53 int b= (g - gmin) * 100 / (gmax - gmin);
54 if(b >= 99) b= 99;
55 if(b < 0) b= 0;
56 bins[b]++;
57 }
58
59 float saturation= 0;
60 float qbins= 0;
61 for(unsigned i= 0; i < 100; i++)
62 {
63 qbins= qbins + float(bins[i]) / float(image.size());
64 if(qbins > .75f)
65 return gmin + float(i+1) / 100 * (gmax - gmin);
66 }
67
68 return gmax;
69}

◆ tone()

Image tone ( const Image image,
const float  saturation 
)

correction de l'exposition d'une image + transformation gamma.

Definition at line 72 of file image_io.cpp.

73{
74 Image tmp(image.width(), image.height());
75
76 float k= 1 / std::pow(saturation, 1 / 2.2f);
77 for(unsigned i= 0; i < image.size(); i++)
78 {
79 Color color= image(i);
80 if(std::isnan(color.r) || std::isnan(color.g) || std::isnan(color.b))
81 // marque les pixels pourris avec une couleur improbable...
82 color= Color(1, 0, 1);
83 else
84 // sinon transformation rgb -> srgb
85 color= k * srgb(color);
86
87 tmp(i)= Color(color, 1);
88 }
89
90 return tmp;
91}