To display raw image data!

Hi,
I am new to SDL, I am using SDL_CreateRGBSurfaceFrom to display raw
image ( each pixel is in the format unsigned char ) captured by the
Camera connected to my PC. When I run this, I only get a black screen.
If I load a bmp image using SDL_LoadBMP, I get the image displayed
correctly. Any help in this regard, would be highly appreciated.
And suppose if I would like to extend this from static image to video,
how do I go about ?
Thanks,
Raj

This is the code that I am using !

    /* Initialize SDL */
    if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
            ComplainAndExit();
    }
    atexit(SDL_Quit);

  imagebmp =

SDL_CreateRGBSurfaceFrom(camera1.m_pData,640,480,8,640,0,0,0,0);

    if ( imagebmp == NULL ) {
            ComplainAndExit();
    }

    /* Set the video mode (640x480 at native depth) */
    screen = SDL_SetVideoMode(640, 480, 0,

SDL_HWSURFACE|SDL_FULLSCREEN);
if ( screen == NULL ) {
ComplainAndExit();
}

    /* Set the video colormap */
    if ( imagebmp->format->palette != NULL ) {
            SDL_SetColors(screen,
                            imagebmp->format->palette->colors, 0,
                            imagebmp->format->palette->ncolors);
    }

    /* Convert the image to the video format (maps colors) */
    image = SDL_DisplayFormat(imagebmp);
    SDL_FreeSurface(imagebmp);
    if ( image == NULL ) {
            ComplainAndExit();
    }

    /* Draw bands of color on the raw surface */
    if ( SDL_MUSTLOCK(screen) ) {
            if ( SDL_LockSurface(screen) < 0 )
                    ComplainAndExit();
    }

/*
buffer=(Uint8 )screen->pixels;
for ( i=0; ih; ++i ) {
memset(buffer,(i
255)/screen->h,
screen->w*screen->format->BytesPerPixel);
buffer += screen->pitch;
}
*/
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}

    /* Blit the image to the center of the screen */
    dstrect.x = (screen->w-image->w)/2;
    dstrect.y = (screen->h-image->h)/2;
    dstrect.w = image->w;
    dstrect.h = image->h;
    if ( SDL_BlitSurface(image, NULL, screen, &dstrect) < 0 ) {
            SDL_FreeSurface(image);
            ComplainAndExit();
    }
    SDL_FreeSurface(image);

    /* Update the screen */
    SDL_UpdateRects(screen, 1, &dstrect);

    SDL_Delay(5000);        /* Wait 5 seconds */
    exit(0);

}

Hi,
I am new to SDL, I am using SDL_CreateRGBSurfaceFrom to display raw
image ( each pixel is in the format unsigned char ) captured by the
Camera connected to my PC. When I run this, I only get a black screen.
If I load a bmp image using SDL_LoadBMP, I get the image displayed
correctly. Any help in this regard, would be highly appreciated.
And suppose if I would like to extend this from static image to video,
how do I go about ?

SDL_LoadBMP sets the palette of the returned SDL_Surface when you load an 8bit
paletted bmp (which contains its own palette). SDL_CreateRGBSurfaceFrom does
not. How could it? The raw image data contains no palette info.

  imagebmp =

SDL_CreateRGBSurfaceFrom(camera1.m_pData,640,480,8,640,0,0,0,0);

You need to set the palette of the returned surface here using SDL_SetPalette.
If your camera is giving you “unsigned char” (8bit paletted) data, it must be
giving you a palette too, right? You are using the “default” empty palette,
that is why everything is black.

    /* Set the video colormap */
    if ( imagebmp->format->palette != NULL ) {
            SDL_SetColors(screen,
                            imagebmp->format->palette->colors, 0,
                            imagebmp->format->palette->ncolors);
    }

You are setting the screen palette to the blank palette of the created surface
imagebmp, which (above) has not been set yet.

    /* Convert the image to the video format (maps colors) */
    image = SDL_DisplayFormat(imagebmp);
    SDL_FreeSurface(imagebmp);
    if ( image == NULL ) {
            ComplainAndExit();
    }

This won’t work if you don’t set the palette of imagebmp. Same as above.On Monday 21 October 2002 12:48, V.Rajashekar wrote:


Max Watson <@Max_Watson>

Thanks a lot for the tips suggested and now it worked out fine. I am now
in need of another quick question. Suppose, if I would like to extend
from Static Image to real time image ( i.e Video ) how do I go about ?
Do I need to do looping ? Or is there any other efficient method. I
actually need to get a very good Frame Rate.
And reply in this regard would be very helpful.
Thanks,
Raj> ----- Original Message -----

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org] On Behalf Of
Max Watson
Sent: October 21, 2002 1:06 PM
To: sdl at libsdl.org
Subject: Re: [SDL] To display raw image data !

On Monday 21 October 2002 12:48, V.Rajashekar wrote:

Hi,
I am new to SDL, I am using SDL_CreateRGBSurfaceFrom to display raw
image ( each pixel is in the format unsigned char ) captured by the
Camera connected to my PC. When I run this, I only get a black screen.

If I load a bmp image using SDL_LoadBMP, I get the image displayed
correctly. Any help in this regard, would be highly appreciated. And
suppose if I would like to extend this from static image to video, how

do I go about ?

SDL_LoadBMP sets the palette of the returned SDL_Surface when you load
an 8bit
paletted bmp (which contains its own palette). SDL_CreateRGBSurfaceFrom
does
not. How could it? The raw image data contains no palette info.

  imagebmp = 

SDL_CreateRGBSurfaceFrom(camera1.m_pData,640,480,8,640,0,0,0,0);

You need to set the palette of the returned surface here using
SDL_SetPalette.
If your camera is giving you “unsigned char” (8bit paletted) data, it
must be
giving you a palette too, right? You are using the “default” empty
palette,
that is why everything is black.

    /* Set the video colormap */
    if ( imagebmp->format->palette != NULL ) {
            SDL_SetColors(screen,
                            imagebmp->format->palette->colors, 0,
                            imagebmp->format->palette->ncolors);
    }

You are setting the screen palette to the blank palette of the created
surface
imagebmp, which (above) has not been set yet.

    /* Convert the image to the video format (maps colors) */
    image = SDL_DisplayFormat(imagebmp);
    SDL_FreeSurface(imagebmp);
    if ( image == NULL ) {
            ComplainAndExit();
    }

This won’t work if you don’t set the palette of imagebmp. Same as above.


Max Watson


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl