Python: Add image and PNG-write bindings
Jeffrey Pfau jeffrey@endrift.com
Fri, 14 Oct 2016 17:51:24 -0700
6 files changed,
69 insertions(+),
3 deletions(-)
M
src/platform/python/CMakeLists.txt
→
src/platform/python/CMakeLists.txt
@@ -4,7 +4,7 @@ get_property(COMPILE_DEFINITIONS DIRECTORY ${CMAKE_SOURCE_DIR} PROPERTY COMPILE_DEFINITIONS)
foreach(INCLUDE_DIR IN LISTS INCLUDE_DIRECTORIES) list(APPEND PY_INCLUDE_DIRS -I${INCLUDE_DIR}) 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}) endforeach()
M
src/platform/python/_builder.h
→
src/platform/python/_builder.h
@@ -1,16 +1,28 @@
#define COMMON_H -#define _TIME_H_ +#define PNG_H #define _SYS_TIME_H_ +#define _TIME_H_ + #define ATTRIBUTE_FORMAT(X, Y, Z) #define DECL_BITFIELD(newtype, oldtype) typedef oldtype newtype #define DECL_BIT(type, name, bit) #define DECL_BITS(type, name, bit, nbits) + typedef int... time_t; typedef int... off_t; typedef ... va_list; +typedef ...* png_structp; +typedef ...* png_infop; +typedef ...* png_unknown_chunkp; + #include <limits.h> -#include "platform/python/vfs-py.h" + #include "core/core.h" +#include "platform/python/vfs-py.h" + +#ifdef USE_PNG +#include "util/png-io.h" +#endif #ifdef M_CORE_GBA #include "arm/arm.h" #include "gba/gba.h"
M
src/platform/python/_builder.py
→
src/platform/python/_builder.py
@@ -13,6 +13,7 @@ #include "arm/arm.h"
#include "gba/gba.h" #include "lr35902/lr35902.h" #include "gb/gb.h" +#include "util/png-io.h" #include "util/vfs.h" struct VFile* VFileFromPython(void* fileobj);
M
src/platform/python/mgba/core.py
→
src/platform/python/mgba/core.py
@@ -70,6 +70,9 @@ height = ffi.new("unsigned*")
self._core.desiredVideoDimensions(self._core, width, height) return width[0], height[0] + def setVideoBuffer(self, image): + self._core.setVideoBuffer(self._core, image.buffer, image.stride) + def reset(self): self._core.reset(self._core)
A
src/platform/python/mgba/image.py
@@ -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
A
src/platform/python/mgba/png.py
@@ -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