Why does my ofImage.draw() not fill the screen?

Hi everyone, I’m very much new to OF and struggling with trying to draw to my screen pixel by pixel.
I’m running my app in OF_FULLSCREEN and have:

    ofImage theScreen;
    int pixelCount;

and

void ofApp::setup() {
    ofBackground(0);
    pixelCount = ofGetWidth() * ofGetHeight();
    theScreen.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
}

// ...

void ofApp::draw() {
       
    for (int k = 0; k < pixelCount; k++) {
        theScreen.setColor(k, ofColor(ofRandom(255), ofRandom(255), ofRandom(255)));
    }
    theScreen.update();
    theScreen.draw(0,0, ofGetWidth(), ofGetHeight());
}

Yet the resulting image (static noise) only covers the top quarter of my screen:

Any idea what I’m doing wrong here?

Hi, I’m thinking that when you .setColor() for an index of an ofImage, you’re using an index to the ofPixels of the image. An RGB image type (OF_IMAGE_COLOR) will have 3 pixels to each color, while and RGBA image type (OF_IMAGE_COLOR_ALPHA) will have 4 pixels for each color. So to fill the screen, you’d multiply the pixelCount by either 3 or 4, depending on what kind of color image you have.

    pixelCount = ofGetWidth() * ofGetHeight() * 3; // an RGB image (OF_IMAGE_COLOR)

There are a few ways to set the image type; here is one:

theScreen.setImageType(OF_IMAGE_COLOR);

Now, if you use a nested loop, and loop over the y and x of the image, then .setColor() just sets the color at each (x, y) coordinate in the image:

    for(size_t y{0}; y < ofGetHeight(); ++y)
    {
        for(size_t x{0}; x < ofGetWidth(); ++x)
        {
            theScreen.setColor(x, y, ofColor(ofRandom(255), ofRandom(255), ofRandom(255)));
        }
    }
    theScreen.update();