23

Why are BMP images stored upside down and zero-padded so they are four-byte aligned?

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • 1
    possible duplicate of [Why must "stride" in the System.Drawing.Bitmap constructor be a multiple of 4?](http://stackoverflow.com/questions/2185944/why-must-stride-in-the-system-drawing-bitmap-constructor-be-a-multiple-of-4) – Hans Passant Dec 01 '11 at 18:09
  • 1
    http://support.microsoft.com/kb/q81498/ claims the upsidedownness is an artifact of being backwards compatible with Presentation Manager, which means it's something that IBM did for OS/2. – Marc B Dec 01 '11 at 18:13
  • 2
    They are upside-down for compatibility with OS/2 Presentation Manager. It can actually be either, top line first bitmaps have a negative height. – Hans Passant Dec 01 '11 at 18:14
  • @HansPassant, the ability to have a negative height is a feature added well after the bitmap format was popularized. – Mark Ransom Dec 01 '11 at 19:38
  • Guys settle the negative height argument this BMP browser drop-in/png/jpg universal "uncompressed" replacement library ain't gonna write itself!! – bobobobo Dec 01 '11 at 19:57

2 Answers2

42

Here's a quote from Petzold:

So, in DIBs, the bottom row of the image is the first row of the file, and the top row of the image is the last row in the file. This is called a bottom-up organization. Because this organization is counterintuitive, you may ask why it's done this way.

Well, it all goes back to the OS/2 Presentation Manager. Someone at IBM decided that all coordinate systems in PM—including those for windows, graphics, and bitmaps—should be consistent. This provoked a debate: Most people, including programmers who have worked with full-screen text programming or windowing environments, think in terms of vertical coordinates that increase going down the screen. However, hardcore computer graphics programmers approach the video display from a perspective that originates in the mathematics of analytic geometry. This involves a rectangular (or Cartesian) coordinate system where increasing vertical coordinates go up in space.

In short, the mathematicians won. Everything in PM was saddled with a bottom-left origin, including window coordinates. And that's how DIBs came to be this way.

Source: Charles Petzold, Programming for Windows 5th Edition, Chapter 15.

pezcode
  • 5,490
  • 2
  • 24
  • 37
  • 4
    Interesting that they kept this representation for DIBs but changed it for window coordinates, reintroducing the inconsistency. – Mark Ransom Dec 01 '11 at 19:36
-5

They are stored accordingly to a display coordinate system. (0, 0) is at the upper left corner. X (corresponds to width) goes from left to right. Y (corresponds to height) goes from up to bottom.

As for "4 Byte question" see Why must "stride" in the System.Drawing.Bitmap constructor be a multiple of 4?

Community
  • 1
  • 1
Roman Byshko
  • 8,591
  • 7
  • 35
  • 57