//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;
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 = newCCTexture2D;
texture->initWithImage(finalImage);
CCSprite* newSprite = CCSprite::createWithTexture(texture);
delete finalImage;
texture->release();
return newSprite;