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" ;-)

时间: 2024-10-05 00:31:38

AGG第四十二课 Blitting an image over another with transparency的相关文章

NeHe OpenGL教程 第四十二课:多重视口

转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线教程的编写,以及yarn的翻译整理表示感谢. NeHe OpenGL第四十二课:多重视口 多重视口 画中画效果,很酷吧.使用视口它变得很简单,但渲染四次可会大大降低你的显示速度哦:) 欢迎来到充满趣味的另一课.这次我将向你展示怎样在单个窗口内显示多个视口.这些视口在窗口模式下能正确的调整大小.其中有

JAVA学习第四十二课 — 泛型(二)—泛型接口&&通配符应用

一.泛型接口 interface Inter<T>{ public void show(T t); } class InterImple implements Inter<String>{//知道是字符串类型 public void show(String str){ System.out.println("show "+str); } } class InterImple_2<Q> implements Inter<Q>{//不知道是什

AGG第三十二课 renderer_outline_aa更快的渲染线段算法

留给:曾经在校园奔跑时候,摔破膝盖,擦伤手掌的孩子! 1 前言 本章提供了采用新的线段渲染算法的例子,相比于已经实现的stroke管道算法,平均提高了2倍的速度,最好的情况下是2.6倍加速度.当然这种算法应用在细线上速度才会快,最好是小于2个像素. 在其他的章节中会跟conv_stroke进行效率的对比.同样的避免不了引入更多的限制,这一点可以在下面了解到.多说一句:conv_stroke是最昂贵的转换器. The rasterizer itself works much faster, bes

第四十二课:基于CSS的动画引擎

由于低版本浏览器不支持css3 animation,因此我们需要根据浏览器来选择不同的动画引擎.如果浏览器支持css3 animation,那么就使用此动画引擎,如果不支持,就使用javascript的动画引擎. 首先,我们看一下判定条件,方便切换.前面说过,浏览器把所有事件类型的构造器放在window上,只不过不可遍历.我们用Object.getOwnPropertyNames(window),可以得到window对象中的所有属性,然后再filter一下,就能得到所有的事件构造器了.最后,我们

第四十二课 KMP算法的应用

思考: replace图解: 程序完善: DTString.h: 1 #ifndef DTSTRING_H 2 #define DTSTRING_H 3 4 #include "Object.h" 5 6 namespace DTLib 7 { 8 9 class String : Object 10 { 11 protected: 12 char* m_str; 13 int m_length; 14 15 void init(const char* s); 16 bool equa

【C++探索之旅】第一部分第十二课:指针一出,谁与争锋

内容简介 1.第一部分第十二课:指针一出,谁与争锋 2.第一部分第十三课预告:第一部分小测验 指针一出,谁与争锋 上一课<[C++探索之旅]第一部分第十一课:小练习,猜单词>中,我们用一个小游戏来总结了之前几课学习的知识点. 现在,终于来到第一部分的最后一个知识点了,也是C++的基础部分的最后一个讲题.之后进入第二部分,就会开始面向对象之旅.因此,这一课也注定不平凡.系好安全带吧,因为马力要加足了! 指针这个C系语言的难点(著名的C语言里也有指针),令无数英雄"尽折腰",也

程序员的奋斗史(四十二)——大学断代史(六)——我与图书馆

文/温国兵 作为一个爱读书之人,图书馆简直是人间天堂.反之,不过地狱. 读书的好处在于,可以穿越古今中外,超越时间和空间的界限,到达你想到达的地方.你可以回到唐朝和诗仙酌酒言欢,可以回到战国和庄子高谈庄周梦蝶.鲲鹏之硕,可以回到18世纪的法国聆听哲人卢梭的教导,可以回到19世纪的德国瞻仰尼采的智慧,可以回到20世纪的中国感受王小波的特立独行,可以回到春秋时期领略老子的道,可以回到20世纪感受徐志摩的唯美诗歌--书中自有黄金屋,书中自有颜如玉,从书中可以获取到广阔的精神食粮,指引着我们前进,教导我

第四十二章

第四十二章1 道生“肾” 道生一,一生二,二生三,三生万物. 道生出混沌之气,混沌之气分出阴阳,阴阳又交汇出新的物质,从而生出万物. 道在我们身体内体现为“肾精”,要保护好.各位朋友大家好,今天我们接着来聊<道德经>.我们来看看老子带给我们什么样新的人生启发了,每天启发一点,天天进步.今天我们开始学习第四十二章,非常开心.因为我们<道德经>已经学习到一半的位置了,因为整个八十一章,我们学习到第四十二章了,过了一半了,这时间也是飞快的.我之前预计3年,我估计现在2年差不多讲完了.因为

Kali Linux Web 渗透测试— 第十二课-websploit

Kali Linux Web 渗透测试— 第十二课-websploit 文/玄魂 目录 Kali Linux Web 渗透测试— 第十二课-websploit............................................... 1 Websploit 简介........................................................................................... 2 主要功能...........