Revision: 6318
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6318&view=rev
Author:   mdboom
Date:     2008-10-24 14:05:07 +0000 (Fri, 24 Oct 2008)

Log Message:
-----------
Support reading low-bit depth (< 8) PNG images.  Add PNG test suite to make 
sure we're supporting the basic kinds of PNG files available.

Modified Paths:
--------------
    trunk/matplotlib/src/_png.cpp

Added Paths:
-----------
    trunk/matplotlib/examples/tests/pngsuite/
    trunk/matplotlib/examples/tests/pngsuite/basn0g01.png
    trunk/matplotlib/examples/tests/pngsuite/basn0g02.png
    trunk/matplotlib/examples/tests/pngsuite/basn0g04.png
    trunk/matplotlib/examples/tests/pngsuite/basn0g08.png
    trunk/matplotlib/examples/tests/pngsuite/basn0g16.png
    trunk/matplotlib/examples/tests/pngsuite/basn2c08.png
    trunk/matplotlib/examples/tests/pngsuite/basn2c16.png
    trunk/matplotlib/examples/tests/pngsuite/basn3p01.png
    trunk/matplotlib/examples/tests/pngsuite/basn3p02.png
    trunk/matplotlib/examples/tests/pngsuite/basn3p04.png
    trunk/matplotlib/examples/tests/pngsuite/basn3p08.png
    trunk/matplotlib/examples/tests/pngsuite/basn4a08.png
    trunk/matplotlib/examples/tests/pngsuite/basn4a16.png
    trunk/matplotlib/examples/tests/pngsuite/basn6a08.png
    trunk/matplotlib/examples/tests/pngsuite/basn6a16.png
    trunk/matplotlib/examples/tests/pngsuite/pngsuite.py
    trunk/matplotlib/examples/tests/pngsuite/truth.png

Added: trunk/matplotlib/examples/tests/pngsuite/basn0g01.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn0g01.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn0g02.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn0g02.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn0g04.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn0g04.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn0g08.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn0g08.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn0g16.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn0g16.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn2c08.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn2c08.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn2c16.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn2c16.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn3p01.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn3p01.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn3p02.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn3p02.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn3p04.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn3p04.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn3p08.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn3p08.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn4a08.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn4a08.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn4a16.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn4a16.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn6a08.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn6a08.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/basn6a16.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/basn6a16.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Added: trunk/matplotlib/examples/tests/pngsuite/pngsuite.py
===================================================================
--- trunk/matplotlib/examples/tests/pngsuite/pngsuite.py                        
        (rev 0)
+++ trunk/matplotlib/examples/tests/pngsuite/pngsuite.py        2008-10-24 
14:05:07 UTC (rev 6318)
@@ -0,0 +1,24 @@
+"""
+This test loads a subset of the files in Willem van Schaik's PNG test
+suite available here:
+
+   http://libpng.org/pub/png/pngsuite.html
+
+The result should look like truth.png.
+"""
+
+from matplotlib import pyplot as plt
+import glob
+
+files = glob.glob("basn*.png")
+files.sort()
+
+plt.figure(figsize=(len(files), 2))
+
+for i, fname in enumerate(files):
+    data = plt.imread(fname)
+    plt.imshow(data, extent=[i,i+1,0,1])
+
+plt.gca().get_frame().set_facecolor("#ddffff")
+plt.gca().set_xlim(0, len(files))
+plt.show()

Added: trunk/matplotlib/examples/tests/pngsuite/truth.png
===================================================================
(Binary files differ)


Property changes on: trunk/matplotlib/examples/tests/pngsuite/truth.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream

Modified: trunk/matplotlib/src/_png.cpp
===================================================================
--- trunk/matplotlib/src/_png.cpp       2008-10-24 11:43:31 UTC (rev 6317)
+++ trunk/matplotlib/src/_png.cpp       2008-10-24 14:05:07 UTC (rev 6318)
@@ -213,19 +213,25 @@
 
   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) {
+    png_set_packing(png_ptr);
+  } else {
+  }
+
   // 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)
+      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)
+  } else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
     png_set_palette_to_rgb(png_ptr);
+  }
 
-
-  int bit_depth = info_ptr->bit_depth;
-  if (bit_depth == 16)  png_set_strip_16(png_ptr);
-
-
   png_set_interlace_handling(png_ptr);
   png_read_update_info(png_ptr, info_ptr);
 
@@ -247,8 +253,6 @@
 
   png_read_image(png_ptr, row_pointers);
 
-
-
   npy_intp dimensions[3];
   dimensions[0] = height;  //numrows
   dimensions[1] = width;   //numcols
@@ -256,19 +260,31 @@
 
   PyArrayObject *A = (PyArrayObject *) PyArray_SimpleNew(3, dimensions, 
PyArray_FLOAT);
 
-
-  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];
-      //if ((y<10)&&(x==10)) std::cout << "r = " << ptr[0] << " " << 
ptr[0]/255.0 << std::endl;
-      *(float*)(A->data + offset + 0*A->strides[2]) = (float)(ptr[0]/255.0f);
-      *(float*)(A->data + offset + 1*A->strides[2]) = (float)(ptr[1]/255.0f);
-      *(float*)(A->data + offset + 2*A->strides[2]) = (float)(ptr[2]/255.0f);
-      *(float*)(A->data + offset + 3*A->strides[2]) = rgba ? 
(float)(ptr[3]/255.0f) : 1.0f;
+  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;
+      }
     }
+  } 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.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to