Blur a Bitmap [A5]
hdlopesrocha

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

Dustin Dettmer

Sum to area.

jmasterx

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

gnolam

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?

hdlopesrocha

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

Arthur Kalliokoski

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.

Elias

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.

Næssén

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.

Trent Gamblin

Or do a poor man's blur.

bamccaig

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

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

gnolam

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

Elias

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.

kazzmir

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

someone972

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.

Trent Gamblin

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

Thread #608115. Printed from Allegro.cc