gKit2 light
Loading...
Searching...
No Matches
utilitaires pour manipuler des images

Files

file  src/gKit/color.h
file  src/gKit/image.h
file  src/gKit/image_io.h

Classes

struct  Color
 representation d'une couleur (rgba) transparente ou opaque. More...
class  Image
 representation d'une image. More...
struct  ImageData
 stockage temporaire des donnees 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 srgb (const Color &color)
 transformation couleur : rgb lineaire vers srgb
Color linear (const Color &color)
 transformation couleur : srgb vers rgb lineaire
Color abs (const Color &color)
Image read_image (const char *filename, const bool flipY=true)
Image read_image_hdr (const char *filename, const bool flipY=true)
unsigned read_image_size (const char *filename)
 renvoie la taille en octets de l'image chargee. ou 0 si erreur.
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 mipmap (const Image &image)
 reduit une image.
Image srgb (const Image &image)
 transformation couleur : rgb lineaire vers srgb
Image linear (const Image &image)
 transformation couleur : srgb vers rgb lineaire
Image flipY (const Image &image)
 retourne l'image
Image flipX (const Image &image)
 retourne l'image
Image copy (const Image &image, const unsigned xmin, const unsigned ymin, const unsigned width, const unsigned height)
 renvoie un bloc de l'image
ImageData read_image_data (const char *filename, const bool flipY=true)
 charge les donnees d'un fichier png. renvoie une image initialisee par defaut en cas d'echec.
int write_image_data (ImageData &image, const char *filename, const bool flipY=true)
 enregistre des donnees dans un fichier png.
ImageData read_image_data (const void *buffer, const unsigned size, const bool flipY=true)
 charge les donnees d'un fichier png stocke en memoire. renvoie une image initialisee par defaut en cas d'echec.
ImageData flipY (const ImageData &image)
 retourne l'image
ImageData flipX (const ImageData &image)
 retourne l'image
ImageData copy (const ImageData &image, const unsigned xmin, const unsigned ymin, const unsigned width, const unsigned height)
 renvoie un bloc de l'image
ImageData mipmap (const ImageData &image)
 renvoie une image filtree plus petite.
int miplevels (const int width, const int height)
 renvoie le nombre de mipmap d'une image width x height.

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+()

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}

◆ srgb() [1/2]

Color srgb ( const Color & color)

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)

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 a partir d'un fichier. renvoie Image::error() en cas d'echec. a detruire avec image::release( ).

Parameters
filemanenom de l'image a charger

Definition at line 171 of file image_io.cpp.

172{
173 stbi_ldr_to_hdr_scale(1);
174 stbi_ldr_to_hdr_gamma(1);
175 stbi_set_flip_vertically_on_load(flipY);
176
177 int width, height, channels;
178 float *data= stbi_loadf(filename, &width, &height, &channels, 4);
179 if(!data)
180 {
181 printf("[error] loading '%s'...\n", filename);
182 return {};
183 }
184
185 Image image(width, height);
186 for(unsigned i= 0, offset= 0; i < image.size(); i++, offset+= 4)
187 image(i)= Color( data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
188
189 stbi_image_free(data);
190 return image;
191}
representation d'une image.
Definition image.h:21
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition text.cpp:140
Image flipY(const Image &image)
retourne l'image
Definition image_io.cpp:112

◆ read_image_hdr()

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

Definition at line 193 of file image_io.cpp.

194{
195 return read_image(filename, flipY );
196}
Image read_image(const char *filename, const bool flipY)
Definition image_io.cpp:171

◆ read_image_size()

unsigned read_image_size ( const char * filename)

renvoie la taille en octets de l'image chargee. ou 0 si erreur.

Definition at line 162 of file image_io.cpp.

163{
164 int w, h, c;
165 if(!stbi_info(filename, &w, &h, &c))
166 return 0;
167
168 return sizeof(Color)*w*h;
169}

◆ write_image()

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

enregistre une image au format .png

Definition at line 225 of file image_io.cpp.

226{
227 return write_image_png(image, filename, flipY );
228}
bool write_image_png(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .png
Definition image_io.cpp:206

◆ 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 206 of file image_io.cpp.

207{
208 if(image.size() == 0)
209 return false;
210
211 std::vector<unsigned char> tmp(image.width()*image.height()*4);
212 for(unsigned i= 0, offset= 0; i < image.size(); i++, offset+= 4)
213 {
214 Color pixel= image(i) * 255;
215 tmp[offset ]= clamp(pixel.r, 0, 255);
216 tmp[offset +1]= clamp(pixel.g, 0, 255);
217 tmp[offset +2]= clamp(pixel.b, 0, 255);
218 tmp[offset +3]= clamp(pixel.a, 0, 255);
219 }
220
221 stbi_flip_vertically_on_write(flipY);
222 return stbi_write_png(filename, image.width(), image.height(), 4, tmp.data(), image.width() * 4) != 0;
223}

◆ 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 230 of file image_io.cpp.

231{
232 if(image.size() == 0)
233 return false;
234
235 std::vector<unsigned char> tmp(image.width()*image.height()*4);
236 for(unsigned i= 0, offset= 0; i < image.size(); i++, offset+= 4)
237 {
238 Color pixel= image(i) * 255;
239 tmp[offset ]= clamp(pixel.r, 0, 255);
240 tmp[offset +1]= clamp(pixel.g, 0, 255);
241 tmp[offset +2]= clamp(pixel.b, 0, 255);
242 tmp[offset +3]= clamp(pixel.a, 0, 255);
243 }
244
245 stbi_flip_vertically_on_write(flipY);
246 return stbi_write_bmp(filename, image.width(), image.height(), 4, tmp.data()) != 0;
247}

◆ 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 249 of file image_io.cpp.

250{
251 if(image.size() == 0)
252 return false;
253
254 stbi_flip_vertically_on_write(flipY);
255 return stbi_write_hdr(filename, image.width(), image.height(), 4, image.data()) != 0;
256}

◆ 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 258 of file image_io.cpp.

259{
260 if(image.size() == 0)
261 return false;
262
263 Image tmp= tone(image, range(image));
264 return write_image_png(tmp, filename, flipY);
265}

◆ mipmap() [1/2]

Image mipmap ( const Image & image)

reduit une image.

Definition at line 94 of file image_io.cpp.

95{
96 unsigned w= std::max(unsigned(1), image.width() / 2);
97 unsigned h= std::max(unsigned(1), image.height() / 2);
98
99 Image tmp(w, h);
100
101 for(unsigned py= 0; py < h; py++)
102 for(unsigned px= 0; px < w; px++)
103 {
104 unsigned x= 2*px;
105 unsigned y= 2*py;
106 tmp(px, py)= (image(x, y) + image(x +1, y) + image(x +1, y +1) + image(x, y +1)) / 4;
107 }
108
109 return tmp;
110}

◆ srgb() [2/2]

Image srgb ( const Image & image)

transformation couleur : rgb lineaire vers srgb

Definition at line 15 of file image_io.cpp.

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

◆ linear() [2/2]

Image linear ( const Image & image)

transformation couleur : srgb vers rgb lineaire

Definition at line 25 of file image_io.cpp.

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

◆ flipY() [1/2]

Image flipY ( const Image & image)

retourne l'image

Definition at line 112 of file image_io.cpp.

113{
114 // flip de l'image : origine en haut a gauche
115 Image flip(image.width(), image.height());
116
117 for(unsigned y= 0; y < image.height(); y++)
118 for(unsigned x= 0; x < image.width(); x++)
119 {
120 unsigned s= image.offset(x, y);
121 unsigned d= flip.offset(x, flip.height() - y -1);
122
123 flip(d)= image(s);
124 }
125
126 return flip;
127}

◆ flipX() [1/2]

Image flipX ( const Image & image)

retourne l'image

Definition at line 129 of file image_io.cpp.

130{
131 Image flip(image.width(), image.height());
132
133 for(unsigned y= 0; y < image.height(); y++)
134 for(unsigned x= 0; x < image.width(); x++)
135 {
136 unsigned s= image.offset(x, y);
137 unsigned d= flip.offset(flip.width() -x -1, y);
138
139 flip(d)= image(s);
140 }
141
142 return flip;
143}

◆ copy() [1/2]

Image copy ( const Image & image,
const unsigned xmin,
const unsigned ymin,
const unsigned width,
const unsigned height )

renvoie un bloc de l'image

Definition at line 145 of file image_io.cpp.

146{
147 Image copy(width, height);
148
149 for(unsigned y= 0; y < height; y++)
150 for(unsigned x= 0; x < width; x++)
151 {
152 unsigned s= image.offset(xmin+x, ymin+y);
153 unsigned d= copy.offset(x, y);
154
155 copy(d)= image(s);
156 }
157
158 return copy;
159}
Image copy(const Image &image, const unsigned xmin, const unsigned ymin, const unsigned width, const unsigned height)
renvoie un bloc de l'image
Definition image_io.cpp:145

◆ read_image_data() [1/2]

ImageData read_image_data ( const char * filename,
const bool flipY = true )

charge les donnees d'un fichier png. renvoie une image initialisee par defaut en cas d'echec.

Definition at line 330 of file image_io.cpp.

331{
332 stbi_set_flip_vertically_on_load(flipY);
333
334 int width, height, channels;
335 unsigned char *data= stbi_load(filename, &width, &height, &channels, 0);
336 if(!data)
337 {
338 printf("[error] loading '%s'...\n", filename);
339 return {};
340 }
341
342 return image_data(data, width, height, channels);
343}

◆ write_image_data()

int write_image_data ( ImageData & image,
const char * filename,
const bool flipY = true )

enregistre des donnees dans un fichier png.

Definition at line 346 of file image_io.cpp.

347{
348 if(image.size != 1)
349 {
350 printf("[error] writing color image '%s'... not an 8 bits image.\n", filename);
351 return -1;
352 }
353
354 stbi_flip_vertically_on_write(flipY);
355
356 int code= 0;
357 if(std::string(filename).rfind(".png") != std::string::npos)
358 code= stbi_write_png(filename, image.width, image.height, image.channels, image.pixels.data(), image.width * image.channels) != 0;
359 else if(std::string(filename).rfind(".bmp") != std::string::npos)
360 code= stbi_write_bmp(filename, image.width, image.height, image.channels, image.pixels.data()) != 0;
361
362 else
363 {
364 printf("[error] writing color image '%s'... not a .png / .bmp image.\n", filename);
365 return -1;
366 }
367
368 if(code > 0)
369 return 0;
370
371 printf("[error] writing color image '%s'...\n", filename);
372 return -1;
373}

◆ read_image_data() [2/2]

ImageData read_image_data ( const void * buffer,
const unsigned size,
const bool flipY = true )

charge les donnees d'un fichier png stocke en memoire. renvoie une image initialisee par defaut en cas d'echec.

Definition at line 315 of file image_io.cpp.

316{
317 stbi_set_flip_vertically_on_load(flipY);
318
319 int width, height, channels;
320 unsigned char *data= stbi_load_from_memory((const unsigned char *) buffer, size, &width, &height, &channels, 0);
321 if(!data)
322 {
323 printf("[error] reading buffer image...\n");
324 return {};
325 }
326
327 return image_data(data, width, height, channels);
328}

◆ flipY() [2/2]

ImageData flipY ( const ImageData & image)

retourne l'image

Definition at line 376 of file image_io.cpp.

377{
378 // flip de l'image : origine en haut a gauche
379 ImageData flip(image.width, image.height, image.channels);
380
381 for(unsigned y= 0; y < image.height; y++)
382 for(unsigned x= 0; x < image.width; x++)
383 {
384 unsigned s= image.offset(x, y);
385 unsigned d= flip.offset(x, flip.height - y -1);
386
387 for(unsigned i= 0; i < image.channels; i++)
388 flip.pixels[d +i]= image.pixels[s +i];
389 }
390
391 return flip;
392}
stockage temporaire des donnees d'une image.
Definition image_io.h:53

◆ flipX() [2/2]

ImageData flipX ( const ImageData & image)

retourne l'image

Definition at line 394 of file image_io.cpp.

395{
396 ImageData flip(image.width, image.height, image.channels);
397
398 for(unsigned y= 0; y < image.height; y++)
399 for(unsigned x= 0; x < image.width; x++)
400 {
401 unsigned s= image.offset(x, y);
402 unsigned d= flip.offset(flip.width -x -1, y);
403
404 for(unsigned i= 0; i < image.channels; i++)
405 flip.pixels[d +i]= image.pixels[s +i];
406 }
407
408 return flip;
409}

◆ copy() [2/2]

ImageData copy ( const ImageData & image,
const unsigned xmin,
const unsigned ymin,
const unsigned width,
const unsigned height )

renvoie un bloc de l'image

Definition at line 411 of file image_io.cpp.

412{
413 ImageData copy(width, height, image.channels);
414
415 for(unsigned y= 0; y < height; y++)
416 for(unsigned x= 0; x < width; x++)
417 {
418 unsigned s= image.offset(xmin +x, ymin +y);
419 unsigned d= copy.offset(x, y);
420
421 for(unsigned i= 0; i < image.channels; i++)
422 copy.pixels[d +i]= image.pixels[s +i];
423 }
424
425 return copy;
426}

◆ mipmap() [2/2]

ImageData mipmap ( const ImageData & image)

renvoie une image filtree plus petite.

◆ miplevels()

int miplevels ( const int width,
const int height )

renvoie le nombre de mipmap d'une image width x height.

Definition at line 453 of file image_io.cpp.

454{
455 int w= width;
456 int h= height;
457 int levels= 1;
458 while(w > 1 || h > 1)
459 {
460 w= std::max(1, w / 2);
461 h= std::max(1, h / 2);
462 levels= levels + 1;
463 }
464
465 return levels;
466}