字体图片rgb565格式转换为ARGB888格式

RGB的意思是红色(Red)、绿色(Green)和蓝色(Blue)
rgb565 是16位(2字节)
565表示在16位编码中每个颜色分量所占的位数。红色占5个bit,绿色占6个bit,蓝色占5个bit。
argb888 是32位 (4字节)
888表示红色占8个bit,绿色占8个bit,蓝色占8个bit,还有a表示透明度,也是占8个bit
那么一个像素点占8+8+8+8=32位(4字节)

直接贴参考代码

//将渲染后的Surface转换成Bitmap
 void MySample_SurfaceWord_ToBMP(SDL_Surface *surface,BITMAP_S *stBitmap,SDL_Color fntcol)
{
  unsigned short words_color = ((fntcol.r >> 3) << 11) + ((fntcol.g >> 2) << 5) + (fntcol.b >> 3);  //字体颜色
  unsigned short bck_color = 0xffff - words_color; //字体以外的背景颜色
  stBitmap->u32Height = (surface->h); //BITMAP 的宽高向上2对齐
	stBitmap->u32Width = (surface->w);
	stBitmap->pData = malloc(4*(stBitmap->u32Height)*(stBitmap->u32Width)); //申请空间,ARGB8888=>4Byte/Pixel,总大小为4*w*h
	memset(stBitmap->pData,0,4*(stBitmap->u32Height)*(stBitmap->u32Width));
	int i,j;
	int w = surface->w;
	int h = surface->h;

	for (i = 0; i < h; ++i)
	{
		RK_U32 *p_dst = (RK_U32*)stBitmap->pData;
		RK_U16 *p_src = (RK_U16*)surface->pixels;
	    int dis_pos = 0;
	    if(w % 2 != 0)
	    	dis_pos = i;     //处理w为奇数的情况
		for(j=0;j<w;j++)
		{
			int a,r, g , b;
			r = (p_src[i*w+dis_pos+j] & 0xF800) >> 8;   //原图像是RGB565,RGB各分量提取
			g = (p_src[i*w+dis_pos+j] & 0x07e0) >> 3;
			b = (p_src[i*w+dis_pos+j] & 0x001f) << 3;
			
      //一致则A位设置为0,透明
      if (bck_color == p_src[i*w+dis_pos+j])
      a = 0x00;   //当发现这一位与背景颜色相同时候,把a的透明度设为完全透明
      else a = 0xff;

			p_dst[i*stBitmap->u32Width+j] = (a << 24) | (r << 16) | (g << 8) | b; //转换成ARGB888
		}
	}
	stBitmap->enPixelFormat = PIXEL_FORMAT_ARGB_8888;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值