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

Skip to content

Commit 9f76dc7

Browse files
committed
Simplify read_png
1 parent d8c1d0d commit 9f76dc7

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

src/_png.cpp

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,14 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
240240
bool close_file = false;
241241
bool close_dup_file = false;
242242
PyObject *py_file = NULL;
243-
PyArrayObject *A = NULL;
244243
png_structp png_ptr = NULL;
245244
png_infop info_ptr = NULL;
246245
int num_dims;
247246
std::vector<png_bytep> row_pointers;
248247
png_uint_32 width = 0;
249248
png_uint_32 height = 0;
250249
int bit_depth;
250+
PyObject *result = NULL;
251251

252252
// TODO: Remove direct calls to Numpy API here
253253

@@ -375,70 +375,57 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
375375
if (float_result) {
376376
double max_value = (1 << bit_depth) - 1;
377377

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);
382379

383380
for (png_uint_32 y = 0; y < height; y++) {
384381
png_byte *row = row_pointers[y];
385382
for (png_uint_32 x = 0; x < width; x++) {
386383
if (bit_depth == 16) {
387384
png_uint_16 *ptr = &reinterpret_cast<png_uint_16 *>(row)[x * dimensions[2]];
388385
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;
390387
}
391388
} else {
392389
png_byte *ptr = &(row[x * dimensions[2]]);
393390
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;
395392
}
396393
}
397394
}
398395
}
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-
}
408396

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);
412400

413401
for (png_uint_32 y = 0; y < height; y++) {
414402
png_byte *row = row_pointers[y];
415403
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+
}
418410

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];
439421
}
440422
}
441423
}
424+
425+
result = A.pyobj();
426+
} else {
427+
PyErr_SetString(PyExc_RuntimeError, "image has unknown bit depth");
428+
goto exit;
442429
}
443430

444431
// free the png memory
@@ -467,10 +454,10 @@ static PyObject *_read_png(PyObject *filein, bool float_result)
467454
}
468455

469456
if (PyErr_Occurred()) {
470-
Py_XDECREF((PyObject *)A);
457+
Py_XDECREF(result);
471458
return NULL;
472459
} else {
473-
return (PyObject *)A;
460+
return result;
474461
}
475462
}
476463

0 commit comments

Comments
 (0)