CustomFilterCallback() custom parameter! (Windows x64)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

CustomFilterCallback() custom parameter! (Windows x64)

Post by Mijikai »

Since PureBasic does not allow custom parameters (i hope this gets added eventually).
Here is some hackish way to add a additional custom parameter to the not so CustomFilterCallback()

Code for x64 (needs a test image):

Code: Select all

EnableExplicit

UseJPEGImageDecoder()

Procedure.i FilterImageCallback(X.i,Y.i,ColorSrc.i,ColorDst.i)
  Protected *parameter
  !mov rax,[rsp + 150h]                                ;<- locate the image structure
  !mov rax,[rax + 20h]                                 ;<- extract the custom parameter
  !mov qword[p.p_parameter],rax
  Debug *parameter
  ProcedureReturn ColorSrc
EndProcedure

Procedure.i FilterImage(*Buffer,Path.s = #Null$)
  Protected image.i
  Protected backup.i
  Protected *parameter.Integer
  If *Buffer
    image = CatchImage(#PB_Any,*Buffer)
  Else
    If Path
      image = LoadImage(#PB_Any,Path)
    EndIf 
  EndIf
  If IsImage(image)
    *parameter = image + $20                          ;<- pointer to the image structure (vacant spot afaik)
    backup = *parameter\i                             ;<- make a backup just to be safe
    If StartDrawing(ImageOutput(image))
      DrawingMode(#PB_2DDrawing_CustomFilter) 
      *parameter\i = 123456789                        ;<- set the custom parameter
      CustomFilterCallback(@FilterImageCallback())
      Box(0,0,1,1)                                    ;<- process one pixel
      StopDrawing()
    Else
      FreeImage(image)
      image = #Null
    EndIf
    *parameter\i = backup                             ;<- restore the image structure
  EndIf
  ProcedureReturn image
EndProcedure

FilterImage(#Null,"test.jpg")

End
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: CustomFilterCallback() custom parameter! (Windows x64)

Post by mk-soft »

Callbacks are always called by the external function. Where is the additional parameter to come from?

I use a shared variable to pass my own parameters to the callback...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: CustomFilterCallback() custom parameter! (Windows x64)

Post by Mijikai »

mk-soft wrote:Callbacks are always called by the external function. Where is the additional parameter to come from?

I use a shared variable to pass my own parameters to the callback...
The same way you provide the function address?!
Post Reply