Why do my images seem to be in the format of Bgra instead of Argb?

荒凉一梦 提交于 2019-12-10 09:31:55

问题


So, I am very confused over a quick test that I just ran. I am doing some image processing in C#. Get/SetPixel() have proven to be too slow, so I am using LockBits to get at the raw data.

However, I seem to have hit a situation which I can't figure out. While scanning the image, it seems that each pixel is laid out as Bgra, that is, blue byte, green byte, red byte, and alpha, in that order. I was under the impression that they would be laid out in Argb order. here is a sample of the code that I am using.

BitmapData baseData =
    m_baseImage.LockBits(new Rectangle(new Point(0, 0), m_baseImage.Size), 
        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Bitmap test = new Bitmap(m_baseImage.Width, m_baseImage.Height);           

byte* ptr = (byte*)baseData.Scan0;
for (int y = 0; y < m_baseImage.Height; ++y)
{                              
    for (int x = 0; x < m_baseImage.Width; ++x)
    {
        // this works, image is copied correctly
        Color c1 = Color.FromArgb(*(ptr + 3), *(ptr + 2), *(ptr + 1), *ptr);
        // below does not work!  Bytes are reversed.
        //Color c1 = Color.FromArgb(*ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3));

        test.SetPixel(x, y, c1);
        ptr += 4;
    }             
}

m_baseImage.UnlockBits(baseData);
pictureBox1.Image = m_baseImage;
pictureBox2.Image = test;

The first line which grabs the color of the base image works, the second does not. I am pretty sure that I am missing something very obvious here.


回答1:


Not only are the colors reversed BGRA, but the rows are reversed as well - the bottom of the image is the first in memory. It's just the way Windows has always worked.

The little-endian explanation seems obvious, but I don't think it's the truth. If you look at the definition of COLORREF in the Windows API, you'll notice that Red is the low order byte and Blue is the higher order; if you stored this as a single integer value, it would be RGB0.




回答2:


ARGB refers to the byte order in words fetched as words. If you fetch the bytes one at a time, you'll receive em low to hi as IBM PC's are little-endian



来源:https://stackoverflow.com/questions/580666/why-do-my-images-seem-to-be-in-the-format-of-bgra-instead-of-argb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!