8 #include <SDL2_image/SDL_image.h>
9 #include <SDL2_image/SDL_surface.h>
11 #include <SDL2/SDL_image.h>
12 #include <SDL2/SDL_surface.h>
21 SDL_Surface *surface= IMG_Load(filename);
24 printf(
"[error] loading image '%s'... sdl_image failed.\n", filename);
29 const SDL_PixelFormat format= *surface->format;
30 int width= surface->w;
31 int height= surface->h;
32 int channels= format.BitsPerPixel / 8;
34 printf(
"loading image '%s' %dx%d %d channels...\n", filename, width, height, channels);
36 Image image(surface->w, surface->h);
38 if(format.BitsPerPixel == 32)
41 for(
int y= height -1; y >= 0; y--, py++)
43 Uint8 *pixel= (Uint8 *) surface->pixels + py * surface->pitch;
45 for(
int x= 0; x < width; x++)
47 Uint8 r= pixel[format.Rshift / 8];
48 Uint8 g= pixel[format.Gshift / 8];
49 Uint8 b= pixel[format.Bshift / 8];
50 Uint8 a= pixel[format.Ashift / 8];
52 image(x, y)=
Color((
float) r / 255.f, (
float) g / 255.f, (
float) b / 255.f, (
float) a / 255.f);
53 pixel= pixel + format.BytesPerPixel;
61 for(
int y= height -1; y >= 0; y--, py++)
63 Uint8 *pixel= (Uint8 *) surface->pixels + py * surface->pitch;
65 for(
int x= 0; x < surface->w; x++)
70 if(format.BitsPerPixel >= 8) { r= pixel[format.Rshift / 8]; g= r; b= r; }
71 if(format.BitsPerPixel >= 16) { g= pixel[format.Gshift / 8]; b= 0; }
72 if(format.BitsPerPixel >= 24) { b= pixel[format.Bshift / 8]; }
74 image(x, y)=
Color((
float) r / 255.f, (
float) g / 255.f, (
float) b / 255.f);
75 pixel= pixel + format.BytesPerPixel;
80 SDL_FreeSurface(surface);
87 if(std::string(filename).rfind(
".png") == std::string::npos && std::string(filename).rfind(
".bmp") == std::string::npos )
89 printf(
"[error] writing color image '%s'... not a .png / .bmp image.\n", filename);
94 std::vector<Uint8> flip(image.
width() * image.
height() * 4);
97 for(
int y= 0; y < image.
height(); y++)
98 for(
int x= 0; x < image.
width(); x++)
101 Uint8 r= (Uint8)
std::min(std::floor(color.r * 255.f), 255.f);
102 Uint8 g= (Uint8)
std::min(std::floor(color.g * 255.f), 255.f);
103 Uint8 b= (Uint8)
std::min(std::floor(color.b * 255.f), 255.f);
104 Uint8 a= (Uint8)
std::min(std::floor(color.a * 255.f), 255.f);
113 SDL_Surface *surface= SDL_CreateRGBSurfaceFrom((
void *) &flip.front(), image.
width(), image.
height(),
114 32, image.
width() * 4,
129 if(std::string(filename).rfind(
".png") != std::string::npos)
130 code= IMG_SavePNG(surface, filename);
131 else if(std::string(filename).rfind(
".bmp") != std::string::npos)
132 code= SDL_SaveBMP(surface, filename);
134 SDL_FreeSurface(surface);
136 printf(
"[error] writing color image '%s'...\n%s\n", filename, SDL_GetError());
150 SDL_PixelFormat format= *surface->format;
152 int width= surface->w;
153 int height= surface->h;
154 int channels= format.BitsPerPixel / 8;
156 if(channels < 3) channels= 3;
157 ImageData image(width, height, channels);
162 if(format.BitsPerPixel == 32)
165 for(
int y= height -1; y >= 0; y--, py++)
167 Uint8 *pixel= (Uint8 *) surface->pixels + py * surface->pitch;
169 for(
int x= 0; x < width; x++)
171 Uint8 r= pixel[format.Rshift / 8];
172 Uint8 g= pixel[format.Gshift / 8];
173 Uint8 b= pixel[format.Bshift / 8];
174 Uint8 a= pixel[format.Ashift / 8];
176 std::size_t offset= image.offset(x, y);
177 image.pixels[offset]= r;
178 image.pixels[offset +1]= g;
179 image.pixels[offset +2]= b;
180 image.pixels[offset +3]= a;
181 pixel= pixel + format.BytesPerPixel;
189 for(
int y= height -1; y >= 0; y--, py++)
191 Uint8 *pixel= (Uint8 *) surface->pixels + py * surface->pitch;
193 for(
int x= 0; x < surface->w; x++)
199 if(format.BitsPerPixel >= 8) { r= pixel[format.Rshift / 8]; g= r; b= r; }
200 if(format.BitsPerPixel >= 16) { g= pixel[format.Gshift / 8]; b= 0; }
201 if(format.BitsPerPixel >= 24) { b= pixel[format.Bshift / 8]; }
203 std::size_t offset= image.offset(x, y);
204 image.pixels[offset]= r;
205 image.pixels[offset +1]= g;
206 image.pixels[offset +2]= b;
207 pixel= pixel + format.BytesPerPixel;
212 SDL_FreeSurface(surface);
219 SDL_Surface *surface= IMG_Load(filename);
222 printf(
"[error] loading image '%s'... sdl_image failed.\n%s\n", filename, SDL_GetError());
231 if(std::string(filename).rfind(
".png") == std::string::npos && std::string(filename).rfind(
".bmp") == std::string::npos )
233 printf(
"[error] writing color image '%s'... not a .png / .bmp image.\n", filename);
239 printf(
"[error] writing color image '%s'... not an 8 bits image.\n", filename);
244 std::vector<Uint8> flip(image.width * image.height * 4);
247 for(
int y= 0; y < image.height; y++)
248 for(
int x= 0; x < image.width; x++)
250 std::size_t offset= image.offset(x, image.height - y -1);
251 Uint8 r= image.pixels[offset];
252 Uint8 g= image.pixels[offset +1];
253 Uint8 b= image.pixels[offset +2];
255 if(image.channels > 3)
256 a= image.pixels[offset +3];
266 SDL_Surface *surface= SDL_CreateRGBSurfaceFrom((
void *) &flip.front(), image.width, image.height,
283 if(std::string(filename).rfind(
".png") != std::string::npos)
284 code= IMG_SavePNG(surface, filename);
285 else if(std::string(filename).rfind(
".bmp") != std::string::npos)
286 code= SDL_SaveBMP(surface, filename);
288 SDL_FreeSurface(surface);
290 printf(
"[error] writing color image '%s'...\n%s\n", filename, SDL_GetError());
300 for(
int y= 0; y < image.
height(); y++)
301 for(
int x= 0; x < image.
width(); x++)
303 size_t s= image.
offset(x, y);
316 for(
int y= 0; y < image.
height(); y++)
317 for(
int x= 0; x < image.
width(); x++)
319 size_t s= image.
offset(x, y);
328 Image copy(
const Image& image,
const int xmin,
const int ymin,
const int width,
const int height )
332 for(
int y= 0; y < height; y++)
333 for(
int x= 0; x < width; x++)
335 size_t s= image.
offset(xmin+x, ymin+y);
348 ImageData flip(image.width, image.height, image.channels);
350 for(
int y= 0; y < image.height; y++)
351 for(
int x= 0; x < image.width; x++)
353 size_t s= image.offset(x, y);
354 size_t d= flip.offset(x, flip.height - y -1);
356 for(
int i= 0; i < image.channels; i++)
357 flip.pixels[d+i]= image.pixels[s+i];
365 ImageData flip(image.width, image.height, image.channels);
367 for(
int y= 0; y < image.height; y++)
368 for(
int x= 0; x < image.width; x++)
370 size_t s= image.offset(x, y);
371 size_t d= flip.offset(flip.width -x -1, y);
373 for(
int i= 0; i < image.channels; i++)
374 flip.pixels[d+i]= image.pixels[s+i];
384 for(
int y= 0; y < height; y++)
385 for(
int x= 0; x < width; x++)
387 size_t s= image.offset(xmin+x, ymin+y);
390 for(
int i= 0; i < image.channels; i++)
391 copy.pixels[d+i]= image.pixels[s+i];
401 for(
int y= 0; y < mip.height; y++)
402 for(
int x= 0; x < mip.width; x++)
404 size_t d= mip.offset(x, y);
405 for(
int i= 0; i < image.channels; i++)
407 image.pixels[image.offset(2*x, 2*y)+i]
408 + image.pixels[image.offset(2*x+1, 2*y)+i]
409 + image.pixels[image.offset(2*x, 2*y+1)+i]
410 + image.pixels[image.offset(2*x+1, 2*y+1)+i] ) / 4;
420 for(
int y= 0; y < mip.
height(); y++)
421 for(
int x= 0; x < mip.
width(); x++)
422 mip(x, y)= (image(2*x, 2*y) + image(2*x+1, 2*y)
423 + image(2*x, 2*y+1) + image(2*x+1, 2*y+1)) / 4;
433 for(
unsigned i= 0; i < image.
size(); i++)
434 tmp(i)=
srgb(image(i));
444 for(
unsigned i= 0; i < image.
size(); i++)
representation d'une image.
int height() const
renvoie la hauteur de l'image.
unsigned size() const
renvoie le nombre de pixels de l'image.
unsigned offset(const int x, const int y) const
renvoie l'indice du pixel.
int width() const
renvoie la largeur de l'image.
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().
ImageData read_image_data(const char *filename)
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)
enregistre des donnees dans un fichier png.
ImageData image_data(SDL_Surface *surface)
converti une surface SDL en imageData, cf RWops pour charger les images deja en memoire.
int write_image(const Image &image, const char *filename)
enregistre une image dans un fichier png.
Image flipY(const Image &image)
retourne l'image
Image flipX(const Image &image)
retourne l'image
Image copy(const Image &image, const int xmin, const int ymin, const int width, const int height)
renvoie un bloc de l'image
Image linear(const Image &image)
conversion de srgb vers rgb lineaire. necessaire pour les images couleurs.
ImageData downscale(const ImageData &image)
renvoie une image filtree plus petite.
Image read_image(const char *filename)
Image srgb(const Image &image)
conversion de rgb lineaire vers srgb.
Point max(const Point &a, const Point &b)
renvoie la plus grande composante de chaque point. x, y, z= max(a.x, b.x), max(a.y,...
Point min(const Point &a, const Point &b)
renvoie la plus petite composante de chaque point. x, y, z= min(a.x, b.x), min(a.y,...
representation d'une couleur (rgba) transparente ou opaque.
stockage temporaire des donnees d'une image.