// 读取图像数据,WIDTHBYTES宏用于生成每行字节数
int nWidthBytes = WIDTHBYTES((pBMIH->biWidth)*pBMIH->biBitCount);
// 申请biHeight个长度为biWidthBytes的数组,用他们来保存位图数据
lpData = new LPBYTE[(pBMIH->biHeight)];
for(int i=0; i<(pBMIH->biHeight); i++)
{
lpData[i] = new BYTE[nWidthBytes];
file.Read(lpData[i], nWidthBytes);
}
没读完一行后,怎么读下一行,难道执行完read后会指向下一行吗?

解决方案 »

  1.   

    BOOL CImg::AttachFromFile(CFile &file)
    {
    // 文件数据
    LPBYTE  *lpData;
    // 位图信息头
    BITMAPINFOHEADER *pBMIH;
    // 颜色表指针
    LPVOID lpvColorTable = NULL;
    // 颜色表颜色数目
    int nColorTableEntries;
    BITMAPFILEHEADER bmfHeader;
    // 读取文件头
    if(!file.Read(&bmfHeader, sizeof(bmfHeader)))
    return FALSE;
    // 检查开头两字节是否为BM
    if(bmfHeader.bfType != MAKEWORD('B', 'M'))
    {
    return FALSE;
    }
    // 读取信息头
    pBMIH= (BITMAPINFOHEADER*)newBYTE[bmfHeader.bfOffBits - sizeof(bmfHeader)];
    if(!file.Read(pBMIH, bmfHeader.bfOffBits - sizeof(bmfHeader)))
    {
    delete pBMIH;
    return FALSE;
    }
    // 定位到颜色表
    nColorTableEntries = 
    (bmfHeader.bfOffBits-sizeof(bmfHeader)-sizeof(BITMAPINFOHEADER))/sizeof(RGBQUAD);
    if(nColorTableEntries > 0)
    {
    lpvColorTable = pBMIH + 1;
    }
    pBMIH->biHeight = abs(pBMIH->biHeight);
    // 读取图像数据,WIDTHBYTES宏用于生成每行字节数
    int nWidthBytes = WIDTHBYTES((pBMIH->biWidth)*pBMIH->biBitCount);
    // 申请biHeight个长度为biWidthBytes的数组,用他们来保存位图数据
    lpData = new LPBYTE[(pBMIH->biHeight)];
    for(int i=0; i<(pBMIH->biHeight); i++)
    {
    lpData[i] = new BYTE[nWidthBytes];
    file.Read(lpData[i], nWidthBytes);

    }
    // 更新数据
    CleanUp();
    m_lpData = lpData;
    m_pBMIH = pBMIH;
    m_lpvColorTable = lpvColorTable;
    m_nColorTableEntries = nColorTableEntries;
    return TRUE;
    }
    整个程序,还有lpvColorTable = pBMIH + 1;什么意思?
      

  2.   

    这段代码是将一个位图文件的内容读取出来,分别存储到不同的结构对象中。
    lpvColorTable = pBMIH+1是定位调色板信息地址的。当存在调色板时(256色以内需要调色板),调色板的指针指向位图文件描述体之后。pBMIH+1,这里的1代表着一个BITMAPINFOHEADER结构体大小