Forum > C++ > Getting image's pixel RGBA

Getting image's pixel RGBA

By evrac Posted 2012-02-20 11:07 Comments 2
evrac
  • Posts: 7

I’m sorry for asking this here, but i’m like browsing whole day for this..
How to get an image’s single pixel RGBA and copy it into an array?
And it’s not the whole screen, like using glreadpixel..

my friend in cocos2d iphone use this code to do it..
the CGDataProviderCopyData thing, only do exist in xcode he says..

-(unsigned char**)makeDressMap {
NSString**tempString = [NSString stringWithFormat:`"dress%i.png",dressID];
UIImage *tempDressImage = [UIImage imageNamed:tempString];

int width = (int)tempDressImage.size.width;
int height = (int)tempDressImage.size.height;

CCLOG(`“w i",width);
CCLOG(@"h i”,height);
>
unsigned char collisionMap = malloc;
memset;
>
CFDataRef imageData = CGDataProviderCopyData);
const UInt32
pixels = (const UInt32*)CFDataGetBytePtr(imageData);
>
for (int j = 0; j<(width*height); j++) {
if (pixels[j] & 0xff000000) {
collisionMap[j] |= 1;
//CCLOG(@“%i”,collisionMap[j]);
}
}
>
CFRelease(imageData);
return collisionMap;
}

GMTDev
  • Posts: 205
  • Location: Vancouver, Canada

#1 RE: 2012-02-21 20:46

I’m not sure of a simple “get one pixel” call but you can get access to an images data using “CCImage->getData();”

Have a look at the file CCTexture2D.cpp, function:
bool CCTexture2D::initPremultipliedATextureWithImage(CCImage *image, unsigned int POTWide, unsigned int POTHigh)

it uses a CCImage and calls “tempData = (unsigned char*)(image->getData());”.

Check out http://www.arrowmania.com - Arrow Mania 2! For iOS, Android and Amazon Kindle Fire. Developed using Cocsos2D-X ;)

evrac
  • Posts: 7

#2 RE: 2012-02-22 03:59

Appreciate for answering, and I have read it a little..
I think it has something loop for whole image..
But my boss said it doesn’t need to be exactly in the image, so this case will be closed for a while :)

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps