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];
}
*(rgb2 + (j * 4) + (i * pitch2) + 3) = (unsigned char)((add) & 0xFF);
}
}
out.SetTransparentColor(RGB(44, 44, 44));
img.Destroy();
out.Save(argv[1],Gdiplus::ImageFormatPNG);
out.Destroy();
}