Results 1 to 7 of 7

Thread: [RESOLVED] Help with Cairo Mask - Desperate for strip-down basics - Cairo programmers!

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Resolved [RESOLVED] Help with Cairo Mask - Desperate for strip-down basics - Cairo programmers!

    Without exaggeration, I have spent from 7:00am till now (8:50pm) trying to widdle down the CC.Mask (Cairo RC6) use to the very basic needed to just do something like this:

    1. On the background surface (call it srfBackground), you have an image.

    The example image found in the Cairo Tutorials #11 Masking is what I've been working with.

    Code:
      
    'same image-resource as in the clipping-example
     Cairo.ImageList.AddImage "SourceImage", App.Path & "\cubes.jpg"
    2. On top of this is the MASK surface (call it srfMask), which for simplicity is a big letter "M" in the center.


    The mask does not need to move. I just want a small example of using the CC.Mask where you can see the background cube image only through the big letter "M".

    The LEAST amount of code required to do this simple thing.

    Seriously, I've searched the whole forum with every keyword tied to 'Cairo' and 'Mask' and found every one of them using .MaskSurface and not .Mask.

    But the example in the #11 tutorial uses cc.Mask but I have not been successful in figuring out the basics because that tutorial involves a form, a mod, a class, and a user control.

    I tried creating a bare-bones cCairoPattern to feed cc.Mask, that didn't go well.

    I'm not clear on the application of CC.PushGroup and CC.PopGroup. I've read and re-read the comments but it is not clear to me. I'm GUESSING that my MASK image or pattern is to be defined within these?

    Help please!

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

    Re: Help with Cairo Mask - Desperate for strip-down basics - Cairo programmers!

    If I've understood your goal correctly, this should work:

    Code:
    Option Explicit
    
    Private Sub Form_Click()
       Cairo.ImageList.AddImage "SourceImage", App.Path & "\cubes.jpg"
    
       With Cairo.CreateSurface(Me.Width, Me.Height)
          With .CreateContext
             .SelectFont "Arial", 350, , True
    
             .DrawText 0, 0, Me.Width, Me.Height, "M", , , , , , , True ' Setting the PathOnly parameter to True is the "magic" here. We don't actually draw the text, we just create the path/shape of the text
    
             .Clip  ' Now we clip the context to the shape of the text
    
             .RenderSurfaceContent "SourceImage", 0, 0 ' And now we draw our image onto the clipped shape of the text
          End With
          
          .DrawToDC Me.hDC ' Finally we will draw the result to the form.
       End With
    End Sub
    
    Private Sub Form_Load()
       Me.ScaleMode = vbPixels 
    End Sub
    AFAICT there is no need for Masks for this task, the magic is in "drawing" the text as a path only, clipping to that path, and then rendering the surface within the clipped path.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: Help with Cairo Mask - Desperate for strip-down basics - Cairo programmers!

    Quote Originally Posted by jpbro View Post
    If I've understood your goal correctly, this should work:

    Code:
    Option Explicit
    
    Private Sub Form_Click()
       Cairo.ImageList.AddImage "SourceImage", App.Path & "\cubes.jpg"
    
       With Cairo.CreateSurface(Me.Width, Me.Height)
          With .CreateContext
             .SelectFont "Arial", 350, , True
    
             .DrawText 0, 0, Me.Width, Me.Height, "M", , , , , , , True ' Setting the PathOnly parameter to True is the "magic" here. We don't actually draw the text, we just create the path/shape of the text
    
             .Clip  ' Now we clip the context to the shape of the text
    
             .RenderSurfaceContent "SourceImage", 0, 0 ' And now we draw our image onto the clipped shape of the text
          End With
          
          .DrawToDC Me.hDC ' Finally we will draw the result to the form.
       End With
    End Sub
    
    Private Sub Form_Load()
       Me.ScaleMode = vbPixels 
    End Sub
    AFAICT there is no need for Masks for this task, the magic is in "drawing" the text as a path only, clipping to that path, and then rendering the surface within the clipped path.
    Thanks for the response.

    Yes, the end result is correct and too simple for masks.

    However, I'm trying to put together a few lines of code that utilizes the cc.Mask routine.

    :-)

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

    Re: Help with Cairo Mask - Desperate for strip-down basics - Cairo programmers!

    Quote Originally Posted by webbiz View Post
    ...I'm trying to put together a few lines of code that utilizes the cc.Mask routine.
    I gave two small examples (describing Masks with some background-info) recently in #4 and #6 here:
    https://www.vbforums.com/showthread....=1#post5552433

    In more general terms:
    - a "Clip" is "On/Off" - it describes (with its Path) a hard border (further drawings restricted to the "inside" of the path).
    - a "Mask" is like a predefined Stamp (based on a separate Surface one has to create beforehand... think "Potato-Surface")
    - the CC.MaskSurface-call is performing "the Stamping-Operation"
    .. (using that predefined "Potato-Surface", against a prior set CC.Source as the "material to stamp with")

    So, where Clipping is "On/Off" - a Masking allows "degrees of applying the Source" -
    (depending on the Alpha-Channel of the "Stamp-Mask", in your predefined "Potato-Surface").

    Just in case you're struggling with "Potato-Stamping" (though if you have kids, you should know already ):


    HTH

    Olaf
    Last edited by Schmidt; Jan 27th, 2022 at 05:27 AM.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: Help with Cairo Mask - Desperate for strip-down basics - Cairo programmers!

    Quote Originally Posted by Schmidt View Post
    I gave two small examples (describing Masks with some background-info) recently in #4 and #6 here:
    https://www.vbforums.com/showthread....=1#post5552433

    In more general terms:
    - a "Clip" is "On/Off" - it describes (with its Path) a hard border (further drawings restricted to the "inside" of the path).
    - a "Mask" is like a predefined Stamp (based on a separate Surface one has to create beforehand... think "Potato-Surface")
    - the CC.MaskSurface-call is performing "the Stamping-Operation"
    .. (using that predefined "Potato-Surface", against a prior set CC.Source as the "material to stamp with")

    So, where Clipping is "On/Off" - a Masking allows "degrees of applying the Source" -
    (depending on the Alpha-Channel of the "Stamp-Mask", in your predefined "Potato-Surface").

    Just in case you're struggling with "Potato-Stamping" (though if you have kids, you should know already ):

    HTH

    Olaf
    Hello Olaf, thanks for the reply.

    I apologize that my mask post questions are not coming across clearly. I'm not sure how else to express this.

    What I'm trying to find is how to use (cc.Mask) in the most basic, fewest lines, possible in order to having something to use to explain its use in my documentation.

    ccMask, not cc.MaskSurface.

    Yes, cc.MaskSurface examples I have found in doing a search.

    Unfortunately, I cannot find enough information on just cc.Mask other than Tutorial #11. The code in #11 is a bit complicated to follow (sorry, but the comments on cc.Pushup and cc.Popup aren't clear to me).



    As to "Potato-Stamping", never heard of that and yes I raised a daughter many years ago.

    Is that a Euro thing?


    As to the subject of Masks itself, I understand masks. I do video editing and use masks and alpha channels all the time.


    cc.Mask()

    How is this used in its most basic fewest lines if someone wants to just show a few lines of code to demonstrate how it is suppose to be used?

    What is the difference between cc.Mask() and cc.MaskSurface(), and when would someone choose one over the other?

    That's what I'm trying to figure out so I can document it in my "Cairo for Dummies" writeup. :-)

    Thank you!
    Last edited by webbiz; Jan 27th, 2022 at 01:48 PM.

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

    Re: Help with Cairo Mask - Desperate for strip-down basics - Cairo programmers!

    Quote Originally Posted by webbiz View Post
    What is the difference between cc.Mask() and cc.MaskSurface(), and when would someone choose one over the other?
    Basically none (they do the same thing).

    In one case (CC.Mask) - a cCairoPattern is expected as the input-param (as Intellisense told you),
    in the other (CC.MaskSurface) a cCairoSurface is expected (along with optional x, y Offsets).

    These optional Offset-Params are not needed in case of CC.Mask (and the underlying cCairoPattern),
    beause "a Pattern" can be assigned its own Coord-Sys (it has its own "Pattern-Matrix"-Object).

    And yes, you can easily "elevate" a cCairoSurface to a cCairoPattern, using:
    Set MyPattern = MySurface.CreateSurfacePattern(...)

    And thus, a cCairoPattern *can* have an underlying Surface beneath it,
    but not necessarily so - there's also "more lighweight Patterns" (without a Surface underneath), as e.g.:

    Set MyLinearPattern = Cairo.CreateLinearPattern(...)
    Set MyRadialPattern = Cairo.CreateRadialPattern(...)

    In this regard, reading your post in the other thread here: https://www.vbforums.com/showthread....=1#post5553764

    As my examples above should have shown you, you apparently do not need the "New "-Keyword,
    to construct a new Object of a given type.

    In case of cCairoPatterns (or cCairoSurfaces), you go over a "constructor-object" (in this case Cairo As cCairo) -
    and call the appropriate Createxxx(...) function (with constructor-params), to create your new Pattern- or Surface-instances.

    HTH

    Olaf

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: Help with Cairo Mask - Desperate for strip-down basics - Cairo programmers!

    Thank you Olaf.

    It makes it a little clearer. There is so much to learn on this subject. :-)

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