odin-blend2d

Odin bindings to Blend2D
Log | Files | Refs | README | LICENSE

bl_sample_capi.c (2130B)


      1 #include <blend2d.h>
      2 
      3 int main(void) {
      4   BLResult r;
      5   BLImageCore img;
      6   BLContextCore ctx;
      7 
      8   r = bl_image_init_as(&img, 480, 480, BL_FORMAT_PRGB32);
      9   if (r != BL_SUCCESS)
     10     return 1;
     11 
     12   r = bl_context_init_as(&ctx, &img, NULL);
     13   if (r != BL_SUCCESS) {
     14     // Image has been already created, so destroy it.
     15     bl_image_destroy(&img);
     16     return 1;
     17   }
     18 
     19   bl_context_clear_all(&ctx);
     20 
     21   // First shape filled with a radial gradient.
     22   // By default, SRC_OVER composition is used.
     23   BLGradientCore radial;
     24   BLRadialGradientValues radial_values = {
     25     180, 180, 180, 180, 180, 0
     26   };
     27 
     28   bl_gradient_init_as(&radial,
     29     BL_GRADIENT_TYPE_RADIAL, &radial_values,
     30     BL_EXTEND_MODE_PAD, NULL, 0, NULL);
     31   bl_gradient_add_stop_rgba32(&radial, 0.0, 0xFFFFFFFFu);
     32   bl_gradient_add_stop_rgba32(&radial, 1.0, 0xFFFF6F3Fu);
     33 
     34   BLCircle circle = {180, 180, 160};
     35   bl_context_fill_geometry_ext(&ctx,
     36     BL_GEOMETRY_TYPE_CIRCLE, &circle, &radial);
     37 
     38   // Unused styles must be destroyed.
     39   bl_gradient_destroy(&radial);
     40 
     41   // Second shape filled with a linear gradient.
     42   BLGradientCore linear;
     43   BLLinearGradientValues linear_values = {
     44     195, 195, 470, 470
     45   };
     46 
     47   bl_gradient_init_as(&linear,
     48     BL_GRADIENT_TYPE_LINEAR, &linear_values,
     49     BL_EXTEND_MODE_PAD, NULL, 0, NULL);
     50   bl_gradient_add_stop_rgba32(&linear, 0.0, 0xFFFFFFFFu);
     51   bl_gradient_add_stop_rgba32(&linear, 1.0, 0xFF3F9FFFu);
     52 
     53   // Use 'bl_context_set_comp_op()' to change a composition operator.
     54   bl_context_set_comp_op(&ctx, BL_COMP_OP_DIFFERENCE);
     55 
     56   BLRoundRect round_rect = { 195, 195, 270, 270, 25, 25 };
     57   bl_context_fill_geometry_ext(&ctx,
     58     BL_GEOMETRY_TYPE_ROUND_RECT, &round_rect, &linear);
     59 
     60   // Unused styles must be destroyed.
     61   bl_gradient_destroy(&linear);
     62 
     63   // Finalize the rendering and destroy the rendering context.
     64   bl_context_destroy(&ctx);
     65 
     66   // An example of querying a codec from Blend2D internal codecs.
     67   BLImageCodecCore codec;
     68   bl_image_codec_init_by_name(&codec, "PNG", SIZE_MAX, NULL);
     69   bl_image_write_to_file(&img, "bl_sample_capi.png", &codec);
     70   bl_image_codec_destroy(&codec);
     71 
     72   bl_image_destroy(&img);
     73   return 0;
     74 }