【问题标题】:How to combine 2 bitmaps via color mask?如何通过颜色蒙版组合 2 个位图?
【发布时间】:2015-02-13 07:38:05
【问题描述】:

我想使用 3 个位图来创建 1 个结果位图。首先是背景。第二个应在蒙版的帮助下绘制在第一个之上。
图像被加载到Bitmap/BitmapData 对象中。

示例:
(红绿图像为蒙版,红色为可见部分)

  back      mask    source    result

那我该怎么做呢?我在 ActionScript-3 中使用什么绘图函数?

感谢您的帮助!

编辑:
(我的第一个工作解决方案,仅适用于纯红色、绿色或蓝色)

var back: BitmapData = // load the back image
var mask: BitmapData = // load the mask image
var source:BitmapData = // load the source image (32-bit)
var result:BitmapData = back.clone();

// clone source because it will be modified in next step
var source2: BitmapData = source.clone();

// red of the mask becomes the alpha channel of source2
source2.copyChannel(mask, new Rectangle(0, 0, mask.width, mask.height), new Point(0, 0), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);

// draw source2 to result           
result.draw(source2);

(有没有更有效的方法?在这个解决方案中,我必须克隆源以保持原始源位图完好无损。)

【问题讨论】:

  • 您总是必须克隆源代码,因为它是一个源代码。另一种方法是使用带有蒙版的影片剪辑。

标签: actionscript-3 flash bitmap mask bitmapdata


【解决方案1】:

我不知道这是否是最好的方法(性能和逻辑),但你可以尝试:

var background:BitmapData = new Background();
var bmd1:BitmapData = new Mask();
var bmd2:BitmapData = new SourceImage();

var bmDiff:Bitmap = new Bitmap(mergeMaskAndBackground(bmd2, background, bmd1));
addChild(bmDiff);

function mergeMaskAndBackground(src:BitmapData, background:BitmapData, msk:BitmapData):BitmapData
{
     var diffBmpData:BitmapData = BitmapData(src.compare(msk)); 
     diffBmpData.floodFill(0, 0, BitmapDataChannel.RED);

     diffBmpData.draw(background, new Matrix(), new ColorTransform(), BlendMode.OVERLAY, background.rect, true);

     return diffBmpData;
}

【讨论】:

    猜你喜欢
    • 2013-04-15
    • 2023-02-18
    • 2019-12-14
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2020-04-12
    相关资源
    最近更新 更多