@@ -48,6 +48,8 @@ class _png_module : public Py::ExtensionModule<_png_module>
48
48
" read_png_float(fileobj)" );
49
49
add_varargs_method (" read_png_uint8" , &_png_module::read_png_uint8,
50
50
" read_png_uint8(fileobj)" );
51
+ add_varargs_method (" read_png_int" , &_png_module::read_png_int,
52
+ " read_png_int(fileobj)" );
51
53
initialize (" Module to write PNG files" );
52
54
}
53
55
@@ -57,7 +59,8 @@ class _png_module : public Py::ExtensionModule<_png_module>
57
59
Py::Object write_png (const Py::Tuple& args);
58
60
Py::Object read_png_uint8 (const Py::Tuple& args);
59
61
Py::Object read_png_float (const Py::Tuple& args);
60
- PyObject* _read_png (const Py::Object& py_fileobj, const bool float_result);
62
+ Py::Object read_png_int (const Py::Tuple& args);
63
+ PyObject* _read_png (const Py::Object& py_fileobj, const bool float_result, int result_bit_depth = -1 );
61
64
};
62
65
63
66
static void write_png_data (png_structp png_ptr, png_bytep data, png_size_t length)
@@ -297,7 +300,8 @@ static void read_png_data(png_structp png_ptr, png_bytep data, png_size_t length
297
300
}
298
301
299
302
PyObject*
300
- _png_module::_read_png (const Py::Object& py_fileobj, const bool float_result)
303
+ _png_module::_read_png (const Py::Object& py_fileobj, const bool float_result,
304
+ int result_bit_depth)
301
305
{
302
306
png_byte header[8 ]; // 8 is the maximum size that can be checked
303
307
FILE* fp = NULL ;
@@ -502,7 +506,18 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result)
502
506
}
503
507
}
504
508
} else {
505
- A = (PyArrayObject *) PyArray_SimpleNew (num_dims, dimensions, NPY_UBYTE);
509
+ if (result_bit_depth < 0 ) {
510
+ result_bit_depth = bit_depth;
511
+ }
512
+
513
+ if (result_bit_depth == 8 ) {
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);
517
+ } else {
518
+ throw Py::RuntimeError (
519
+ " _image_module::readpng: image has unknown bit depth" );
520
+ }
506
521
507
522
if (A == NULL )
508
523
{
@@ -518,17 +533,32 @@ _png_module::_read_png(const Py::Object& py_fileobj, const bool float_result)
518
533
if (bit_depth == 16 )
519
534
{
520
535
png_uint_16* ptr = &reinterpret_cast <png_uint_16*>(row)[x * dimensions[2 ]];
521
- for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++)
522
- {
523
- *(png_byte*)(A->data + offset + p*A->strides [2 ]) = ptr[p] >> 8 ;
536
+
537
+ if (result_bit_depth == 16 ) {
538
+ for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++)
539
+ {
540
+ *(png_uint_16*)(A->data + offset + p*A->strides [2 ]) = ptr[p];
541
+ }
542
+ } else {
543
+ for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++)
544
+ {
545
+ *(png_byte*)(A->data + offset + p*A->strides [2 ]) = ptr[p] >> 8 ;
546
+ }
524
547
}
525
548
}
526
549
else
527
550
{
528
551
png_byte* ptr = &(row[x * dimensions[2 ]]);
529
- for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++)
530
- {
531
- *(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
+ }
532
562
}
533
563
}
534
564
}
@@ -569,6 +599,12 @@ _png_module::read_png_float(const Py::Tuple& args)
569
599
570
600
Py::Object
571
601
_png_module::read_png_uint8 (const Py::Tuple& args)
602
+ {
603
+ throw Py::RuntimeError (" read_png_uint8 is deprecated. Use read_png_int instead." );
604
+ }
605
+
606
+ Py::Object
607
+ _png_module::read_png_int (const Py::Tuple& args)
572
608
{
573
609
args.verify_length (1 );
574
610
return Py::asObject (_read_png (args[0 ], false ));
0 commit comments