diff options
author | Tavian Barnes <tavianator@gmail.com> | 2011-09-18 14:53:37 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2011-09-18 14:53:37 -0400 |
commit | 57ca6b8f4789d74a3d743a1f11c3b189c59d4edf (patch) | |
tree | 5aaa13c2223d636fc39c83aef2900145652f0bf5 | |
parent | b2e1763c7d54e76a49f6f4434996f37882ae2171 (diff) | |
download | dimension-57ca6b8f4789d74a3d743a1f11c3b189c59d4edf.tar.xz |
Add ImageMaps to Python module.
-rw-r--r-- | dimension/tests/demo.dmnsn | 2 | ||||
-rw-r--r-- | libdimension-python/dimension.pxd | 2 | ||||
-rw-r--r-- | libdimension-python/dimension.pyx | 44 | ||||
-rw-r--r-- | libdimension/tests/Makefile.am | 2 | ||||
-rw-r--r-- | libdimension/tests/render.c | 22 |
5 files changed, 63 insertions, 9 deletions
diff --git a/dimension/tests/demo.dmnsn b/dimension/tests/demo.dmnsn index 331c2b4..040ae4f 100644 --- a/dimension/tests/demo.dmnsn +++ b/dimension/tests/demo.dmnsn @@ -26,7 +26,7 @@ camera.rotate(53*Y) background = PigmentMap( pattern = Gradient(Y), map = { - 0: Orange, + 0: ImageMap("../../libdimension/tests/png2.png").rotate(53*Y), 0.35: Color(0, 0.1, 0.2, trans = 0.1, filter = 0.0), }, ) diff --git a/libdimension-python/dimension.pxd b/libdimension-python/dimension.pxd index 1164be5..b70508c 100644 --- a/libdimension-python/dimension.pxd +++ b/libdimension-python/dimension.pxd @@ -195,6 +195,8 @@ cdef extern from "../libdimension/dimension.h": int dmnsn_png_optimize_canvas(dmnsn_canvas *canvas) int dmnsn_png_write_canvas(dmnsn_canvas *canvas, FILE *file) dmnsn_progress *dmnsn_png_write_canvas_async(dmnsn_canvas *canvas, FILE *file) + dmnsn_canvas *dmnsn_png_read_canvas(FILE *file) + dmnsn_progress *dmnsn_png_read_canvas_async(dmnsn_canvas **canvas, FILE *file) int dmnsn_gl_optimize_canvas(dmnsn_canvas *canvas) int dmnsn_gl_write_canvas(dmnsn_canvas *canvas) diff --git a/libdimension-python/dimension.pyx b/libdimension-python/dimension.pyx index 0fed65f..b8855fe 100644 --- a/libdimension-python/dimension.pyx +++ b/libdimension-python/dimension.pyx @@ -25,6 +25,13 @@ Dimension: a high-performance photo-realistic 3D renderer. import os ########### +# Helpers # +########### + +cdef _raise_OSError(): + raise OSError(errno, os.strerror(errno)) + +########### # Globals # ########### @@ -536,12 +543,12 @@ cdef class Canvas: def optimize_PNG(self): """Optimize a canvas for PNG output.""" if dmnsn_png_optimize_canvas(self._canvas) != 0: - raise OSError(errno, os.strerror(errno)) + _raise_OSError() def optimize_GL(self): """Optimize a canvas for OpenGL output.""" if dmnsn_gl_optimize_canvas(self._canvas) != 0: - raise OSError(errno, os.strerror(errno)) + _raise_OSError() def clear(self, c): """Clear a canvas with a solid color.""" @@ -556,11 +563,11 @@ cdef class Canvas: cdef char *cpath = bpath cdef FILE *file = fopen(cpath, "wb") if file == NULL: - raise OSError(errno, os.strerror(errno)) + _raise_OSError() def finalize(): if fclose(file) != 0: - raise OSError(errno, os.strerror(errno)) + _raise_OSError() progress = _Progress(dmnsn_png_write_canvas_async(self._canvas, file)) progress._finalizer = finalize @@ -569,7 +576,7 @@ cdef class Canvas: def draw_GL(self): """Export the canvas to the current OpenGL context.""" if dmnsn_gl_write_canvas(self._canvas) != 0: - raise OSError(errno, os.strerror(errno)) + _raise_OSError() cdef class _CanvasProxy: cdef dmnsn_canvas *_canvas @@ -677,6 +684,29 @@ cdef Pigment _Pigment(dmnsn_pigment *pigment): DMNSN_INCREF(self._pigment) return self +cdef class ImageMap(Pigment): + """An image-mapped pigment.""" + def __init__(self, path, *args, **kwargs): + """ + Create an ImageMap. + + Keyword arguments: + path -- the path of the PNG file to open + """ + bpath = path.encode("UTF-8") + cdef char *cpath = bpath + cdef FILE *file = fopen(cpath, "rb") + if file == NULL: + _raise_OSError() + cdef dmnsn_canvas *canvas = dmnsn_png_read_canvas(file) + if canvas == NULL: + _raise_OSError() + if fclose(file) != 0: + _raise_OSError() + + self._pigment = dmnsn_new_canvas_pigment(canvas) + Pigment.__init__(self, *args, **kwargs) + cdef class PigmentMap(Pigment): """A pigment map.""" def __init__(self, Pattern pattern not None, map, bool sRGB not None = True, @@ -696,13 +726,13 @@ cdef class PigmentMap(Pigment): if hasattr(map, "items"): for i, pigment in map.items(): pigment = Pigment(pigment) - real_pigment = (<Pigment?>pigment)._pigment + real_pigment = (<Pigment>pigment)._pigment DMNSN_INCREF(real_pigment) dmnsn_add_map_entry(pigment_map, i, &real_pigment) else: for i, pigment in enumerate(map): pigment = Pigment(pigment) - real_pigment = (<Pigment?>pigment)._pigment + real_pigment = (<Pigment>pigment)._pigment DMNSN_INCREF(real_pigment) dmnsn_add_map_entry(pigment_map, i/len(map), &real_pigment) diff --git a/libdimension/tests/Makefile.am b/libdimension/tests/Makefile.am index eec2e9d..f44cdd6 100644 --- a/libdimension/tests/Makefile.am +++ b/libdimension/tests/Makefile.am @@ -32,6 +32,8 @@ check_PROGRAMS = warning.test \ TESTS = $(check_PROGRAMS) XFAIL_TESTS = warning-as-error.test error.test +render.log: png.log + if !PNG XFAIL_TESTS += png.test endif diff --git a/libdimension/tests/render.c b/libdimension/tests/render.c index ae0301f..ee63d06 100644 --- a/libdimension/tests/render.c +++ b/libdimension/tests/render.c @@ -76,11 +76,31 @@ dmnsn_test_scene_add_background(dmnsn_scene *scene) { dmnsn_pattern *sky_gradient = dmnsn_new_gradient_pattern(dmnsn_y); dmnsn_map *sky_gradient_pigment_map = dmnsn_new_pigment_map(); - dmnsn_pigment_map_add_color(sky_gradient_pigment_map, 0.0, dmnsn_orange); + + dmnsn_canvas *png_canvas = NULL; + dmnsn_pigment *png_pigment; + FILE *png = fopen("png2.png", "rb"); + if (png) { + png_canvas = dmnsn_png_read_canvas(png); + fclose(png); + } + if (png_canvas) { + png_pigment = dmnsn_new_canvas_pigment(png_canvas); + png_pigment->trans = dmnsn_rotation_matrix( + dmnsn_new_vector(0.0, dmnsn_radians(53.0), 0.0) + ); + } else { + /* Loading png2.png failed, possibly compiled with --disable-png */ + fprintf(stderr, "--- WARNING: Couldn't open or read png2.png! ---\n"); + png_pigment = dmnsn_new_solid_pigment(dmnsn_orange); + } + dmnsn_add_map_entry(sky_gradient_pigment_map, 0.0, &png_pigment); + dmnsn_color background = dmnsn_color_from_sRGB( dmnsn_new_color5(0.0, 0.1, 0.2, 0.1, 0.0) ); dmnsn_pigment_map_add_color(sky_gradient_pigment_map, 0.35, background); + scene->background = dmnsn_new_pigment_map_pigment(sky_gradient, sky_gradient_pigment_map, DMNSN_PIGMENT_MAP_SRGB); |