首先我们来代码吧:
int joseph_creat_bmp_data_show(TTF_Font *font,char *strings,BITMAP_S *osd_bmp_in)
{
#if 0
SDL_Surface *text;
text = (SDL_Surface *)malloc(sizeof(SDL_Surface));
memset(text,0,sizeof(SDL_Surface));
#endif
SDL_Color forecol= { 0x00, 0x00, 0x00, 0x0};
SDL_Surface *text = TTF_RenderUTF8_Solid(font, strings, forecol);
if(text == NULL)
{
printf("%s:[%d] \n",__FUNCTION__,__LINE__);
exit(0);
}
SDL_PixelFormat *fmt;
fmt = (SDL_PixelFormat*)malloc(sizeof(SDL_PixelFormat));
if(fmt == NULL)
{
printf("%s:[%d] \n",__FUNCTION__,__LINE__);
exit(0);
}
memset(fmt,0,sizeof(SDL_PixelFormat));
/*表示位图的像素格式为:每个像素为16bit,也就是每个像素占用内存2个Byte*/
fmt->BitsPerPixel = 16;
fmt->BytesPerPixel = 2;
fmt->Rmask = 0xff000000;//0x00FF0000
fmt->Gmask = 0x0000ff00;//0x0000FF00
fmt->Bmask = 0x000000ff;//0x000000FF
fmt->Amask = 0;
SDL_Surface *osd_test = SDL_ConvertSurface(text,fmt,0);
if(osd_test == NULL)
{
printf("%s:[%d] \n",__FUNCTION__,__LINE__);
exit(0);
}
osd_bmp_in->pData = malloc(2*(osd_test->w)*(osd_test->h));
if(osd_bmp_in->pData == NULL)
{
printf("%s:[%d] \n",__FUNCTION__,__LINE__);
exit(0);
}
/*位图的高*宽就是总像素,总像素*每个像素的内存空间大小,就是整个bmp占内存的大小*/
memset(osd_bmp_in->pData,0,(2*(osd_test->w)*(osd_test->h)));
printf("%s:[%d] \n",__FUNCTION__,__LINE__);
osd_bmp_in->u32Width = osd_test->w;
osd_bmp_in->u32Height = osd_test->h;
/*此处最好不要直接使用指针进行位图内存的指向,这样存在潜在的内存泄漏,这样memcpy后可以手动释放*/
memcpy(osd_bmp_in->pData,osd_test->pixels,(2*(osd_test->w)*(osd_test->h)));
//osd_bmp_in->pData = osd_test->pixels;
printf("%s:[%d] \n",__FUNCTION__,__LINE__);
osd_bmp_in->enPixelFormat= PIXEL_FORMAT_RGB_1555 ;
SDL_FreeSurface(text);
text = NULL;
SDL_FreeSurface(osd_test);
text = NULL;
free(fmt);
fmt = NULL;
return 0;
}
注:
1、改程序测试没有内存泄漏
总结:
位图占用内存的大小为:“每个像素占用内存的大小*总像素个数”