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

How to edit screen pixels

$
0
0

@noine wrote:

So in Processing you can edit pixels with loadPixels() and updatePixels() and I'm trying to achieve the same here. Below the image is the code I'm using.

I know that ofPixels is an unsigned char* + some functionalities. Since this is an array of pointers, the line:

img.getPixels() = pixels;

shouldn't be necessary, but else I won't get a visual.

It's result can be seen in the image below, but I don't understand why I get three images.

So can you give me a correct (and maybe the best) way to edit screen pixels?

ofImage img;
ofPixels pixels;

void ofApp::setup(){
	img.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_GRAYSCALE);
}

void ofApp::update(){
	float noiseScale = ofMap(mouseX, 0, ofGetWidth(), 0, 0.1);
	float noiseVel = ofGetElapsedTimef();

	img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
	pixels = img.getPixels();
	int w = img.getWidth();
	int h = img.getHeight();
	for (int y = 0; y < w; y++) {
		for (int x = 0; x < h; x++) {
			int i = y * w + x;
			float noiseValue = ofNoise(x * noiseScale, y * noiseScale, noiseVel);
			pixels[i] = 255 * noiseValue;
		}
	}
	img.getPixels() = pixels;
	img.update();
}


void ofApp::draw(){
	ofSetColor(ofColor::white);
	img.draw(0, 0, ofGetWidth(), ofGetHeight());
}

Posts: 10

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles