First, let's explain the concept of scanning rows: scanning rows refers to the row of pixel bytes in the image in the memory. Now let's discuss how image data is stored in computers.

The Byte arrays of image data are generally positive. The scanning rows are stored from the bottom up. That is to say, the starting byte in the array indicates the lower left corner of the image. Of course, there is another kind of BMP that is backward. In potoshop7.0, when saving BMP, there is an option to flip the row order. In this way, BMP scans rows for storage from top to bottom, that is, the starting byte in the array indicates the upper left corner of the image. Generally, positive BMP is used.

The size of the Image scan line depends on the number of colors of the image and the Image Width expressed in pixels.

The BMP format also has a very important rule: the byte data of each scan row must be divisible by four, that is, DWORD alignment (DWORD is a data type, length: 4 bytes ). If the number of bytes in a row of an image cannot be fully divided by four, you must add 0 at the end of each row to meet the requirements.

Set the number of digits of the image to N, the image width to W (expressed by the number of bytes), the height to H (expressed by the number of bytes), and the number of scanned rows to L. Now let's look at some situations of the image.

1. The number of colors of the image = the N power of 2. The number of digits in an image is the number of digits in a binary representation of a pixel. The color of a 16-bit image is 2 to the power of 16 = 65536, which is what we often call 60 thousand colors.

2. One byte is 8 bits, so that the number of bytes required for each pixel is N/8.

3. Number of bytes per line of the image = w x n/8

4. The number of bytes per Scan Line of the image must be 4 bytes apart.

When w * n/8 can be divisible by 4, L = W * n/8;

When w * n/8 cannot be divisible by 4, the number of bytes of data in each row must be 4-(w * n/8)

MoD 4 (mod is the remainder operation, X mod Y refers to the remainder of X divided by Y), so l = W * n/8 + 4-(w * n/8) mod

4.

5. Size of the image data array in bytes = L * H

6. The size of the image file. If the image is saved using Photoshop, in addition to the file header and data array, add the remaining two bytes of 0. Size of the file header. The 24-Bit Bitmap is 36 h = 54,16-bit r5g6b5 is 46 h = 70, and the 8-bit is 436 H = 1078.

For example, a 16-bit r5g6b5 image, n = 16, if W = 13, H = 11, w * n/8 = 26 cannot be divisible by 4, the number of bytes after each row of data must be 4-26

MoD

4 = 2. In HW, 0000 is required for every 26 bytes. L = W * n/8 + 2 = 28. Size of the image data array in bytes = L * H = 28*11 = 308, and size of the image file = 308 + 70 + 2 = 380.

The image data in T4/T5 is in the 16-bit r5g6b5 format, but unlike the r5g6b5 in the computer, it does not require the scanning row to be divisible by 4, so there is no zero-padding problem. For r5grb6, because w * n/8 = W * 16/8 = W * 2, when the Image Width W is an odd number, if such r5g6b5 is replaced with the axf, the zero value must be removed.

For T4/T5, the size of the image data array is equal to L * H = W * n/8 * H = W * H * 2.