问题描述
我可以找到很多有关如何在托管c ++中执行此操作的示例,但对于非托管c ++则找不到.
I can find many examples on how to do this in managed c++ but none for unmanaged.
我想尽可能高效地获取所有像素数据,但是我需要更多有关scan0的信息,以便我可以正确地遍历像素数据并从中获取每个rgba值.
I want to get all the pixel data as efficiently as possible, but some of the scan0 stuff I would need more info about so I can properly iterate through the pixel data and get each rgba value from it.
现在我有这个:
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的等效操作?谢谢
How do I use lockdat to do the equivalent of getpixel?Thanks
推荐答案
lockdat-> Scan0是指向位图像素数据的指针.请注意,您确实确实在乎您要求的像素格式,而PixelFormatDontCare不会.因为指针的使用方式受像素格式的影响. PixelFormat32bppARGB是最简单的,一个像素将是一个int的大小,4个字节代表alpha,红色,绿色和蓝色.步幅将等于位图的宽度.一个简单的memcpy()可能会完成工作.请注意,位图是颠倒存储的.
lockdat->Scan0 is a pointer to the pixel data of the bitmap. Note that you really do care what pixel format you ask for, PixelFormatDontCare won't do. Because how you use the pointer is affected by the pixel format. PixelFormat32bppARGB is the easiest, one pixel will be the size of an int, 4 bytes representing alpha, red, green and blue. And the stride will be equal to the width of the bitmap. Making it likely that a simple memcpy() will get the job done. Beware the bitmaps are stored upside-down.
这篇关于在本机C ++中锁定GDI +位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!