Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap Stride And 4 bytes Relation?

Whats does this sentence mean:

The Stride property, holds the width of one row in bytes. The size of a row however may not be an exact multiple of the pixel size because for efficiency, the system ensures that the data is packed into rows that begin on a four byte boundary and are padded out to a multiple of four bytes.

like image 588
S.A.Parkhid Avatar asked Apr 20 '11 20:04

S.A.Parkhid


People also ask

What is bitmap stride?

The stride is the width of a single row of pixels (a scan line), rounded up to a four-byte boundary. If the stride is positive, the bitmap is top-down. If the stride is negative, the bitmap is bottom-up.

What is stride of an image?

The stride is the number of bytes from one row of pixels in memory to the next row of pixels in memory. Stride is also called pitch. If padding bytes are present, the stride is wider than the width of the image, as shown in the following illustration.

How do you measure image stride?

You can calculate the stride in a number of ways depending on the way that the storage needed for each pixel is specified – bits or bytes. If we assume that the bitmap needs three bytes per pixel, e.g. it uses an RGB format then the stride is: int stride=width*3 + (width %4);


3 Answers

Stride is padded. That means that it gets rounded up to the nearest multiple of 4. (assuming 8 bit gray, or 8 bits per pixel):

Width | stride
--------------
1     | 4
2     | 4
3     | 4
4     | 4
5     | 8
6     | 8
7     | 8
8     | 8
9     | 12
10    | 12
11    | 12
12    | 12

etc.

In C#, you might implement this like this:

static int PaddedRowWidth(int bitsPerPixel, int w, int padToNBytes) 
{
    if (padToNBytes == 0)
        throw new ArgumentOutOfRangeException("padToNBytes", "pad value must be greater than 0.");
    int padBits = 8* padToNBytes;
    return ((w * bitsPerPixel + (padBits-1)) / padBits) * padToNBytes;
}

static int RowStride(int bitsPerPixel, int width) { return PaddedRowWidth(bitsPerPixel, width, 4); }
like image 83
plinth Avatar answered Sep 25 '22 19:09

plinth


That means if your image width is 17 pixels and with 3 bytes for color, you get 51 bytes. So your image width in bytes is 51 bytes, then the stride is 52 bytes, which is the image width in bytes rounded up to the next 4-byte boundary.

like image 45
Chris O Avatar answered Sep 25 '22 19:09

Chris O


Let me give you an example:

This means that if the width is 160, stride will be 160. But if width is 161, then stride will be 164.

like image 21
Aliostad Avatar answered Sep 24 '22 19:09

Aliostad