BOOL CAvi1::CreateFromPackedDIBPointer(LPBYTE pDIB, int iFrame)
 {
ASSERT(pDIB!=NULL);//Creates a full-color (no palette) DIB from a pointer to a
//full-color memory DIB  int i,j; lWidth=bih.biWidth;
 lHeight=bih.biHeight; DstBufSize=sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256+lWidth*lHeight;
 SrcLineBytes=(lWidth*bih.biBitCount+31)/32*4;
 SrcBufSize=SrcLineBytes*lHeight; if ((hTempImgData=LocalAlloc(LHND,DstBufSize))==NULL)
 {
    AfxMessageBox("Error alloc memory!",MB_OK,0);
    return false;
  }  lpBuf = pDIB; lpImgData=(LPBITMAPINFOHEADER)GlobalLock(lpBuf);
 lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData); lpPtr=(BYTE *)lpImgData;
 lpTempPtr=(char *)lpTempImgData;DstBf.bfType=0x4d42; //BM header
DstBf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256 + lWidth*lHeight;
DstBf.bfReserved1=0;
DstBf.bfReserved2=0;
DstBf.bfOffBits=sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + +sizeof(RGBQUAD)*256; //54   DstBi.biSize=sizeof(BITMAPINFOHEADER);
   DstBi.biWidth=lWidth;
   DstBi.biHeight=lHeight;
   DstBi.biPlanes=1;
   DstBi.biBitCount=8;
   DstBi.biCompression=BI_RGB;
   DstBi.biSizeImage=lWidth*lHeight;
   DstBi.biXPelsPerMeter=0;
   DstBi.biYPelsPerMeter=0;
   DstBi.biClrUsed=0;
   DstBi.biClrImportant=0;   memset(lpTempPtr,(BYTE)255,DstBufSize);
   memcpy(lpTempPtr,(LPSTR)&DstBi,sizeof(BITMAPINFOHEADER));
   lpTempPtr=lpTempPtr+sizeof(BITMAPINFOHEADER);
   memcpy(lpTempPtr,(LPSTR)Quad,sizeof(RGBQUAD)*256);
   lpTempPtr=lpTempPtr-sizeof(BITMAPINFOHEADER); for(i=0;i<lHeight;i++)
   {
    for(j=0;j<lWidth;j++)
    {
     lpPtr=(BYTE*)lpImgData+(SrcBufSize-SrcLineBytes-i*SrcLineBytes)+j*3;
     lpTempPtr=(char *)lpTempImgData+(DstBufSize-lWidth-i*lWidth)+j;
     r=*(lpPtr++);
     g=*(lpPtr++);
     b=*(lpPtr++);
     gray=(unsigned)(g*59+r*30+b*11)/100;
     if (gray>255)
      gray=255;
     *lpTempPtr=bGrayMap[gray];
    }
   } FileName.Format("Frame-%05d.bmp", iFrame);
 hf=_lcreat(FileName,0);
 _lwrite(hf,(LPSTR)&DstBf,sizeof(BITMAPFILEHEADER));
 _lwrite(hf,(LPSTR)lpTempImgData,DstBufSize);
 _lclose(hf); LocalUnlock(hTempImgData);
 LocalFree(hTempImgData);
 GlobalUnlock(lpBuf); return TRUE;
}

解决方案 »

  1.   

    整个程序的中下部位:
         r=*(lpPtr++);
         g=*(lpPtr++);
         b=*(lpPtr++);
    应改为:
         b=*(lpPtr++);
         g=*(lpPtr++);
         r=*(lpPtr++);
      

  2.   

    unsigned char RGB[3]; 
    unsigned char *p = lpImgData;
    for(i=lHeight-1;i>=0;i--)
       {
        for(j=0;j<lWidth;j++)
        {
           for(k=0;k<3;k++)
              RGB[i] = *p++;
         //lpPtr=(BYTE*)lpImgData+(SrcBufSize-SrcLineBytes-i*SrcLineBytes)+j*3;
         //lpTempPtr=(char *)lpTempImgData+(DstBufSize-lWidth-i*lWidth)+j;
         //r=*(lpPtr++);
         //g=*(lpPtr++);
         //b=*(lpPtr++);
         gray=(unsigned)(g*59+r*30+b*11)/100;
         if (gray>255)
          gray=255;
         lpTempPtr[i*lWidth+j]=gray;//bGrayMap[gray];
        }
       }