How to access rgb data of an image


#1

I’d like to load up a png file and access the data of its pixels through an index. Is there a way for me to do that?


#2

I haven’t tried to do it myself yet, but from the docs it looks like you’d make a PixelData object of the correct size, copy the texture to it, then read from that. Something like:

// given a texture named tex
auto pixData = PixelData::create(WIDTH, HEIGHT, DEPTH, FORMAT);
tex->readData(pixData).blockUntilComplete();
// Reading a pixel at (x,y)
auto pixel = pixData->getColorAt(x + y * width);
// Or setColorAt

From there, you’d just do a tex->writeData(pixData); again to set it (I think, again, haven’t tried any of this myself yet).

Docs of note:
http://docs.bsframework.io/latest/classbs_1_1_texture.html
http://docs.bsframework.io/latest/classbs_1_1_pixel_data.html
http://docs.bsframework.io/latest/classbs_1_1_async_op.html


#3

Thank you for your help! However, the operation doesn’t seem to end, no matter how long I wait. Do you know what might be the cause of it? This is the code and the image. (The image is just above the code, very small 10x10)

test

HTexture tex = gImporter().import<Texture>("map\\test.png");
auto pixData = PixelData::create(10, 10, 8, PF_RGB8);

auto async = tex->readData(pixData);

while (!async.hasCompleted())
{
	;
}

#4

You need to ensure the operation is submitted to the core thread. Use gCoreThread.submitAll() after the readData call.

More information about the core thread here: http://docs.bsframework.io/nightly/Developer_Manuals/Low_Level_rendering/coreThread.html


#5

Thanks! It’s working now! :slight_smile: