开源库CImg 数据格式存储之二(RGB 顺序)

在上一篇博客中已经初步说明了GDI和CImg数据的存储格式感谢博友 Imageshop 评论说明

CImg的说明文档中已有详细说明(详见上篇博客说明)

CImg的数据格式确实是RRRGGGBBB顺序存储的已经毫无疑问,但是其参考手册中对其他GDI

的数据格式说明是略有瑕疵,参考手册说其他GDI的数据格式是RGBRGBRGB,其实则不是经过验证

bmp类型的数据格式应该是BGRBGRBGR 下面用code验证

说明:使用MFC 同时用CImage和CImg加载同一幅图片

void ImageIO::loadImage(const BiCImg & image, T*& pImagePlane,int& width,int& height,int& nchannels)
{
    // get the image information
    
    width=image.width();
    height=image.height();
    nchannels=3;
    int rgb_leng=width*height;
    pImagePlane=new T[width*height*nchannels];

    // check whether the type is float point
    bool IsFloat=false;
    if(typeid(T)==typeid(double) || typeid(T)==typeid(float) || typeid(T)==typeid(long double))
        IsFloat=true;
    
    const unsigned char* plinebuffer;
    plinebuffer=image.data(0,0);
    for(int i=0;i<height;i++)
    {        
        //plinebuffer=image.scanLine(i);
        for(int j=0;j<width;j++)
        {
                        
pImagePlane[(i*width+j)*3]=plinebuffer[i*width+j+2*rgb_leng];//RGB b
                pImagePlane[(i*width+j)*3+1]=plinebuffer[i*width+j+rgb_leng];//RGB g
                pImagePlane[(i*width+j)*3+2]=plinebuffer[i*width+j];//RGB r
            
        }
    }
}

上述为正确的顺序,若改为如下代码

                pImagePlane[(i*width+j)*3]=plinebuffer[i*width+j];//RGB r 

                pImagePlane[(i*width+j)*3+1]=plinebuffer[i*width+j+rgb_leng];//RGB g

           pImagePlane[(i*width+j)*3+2]=plinebuffer[i*width+j+2*rgb_leng];//RGB b

实验效果如下图

右图为原始图片,明显左图的蓝色部分取代了原始图片的红色应该是BGR缺写成了RGB 

原文地址:https://www.cnblogs.com/sheshouyanhun/p/3270415.html