MS BMP specification issue

Wojciech Sobiesiak 96 Reputation points
2023-09-22T17:55:32.0066667+00:00

Hello.

I have some question about BMP specification, been talking to my very good friend GPT and he couldn't help with it.

Question is

What is the purpose of this 0 , 0 after color arrays (2x2 pixels)

unsigned char tablica[] = {66,77,70,0,0,0,0,0,0,0,54,0,0,0,40,0,0,0,2,0,0,0,2,0,0,0,1,0,24,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255,255,255,0,0,255,255,255,255,255,255,0,0,};

When I write 30px x 30px - I need to write additional 60 bytes.

I want to create hex-dec-binary bmp editor in php and realy need to understand what's going on :(

And do I understad that red pixel is at the right side of pixels value (B,G,R)?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,676 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,780 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 118.4K Reputation points
    2023-09-23T17:38:44.99+00:00

    I think that 0,0 represents the gaps between rows of pixel data. When the width is 2 and the image requires 24 bits per pixels, which is three bytes, then the width is 6 bytes. This width is rounded up to a four-byte boundary by padding. (Mentioned in Wikipedia too). The rounded width is 8 bytes, therefore the sequences of pixel data are followed by 0, 0.

    The rows that include pixel data and gaps (if required) are called “strides” or “scan lines”.

    The gaps appear in case of 30x30 image too.

    Probably there is a justification for such rounding, maybe related to optimizations for typical hardware.

    1 person found this answer helpful.

  2. Wojciech Sobiesiak 96 Reputation points
    2023-09-26T03:53:17.62+00:00

    cannot post answer here.

    Xiaopo is this correct?

    [http://infinityhost.ct8.pl/a/bmp_answer.html]

    OS shows 2,74 KB (bytes: 2 814)

    GPT method Image size: 2760 bytes

    GPT - "Certainly! To create a BMP file where there are no additional 2 bytes of padding after each row, you'll need to make sure that the width of the image (in pixels) multiplied by the bits per pixel is divisible evenly by 8.

    Let's say you want to create a 30x30 pixel image with 24 bits per pixel and no additional padding. Here's the calculation:

    Width: 30 pixels Height: 30 pixels Color depth: 24 bits per pixel

    Size of one row (in bits): 30 pixels * 24 bits per pixel = 720 bits

    Since 1 byte = 8 bits, size of one row (in bytes): 720 bits / 8 = 90 bytes

    Total size of image (without padding): 90 bytes/row * 30 rows = 2700 bytes

    So, for a 30x30 pixel image with 24 bits per pixel and no additional padding, the size of the BMP file would be 2700 bytes."

    Me after checking
    "Image size: 2760 bytes -------------------------------- Process exited after 0.03797 seconds with return value 0 Press any key to continue . . ."

    GPT "The result you're getting, 2760 bytes, is likely due to additional padding being added.

    In the case of BMP images, the size of each row is typically rounded up to the nearest multiple of 4 bytes (32 bits). This is to ensure proper alignment for memory access.

    Since your image has a width of 30 pixels and each pixel is represented using 24 bits (3 bytes), the calculated size of one row would be 90 bytes. However, to meet the alignment requirement, an additional 2 bytes of padding are added, resulting in a total row size of 92 bytes.

    So, for a 30x30 pixel image with 24 bits per pixel, the actual size with padding would be:"

    But OS still reports 2814 B.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.