图片的alpha融合算法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xiuzhentianting/article/details/48442733
      if LSrcAlpha > 0 then
      begin
        if LSrcAlpha = 255 then
        begin
          LAlphaDest[X] := LSrcAlpha;
          Move(LImageSrc[L3SrcIndex], LImageDest[L3DestIndex], SizeOf(TRGBTriple));
        end else
        begin
          LDestAlpha := LAlphaDest[X];
          if LDestAlpha = 0 then
          begin
            LAlphaDest[X] := LSrcAlpha;
            Move(LImageSrc[L3SrcIndex], LImageDest[L3DestIndex], SizeOf(TRGBTriple));
          end else if LDestAlpha = 255 then
          begin
            Integer(LPSrcRGB) := Integer(LImageSrc) + L3SrcIndex; 
            Integer(LPDestRGB) := Integer(LImageDest) + L3DestIndex;
            
            LPDestRGB^.rgbtRed := (255 + LPSrcRGB^.rgbtRed * LSrcAlpha + LPDestRGB^.rgbtRed * (not LSrcAlpha)) shr 8;
            LPDestRGB^.rgbtGreen := (255 + LPSrcRGB^.rgbtGreen * LSrcAlpha + LPDestRGB^.rgbtGreen * (not LSrcAlpha)) shr 8;
            LPDestRGB^.rgbtBlue := (255 + LPSrcRGB^.rgbtBlue * LSrcAlpha + LPDestRGB^.rgbtBlue * (not LSrcAlpha)) shr 8;
          end else
          begin
            // Alpha合成 255*(LDestAlpha + LSrcAlpha) - LDestAlpha*LSrcAlpha = LDestAlpha*(255 - LSrcAlpha) + 255 * LSrcAlpha;
            // 255 - a = not a
            LTmpAlphaD := LDestAlpha * (not LSrcAlpha);
            LTmpAlphaS := 255 * LSrcAlpha;
            LMergedAlpha := LTmpAlphaD + LTmpAlphaS;

            Integer(LPSrcRGB) := Integer(LImageSrc) + L3SrcIndex;
            Integer(LPDestRGB) := Integer(LImageDest) + L3DestIndex;
            
            LPDestRGB^.rgbtRed := (LPDestRGB^.rgbtRed * LTmpAlphaD + LPSrcRGB^.rgbtRed * LTmpAlphaS) div LMergedAlpha;
            LPDestRGB^.rgbtGreen := (LPDestRGB^.rgbtGreen * LTmpAlphaD + LPSrcRGB^.rgbtGreen * LTmpAlphaS) div LMergedAlpha;
            LPDestRGB^.rgbtBlue := (LPDestRGB^.rgbtBlue * LTmpAlphaD + LPSrcRGB^.rgbtBlue * LTmpAlphaS) div LMergedAlpha;
          end;
        end;

展开阅读全文

没有更多推荐了,返回首页