Get raw image data from sprite or texture2d

Get raw image data from sprite or texture2d
0.0 0


How can i get raw image data from sprite or texture2d(like CCImage getData())?

Cocos2d-x 3 way to extract Image data from texture2D if i loaded plist file into SpriteFrameCache

Ok, i got solution:
For cocos2d-1.0.1-x-0.13.0-beta

// fragment - CCSprite with dinamically generated texture
CCRenderTexture* r = CCRenderTexture::renderTextureWithWidthAndHeight(fragment->getTexture()->getPixelsWide(), fragment->getTexture()->getPixelsHigh());

    r->beginWithClear(1, 1, 1, 0);

    CCImage *testImage = new CCImage;
unsigned char *data = testImage->getData();

in Cocos2d-x 2.0.1:

CCRenderTexture* r = CCRenderTexture::renderTextureWithWidthAndHeight(fragment->getTexture()->getPixelsWide(), fragment->getTexture()->getPixelsHigh());

    r->beginWithClear(1, 1, 1, 0);

    CCImage *testImage = r->newCCImage();
unsigned char *data = testImage->getData();

If you know texture name and it’s stored in your assets directory, so you can use that:

CCImage *testImage = new CCImage;
unsigned char *data = testImage->getData();


Thanks Alex for your solution. You’ve saved my time :wink:


Hi !

I need to access specific pixels in my game, and for that I’m using your solution to retrieve data, but how do you use the “unsigned char *data” after that ?

I am trying to simply show “data[0]” in the log but nothing shows up :confused: I tried different format (s, d, i, x, %p)… data, as a pointer, shows its adress but then nothing :confused:

I don’t understand how to use it, can you help me ?

Many thanks !


unsigned char data is a pointer to raw image data so if you want to get rgba values of pixel which has coordinates then this should work:
unsigned char
pixel = data + (x + y * width) * 4;
unsigned char r = pixel;
unsigned char g =
(pixel + 1);
unsigned char b = ;
unsigned char a =
(pixel + 3);

I’m assuming the image is rgba8888 (32 bit);


Thanks for the answer !

Though I can’t get it to work :confused:

Here is what I do :
unsigned char *pigeonData = pigeon->getData(); // calls image->getData() inside pigeon.
unsigned char *pigeonPixel = pigeonData + pigeonIndice * 4; // pigeonIndice is the (x + y * width)

unsigned char redPigeon = *pigeonPixel;
unsigned char greenPigeon = *(pigeonPixel + 1);
unsigned char bluePigeon = *(pigeonPixel + 2);

Then I log using the following “template” :
cocos2d::CCLog("World::checkCollisions() > pigeonPixel = %x", pigeonPixel);
And I do the same for r,g,b.

pigeonPixel gives me something, so data is not null, but r,g and b stay at 0 :confused:

I must be missing / doing something stupid here… :s

My aim is to add r,g,b and see if it’s different than 0 (transparent pixel or not).

Thanks again ! =D


Is you image 32bit rgba8888? image~~>getBitsPerComponent should return 8 and image~~>hasAlpha() should return true. If you are using 24bit image without alpha channel (for example jpg) then you should be multiplying pixel index with 3 (unsigned char pigeonPixel = pigeonData + pigeonIndice 3; ) Also you can check image~~>getWidth and image~~>getHeight() to be sure that your image is loaded correctly.


Yes, I have everything as expected (8 bits, alpha, correct width). My image is a png file by the way.

It appears that my code works… The problem might be how I log it (using %x).

I’ll have to run some other tests to be sure that it’s only a noob log error though :o)

Thanks for your help!



Thanks Leszek S
Your solution saved me !


Already tried all the options, but I did not get the dynamically generated textures. For example, the code below

  CCTexture2D *Texture = new CCTexture2D();
  static Color4B Data[1024*1024];
  for(int i = 0; i < 1024*1024; i++) 
  	Data[i] = black;
  Texture->initWithData(Data, sizeof(Color4B)*1024*1024, kCCTexture2DPixelFormat_RGBA8888, 1024, 1024, CCSize(1024, 1024));

is not working since glGetError() inside initWithData() function returns an error (1228 or 0x502).
How do I get manual editing textures dynamically?


Please help!


What’s the equivalent of CCTexture2D::getData in v3? Can’t find anything about it, even a deprecation warning in the source.


Has anyone got a solution for this?? please help


The solution is mentioned by @CrazyD0G. This is what works for me in 3.9

// sourceSprite is grab texture from
int width = sourceSprite->getTexture()->getPixelsWide();
int height = sourceSprite->getTexture()->getPixelsHigh();

auto renderTexture = RenderTexture::create(width, height, sourceSprite->getTexture()->getPixelFormat());

sourceSprite->setPosition(width/2, height/2);

auto sprite = Sprite::createWithTexture(renderTexture->getSprite()->getTexture());
sprite->setFlippedY(true); // important



I also want this in 3.14 version



Image *img;
img = new cocos2d::Image();
img->initWithImageFile( "imagename.png" );
unsigned char* rawdata = img->getData();


Hi . Can we handle a individual pixel with this data and check if it is alpha ?


Using rawdata and supposing the image is in format rgba8888, you have 4 bytes (4 chars) for each pixel. So, to get data of a specific pixel you can use the following formula:

red   = rawdata[ (y * imageWidth + x) * 4 + 0 ];
green = rawdata[ (y * imageWidth + x) * 4 + 1 ];
blue  = rawdata[ (y * imageWidth + x) * 4 + 2 ];
alpha = rawdata[ (y * imageWidth + x) * 4 + 3 ];