cocos2d-x中使图片背景全透明(另一种方法)
2014-05-16 10:59
351 查看
CCLayerColor::initWithColor(ccc4(255,255,255,255));
CCImage *pImage = new CCImage();
pImage->autorelease();
pImage->initWithImageFile("arraw.png",CCImage::EImageFormat::kFmtPng);
//遍历图片的所有像素.
unsigned char *pData = pImage->getData();
int nPixelIndex = 0;
for (int nCol = 0; nCol < pImage->getHeight(); nCol ++)
{
for (int nRow = 0; nRow < pImage->getWidth(); nRow ++)
{
//取图片的RGB值.
int nBeginPos = nPixelIndex;
unsigned int nRValue = pData[nPixelIndex];
nPixelIndex++;
unsigned int nGValue = pData[nPixelIndex];
nPixelIndex ++;
unsigned int nBValue = pData[nPixelIndex];
nPixelIndex ++;
unsigned int n***alue = pData[nPixelIndex];
nPixelIndex ++;
int nAlphaRatio = 0;
//本代码的核心:取RGB中的最大值赋给nAlphaRatio。如果nAlphaRatio为0,则像素中的alpha通道就为0,否则像素中的
//alpha通道值就是nAlphaRatio。这样做是为了在图片中颜色渐变过渡比较大的区域实现平滑的过渡。让最终形成的
//图片看起来不粗糙.
nAlphaRatio = nRValue>nGValue?(nRValue>nBValue?nRValue:nBValue):(nGValue>nBValue?nGValue:nBValue);
if(nAlphaRatio != 0)
{
n***alue = nAlphaRatio;
}
else
{
n***alue= 0;
}
pData[nBeginPos] = (unsigned
char)nRValue;
pData[nBeginPos+ 1] = (unsigned
char)nGValue;
pData[nBeginPos + 2] = (unsigned
char)nBValue;
//修改原图的alpha值.
pData[nBeginPos + 3] = (unsigned
char)n***alue;
}
}
CCTexture2D *pTexture = new CCTexture2D;
pTexture->autorelease();
pTexture->initWithImage(pImage);
CCTexture2DPixelFormat ccpf = pTexture->getPixelFormat();
CCAssert(ccpf == kTexture2DPixelFormat_RGBA8888, "your
png file's pixel format is not RGBA8888 or not have alpha panel");
CCSprite* pArrowSprite= new CCSprite();
pArrowSprite->initWithTexture(pTexture);
CCSize size = CCDirector::sharedDirector()->getWinSize();
pArrowSprite->setPosition(ccp(size.width/2 + 20, size.height/2 - 20));
this->addChild(pArrowSprite, 6);
直接看代码注释就懂了。贴上效果图,打完收工。
CCImage *pImage = new CCImage();
pImage->autorelease();
pImage->initWithImageFile("arraw.png",CCImage::EImageFormat::kFmtPng);
//遍历图片的所有像素.
unsigned char *pData = pImage->getData();
int nPixelIndex = 0;
for (int nCol = 0; nCol < pImage->getHeight(); nCol ++)
{
for (int nRow = 0; nRow < pImage->getWidth(); nRow ++)
{
//取图片的RGB值.
int nBeginPos = nPixelIndex;
unsigned int nRValue = pData[nPixelIndex];
nPixelIndex++;
unsigned int nGValue = pData[nPixelIndex];
nPixelIndex ++;
unsigned int nBValue = pData[nPixelIndex];
nPixelIndex ++;
unsigned int n***alue = pData[nPixelIndex];
nPixelIndex ++;
int nAlphaRatio = 0;
//本代码的核心:取RGB中的最大值赋给nAlphaRatio。如果nAlphaRatio为0,则像素中的alpha通道就为0,否则像素中的
//alpha通道值就是nAlphaRatio。这样做是为了在图片中颜色渐变过渡比较大的区域实现平滑的过渡。让最终形成的
//图片看起来不粗糙.
nAlphaRatio = nRValue>nGValue?(nRValue>nBValue?nRValue:nBValue):(nGValue>nBValue?nGValue:nBValue);
if(nAlphaRatio != 0)
{
n***alue = nAlphaRatio;
}
else
{
n***alue= 0;
}
pData[nBeginPos] = (unsigned
char)nRValue;
pData[nBeginPos+ 1] = (unsigned
char)nGValue;
pData[nBeginPos + 2] = (unsigned
char)nBValue;
//修改原图的alpha值.
pData[nBeginPos + 3] = (unsigned
char)n***alue;
}
}
CCTexture2D *pTexture = new CCTexture2D;
pTexture->autorelease();
pTexture->initWithImage(pImage);
CCTexture2DPixelFormat ccpf = pTexture->getPixelFormat();
CCAssert(ccpf == kTexture2DPixelFormat_RGBA8888, "your
png file's pixel format is not RGBA8888 or not have alpha panel");
CCSprite* pArrowSprite= new CCSprite();
pArrowSprite->initWithTexture(pTexture);
CCSize size = CCDirector::sharedDirector()->getWinSize();
pArrowSprite->setPosition(ccp(size.width/2 + 20, size.height/2 - 20));
this->addChild(pArrowSprite, 6);
直接看代码注释就懂了。贴上效果图,打完收工。
相关文章推荐
- 【转】ie6下使PNG背景图片透明的方法
- IE6下PNG透明图片背景显示灰色的完美解决方法
- 轻松解决PNG图片在IE6中背景不透明方法
- vc++加载透明png图片方法——GDI+和CImage两种 ,vs2005 vs2008 PNG背景
- 解决png图片在IE6背景无法透明不兼容解决方法
- 当把lable或着picture放到图片上使lable和picture透明到图片背景的方法
- visio实现背景透明图片的最简单方法
- 解决PNG图片在IE6中背景不透明方法_解决IE6中PNG背景不透明方法
- DZ X页头主菜单背景图片透明自由更换页头背景图片的方法
- WebView设置透明和设置背景图片的方法
- 实现png图片和png背景透明(支持多浏览器)的方法
- ie6下使PNG背景图片透明的方法
- beamer中插入透明背景图片的方法(转)
- PNG格式图片在IE6下的背景透明问题-解决方法
- 使IE6下PNG背景图片透明的七种方法
- 从Photoshop CS2中获得透明背景图片的方法
- E6中PNG透明背景图片处理方法
- png图片在ie6下背景透明的最低效的方法
- IE6显示png透明背景、图片的方法
- IE6显示png透明背景、图片的方法