纯C语言RGBA8888转ARGB1555格式

#define MICSIZE 10
#if 1

	FILE* fpbmp;
	FILE* fpout;
	unsigned char* pu8FileBuffer = NULL;
	fpbmp = fopen("xxx.rgba", "rb");

	if (fpbmp == NULL)
	{
		printf("Open bmp failed!!!\n");
	}
	else
	{
		printf("Open bmp success!!!\n");
	}

	fpout = fopen("1111.argb1555", "wb+");
	if (fpout == NULL)
	{
		printf("Open out.bmp failed!!!\n");
	}
	else
	{
		printf("Open out.bmp success!!!\n");
	}

	fseek(fpbmp, 0, SEEK_SET);  //定位原图 偏移位置
	fseek(fpout, 0, SEEK_SET);  //定位新图 偏移位置


	unsigned char u8bitAlphaChannel = 0;
	unsigned char u8bitRedChannel = 0;
	unsigned char u8bitGreenChannel = 0;
	unsigned char u8bitBlueChannel = 0;
	unsigned int alpha = 0;
	unsigned int red = 0;
	unsigned int green = 0;
	unsigned int blue = 0;
	unsigned short ARGB1555pixel= 0;


	pu8FileBuffer = (unsigned char*)malloc(MICSIZE * MICSIZE *4);
	if (pu8FileBuffer == NULL)
	{
		printf("malloc failed fileSize=%d\n", fpbmp);
		fclose(fpbmp);
		 
	}
	memset(pu8FileBuffer, 0, MICSIZE * MICSIZE *4);
	//fseek(pFile, 54, SEEK_SET);

	fread(pu8FileBuffer, 1, MICSIZE * MICSIZE *4, fpbmp);
	fclose(fpbmp);

	unsigned short* pBmpBufOut = NULL;
	pBmpBufOut = (unsigned short*)malloc(MICSIZE * MICSIZE * 2);
	memset(pBmpBufOut, 0, MICSIZE * MICSIZE * 2);



	for (int i = 0; i < MICSIZE * MICSIZE * 4; i+=4)
	{

		u8bitAlphaChannel = *(pu8FileBuffer + i + 3);
		u8bitRedChannel = *(pu8FileBuffer + i + 0);
		u8bitGreenChannel  = *(pu8FileBuffer + i + 1);
		u8bitBlueChannel = *(pu8FileBuffer + i + 2);

		alpha = (u8bitAlphaChannel + 127) / 255;
		red = (u8bitRedChannel * 31 + 127) / 255;
		green = (u8bitGreenChannel * 31 + 127) / 255;
		blue = (u8bitBlueChannel * 31 + 127) / 255;
		ARGB1555pixel = (alpha << 15) | (red << 10) | (green << 5) | blue;

		*(pBmpBufOut + i / 4) = ARGB1555pixel;
	}
	fwrite(pBmpBufOut, MICSIZE * MICSIZE, sizeof(unsigned short), fpout);

	fclose(fpout);
#endif

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]中提供的代码,可以使用以下三种方法将RGBA转换为ARGB: 方法1:字符串截取和组合 ```java String argbStr = rgba.substring(0, 1) + rgba.substring(7, 9) + rgba.substring(1, 7); int argb = Color.parseColor(argbStr); ``` 方法2:为Char数组,遍历重组 ```java char\[\] chars = rgba.toCharArray(); StringBuilder tempStr = new StringBuilder(); for (int i = 0; i < chars.length; i++) { if (i == 0) { tempStr.append(chars\[0\]); } else if (i == 1) { tempStr.append(chars\[7\]); } else if (i == 2) { tempStr.append(chars\[8\]); } else { tempStr.append(chars\[i - 2\]); } } int argb = Color.parseColor(tempStr.toString()); ``` 方法3:位操作 ```java int rgba = Color.parseColor(rgbaStr); int argb = (rgba >>> 8) | (rgba << (32 - 8)); ``` 另外,引用\[2\]中提供了类似的代码,可以根据需要进行调整和验证。引用\[3\]提到了在使用openGLES时,需要将RGBA转换为ARGB,但没有提供具体的代码实现。 #### 引用[.reference_title] - *1* *2* [Android 必知必会 - RGBAARGB](https://blog.csdn.net/ys743276112/article/details/105443683)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [openGLES使用:RGBAARGB](https://blog.csdn.net/aa642531/article/details/80230121)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值