As for libpng, here is an example about how to use it to read and write a png. But actually the read() method can be simplified as below.
...
png_read_png( png, info, PNG_TRANSFORM_IDENTITY, NULL );
png_bytepp row_pointers = png_get_rows( png, info );
auto width = png_get_image_width( png, info );
auto height = png_get_image_height( png, info );
...
Note, png_read_png() must be called first before calling png_get_rows(), otherwise the return pointer row_pointers would be invalid. Its manual says:
After you have called png_read_png(), you can retrieve the image data with row_pointers = png_get_rows(png_ptr, info_ptr);