Results 1 to 8 of 8

Thread: [RESOLVED] Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

  1. #1

    Thread Starter
    Hyperactive Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    379

    Resolved [RESOLVED] Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

    Hi to the Cairo squad.
    Just wondering if you are experiencing any weirdness of the CAIRO_OPERATOR_CLEAR function?
    Unless I'm reading it wrong, it's supposed to do this:
    Name:  Operator Clear.jpg
Views: 379
Size:  75.2 KB

    Instead, for me, it's erasing all of Surface 1 no matter what I do.
    * Large image on Surface 1, small image on Surface 2 (with transparent areas). Erased all of Surface 1.
    * Tried making the whole of surface 1 black, then second surface with tiny image (rest of image was transparent). Erased all of Surface 1.
    * Tried same above with cairo.imagelist.item (with transparent areas). Erased all of Surface 1.

    Something's afoot!
    Anyone know what's up? I've found the other operator functions to be perfect as is.

    The link I was reading about it.
    https://www.cairographics.org/operators/

  2. #2
    PowerPoster
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    2,412

    Re: Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

    It looks like Cairo/RC6 is treating the entire surface as a block, so that's why the underlying surface is being cleared. If you use the various shape drawing methods (e.g. Ellipse) then CAIRO_OPERATOR_CLEAR appears to work as you are expecting. E.g.:

    Code:
       Dim s1 As cCairoSurface
       
       Set s1 = Cairo.CreateSurface(300, 300)
       
       With s1.CreateContext
          .Ellipse 100, 100, 200, 200
          .SetSourceColor vbWhite
          .Fill
          
          .Operator = CAIRO_OPERATOR_CLEAR
          .Ellipse 200, 200, 300, 300
          .SetSourceColor vbRed
          .Fill
       End With
    Results in a circle with a "bite" taken out of it.

    I'm not sure if this is something Olaf can change, or if it is working as designed.

  3. #3

    Thread Starter
    Hyperactive Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    379

    Re: Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

    Thanks for checking Jpbro. It's weird, as the operators generally work for surfaces (from what I've used so far), not just vector things.

  4. #4
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

    Quote Originally Posted by -Corso-> View Post
    Thanks for checking Jpbro. It's weird, as the operators generally work for surfaces (from what I've used so far), not just vector things.
    The thing with "Surfaces, when they contain a Sprite-Shape" is,
    that they do not contain any true (Vector-)Path-Information (unless the Surface was derived from an SVG).

    So, the only thing that can be done on Image(Pxl)Surfaces in that regard,
    (of gathering "a kind of Path-Info for a certain Sprite-Shape"),
    is via analyzing their underlying Pixels, looking at their Alpha-Channel...

    And the Cairo-Command which does such an Alpha-Channel-based analyzing, is:
    - CC.MaskSurface

    Below is some Demo-Code, which renders the same Sprite 3 times in different "modes":
    - normally (with full color+alpha-info, using CC.RenderSurfaceContent)
    - color-stamping (using CC.MaskSurface in conjunction with a prior set SourceColor)
    - and "full out-masking" (using CC.MaskSurface after priorily setting the Clear-Operator)

    Code:
    Option Explicit
    
    Private CC As cCairoContext, Sprite As cCairoSurface
    
    Private Sub Form_Load()
      Set CC = Cairo.CreateSurface(1024, 768).CreateContext
      Set Sprite = Cairo.ImageList.AddIconFromResourceFile("", "shell32", 167)
      Redraw
    End Sub
     
    Private Sub Redraw()
      CC.Paint 1, Cairo.CreateSolidPatternLng(vbWhite)
      
      'normal, full-color-rendering (with alpha) onto the Main-Surface
      CC.RenderSurfaceContent Sprite, 10, 10
     
      CC.SetSourceColor vbRed 'Color-Stamping a certain Sprite-Shape
      CC.MaskSurface Sprite, 80, 10 'this uses only the Alpha-Channel of the Sprite
      
      CC.Operator = CAIRO_OPERATOR_CLEAR 'complete "out-masking" via Clear-Operator
      CC.MaskSurface Sprite, 150, 10
      
      Set Picture = CC.Surface.Picture
    End Sub
    HTH

    Olaf
    Last edited by Schmidt; Jan 18th, 2022 at 03:26 AM.

  5. #5

    Thread Starter
    Hyperactive Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    379

    Re: Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

    Thank you Olaf, Masksurface, another good word to learn.
    After a lot of messing about, I manged to get a basic workaround.
    Shortening the story down. 'All' the hand drawn objects in the game-test have a necessary, dark outline for visual appeal. It's easy to make, but I needed to clip that outline out for other lighting effects. I think I'll visit masksurface function for it instead.

    Thank you!

    And with it, my auto-plant-tree-building-randomizer-hand-drawn-mimicker is fully operational!

    Name:  tenor.gif
Views: 249
Size:  39.2 KB

  6. #6
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

    Quote Originally Posted by -Corso-> View Post
    'All' the hand drawn objects in the game-test have a necessary, dark outline for visual appeal.
    It's easy to make, but I needed to clip that outline out for other lighting effects.
    I think I'll visit masksurface function for it instead.
    You can use the GaussionBlur-Method for that:

    Code:
    Option Explicit
    
    Private CC As cCairoContext, Sprite As cCairoSurface, SpHalo As cCairoSurface
    
    Private Sub Form_Load()
      Set CC = Cairo.CreateSurface(1024, 768).CreateContext
      Set Sprite = Cairo.ImageList.AddIconFromResourceFile("", "shell32", 167, 64, 64)
      Set SpHalo = CreateHaloFor(Sprite)
      
      Caption = "Sprite=" & Sprite.Width & "x" & Sprite.Height & _
                ", Halo=" & SpHalo.Width & "x" & Sprite.Height
      Redraw
    End Sub
    
    Private Sub Redraw()
      CC.Paint 1, Cairo.CreateSolidPatternLng(vbWhite)
      
      'normal, full-color- (with alpha) onto the Main-Surface
      CC.RenderSurfaceContent Sprite, 10, 10 'render the Sprite normally
     
      CC.RenderSurfaceContent SpHalo, 80, 10 'render the Halo normally
      
      CC.SetSourceColor vbBlack: CC.MaskSurface SpHalo, 150, 10 'Color-Stamping of the Halo in black
      
      'finally a combination of the Sprite (and a black Halo) at the same place
      CC.RenderSurfaceContent Sprite, 220, 10
      CC.SetSourceColor vbBlack: CC.MaskSurface SpHalo, 220, 10 '
     
      Set Picture = CC.Surface.Picture
    End Sub
    
    Function CreateHaloFor(Srf As cCairoSurface) As cCairoSurface
      Set CreateHaloFor = Cairo.CreateSurface(Srf.Width, Srf.Height)
      With CreateHaloFor.CreateContext
           Dim Blur As cCairoSurface, ROffs As Long
           Set Blur = Srf.GaussianBlur(3.3, ROffs)
           .RenderSurfaceContent Blur, -ROffs, -ROffs
           .Operator = CAIRO_OPERATOR_CLEAR
           .MaskSurface Srf
      End With
    End Function
    The above code will produce this:
    Name:  SpriteHalo.png
Views: 267
Size:  31.4 KB

    HTH

    Olaf

  7. #7

    Thread Starter
    Hyperactive Member -Corso->'s Avatar
    Join Date
    Oct 2021
    Posts
    379

    Re: [RESOLVED] Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

    Ah neat, thanks Olaf. I'll keep that one in mind for other effects later.

    By the way, I have been meaning to ask. Do we need to do this anymore?
    Private Sub Form_Terminate()
    Construction_Zone.CleanupRichClientDll 'Remove all the surfaces and items to recover memory

    I've noticed the examples lately don't have this, should we keep cleaning up as normal?
    (I have no idea, unless there is something special in the RC6 now?)

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: [RESOLVED] Cairo Image Operator: CAIRO_OPERATOR_CLEAR is it acting weirdly?

    Quote Originally Posted by -Corso-> View Post
    By the way, I have been meaning to ask. Do we need to do this anymore?
    Private Sub Form_Terminate()
    Construction_Zone.CleanupRichClientDll 'Remove all the surfaces and items to recover memory

    I've noticed the examples lately don't have this...
    The RC6 does not require these Cleanup-Calls anymore...

    Olaf

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width