@@ -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
6666static 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
302302PyObject*
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