42 Image::Image(
const uint8_t* data,
unsigned int width,
unsigned int height):
44 SDL_Surface* surface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, width,height, 32,
45 RMASK, GMASK, BMASK ,AMASK);
46 SDL_LockSurface(surface);
48 unsigned int size = width * height * 4;
49 uint8_t* pixeldata =
static_cast<uint8_t*
>(surface->pixels);
50 std::copy(data, data + size, pixeldata);
51 SDL_UnlockSurface(surface);
55 void Image::reset(SDL_Surface* surface) {
57 SDL_FreeSurface(m_surface);
62 while (!m_clipstack.empty()) {
65 m_area.
x = m_area.
y = m_area.
w = m_area.
h = 0;
75 SDL_Surface* srf = m_surface;
80 unsigned int Image::getWidth()
const {
87 unsigned int Image::getHeight()
const {
94 const Rect& Image::getArea() {
95 m_area.
w = getWidth();
96 m_area.
h = getHeight();
100 void Image::setXShift(
int xshift) {
104 void Image::setYShift(
int yshift) {
108 void Image::getPixelRGBA(
int x,
int y, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* a) {
109 if ((x < 0) || (x >= m_surface->w) || (y < 0) || (y >= m_surface->h)) {
117 int bpp = m_surface->format->BytesPerPixel;
118 Uint8 *p = (Uint8*)m_surface->pixels + y * m_surface->pitch + x * bpp;
125 pixel = *(Uint16 *)p;
128 if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
129 pixel = p[0] << 16 | p[1] << 8 | p[2];
131 pixel = p[0] | p[1] << 8 | p[2] << 16;
135 pixel = *(Uint32 *)p;
137 SDL_GetRGBA(pixel, m_surface->format, r, g, b, a);
141 render(rect, SDL_GetVideoSurface(), alpha);
144 void Image::pushClipArea(
const Rect& cliparea,
bool clear) {
148 m_clipstack.push(ci);
152 void Image::popClipArea() {
153 assert(!m_clipstack.empty());
155 if (m_clipstack.empty()) {
158 ClipInfo ci = m_clipstack.top();
163 const Rect& Image::getClipArea()
const {
164 if (m_clipstack.empty()) {
165 return m_clipstack.top().r;
175 void Image::saveAsPng(
const std::string& filename, SDL_Surface& surface) {
180 png_bytep *rowpointers = NULL;
182 fp = fopen(filename.c_str(),
"wb");
189 pngptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
191 if (pngptr == NULL) {
197 infoptr = png_create_info_struct(pngptr);
198 if (infoptr == NULL) {
200 png_destroy_write_struct(&pngptr, (png_infopp)NULL);
204 if (setjmp(png_jmpbuf(pngptr))) {
205 png_destroy_write_struct(&pngptr, &infoptr);
211 png_init_io(pngptr, fp);
214 SDL_LockSurface(&surface);
216 colortype = PNG_COLOR_TYPE_RGB;
217 if(m_surface->format->palette){
218 colortype |= PNG_COLOR_TYPE_PALETTE;
220 else if (m_surface->format->Amask){
221 colortype |= PNG_COLOR_TYPE_RGB_ALPHA;
225 png_set_IHDR(pngptr, infoptr, surface.w, surface.h, 8, colortype,
226 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
228 png_write_info(pngptr, infoptr);
229 png_set_packing(pngptr);
231 rowpointers =
new png_bytep[surface.h];
232 for (
int i = 0; i < surface.h; i++) {
233 rowpointers[i] = (png_bytep)(Uint8 *)surface.pixels + i*surface.pitch;
236 png_write_image(pngptr, rowpointers);
237 png_write_end(pngptr, infoptr);
239 SDL_UnlockSurface(&surface);
240 delete [] rowpointers;
241 png_destroy_write_struct(&pngptr, &infoptr);