On 12/03/2012 07:00 PM, Chris Barker - NOAA Federal wrote:
> On Mon, Dec 3, 2012 at 12:24 PM, Chris Barker - NOAA Federal
> <chris.bar...@noaa.gov> wrote:
>
>>>>> but some of that complexity could be reduced by using Numpy arrays in 
>>>>> place
>>> It would at least make this a more fair comparison to have the Cython
>>> code as Cythonic as possible.  However, I couldn't find any ways around
>>> using these particular APIs -- other than the Numpy stuff which probably
>>> does have a more elegant solution in the form of Cython arrays and
>>> memory views.
> OK -- so I poked at it, and this is my (very untested) version of
> write_png (I left out the py3 stuff, though it does look like it may
> be required for file handling...
>
> Letting Cython unpack the numpy array is the real win. Maybe having it
> this simple won't work for MPL, but this is what my code tends to look
> like.
>
>
> def write_png(cnp.ndarray[cnp.uint32, ndim=2, mode="c" ] buff not None,
>                file_obj,
>                double dpi=0.0):
>
>      cdef png_uint_32 width  = buff.size[0]
>      cdef png_uint_32 height = buff.size[1]
>
>      if PyFile_CheckExact(file_obj):
>          cdef FILE *fp = fdopen(file_obj.fileno(), "w")
>          fp = PyFile_AsFile(file_obj)
>          write_png_c(buff[0,0], width, height, fp,
>                      NULL, NULL, NULL, dpi)
>          return
>      else:
>          raise TypeError("write_png only works with real PyFileObject")
>
>
> NOTE: that could be:
>
> cnp.ndarray[cnp.uint8, ndim=3, mode="c" ]
>
> I'm not sure how MPL stores image buffers.
>
> or you could accept any object, then call:
>
> np.view()
The buffer comes in both ways, so the latter solution seems like the 
thing to do.

Thanks for working this through.  This sort of thing is very helpful.

We can also, of course, maintain the existing code that allows writing 
to an arbitrary file-like object, but this fast path (where it is a 
"real" file) is very important.  It's significantly faster than calling 
methods on Python objects.

Mike

------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-devel

Reply via email to