@@ -240,14 +240,14 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
240
240
bool close_file = false ;
241
241
bool close_dup_file = false ;
242
242
PyObject *py_file = NULL ;
243
- PyArrayObject *A = NULL ;
244
243
png_structp png_ptr = NULL ;
245
244
png_infop info_ptr = NULL ;
246
245
int num_dims;
247
246
std::vector<png_bytep> row_pointers;
248
247
png_uint_32 width = 0 ;
249
248
png_uint_32 height = 0 ;
250
249
int bit_depth;
250
+ PyObject *result = NULL ;
251
251
252
252
// TODO: Remove direct calls to Numpy API here
253
253
@@ -375,70 +375,57 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
375
375
if (float_result) {
376
376
double max_value = (1 << bit_depth) - 1 ;
377
377
378
- A = (PyArrayObject *)PyArray_SimpleNew (num_dims, dimensions, NPY_FLOAT);
379
- if (A == NULL ) {
380
- goto exit ;
381
- }
378
+ numpy::array_view<float , 3 > A (dimensions);
382
379
383
380
for (png_uint_32 y = 0 ; y < height; y++) {
384
381
png_byte *row = row_pointers[y];
385
382
for (png_uint_32 x = 0 ; x < width; x++) {
386
383
if (bit_depth == 16 ) {
387
384
png_uint_16 *ptr = &reinterpret_cast <png_uint_16 *>(row)[x * dimensions[2 ]];
388
385
for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++) {
389
- *( float *) PyArray_GETPTR3 (A, y, x, p) = (float )(ptr[p]) / max_value;
386
+ A ( y, x, p) = (float )(ptr[p]) / max_value;
390
387
}
391
388
} else {
392
389
png_byte *ptr = &(row[x * dimensions[2 ]]);
393
390
for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++) {
394
- *( float *) PyArray_GETPTR3 (A, y, x, p) = (float )(ptr[p]) / max_value;
391
+ A ( y, x, p) = (float )(ptr[p]) / max_value;
395
392
}
396
393
}
397
394
}
398
395
}
399
- } else {
400
- if (bit_depth == 8 ) {
401
- A = (PyArrayObject *)PyArray_SimpleNew (num_dims, dimensions, NPY_UBYTE);
402
- } else if (bit_depth == 16 ) {
403
- A = (PyArrayObject *)PyArray_SimpleNew (num_dims, dimensions, NPY_UINT16);
404
- } else {
405
- PyErr_SetString (PyExc_RuntimeError, " image has unknown bit depth" );
406
- goto exit ;
407
- }
408
396
409
- if (A == NULL ) {
410
- goto exit ;
411
- }
397
+ result = A. pyobj ();
398
+ } else if (bit_depth == 16 ) {
399
+ numpy::array_view<png_uint_16, 3 > A (dimensions);
412
400
413
401
for (png_uint_32 y = 0 ; y < height; y++) {
414
402
png_byte *row = row_pointers[y];
415
403
for (png_uint_32 x = 0 ; x < width; x++) {
416
- if (bit_depth == 16 ) {
417
- png_uint_16 *ptr = &reinterpret_cast <png_uint_16 *>(row)[x * dimensions[2 ]];
404
+ png_uint_16 *ptr = &reinterpret_cast <png_uint_16 *>(row)[x * dimensions[2 ]];
405
+ for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++) {
406
+ A (y, x, p) = ptr[p];
407
+ }
408
+ }
409
+ }
418
410
419
- if (bit_depth == 16 ) {
420
- for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++) {
421
- *(png_uint_16 *)PyArray_GETPTR3 (A, y, x, p) = ptr[p];
422
- }
423
- } else {
424
- for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++) {
425
- *(png_byte *)PyArray_GETPTR3 (A, y, x, p) = ptr[p] >> 8 ;
426
- }
427
- }
428
- } else {
429
- png_byte *ptr = &(row[x * dimensions[2 ]]);
430
- if (bit_depth == 16 ) {
431
- for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++) {
432
- *(png_uint_16 *)PyArray_GETPTR3 (A, y, x, p) = ptr[p];
433
- }
434
- } else {
435
- for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++) {
436
- *(png_byte *)PyArray_GETPTR3 (A, y, x, p) = ptr[p];
437
- }
438
- }
411
+ result = A.pyobj ();
412
+ } else if (bit_depth == 8 ) {
413
+ numpy::array_view<png_byte, 3 > A (dimensions);
414
+
415
+ for (png_uint_32 y = 0 ; y < height; y++) {
416
+ png_byte *row = row_pointers[y];
417
+ for (png_uint_32 x = 0 ; x < width; x++) {
418
+ png_byte *ptr = &(row[x * dimensions[2 ]]);
419
+ for (png_uint_32 p = 0 ; p < (png_uint_32)dimensions[2 ]; p++) {
420
+ A (y, x, p) = ptr[p];
439
421
}
440
422
}
441
423
}
424
+
425
+ result = A.pyobj ();
426
+ } else {
427
+ PyErr_SetString (PyExc_RuntimeError, " image has unknown bit depth" );
428
+ goto exit ;
442
429
}
443
430
444
431
// free the png memory
@@ -467,10 +454,10 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
467
454
}
468
455
469
456
if (PyErr_Occurred ()) {
470
- Py_XDECREF ((PyObject *)A );
457
+ Py_XDECREF (result );
471
458
return NULL ;
472
459
} else {
473
- return (PyObject *)A ;
460
+ return result ;
474
461
}
475
462
}
476
463
0 commit comments