Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.57 KB | None | 0 0
  1.  
  2.  
  3. int IsColonne(SDL_Surface *image,int x)                                        
  4. {                                                                              
  5.   Uint8 r;                                                                      
  6.   Uint8 g;                                                                      
  7.   Uint8 b;                                                                      
  8.   int res = 1;                                                                  
  9.   for(int y = 0; y<image->h-1;++y)                                              
  10.   {                                                                            
  11.     Uint32 p = (getpixel(image,(unsigned)x,(unsigned)y));                      
  12.     SDL_GetRGB(p,image->format,&r,&g,&b);                                      
  13.                                                                                
  14.     if( g == 0)                                                                
  15.     {                                                                          
  16.       res = 0;                                                                  
  17.     }                                                                          
  18.   }                                                                            
  19.   return res;                                                                  
  20. }                                                                              
  21. //Test si la ligne  est totalement blanche                                      
  22. int IsLine(SDL_Surface *image, int y)                                          
  23. { Uint8 r;                                                                      
  24.   Uint8 g;                                                                      
  25.   Uint8 b;                                                                      
  26.   int res = 1;                                                                  
  27.   for(int x = 0; x<image->w;++x)                                                
  28.   {                                                                            
  29.     Uint32 p = (getpixel(image,(unsigned)x,(unsigned)y));                      
  30.     SDL_GetRGB(p,image->format,&r,&g,&b);                                      
  31.                                                                                
  32.     if(b == 0)                                                                  
  33.     {                                                                          
  34.      res = 0;                                                                  
  35.                                                                                
  36.     }                                                                          
  37.   }                                                                            
  38.   return res;                                                                  
  39. }  
  40.  
  41. // Dessine une ligne autour des blocs (Pour mieux visualiser)                  
  42. void LineMarker(SDL_Surface *image,int y)                                      
  43. {                                                                              
  44.   Uint8 r;                                                                      
  45.   Uint8 g;                                                                      
  46.   Uint8 b;                                                                      
  47.   for( int x = 0 ; x <image->w; ++x)                                            
  48.   {                                                                            
  49.     r = 255;                                                                    
  50.     g=1;                                                                        
  51.     b=1;                                                                        
  52.     putpixel(image,(unsigned)x,(unsigned)y,SDL_MapRGB(image->format,r,g,b));    
  53.                                                                                
  54.   }                                                                            
  55. }                                                                              
  56. //Un truc qui marque les colonnes...En rouge, parce que c'est moche le rouge.  
  57. void ColumnMarker(SDL_Surface *image,int x)                                    
  58. {                                                                              
  59.   Uint8 r;                                                                      
  60.   Uint8 g;                                                                      
  61.   Uint8 b;                                                                      
  62.   for( int y = 0 ; y < image->h; ++y)                                          
  63.   {                                                                            
  64.     r = 255;                                                                    
  65.     g=1;                                                                        
  66.     b=1;                                                                        
  67.     putpixel(image,(unsigned)x,(unsigned)y,SDL_MapRGB(image->format,r,g,b));    
  68.   }                                                                            
  69.                                                                                
  70. }  
  71.  
  72.  
  73.  
  74. //Thx Vicky again ?!                                                            
  75. void OnceAgainLine(SDL_Surface *image)                                          
  76. {                                                                              
  77.     for(int y = 0;y<image->h; ++y)                                              
  78.     {                                                                          
  79.                                                                                
  80.       if(IsLine(image,y) == 0)                                                  
  81.       {                                                                        
  82.         LineMarker(image,y);                                                    
  83.         y = image->h -1;                                                        
  84.       }                                                                        
  85.     }                                                                          
  86.     for(int y  = image->h -1; y>0;--y)                                          
  87.     {                                                                          
  88.       if(IsLine(image,y) == 0)                                                  
  89.       {                                                                        
  90.         LineMarker(image,y);                                                    
  91.         y = 0;                                                                  
  92.       }                                                                        
  93.     }                                                                          
  94. }                                                                              
  95. void OnceAgainColumn(SDL_Surface *image)                                        
  96. {                                                                              
  97.   for(int x = 0; x<image->w;++x)                                                
  98.   {                                                                            
  99.     if(IsColonne(image,x) == 0)                                                
  100.     {                                                                          
  101.       ColumnMarker(image,x);                                                    
  102.       x = image->w-1;                                                          
  103.     }                                                                          
  104.                                                                                
  105.   }                                                                            
  106.   for(int x = image->w -1; x>0; --x)                                            
  107.   {                                                                            
  108.       if(IsColonne(image,x) == 0)                                              
  109.       {                                                                        
  110.         ColumnMarker(image,x);                                                  
  111.         x = 0;                                                                  
  112.       }                                                                        
  113.   }                                                                            
  114. }  
  115.  
  116.  
  117.  
  118. //Fonction qui-fait-tout                                                        
  119. int BW(SDL_Surface *image)                                                      
  120. {                                                                              
  121.   Uint8 r;                                                                      
  122.   Uint8 g;                                                                      
  123.   Uint8 b;                                                                      
  124.   for(int x = 0; x < image->w;x++)                                              
  125.   {                                                                            
  126.     for(int y = 0; y < image->h;y++)                                            
  127.     {                                                                          
  128.       Uint32 p = getpixel(image,(unsigned)x,(unsigned)y);                      
  129.       SDL_GetRGB(p,image->format,&r,&g,&b);                                    
  130.       r= g = b =  (float)r * 0.3 + (float)g * 0.59 + (float)b * 0.11;          
  131.       int lim = 255/2;                                                          
  132.       if(r>lim) { r = g = b =  255; }                                          
  133.       else{ r = g = b = 0;}                                                    
  134.       putpixel(image,(unsigned)x,(unsigned)y,SDL_MapRGB(image->format,r,g,b));  
  135.     }                                                                          
  136.                                                                                
  137.   }                                                                            
  138.   OnceAgainLine(image);                                                        
  139.   OnceAgainColumn(image);                                                      
  140.   image = display_image(image);                                                
  141.   SDL_FreeSurface(image);                                                      
  142.   return 0;                                                                    
  143. }                                                                              
  144. //Vraiment besoin d'un commentaire pour le main ?                              
  145. int main(int argc,char *argv[])                                                
  146. {                                                                              
  147.   if(argc>1){                                                                  
  148.     init_sdl();                                                                
  149.     SDL_Surface* image = load_image(argv[1]);                                  
  150.     image = display_image(image);                                              
  151.     SDL_FreeSurface(image);                                                    
  152.     image = load_image(argv[1]);                                                
  153.     BW(image);                                                                  
  154.   }                                                                            
  155.   return 0;                                                                    
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement