www.pudn.com > p1.rar > ProcessAVI.cpp


//--------------------------------------------------------------------------- 
 
#include  
#pragma hdrstop 
 
#include "ProcessAVI.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm2 *Form2; 
//--------------------------------------------------------------------------- 
__fastcall TForm2::TForm2(TComponent* Owner) 
        : TForm(Owner) 
{ 
} 
//--------------------------------------------------------------------------- 
__fastcall TForm2::~TForm2() 
{ 
    if(pbitmapinfo!=NULL) 
     {delete pbitmapinfo; 
     pbitmapinfo=NULL; 
     } 
    if(pimagebuf!=NULL) 
     {delete pimagebuf; 
     pimagebuf=NULL; 
     } 
    if(pfileinfo!=NULL) 
     {delete pfileinfo; 
     pfileinfo=NULL; 
     } 
} 
void  TForm2::AviCreate(AnsiString &string) 
{ 
    HRESULT   hr; 
    pfileinfo=new AVIFILEINFO; 
    hr=AVIFileOpen(&pfile,string.c_str(),OF_READ,NULL); 
     
    //得到文件信息 
    hr=AVIFileInfo(pfile,pfileinfo,sizeof(AVIFILEINFO)); 
 
 
    cx=pfileinfo->dwWidth; 
    cy=pfileinfo->dwHeight; 
    clength=pfileinfo->dwLength; 
 
    pfile->GetStream(&pavistream,streamtypeVIDEO,0); 
    AVIStreamInfo (pavistream,&streaminfo,sizeof(AVISTREAMINFO)); 
     
    cdim= streaminfo.dwSuggestedBufferSize/cx/cy; 
 
     PUCHAR temp; 
     RGBQUAD *image_map; 
     switch (cdim) 
     { 
         case 1: 
             temp=new UCHAR[sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)]; 
             pbitmapinfo=(BITMAPINFO*)temp; 
             image_map=(RGBQUAD*)(temp+sizeof(BITMAPINFOHEADER)); 
             for(int i=0;i<256;i++) 
             { 
                 image_map[i].rgbBlue=i; 
                 image_map[i].rgbGreen=i; 
                 image_map[i].rgbRed=i; 
                 image_map[i].rgbReserved=0; 
             } 
             break; 
         case 0: 
             pbitmapinfo=new BITMAPINFO; 
             cdim=3; 
             break; 
         default: 
            break; 
     } 
 
 
 
    pbitmapinfo->bmiHeader.biBitCount=8*cdim; 
    pbitmapinfo->bmiHeader.biClrImportant=0; 
    pbitmapinfo->bmiHeader.biCompression=BI_RGB; 
    pbitmapinfo->bmiHeader.biHeight=cy; 
    pbitmapinfo->bmiHeader.biWidth=cx; 
    pbitmapinfo->bmiHeader.biPlanes=1; 
    pbitmapinfo->bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 
    pbitmapinfo->bmiHeader.biXPelsPerMeter=0; 
    pbitmapinfo->bmiHeader.biYPelsPerMeter=0; 
    pbitmapinfo->bmiHeader.biSizeImage=cx*cy; 
    pbitmapinfo->bmiHeader.biClrUsed=cdim==1?256:0; 
     
    pimagebuf=(BYTE*)new UCHAR[cx*cy*cdim];   
      
} 
 
BOOL TForm2::AviRead(int mFrame) 
{ 
     long hr; 
     //从mFrame开始读一帧 ,存入pimagebuf 
     hr=pavistream->Read(mFrame,1,pimagebuf,cx*cy*cdim,NULL,NULL); 
 
     if(hr==0) return TRUE; else return FALSE; 
} 
 
BOOL TForm2::AviFileSave(AnsiString openfile, AnsiString savefile) 
{ 
      long hr; 
      PAVIFILE psavefile; 
      AVISTREAMINFO strhdr; 
      PAVISTREAM ps; 
      long nFrames ; 
 
      long imagesize; 
      imagesize= cx*cy*cdim; 
 
      AVIFileOpen(&psavefile,savefile.c_str(),OF_WRITE | OF_CREATE,NULL); 
      strhdr=streaminfo; 
      hr = AVIFileCreateStream(psavefile,&ps,&strhdr); 
 
 
      for(nFrames=0;nFramesSetFormat(nFrames,&(pbitmapinfo->bmiHeader),40+1024) ; 
          else if(cdim==3)   hr=ps->SetFormat(nFrames,&(pbitmapinfo->bmiHeader),40) ; 
          AviRead(nFrames); 
          ImageProcess(pimagebuf,pimagebuf,cx,cy); 
          hr = AVIStreamWrite(ps,nFrames ,1,(LPBYTE) pimagebuf,imagesize, 
                              AVIIF_KEYFRAME, NULL,NULL); 
 
 
      } 
 
      AVIStreamClose(ps); 
      hr=AVIFileRelease(psavefile); 
 
      if(hr==0) return TRUE; else return FALSE; 
 
} 
void TForm2:: ImageProcess(PUCHAR SourceImage,PUCHAR ResultImage,int w,int h) 
{ 
    int i,j; 
    for(i=10;i<20;i++) 
        for(j=10;j<20;j++) 
           ResultImage[j*cx+i]=255; 
}