mirror of https://github.com/kivy/kivy.git
sdl2: add support for reading image from memory
This commit is contained in:
parent
bed1f7d75e
commit
2feef02c13
|
@ -38,9 +38,8 @@ def save(filename, w, h, fmt, pixels, flipped):
|
|||
IMG_SavePNG(image, c_filename)
|
||||
SDL_FreeSurface(image)
|
||||
|
||||
def load(filename):
|
||||
cdef bytes c_filename = filename.encode('utf-8')
|
||||
cdef SDL_Surface *image = IMG_Load(c_filename)
|
||||
|
||||
cdef load_from_surface(SDL_Surface *image):
|
||||
cdef SDL_Surface *image2 = NULL
|
||||
cdef SDL_Surface *fimage = NULL
|
||||
cdef SDL_PixelFormat pf
|
||||
|
@ -48,7 +47,6 @@ def load(filename):
|
|||
|
||||
try:
|
||||
if image == NULL:
|
||||
#print 'UNABLE TO LOAD O_o?'
|
||||
return None
|
||||
|
||||
fmt = ''
|
||||
|
@ -104,7 +102,38 @@ def load(filename):
|
|||
return (fimage.w, fimage.h, fmt, pixels, fimage.pitch)
|
||||
|
||||
finally:
|
||||
if image:
|
||||
SDL_FreeSurface(image)
|
||||
if image2:
|
||||
SDL_FreeSurface(image2)
|
||||
|
||||
|
||||
def load_from_filename(filename):
|
||||
cdef bytes c_filename = filename.encode('utf-8')
|
||||
cdef SDL_Surface *image = IMG_Load(c_filename)
|
||||
if image == NULL:
|
||||
return
|
||||
try:
|
||||
return load_from_surface(image)
|
||||
finally:
|
||||
if image:
|
||||
SDL_FreeSurface(image)
|
||||
|
||||
def load_from_memory(bytes data):
|
||||
cdef SDL_RWops *rw = NULL
|
||||
cdef SDL_Surface *image = NULL
|
||||
cdef char *c_data = data
|
||||
|
||||
rw = SDL_RWFromMem(c_data, len(data))
|
||||
if rw == NULL:
|
||||
return
|
||||
|
||||
image = IMG_Load_RW(rw, 0)
|
||||
if image == NULL:
|
||||
return
|
||||
|
||||
try:
|
||||
return load_from_surface(image)
|
||||
finally:
|
||||
if image:
|
||||
SDL_FreeSurface(image)
|
||||
if rw:
|
||||
SDL_FreeRW(rw)
|
||||
|
|
|
@ -27,8 +27,16 @@ class ImageLoaderSDL2(ImageLoaderBase):
|
|||
def can_save():
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def can_load_memory():
|
||||
return True
|
||||
|
||||
def load(self, filename):
|
||||
info = _img_sdl2.load(filename)
|
||||
if self._inline:
|
||||
data = filename.read()
|
||||
info = _img_sdl2.load_from_memory(data)
|
||||
else:
|
||||
info = _img_sdl2.load_from_filename(filename)
|
||||
if not info:
|
||||
Logger.warning('Image: Unable to load image <%s>' % filename)
|
||||
raise Exception('SDL2: Unable to load image')
|
||||
|
@ -36,6 +44,7 @@ class ImageLoaderSDL2(ImageLoaderBase):
|
|||
w, h, fmt, pixels, rowlength = info
|
||||
|
||||
# update internals
|
||||
if not self._inline:
|
||||
self.filename = filename
|
||||
return [ImageData(
|
||||
w, h, fmt, pixels, source=filename,
|
||||
|
|
Loading…
Reference in New Issue