设为首页 加入收藏

TOP

图像(层)正常混合模式详解(下) (一)
2014-11-24 00:36:37 来源: 作者: 【 】 浏览:85
Tags:图像 正常 混合 模式 详解

http://www.2cto.com/kf/201201/116377.html一文中开始时说过,图像的合成操作包括图像显示、图像拷贝、图像拼接以及的图层拼合叠加等,本文在http://www.2cto.com/kf/201201/116377.html基础上谈谈图像拼接和图像显示。

图像拼接比较简单,只要在图像正常混合函数ImageMixer基础上定位图像混合坐标就可以了。下面是一个有图像混合坐标的ImageMixer函数:


// 获取子图数据
BOOL GetSubBitmapData(CONST BitmapData *data, INT x, INT y, INT width, INT height, BitmapData *sub)
{
if (x < 0)
{
width += x;
x = 0;
}
if (x + width > (INT)data->Width)
width = (INT)data->Width - x;
if (width <= 0) return FALSE;
if (y < 0)
{
height += y;
y = 0;
}
if (y + height > (INT)data->Height)
height = (INT)data->Height - y;
if (height <= 0) return FALSE;
sub->Width = width;
sub->Height = height;
sub->Stride = data->Stride;
sub->PixelFormat = data->PixelFormat;
sub->Scan0 = (CHAR*)data->Scan0 + y * data->Stride + (x << 2);
sub->Reserved = data->Reserved;
return TRUE;
}
//---------------------------------------------------------------------------

VOID ImageMixer(BitmapData *dest, INT x, INT y, CONST BitmapData *source, INT alpha)
{
BitmapData dst, src;
if (GetSubBitmapData(dest, x, y, source->Width, source->Height, &dst))
{
GetSubBitmapData(source, x < 0 -x : 0, y < 0 -y : 0, dst.Width, dst.Height, &src);
ImageMixer(&dst, &src, alpha);
}
}
//---------------------------------------------------------------------------
// 获取子图数据
BOOL GetSubBitmapData(CONST BitmapData *data, INT x, INT y, INT width, INT height, BitmapData *sub)
{
if (x < 0)
{
width += x;
x = 0;
}
if (x + width > (INT)data->Width)
width = (INT)data->Width - x;
if (width <= 0) return FALSE;
if (y < 0)
{
height += y;
y = 0;
}
if (y + height > (INT)data->Height)
height = (INT)data->Height - y;
if (height <= 0) return FALSE;
sub->Width = width;
sub->Height = height;
sub->Stride = data->Stride;
sub->PixelFormat = data->PixelFormat;
sub->Scan0 = (CHAR*)data->Scan0 + y * data->Stride + (x << 2);
sub->Reserved = data->Reserved;
return TRUE;
}
//---------------------------------------------------------------------------

VOID ImageMixer(BitmapData *dest, INT x, INT y, CONST BitmapData *source, INT alpha)
{
BitmapData dst, src;
if (GetSubBitmapData(dest, x, y, source->Width, source->Height, &dst))
{
GetSubBitmapData(source, x < 0 -x : 0, y < 0 -y : 0, dst.Width, dst.Height, &src);
ImageMixer(&dst, &src, alpha);
}
}
//---------------------------------------------------------------------------

上面的代码中增加了一个获取子图像数据结构的函数GetSubBitmapData和一个有图像混合坐标的重载函数ImageMixer,在重载函数ImageMixer中,调用GetSubBitmapData获取了一个按图像混合起始坐标(x,y)计算的目标图与源图的交集子图像数据结构,然后将原图像混合到目标子图像上。通过多次这样的混合操作就可以实现图像的拼接。

下面是一个使用BCB2007和GDI+实现图像拼接的例子程序:


void __fastcall TForm1::Button3Click(TObject *Sender)
{
BitmapData data, dst, src;

// 建立新图像
Gdiplus::Bitmap *newBmp = new Gdiplus::Bitmap(768, 256, PixelFormat32bppARGB);
LockBitmap(newBmp, &data);

// 合成目标到新图左边
Gdiplus::Bitmap *dest = new Gdiplus::Bitmap(L"d:\\xmas_011.png");
LockBitmap(dest, &dst);
ImageMixer(&data, 0, 0, &dst, 255);

// 合成源图到新图中间
Gdiplus::Bitmap *source = new Gdiplus::Bitmap(L"d:\\Apple.png");
LockBitmap(source, &src);
ImageMixer(&data, dest->GetWidth(), 0, &src, 255);

// 目标图与源图混合
ImageMixer(&dst, &src, 192);

// 混合后的目标图合成到新图右边
ImageMixer(&data, dest->GetWidth() << 1, 0, &dst, 255);
UnlockBitmap(source, &src);
UnlockBitmap(dest, &dst);
UnlockBitmap(newBmp, &data);

// 显示拼接后的图像
Gdiplus::Graphics *g = new Gdiplus::G

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Micro2440/Mini2440 linux下蜂鸣(.. 下一篇终端对非打印字符的显示方式的有..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: