Sign in to follow this  

SDL Surface data saving using Devil

Recommended Posts

Hello to all; I'm trying to save surface data [pixels] of an SDL_Surface to a file using Devil. Well i can't save the surface as it always shows me the famous error about memory reading [i'm using vc++] do you have any idea on how to do this?

Share this post

Link to post
Share on other sites
How are you doing the copying?

I imagine you would do it something like this:

Create a new texture.[Save state]

Make it the current image.

Resize the texture and copy SDL_Surface data to it.
(Note: Actual parameters depend on the format of the SDL_Surface data)
(Note: Make sure you lock the SDL_Surface.[SDL_LockSurface(sdlimage);])
1, //Depth 1 = 2d,2 = 3d
3, //Bytes per pixel
IL_RGB, //format

Unlock the surface.[SDL_UnlockSurface(sdlimage)]
Save it to whatever format you want.

Delete the texture.[Restore state]

Good Luck.

Share this post

Link to post
Share on other sites
Also make sure you give it the pitch instead of the width of the surface where appropriate. The width of an image is usually only a logical width and not really what the pixel data in memory looks like.

Share this post

Link to post
Share on other sites
thanks so much for your answers but i think i got another problem i first tried the pitch as basement said but it gave the wrong output... So i thought i can try width but it gave another 'wrong output'. So here is my code and what my output [with pitch] looks like

ILuint img;
ilGenImages(1, &img);

if (Bitmap == NULL || Bitmap->w == 0 || Bitmap->h == 0) {
return IL_FALSE;


if (!ilTexImage(Bitmap->pitch, Bitmap->h, 1, Bitmap->format->BytesPerPixel, IL_RGB, IL_UNSIGNED_BYTE, Bitmap->pixels))
return IL_FALSE;


ilSave(type, filename);

ilDeleteImages(1, &img);
return IL_TRUE;


and here is the input:

ok as you can see i don't thik pitch is what i need; i think w is what i need because my output image looks good [in sizes i mean] with Bitmap->w; but there's still something missing... look at the next output:

[Edited by - Dec1pher on April 5, 2006 10:41:12 AM]

Share this post

Link to post
Share on other sites
Make sure the SDL surface is either 3 or 4 bytes per pixel. If it is not, convert it to the correct format using SDL_ConvertSurface(). Imagelib wouldn't handle any surface format besides these two.
I noticed in the ilut.h header a stub for ilutSDLSurfaceFromBitmap(). It would be good if you submitted this code to the imagelib people:)

Share this post

Link to post
Share on other sites
Thanks everyone for your help...
I think i'm reaching somewhere.

As you can see my image was 250 * 250 so i tried with another image its sizes are 305 * 128 and guess what? Code worked perfectly. So now what do you think about this? What can be the problem...

Share this post

Link to post
Share on other sites
My suggestion: use SDL_image to load your pictures. Linky

It loads alot of files and there will be no conversion hassle from DevIL's format to SDL's format.

Share this post

Link to post
Share on other sites
It looks like SDL surfaces use padding(on 32bit boundaries) for each image row.

Imagelib doesn't appear to be able to detect the padding. A pitch parameter for ilTexImage() would solve this problem.

A work around would be to convert all images to 32bit and then you won't have to deal with the padding issue.

Note that SDL_SaveBMP() is available if this format is an option for you.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this