42 IResource(createUniqueImageName(), loader),
82 SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, width,height, 32,
84 SDL_LockSurface(surface);
88 std::copy(data, data + size, pixeldata);
89 SDL_UnlockSurface(surface);
99 SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, width,height, 32,
101 SDL_LockSurface(surface);
105 std::copy(data, data + size, pixeldata);
106 SDL_UnlockSurface(surface);
181 int32_t bpp =
m_surface->format->BytesPerPixel;
204 pixel = *(Uint16 *)p;
208 if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
209 pixel = p[0] << 16 | p[1] << 8 | p[2];
211 pixel = p[0] | p[1] << 8 | p[2] << 16;
216 pixel = *(Uint32 *)p;
219 SDL_GetRGBA(pixel,
m_surface->format, r, g, b, a);
231 png_bytep *rowpointers = NULL;
233 fp = fopen(filename.c_str(),
"wb");
240 pngptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
242 if (pngptr == NULL) {
248 infoptr = png_create_info_struct(pngptr);
249 if (infoptr == NULL) {
251 png_destroy_write_struct(&pngptr, (png_infopp)NULL);
255 if (setjmp(png_jmpbuf(pngptr))) {
256 png_destroy_write_struct(&pngptr, &infoptr);
262 png_init_io(pngptr, fp);
265 SDL_LockSurface(const_cast<SDL_Surface*>(&surface));
267 colortype = PNG_COLOR_TYPE_RGB;
268 if(surface.format->palette){
269 colortype |= PNG_COLOR_TYPE_PALETTE;
271 else if (surface.format->Amask){
272 colortype |= PNG_COLOR_TYPE_RGB_ALPHA;
276 png_set_IHDR(pngptr, infoptr, surface.w, surface.h, 8, colortype,
277 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
279 png_write_info(pngptr, infoptr);
280 png_set_packing(pngptr);
282 rowpointers =
new png_bytep[surface.h];
283 for (int32_t i = 0; i < surface.h; i++) {
284 rowpointers[i] = (png_bytep)(Uint8 *)surface.pixels + i*surface.pitch;
287 png_write_image(pngptr, rowpointers);
288 png_write_end(pngptr, infoptr);
290 SDL_UnlockSurface(const_cast<SDL_Surface*>(&surface));
291 delete [] rowpointers;
292 png_destroy_write_struct(&pngptr, &infoptr);
299 static std::string baseName =
"image";
301 std::ostringstream oss;
302 oss << uniqueNumber <<
"_" << baseName;
304 const std::string name = oss.str();
314 m_surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, srcimg->
getWidth(),
321 rect.
x + xoffset, rect.
y + yoffset,
322 static_cast<Uint16
>(srcimg->
getWidth()),
323 static_cast<Uint16>(srcimg->
getHeight()) };
326 SDL_Rect srcrect = { rect.
x, rect.
y, rect.
w, rect.
h };
332 SDL_Rect dstrect = { xoffset, yoffset,
333 static_cast<Uint16
>(srcimg->
getWidth()),
334 static_cast<Uint16>(srcimg->
getHeight()) };
337 SDL_Rect srcrect = { rect.
x, rect.
y, rect.
w, rect.
h };
343 SDL_SetAlpha(srcimg->
m_surface, SDL_SRCALPHA, 0);
347 if ((x < 0) || (x >= surface->w) || (y < 0) || (y >= surface->h)) {
351 int32_t bpp = surface->format->BytesPerPixel;
352 SDL_LockSurface(surface);
353 Uint8* p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp;
354 Uint32 pixel = SDL_MapRGBA(surface->format, r, g, b, a);
362 *(Uint16 *)p = pixel;
366 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
367 p[0] = (pixel >> 16) & 0xff;
368 p[1] = (pixel >> 8) & 0xff;
373 p[1] = (pixel >> 8) & 0xff;
374 p[2] = (pixel >> 16) & 0xff;
379 *(Uint32 *)p = pixel;
382 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.
const Rect & getSubImageRect() const
Returns area of the image it occupies in the shared image.
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
const Rect & getArea() const
virtual ~Image()
Destructor.
uint32_t getHeight() const
virtual void copySubimage(uint32_t xoffset, uint32_t yoffset, const ImagePtr &img)
Copies given image into this one with respect to given offsets.
virtual void load(IResource *res)
uint32_t getWidth() const
bool isSharedImage() const
Returns true if this image shares data with another one.
virtual void load(IResource *resource)=0
T w
Width of the rectangle.