C++ 使用Cimage将图片黑色通道转透明通道

C++ 使用Cimage将图片黑色通道转透明通道

#include <atlimage.h>
#include<iostream>
void main(int argc, char** argv)
{
	char* imgpath;
	if (argc != 5)
		return;
	imgpath = argv[1];

	CImage img;
	CImage out;
	img.Load(imgpath);
	int bpp = img.GetBPP();
	int rgb_c = bpp / 8;
	out.Create(img.GetWidth(), img.GetHeight(), 32, CImage::createAlphaChannel);
	unsigned char* rgb1 = (unsigned char*)img.GetBits();
	unsigned char* rgb2 = (unsigned char*)out.GetBits();
	int pitch1 = img.GetPitch();
	int pitch2 = out.GetPitch();

	for (int i = 0; i < img.GetHeight(); i++)
	{
		for (int j = 0; j < img.GetWidth(); j++)
		{
			int add = 0;
			unsigned char tt[3];
			for (int kkk = 0; kkk < 3; kkk++)
			{
				tt[kkk] = *(rgb1 + (j * rgb_c) + (i * pitch1) + kkk);
				add += tt[kkk];
			}
			add /= 3;
			for (int kkk = 0; kkk < 3; kkk++)
			{
				*(rgb2 + (j * 4) + (i * pitch2) + kkk) = tt[kkk];// *(float)(add / (float)255.0f);
			}
			*(rgb2 + (j * 4) + (i * pitch2) + 3) = (unsigned char)((add) & 0xFF); // 设置透明度为add的值
		}
	}
	out.SetTransparentColor(RGB(44, 44, 44));
	img.Destroy();
	out.Save(argv[1],Gdiplus::ImageFormatPNG);
	out.Destroy();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值