Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2872fa

Browse files
committed
Fix some problems in the last commit. Use a grayscale image, since multi-byte rgba is not supported by the image interpolation code.
1 parent 0e4f3cc commit c2872fa

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed
Binary file not shown.
Loading

lib/matplotlib/tests/test_png.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_pngsuite():
3131
def test_imread_png_uint16():
3232
from matplotlib import _png
3333
img = _png.read_png_int(os.path.join(os.path.dirname(__file__),
34-
'baseline_images/test_png/pngtest16rgba.png'))
35-
assert (img.dtype == np.uint16)
34+
'baseline_images/test_png/uint16.png'))
3635

37-
assert np.sum(img.flatten()) == 104855776
36+
assert (img.dtype == np.uint16)
37+
assert np.sum(img.flatten()) == 134184960

src/_png.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class _png_module : public Py::ExtensionModule<_png_module>
6060
Py::Object read_png_uint8(const Py::Tuple& args);
6161
Py::Object read_png_float(const Py::Tuple& args);
6262
Py::Object read_png_int(const Py::Tuple& args);
63-
PyObject* _read_png(const Py::Object& py_fileobj, const bool float_result, const int bit_depth = -1);
63+
PyObject* _read_png(const Py::Object& py_fileobj, const bool float_result, int result_bit_depth = -1);
6464
};
6565

6666
static void write_png_data(png_structp png_ptr, png_bytep data, png_size_t length)
@@ -301,7 +301,7 @@ static void read_png_data(png_structp png_ptr, png_bytep data, png_size_t length
301301

302302
PyObject*
303303
_png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
304-
const int result_bit_depth)
304+
int result_bit_depth)
305305
{
306306
png_byte header[8]; // 8 is the maximum size that can be checked
307307
FILE* fp = NULL;
@@ -506,17 +506,17 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
506506
}
507507
}
508508
} else {
509+
if (result_bit_depth < 0) {
510+
result_bit_depth = bit_depth;
511+
}
512+
509513
if (result_bit_depth == 8) {
510514
A = (PyArrayObject *) PyArray_SimpleNew(num_dims, dimensions, NPY_UBYTE);
515+
} else if (result_bit_depth == 16) {
516+
A = (PyArrayObject *) PyArray_SimpleNew(num_dims, dimensions, NPY_UINT16);
511517
} else {
512-
if (bit_depth == 8) {
513-
A = (PyArrayObject *) PyArray_SimpleNew(num_dims, dimensions, NPY_UBYTE);
514-
} else if (bit_depth == 16) {
515-
A = (PyArrayObject *) PyArray_SimpleNew(num_dims, dimensions, NPY_UINT16);
516-
} else {
517-
throw Py::RuntimeError(
518-
"_image_module::readpng: image has unknown bit depth");
519-
}
518+
throw Py::RuntimeError(
519+
"_image_module::readpng: image has unknown bit depth");
520520
}
521521

522522
if (A == NULL)
@@ -534,7 +534,7 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
534534
{
535535
png_uint_16* ptr = &reinterpret_cast<png_uint_16*>(row)[x * dimensions[2]];
536536

537-
if (bit_depth == 16) {
537+
if (result_bit_depth == 16) {
538538
for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++)
539539
{
540540
*(png_uint_16*)(A->data + offset + p*A->strides[2]) = ptr[p];
@@ -549,9 +549,16 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
549549
else
550550
{
551551
png_byte* ptr = &(row[x * dimensions[2]]);
552-
for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++)
553-
{
554-
*(png_byte*)(A->data + offset + p*A->strides[2]) = ptr[p];
552+
if (result_bit_depth == 16) {
553+
for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++)
554+
{
555+
*(png_uint_16*)(A->data + offset + p*A->strides[2]) = ptr[p];
556+
}
557+
} else {
558+
for (png_uint_32 p = 0; p < (png_uint_32)dimensions[2]; p++)
559+
{
560+
*(png_byte*)(A->data + offset + p*A->strides[2]) = ptr[p];
561+
}
555562
}
556563
}
557564
}

0 commit comments

Comments
 (0)