Revision: 7032
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=7032&view=rev
Author:   astraw
Date:     2009-04-06 01:52:45 +0000 (Mon, 06 Apr 2009)

Log Message:
-----------
Merged revisions 7027-7031 via svnmerge from 
https://matplotlib.svn.sourceforge.net/svnroot/matplotlib/branches/v0_98_5_maint

........
  r7027 | astraw | 2009-04-05 15:06:05 -0700 (Sun, 05 Apr 2009) | 1 line
  
  pngsuite test: plot images in grayscale if the input array is 2 dimensional
........
  r7028 | astraw | 2009-04-05 15:07:01 -0700 (Sun, 05 Apr 2009) | 1 line
  
  read 12 bit PNGs (patch from Tobias Wood)
........
  r7029 | astraw | 2009-04-05 15:07:34 -0700 (Sun, 05 Apr 2009) | 1 line
  
  trivial: remove trailing whitespace from source code
........
  r7030 | astraw | 2009-04-05 15:08:24 -0700 (Sun, 05 Apr 2009) | 1 line
  
  trivial: remove outdated comment
........
  r7031 | astraw | 2009-04-05 15:09:45 -0700 (Sun, 05 Apr 2009) | 1 line
  
  update changelog about Tobias' patch
........

Modified Paths:
--------------
    trunk/matplotlib/CHANGELOG
    trunk/matplotlib/examples/tests/pngsuite/pngsuite.py
    trunk/matplotlib/lib/matplotlib/image.py
    trunk/matplotlib/src/_png.cpp

Property Changed:
----------------
    trunk/matplotlib/


Property changes on: trunk/matplotlib
___________________________________________________________________
Modified: svnmerge-integrated
   - /branches/v0_91_maint:1-6428 /branches/v0_98_5_maint:1-7025
   + /branches/v0_91_maint:1-6428 /branches/v0_98_5_maint:1-7025,7027-7031

Modified: trunk/matplotlib/CHANGELOG
===================================================================
--- trunk/matplotlib/CHANGELOG  2009-04-05 22:09:45 UTC (rev 7031)
+++ trunk/matplotlib/CHANGELOG  2009-04-06 01:52:45 UTC (rev 7032)
@@ -1,3 +1,6 @@
+2009-04-05 _png.read_png() reads 12 bit PNGs (patch from 
+           Tobias Wood) - ADS
+
 2009-04-04 Allow log axis scale to clip non-positive values to
            small positive value; this is useful for errorbars. - EF
 

Modified: trunk/matplotlib/examples/tests/pngsuite/pngsuite.py
===================================================================
--- trunk/matplotlib/examples/tests/pngsuite/pngsuite.py        2009-04-05 
22:09:45 UTC (rev 7031)
+++ trunk/matplotlib/examples/tests/pngsuite/pngsuite.py        2009-04-06 
01:52:45 UTC (rev 7032)
@@ -8,6 +8,7 @@
 """
 
 from matplotlib import pyplot as plt
+import matplotlib.cm as cm
 import glob
 
 files = glob.glob("basn*.png")
@@ -17,7 +18,11 @@
 
 for i, fname in enumerate(files):
     data = plt.imread(fname)
-    plt.imshow(data, extent=[i,i+1,0,1])
+    cmap = None # use default colormap
+    if data.ndim==2:
+        # keep grayscale images gray
+        cmap = cm.gray
+    plt.imshow(data, extent=[i,i+1,0,1], cmap=cmap)
 
 plt.gca().get_frame().set_facecolor("#ddffff")
 plt.gca().set_xlim(0, len(files))

Modified: trunk/matplotlib/lib/matplotlib/image.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/image.py    2009-04-05 22:09:45 UTC (rev 
7031)
+++ trunk/matplotlib/lib/matplotlib/image.py    2009-04-06 01:52:45 UTC (rev 
7032)
@@ -748,8 +748,6 @@
     <http://www.pythonware.com/products/pil/>`_ is installed, it will
     use it to load the image and return an array (if possible) which
     can be used with :func:`~matplotlib.pyplot.imshow`.
-
-    TODO: support RGB and grayscale return values in _image.readpng
     """
 
     def pilread():

Modified: trunk/matplotlib/src/_png.cpp
===================================================================
--- trunk/matplotlib/src/_png.cpp       2009-04-05 22:09:45 UTC (rev 7031)
+++ trunk/matplotlib/src/_png.cpp       2009-04-06 01:52:45 UTC (rev 7032)
@@ -208,38 +208,37 @@
 
   png_init_io(png_ptr, fp);
   png_set_sig_bytes(png_ptr, 8);
-
   png_read_info(png_ptr, info_ptr);
 
   png_uint_32 width = info_ptr->width;
   png_uint_32 height = info_ptr->height;
-  bool do_gray_conversion = (info_ptr->bit_depth < 8 &&
-                             info_ptr->color_type == PNG_COLOR_TYPE_GRAY);
 
   int bit_depth = info_ptr->bit_depth;
-  if (bit_depth == 16) {
-    png_set_strip_16(png_ptr);
-  } else if (bit_depth < 8) {
+
+  // Unpack 1, 2, and 4-bit images
+  if (bit_depth < 8)
     png_set_packing(png_ptr);
-  }
 
-  // convert misc color types to rgb for simplicity
-  if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY ||
-      info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
-    png_set_gray_to_rgb(png_ptr);
-  } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
+  // If sig bits are set, shift data
+  png_color_8p sig_bit;
+  if ((info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) && 
png_get_sBIT(png_ptr, info_ptr, &sig_bit))
+    png_set_shift(png_ptr, sig_bit);
+
+  // Convert big endian to little
+  if (bit_depth == 16)
+    png_set_swap(png_ptr);
+
+  // Convert palletes to full RGB
+  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
     png_set_palette_to_rgb(png_ptr);
-  }
 
+  // If there's an alpha channel convert gray to RGB
+  if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+    png_set_gray_to_rgb(png_ptr);
+
   png_set_interlace_handling(png_ptr);
   png_read_update_info(png_ptr, info_ptr);
 
-  bool rgba = info_ptr->color_type == PNG_COLOR_TYPE_RGBA;
-  if ( (info_ptr->color_type != PNG_COLOR_TYPE_RGB) && !rgba) {
-    std::cerr << "Found color type " << (int)info_ptr->color_type  << 
std::endl;
-    throw Py::RuntimeError("_image_module::readpng: cannot handle color_type");
-  }
-
   /* read file */
   if (setjmp(png_jmpbuf(png_ptr)))
     throw Py::RuntimeError("_image_module::readpng: error during read_image");
@@ -255,35 +254,34 @@
   npy_intp dimensions[3];
   dimensions[0] = height;  //numrows
   dimensions[1] = width;   //numcols
-  dimensions[2] = 4;
+  if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
+    dimensions[2] = 4;     //RGBA images
+  else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
+    dimensions[2] = 3;     //RGB images
+  else
+    dimensions[2] = 1;     //Greyscale images
+  //For gray, return an x by y array, not an x by y by 1
+  int num_dims  = (info_ptr->color_type & PNG_COLOR_MASK_COLOR) ? 3 : 2;
 
-  PyArrayObject *A = (PyArrayObject *) PyArray_SimpleNew(3, dimensions, 
PyArray_FLOAT);
+  double max_value = (1 << ((bit_depth < 8) ? 8 : bit_depth)) - 1;
+  PyArrayObject *A = (PyArrayObject *) PyArray_SimpleNew(num_dims, dimensions, 
PyArray_FLOAT);
 
-  if (do_gray_conversion) {
-    float max_value = (float)((1L << bit_depth) - 1);
-    for (png_uint_32 y = 0; y < height; y++) {
-      png_byte* row = row_pointers[y];
-      for (png_uint_32 x = 0; x < width; x++) {
-        float value = row[x] / max_value;
-        size_t offset = y*A->strides[0] + x*A->strides[1];
-        *(float*)(A->data + offset + 0*A->strides[2]) = value;
-        *(float*)(A->data + offset + 1*A->strides[2]) = value;
-        *(float*)(A->data + offset + 2*A->strides[2]) = value;
-        *(float*)(A->data + offset + 3*A->strides[2]) = 1.0f;
-      }
+  for (png_uint_32 y = 0; y < height; y++) {
+    png_byte* row = row_pointers[y];
+       for (png_uint_32 x = 0; x < width; x++) {
+         size_t offset = y*A->strides[0] + x*A->strides[1];
+         if (bit_depth == 16) {
+           png_uint_16* ptr = &reinterpret_cast<png_uint_16*> (row)[x * 
dimensions[2]];
+               for (png_uint_32 p = 0; p < dimensions[2]; p++)
+             *(float*)(A->data + offset + p*A->strides[2]) = (float)(ptr[p]) / 
max_value;
+         } else {
+           png_byte* ptr = &(row[x * dimensions[2]]);
+           for (png_uint_32 p = 0; p < dimensions[2]; p++)
+               {
+             *(float*)(A->data + offset + p*A->strides[2]) = (float)(ptr[p]) / 
max_value;
+           }
+         }
     }
-  } else {
-    for (png_uint_32 y = 0; y < height; y++) {
-      png_byte* row = row_pointers[y];
-      for (png_uint_32 x = 0; x < width; x++) {
-        png_byte* ptr = (rgba) ? &(row[x*4]) : &(row[x*3]);
-        size_t offset = y*A->strides[0] + x*A->strides[1];
-        *(float*)(A->data + offset + 0*A->strides[2]) = (float)(ptr[0]/255.0);
-        *(float*)(A->data + offset + 1*A->strides[2]) = (float)(ptr[1]/255.0);
-        *(float*)(A->data + offset + 2*A->strides[2]) = (float)(ptr[2]/255.0);
-        *(float*)(A->data + offset + 3*A->strides[2]) = rgba ? 
(float)(ptr[3]/255.0) : 1.0f;
-      }
-    }
   }
 
   //free the png memory


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to