'
'                           ___________________________________________
'         _______ _ _ _     \___           ___________________________/
'    /\  (_______) (_) |       /  //  //  /  
'   /  \  _  __  | |_| | _    /  //  //  / << version 0.86 alpha test >>
'  / /\ \| |(__| | | | || \  /  //  //  /  For FreeBASIC (v0.20b or later).
' | |__| | |     | | | |_) )/  //  //  /     
' |_|  |_|_|     |_|_|____//  //  //  /   Original Development by
'  _______________________/  //  //  /___ Adigun Azikiwe Polack for Aura Flow.
' /_____________________________________/
'
'  A  u  r  a      F  l  o  w      L  i  b  r  a  r  y      3
'_________________________________________________________________________
'
' (c)2005-2016 Adigun Azikiwe Polack/Aura Flow.  All Rights Reserved.
'
'
'
'   ______                 __    _              _______ ____           
'  / ____/________ _____  / /_  (_)_________   / ____(_) / /____  _____
' / / __/ ___/ __ `/ __ \/ __ \/ / ___/ ___/  / /_  / / / __/ _ \/ ___/
'/ /_/ / /  / /_/ / /_/ / / / / / /__(__  )  / __/ / / / /_/  __/ /    
'\____/_/   \__,_/ .___/_/ /_/_/\___/____/  /_/   /_/_/\__/\___/_/     
'               /_/                            __  ___          __      __   
'                                             /  |/  /___  ____/ /_  __/ /__ 
'                                            / /|_/ / __ \/ __  / / / / / _ \
'                                           / /  / / /_/ / /_/ / /_/ / /  __/
'                                          /_/  /_/\____/\__,_/\__,_/_/\___/ 
'                        
'
'_________________________________________________________________________
'                        ----- N O T I C E -----
'_________________________________________________________________________
' The following is this piece of source code to this version of AFlib III.
' You are free to look at it to give you some inspiring ideas in your own
' programming in FreeBASIC, but you are *never* to even change or adjust
' any of this source code whatsoever in any way, shape, or form at all.
' EVER.  ;)
'
' The reason for this is to ensure that all of the routines in this source
' code are working as properly as they should in FreeBASIC v0.20b or
' newer.  That is all.
'
' With that said, do enjoy the library and be inspired even more
' greatly than ever!!!  (^-^)//
'_________________________________________________________________________



'$profile
'$stop_scan

#include once "fbgfx.bi"
ScreenControl(FB.SET_DRIVER_NAME, "GDI")

#define isBufferNew_e( __buffer__ ) iif( __buffer__ <> 0, *cast( Integer Ptr, __buffer__ ) = 7, 0 )

Declare Sub AFlibIII_Mosaic (Byval size as Integer, Byval buffer as any ptr = 0)
Declare Sub AFlibIII_Mosaic_Width (Byval size as Integer, Byval buffer as any PTR = 0)
Declare Sub AFlibIII_Patchwork (Byval buffer as any ptr = 0)
Declare Sub AFlibIII_StainedTile (Byval buffer as any ptr = 0)
Declare Sub AFlibIII_GlassWall (Byval buffer as any ptr = 0)
Declare Sub AFlibIII_OilPaint (Byval buffer as any ptr = 0)
Declare Sub AFlibIII_Quilt (Byval buffer as any ptr = 0)

Declare Sub AFlibIII_Scanlines_H (Byval buffer as any ptr = 0)
Declare Sub AFlibIII_Scanlines_V (Byval buffer as any ptr = 0)
Declare Sub AFlibIII_Scanlines_add_H (Byval buffer as any ptr = 0)
Declare Sub AFlibIII_Scanlines_add_V (Byval buffer as any ptr = 0)

Declare Sub AFlibIII_CustomScanlines_H ( Byval LineStart as Integer, _
                                         Byval LineSkip as Integer, _
                                         Byval alpha as Integer, _
                                         Byval buffer as any ptr = 0 )


Declare Sub AFlibIII_CustomScanlines_V ( Byval LineStart as Integer, _
                                         Byval LineSkip as Integer, _
                                         Byval alpha as Integer, _
                                         Byval buffer as any ptr = 0 )

Declare Sub AFlibIII_CustomScanlines_add_H ( Byval LineStart as Integer, _
                                             Byval LineSkip as Integer, _
                                             Byval add_blend as Integer, _
                                             Byval buffer as any ptr = 0 )

Declare Sub AFlibIII_CustomScanlines_add_V ( Byval LineStart as Integer, _
                                             Byval LineSkip as Integer, _
                                             Byval add_blend as Integer, _
                                             Byval buffer as any ptr = 0 )

Declare Sub AFlibIII_CustomScanlines_color_H ( Byval LineStart as Integer, _
                                               Byval LineSkip as Integer, _
                                               Byval col as integer, _
                                               Byval alpha_level as integer, _
                                               Byval buffer as any ptr = 0 )

Declare Sub AFlibIII_CustomScanlines_color_V ( Byval LineStart as Integer, _
                                               Byval LineSkip as Integer, _
                                               Byval col as integer, _
                                               Byval alpha_level as integer, _
                                               Byval buffer as any ptr = 0 )

Declare Sub AFlibIII_CustomScanlines_color_add_H ( Byval LineStart as Integer, _
                                                   Byval LineSkip as Integer, _
                                                   Byval col as integer, _
                                                   Byval add_level as integer, _
                                                   Byval buffer as any ptr = 0 )

Declare Sub AFlibIII_CustomScanlines_color_add_V ( Byval LineStart as Integer, _
                                                   Byval LineSkip as Integer, _
                                                   Byval col as integer, _
                                                   Byval add_level as integer, _
                                                   Byval buffer as any ptr = 0 )

Declare Sub AFlibIII_glow32 ( Byval Deepness_of_the_Glow as Integer, _
                              Byval Glow_Brightness as Integer, _
                              Byval Glow_Level as Integer = 255, _
                              Byval buffer as any PTR = 0 )

Declare Sub AFlibIII_blur32 ( Byval Deepness_of_the_Blur as Integer, _
                              Byval Blur_Level as Integer, _
                              Byval buffer as any PTR = 0 )

Declare Sub Private4_check_buffersize ( Byval buffer As Any Ptr, Byref x As Integer, _
                                        Byref y As Integer )

Declare Sub blur_h(byval source as uinteger ptr, byval dest as uinteger ptr,_
            byval wid as integer, byval hei as integer, byval radius as uinteger)
Declare Sub blur_v(byval source as uinteger ptr, byval dest as uinteger ptr,_
            byval wid as integer, byval hei as integer, byval radius as uinteger)           



'__________________________________________________________________________
Sub AFlibIII_Mosaic (Byval size as Integer, Byval buffer as any PTR = 0)

If size <= 1 then Exit Sub
If size >= 512 then size = 512

Dim as Integer P, Xsize, Ysize, Gfx_horiz, Gfx_vert

If not buffer = 0 then
  Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
else
  ScreenInfo Gfx_horiz, Gfx_vert
End if

ScreenLock

For Xsize = 0 TO (Gfx_horiz - 1) step size
  For Ysize = 0 TO (Gfx_vert - 1) step size
    If not buffer = 0 then
      P = Point(Xsize, Ysize, buffer)
      Line buffer, (Xsize - 1, Ysize - 1)-(Xsize + size - 1, Ysize + size - 1), P, BF
    else
      P = Point(Xsize, Ysize)
      Line (Xsize - 1, Ysize - 1)-(Xsize + size - 1, Ysize + size - 1), P, BF
    End if
  Next Ysize
Next Xsize

ScreenUnlock

End Sub


'__________________________________________________________________________
Sub AFlibIII_Mosaic_Width (Byval size as Integer, Byval buffer as any PTR = 0)

If size <= 1 then Exit Sub
If size >= 512 then size = 512

Dim as Integer P, Xsize, Ysize, Gfx_horiz, Gfx_vert

If not buffer = 0 then
  Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
else
  ScreenInfo Gfx_horiz, Gfx_vert
End if

ScreenLock

For Xsize = 0 TO (Gfx_horiz - 1) step size
  If not buffer = 0 then
    For Ysize = 0 TO (Gfx_vert - 1) step 1
      P = Point(Xsize, Ysize, buffer)
      Line buffer, (Xsize - 1, Ysize)-(Xsize + size - 1, Ysize), P
    Next
  else
    For Ysize = 0 TO (Gfx_vert - 1) step 1
      P = Point(Xsize, Ysize)
      Line (Xsize - 1, Ysize)-(Xsize + size - 1, Ysize), P
    Next
  End if
Next Xsize

ScreenUnlock

End Sub


'__________________________________________________________________________
Sub AFlibIII_Patchwork (Byval buffer as any PTR = 0)

Dim as Integer P, Xsize, Ysize, TempC, BaseColor, C, Passes
Dim as Integer Gfx_horiz, Gfx_vert
ScreenInfo Gfx_horiz, Gfx_vert

ScreenLock

If not buffer = 0 then
  Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
  For Xsize = 0 TO (Gfx_horiz - 1) step 4
    For Ysize = 0 TO (Gfx_vert - 1) step 4
      P = Point(Xsize, Ysize, buffer)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize, buffer) and &hffffff
      Circle buffer, (Xsize, Ysize), 3, P, , , , F
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle buffer, (Xsize, Ysize), 2, C, , , , F
    Next Ysize
  Next Xsize
else
  For Xsize = 0 TO (Gfx_horiz - 1) step 4
    For Ysize = 0 TO (Gfx_vert - 1) step 4
      P = Point(Xsize, Ysize)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize) and &hffffff
      Circle (Xsize, Ysize), 3, P, , , , F
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle (Xsize, Ysize), 2, C, , , , F
    Next Ysize
  Next Xsize
End if

ScreenUnlock

End Sub


'__________________________________________________________________________
Sub AFlibIII_StainedTile (Byval buffer as any PTR = 0)

Dim as Integer P, Xsize, Ysize, TempC, BaseColor, C, Passes
Dim as Integer Gfx_horiz, Gfx_vert
ScreenInfo Gfx_horiz, Gfx_vert

ScreenLock

If not buffer = 0 then
  Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
  For Xsize = 0 TO (Gfx_horiz - 1) step 8
    For Ysize = 0 TO (Gfx_vert - 1) step 8
      P = Point(Xsize, Ysize, buffer)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize, buffer) and &hffffff
      Circle buffer, (Xsize, Ysize), 7, P, , , , F
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle buffer, (Xsize, Ysize), 6, C, , , , F
    Next Ysize
  Next Xsize
else
  For Xsize = 0 TO (Gfx_horiz - 1) step 8
    For Ysize = 0 TO (Gfx_vert - 1) step 8
      P = Point(Xsize, Ysize)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize) and &hffffff
      Circle (Xsize, Ysize), 7, P, , , , F
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle (Xsize, Ysize), 6, C, , , , F
    Next Ysize
  Next Xsize
End if

ScreenUnlock

End Sub


'__________________________________________________________________________
Sub AFlibIII_GlassWall (Byval buffer as any PTR = 0)

Dim as Integer P, Xsize, Ysize, TempC, BaseColor, C, Passes
Dim as Integer Gfx_horiz, Gfx_vert
ScreenInfo Gfx_horiz, Gfx_vert

ScreenLock

If not buffer = 0 then
  Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
  For Xsize = 0 TO (Gfx_horiz - 1) step 16
    For Ysize = 0 TO (Gfx_vert - 1) step 16
      P = Point(Xsize, Ysize, buffer)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize, buffer) and &hffffff
      Circle buffer, (Xsize, Ysize), 9, P, , , , F
      Circle buffer, (Xsize, Ysize), 13, P
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle buffer, (Xsize, Ysize), 10, C, , , , F
      Circle buffer, (Xsize, Ysize), 15, C
    Next Ysize
  Next Xsize
else
  For Xsize = 0 TO (Gfx_horiz - 1) step 16
    For Ysize = 0 TO (Gfx_vert - 1) step 16
      P = Point(Xsize, Ysize)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize) and &hffffff
      Circle (Xsize, Ysize), 9, P, , , , F
      Circle (Xsize, Ysize), 13, P
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle (Xsize, Ysize), 10, C, , , , F
      Circle (Xsize, Ysize), 15, C
    Next Ysize
  Next Xsize
End if

ScreenUnlock

End Sub


'__________________________________________________________________________
Sub AFlibIII_OilPaint (Byval buffer as any PTR = 0)

Dim as Integer P, Xsize, Ysize, TempC, BaseColor, C, Passes
Dim as Integer Gfx_horiz, Gfx_vert
ScreenInfo Gfx_horiz, Gfx_vert

ScreenLock

If not buffer = 0 then
  Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
  For Xsize = 0 TO (Gfx_horiz - 1) step 8
    For Ysize = 0 TO (Gfx_vert - 1) step 8
      P = Point(Xsize, Ysize, buffer)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize, buffer) and &hffffff
      Circle buffer, (Xsize, Ysize), 4, P, , , , F
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle buffer, (Xsize, Ysize), 5, C, , , , F
      Circle buffer, (Xsize, Ysize), 7, C
    Next Ysize
  Next Xsize
else
  For Xsize = 0 TO (Gfx_horiz - 1) step 8
    For Ysize = 0 TO (Gfx_vert - 1) step 8
      P = Point(Xsize, Ysize)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize) and &hffffff
      Circle (Xsize, Ysize), 4, P, , , , F
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle (Xsize, Ysize), 5, C, , , , F
      Circle (Xsize, Ysize), 7, C
    Next Ysize
  Next Xsize
End if

ScreenUnlock

End Sub


'__________________________________________________________________________
Sub AFlibIII_Quilt (Byval buffer as any PTR = 0)

Dim as Integer P, Xsize, Ysize, TempC, BaseColor, C, Passes
Dim as Integer Gfx_horiz, Gfx_vert
ScreenInfo Gfx_horiz, Gfx_vert

ScreenLock

If not buffer = 0 then
  Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
  For Xsize = 0 TO (Gfx_horiz - 1) step 4
    For Ysize = 0 TO (Gfx_vert - 1) step 4
      P = Point(Xsize, Ysize, buffer)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize, buffer) and &hffffff
      Circle buffer, (Xsize, Ysize), 1, P, , , , F
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle buffer, (Xsize, Ysize), 2, C, , , , F
      Circle buffer, (Xsize, Ysize), 3, C
    Next Ysize
  Next Xsize
else
  For Xsize = 0 TO (Gfx_horiz - 1) step 4
    For Ysize = 0 TO (Gfx_vert - 1) step 4
      P = Point(Xsize, Ysize)
      TempC = P and &hffffff              'and color of box by 15
      BaseColor = P - TempC               'get basecolor
      P = Point(Xsize, Ysize) and &hffffff
      Circle (Xsize, Ysize), 1, P, , , , F
      C = ((TempC + P) \ 2) + BaseColor     'average and add
      Circle (Xsize, Ysize), 2, C, , , , F
      Circle (Xsize, Ysize), 3, C
    Next Ysize
  Next Xsize
End if

ScreenUnlock

End Sub


'__________________________________________________________________________
Sub AFlibIII_Scanlines_H (Byval buffer as any PTR = 0)

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = 0 to Gfx_vert step 2
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), 0
    Next
    Put buffer, (0, 0), TempBuffer, alpha, 128
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = 0 to Gfx_vert step 2
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), 0
    Next
    Put (0, 0), TempBuffer, alpha, 128
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_Scanlines_V (Byval buffer as any PTR = 0)

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = 0 to Gfx_horiz step 2
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), 0
    Next
    Put buffer, (0, 0), TempBuffer, alpha, 128
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = 0 to Gfx_horiz step 2
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), 0
    Next
    Put (0, 0), TempBuffer, alpha, 128
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_Scanlines_add_H (Byval buffer as any PTR = 0)

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = 0 to Gfx_vert step 2
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), RGB(255, 255, 255)
    Next
    Put buffer, (0, 0), TempBuffer, add, 64
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = 0 to Gfx_vert step 2
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), RGB(255, 255, 255)
    Next
    Put (0, 0), TempBuffer, add, 64
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_Scanlines_add_V (Byval buffer as any PTR = 0)

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = 0 to Gfx_horiz step 2
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), RGB(255, 255, 255)
    Next
    Put buffer, (0, 0), TempBuffer, add, 64
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = 0 to Gfx_horiz step 2
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), RGB(255, 255, 255)
    Next
    Put (0, 0), TempBuffer, add, 64
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_CustomScanlines_H ( Byval LineStart as Integer, _
                                 Byval LineSkip as Integer, _
                                 Byval alpha as Integer, _
                                 Byval buffer as any ptr = 0 )

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_vert step LineSkip
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), 0
    Next
    Put buffer, (0, 0), TempBuffer, alpha, alpha
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_vert step LineSkip
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), 0
    Next
    Put (0, 0), TempBuffer, alpha, alpha
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_CustomScanlines_V ( Byval LineStart as Integer, _
                                 Byval LineSkip as Integer, _
                                 Byval alpha as Integer, _
                                 Byval buffer as any ptr = 0 )

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_horiz step LineSkip
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), 0
    Next
    Put buffer, (0, 0), TempBuffer, alpha, alpha
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_horiz step LineSkip
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), 0
    Next
    Put (0, 0), TempBuffer, alpha, alpha
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_CustomScanlines_add_H ( Byval LineStart as Integer, _
                                     Byval LineSkip as Integer, _
                                     Byval add_blend as Integer, _
                                     Byval buffer as any ptr = 0 )

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_vert step LineSkip
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), RGB(255, 255, 255)
    Next
    Put buffer, (0, 0), TempBuffer, add, add_blend
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_vert step LineSkip
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), RGB(255, 255, 255)
    Next
    Put (0, 0), TempBuffer, add, add_blend
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_CustomScanlines_add_V ( Byval LineStart as Integer, _
                                     Byval LineSkip as Integer, _
                                     Byval add_blend as Integer, _
                                     Byval buffer as any ptr = 0 )

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_horiz step LineSkip
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), RGB(255, 255, 255)
    Next
    Put buffer, (0, 0), TempBuffer, add, add_blend
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_horiz step LineSkip
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), RGB(255, 255, 255)
    Next
    Put (0, 0), TempBuffer, add, add_blend
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_CustomScanlines_color_H ( Byval LineStart as Integer, _
                                       Byval LineSkip as Integer, _
                                       Byval col as integer, _
                                       Byval alpha_level as integer, _
                                       Byval buffer as any ptr = 0 )

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_vert step LineSkip
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), col
    Next
    Put buffer, (0, 0), TempBuffer, alpha, alpha_level
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_vert step LineSkip
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), col
    Next
    Put (0, 0), TempBuffer, alpha, alpha_level
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_CustomScanlines_color_V ( Byval LineStart as Integer, _
                                       Byval LineSkip as Integer, _
                                       Byval col as integer, _
                                       Byval alpha_level as integer, _
                                       Byval buffer as any ptr = 0 )

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_horiz step LineSkip
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), col
    Next
    Put buffer, (0, 0), TempBuffer, alpha, alpha_level
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_horiz step LineSkip
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), col
    Next
    Put (0, 0), TempBuffer, alpha, alpha_level
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_CustomScanlines_color_add_H ( Byval LineStart as Integer, _
                                           Byval LineSkip as Integer, _
                                           Byval col as integer, _
                                           Byval add_level as integer, _
                                           Byval buffer as any ptr = 0 )

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_vert step LineSkip
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), col
    Next
    Put buffer, (0, 0), TempBuffer, add, add_level
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_vert step LineSkip
      Line TempBuffer, (0, Scanline)-(Gfx_horiz, Scanline), col
    Next
    Put (0, 0), TempBuffer, add, add_level
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub


'__________________________________________________________________________
Sub AFlibIII_CustomScanlines_color_add_V ( Byval LineStart as Integer, _
                                           Byval LineSkip as Integer, _
                                           Byval col as integer, _
                                           Byval add_level as integer, _
                                           Byval buffer as any ptr = 0 )

Dim as Integer Gfx_horiz, Gfx_vert, Scanline
Dim as any ptr TempBuffer

'Dim ScanMarker as any ptr = ImageCreate (Gfx_horiz, 1, 0)


ScreenLock
  If not buffer = 0 then
    Private4_check_buffersize buffer, Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_horiz step LineSkip
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), col
    Next
    Put buffer, (0, 0), TempBuffer, add, add_level
  else
    ScreenInfo Gfx_horiz, Gfx_vert
    TempBuffer = Imagecreate(Gfx_horiz, Gfx_vert, RGB(255, 0, 255))
    For Scanline = LineStart to Gfx_horiz step LineSkip
      Line TempBuffer, (Scanline, 0)-(Scanline, Gfx_vert), col
    Next
    Put (0, 0), TempBuffer, add, add_level
  End if
ScreenUnlock

ImageDestroy TempBuffer
TempBuffer = 0

End Sub



'__________________________________________________________________________
Sub AFlibIII_glow32 ( Byval Deepness_of_the_Glow as Integer, _
                      Byval Glow_Brightness as Integer, _
                      Byval Glow_Level as Integer = 255, _
                      Byval buffer as any PTR = 0 )
                      
'----------------------------------------------------------------------
' BIG special thanks to Richard Eric M. Lope for this OUTSTANDING glow
' routine!!!  d=^-^=b !!
'----------------------------------------------------------------------

dim as integer GFX_x, GFX_y, GFX_depth
ScreenInfo GFX_x, GFX_y, GFX_depth

If GFX_depth < 24 then Exit Sub

If Deepness_of_the_Glow < 0 then Deepness_of_the_Glow = 0
If Deepness_of_the_Glow > 8 then Deepness_of_the_Glow = 8

If Glow_Level < 0 then Glow_Level = 0
If Glow_Level > 255 then Glow_Level = 255

If not buffer = 0 then
  Private4_check_buffersize buffer, GFX_x, GFX_y
End if

dim as FB.Image ptr image = imagecreate(GFX_x, GFX_y)
var image_ptr = cast(uinteger ptr, image+1)

dim as FB.Image ptr image2 = imagecreate(GFX_x, GFX_y)
var image_ptr2 = cast(uinteger ptr, image2+1)

get buffer, (0, 0)-(GFX_x - 1, GFX_y - 1), image

blur_h(image_ptr, image_ptr2, GFX_x, GFX_y, Deepness_of_the_Glow * 2)
blur_v(image_ptr2, image_ptr, GFX_x, GFX_y, Deepness_of_the_Glow * 2)
blur_h(image_ptr, image_ptr2, GFX_x, GFX_y, Deepness_of_the_Glow)
blur_v(image_ptr2, image_ptr, GFX_x, GFX_y, Deepness_of_the_Glow)

ScreenLock
For Times as Integer = 0 to Glow_Brightness
  put buffer, (0, 0), image, add, Glow_Level
Next
ScreenUnlock

ImageDestroy image
ImageDestroy image2
End Sub


'__________________________________________________________________________
Sub AFlibIII_blur32 ( Byval Deepness_of_the_Blur as Integer, _
                      Byval Blur_Level as Integer, _
                      Byval buffer as any PTR = 0 )
                      
'----------------------------------------------------------------------
' BIG special thanks to Richard Eric M. Lope for this OUTSTANDING blur
' routine!!!  d=^-^=b !!
'----------------------------------------------------------------------

dim as integer GFX_x, GFX_y, GFX_depth
ScreenInfo GFX_x, GFX_y, GFX_depth

If GFX_depth < 24 then Exit Sub

If Deepness_of_the_Blur < 0 then Deepness_of_the_Blur = 0
If Deepness_of_the_Blur > 8 then Deepness_of_the_Blur = 8

If not buffer = 0 then
  Private4_check_buffersize buffer, GFX_x, GFX_y
End if

dim as FB.Image ptr image = imagecreate(GFX_x, GFX_y)
var image_ptr = cast(uinteger ptr, image+1)

dim as FB.Image ptr image2 = imagecreate(GFX_x, GFX_y)
var image_ptr2 = cast(uinteger ptr, image2+1)

get buffer, (0, 0)-(GFX_x - 1, GFX_y - 1), image

blur_h(image_ptr, image_ptr2, GFX_x, GFX_y, Deepness_of_the_Blur * 2)
blur_v(image_ptr2, image_ptr, GFX_x, GFX_y, Deepness_of_the_Blur * 2)
blur_h(image_ptr, image_ptr2, GFX_x, GFX_y, Deepness_of_the_Blur)
blur_v(image_ptr2, image_ptr, GFX_x, GFX_y, Deepness_of_the_Blur)

ScreenLock
For Times as Integer = 0 to Blur_Level
  put buffer, (0, 0), image, trans
Next
ScreenUnlock

ImageDestroy image
ImageDestroy image2
End Sub


Private Sub Private4_check_buffersize ( Byval buffer As Any Ptr, Byref x As Integer, _
                                        Byref y As Integer )

'----------------------------------------------------------------------
' BIG special thanks to cha0s on this *excellent* buffer detection
' routine!!!  d=^-^=b !!
'----------------------------------------------------------------------

x = 0: y = 0
If buffer = 0 Then Exit Sub

If isBufferNew_e( buffer ) Then
  x = cast( Uinteger Ptr, buffer )[2]
  y = cast( Uinteger Ptr, buffer )[3]
else
  x = cast( Ushort Ptr, buffer )[0] Shr 3
  y = cast( Ushort Ptr, buffer )[1]
end If
        
end sub


Private Sub blur_h(byval source as uinteger ptr, byval dest as uinteger ptr,_
                   byval wid as integer, byval hei as integer, byval radius as uinteger)
    dim as uinteger x, y, pixel
    dim as uinteger r, g, b, a
    dim as uinteger divisor
    dim as integer kx
    divisor = (radius * 2 + 1)
        for y = 0 to hei -1
            for x = 0 to wid - 1                               
                r = 0
                g = 0
                b = 0
                a = source[y * wid + x] shr 24
                for kx = -radius to radius
                    pixel = source[y * wid + x + kx]
                    r += (pixel shr 16 )
                    g += (pixel shr 8 and 255)
                    b += (pixel and 255)
                next kx                               
                r \=divisor
                g \=divisor
                b \=divisor
                dest[y * wid+x] = a shl 24 or r shl 16 or g shl 8 or b
            next x
        next y
end sub
 
Private Sub blur_v(byval source as uinteger ptr, byval dest as uinteger ptr,_
                   byval wid as integer, byval hei as integer, byval radius as uinteger)
    dim as uinteger x, y, pixel
    dim as uinteger r, g, b, a
    dim as uinteger divisor
    dim as integer ky
    divisor = (radius * 2 + 1)
 
      for x  = 0 to wid - 1         
            for y = radius to (hei - 1)  - radius                               
                r = 0
                g = 0
                b = 0
                a = source[y * wid + x] shr 24
                for ky = -radius to radius
                    pixel = source[x + (y + ky)*wid]                                       
                    r += (pixel shr 16 )
                    g += (pixel shr 8 and 255)
                    b += (pixel and 255)
                next ky                     
                r \=divisor
                g \=divisor
                b \=divisor
                dest[x + y*wid] = a shl 24 or r shl 16 or g shl 8 or b
            next y           
      next x
         
end sub
