LoadIMG

Hello John, below is my function LoadIMG. Well, it is not 100% mine
(maybe just 5%).
Since I’m completely new to this news group I’m not willing to post
large binary files :wink: I’ll try to make a small version of the program to
see what happens then and if the problem persist I’ll post it here as
you tell me.
I use SDL_image to load .jpg images.

SDL_Surface *LoadIMG( char *FileName, ColorRGB transparentColor )
{
SDL_Surface *temp = NULL;
SDL_Surface *ready = NULL;

// Load image with SDL_image call
temp = IMG_Load( FileName );
if( temp == NULL )
{
printf( “Image file load failed: %s\n”,
IMG_GetError()
);
exit( 2 );
}
// Set transparent color for this surface (image)
if ( SDL_SetColorKey( temp, SDL_SRCCOLORKEY,
SDL_MapRGB( temp->format,
transparentColor.r,
transparentColor.g,
transparentColor.b
)
) == -1 )
printf( “Failed to set transparent color: %s\n”,
SDL_GetError()
);

// Set display format for fast blitting
ready = SDL_DisplayFormat( temp );
if( ready == NULL )
{
printf( “Image display format conversion failed: %s\n”,
SDL_GetError()
);
exit( 3 );
}

return ready;
}

I use two functions like the one bellow to load a new set of images. The
only difference is that the macros IMAGE00 are different. Such macro
is defined like
#define IMAGE0101 “./data/images/image01.jpg”. For later versions I
plant to generate the strings or to look for them in a directory or to
read their names/paths from a file. What I am basically trying to do is
an image viewer with special effects and my real goal is to learn how to
use SDL to make a more productive program, that needs to load
dynamically lots of images.

void LoadImageSet01()
{
// the following loop doesn’t help either
for ( int index = 0; index < 15; index ++ )
{
if ( images[ index ] != NULL )
SDL_FreeSurface( images[ index ] );
}
// Set transparent key color to blue (255)
ColorRGB transparent;
transparent.r = 0;
transparent.g = 0;
transparent.b = 255;

// Load images
images[ 0 ] = LoadIMG( IMAGE0101, transparent );
images[ 1 ] = LoadIMG( IMAGE0102, transparent );
images[ 2 ] = LoadIMG( IMAGE0103, transparent );
images[ 3 ] = LoadIMG( IMAGE0104, transparent );
images[ 4 ] = LoadIMG( IMAGE0105, transparent );
images[ 5 ] = LoadIMG( IMAGE0106, transparent );
images[ 6 ] = LoadIMG( IMAGE0107, transparent );
images[ 7 ] = LoadIMG( IMAGE0108, transparent );
images[ 8 ] = LoadIMG( IMAGE0109, transparent );
images[ 9 ] = LoadIMG( IMAGE0110, transparent );
images[ 10 ] = LoadIMG( IMAGE0111, transparent );
images[ 11 ] = LoadIMG( IMAGE0112, transparent );
images[ 12 ] = LoadIMG( IMAGE0113, transparent );
images[ 13 ] = LoadIMG( IMAGE0114, transparent );
images[ 14 ] = LoadIMG( IMAGE0115, transparent );
}

You should free the temp surface before you return, because SDL_DisplayFormat()
creates a new surface. You may also consider using the temp surface if converting
fails (yes, it will be slower, but…), something like this:

ready = SDL_DisplayFormat(temp);
if(ready)
	SDL_FreeSurface(temp);
else
	ready = tmp;

return ready;

BTW, does anyone know in which cases will SDL_DisplayFormat actually fail?On Sat, May 10, 2003 at 11:01:13AM +0300, Hri wrote:

// Set display format for fast blitting
ready = SDL_DisplayFormat( temp );
if( ready == NULL )
{
printf( “Image display format conversion failed: %s\n”,
SDL_GetError()
);
exit( 3 );
}

return ready;
}


Ivan Stankovic, @Ivan_Stankovic

Thank you everybody who helped me realize that
SDL_FreeSurface( temp ) would solve the memory problem.

The problem with SDL_HWPALETTE still remains. Or it simply doesn’t make
any sense to use a palette with 16bit and 32bit colors?

(sorry that I have started a new thread and that I haven’t posted the
last messages to the previous one: Problem with reusing array of
surfaces)> SDL_Surface *LoadIMG( char *FileName, ColorRGB transparentColor )

{
SDL_Surface *temp = NULL;
SDL_Surface *ready = NULL;

// Load image with SDL_image call
temp = IMG_Load( FileName );
if( temp == NULL )
{
printf( “Image file load failed: %s\n”,
IMG_GetError()
);
exit( 2 );
}
// Set transparent color for this surface (image)
if ( SDL_SetColorKey( temp, SDL_SRCCOLORKEY,
SDL_MapRGB( temp->format,
transparentColor.r,
transparentColor.g,
transparentColor.b
)
) == -1 )
printf( “Failed to set transparent color: %s\n”,
SDL_GetError()
);

// Set display format for fast blitting
ready = SDL_DisplayFormat( temp );
if( ready == NULL )
{
printf( “Image display format conversion failed: %s\n”,
SDL_GetError()
);
exit( 3 );
}

return ready;
}

yeah, palettes are only for 8 bits.

16 and 32 bit modes you actualy specify how much red,green and blue you want
in the color so you dont use a palette.> ----- Original Message -----

From: hr_hristov@hotmail.com (Hri)
To:
Sent: Saturday, May 10, 2003 8:47 AM
Subject: [SDL] Re: LoadIMG

Thank you everybody who helped me realize that
SDL_FreeSurface( temp ) would solve the memory problem.

The problem with SDL_HWPALETTE still remains. Or it simply doesn’t make
any sense to use a palette with 16bit and 32bit colors?

(sorry that I have started a new thread and that I haven’t posted the
last messages to the previous one: Problem with reusing array of
surfaces)

SDL_Surface *LoadIMG( char *FileName, ColorRGB transparentColor )
{
SDL_Surface *temp = NULL;
SDL_Surface *ready = NULL;

// Load image with SDL_image call
temp = IMG_Load( FileName );
if( temp == NULL )
{
printf( “Image file load failed: %s\n”,
IMG_GetError()
);
exit( 2 );
}
// Set transparent color for this surface (image)
if ( SDL_SetColorKey( temp, SDL_SRCCOLORKEY,
SDL_MapRGB( temp->format,
transparentColor.r,
transparentColor.g,
transparentColor.b
)
) == -1 )
printf( “Failed to set transparent color: %s\n”,
SDL_GetError()
);

// Set display format for fast blitting
ready = SDL_DisplayFormat( temp );
if( ready == NULL )
{
printf( “Image display format conversion failed: %s\n”,
SDL_GetError()
);
exit( 3 );
}

return ready;
}


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