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

来源:互联网 发布:自学javascript要多久 编辑:程序博客网 时间:2024/05/16 07:00
 代码:
  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];
但是不建议在批量处理图片混合时使用,非常吃内存,手机撑不住 
0 0
原创粉丝点击