Thread: Bitmap. 4 Byte Boundry...?

  1. #1
    Join Date
    Aug 2001

    Bitmap. 4 Byte Boundry...?

    I was looking over's bitmap-loading
    tutorial, and he says this:

    Something interesting to note about the BMP file
    format is that each scan line is padded to the
    nearest 4-byte boundry. So, if the image read
    has a width that is not divisible by four, say,
    21 bytes, there would be 3 bytes of padding at
    the end of every scan line.

    What exactly does this mean? Scan line? Padded
    to the nearest 4-byte boundry? Will someone
    please explain this to me in idiot-terms?

    Thank you,
    Staying away from General.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001

    Exclamation I'll try to answer

    Imagine a bitmap as a lot of "rows" (rows of pixels) lying on top of each other to from a picture. One of these rows is called a scanline. The length of this scanline is the width of the picture.

    To speed up mathematical operations with bitmaps, these scanlines must be divisible by 4 (4, 8, 12... 40... etc). Why? Because it is way faster to multiply a number with 4 than say 3. It has to do with bit-shifting, but I'm not going to explain that here.

    If you have a bitmap with the width 9, the scanline is actually 12 pixels long. the last 3 bytes (pixels) is not in the actual bitmap. So when you read the bitmap into the memory, you have to have a function to skip these bytes, otherwise the bitmap will look "angled" (which is a pretty cool effect by the way ).

    I don't have time to make up some code for that now. Perhaps someone else can, or you might try it on your own (I did ).

    Good luck

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2001
    The reason that the bitmaps are aligned to 4-byte boundaries is because Intel processors only can read data at a 4-byte boundary. If values aren't aligned, Intels can't work with them as fast as if they were aligned because it will have to read more data to get the actual value. That's the only reason.

  4. #4
    None of the above answers is completely accurate. Here is the CORRECT answer:

    The reason you read/write data in/out from RAM and the CPU 4-bytes at a time, is because this is the current _native word length_ of the processor-- 32 bits. aka "unsigned long".

    Because it is the native on-the-bus word-size of most processors today (any processor, whether it's Motorola 680x0 or Intel P XX, or AMD whatever) it doesn't have to be masked or shifted by the processor in order to worked with odd-numbers of bits.

    This is called _performance_ code. Whenever you allocate a bitmap or a pixmap, you allocate each scanline (ie. "row") so it rounds up to the next nearest boundary.

    This is why I am always preaching about 'working with the machine"--- if you code such that the CPU has to do less work to execute your code, it runs lots faster.


  5. #5
    Registered User
    Join Date
    Sep 2001
    That was what I was trying to say. If the processor tries to read a DWORD from a non-aligned memory, it has to read both the DWORD in front and the DWORD behind and combine the results.
    // Gliptic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  4. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  5. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM