# HG changeset patch # User Matthew Turk # Date 1283211810 25200 # Branch yt # Node ID 68a3f7a4c6f107de8f84b5f2c69aab779602ef3c # Parent 619e237aba6d3b7a7bf3de6135b592afcb388492 Adding a new function to write pngs to existing file handles, fixed the API for the image panner diff -r 619e237aba6d3b7a7bf3de6135b592afcb388492 -r 68a3f7a4c6f107de8f84b5f2c69aab779602ef3c yt/utilities/_amr_utils/png_writer.pyx --- a/yt/utilities/_amr_utils/png_writer.pyx Mon Aug 30 15:41:06 2010 -0700 +++ b/yt/utilities/_amr_utils/png_writer.pyx Mon Aug 30 16:43:30 2010 -0700 @@ -33,6 +33,13 @@ # NOTE that size_t might not be int void *alloca(int) +# Idiom for accessing Python files, from Cython FAQ +# First, declare the Python macro to access files: +cdef extern from "Python.h": + ctypedef struct FILE + FILE* PyFile_AsFile(object) + void fprintf(FILE* f, char* s, char* s) + cdef extern from "png.h": ctypedef unsigned long png_uint_32 ctypedef long png_int_32 @@ -99,6 +106,54 @@ void png_destroy_write_struct( png_structp *png_ptr_ptr, png_infop *info_ptr_ptr) +def write_png_to_file(np.ndarray[np.uint8_t, ndim=3] buffer, + object py_fileobj, int dpi=100, + int close = 0): + + # This is something of a translation of the matplotlib _png module + cdef png_byte *pix_buffer = buffer.data + cdef int width = buffer.shape[1] + cdef int height = buffer.shape[0] + cdef FILE *fileobj = PyFile_AsFile(py_fileobj) + + cdef png_bytep *row_pointers + cdef png_structp png_ptr + cdef png_infop info_ptr + + cdef png_color_8 sig_bit + cdef png_uint_32 row + + row_pointers = alloca(sizeof(png_bytep) * height) + + for row in range(height): + row_pointers[row] = pix_buffer + row * width * 4 + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL) + info_ptr = png_create_info_struct(png_ptr) + + # Um we are ignoring setjmp sorry guys + + png_init_io(png_ptr, fileobj) + + png_set_IHDR(png_ptr, info_ptr, width, height, 8, + PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE) + + cdef size_t dots_per_meter = (dpi / (2.54 / 100.0)) + png_set_pHYs(png_ptr, info_ptr, dots_per_meter, dots_per_meter, + PNG_RESOLUTION_METER) + + sig_bit.gray = 0 + sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 8 + + png_set_sBIT(png_ptr, info_ptr, &sig_bit) + + png_write_info(png_ptr, info_ptr) + png_write_image(png_ptr, row_pointers) + png_write_end(png_ptr, info_ptr) + + if close == 1: fclose(fileobj) + png_destroy_write_struct(&png_ptr, &info_ptr) + def write_png(np.ndarray[np.uint8_t, ndim=3] buffer, char *filename, int dpi=100): diff -r 619e237aba6d3b7a7bf3de6135b592afcb388492 -r 68a3f7a4c6f107de8f84b5f2c69aab779602ef3c yt/visualization/image_panner/__init__.py --- a/yt/visualization/image_panner/__init__.py Mon Aug 30 15:41:06 2010 -0700 +++ b/yt/visualization/image_panner/__init__.py Mon Aug 30 16:43:30 2010 -0700 @@ -20,7 +20,3 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ - -from vm_panner import VariableMeshPanner, WindowedVariableMeshPanner, \ - MultipleWindowVariableMeshPanner, ImageSaver, \ - PanningCeleritasStreamer, NonLocalDataImagePanner diff -r 619e237aba6d3b7a7bf3de6135b592afcb388492 -r 68a3f7a4c6f107de8f84b5f2c69aab779602ef3c yt/visualization/image_panner/api.py --- a/yt/visualization/image_panner/api.py Mon Aug 30 15:41:06 2010 -0700 +++ b/yt/visualization/image_panner/api.py Mon Aug 30 16:43:30 2010 -0700 @@ -27,3 +27,7 @@ along with this program. If not, see . """ + +from vm_panner import VariableMeshPanner, WindowedVariableMeshPanner, \ + MultipleWindowVariableMeshPanner, ImageSaver, \ + PanningCeleritasStreamer, NonLocalDataImagePanner diff -r 619e237aba6d3b7a7bf3de6135b592afcb388492 -r 68a3f7a4c6f107de8f84b5f2c69aab779602ef3c yt/visualization/image_panner/vm_panner.py --- a/yt/visualization/image_panner/vm_panner.py Mon Aug 30 15:41:06 2010 -0700 +++ b/yt/visualization/image_panner/vm_panner.py Mon Aug 30 16:43:30 2010 -0700 @@ -23,9 +23,12 @@ import numpy as na import types, os -from yt.raven import FixedResolutionBuffer, ObliqueFixedResolutionBuffer -from yt.lagos import data_object_registry, AMRProjBase, AMRSliceBase, \ - x_dict, y_dict +from yt.visualization.fixed_resolution import \ + FixedResolutionBuffer, ObliqueFixedResolutionBuffer +from yt.data_objects.data_containers import \ + data_object_registry, AMRProjBase, AMRSliceBase +from yt.utilities.definitions import \ + x_dict, y_dict from yt.funcs import * class VariableMeshPanner(object):