@@ -60,7 +60,7 @@ class _png_module : public Py::ExtensionModule<_png_module>
60
60
Py::Object read_png_uint8 (const Py::Tuple& args);
61
61
Py::Object read_png_float (const Py::Tuple& args);
62
62
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 );
64
64
};
65
65
66
66
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
301
301
302
302
PyObject*
303
303
_png_module::_read_png (const Py::Object& py_fileobj, const bool float_result,
304
- const int result_bit_depth)
304
+ int result_bit_depth)
305
305
{
306
306
png_byte header[8 ]; // 8 is the maximum size that can be checked
307
307
FILE* fp = NULL ;
@@ -506,17 +506,17 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
506
506
}
507
507
}
508
508
} else {
509
+ if (result_bit_depth < 0 ) {
510
+ result_bit_depth = bit_depth;
511
+ }
512
+
509
513
if (result_bit_depth == 8 ) {
510
514
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);
511
517
} 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" );
520
520
}
521
521
522
522
if (A == NULL )
@@ -534,7 +534,7 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result,
534
534
{
535
535
png_uint_16* ptr = &reinterpret_cast <png_uint_16*>(row)[x * dimensions[2 ]];
536
536
537
- if (bit_depth == 16 ) {
537
+ if (result_bit_depth == 16 ) {
538
538
for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++)
539
539
{
540
540
*(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,
549
549
else
550
550
{
551
551
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
+ }
555
562
}
556
563
}
557
564
}
0 commit comments