How to histogram an image in frame buffer ?

I need to get a histogram of an image that’s already drawn in the frame buffer.

It seems that I have to read the image back to the memory using glReadPixel() for glHistogram() to work correctly, which is very time-consuming and kind of a waste. Without glReadPixle( ), everything I get from the histogram is zero.

Anybody knows if I can avoid reading back to memory and still get the right histogram data for the image?

The following is the code I used:

glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

// set up for histogramming
glResetHistogram(GL_HISTOGRAM);
glHistogram (GL_HISTOGRAM, HISTOGRAM_SIZE, GL_RGB, GL_FALSE);
glEnable(GL_HISTOGRAM);

DrawModels();

glReadPixels(0, 0, 100, 100, GL_RGB, GL_FLOAT, aa);

glGetHistogram(GL_HISTOGRAM, GL_FALSE, GL_RGB, GL_UNSIGNED_SHORT, histogram);

Your help will be really appreciated.

Thanks.

Note that the last parameter of glHistogram defines whether pixels are consumed by the histogramming or not. If set to yes, the drivers doesn’t have to transfer the data and might work faster.

Another option would be to glCopyPixels to the same framebuffer location. Thus pixel data doesn’t have to leave the card and might run faster.

But in general I would be mildly surprised if current game-level hardware accellerated histogramming, as AFAIK it’s totally irrelevant for games right now.

Just my .02$

Histograms might be useful in conjunction with floating point render targets to decide on how to do tone mapping (exposure) for displaying HDR renders.