备忘一下.
//得到灰度CCSprite, int l_iGrayValue = (int)(l_iR*0.299 + l_iG*0.587 + l_iB*0.114);
CCSprite* Tools::getGraySpriteWithFile(const char* p_file){
CCSprite* l_sprite = Tools::getSpriteWithFile(p_file);
CCSize l_sizeInPixel = l_sprite->getContentSizeInPixels();
//开辟出内存buffer
unsigned char* l_buffer = (unsigned char*)malloc(l_sizeInPixel.width * l_sizeInPixel.height * 4);
memset(l_buffer, 0, l_sizeInPixel.width * l_sizeInPixel.height * 4);
//写内存
CCRenderTexture* l_renderTexture = CCRenderTexture::renderTextureWithWidthAndHeight(l_sizeInPixel.width, l_sizeInPixel.height, kCCTexture2DPixelFormat_RGBA8888);
l_renderTexture->begin();
l_sprite->draw();
glReadPixels(0, 0, l_sizeInPixel.width, l_sizeInPixel.height, GL_RGBA, GL_UNSIGNED_BYTE, l_buffer);
l_renderTexture->end();
//变成灰度内存
for (int i=0; i<l_sizeInPixel.height; i++) {
for (int j=0; j<l_sizeInPixel.width; j++) {
CCPoint l_pointInPixel = ccp(j, i);
//l_buffer[4*(int)(l_pointInPixel.y*l_sizeInPixel.width + l_pointInPixel.x)+3] = 0;
int l_iR = l_buffer[4*(int)(l_pointInPixel.y*l_sizeInPixel.width + l_pointInPixel.x)+0];
int l_iG = l_buffer[4*(int)(l_pointInPixel.y*l_sizeInPixel.width + l_pointInPixel.x)+1];
int l_iB = l_buffer[4*(int)(l_pointInPixel.y*l_sizeInPixel.width + l_pointInPixel.x)+2];
int l_iGrayValue = (int)(l_iR*0.299 + l_iG*0.587 + l_iB*0.114);
l_buffer[4*(int)(l_pointInPixel.y*l_sizeInPixel.width + l_pointInPixel.x)+0] = l_iGrayValue;
l_buffer[4*(int)(l_pointInPixel.y*l_sizeInPixel.width + l_pointInPixel.x)+1] = l_iGrayValue;
l_buffer[4*(int)(l_pointInPixel.y*l_sizeInPixel.width + l_pointInPixel.x)+2] = l_iGrayValue;
}
}
//生成灰度CCSprite
CCTexture2D* l_texture = new CCTexture2D();
l_texture->initWithData(l_buffer, kTexture2DPixelFormat_RGBA8888, l_sizeInPixel.width, l_sizeInPixel.height,CCSizeMake(l_sizeInPixel.width, l_sizeInPixel.height));
l_texture->autorelease();
CCSprite* l_spriteGray = CCSprite::spriteWithTexture(l_texture);
//释放临时内存
free(l_buffer);
return l_spriteGray;
}
本文原创, 转载请注明出处!