Python: Add image and PNG-write bindings

This commit is contained in:
Jeffrey Pfau 2016-10-14 17:51:24 -07:00
parent f5312fef78
commit 0723646354
6 changed files with 69 additions and 3 deletions

View File

@ -4,7 +4,7 @@ get_property(COMPILE_DEFINITIONS DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY COMPILE_
foreach(INCLUDE_DIR IN LISTS INCLUDE_DIRECTORIES) foreach(INCLUDE_DIR IN LISTS INCLUDE_DIRECTORIES)
list(APPEND PY_INCLUDE_DIRS -I${INCLUDE_DIR}) list(APPEND PY_INCLUDE_DIRS -I${INCLUDE_DIR})
endforeach() endforeach()
foreach(COMPILE_DEF IN LISTS COMPILE_DEFINITIONS) foreach(COMPILE_DEF IN LISTS COMPILE_DEFINITIONS FEATURE_DEFINES)
list(APPEND PY_COMPILE_DEFS -D${COMPILE_DEF}) list(APPEND PY_COMPILE_DEFS -D${COMPILE_DEF})
endforeach() endforeach()

View File

@ -1,16 +1,28 @@
#define COMMON_H #define COMMON_H
#define _TIME_H_ #define PNG_H
#define _SYS_TIME_H_ #define _SYS_TIME_H_
#define _TIME_H_
#define ATTRIBUTE_FORMAT(X, Y, Z) #define ATTRIBUTE_FORMAT(X, Y, Z)
#define DECL_BITFIELD(newtype, oldtype) typedef oldtype newtype #define DECL_BITFIELD(newtype, oldtype) typedef oldtype newtype
#define DECL_BIT(type, name, bit) #define DECL_BIT(type, name, bit)
#define DECL_BITS(type, name, bit, nbits) #define DECL_BITS(type, name, bit, nbits)
typedef int... time_t; typedef int... time_t;
typedef int... off_t; typedef int... off_t;
typedef ... va_list; typedef ... va_list;
typedef ...* png_structp;
typedef ...* png_infop;
typedef ...* png_unknown_chunkp;
#include <limits.h> #include <limits.h>
#include "platform/python/vfs-py.h"
#include "core/core.h" #include "core/core.h"
#include "platform/python/vfs-py.h"
#ifdef USE_PNG
#include "util/png-io.h"
#endif
#ifdef M_CORE_GBA #ifdef M_CORE_GBA
#include "arm/arm.h" #include "arm/arm.h"
#include "gba/gba.h" #include "gba/gba.h"

View File

@ -13,6 +13,7 @@ ffi.set_source("mgba._pylib", """
#include "gba/gba.h" #include "gba/gba.h"
#include "lr35902/lr35902.h" #include "lr35902/lr35902.h"
#include "gb/gb.h" #include "gb/gb.h"
#include "util/png-io.h"
#include "util/vfs.h" #include "util/vfs.h"
struct VFile* VFileFromPython(void* fileobj); struct VFile* VFileFromPython(void* fileobj);

View File

@ -70,6 +70,9 @@ class Core:
self._core.desiredVideoDimensions(self._core, width, height) self._core.desiredVideoDimensions(self._core, width, height)
return width[0], height[0] return width[0], height[0]
def setVideoBuffer(self, image):
self._core.setVideoBuffer(self._core, image.buffer, image.stride)
def reset(self): def reset(self):
self._core.reset(self._core) self._core.reset(self._core)

View File

@ -0,0 +1,26 @@
# Copyright (c) 2013-2016 Jeffrey Pfau
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from _pylib import ffi, lib
from . import png
class Image:
def __init__(self, width, height, stride=0):
self.width = width
self.height = height
self.stride = stride
self.constitute()
def constitute(self):
if self.stride <= 0:
self.stride = self.width
self.buffer = ffi.new("color_t[{}]".format(self.stride * self.height))
def savePNG(self, f):
p = png.PNG(f)
success = p.writeHeader(self)
success = success and p.writePixels(self)
p.writeClose()
return success

View File

@ -0,0 +1,24 @@
# Copyright (c) 2013-2016 Jeffrey Pfau
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from _pylib import ffi, lib
from . import vfs
class PNG:
def __init__(self, f):
self.vf = vfs.open(f)
def writeHeader(self, image):
self._png = lib.PNGWriteOpen(self.vf.handle)
self._info = lib.PNGWriteHeader(self._png, image.width, image.height)
return self._info != ffi.NULL
def writePixels(self, image):
return lib.PNGWritePixels(self._png, image.width, image.height, image.stride, image.buffer)
def writeClose(self):
lib.PNGWriteClose(self._png, self._info)
del self._png
del self._info