windows 为什么 bmps 存储颠倒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8346115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:34:00  来源:igfitidea点击:

Why are bmps stored upside down?

windowsimagelegacybmp

提问by bobobobo

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

为什么 BMP 图像颠倒存储并用零填充,因此它们是四字节对齐的?

回答by pezcode

Here's a quote from Petzold:

这是来自 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.

因此,在 DIB 中,图像的底行是文件的第一行,图像的顶行是文件的最后一行。这称为自下而上的组织。因为这个组织是违反直觉的,你可能会问为什么这样做。

好吧,这一切都可以追溯到 OS/2 Presentation Manager。IBM 的某个人决定 PM 中的所有坐标系——包括那些用于窗口、图形和位图的坐标系——应该是一致的。这引发了一场争论:大多数人,包括在全屏文本编程或窗口环境中工作过的程序员,都认为垂直坐标会在屏幕下方增加。然而,核心计算机图形程序员从源自解析几何数学的角度来处理视频显示。这涉及一个矩形(或笛卡尔)坐标系,其中垂直坐标在空间中增加。

简而言之,数学家赢了。PM 中的所有内容都带有左下角的原点,包括窗口坐标。这就是 DIB 是如何变成这样的。

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

来源:Charles Petzold,Windows 编程第 5 版,第 15 章。

回答by Beginner

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.

它们相应地存储到显示坐标系中。(0, 0) 位于左上角。X(对应于宽度)从左到右。Y(对应于高度)从上到下。

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

至于“4 字节问题”,请参阅为什么 System.Drawing.Bitmap 构造函数中的“步幅”必须是 4 的倍数?