|
Blur a Bitmap [A5] |
Henrique Rocha
Member #13,042
July 2011
|
Hello again my friends! I am trying to blur a bitmap, but it's too slow. 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?
|
ImLeftFooted
Member #3,935
October 2003
|
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. Agui GUI API -> https://github.com/jmasterx/Agui |
gnolam
Member #2,030
March 2002
|
If you want to do it in real-time, I suggest you use a shader. Also, use more descriptive variable names. -- |
Henrique Rocha
Member #13,042
July 2011
|
l - line. How do I use shaders? Thanks
|
Arthur Kalliokoski
Second in Command
February 2005
|
Now you say: l - line. But in 6 months you'll say l? c? vl? vc? p? Not to mention a global search/replace would be extremely painful. They all watch too much MSNBC... they get ideas. |
Elias
Member #358
May 2000
|
hdlopesrocha said: 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
Member #5,025
September 2004
|
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
Member #261
April 2000
|
Or do a poor man's blur.
|
bamccaig
Member #7,536
July 2006
|
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
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
ImLeftFooted
Member #3,935
October 2003
|
There's gotta be an OpenGL extension or something for blur -- isn't there? |
gnolam
Member #2,030
March 2002
|
It's pretty much the ideal use case for a fragment shader. -- |
Elias
Member #358
May 2000
|
Dustin Dettmer said: 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
Member #1,786
December 2001
|
Person reading bamccaig's code: Whats this variable, gt? |
someone972
Member #7,719
August 2006
|
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
Member #261
April 2000
|
Cg shaders are already supported by the shader add-on in 5.1.
|
|