Alpha conversion

Just starting out? Need help? Post your questions and find answers here.
RBeausoleil
User
User
Posts: 18
Joined: Tue Aug 20, 2019 6:47 pm

Alpha conversion

Post by RBeausoleil »

Hi All,

I created a black mask (32 Bit) image and use the Vectors library (Need arcs) , this library uses the display with antialias.

I made several tests with PB2DDrawing to convert the Alpha value of the color points to get an image in black and white only (Clear all the colors that make up the antialias), while maintaining the image in 32 bits .. Sad Reality ... I have not yet found a solution ... I really need help.

Thx.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Alpha conversion

Post by Danilo »

Take a look at the commands DrawingMode(#PB_2DDrawing_CustomFilter) and CustomFilterCallback()

Something like the following: You draw your image onto another image using a filter callback.
Within the callback you can check and change the color of each pixel when it is copied.

Right side is the antialiased drawing, left side is the copied image with anti-aliasing removed:

Code: Select all

Procedure FilterCallback(x, y, SourceColor, TargetColor)
    If SourceColor = $FFFFFFFF
        ProcedureReturn $FFFFFFFF
    EndIf
    ProcedureReturn RGBA($00,$00,$00,$FF)
EndProcedure

Procedure.i CreateTheImageWithArcs(Width,Height)
    Protected img.i = CreateImage(#PB_Any, Width, Height, 32)
    If img And StartVectorDrawing(ImageVectorOutput(img))
        MovePathCursor(0, -10)
        AddPathArc(Width*0.33, Height-10, Width, Height-10, 20)
        AddPathArc(Width, Height-10, Width*0.66, 0, 20)
        AddPathLine(Width*0.66, -10)
        
        VectorSourceColor(RGBA(255,255,255,255))
        StrokePath(10)
        StopVectorDrawing()
    EndIf
    ProcedureReturn img
EndProcedure

Procedure CreateMask(FromImage)
    Protected result.i
    If FromImage
        result = CreateImage(#PB_Any,ImageWidth(FromImage),ImageHeight(FromImage),32)
        If result
            If StartDrawing( ImageOutput(result) )
                DrawingMode(#PB_2DDrawing_CustomFilter)
                CustomFilterCallback(@FilterCallback())
                DrawImage(ImageID(FromImage),0,0)
                StopDrawing()
            EndIf
        EndIf
    EndIf
    ProcedureReturn result
EndProcedure

If OpenWindow(0, 0, 0, 400, 200, "Alpha", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    original = CreateTheImageWithArcs(200,200)
    blackwhite = CreateMask(original)
    ImageGadget(0,  0 , 0, 200, 200, ImageID( original   ))
    ImageGadget(1, 200, 0, 200, 200, ImageID( blackwhite ))
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
EndIf
RBeausoleil
User
User
Posts: 18
Joined: Tue Aug 20, 2019 6:47 pm

Re: Alpha conversion

Post by RBeausoleil »

Thx a lot Danilo,

This did not seem very obvious because, I tried desperately to simply change the Alpha value of the source image without thinking of converting to another image. This will greatly help me to remove the antialias effect that caused me headaches.

Thank you so much again.

Roger
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4984
Joined: Sun Apr 12, 2009 6:27 am

Re: Alpha conversion

Post by RASHAD »

Hello Danilo
Very glad to see you around :)
Your new avatar looks somehow like the National Park Guard :P

No Antialias in one touch

Code: Select all

If OpenWindow(0, 0, 0, 200, 200, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    CanvasGadget(0, 0, 0, 200, 200)
    Width = 200
    Height = 200
    
    If StartVectorDrawing(CanvasVectorOutput(0))
      VectorSourceColor(RGBA(0, 0, 0, 255))
      FillVectorOutput()
      MovePathCursor(0, -10)
      AddPathArc(Width*0.33, Height-10, Width, Height-10, 20)
      AddPathArc(Width, Height-10, Width*0.66, 0, 20)
      AddPathLine(Width*0.66, -10)
     
      VectorSourceColor(RGBA(255,255,255,255))
      StrokePath(10)
      StartDrawing(CanvasOutput(0))
        For x = 0 To 199
          For y = 0 To 199
            c = Point(x,y)
            If c <> $FFFFFF
              Plot(x,y,$FF000000)
            EndIf
          Next
        Next
      StopDrawing()    
      StopVectorDrawing()
    EndIf
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  EndIf
Egypt my love
Post Reply