Bitmap Stride And 4 bytes Relation?

后端 未结 3 1939
抹茶落季
抹茶落季 2021-02-06 00:00

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 pix

3条回答
  •  余生分开走
    2021-02-06 00:44

    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); }
    

提交回复
热议问题