多张图片合成一张图片(alpha混合)

  代码:
  1.         CCImage* body = new CCImage();
  2.         body->initWithImageFile("b.png", CCImage::kFmtPng);
  3.         CCImage* cloth = new CCImage();
  4.         cloth->initWithImageFile("y.png", CCImage::kFmtPng);
  5.         CCImage* hair = new CCImage();
  6.         hair->initWithImageFile("t.png", CCImage::kFmtPng);

  7.         unsigned char *bData = body->getData();
  8.         unsigned char *cData = cloth->getData();
  9.         unsigned char *hData = hair->getData();

  10.         int iIndex = 0;
  11.         for (int i = 0; i < body->getHeight(); i ++)
  12.         {
  13.                 for (int j = 0; j < body->getWidth(); j ++)
  14.                 {
  15.                         int iBPos = iIndex;

  16.                         unsigned int bodyB = bData[iIndex];
  17.                         unsigned int clothB = cData[iIndex];
  18.                         unsigned int hairB = hData[iIndex];
  19.                         iIndex ++;
  20.                         unsigned int bodyG = bData[iIndex];
  21.                         unsigned int clothG = cData[iIndex];
  22.                         unsigned int hairG = hData[iIndex];
  23.                         iIndex ++;
  24.                         unsigned int bodyR = bData[iIndex];
  25.                         unsigned int clothR = cData[iIndex];
  26.                         unsigned int hairR = hData[iIndex];
  27.                         iIndex ++;
  28.                         unsigned int bodyA = bData[iIndex];
  29.                         unsigned int clothA = cData[iIndex];
  30.                         unsigned int hairA = hData[iIndex];
  31.                         iIndex ++;
  32.                         //身体和衣服混合
  33.                         bodyB = clothB*(clothA/255.f) + ((255.f-clothA)/255.f)*bodyB;
  34.                         bodyG = clothG*(clothA/255.f) + ((255.f-clothA)/255.f)*bodyG;
  35.                         bodyR = clothR*(clothA/255.f) + ((255.f-clothA)/255.f)*bodyR;
  36.                         bodyA =((clothA/255.f)*(clothA/255.f)+((255.f-clothA)/255.f)*(bodyA/255.f))*255.f;
  37.                         //身体和衣服混合后,再和头发混合
  38.                         bodyB = hairB*(hairA/255.f) + ((255.f-hairA)/255.f)*bodyB;
  39.                         bodyG = hairG*(hairA/255.f) + ((255.f-hairA)/255.f)*bodyG;
  40.                         bodyR = hairR*(hairA/255.f) + ((255.f-hairA)/255.f)*bodyR;
  41.                         bodyA =((hairA/255.f)*(hairA/255.f)+((255.f-hairA)/255.f)*(bodyA/255.f))*255.f;
  42.                         //结果值
  43.                         bData[iBPos] = (unsigned char)bodyB;
  44.                         bData[iBPos + 1] = (unsigned char)bodyG;
  45.                         bData[iBPos + 2] = (unsigned char)bodyR;
  46.                         bData[iBPos + 3] = (unsigned char)bodyA;
  47.                 }
  48.         }

  49.         CCTexture2D* texture = new CCTexture2D;
  50.         texture->initWithImage(body);
  51.         CCSprite* bSprite = CCSprite::spriteWithTexture(texture);
  52.         bSprite->setPosition(ccp(240,150));
  53.         addChild(bSprite, 1);
  54.         body->release();
  55.         cloth->release();
  56.         hair->release();
  57.         texture->release();
  其实就一个公式:
       目标值 = 上图alpha值*上图值 + (1-上图alpha值)*下图值       alpha = [0, 1];
但是不建议在批量处理图片混合时使用,非常吃内存,手机撑不住 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lin&Yi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值