AGG第四十二课 Blitting an image over another with transparency

简介:

问题:

I've managed to blit a loaded image onto another through the method 

"copy_from(...)" of renderer_base.

I'd like to know how can i blit the same image and also specifying a color 

that will NOT overwrite the pixels of the destination image ( a color that 

will then result transparent ).

I complete my question by specifying that the source image is a BMP and 

therefore doesn't support transparency and alpha channel in the pixel 

values.

Any suggestion is appreciated

回答:

Yes, your simple question isn't actually that simple. I could add this 

functionality to copy_from, but I don't know what exactly to add. Do you 

need to make transparent only one color? Or a number of colors? Or maybe it 

should be some volume in RGB with transparency determined by the distance 

from a certain point. OK, suppose you need only one color, bu someone else 

may want to make a number of colors translucent. And so on. We need a kind 

of a general solution.


For now you can try to use a method similar to the one in 

examples/image_alpha.cpp

See class span_conv_brightness_alpha_rgb8 and modify it as you want.

But it's a transformer, so that, works slower than copy_from. Besides, with 

any kind of a filter (bilinear and such) the colors are not exact, so, it's 

probably makes sense to use span_image_filter_rgb_nn instead of 

span_image_filter_rgb_bilinear.


Maybe it makes sense to add some functor to copy_from(), but I have no idea 

how to do that in a general way and without losing performance. Perhaps 

there should be another function more like blend_from(). Any ideas?

答复:

Actually I've gone for a "specialized" solution, that just solves my case. 

In detail, I created a new renderer that's only suitable to copy bitmaps one 

over another with 1 transparent color.

I only have ported the "copy_from" method from renderer_base along with all 

the clipping helper methods and obejcts of your original rendere_base.

I added a method that sets a trasparency color:


    setTransaprentColor(color_type color)

    {

        ...

    }


In the "copy_from" method, I get the color of the source bitmap with the 

"pixel(x,y)" method, check it against "transparentColor" and, if equal, I 

simply skip the point with a "continue" statement.

Else, I call "copy_pixel" on the underlying rendering_buffer with the exact 

value of the source bitmap pixel.


It's evident that it can be easily extended to a generic "array" of 

transparent colors, with the cost of cycling through all of them to see if 

one of them matches the source color. Even better, one could even imagine to 

"map" some source color with some other colors (along with its own alpha 

channel) once the source RGB values are matched against the RGB values of 

the "transformer" object. I thought about it and it is not difficult to code 

such a transformer object and pass it to a renderer. But that would be too 

much for my special case and also kind of "expensive" in terms of 

performance, so I just left it "for future expansions" ;-)


     本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1969721,如需转载请自行联系原作者

相关文章
|
4月前
|
搜索推荐 Docker 容器
生信分析代码之前还好好的,怎么就报错了 Error in Ops. data. frame(guide_loc, panel_loc) :'==' only defined for equally-sized data frames
执行 `DimPlot` 函数时遇到错误 `;Error in Ops. data. frame(g guides_loc, panel_loc) : '==' only defined for equally-sized data frames`。解决方案和办法
1245 0
生信分析代码之前还好好的,怎么就报错了 Error in Ops. data. frame(guide_loc, panel_loc) :'==' only defined for equally-sized data frames
|
6月前
|
关系型数据库 MySQL
【随手记】MySQL中ROW_NUMBER()、RANK()和DENSE_RANK()函数的用法
【随手记】MySQL中ROW_NUMBER()、RANK()和DENSE_RANK()函数的用法
203 1
|
7月前
|
计算机视觉
halcon系列基础之Scale_image_range
halcon系列基础之Scale_image_range
358 0
|
7月前
|
Python
GEE—关于RSEI生态遥感指数中出现的问题 Layer error: Image.rename: The number of names (1) must match the number of..
GEE—关于RSEI生态遥感指数中出现的问题 Layer error: Image.rename: The number of names (1) must match the number of..
219 0
UVa837 - Light and Transparencies(排序)
UVa837 - Light and Transparencies(排序)
55 0
|
机器学习/深度学习 编解码 机器人
Paper:《First Order Motion Model for Image Animation》翻译与解读
Paper:《First Order Motion Model for Image Animation》翻译与解读
Paper:《First Order Motion Model for Image Animation》翻译与解读
halcon模板匹配实践(2)算子find_shape_model里的参数Row, Column, Angle含义是什么?
halcon模板匹配实践(2)算子find_shape_model里的参数Row, Column, Angle含义是什么?
796 0
halcon模板匹配实践(2)算子find_shape_model里的参数Row, Column, Angle含义是什么?
|
算法
PAT (Advanced Level) Practice - 1033 To Fill or Not to Fill(25 分)
PAT (Advanced Level) Practice - 1033 To Fill or Not to Fill(25 分)
99 0
|
机器学习/深度学习 数据可视化 机器人
Paper:《First Order Motion Model for Image Animation》翻译与解读(一)
Paper:《First Order Motion Model for Image Animation》翻译与解读
|
编解码 机器人 测试技术
Paper:《First Order Motion Model for Image Animation》翻译与解读(二)
Paper:《First Order Motion Model for Image Animation》翻译与解读