8 #include <SDL2_image/SDL_image.h>
10 #include <SDL2/SDL_image.h>
19 SDL_Surface *surface= IMG_Load(filename);
22 printf(
"loading image '%s'... sdl_image failed.\n", filename);
27 const SDL_PixelFormat format= *surface->format;
28 if(format.BitsPerPixel != 24 && format.BitsPerPixel != 32)
30 printf(
"loading image '%s'... format failed. (bpp %d)\n", filename, format.BitsPerPixel);
31 SDL_FreeSurface(surface);
35 int width= surface->w;
36 int height= surface->h;
37 int channels= (format.BitsPerPixel == 32) ? 4 : 3;
39 Image image(surface->w, surface->h);
41 printf(
"loading image '%s' %dx%d %d channels...\n", filename, width, height, channels);
44 if(format.BitsPerPixel == 32)
47 for(
int y= height -1; y >= 0; y--, py++)
49 Uint8 *pixel= (Uint8 *) surface->pixels + py * surface->pitch;
51 for(
int x= 0; x < width; x++)
53 Uint8 r= pixel[format.Rshift / 8];
54 Uint8 g= pixel[format.Gshift / 8];
55 Uint8 b= pixel[format.Bshift / 8];
56 Uint8 a= pixel[format.Ashift / 8];
58 image(x, y)=
Color((
float) r / 255.f, (
float) g / 255.f, (
float) b / 255.f, (
float) a / 255.f);
59 pixel= pixel + format.BytesPerPixel;
64 else if(format.BitsPerPixel == 24)
67 for(
int y= height -1; y >= 0; y--, py++)
69 Uint8 *pixel= (Uint8 *) surface->pixels + py * surface->pitch;
71 for(
int x= 0; x < surface->w; x++)
73 const Uint8 r= pixel[format.Rshift / 8];
74 const Uint8 g= pixel[format.Gshift / 8];
75 const Uint8 b= pixel[format.Bshift / 8];
77 image(x, y)=
Color((
float) r / 255.f, (
float) g / 255.f, (
float) b / 255.f);
78 pixel= pixel + format.BytesPerPixel;
83 SDL_FreeSurface(surface);
90 if(std::string(filename).rfind(
".png") == std::string::npos && std::string(filename).rfind(
".bmp") == std::string::npos )
92 printf(
"writing color image '%s'... failed, not a .png / .bmp image.\n", filename);
97 std::vector<Uint8> flip(image.
width() * image.
height() * 4);
100 for(
int y= 0; y < image.
height(); y++)
101 for(
int x= 0; x < image.
width(); x++)
104 Uint8 r= (Uint8) std::min(std::floor(color.r * 255.f), 255.f);
105 Uint8 g= (Uint8) std::min(std::floor(color.g * 255.f), 255.f);
106 Uint8 b= (Uint8) std::min(std::floor(color.b * 255.f), 255.f);
107 Uint8 a= (Uint8) std::min(std::floor(color.a * 255.f), 255.f);
116 SDL_Surface *surface= SDL_CreateRGBSurfaceFrom((
void *) &flip.front(), image.
width(), image.
height(),
117 32, image.
width() * 4,
132 if(std::string(filename).rfind(
".png") != std::string::npos)
133 code= IMG_SavePNG(surface, filename);
134 else if(std::string(filename).rfind(
".bmp") != std::string::npos)
135 code= SDL_SaveBMP(surface, filename);
137 SDL_FreeSurface(surface);
139 printf(
"writing color image '%s'... failed\n%s\n", filename, SDL_GetError());
147 SDL_Surface *surface= IMG_Load(filename);
150 printf(
"loading image '%s'... sdl_image failed.\n", filename);
155 const SDL_PixelFormat format= *surface->format;
156 if(format.BitsPerPixel != 24 && format.BitsPerPixel != 32)
158 printf(
"loading image '%s'... format failed. (bpp %d)\n", filename, format.BitsPerPixel);
159 SDL_FreeSurface(surface);
163 int width= surface->w;
164 int height= surface->h;
165 int channels= (format.BitsPerPixel == 32) ? 4 : 3;
167 ImageData image(width, height, channels);
169 printf(
"loading image '%s' %dx%d %d channels...\n", filename, width, height, channels);
172 if(format.BitsPerPixel == 32)
175 for(
int y= height -1; y >= 0; y--, py++)
177 Uint8 *pixel= (Uint8 *) surface->pixels + py * surface->pitch;
179 for(
int x= 0; x < width; x++)
181 Uint8 r= pixel[format.Rshift / 8];
182 Uint8 g= pixel[format.Gshift / 8];
183 Uint8 b= pixel[format.Bshift / 8];
184 Uint8 a= pixel[format.Ashift / 8];
186 std::size_t offset= image.offset(x, y);
187 image.data[offset]= r;
188 image.data[offset +1]= g;
189 image.data[offset +2]= b;
190 image.data[offset +3]= a;
191 pixel= pixel + format.BytesPerPixel;
196 else if(format.BitsPerPixel == 24)
199 for(
int y= height -1; y >= 0; y--, py++)
201 Uint8 *pixel= (Uint8 *) surface->pixels + py * surface->pitch;
203 for(
int x= 0; x < surface->w; x++)
205 const Uint8 r= pixel[format.Rshift / 8];
206 const Uint8 g= pixel[format.Gshift / 8];
207 const Uint8 b= pixel[format.Bshift / 8];
209 std::size_t offset= image.offset(x, y);
210 image.data[offset]= r;
211 image.data[offset +1]= g;
212 image.data[offset +2]= b;
213 pixel= pixel + format.BytesPerPixel;
218 SDL_FreeSurface(surface);
224 if(std::string(filename).rfind(
".png") == std::string::npos && std::string(filename).rfind(
".bmp") == std::string::npos )
226 printf(
"writing color image '%s'... failed, not a .png / .bmp image.\n", filename);
232 printf(
"writing color image '%s'... failed, not an 8 bits image.\n", filename);
237 std::vector<Uint8> flip(image.width * image.height * 4);
240 for(
int y= 0; y < image.height; y++)
241 for(
int x= 0; x < image.width; x++)
243 std::size_t offset= image.offset(x, image.height - y -1);
244 Uint8 r= image.data[offset];
245 Uint8 g= image.data[offset +1];
246 Uint8 b= image.data[offset +2];
248 if(image.channels > 3)
249 a= image.data[offset +3];
259 SDL_Surface *surface= SDL_CreateRGBSurfaceFrom((
void *) &flip.front(), image.width, image.height,
276 if(std::string(filename).rfind(
".png") != std::string::npos)
277 code= IMG_SavePNG(surface, filename);
278 else if(std::string(filename).rfind(
".bmp") != std::string::npos)
279 code= SDL_SaveBMP(surface, filename);
281 SDL_FreeSurface(surface);
283 printf(
"writing color image '%s'... failed\n%s\n", filename, SDL_GetError());
ImageData read_image_data(const char *filename)
charge les donnees d'un fichier png. renvoie une image initialisee par defaut en cas d'echec...
stockage temporaire des donnees d'une image.
representation d'une couleur (rgba) transparente ou opaque.
int write_image_data(ImageData &image, const char *filename)
enregistre des donnees dans un fichier png.
int height() const
renvoie la hauteur de l'image.
Image read_image(const char *filename)
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().
int write_image(const Image &image, const char *filename)
enregistre une image dans un fichier png.
representation d'une image.
int width() const
renvoie la largeur de l'image.