Blank output screen on inserting a while loop in draw

Hey,

I am using OF007 on XCode/10.6.8 and I seem to be facing a weird bug. I am trying to build a basic pixellation example by using the color information from an image and drawing circles onto the screen with the color information. The code seems to work fine when I add the pixels sequentially onto the output window but when I try to draw them all together using a for/while, I just get a blank screen. Maybe its something really silly but its really got me stumped :frowning:

testApp.h

  
  
#pragma once  
  
#include "ofMain.h"  
#define bytesPerPixel 3  
class testApp : public ofBaseApp{  
	private:  
		ofImage myPic;  
		int location;  
		int fullSize;  
		int locX, locY;  
		unsigned char *pixels;  
		float radiusNoise;  
	public:  
		void setup();  
		void update();  
		void draw();  
		  
};  
  

testApp.cpp

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	location =0;  
	locX = 0;  
	locY = 0;  
	myPic.loadImage("myimg.jpg");  
	fullSize = myPic.width*myPic.height;  
	ofSetWindowShape(myPic.width, myPic.height);  
	pixels = myPic.getPixels();  
	//ofSetVerticalSync(true);  
	ofEnableAlphaBlending();  
	ofBackground(255, 255, 255);  
	ofSetBackgroundAuto(false);  
	ofSetFrameRate(500);  
	ofEnableSmoothing();  
	radiusNoise = 10;  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	ofSetupScreen(); //does important stuff, no idea what  
	while (location<=fullSize) {  
  
	location = (locY*myPic.width)+locX;  
  
	int r = pixels[bytesPerPixel*location];  
	int g = pixels[bytesPerPixel*location+1];  
	int b = pixels[bytesPerPixel*location+2];  
	ofSetLineWidth(2);  
	ofNoFill();  
	ofSetColor(0);  
	ofFill();  
	ofSetColor(r, g, b, 150);  
	int col = location%myPic.width;  
	int row = location/myPic.width;  
	  
	ofCircle(col, row, ofNoise(radiusNoise)*5);  
	//ofSetHexColor(0xffffff);  
	locX += 5;  
	radiusNoise += 0.1;  
	if (locX >= ofGetWidth()) {  
		locX = 0;  
		locY += 5;  
	}  
	}  
}  
  

Changing the while to an if makes the code work fine, except the first 3 circles mysteriously seem to be absent!

Any ideas would be great :slight_smile:

Thanks
/sumit
The code is a modified version of Josh Noble’s code from Programming Interactivity.

in draw you have ofSetupScreen() but dont show the code of that function to see if its correct to use in draw.

Maybe you can try removing this call ofSetupScreen() and doing something like

cout << “location::” << location << " full"
while (location<=fullSize) {

location = (locY*myPic.width)+locX;

int r = pixels[bytesPerPixel*location];
int g = pixels[bytesPerPixel*location+1];
int b = pixels[bytesPerPixel*location+2];
ofSetLineWidth(2);
ofNoFill();
ofSetColor(0);
ofFill();
ofSetColor(r, g, b, 150);
int col = location%myPic.width;
int row = location/myPic.width;

ofCircle(col, row, ofNoise(radiusNoise)*5);
//ofSetHexColor(0xffffff);
locX += 5;
radiusNoise += 0.1;
if (locX >= ofGetWidth()) {
locX = 0;
locY += 5;
}
}

cout << “location::” << location << " fullSize::" << fullSize << endl;
while (location<=fullSize) {

location = (locY*myPic.width)+locX;

int r = pixels[bytesPerPixel*location];
int g = pixels[bytesPerPixel*location+1];
int b = pixels[bytesPerPixel*location+2];
ofSetLineWidth(2);
ofNoFill();
ofSetColor(0);
ofFill();
ofSetColor(r, g, b, 150);
int col = location%myPic.width;
int row = location/myPic.width;

cout << “drawing a circle at::” << col << row << " size::" << ofToString(ofNoise(radiusNoise)*5) << endl;

ofCircle(col, row, ofNoise(radiusNoise)*5);
//ofSetHexColor(0xffffff);
locX += 5;
radiusNoise += 0.1;
if (locX >= ofGetWidth()) {
locX = 0;
locY += 5;
}
}

To give some output to the console to see whats happening… hope that helps

Hey!

Thanks a lot for the reply. Actually ofSetupScreen() seems to be a OF utility function which was in Josh’s code. However I tried commenting out the call to the function and also adding the console outs you recommended. According to the console, circles are getting drawn as they should be. Still the output remains completely blank. Its just super weird :frowning:

/sumit

no problem, im glad to help… if i can

Try setting the rgb color manually

int r = pixels[bytesPerPixel*location];
int g = pixels[bytesPerPixel*location+1];
int b = pixels[bytesPerPixel*location+2];

int r = 200;
int g = 100;
int b = 50;

What are you trying to do??
rendering a ofImage using circles??? if so i think that it can be done in another way:

in setup

  • load the image

in draw

  • looop pixels of the image
  • get the color getColorAt(x,y)
    so once you have the color extract the components

hope that understandme, english is not my native language

Yes, I am essentially trying to render an ofImage using circles. Thanks for the getColor approach, it simplifies the code to a great extent! But unfortunately, the blank screen problem still remains. Now additionally, I get a out of memory error (gdb.exc_bad_excess) when I sample every 5 or 10 pixels. XCode points me to this line in ofColor.cpp

  
		c.set( pixels[index], pixels[index+1], pixels[index+2], pixels[index+3] );  
  

modified code: testApp.h

  
  
#pragma once  
  
#include "ofMain.h"  
#define bytesPerPixel 3  
class testApp : public ofBaseApp{  
	private:  
		ofImage myPic;  
		ofPixels pixels;  
		long int location;  
		long int fullSize;  
		int locX, locY;  
		//unsigned char *pixels;  
		//float radiusNoise;  
	public:  
		void setup();  
		void update();  
		void draw();  
		  
};  
  

  
  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	location =0;  
	locX = 0;  
	locY = 0;  
	ofLoadImage(pixels, "myimage.jpg");  
	fullSize = pixels.size();  
	ofSetWindowShape(pixels.getWidth(), pixels.getHeight());  
	ofEnableAlphaBlending();  
	ofBackground(255, 255, 255);  
	ofSetBackgroundAuto(false);  
	//ofSetFrameRate(24);  
	ofEnableSmoothing();  
	//radiusNoise = 10;  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	//ofSetupScreen(); //does important stuff, no idea what  
	location = 0;  
	while (location<fullSize/5) {  
		cout << "location::" << location << " fullSize::" << fullSize << endl;  
		ofColor imageColor = pixels.getColor(locX, locY);  
		ofFill();  
		ofSetColor(imageColor);  
		//cout << "drawing a circle at::" << locX <<  locY << " size::" << ofToString(ofNoise(radiusNoise)*5) << endl;	  
		ofCircle(locX, locY, 5);  
		locX += 10;  
		radiusNoise += 0.1;  
		if (locX >= ofGetWidth()) {  
			locX = 0;  
			locY += 10;  
		}  
		location++;  
	}  
}  
  

Any ideas on why I seem to be out of memory now? Again, if I change the while to an if, the code runs without any problems, only it to gets rendered one pixel at a time.

i think that the way that your are looping the pixels of the image is a bit extrange, maybe can try

declare ofImage in header

  
  
ofImage myImage;  
  
void testApp::setup(){   
	//load image in setup:  
	myImage.loadImage("image.jpg");  
}  
  
void testApp::draw(){   
	ofSetColor(255,255,255);  
	for(int x=0;x<myImage.getWidth();x+=6){  
		for(int y=0; y<myImage.getHeight();y+=6){  
			ofColor c = myImage.getColor(x,y);  
			ofSetColor(c); // or ofSetColor(c.r,c.g,c.b);  
			ofCircle(x,y,6); // or ofCircle(x-3,y-3,6)  
			}  
	}  
}  
  

Awesome! Now that I come to think of it, it was pretty weird how I was looping through the pixels :slight_smile: The code finally runs! Yay!

Thanks a lot for all your help :slight_smile:

PS. any ideas on what ofSetupScreen does? Thats still a mystery function :stuck_out_tongue:

/sumit

ofSetupScreen() just sets up the screen perspective for whatever renderer you’re using, i.e. the angle of the field of view, front and back clipping plane, i.e. how far the front and back of the area that can be seen are.