Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stride and padding of an image

I wanted to use CreateBitmapFromMemory method, and its requires the stride as an input. and this stride confused me.

cbStride [in]

Type: UINT

The number of bytes between successive scanlines in pbBuffer.

and here it says: stride = image width + padding

  1. Why do we need these extra space(padding). why dont just image width.

This is how calculate the stride right?

    lWidthByte = (lWidth * bits + 7) / 8;

lWidth→pixel count

bits→bits per pixel

I suppuse deviding by 8 is for convert to byte. but,

  1. What is (+7) doing here?

and finally

    cbStride =((lWidthByte + 3) / 4) * 4;
  1. Whats going on here? (why not cbStride = lWidthByte)

Please help me to clear these.

like image 264
mhs Avatar asked Oct 21 '25 13:10

mhs


1 Answers

The use of padding is due to various (old and current) memory layout optimizations.
Having image pixel-rows have a length (in bytes) that is an integral multiple of 4/8/16 bytes can significantly simplify and optimized many image based operations. The reason is that these sizes allow proper storing and parallel pixel processing in the CPU registers, e.g. with SSE/MMX, without mixing pixels from two consecutive rows.
Without padding, extra code has to be inserted to handle partial WORD/DWORD pixel data since two consecutive pixels in memory might refer to one pixel on the right of one row and the left pixel on the next row.

If your image is a single channel image with 8 bit depth, i.e. grayscale in the range [0,255], then the stride would be the image width rounded up to the nearest multiple of 4 or 8 bytes. Note that the stride always specified in bytes even when a pixel may have more than one byte depth.

For images with more channels and/or more than one byte per pixel/channel, the stride would be the image width in bytes rounded up to the nearest multiple of 4 or 8 bytes.

The +7 and similar arithmetic examples you gave just make sure that the numbers are correctly rounded since integer math truncates the non-integer component of the division.
Just insert some numbers and see how it works. Don't forget to truncate (floor()) the intermediate division results.

like image 171
Adi Shavit Avatar answered Oct 23 '25 09:10

Adi Shavit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!