一 压缩
(1) 开始压缩前的准备
if (CompressHic==NULL)
{
memset(&compVars, 0, sizeof(COMPVARS));
compVars.cbSize = sizeof(COMPVARS);
//准备好于压缩数据的输入格式
bpiCompressIn.bmiHeader.biWidth = ImageWidth;
bpiCompressIn.bmiHeader.biHeight = ImageHeight;
bpiCompressIn.bmiHeader.biPlanes = 1;
bpiCompressIn.bmiHeader.biBitCount = ColorBits;
bpiCompressIn.bmiHeader.biCompression = BI_RGB;
bpiCompressIn.bmiHeader.biSizeImage = 0;
bpiCompressIn.bmiHeader.biXPelsPerMeter = 0;
bpiCompressIn.bmiHeader.biYPelsPerMeter = 0;
bpiCompressIn.bmiHeader.biClrUsed = 0;
bpiCompressIn.bmiHeader.biClrImportant = 0;
//准备好于压缩数据的输出格式
bpiCompressOut.bmiHeader.biWidth = ImageWidth;
bpiCompressOut.bmiHeader.biHeight = ImageHeight;
bpiCompressOut.bmiHeader.biPlanes = 1;
bpiCompressOut.bmiHeader.biBitCount = ColorBits;
bpiCompressOut.bmiHeader.biCompression = BI_RGB;
bpiCompressOut.bmiHeader.biSizeImage = 0;
bpiCompressOut.bmiHeader.biXPelsPerMeter = 0;
bpiCompressOut.bmiHeader.biYPelsPerMeter = 0;
bpiCompressOut.bmiHeader.biClrUsed = 0;
bpiCompressOut.bmiHeader.biClrImportant = 0;
//选择一个压缩算法
if (!ICCompressorChoose(FormMain->Handle, ICMF_CHOOSE_ALLCOMPRESSORS, NULL, NULL, &compVars, "Select a compressor"))
{
return;
}
else
{
CompressHic = compVars.hic;
bpiCompressOut.bmiHeader.biCompression = compVars.fccHandler;
//确定上面指定的输入输出格式是否被支持
if (ICCompressQuery(CompressHic, &bpiCompressIn, &bpiCompressOut)!=ICERR_OK)
{
//确定输入格式是否被支持,忽略输出格式的指定
if (ICCompressQuery(CompressHic, &bpiCompressIn, NULL)!=ICERR_OK)
{
ICCompressorFree(&compVars);
//if (ICClose(CompressHic)!=ICERR_OK) //不需要
//;
CompressHic = NULL;
return;
}
else
{
//若输入格式被支持,取得默认的输出格式
DWORD dwFormatSize = 0;
dwFormatSize = ICCompressGetFormatSize(CompressHic, &bpiCompressIn);
if (dwFormatSize==0xffffffff || dwFormatSize==0)
{
ICCompressorFree(&compVars);
if (ICClose(CompressHic)!=ICERR_OK) //估计也是不需要
;
return;
}
HANDLE h = GlobalAlloc(GHND, dwFormatSize);
BITMAPINFO *tmpBmi = (LPBITMAPINFO)GlobalLock(h);
if (ICCompressGetFormat(CompressHic, &bpiCompressIn, tmpBmi)!=ICERR_OK)
{
GlobalFree(tmpBmi);
ICCompressorFree(&compVars);
if (ICClose(CompressHic)!=ICERR_OK) //估计也是不需要
;
return;
}
memcpy(&bpiCompressOut, tmpBmi, dwFormatSize>sizeof(BITMAPINFOHEADER)?sizeof(BITMAPINFOHEADER):dwFormatSize);
GlobalFree(tmpBmi);
}
}
//准备开始
if (!ICSeqCompressFrameStart(&compVars, &bpiCompressIn))
{
ICCompressorFree(&compVars);
if (ICClose(CompressHic)!=ICERR_OK) //估计也是不需要
;
return;
}
}
}
(2). 停止压缩
ICSeqCompressFrameEnd(&compVars);
ICCompressorFree(&compVars);
CompressHic = NULL;
(3). 每次压缩一系列影像数据前只需执行一次步骤(1), 然后针对每个影像执行下面的操作.
想结束压缩时执行一次步骤(2)
static DWord FrameID = 0, tmpDWord;
memcpy(CompressDataBufferIn,..., size)//取得影像数据
//下面参数的含义可参看VFW的帮助.需要指出,输出buffer由函数自动分配内存,其它的buffer得自己预先分配好内存
if (ICCompress(CompressHic, 0, &bpiCompressOut.bmiHeader, CompressDataBufferOut,
&bpiCompressIn.bmiHeader, CompressDataBufferIn,
NULL, &tmpDWord, FrameID, 0, SuggestedQuality,
&bpiCompressIn.bmiHeader, CompressDataBufferInPrev)!=ICERR_OK)
{
//Error
return;
}
FrameID++;
size = ThisPtr->bpiCompressOut.bmiHeader.biSizeImage;//取得压缩后的数据大小
二 解压缩
(1) 开始前的准备
//根据原始影像数据定义解压缩的输入格式
bpiDecompressIn.bmiHeader.biWidth = ImageWidth;
bpiDecompressIn.bmiHeader.biHeight = ImageHeight;
bpiDecompressIn.bmiHeader.biPlanes = 1;
bpiDecompressIn.bmiHeader.biBitCount = ColorBits;
bpiDecompressIn.bmiHeader.biCompression = compVars.fccHandler;
bpiDecompressIn.bmiHeader.biSizeImage = 0;
bpiDecompressIn.bmiHeader.biXPelsPerMeter = 0;
bpiDecompressIn.bmiHeader.biYPelsPerMeter = 0;
bpiDecompressIn.bmiHeader.biClrUsed = 0;
bpiDecompressIn.bmiHeader.biClrImportant = 0;
bpiDecompressOut.bmiHeader.biWidth = ImageWidth;
bpiDecompressOut.bmiHeader.biHeight = ImageHeight;
bpiDecompressOut.bmiHeader.biPlanes = 1;
bpiDecompressOut.bmiHeader.biBitCount = ColorBits;
bpiDecompressOut.bmiHeader.biCompression = BI_RGB;
bpiDecompressOut.bmiHeader.biSizeImage = 0;//ImageWidth*ImageHeight*(int)((ColorBits+1)/8);
bpiDecompressOut.bmiHeader.biXPelsPerMeter = 0;
bpiDecompressOut.bmiHeader.biYPelsPerMeter = 0;
bpiDecompressOut.bmiHeader.biClrUsed = 0;
bpiDecompressOut.bmiHeader.biClrImportant = 0;
//按照指定的数据格式打开解压算法
DecompressHic = ICDecompressOpen(ICTYPE_VIDEO, compVars.fccHandler, &(bpiDecompressIn.bmiHeader), &(bpiDecompressOut.bmiHeader));
if (DecompressHic==NULL)
{
//
DecompressHic = ICDecompressOpen(ICTYPE_VIDEO, compVars.fccHandler, &(bpiDecompressIn.bmiHeader), NULL);
if (DecompressHic==NULL)
{
//不支持输入数据格式,无法解压缩
return;
}
else
{
//取得默认的输出数据格式
DWORD dwFormatSize = 0;
dwFormatSize = ICDecompressGetFormatSize(DecompressHic, &bpiDecompressIn);
if (dwFormatSize==0xffffffff || dwFormatSize==0)
{
if (ICClose(DecompressHic)!=ICERR_OK)
;
return;
}
HANDLE h = GlobalAlloc(GHND, dwFormatSize);
BITMAPINFO *tmpBmi = (LPBITMAPINFO)GlobalLock(h);
if (ICDecompressGetFormat(DecompressHic, &bpiDecompressIn, tmpBmi)!=ICERR_OK)
{
if (ICClose(DecompressHic)!=ICERR_OK)
;
GlobalFree(tmpBmi);
return;
}
memcpy(&bpiDecompressOut, tmpBmi, dwFormatSize>sizeof(BITMAPINFOHEADER)?sizeof(BITMAPINFOHEADER):dwFormatSize);
GlobalFree(tmpBmi);
}
}
//开始
if (ICDecompressbegin
(DecompressHic, &bpiDecompressIn, &bpiDecompressOut)!=ICERR_OK)
{
if (ICClose(DecompressHic)!=ICERR_OK)
;
return;
}
}
(2) 结束解压缩
if (ICDecompressEnd(DecompressHic)==ICERR_OK)
{
if (ICClose(DecompressHic)!=ICERR_OK)
;
DecompressHic = NULL;
}
(3) 和压缩数据类似, 开始前执行一次步骤(1), 准备好解压缩算法, 然后针对每一个需要解压的影像数据
执行下面的操作.结束解压缩时执行步骤(2)
bpiDecompressIn.bmiHeader.biSizeImage = size;
//解压缩并显示. 请预先分配好需要的buffer
if (ICDecompress(DecompressHic, 0, &(bpiDecompressIn.bmiHeader), CompressDataBufferOut,
&bpiDecompressOut.bmiHeader, DecompressDataBufferOut)==ICERR_OK)
{
//显示解压缩影像或作其它处理
HDC hDC = GetDC(PanelDecomDisplay->Handle);
if (StretchDIBits(hDC, 0, 0, PanelDecomDisplay->Width ,PanelDecomDisplay->Height,
0, 0, width, height,DecompressDataBufferOut, &(bpiDecompressOut) ,
DIB_RGB_COLORS, SRCCOPY)==GDI_ERROR)
;//error
ReleaseDC(PanelDecomDisplay->Handle,hDC);
}
上面的代码片断是经过实际运行通过的, 很抱歉不能给出全部代码. 祝好运!
欢迎有经验的朋友跟我联系, 大家共同提高.
wolf-lee@163.com