Given the height of the image, how to calculate GL_UNPACK_ALIGNMENT?

A large image library gives the image as width, pitch, pitch, and then pixels of the array. OpenGL, however, does not take the step directly; instead, it expects:

glPixelStorei(GL_UNPACK_ALIGNMENT, align); glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length); 

row_length pitch / bytes_per_pixel , but how to calculate the correct value for align ?

+1
source share
2 answers

For the vast majority of images that you see, you do not need GL_ROW_LENGTH . Setting this value to a non-zero value can greatly degrade download performance; use it only when you absolutely need it.

For most images, the step will be just ( width * bpp ) aligned with some border. For example, if bpp is 3 (bytes per pixel) and the width is 9, you get 27. This is not a very good line number for many computers, so they often align it to the nearest multiple of 4. Thus, the height that you will see in most images equal to 28.

So, to calculate the alignment, simply calculate the highest power of two (up to 8), which are divided by height. If the image height is 28, the highest power is 4. Set the value to GL_UNPACK_ALIGNMENT . You can usually stay at 4 because most of the images will use the most alignment.

The time when you need to use GL_ROW_LENGTH is when the calculated step ( align(width * bpp, alignment) ) does not match the actual tash. If this happens, you need to use alignment and string length. But it is such a rare occurrence that you probably should simply discard any image that uses such an incredibly wrong step.

0
source

GL_UNPACK_ALIGNMENT is the byte offset at which the first pixel in the line begins with the image. Setting it to one is almost always a safe bet (i.e., it works with almost every type of image [and it never worked for me]).

The default value is four, which is great for RGBA images, but with errors for RGB images (you will immediately know when you see the image - it will look like a set of diagonal stripes).

-1
source

Source: https://habr.com/ru/post/975087/


All Articles