Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Blur a Bitmap [A5]

This thread is locked; no one can reply to it. rss feed Print
Blur a Bitmap [A5]
hdlopesrocha
Member #13,042
July 2011
avatar

Hello again my friends!

I am trying to blur a bitmap, but it's too slow.

#SelectExpand
1void blur_bitmap(ALLEGRO_BITMAP * bmp, int force){ 2 ALLEGRO_BITMAP * temp_bmp = al_create_bitmap(al_get_bitmap_width(bmp), al_get_bitmap_height(bmp)); 3 int l,c; 4 int vl, vc; 5 int p; /* number of neighbor pixels */ 6 int sr, sg, sb,sa; /* sum of each color */ 7 ALLEGRO_COLOR pixel; 8 9 al_lock_bitmap(bmp, ALLEGRO_LOCK_READONLY, al_get_bitmap_format(bmp) ); 10 11 al_set_target_bitmap(temp_bmp); 12 13 for(l=0; l<al_get_bitmap_height(bmp); ++l){ 14 for(c=0; c<al_get_bitmap_width(bmp); ++c){ 15 sr=sg=sb=sa=p=0; 16 for(vl=l-force; vl<l+force; ++vl){ 17 for(vc=c-force; vc<c+force; ++vc){ 18 if(vc>= 0 && vc < al_get_bitmap_width(bmp) && vl>= 0 && vl < al_get_bitmap_height(bmp)){ 19 ++p; 20 pixel = al_get_pixel(bmp,vc,vl); 21 sr += (pixel.r)*255; 22 sg += (pixel.g)*255; 23 sb += (pixel.b)*255; 24 sa += (pixel.a)*255; 25 } 26 } 27 } 28 al_put_pixel(c,l, al_map_rgba(sr/p, sg/p, sb/p,sa/p)); 29 } 30 } 31 al_unlock_bitmap(bmp); 32 33 al_set_target_bitmap(bmp); 34 al_draw_bitmap(temp_bmp,0,0,0); 35 al_destroy_bitmap(temp_bmp); 36}

How can I improve the speed?
Thanks for helping ;)

ERA of Empires - Allegro 5
http://www.youtube.com/user/hdlopesrocha

Dustin Dettmer
Member #3,935
October 2003
avatar

Sum to area.

jmasterx
Member #11,410
October 2009

Since you are using A5, you might want to look into shaders. They will blur much faster.

gnolam
Member #2,030
March 2002
avatar

If you want to do it in real-time, I suggest you use a shader.

Also, use more descriptive variable names. :P
l? c? vl? vc? p?

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

hdlopesrocha
Member #13,042
July 2011
avatar

l - line.
c - column.
vc, cl - variables to calculate average color of an area.
p - number of valid pixels inside that area (good for corners)

How do I use shaders?

Thanks ;)

ERA of Empires - Allegro 5
http://www.youtube.com/user/hdlopesrocha

Arthur Kalliokoski
Second in Command
February 2005
avatar

Now you say:

l - line.
c - column.
vc, cl - variables to calculate average color of an area.
p - number of valid pixels inside that area (good for corners)

But in 6 months you'll say

l? c? vl? vc? p?

Not to mention a global search/replace would be extremely painful.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Elias
Member #358
May 2000

How do I use shaders?

Easiest way would be to set the ALLEGRO_OPENGL display flag - then just use the OpenGL API instead of the Allegro API for drawing (i.e. glDrawArrays instead of al_draw_bitmap). The OpenGL API also contains the functions to compile and set shaders.

The SVN version of Allegro also has a few examples showing how to mix shaders with the Allegro API if you prefer that (call al_draw_bitmap but have it use a shader) - look at e.g. ex_shader or ex_palette. This is very much in-development still though so only use if you're ready to report bugs.

--
"Either help out or stop whining" - Evert

Næssén
Member #5,025
September 2004
avatar

If you for some reason don't want use shaders you can always do a box blur separating the horizontal and vertical computations. Do a box blur three times if you want a Gaussian blur.

_____________________
darkbits.org | Google+

Trent Gamblin
Member #261
April 2000
avatar

Or do a poor man's blur.

bamccaig
Member #7,536
July 2006
avatar

Descriptive variable names sound good in theory, but in practice I find that it often hinders readability[1]. What I prefer to do is write short subroutines with variable names between 1 and 3 characters in length. I'll generally use mnemonic devices to make them somewhat sensible. They're usually self documenting by type or assignment. If not, I'll add a comment to document them. I only use descriptive names when the code is long or complicated enough to warrant it. I don't think of images as "lines" and "columns" though so in this case I would probably prefer either x and y or i and j. I think that more modularization would benefit the readability of this subroutine more than descriptive variable names, though.

References

  1. This is especially true when dealing with objects and accessing properties and methods a few levels deep. Don't get me wrong though: properties and methods should always have descriptive names.
Dustin Dettmer
Member #3,935
October 2003
avatar

There's gotta be an OpenGL extension or something for blur -- isn't there?

gnolam
Member #2,030
March 2002
avatar

It's pretty much the ideal use case for a fragment shader.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Elias
Member #358
May 2000

There's gotta be an OpenGL extension or something for blur

Fragment shaders. Those are ancient, they were available as extension even before OpenGL 2.0. Therefore a special extension introduced after that wouldn't make too much sense given how easy it is to do already.

--
"Either help out or stop whining" - Evert

kazzmir
Member #1,786
December 2001
avatar

Person reading bamccaig's code: Whats this variable, gt?
bamccaig: F**k you, thats what it means!

someone972
Member #7,719
August 2006
avatar

Would it be possible to use CG shaders for allegro? That way there would be a shader for both OpenGL and DirectX, with a simple API for applying them.

______________________________________
As long as it remains classified how long it took me to make I'll be deemed a computer game genius. - William Labbett
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why. -Unknown
I have recklessly set in motion a chain of events with the potential to so-drastically change the path of my life that I can only find it to be beautifully frightening.

Trent Gamblin
Member #261
April 2000
avatar

Cg shaders are already supported by the shader add-on in 5.1.

Go to: