代码:
目标值 = 上图alpha值*上图值 + (1-上图alpha值)*下图值 alpha = [0, 1];- CCImage* body = new CCImage();
- body->initWithImageFile("b.png", CCImage::kFmtPng);
- CCImage* cloth = new CCImage();
- cloth->initWithImageFile("y.png", CCImage::kFmtPng);
- CCImage* hair = new CCImage();
- hair->initWithImageFile("t.png", CCImage::kFmtPng);
-
- unsigned char *bData = body->getData();
- unsigned char *cData = cloth->getData();
- unsigned char *hData = hair->getData();
-
- int iIndex = 0;
- for (int i = 0; i < body->getHeight(); i ++)
- {
- for (int j = 0; j < body->getWidth(); j ++)
- {
- int iBPos = iIndex;
-
- unsigned int bodyB = bData[iIndex];
- unsigned int clothB = cData[iIndex];
- unsigned int hairB = hData[iIndex];
- iIndex ++;
- unsigned int bodyG = bData[iIndex];
- unsigned int clothG = cData[iIndex];
- unsigned int hairG = hData[iIndex];
- iIndex ++;
- unsigned int bodyR = bData[iIndex];
- unsigned int clothR = cData[iIndex];
- unsigned int hairR = hData[iIndex];
- iIndex ++;
- unsigned int bodyA = bData[iIndex];
- unsigned int clothA = cData[iIndex];
- unsigned int hairA = hData[iIndex];
- iIndex ++;
- //身体和衣服混合
- bodyB = clothB*(clothA/255.f) + ((255.f-clothA)/255.f)*bodyB;
- bodyG = clothG*(clothA/255.f) + ((255.f-clothA)/255.f)*bodyG;
- bodyR = clothR*(clothA/255.f) + ((255.f-clothA)/255.f)*bodyR;
- bodyA =((clothA/255.f)*(clothA/255.f)+((255.f-clothA)/255.f)*(bodyA/255.f))*255.f;
- //身体和衣服混合后,再和头发混合
- bodyB = hairB*(hairA/255.f) + ((255.f-hairA)/255.f)*bodyB;
- bodyG = hairG*(hairA/255.f) + ((255.f-hairA)/255.f)*bodyG;
- bodyR = hairR*(hairA/255.f) + ((255.f-hairA)/255.f)*bodyR;
- bodyA =((hairA/255.f)*(hairA/255.f)+((255.f-hairA)/255.f)*(bodyA/255.f))*255.f;
- //结果值
- bData[iBPos] = (unsigned char)bodyB;
- bData[iBPos + 1] = (unsigned char)bodyG;
- bData[iBPos + 2] = (unsigned char)bodyR;
- bData[iBPos + 3] = (unsigned char)bodyA;
- }
- }
-
- CCTexture2D* texture = new CCTexture2D;
- texture->initWithImage(body);
- CCSprite* bSprite = CCSprite::spriteWithTexture(texture);
- bSprite->setPosition(ccp(240,150));
- addChild(bSprite, 1);
- body->release();
- cloth->release();
- hair->release();
- texture->release();
但是不建议在批量处理图片混合时使用,非常吃内存,手机撑不住