IdeaMonk

thoughts, ideas, code and other things...

Saturday, August 16, 2008

Paint Daubs effect through Allegro & C++

Went to automate some effect like this in allegro today, ended up unfinished with the paint daubs effect over bitmaps. It looks something like this -> ;)
What it basically does is draws filled circles by picking colours from original bitmap onto a copy of it. So, when many circles overlap each other they give a painting style effect. As if the image was made with short impressions of brush over and over again.
  1. /* 
  2. Paint Daubs v.1             16 Aug 2008 
  3. An attempt at making image look like vintage art paintings. 
  4.  
  5. This basic version takes a base image, 
  6. randomly creates filled circles on target image to get such effect. 
  7.  
  8. Usage 
  9. ------ 
  10. $> paint_daubs <base.bmp> <tagert.bmp> [%blob_max] [depth] [s/c] 
  11.  
  12. %blob_max is the % of size of image that a blob will size up to 
  13. depth is number of times the blobs are drawn 
  14. s/c - s for square fills, c for circles 
  15.  
  16.              -- Abhishek Mishra 
  17.               ideamonk.blogspot.com 
  18. */  
  19.   
  20. #include <iostream>  
  21. #include <allegro.h>  
  22. using namespace std;  
  23.   
  24. //Config parameters  
  25. float BASEBLOB_MAX = .02f;  
  26. float BASE_COUNT = 200.0f;  
  27.   
  28.   
  29. BITMAP  *base, *target;  
  30. PALETTE base_pal, tar_pal;  
  31.   
  32. void show_menu(char *fname){  
  33. cout << "Usage\n"  
  34. <<  "------\n"  
  35. <<  fname << " <base.bmp> <tagert.bmp> [%%blob_max] [depth] [s/c]\n";  
  36. }  
  37.   
  38. bool init_bitmaps(char *fbase, char *ftarget){  
  39. cout << "\n[+] Loading bitmaps...\n";  
  40.   
  41. // Load all three bitmaps  
  42. base = load_bitmap(fbase,base_pal);  
  43. if (!base) {  
  44. cout << " [-] Couldn't load "<<fbase <<"\n";  
  45. return false;  
  46. }  
  47.   
  48. target = load_bitmap (fbase,tar_pal);  
  49. if (!target) {  
  50. cout << " [-] Couldn't create bitmap for target\n";  
  51. return false;  
  52. }  
  53.   
  54. return true;  
  55. }  
  56.   
  57. void process_base(char *s){  
  58. int min_dim = (base->w < base->h) ? base->w : base->h;  
  59. int bmax = (int) (BASEBLOB_MAX * min_dim);  
  60. int bmin =  2;  
  61. int times = (int) (BASE_COUNT * min_dim);  
  62. int width,color,px,py;  
  63.   
  64. for (int i=0;i<times;){  
  65. width = bmin + rand() % (bmax-bmin);  
  66. px = rand() % base->w;  
  67. py = rand() % base->h;  
  68. color = getpixel(base,px,py);  
  69.   
  70. if (color != makecol (255,0,255) ){  
  71. // Select fill type  
  72. if (*s=='s'){  
  73.  rectfill (target,px-width/2,py-width/2,px+width/2,py+width/2, color);  
  74. else {  
  75.  circlefill(target,px,py,width/2,color);  
  76. }  
  77. i++;  
  78. }  
  79. }  
  80. }  
  81.   
  82. int main (int argc, char *argv[]){  
  83. if (argc < 3){  
  84. show_menu(argv[0]);  
  85. else {  
  86. //Allegro initialization routines  
  87. set_color_depth(32);  
  88. set_color_conversion(COLORCONV_EXPAND_HI_TO_TRUE);  
  89. allegro_init();  
  90.   
  91. // Load the bitmaps  
  92. if (!init_bitmaps(argv[1],argv[2]))  
  93. return 0;  
  94.   
  95. if (argc > 3)  
  96. BASEBLOB_MAX = atof(argv[3]);  
  97. if (argc > 4)  
  98. BASE_COUNT = atof(argv[4]);  
  99.   
  100. cout << "[+] drwaing blobs\n";  
  101. process_base(argv[5]);  
  102.   
  103. cout << "[+] saving "<< argv[2]<<"\n";  
  104. save_bitmap (argv[2],target,tar_pal);  
  105. //Debug  
  106. /* 
  107. set_gfx_mode(GFX_AUTODETECT_WINDOWED,1024,768,0,0); 
  108. blit (target,screen,0,0,0,0,800,600); 
  109. getchar(); 
  110. */  
  111. cout << "[+] done\n";  
  112.   
  113. }  
  114. return 0;  
  115. }  
  116. END_OF_MAIN();  

More samples (click to enlarge) =>
  • with square filter ON (s)
  • with default circlefill option and depth set to 400
To compile the source you will need Allegro working with your development environment. I use DevCPP with Allegro.devpak found at http://devpaks.org/
http://ideamonk.googlepages.com/ddl.gif
For linux & OSX, try installing Allegro and then compilling over g++ with -lalleg option.

Labels: , , ,

2 Comments:

At August 16, 2008 at 9:53 PM , Blogger Krish said...

nice blog! keep up the good work..

i remember bumping onto ur blog while searching for syntaxhighlighter. it was quite helpful.

hv given ref to ur blog...
http://krishlogs.blogspot.com/2008/08/proud-to-be-indian.html

 
At August 16, 2008 at 11:41 PM , Blogger Krish said...

i messed up the link.. my bad..

its:
http://krishcodes.blogspot.com/2008/07/formatting-code-snippets-in-blogpost.html

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home