16、32位图读取到内存后,怎样取得每个点的RGB数值?
如果没有现成代码,麻烦各位老大们说说16、32位的结构也好,谢谢

解决方案 »

  1.   

    COLORREF GetPixel(
      HDC hdc,    // handle to DC
      int nXPos,  // x-coordinate of pixel
      int nYPos   // y-coordinate of pixel
    );
      

  2.   

    对于16和32位色的BMP文件,开始是头结构,接着是位图信息结构,后面是图象数据。图象数据从最下面一行开始按行逐一向上储存,32位色每像素4字节,前三字节是R、G、B;16色位图每像素2字节(16位),中间6位是G,高、低各5位分别是R和B,每行像素数据的字节数保持为4的整数倍,如果像素数据不是4的整数倍,则后面用无效数据补齐。
      

  3.   

    不知道是不是你想要的
            高位              2进制              低位
    32位     AAAAAAAA RRRRRRRR GGGGGGGG BBBBBBBB
    16位555 1 RRRRR GGGGG BBBBB
    16位565 RRRRR GGGGGG BBBBB16位在32位中的位置
    16位555 11111111 RRRRR000 GGGGG000 BBBBB000
    16位565 11111111 RRRRR000 GGGGGG00 BBBBB000
      

  4.   

    可以参考http://www.vckbase.com/document/viewdoc/?id=1786
      

  5.   

    #include"stdio.h"
    #include"iostream.h"
    #include"string.h"
    #include <conio.h>
    #include"stdlib.h"
    #include"windows.h"unsigned char *pBmpBuf;//读入图像数据的指针
    int bmpWidth;//图像的宽
    int bmpHeight;//图像的高
    RGBQUAD *pColorTable;//颜色表指针
    int biBitCount;//图像类型,每像素位数/*****************************************************/
    /*函数名称readBmp()*/bool readBmp(char *bmpName)
    {
    //二进制读方式打开指定的图像文件
    FILE *fp=fopen(bmpName,"rb");
    if(fp==0)  return 0; //跳过位图文件头结构BITMAPFILEHEADER
    fseek(fp,sizeof(BITMAPFILEHEADER),0); //定义位图信息头结构变量,读取位图信息头进内存,存放在变量head中
    BITMAPINFOHEADER head;
    fread(&head, sizeof(BITMAPINFOHEADER), 1,fp); //获取图像宽、高、每像素所占位数等信息
    bmpWidth=head.biWidth;
    bmpHeight=head.biHeight;
    biBitCount=head.biBitCount; //定义变量,计算图像每行像素所占的字节数(必须是4的倍数)
    //int lineByte=(bmpWidth * biBitCount/8+3)/4*4;
    int lineByte=(bmpWidth*biBitCount+31)/32*4; //灰度图像有颜色表,且颜色表表项为256
    if(biBitCount==8)
    {
    //申请颜色表所需要的空间,读颜色表进内存
    pColorTable=new RGBQUAD[256];
    fread(pColorTable,sizeof(RGBQUAD),256,fp);
    } //申请位图数据所需要的空间,读位图数据进内存
    pBmpBuf=new unsigned char[lineByte * bmpHeight];
    fread(pBmpBuf,1,lineByte * bmpHeight,fp);
    int BytesPerLine;
    BYTE data[999999];
    BYTE Color[1300][3];
    BytesPerLine=(bmpWidth*biBitCount+31)/32*4;
        fseek(fp,54,0);
    for(int i=bmpHeight-1;i>=0;i--)

    fread(data,1,BytesPerLine,fp); 

    for(int k=0;k <bmpWidth*3;k++)
    {
    if(k%3==2)
    {
    Color[k/3][2]=data[k-2];//b
    Color[k/3][1]=data[k-1];//g
    Color[k/3][0]=data[k-0];//r
    }
    } int m=1,n;
    cout<<"第"<<i<<"行的颜色red分量矩阵为:"<<endl;
    for(k=0;k<bmpWidth;k++)
    {
    n=int(Color[k][0]);
    if(n/100==0&&n/10==0)
    cout<<"  "<<n<<"    ";
    else if(n/100==0)
    cout<<" "<<n<<"    ";
    else
    cout<<n<<"    ";
    if(k==10*m-1)
    {
    cout<<endl;
    m++;
    }
    }
    cout<<endl<<endl<<endl; m=1;
    cout<<"第"<<i<<"行的颜色green分量矩阵为:"<<endl;
    for(k=0;k<bmpWidth;k++)
    {
    n=int(Color[k][1]);
    if(n/100==0&&n/10==0)
    cout<<"  "<<n<<"    ";
    else if(n/100==0)
    cout<<" "<<n<<"    ";
    else
    cout<<n<<"    ";
    if(k==10*m-1)
    {
    cout<<endl;
    m++;
    }
    }
    cout<<endl<<endl<<endl; m=1;
    cout<<"第"<<i<<"行的颜色blue分量矩阵为:"<<endl;
    for(k=0;k<bmpWidth;k++)
    {
    n=int(Color[k][2]);
    if(n/100==0&&n/10==0)
    cout<<"  "<<n<<"    ";
    else if(n/100==0)
    cout<<" "<<n<<"    ";
    else
    cout<<n<<"    ";
    if(k==10*m-1)
    {
    cout<<endl;
    m++;
    }
    }
    cout<<endl<<endl<<endl;
    } //关闭文件
    fclose(fp);
    return 1;
    }
    /*函数名称:saveBmp()函数参数:char *bmpName文件名字及路径;
    unsigned char *imgBuf待存盘的位图数据;
    int width以像素为单位待存盘位图的宽;
    int  height以像素为单位待存盘位图高;
    int biBitCount每像素所占位数;
    RGBQUAD *pColorTable颜色表指针
    返回值:0为失败,1为成功.
    说明:给定一个图像位图数据、宽、高、颜色表指针及每像素所占的位数等信息,
    将其写到指定文件中*/
    bool saveBmp(char *bmpName,unsigned char *imgBuf,int width,int height,
     int biBitCount, RGBQUAD *pColorTable)
    {
    //如果位图数据指针为0,则没有数据传入,函数返回
    if(!imgBuf) return 0; 

    //颜色表大小,以字节为单位,灰度图像颜色表为1024字节,彩色图像颜色表大小为0
    int colorTablesize=0;
    if(biBitCount==8)
    colorTablesize=1024;

    //待存储图像数据每行字节数为4的倍数
    //int lineByte=(width * biBitCount/8+3)/4*4;
    int lineByte=(bmpWidth*biBitCount+31)/32*4;

    //以二进制写的方式打开文件
    FILE *fp=fopen(bmpName,"wb");
    if(fp==0) return 0;

    //申请位图文件头结构变量,填写文件头信息
    BITMAPFILEHEADER fileHead;
    fileHead.bfType = 0x4D42;

    //bmp类型 
    //bfSize是图像文件4个组成部分之和
    fileHead.bfSize=sizeof(BITMAPFILEHEADER)+
    sizeof(BITMAPINFOHEADER)+colorTablesize+
    lineByte*height;
    fileHead.bfReserved1=0;
    fileHead.bfReserved2=0;

    //bfOffBits是图像文件前3个部分所需空间之和
    fileHead.bfOffBits=54+colorTablesize;

    //写文件头进文件
    fwrite(&fileHead,sizeof(BITMAPFILEHEADER),1,fp);

    //申请位图信息头结构变量,填写信息头信息
    BITMAPINFOHEADER head;
    head.biBitCount=biBitCount;
    head.biClrImportant=0;
    head.biClrUsed=0;head.biCompression=0;
    head.biHeight=height;
    head.biPlanes=1;
    head.biSize=40;
    head.biSizeImage=lineByte*height;
    head.biWidth=width;
    head.biXPelsPerMeter=0;
    head.biYPelsPerMeter=0;

    //写位图信息头进内存
    fwrite(&head,sizeof(BITMAPINFOHEADER),1,fp);

    //如果灰度图像,有颜色表,写入文件
    if(biBitCount==8)
    fwrite(pColorTable,sizeof(RGBQUAD),256,fp);

    //写位图数据进文件
    fwrite(imgBuf,height*lineByte,1,fp);

    //关闭文件fclose(fp);
    return 1;
    }
    void main()
    {
    //读入指定BMP文件进内存
    char readPath[]="D:\\4.bmp";
    readBmp(readPath);

    //输出图像的信息
    printf("width=%d,height=%d,biBitCount=%d\n",bmpWidth,bmpHeight,biBitCount); cout<<endl<<endl<<endl; //将图像数据存盘
    char writePath[]="D:\\2.bmp";
    saveBmp(writePath,pBmpBuf,bmpWidth,bmpHeight,biBitCount,pColorTable);

    //清除缓冲区,pBmpBuf和pColorTable是全局变量,在文件读入时申请的空间
    delete []pBmpBuf;
    if(biBitCount==8)
    delete []pColorTable;
    }