Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

The most efficient way to modify pixel data in ofImage?

$
0
0

@jaeho wrote:

I can't figure out how to use ofImage::getPixels().

According to the reference, ofImage::getPixels() returns a raw pointer to the pixel data. So this code is supposed to draw a black image, but it draws the original image.

ofImage myImage;
myImage.load("apple.jpg");
ofPixels pix = myImage.getPixels();
for (int pi = 0; pi < pix.size(); pi++) {
	pix[pi] = 0;
}
myImage.update();
myImage.draw(0, 0);

When I check this in a debugger, I see the code is copying the data into a new instance of ofPixels instead of referring to the original data block.

I have to use setFromPixels() instead of update() to make this code work.

myImage.setFromPixels(pix);
myImage.draw(0, 0);

But this is a bit inefficient because it makes a copy of the whole data.

What is the correct way to
- get pointer to the data in ofImage
- modify some pixel data
- and update?

Posts: 4

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles