Click or drag to resize

IMcGraphObjBitmapMaskBitmap Method

Mask opacity of parts of the bitmap from image or bitmap source data.

Namespace:  MediaCy.IQL.Display.Overlays
Assembly:  MediaCy.IQL.Display.Overlays (in MediaCy.IQL.Display.Overlays.dll) Version: 10.0.6912.0
Syntax
VB
Sub MaskBitmap ( 
	<OptionalAttribute> MaskBitmapSource As Object,
	Optional Frame As Integer = -1,
	Optional MaskOptionFlags As mcGraphObjMaskBitmapOptions = mcGraphObjMaskBitmapOptions.mcgmboNone,
	<OptionalAttribute> SourceRect As Object,
	<OptionalAttribute> DestOffset As Object
)

Parameters

MaskBitmapSource (Optional)
Type: SystemObject
If not given or empty, then all pixels in the bitmap are made Opaque (Alpha 255) if the MaskOptionFlags mcgmboMissingIsMultiplierByte flag is not set; if the MaskOptionFlags mcgmboMissingIsMultiplierByte flag is set, then the all pixels in the bitmap are set to the alpha value taken from the MaskOptionFlags mcgmboMaskMultiplierByte. Otherwise, if the MaskBitmapSource is non-Empty, it may be one of the same types of image sources as for the BitmapSource argument to the SetBitmap method. However, the source must resolve to a 1 bpp (bit-per-pixel) or an 8 bpp indexed bitmap. The most likely source for such a mask will be an instance of a McBitMask object (1 bpp) or an instance of an 8-bit grayscale (mciqtGray) McImage (8 bpp). Occasionally, however the source may be an object of type McGraphObjBitmap, IDataObject or IStream. Whatever the source, the object is rendered into a masking bitmap made up of single Byte values from 0 to 255. This byte value is then scaled by the mcgmboMaskMultiplierByte and mcgmboMaskDivisorByte of the MaskOptionFlags argument to represent an alpha value from 0 through 255 (zero being transparent and 255 being 100% Opaque). The scaled mask byte value of each mask pixel is then used to set the Alpha byte of a corresponding pixel in the bitmap set by the last call to SetBitmap, with exceptions as controlled by the mcgmboIgnoreZeroMaskValues or mcgmboIgnoreNonZeroMaskValues MaskOptionFlags.
Frame (Optional)
Type: SystemInt32
For multi-frame sources, the frame to be used for masking. If not given, McActiveFrame (-1) is assumed: for McImage sources, this will be McImage.ActiveFrameIndex; for other sources it will be frame zero.
MaskOptionFlags (Optional)
Type: MediaCy.IQL.Display.OverlaysmcGraphObjMaskBitmapOptions
Flags that can be OR'ed together to control aspects of how the MaskBitmapSource is used for masking. The mcgmboMaskMultiplierByte is a muliplier and the mcgmboMaskDivisorByte is a divisor for scaling mask bytes. The multiplier is applied before the divisor, allowing you to scale values with a precision of one part in 256. For example, to scale mask byte values by 75%, the mcgmboMaskMultiplierByte would be 196, and the mcgmboMaskDivisorByte would be 255.
SourceRect (Optional)
Type: SystemObject
If given, a LONGRECT or an array of four values in the order of left, top, right, bottom giving the rectangle in the BitmapSource to be taken as the masking bitmap. If not given, then the entire source image is used.
DestOffset (Optional)
Type: SystemObject
a LONGPOINT or an array of two values in the order of X, Y giving the offset in the bitmap of the left, top point of the rectangle to be masked. If not given, then masking starts at the left,top of the bitmap. No scaling of the mask is ever done; the rectangle masked is always the width and height of the SourceRect. The entire offset masking rectangle does not need to fall within the bitmap NaturalSize. Only the intersection of the masking rectangle with the bitmap is masked; if this intersection is empty, then no masking occurs.
Remarks
SetBitmap (or SetBitmapFromClipboard) must be called before this method is called. Calling MaskBitmap will force the bitmap to be converted to a true-color bitmap (one with zero PaletteCount), if it is not one already. Thus, you must do any Palette property assignments on an indexed-color bitmap before calling MaskBitmap. The effect of any such Palette assignments will be reflected in the colors and alpha values (opacity) of pixels in the converted, true-color bitmap.
Examples
VB
'This is McGraphObjBitmapSamples.bas

Option Explicit

'Note: the BoneBig.jpg example image is a good one for most of these examples
Private Function uLoadExampleImage(Optional strImage As String = "BoneBig.jpg") As Boolean
    If MsgBox("Open example image?", vbYesNo) = vbYes Then
        Images.Open Path + "Images\" + strImage
    End If 'user wants to open an example image
    'Else leave the ActiveImage alone
    If ActiveImage Is Nothing Then
        MsgBox "There is no ActiveImage, so the example cannot run."
    Else
        Output.Show "Output"
        Output.Clear
        ActiveImage.RegionFeatures.Reset
        ActiveImage.LineFeatures.Reset
    End If
    uLoadExampleImage = ActiveImage Is Nothing
End Function 'uLoadExampleImage

'This function gets a bit mask of thresholded regions within the AOI
' of the ActiveImage that are larger than 200 pixels in size
Private Function fGetBitMaskFromBigRegions() As McBitMask
    With ActiveImage
        .RegionFeatures.Threshold.Execute 'get some regions
        Dim selBigOnes As McObject
        Set selBigOnes = McOpGT(.RegionFeatures.mRgnArea, 200)  'larger than 200 pixels
        Dim myBitMask As McBitMask
        Set myBitMask = .RegionFeatures.CreateFeatureMask(mcfmfReturnMcBitMask, selBigOnes, .Aoi.BoundingRect)
        Output.PrintMessage "The mask of big regions has " & myBitMask.BlobCount & " blobs."
        Output.PrintMessage "It is " & myBitMask.Width & " wide by " & myBitMask.Height & " high."
        Set fGetBitMaskFromBigRegions = myBitMask
    End With 'ActiveImage
End Function 'fGetBitMaskFromBigRegions

'Show a color McImage as a McGraphObjBitmap on the AnnotationOverlay.
'Make part of the color range of the displayed bitmap be transparent.
'
Public Sub ShowColorMcImageAsBitmap()
    Windows.CloseAll 'start with a clean plate
    Dim mySrcImage As McImage
        'ColorBlk.tif goes from 0-255 Red top to bottom, 255-0 Blue
        'top to bottom and 0-255 Green, left to right.
    Set mySrcImage = Images.Open(Path + "Images\ColorBlk.tif")
        'Set a circular AOI covering the whole color block
    ActiveImage.Aoi.SetEllipse 0, 127.5, 127.5, 255, 255
    ActiveWindow.Position = Array(410, 10) 'move it to the right

        'Open an underlying grayscale image
    Images.Open (Path + "Images\BoneBig.jpg")
    ActiveWindow.Position = Array(5, 10)
        'Show ColorBlk.tif on top of BoneBig.jpg as a McGraphObjBitmap
        'Note that the overlay shows in color even though BoneBig is monochrome
    With ActiveImage.AnnotationOverlay
        Dim myBM As McGraphObjBitmap
        Set myBM = .Add("McGraphObjBitmap")
        myBM.SetPosition 10, 10
        myBM.SetBitmap mySrcImage
        myBM.NotifyCreationComplete
        MsgBox "The whole circular AOI of the ColorBlk bitmap should show now."
            'Make part of the bitmap's colors to be transparent
        myBM.TransparentColorRange = Array(&H404000, &HC0C0FF) 'Red 0-FF, Green 40-FF, Blue 40-FF hex
        MsgBox "A block in the middle of the ColorBlk should be transparent"
            'Make the whole bitmap translucent
        myBM.Opacity = 50 'percent opaque
        MsgBox "The ColorBlk circle, with its hole, should be translucent."
            'Now place a second, reduced copy, ignoring the Aoi
        Set myBM = .Add("McGraphObjBitmap")
        myBM.SetPosition 270, 10
        myBM.ShowRectangle = True 'show a rectangular border
        myBM.SetBitmap mySrcImage, McActiveFrame, mcgboIgnoreAoi, , Array(100, 100)
        myBM.NotifyCreationComplete
            'Place a third, full size copy of only a portion of the source image
        Set myBM = .Add("McGraphObjBitmap")
        myBM.SetPosition 270, 130
        myBM.ShowRectangle = True 'show border
        myBM.SetBitmap mySrcImage, McActiveFrame, , Array(10, 10, 109, 109)
        myBM.NotifyCreationComplete
            'Finally, place a fourth, reduced copy of just the AOI
        Set myBM = .Add("McGraphObjBitmap")
        myBM.SetPosition 270, 280
        myBM.SetBitmap mySrcImage, McActiveFrame, , , Array(100, 100)
        myBM.NotifyCreationComplete

        MsgBox "Three reduced or selected pieces of the ColorBlk have been added." + vbCrLf + _
            "Images will close when you press OK."
    End With
    mySrcImage.Close 'close the ColorBlk source McImage
    ActiveImage.Modified = False
    ActiveImage.Close 'close test image
End Sub 'ShowColorMcImageAsBitmap

'Show a monochrome McImage as a McGraphObjBitmap on the AnnotationOverlay.
'Some examples of masking of transparency are shown, and then it
'manipulates the pseudo-coloring and opacity of the bitmap by setting
'the palette.
'
Public Sub ShowMonoMcImageAsBitmapWithPalette()
    Windows.CloseAll 'start with a clean plate
    Output.Show ("Output")
    Output.Clear
    Dim mySrcImage As McImage
        'GrayBlk.tif goes from 0 (black) on the top line to 255 on the bottom line
    Set mySrcImage = Images.Open(Path + "Images\GrayBlk.tif")
        ' pseudo-color, GrayBlk.tif
    Dim pscnew As New McPseudoColor
    With pscnew
        .Size = 256
        pscnew.Visible = True
            'Get a Yellow color spectrum as a LUT Variant.  We will use it later
        .ColorSpectrum = mccsYellow
        Dim YellowPseudocolorLUT As Variant
        YellowPseudocolorLUT = .GetLUTForImageType(mySrcImage.Type)
            'now set the spectrum to HSI Blue to Red and attach it to GrayBlk.tif
        pscnew.ColorSpectrum = mccsBlueRed
    End With 'pscNew McPseudoColor
    mySrcImage.AttachPseudoColor pscnew
    ActiveWindow.Position = Array(20, 20)
    MsgBox "GrayBlk.tif should show as a pseudocolored McImage."

        'Open a color image to underly our bitmap
    Images.Open (Path + "Images\NUCSTAIN.TIF")
    ActiveWindow.Position = Array(10, 10)
        'Show the pseudocolored GrayBlk.tif on top of NUCSTAIN.TIF as a McGraphObjBitmap
    With ActiveImage.AnnotationOverlay
            'Show a 1/2 size version of GrayBlk.tif without the pseudocoloring
        Dim myBM1 As McGraphObjBitmap
        Set myBM1 = .Add("McGraphObjBitmap")
        With myBM1
            .SetPosition 350, 30 'show in upper-right of NUCSTAIN
            .ShowRectangle = True
            .BorderColor = vbCyan 'cyan border
            .SetBitmap mySrcImage, -1, mcgboNone, , Array(128, 128)
                'without the mcgboApplyColorCorrection OptionFlags, we don't keep the pseudocoloring
            .NotifyCreationComplete
            'Note that by supplying a destination size to SetBitmap that required rescaling,
            'the bitmap is made a true color image (not indexed).
            If .PaletteCount = 0 Then
                'This is the case here
                Output.PrintMessage "Bitmap 1 is a true-color image; the Palette will be Empty."
            Else
                Output.PrintMessage "The bitmap is a indexed-color image; the Palette can be assigned to."
            End If
        End With 'bitmap 1

            'Show a full version of GrayBlk.tif with the pseudocoloring
        Dim myBM2 As McGraphObjBitmap
        Set myBM2 = .Add("McGraphObjBitmap")
        With myBM2
            .SetPosition 20, 20 'show in upper-left of NUCSTAIN
            .ShowRectangle = True
            .BorderColor = vbMagenta 'magenta border
            .SetBitmap mySrcImage, -1, mcgboApplyColorCorrection
                'mcgboApplyColorCorrection above, means that we keep the pseudocoloring
            .NotifyCreationComplete
            'Note that by not supplying a destination size to SetBitmap,
            'the bitmap is kept as an indexed-color bitmap, allowing the Palette property to be used.
            If .PaletteCount = 0 Then
                'This is the case here
                Output.PrintMessage "The bitmap is a true-color image; the Palette will be Empty."
            Else
                'This is the case here
                Output.PrintMessage "Bitmap 2 is a indexed-color image; the Palette can be assigned to."
            End If
        End With 'bitmap 2
        mySrcImage.Close 'we no longer need the bitmap source GrayBlk.tif McImage
        MsgBox "Two GrayBlk bitmaps should show: one big and pseudocolored, the other small and not."

            'Now create a circular mask for the small gray bitmap1
        Dim mcregionsT As McRegions, mcimgMaskT As McImage
        Set mcregionsT = CreateOperator("McRegions") 'a global McRegions (i.e., no parent McImage)
            'just one cicle covering the whole small bitmap with a McImage mask
        mcregionsT.SetEllipse -1, 63.5, 63.5, 127, 127
        Set mcimgMaskT = mcregionsT.CreateFeatureMask(mcfmfReturnMaskImage Or mcfmfUseFeatureBoundsAsDefault)
        myBM1.MaskBitmap mcimgMaskT 'apply the circular mask
        myBM1.ShowRectangle = False 'turn off the border
        Set mcimgMaskT = Nothing
        MsgBox "The small, gray bitmap should now be a circle."

            'clear the circular mask & set it all to transparent
        myBM1.MaskBitmap Empty, -1, mcgmboMissingIsMultiplierByte
        myBM1.ShowRectangle = True 'turn on the border
        MsgBox "The small, gray bitmap should now be a transparent square."

            'Now create a McBitMask mask with two ellipses
        mcregionsT.SetEllipse -1, 30, 30, 52, 52
        mcregionsT.SetEllipse 1, 97, 30, 52, 52

        Dim mcbitmaskT As McBitMask
        Set mcbitmaskT = mcregionsT.CreateFeatureMask(mcfmfReturnMcBitMask Or mcfmfUseFeatureBoundsAsDefault)
        myBM1.MaskBitmap mcbitmaskT, , , , Array(4, 70) 'apply the circular mask in the bottom, lighter part
        MsgBox "The small, gray bitmap should now have two light circles in the lower half of the square."
            'Set up option flags to scale Opacity to 60 percent
        Dim myOptionFlags As mcGraphObjBitmapOptions
        myOptionFlags = (60& * mcgmboMaskMultiplierByteShift) + (100& * mcgmboMaskDivisorByteShift)
        myBM1.MaskBitmap mcbitmaskT, -1, myOptionFlags, , Array(4, 4)  'apply the 2 circles mask
        MsgBox "The small bitmap should now be have 4 circles, the two upper ones being translucent."
            'Cleanup masking stuff
        Set mcbitmaskT = Nothing
        Set mcregionsT = Nothing
            'Get rid of the small, gray bitmap1; we don't need it for the rest of the example
        myBM1.Remove

            'Make part of the big bitmap's colors to be transparent using the Palette
        With myBM2
                'Make top 1/3 of bitmap 2 transparent
            Dim mcobjPalette As McObject
            Set mcobjPalette = McObjectTemp(.Palette(True)) 'get current palette as a McObject
                'Note that we set the Palette ExposeAsRGBQUAD argument True to get Alpha values
            mcobjPalette.SelectedMcObject(McOpFillIn(0&, .PaletteCount / 3)).OpSelfAndBits &HFFFFFF
            .Palette(True) = mcobjPalette 'Set the new Alpha values, keeping the old pseudocoloring
            MsgBox "The top third of the pseudocolored bitmap should be transparent."
                'Make opacity follow gray value
            'mcobjPalette still holds the current Palette as RGBQUAD values
            mcobjPalette.OpSelfAndBits &HFFFFFF 'clear all Alpha bytes to zero
                'Or in a ramp from 0-255 into the Alpha (high order) byte of the Palette
            mcobjPalette.OpSelfOrBits McOpFillIn(0&, .PaletteCount).OpSelfLeftShift(24)
            .Palette(True) = mcobjPalette 'Set the new Alpha values, keeping the old pseudocoloring
            MsgBox "The pseudocolored bitmap should get progressively more transparent towards its top."
                'Now set a new peudocoloring, keeping the same transparency for each color index.
                'We use the Yellow pseudocoloring LUT we created earlier
            .Palette(False) = YellowPseudocolorLUT 'Set the color values, keeping the old Alpha values
            MsgBox "The the pseudocoloring should change, but it should still get progressively more transparent towards its top." + vbCrLf + _
                "Image will close when you press OK."
        End With 'bitmap 2

    End With ' AnnotationOverlay
    ActiveImage.Modified = False
    ActiveImage.Close 'close test image
End Sub 'ShowMonoMcImageAsBitmapWithPalette

'Show a JPG image in a file 1) as a scaled McGraphObjBitmap and 2) as a sub-selected,
'scaled and rotated McGraphObjBitmap.
'
Public Sub ShowImageFileAsScaledBitmap()
    Dim strFile As String
    strFile = Path & "Images\nikon-e950.jpg" 'An 800 by 600 image
    Images.Add "BlankImage", 500, 200 'smaller than image size
    ActiveWindow.Position = Array(10, 10)

    With ActiveImage.AnnotationOverlay
        Dim myBM As McGraphObjBitmap
            'Show an image from a JPG file at 1/4 size.
        Set myBM = ActiveImage.AnnotationOverlay.Add("McGraphObjBitmap")
        myBM.SetPosition 10, 25
        myBM.SetBitmap strFile, -1, mcgboApplyColorCorrection, , Array(200, 150) 'quarter size
        myBM.NotifyCreationComplete 'done with first image
            'Now show a sub-portion of the same image, scaled and rotated
        Set myBM = ActiveImage.AnnotationOverlay.Add("McGraphObjBitmap")
        myBM.SetPosition 260, 25
        myBM.AngleOfRotation = -25 'make cannon point flatter
        myBM.ShowRectangle = True 'show a border
        myBM.BorderColor = &HFF00FF 'in magenta
        myBM.SetBitmap strFile, -1, mcgboApplyColorCorrection, Array(300, 25, 750, 400), Array(200, 150)
        myBM.NotifyCreationComplete 'done with whole image
    End With
    ActiveImage.Modified = False
    MsgBox "Image will close when you press OK."
    ActiveImage.Close

End Sub 'ShowImageFileAsScaledBitmap

'Threshold bright regions in an AOI, then capture a McBitMask of the
'larger regions and show it as a McGraphObjBitmap underneath the
'displayed region boundaries.
'
Public Sub ShowBitMaskOfAoi()
    If uLoadExampleImage Then Exit Sub
    ActiveWindow.Position = Array(10, 10)
    ActiveImage.Aoi.SetBox -1, 20, 20, 302, 250
        'Threshold and get bit mask of big regions in the AOI
    Dim myBitMask As McBitMask
    Set myBitMask = fGetBitMaskFromBigRegions
    If myBitMask.BlobCount = 0 Then
        Output.PrintMessage "No regions were found."
        Exit Sub
    End If 'no regions found
    'ELSE we have at least one blob to show

    Dim rectBitMask As LONGRECT
    rectBitMask = myBitMask.BoundsRect
    With ActiveImage.RegionFeatures.AutoDisplayOverlay
        Dim myBM As McGraphObjBitmap
        Set myBM = .Add("McGraphObjBitmap")
        With myBM
            .SetPosition rectBitMask.Left, rectBitMask.Top
            .FillColor = vbCyan   'cyan.  Set FillColor before the SetBitmap call
            .SetBitmap myBitMask, -1, mcgboZeroIsTransparent Or mcgboApplyColorCorrection
            'Above, mcgboApplyColorCorrection makes forground bits have the FillColor
            .NotifyCreationComplete
        End With 'myBM
        .MoveToBack myBM 'Make bitmap display 1st, so region bounds will be on top of it
        MsgBox "Large regions should be filled with cyan.  Small regions are not filled."
        myBM.FillColor = vbBlue 'change fill color
        MsgBox "Large regions should be filled with blue" + vbCrLf + _
            "Image will close when you press OK."
    End With 'ActiveImage.RegionFeatures.AutoDisplayOverlay
    ActiveImage.Modified = False
    ActiveImage.Close
End Sub 'ShowBitMaskOfAoi


'Place a translucent, reduced image from the clipboard into the AnnotationOverlay
'
Public Sub PlaceClipboardBitmapIntoAnnotation()
    If uLoadExampleImage Then Exit Sub
    ActiveWindow.Position = Array(10, 10)
        'Check to see if an image or bitmap is already on the clipboard
    If Not ActiveImage.AnnotationOverlay.IsActionAvailable(mcgoaBitmapOnClipboard) Then
        Dim imgT As McImage
        Set imgT = Images.Open(Path + "Images\colorblk.tif", mcicfNotVisible + mcicfNoAddToCollection)
        imgT.Aoi.SetEllipse 0, 127.5, 127.5, 255, 255 'a circular AOI covering whole color block
        imgT.Display.EditCopy mcwcftMcImageAoi 'copy the image to the clipboard
        Set imgT = Nothing 'we are done with this temporary image
    End If 'there is no bitmap on the clipboard, so we put one there

    With ActiveImage.AnnotationOverlay
        Dim myBM As McGraphObjBitmap
        Set myBM = .Add("McGraphObjBitmap")
        With myBM
            .SetPosition 50, 40
                'Place clipboard circular AOI, stretched horizonatlly into an ellipse
            .SetBitmapFromClipboard mcgboApplyColorCorrection, , Array(150, 120)
            .Opacity = 60 'make it partially translucent
            .NotifyCreationComplete
        End With 'myBM
        MsgBox "A small, translucent image from the clipboard should appear in the image." + vbCrLf + _
            "Image will close when you press OK."
        .RemoveAll 'clear the AnnotationOverlay
        'clear the image from the clipboard by copying something else there
        .EditCopy 'this will clear the image from the clipboard
    End With 'ActiveImage.AnnotationOverlay
    ActiveImage.Modified = False
    ActiveImage.Close
End Sub 'PlaceClipboardBitmapIntoAnnotation
See Also