cocos jpg alpha_mask png
2016-01-07 17:26
726 查看
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;
}
相关文章推荐
- Flex 获得png透明截图的问题和解决方法
- 获取图片宽度和高度的类,支持JPG,GIF,PNG,BMP
- Cross-Browser Variable Opacity with PNG
- C语言实现BMP转换JPG的方法
- Cocos2d-x中背景音乐和音效使用实例
- Cocos2d-x学习笔记之CCScene、CCLayer、CCSprite的默认坐标和默认锚点实验
- Cocos2d-x UI开发之CCControlPotentiometer控件类使用实例
- Cocos2d-x UI开发之文本类使用实例
- Cocos2d-x保存用户游戏数据之XML文件是否存在问题判断方法
- Cocos2d-x UI开发之菜单类使用实例
- Cocos2d-x UI开发之CCControlButton控件类实例
- Cocos2d-x学习笔记之世界坐标系、本地坐标系、opengl坐标系、屏幕坐标系
- php不用GD库生成当前时间的PNG格式图象的程序第1/2页
- IE浏览器下PNG相关功能
- Cocos2d-x UI开发之CCControlColourPicker控件类使用实例
- Cocos2d-x学习笔记之CCLayerColor层的使用实例
- jpg 木马如何执行 如何防止.JPG类型木马?
- Cocos2d-x Schedule定时器的使用实例
- php 将bmp图片转为jpg等其他任意格式的图片
- Cocos2d-x中使用CCScrollView来实现关卡选择实例