I’m writing a C++ library for an image format that is based on PNG. One stopping point for me is that I’m unsure as to how I ought to lay out the pixel data in memory; as far as I’m aware, there are two practical approaches:
- An array of size (width * height); each pixel can be accessed by array[y*width + x].
- An array of size (height), containing pointers to arrays of size (width).
The standard reference implementation for PNG (libpng) uses method 2 of the above, while I’ve seen others use method 1. Is one better than the other, or is each a method with its own pros and cons, to where a compromise must be made? Further, which format do most graphical display systems use (perhaps for ease of using the output of my library into other APIs)?
Off the top of my head:
height * widthamount of contiguous memory. Whereas, in case of #2, it has the freedom to allocate smaller chunks of contiguous memory of sizewidth(could as well beheight) off of areas that are free. (When you factor in the channels per pixel, the #1 may fail for even moderately sized images.)