FILE * fp = fopen(Usr.Source, "rb");
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
png_init_io(png_ptr, fp);
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
GraphicHeight = png_get_image_height(png_ptr, info_ptr); // Returns correct value
GraphicWidth = png_get_image_width(png_ptr, info_ptr); // Returns correct value
png_uint_32 rowBytes = png_get_rowbytes(png_ptr, info_ptr); // Returns expected value
png_bytep pngImg = (png_bytep) malloc(GraphicHeight * rowBytes); // Allocates fine
png_bytepp row_pointers = (png_bytepp) malloc(GraphicHeight * sizeof(png_bytep));
for (int rw = 0; rw < GraphicHeight; rw++) {
row_pointers[rw] = pngImg + rw * rowBytes;
png_read_row(png_ptr, row_pointers[rw], NULL); // Crashes on row 0
}
I have tried png_read_rows() and png_read_image(). They both call png_read_row() and crash. When the crash occurs, the trace looks like,
#9 0x00007ffff7f96ada in ?? () from /lib/x86_64-linux-gnu/libpng16.so.16
#10 0x00007ffff7f88dbf in png_read_row () from /lib/x86_64-linux-gnu/libpng16.so.16
All the sample code that I checked and the documentation says that it should work. So why does it crash?
You must not have read the example code and documentation carefully enough. png_read_png()
reads in the entire PNG image, so there are no rows left. You want to use png_read_info()
instead, to read just the information chunks.
You'll also want to use png_read_end()
and png_destroy_read_struct()
to finish reading and clean up at the end.