我可以找到很多关于如何在托管 c++ 中执行此操作的示例,但没有找到非托管的示例。
我希望尽可能高效地获取所有像素数据,但我需要更多有关 scan0 内容的一些信息,以便我可以正确地迭代像素数据并从中获取每个 rgba 值。
现在我有这个:
Bitmap *b = new Bitmap(filename);
if(b == NULL)
{
return 0;
}
UINT w,h;
w = b->GetWidth();
h = b->GetHeight();
Rect *r = new Rect(0,0,w,h);
BitmapData *lockdat;
b->LockBits(r,ImageLockModeRead,PixelFormatDontCare,lockdat);
delete(r);
if(w == 0 && h == 0)
{
return 0;
}
Color c;
std::vector<GLubyte> pdata(w * h * 4,0.0);
for (unsigned int i = 0; i < h; i++) {
for (unsigned int j = 0; j < w; j++) {
b->GetPixel(j,i,&c);
pdata[i * 4 * w + j * 4 + 0] = (GLubyte) c.GetR();
pdata[i * 4 * w + j * 4 + 1] = (GLubyte) c.GetG();
pdata[i * 4 * w + j * 4 + 2] = (GLubyte) c.GetB();
pdata[i * 4 * w + j * 4 + 3] = (GLubyte) c.GetA();
}
}
delete(b);
return CreateTexture(pdata,w,h);
如何使用 lockdat 执行与 getpixel 相同的操作?
谢谢
lockdat->Scan0 是指向位图像素数据的指针。请注意,您really请务必关心您要求的像素格式,PixelFormatDontCare 不会这样做。因为指针的使用方式受到像素格式的影响。 PixelFormat32bppARGB是最简单的,一个像素是一个int的大小,4个字节代表alpha、红、绿和蓝。步幅将等于位图的宽度。一个简单的 memcpy() 就可以完成工作。请注意位图存储颠倒。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)