获取并设置CCSprite(cocos2d-x)中像素的RGB/RGBA值 [英] Getting and setting the RGB / RGBA value of a pixel in a CCSprite (cocos2d-x)
问题描述
我为什么需要这个?
基本上,我需要将彩色图像转换为灰度图像.包括图像的灰度版本可能是一种解决方案,但是在我的情况下空间很小-我不希望我的APK太大.此外,我也想对像素进行一些效果处理.同样,这是为了使APK变小.
Basically I need to turn a color image into gray-scale. Including a gray-scale version of the image could be a solution, but space is tight in my situation - I don't want my APK to be too big. Besides, I would like to work on the pixels for some effects too. Again, this is to make the APK smaller.
我从CCTexture2D找到了 getPixel setPixel 和获取图像的像素RGBA ,但是我想要更简单的方法.
I have found getPixel setPixel from CCTexture2D and Getting image's pixel RGBA, but I would like something more simple.
感谢您的帮助.
谢谢!
推荐答案
这是我为您提供的解决方案:
Here is my solution for you :
1.首先制作图像的CCImage版本:
1.First make a CCImage version of your image:
I)来自文件:
CCImage *img= new CCImage();
img->initWithImageFile("colors.png");
II)来自Sprite:
II) From Sprite :
-
II.1)CCSprite->RenderTexture2D
II.1) CCSprite -> RenderTexture2D
II.2)RenderTexture2D->CCImage( CCImage * testImage = RenderText2D-> newCCImage();
)
II.2) RenderTexture2D -> CCImage (CCImage *testImage = RenderText2D->newCCImage();
)
2.然后您可以执行所需的操作:
2.Then You can do what you need :
CCImage *img= ... // make CCImage from CCSprite
int x=3;
if(img->hasAlpha())
x=4;
unsigned char *data = new unsigned char[img->getDataLen()*x];
data = img->getData();
// [0][0] => Left-Top Pixel !
// But cocos2d Location Y-axis is Bottom(0) to Top(max)
for(int i=0;i<img->getWidth();i++)
{
for(int j=0;j<img->getHeight();j++)
{
unsigned char *pixel = data + (i + j * img->getWidth()) * x;
// You can see/change pixels' RGBA value(0-255) here !
unsigned char r = *pixel;
unsigned char g = *(pixel + 1);
unsigned char b = *(pixel + 2) ;
unsigned char a = *(pixel + 3);
}
}
3.然后,将其转换为texture_2D
3.Then, convert it to texture_2D
//CCImage -> Texture2d
texture_2D= new CCTexture2D();
texture_2D->initWithImage(img);
4.最后回到CCSprite
4.And Finally back to CCSprite
CCSprite *result= CCSprite::createWithTexture(texture_2D);
这篇关于获取并设置CCSprite(cocos2d-x)中像素的RGB/RGBA值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!