42 IResource(createUniqueImageName(), loader),
82 SDL_Surface* surface = SDL_CreateRGBSurface(0, width,height, 32,
84 SDL_LockSurface(surface);
88 std::copy(data, data + size, pixeldata);
89 SDL_UnlockSurface(surface);
99 SDL_Surface* surface = SDL_CreateRGBSurface(0, width,height, 32,
101 SDL_LockSurface(surface);
105 std::copy(data, data + size, pixeldata);
106 SDL_UnlockSurface(surface);
186 int32_t bpp =
m_surface->format->BytesPerPixel;
209 pixel = *(Uint16 *)p;
213 if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
214 pixel = p[0] << 16 | p[1] << 8 | p[2];
216 pixel = p[0] | p[1] << 8 | p[2] << 16;
221 pixel = *(Uint32 *)p;
224 SDL_GetRGBA(pixel,
m_surface->format, r, g, b, a);
236 png_bytep *rowpointers = NULL;
238 fp = fopen(filename.c_str(),
"wb");
245 pngptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
247 if (pngptr == NULL) {
253 infoptr = png_create_info_struct(pngptr);
254 if (infoptr == NULL) {
256 png_destroy_write_struct(&pngptr, (png_infopp)NULL);
260 if (setjmp(png_jmpbuf(pngptr))) {
261 png_destroy_write_struct(&pngptr, &infoptr);
267 png_init_io(pngptr, fp);
270 SDL_LockSurface(const_cast<SDL_Surface*>(&surface));
272 colortype = PNG_COLOR_TYPE_RGB;
273 if(surface.format->palette){
274 colortype |= PNG_COLOR_TYPE_PALETTE;
276 else if (surface.format->Amask){
277 colortype |= PNG_COLOR_TYPE_RGB_ALPHA;
281 png_set_IHDR(pngptr, infoptr, surface.w, surface.h, 8, colortype,
282 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
284 png_write_info(pngptr, infoptr);
285 png_set_packing(pngptr);
287 rowpointers =
new png_bytep[surface.h];
288 for (int32_t i = 0; i < surface.h; i++) {
289 rowpointers[i] = (png_bytep)(Uint8 *)surface.pixels + i*surface.pitch;
292 png_write_image(pngptr, rowpointers);
293 png_write_end(pngptr, infoptr);
295 SDL_UnlockSurface(const_cast<SDL_Surface*>(&surface));
296 delete [] rowpointers;
297 png_destroy_write_struct(&pngptr, &infoptr);
304 static std::string baseName =
"image";
306 std::ostringstream oss;
307 oss << uniqueNumber <<
"_" << baseName;
309 const std::string name = oss.str();
323 SDL_SetSurfaceBlendMode(srcimg->
m_surface, SDL_BLENDMODE_NONE);
327 static_cast<Sint16
>(rect.
x + xoffset),
328 static_cast<Sint16>(rect.
y + yoffset),
329 static_cast<Uint16
>(srcimg->
getWidth()),
330 static_cast<Uint16>(srcimg->
getHeight()) };
334 static_cast<Sint16
>(rect.
x),
335 static_cast<Sint16>(rect.
y),
336 static_cast<Uint16
>(rect.
w),
337 static_cast<Uint16>(rect.
h) };
344 static_cast<Sint16
>(xoffset),
345 static_cast<Sint16>(yoffset),
346 static_cast<Uint16
>(srcimg->
getWidth()),
347 static_cast<Uint16>(srcimg->
getHeight()) };
351 static_cast<Sint16
>(rect.
x),
352 static_cast<Sint16>(rect.
y),
353 static_cast<Uint16
>(rect.
w),
354 static_cast<Uint16>(rect.
h) };
361 SDL_SetSurfaceBlendMode(srcimg->
m_surface, SDL_BLENDMODE_BLEND);
365 if ((x < 0) || (x >= surface->w) || (y < 0) || (y >= surface->h)) {
369 int32_t bpp = surface->format->BytesPerPixel;
370 SDL_LockSurface(surface);
371 Uint8* p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp;
372 Uint32 pixel = SDL_MapRGBA(surface->format, r, g, b, a);
380 *(Uint16 *)p = pixel;
384 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
385 p[0] = (pixel >> 16) & 0xff;
386 p[1] = (pixel >> 8) & 0xff;
391 p[1] = (pixel >> 8) & 0xff;
392 p[2] = (pixel >> 16) & 0xff;
397 *(Uint32 *)p = pixel;
400 SDL_UnlockSurface(surface);
std::string createUniqueImageName()
Image(IResourceLoader *loader=0)
Constructor.
void reset(SDL_Surface *surface)
Resets the image to default values (including the x and y shift values), frees the current surface an...
void saveImage(const std::string &filename)
Saves the image using given filename.
static bool putPixel(SDL_Surface *surface, int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
T h
Height of the rectangle.
ImageLoader for some basic formats like jpeg, png etc.
SDL_Surface * detachSurface()
Removes underlying SDL_Surface from the image (if exists) and returns this.
static void saveAsPng(const std::string &filename, const SDL_Surface &surface)
Saves the SDL_Surface to png format.
void getPixelRGBA(int32_t x, int32_t y, uint8_t *r, uint8_t *g, uint8_t *b, uint8_t *a)
IResourceLoader * m_loader
virtual ~Image()
Destructor.
virtual void copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr &img)
Copies given image into this one with respect to given offsets.
uint32_t getHeight() const
virtual void load(IResource *res)
bool isSharedImage() const
Returns true if this image shares data with another one.
const Rect & getSubImageRect() const
Returns area of the image it occupies in the shared image.
virtual void load(IResource *resource)=0
T w
Width of the rectangle.
uint32_t getWidth() const