|
1 | 1 | include "../../../kivy/lib/sdl2.pxi"
|
2 | 2 |
|
| 3 | +from libc.string cimport memcpy |
| 4 | + |
3 | 5 | cdef class _WindowSDL2Storage:
|
4 | 6 | cdef SDL_Window *win
|
5 | 7 | cdef SDL_GLContext ctx
|
@@ -247,4 +249,43 @@ cdef class _WindowSDL2Storage:
|
247 | 249 | def flip(self):
|
248 | 250 | SDL_GL_SwapWindow(self.win)
|
249 | 251 |
|
| 252 | + def save_bytes_in_png(self, filename, data, int width, int height): |
| 253 | + |
| 254 | + cdef SDL_Surface *surface = SDL_CreateRGBSurfaceFrom( |
| 255 | + <char *>data, width, height, 24, width*3, |
| 256 | + 0x0000ff, 0x00ff00, 0xff0000, 0) |
| 257 | + cdef bytes bytes_filename = <bytes>filename.encode('utf-8') |
| 258 | + cdef char *real_filename = <char *>bytes_filename |
| 259 | + |
| 260 | + cdef SDL_Surface *flipped_surface = flipVert(surface) |
| 261 | + IMG_SavePNG(flipped_surface, real_filename) |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | +# Based on the example at |
| 266 | +# http://content.gpwiki.org/index.php/OpenGL:Tutorials:Taking_a_Screenshot |
| 267 | +cdef SDL_Surface* flipVert(SDL_Surface* sfc): |
| 268 | + cdef SDL_Surface* result = SDL_CreateRGBSurface( |
| 269 | + sfc.flags, sfc.w, sfc.h, sfc.format.BytesPerPixel * 8, |
| 270 | + sfc.format.Rmask, sfc.format.Gmask, sfc.format.Bmask, |
| 271 | + sfc.format.Amask) |
| 272 | + |
| 273 | + |
| 274 | + cdef Uint8* pixels = <Uint8*>sfc.pixels |
| 275 | + cdef Uint8* rpixels = <Uint8*>result.pixels |
| 276 | + |
| 277 | + cdef tuple output = (<int>sfc.w, <int>sfc.h, <int>sfc.format.BytesPerPixel, |
| 278 | + <int>sfc.pitch) |
| 279 | + print(output) |
| 280 | + |
| 281 | + cdef Uint32 pitch = sfc.pitch |
| 282 | + cdef Uint32 pxlength = pitch*sfc.h |
| 283 | + |
| 284 | + cdef Uint32 pos |
| 285 | + |
| 286 | + cdef int line |
| 287 | + for line in range(sfc.h): |
| 288 | + pos = line * pitch; |
| 289 | + memcpy(&rpixels[pos], &pixels[(pxlength-pos)-pitch], pitch) |
250 | 290 |
|
| 291 | + return result |
0 commit comments