SDL Surface data saving using Devil

Started by
6 comments, last by Jack Sotac 18 years ago
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?
Advertisement
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.
0xa0000000
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.
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]
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:)
0xa0000000
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...
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.
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.
0xa0000000

This topic is closed to new replies.

Advertisement