Advertisement
GeeckoDev

camembert fixed

Sep 6th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <Imlib2.h>
  2. #include <math.h>
  3.  
  4. static int u, v;
  5.  
  6. void compute(int h)
  7. {
  8.     u = cosf(60.f * 3.14159f / 180.f) * h;
  9.     v = sinf(60.f * 3.14159f / 180.f) * h;
  10. }
  11.  
  12. int collide(int x, int y, int w, int h)
  13. {
  14.     int t = x - w/2;
  15.  
  16.     return
  17.         h - y < sqrtf(h*h - t*t)    && // circle bound
  18.         h - y >  v * t / u          && // bottom-right bound
  19.         h - y > -v * t / u           ; // bottom-left bound
  20. }
  21.  
  22. int main(int argc, char **argv)
  23. {
  24.     Imlib_Image img;
  25.     Imlib_Image cropped_img;
  26.     DATA32 *data;
  27.     int w, h;
  28.  
  29.     /* Verify the parameters */
  30.  
  31.     if (argc != 3)
  32.         return 1;
  33.  
  34.     /* Load an image and use it */
  35.  
  36.     img = imlib_load_image(argv[1]);
  37.  
  38.     if (!img)
  39.         return 2;
  40.  
  41.     imlib_context_set_image(img);
  42.  
  43.     data = imlib_image_get_data();
  44.     w = imlib_image_get_width();
  45.     h = imlib_image_get_height();
  46.    
  47.     imlib_image_set_has_alpha(1);
  48.  
  49.     /* Reject any pixel that doesn't collide with the shape */
  50.  
  51.     compute(h);
  52.  
  53.     for (int i=0; i<h; i++)
  54.     {
  55.         for (int j=0; j<w; j++)
  56.         {
  57.             if (!collide(j, i, w, h))
  58.                 *data = 0; // Fully transparent black
  59.  
  60.             data++;
  61.         }
  62.     }
  63.  
  64.     /* Crop the image if needed */
  65.  
  66.     if (2*u < w)
  67.     {
  68.         cropped_img = imlib_create_cropped_image(w/2 - u, 0, 2*u, h);
  69.         imlib_free_image_and_decache();
  70.         imlib_context_set_image(cropped_img);
  71.     }
  72.  
  73.     /* Save the image to a file */
  74.  
  75.     imlib_save_image(argv[2]);
  76.     imlib_free_image_and_decache();
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement