[yt-svn] commit/yt: MatthewTurk: Adding a gray option to write_png_to_string.

Bitbucket commits-noreply at bitbucket.org
Thu Jan 31 20:51:58 PST 2013


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/749385e5bd82/
changeset:   749385e5bd82
branch:      yt
user:        MatthewTurk
date:        2013-02-01 05:51:49
summary:     Adding a gray option to write_png_to_string.
affected #:  1 file

diff -r 0514b030f6479c0adb75df5bf3da7ab7310caa91 -r 749385e5bd821410e71178aeafb402ee72b9b785 yt/utilities/lib/png_writer.pyx
--- a/yt/utilities/lib/png_writer.pyx
+++ b/yt/utilities/lib/png_writer.pyx
@@ -93,6 +93,7 @@
         int filter_method)
 
     cdef int PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE
+    cdef int PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_ADAM7
     cdef int PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE
 
     void png_set_pHYs(png_structp png_ptr, png_infop info_ptr,
@@ -240,7 +241,8 @@
 cdef public void my_png_flush(png_structp png_ptr):
     return
 
-def write_png_to_string(np.ndarray[np.uint8_t, ndim=3] buffer, int dpi=100):
+def write_png_to_string(np.ndarray[np.uint8_t, ndim=3] buffer, int dpi=100,
+                        int gray = 0):
 
     # This is something of a translation of the matplotlib _png module
     cdef png_byte *pix_buffer = <png_byte *> buffer.data
@@ -263,16 +265,27 @@
     
     # Um we are ignoring setjmp sorry guys
 
+    cdef int im_type, interlace_type
+    interlace_type = PNG_INTERLACE_NONE
+    if gray == 0:
+        im_type = PNG_COLOR_TYPE_RGB_ALPHA
+        sig_bit.gray = 0
+        sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 8
+    elif gray == 1:
+        im_type = PNG_COLOR_TYPE_GRAY
+        sig_bit.gray = 8
+        sig_bit.red = sig_bit.green = sig_bit.blue = sig_bit.alpha = 0
+    else:
+        raise RuntimeError
+
     png_set_IHDR(png_ptr, info_ptr, width, height, 8,
-                 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
+                 im_type, interlace_type,
                  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE)
 
     cdef size_t dots_per_meter = <size_t> (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)

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list