From the documentation of libpng:
If you know your image size and pixel size ahead of time, you can allocate
row_pointers
prior to callingpng_read_png()
withrow_pointers = png_malloc(png_ptr, height*sizeof(png_bytep)); for (int i=0; i<height, i++) row_pointers[i]=png_malloc(png_ptr, width*pixel_size); png_set_rows(png_ptr, info_ptr, &row_pointers);
But the term "pixel size" isn't mentioned anywhere else in libpng's documentation. I'm curious to know what it means and how it is calculated.
It is referring to how many bytes are used to represent each pixel. E.g. three for RGB, four for RGBA, one for 8-bit greyscale or palletized color, etc.
The calculation shown there isn't correct for pixel sizes less than one byte. The greyscale and indexed-color types permit 1, 2, and 4 bits per pixel, in which case the size of a row would need to rounded up to the number of bytes required to hold it.
You calculate the size by multiplying the bit depth by the number of components, which is 1, 3, 1, 2, 4 respectively for the five rows in this table:
The range of possible pixel sizes is from 1/8 of a byte to 8 bytes. Then to get the length of a scan line, multiply the pixel size by the width, and round up to the next integer.