原文地址:http://i.kimiazhu.info/?p=280
转一篇,不过本位介绍的加亮和之前我想做的加亮闪缩效果还是有差别。
主要的思路是通过CCImage先载入图片,然后getData()获取图片数据,接着逐一获取各像素的RGB值(代码基于jpg,若是png则需要获取RGBA值),通过相应方法修改各像素点的值。之后将该CCImange加载到CCTexture2D中,再由CCSprite载入,然后即可自由操作CCSprite。
以上相当于是在生成CCSprite时载入已加亮或变灰了的图片,至于如何将已有的CCSprite加亮或变灰,尚等研究。
C++ Code:
//根据现有CCSprite,变亮和变灰 static CCSprite* graylightWithCCSprite(CCSprite* oldSprite,bool isLight) { //CCSprite转成CCimage CCPoint p = oldSprite->getAnchorPoint(); oldSprite->setAnchorPoint(ccp(0,0)); CCRenderTexture *outTexture = CCRenderTexture::create((int)oldSprite->getContentSize().width,(int)oldSprite->getContentSize().height); outTexture->begin(); oldSprite->visit(); outTexture->end(); oldSprite->setAnchorPoint(p); CCImage* finalImage = outTexture->newCCImage(); unsigned char *pData = finalImage->getData(); int iIndex = 0; if(isLight) { for (int i = 0; i < finalImage->getHeight(); i ++) { for (int j = 0; j < finalImage->getWidth(); j ++) { // highlight int iHightlightPlus = 50; int iBPos = iIndex; unsigned int iB = pData[iIndex]; iIndex ++; unsigned int iG = pData[iIndex]; iIndex ++; unsigned int iR = pData[iIndex]; iIndex ++; //unsigned int o = pData[iIndex]; iIndex ++; //原来的示例缺少 iB = (iB + iHightlightPlus > 255 ? 255 : iB + iHightlightPlus); iG = (iG + iHightlightPlus > 255 ? 255 : iG + iHightlightPlus); iR = (iR + iHightlightPlus > 255 ? 255 : iR + iHightlightPlus); // iR = (iR < 0 ? 0 : iR); // iG = (iG < 0 ? 0 : iG); // iB = (iB < 0 ? 0 : iB); pData[iBPos] = (unsigned char)iB; pData[iBPos + 1] = (unsigned char)iG; pData[iBPos + 2] = (unsigned char)iR; } } }else{ for (int i = 0; i < finalImage->getHeight(); i ++) { for (int j = 0; j < finalImage->getWidth(); j ++) { // gray int iBPos = iIndex; unsigned int iB = pData[iIndex]; iIndex ++; unsigned int iG = pData[iIndex]; iIndex ++; unsigned int iR = pData[iIndex]; iIndex ++; //unsigned int o = pData[iIndex]; iIndex ++; //原来的示例缺少 unsigned int iGray = 0.3 * iR + 0.4 * iG + 0.2 * iB; pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray; } } } CCTexture2D *texture = new CCTexture2D; texture->initWithImage(finalImage); CCSprite* newSprite = CCSprite::createWithTexture(texture); delete finalImage; texture->release(); return newSprite; }
<pre name="code" class="cpp">CCTexture2D* GetGaryTexture( string fileName ) { string garyFileName; for( int i = 0; i < fileName.size(); i++ ) { if( fileName[ i ] == '.' ) { garyFileName += "_Gary"; } garyFileName += fileName[ i ]; } CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->textureForKey( garyFileName.c_str() ); if ( !pTexture ) { CCImage* pImg = NULL; pImg = new CCImage(); pImg->initWithImageFile( fileName.c_str() ); unsigned char *pData = pImg->getData(); int iIndex = 0; for (int i = 0; i < pImg->getHeight(); i ++) { for (int j = 0; j < pImg->getWidth(); j ++) { int iBPos = iIndex; unsigned int iB = pData[iIndex]; iIndex ++; unsigned int iG = pData[iIndex]; iIndex ++; unsigned int iR = pData[iIndex]; iIndex ++; #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) iIndex ++; #else if( pImg->hasAlpha() )iIndex++; #endif unsigned int iGray = 0.3 * iR + 0.6 * iG + 0.1 * iB; pData[iBPos] = pData[iBPos + 1] = pData[iBPos + 2] = (unsigned char)iGray; } } pTexture = CCTextureCache::sharedTextureCache()->addUIImage(pImg,garyFileName.c_str() );//加入缓存中 pImg->release(); } return pTexture; }
无符号
的char *
的pData
=
pImg-
>
的getData
(
)
;
INT iIndex = 0 ;
的for(int i = 0 ; 我 < pImg- > 的getHeight ( ) ; 我 ++ )
{
对于 ( INT J = 0 ; Ĵ < pImg- > 的getWidth ( ) ; J ++ )
{
INT iBPos = iIndex ;
unsigned int类型 IB = 的pData [ iIndex ]
iIndex ++;
unsigned int类型 iG的 = 的pData [ iIndex ]
iIndex ++;
unsigned int类型 IR = 的pData [ iIndex ]
iIndex ++;
#如果 ( CC_TARGET_PLATFORM == CC_PLATFORM_IOS )
iIndex ++;
#else指令
如果 ( pImg- > hasAlpha ()) iIndex ++;
#ENDIF
unsigned int类型 iGray = 0.3 * 红外 + 0.6 * iG的 + 0.1 * IB ;
pData所 [ iBPos ] = 的pData [ iBPos + 1 ] = 的pData [ iBPos + 2 ] = ( unsigned char型 ) iGray ;
}
}
pTexture = CCTextureCache :: sharedTextureCache () - > addUIImage ( PIMG , garyFileName.c_str ( )); // 加入缓存中
pImg- > 发行 ( ) ;
}
返回 pTexture ;
}
INT iIndex = 0 ;
的for(int i = 0 ; 我 < pImg- > 的getHeight ( ) ; 我 ++ )
{
对于 ( INT J = 0 ; Ĵ < pImg- > 的getWidth ( ) ; J ++ )
{
INT iBPos = iIndex ;
unsigned int类型 IB = 的pData [ iIndex ]
iIndex ++;
unsigned int类型 iG的 = 的pData [ iIndex ]
iIndex ++;
unsigned int类型 IR = 的pData [ iIndex ]
iIndex ++;
#如果 ( CC_TARGET_PLATFORM == CC_PLATFORM_IOS )
iIndex ++;
#else指令
如果 ( pImg- > hasAlpha ()) iIndex ++;
#ENDIF
unsigned int类型 iGray = 0.3 * 红外 + 0.6 * iG的 + 0.1 * IB ;
pData所 [ iBPos ] = 的pData [ iBPos + 1 ] = 的pData [ iBPos + 2 ] = ( unsigned char型 ) iGray ;
}
}
pTexture = CCTextureCache :: sharedTextureCache () - > addUIImage ( PIMG , garyFileName.c_str ( )); // 加入缓存中
pImg- > 发行 ( ) ;
}
返回 pTexture ;
}