Sign in to follow this  

SDL Surface data saving using Devil

This topic is 4265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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]
ilGenImages(1,&mytex);

Make it the current image.
ilBindImage(&mytex);

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);])
ilTexImage(
sdlimage->w,
sdlimage->h,
1, //Depth 1 = 2d,2 = 3d
3, //Bytes per pixel
IL_RGB, //format
IL_UNSIGNED_BYTE, //type
sdlimage->pixels
);

Unlock the surface.[SDL_UnlockSurface(sdlimage)]
Save it to whatever format you want.
ilSave(IL_TGA,"myimage.tga");

Delete the texture.[Restore state]
ilDeleteImages(1,&mytex);

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);
ilBindImage(img);

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

SDL_LockSurface(Bitmap);

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

SDL_UnlockSurface(Bitmap);

ilEnable(IL_FILE_OVERWRITE);
ilSave(type, filename);

ilDeleteImages(1, &img);
return IL_TRUE;

output:


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
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

This topic is 4265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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