Texture2D* StartLayer::addJpgMaskPng(const char* jpgName, const char* maskName)
{
CCImage *jpgImage = new CCImage();
jpgImage->initWithImageFile(jpgName);
unsigned char *jpgData = jpgImage->getData();
int lenJPGPerPixel = jpgImage->getBitPerPixel() / 8;
int width = jpgImage->getWidth();
int height = jpgImage->getHeight();
int len = width * height;
CCImage *alphaImage = new CCImage();
alphaImage->initWithImageFile(maskName);
unsigned char *alphaData = alphaImage->getData();
int lenAlphaPerPixel = alphaImage->getBitPerPixel() / 8;
unsigned char *outPic = new unsigned char[width * height * 4];
int outIndex = 0;
int srcIndex = 0;
for (int i = 0; i < len; i++)
{
outPic[outIndex + 0] = jpgData[srcIndex + 0];
outPic[outIndex + 1] = jpgData[srcIndex + 1];
outPic[outIndex + 2] = jpgData[srcIndex + 2];
outPic[outIndex + 3] = alphaData[i*lenAlphaPerPixel];
srcIndex += 3;
outIndex += 4;
}
CCTexture2D *texture = new CCTexture2D();
texture->initWithData(outPic, len * 4, Texture2D::PixelFormat::RGBA8888, width, height, Size((float)width, (float)height));
delete jpgImage;
delete alphaImage;
delete[] outPic;
return texture;
}
{
CCImage *jpgImage = new CCImage();
jpgImage->initWithImageFile(jpgName);
unsigned char *jpgData = jpgImage->getData();
int lenJPGPerPixel = jpgImage->getBitPerPixel() / 8;
int width = jpgImage->getWidth();
int height = jpgImage->getHeight();
int len = width * height;
CCImage *alphaImage = new CCImage();
alphaImage->initWithImageFile(maskName);
unsigned char *alphaData = alphaImage->getData();
int lenAlphaPerPixel = alphaImage->getBitPerPixel() / 8;
unsigned char *outPic = new unsigned char[width * height * 4];
int outIndex = 0;
int srcIndex = 0;
for (int i = 0; i < len; i++)
{
outPic[outIndex + 0] = jpgData[srcIndex + 0];
outPic[outIndex + 1] = jpgData[srcIndex + 1];
outPic[outIndex + 2] = jpgData[srcIndex + 2];
outPic[outIndex + 3] = alphaData[i*lenAlphaPerPixel];
srcIndex += 3;
outIndex += 4;
}
CCTexture2D *texture = new CCTexture2D();
texture->initWithData(outPic, len * 4, Texture2D::PixelFormat::RGBA8888, width, height, Size((float)width, (float)height));
delete jpgImage;
delete alphaImage;
delete[] outPic;
return texture;
}