RGB mapping using OpenGL

Hello newsgroup,

could anybody tell me why the function this_doesnt_work() doesn’t work. There is no error message but there is also no output. I would expect a somewhat brighter output of the lower part of the image in comparison to the output of the this_works() function.

Thanks in advance,

Alex—

#include
#include <stdio.h>
#include <stdlib.h>

#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>

static SDL_Surface *image = 0;
static int width = 0;
static int height = 0;
static int bpp = 0;

static void quit(int code)
{
SDL_Quit();
exit(code);
}

static void setup()
{
glShadeModel(GL_SMOOTH);
glClearColor(0, 0, 0, 0);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glReadBuffer(GL_BACK);
glDrawBuffer(GL_BACK);
}

static void this_doesnt_work()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float scale_x = (width / 1.0) / image->w,
scale_y = (height / 2.0) / image->h;
glRasterPos2f(-1.0, 1.0);
glPixelZoom(scale_x, -scale_y);
glDrawPixels(image->w, image->h, GL_BGR, GL_UNSIGNED_BYTE, image->pixels);
int size = (int)round(pow(2, bpp));
float *table = new float[size];
float norm = 0.0;
float gamma = 1.0/1.8;
for ( int i = 0; i < size; ++i ) {
norm = i/size;
table[i] = pow(norm, gamma) * i;
}
glRasterPos2f(-1.0, -1.0);
glPixelZoom(1.0, 1.0);
glPixelTransferi(GL_MAP_COLOR, GL_TRUE);
glPixelMapfv(GL_PIXEL_MAP_R_TO_R, size, table);
glPixelMapfv(GL_PIXEL_MAP_G_TO_G, size, table);
glPixelMapfv(GL_PIXEL_MAP_B_TO_B, size, table);
glCopyPixels(0, height/2, width, height/2, GL_COLOR);
glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
SDL_GL_SwapBuffers();
}

static void this_works()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float scale_x = (width / 1.0) / image->w,
scale_y = (height / 2.0) / image->h;
glRasterPos2f(-1.0, 1.0);
glPixelZoom(scale_x, -scale_y);
glDrawPixels(image->w, image->h, GL_BGR, GL_UNSIGNED_BYTE, image->pixels);
glRasterPos2f(-1.0, -1.0);
glPixelZoom(1.0, 1.0);
glCopyPixels(0, height/2, width, height/2, GL_COLOR);
SDL_GL_SwapBuffers();
}

int main(int argc, char* argv[])
{
const SDL_VideoInfo* info = 0;
int flags = 0;
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
std::cerr << SDL_GetError() << “\n”;
quit(1);
}
info = SDL_GetVideoInfo();
if( !info ) {
std::cerr << SDL_GetError() << “\n”;
quit(1);
}
width = 800;
height = 600;
bpp = info->vfmt->BitsPerPixel;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
flags = SDL_OPENGL;
if ( SDL_SetVideoMode(width, height, bpp, flags) == 0 ) {
std::cerr << SDL_GetError() << “\n”;
quit(1);
}
setup();
image = SDL_LoadBMP("/usr/src/SDL_gfx-2.0.11/Test/sample24.bmp");
if ( image ) {
//this_works();
this_doesnt_work();
char c;
std::cin >> c;
}
return 0;
}