Advertisement
UEZ

_GDIPlus_BitmapApplyFilter v0.9.8 build 2024-04-17 beta

UEZ
May 14th, 2017 (edited)
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
FreeBasic 156.11 KB | None | 0 0
  1. 'Coded by UEZ build 2024-04-17 beta
  2. 'Good source: http://www.codeproject.com/Articles/781213/Fundamentals-of-Image-Processing-behind-the-scenes by Jakub Szymanowski
  3.  
  4.  
  5. #ifdef __FB_64BIT__
  6.     #cmdline "-m _GDIPlus_BitmapApplyFilter_x64 -dll -export -gen gcc -Wc -Ofast -Wc -march=native -Wc -funroll-loops -Wc -mfpmath=sse"
  7. #else
  8.     #cmdline "-m _GDIPlus_BitmapApplyFilter -dll -export -gen gcc -Wc -Ofast -Wc -march=native -Wc -funroll-loops -Wc -mfpmath=sse"
  9. #endif
  10.  
  11. '#Include Once "crt\stdlib.bi"
  12. '#Include Once "crt\math.bi"
  13. '#Include Once "win\gdiplus.bi"
  14. '#Include Once "win\winuser.bi"
  15. '#Include Once "windows.bi"
  16.  
  17. #include once "fbgfx.bi"
  18. #include once "delaunay.bi"
  19. #include once "poisson-sampler.bi"
  20.  
  21. #ifdef __FB_64BIT__
  22.     #inclib "gdiplus"
  23.     #include once "win/gdiplus-c.bi"
  24. #else
  25.     #include once "win/gdiplus.bi"
  26.     Using Gdiplus
  27. #endif
  28.  
  29.  
  30. #define CRLF (Chr(13, 10))
  31. #define _Red(c)     ((c And &h00FF0000) Shr 16)
  32. #define _Green(c)   ((c And &h0000FF00) Shr 8)
  33. #define _Blue(c)    ((c And &h000000FF))
  34. #define Map(Val, source_start, source_stop, dest_start, dest_stop)   ((Val - source_start) * (dest_stop - dest_start) / (source_stop - source_start) + dest_start)
  35.    
  36. Public Const sVersion = "v0.9.8 build 2024-04-17 beta", fPi = Acos(-1), fPiSqr = Sqr(2 * fPi), f2Pi = 2 * fPi, fRad = fPi / 180
  37.  
  38. Type tagPalette
  39.     Flags As ULong
  40.     Count As ULong
  41.     ARGB(256) As ULong
  42. End Type
  43.    
  44. Extern "Windows-MS"
  45.  
  46. Enum Filters
  47.         EMBOSS1 = 1, EMBOSS2, EMBOSS3, EMBOSS4, SHARPEN1, BOX_BLUR, GAUSSIAN_BLUR, TRIANGLE_BLUR, UNSHARP, UNSHARP5x5, _
  48.         EDGE_DETECTION1, EDGE_DETECTION2, EDGE_DETECTION3, EDGE_DETECTION4, EDGE_DETECTION5, EDGE_DETECTION6, _
  49.         ANOTHER_BLUR, MOTION_BLUR, SHARPEN2, SOBEL, LAPLACE3x3_1, LAPLACE3x3_2, LAPLACE5x5, PREWITT, KIRSCH, _
  50.         OUTLINE3X3, GAUSSIAN5X5_1, GAUSSIAN5X5_2, LAPLACIANOFGAUSSIAN, SOVELVSPREWITT, GAUSSIAN3X3
  51. End Enum
  52.  
  53. 'Jarvis, Judice, and Ninke
  54. Dim Shared As Single matrixJJN(0 To ..., 0 To ...) = {{0,       0,          0,          7/48,       5/48}, _
  55.                                                       {3/48,    5/48,       7/48,       5/48,       3/48}, _
  56.                                                       {1/48,    3/48,       5/48,       3/48,       1/48}}
  57. 'Atkinson
  58. Dim Shared As Single matrixAtkinson(0 To ..., 0 To ...) = {{0,      0,          1/8,            1/8}, _
  59.                                                            {1/8,    1/8,        1/8,            0}, _
  60.                                                            {0,      1/8,        0,              0}}
  61. 'Burkes
  62. Dim Shared As Single matrixBurkes(0 To ..., 0 To ...) = {{0,        0,          0,          8/32,       4/32}, _  
  63.                                                          {2/32,     4/32,       8/32,       4/32,       2/32}}
  64. 'Two-Row Sierra
  65. Dim Shared As Single matrixSierra2(0 To ..., 0 To ...) = {{0,       0,          0,          4/16,       3/16}, _  
  66.                                                           {1/16,    2/16,       3/16,       2/16,       1/16}}
  67. 'Three-Row Sierra
  68. Dim Shared As Single matrixSierra3(0 To ..., 0 To ...) = {{0,       0,          0,          5/32,       3/32}, _
  69.                                                           {2/32,    4/32,       5/32,       4/32,       2/32}, _
  70.                                                           {0,       2/32,       3/32,       2/32,       0}}
  71. 'Floyd-Steinberg
  72. Dim Shared As Single matrixFS(0 To ..., 0 To ...) = {{0,        0,          7/16}, _
  73.                                                      {3/16,     5/16,       1/16}}     
  74. 'Stucki
  75. Dim Shared As Single matrixStucki(0 To ..., 0 To ...) = {{0,    0,      0,      8/42,   4/42}, _
  76.                                                         {2/424/42,   8/42,   4/42,   2/42}, _
  77.                                                         {1 / 42,    2 / 42,     4 / 42,     2 / 42,     1 / 42}}
  78. Dim Shared As Single matrixErrorDiffusion(0 To ..., 0 To ...) = _
  79.                                                                 {{0,        0,          0,          2 / 14,     1 / 14}, _
  80.                                                                  {0,        2 / 14,     2 / 14,     2 / 14,     0     }, _
  81.                                                                  {1 / 14,   0,          1 / 14,     0,          1 / 14}}
  82.  
  83. 'Bayer-method ordered dither
  84. Dim Shared As Single matrixBayer(0 To ..., 0 To ...) = _
  85.                                                         {{ 0, 32,  8, 40,  2, 34, 10, 42}, _
  86.                                                         {48, 16, 56, 24, 50, 18, 58, 26}, _
  87.                                                         {12, 44,  4, 36, 14, 46,  6, 38}, _
  88.                                                         {60, 28, 52, 20, 62, 30, 54, 22}, _
  89.                                                         { 3, 35, 11, 43,  1, 33,  9, 41}, _
  90.                                                         {51, 19, 59, 27, 49, 17, 57, 25}, _
  91.                                                         {15, 47,  7, 39, 13, 45,  5, 37}, _
  92.                                                         {63, 31, 55, 23, 61, 29, 53, 21}}
  93.                                                                                            
  94. Declare Function QCompare cdecl (ByVal e1 As Any Ptr, ByVal e2 As Any Ptr) As Integer
  95. Declare Function __DeltaE(iR1 As Long, iG1 As Long, iB1 As Long, iR2 As Long, iG2 As Long, iB2 As Long) As Single
  96. Declare Function _GDIPlus_ImageCountColors(himage As Any Ptr) As ULong
  97. Declare Function _GDIPlus_BitmapGetAverageColorValue(hImage As Any Ptr, bNegRGB As BOOL) As ULong
  98. Declare Function _GDIPlus_BitmapCreateBW(hImage As Any Ptr, iThreshold As UShort, bGDI As BOOL = False) As Any Ptr
  99. Declare Function _GDIPlus_BitmapCreateGreyscale(hImage As Any Ptr, bGDI As BOOL = False) As Any Ptr
  100. Declare Function _GDIPlus_BitmapCreateNegative(hImage As Any Ptr, bGDI As BOOL = False) As Any Ptr
  101. Declare Function _GDIPlus_BitmapCreateInverseGreyscale(hImage As Any Ptr, iThreshold As ULong) As Any Ptr
  102. Declare Function _GDIPlus_BitmapCreateFakeGreyscale(hImage As Any Ptr, bGDI As BOOL = False) As Any Ptr
  103. Declare Function _GDIPlus_BitmapCreateInverseBW(hImage As Any Ptr, iThreshold As UByte) As Any Ptr
  104. Declare Function _GDIPlus_BitmapApplyFilter_Blur(ByVal hImage As Any Ptr, iRadius As UByte) As Any Ptr
  105. Declare Function _Min3(fR As Single, fG As Single, fB As Single) As Single
  106. Declare Function _Max3(fR As Single, fG As Single, fB As Single) As Single
  107. Declare Function FindNearestColor(iColor As ULong, ByRef tColorPalette As tagPalette Ptr, iColors As ULong) As ULong
  108. Declare Function PlusTruncate(a As UByte, b As Single) As UByte
  109. Declare Sub CopyArray(a() As Single, b() As Single)
  110. Declare Sub drawTriangles(hImageSource As Any Ptr, hImageDestination As Any Ptr, v() As DTVertex, t() As DTTriangle, tcount As Long, bShowEdges As BOOL = False, iAlpha As UByte = &h60, bWireframe As BOOL = False)
  111.  
  112.  
  113. Declare Function _GDIPlus_BitmapApplyFilter_SymmetricNearestNeighbour(ByVal hImage As Any Ptr, fRadius As Single, bGDI As BOOL) As Any Ptr
  114. Declare Function _GDIPlus_BitmapApplyFilter_Jitter(ByVal hImage As Any Ptr, iAmount As ULong, bGDI As BOOL) As Any Ptr
  115. Declare Function _GDIPlus_BitmapApplyFilter_Median(ByVal hImage As Any Ptr, fRadius As Single, bGDI As BOOL) As Any Ptr
  116. Declare Function _GDIPlus_BitmapApplyFilter_Kuwahara(ByVal hImage As Any Ptr, iSize As ULong, bGDI As BOOL) As Any Ptr
  117. Declare Function _GDIPlus_BitmapApplyFilter_Edges(ByVal hImage As Any Ptr, bMode As UByte, bInverse As BOOL, bGDI As BOOL) As Any Ptr
  118. Declare Function _GDIPlus_BitmapApplyFilter_Pointillism(ByVal hImage As Any Ptr, iRounds As ULong, iSize As ULong, iA As UByte, bBorder As BOOL, bGDI As BOOL) As Any Ptr
  119. Declare Function _GDIPlus_BitmapApplyFilter_Linellism(ByVal hImage As Any Ptr, iRounds As ULong, iSize As ULong, iA As UByte, iMode As UByte, bBorder As BOOL, bGDI As BOOL) As Any Ptr
  120. Declare Function _GDIPlus_BitmapApplyFilter_Convolution(ByVal hImage As Any Ptr, fFactor As Single, fBias As Single, iMode As UByte, pMStruct As Any Ptr, iMatrix As UShort, bGDI As BOOL) As Any Ptr
  121. Declare Function _GDIPlus_BitmapApplyFilter_Raster(ByVal hImage As Any Ptr, iSizeW As ULong, iSizeH As ULong, fDensity As Single, fBrightness As Single, fBias As Single, iMode As Byte, bGDI As BOOL) As Any Ptr
  122. Declare Function _GDIPlus_BitmapApplyFilter_Rasterize(ByVal hImage As Any Ptr, iSpaceX As ULong, iSpaceY As ULong, iDelCol As ULong, bGDI As BOOL) As Any Ptr
  123. Declare Function _GDIPlus_BitmapApplyFilter_Pixelate(ByVal hImage As Any Ptr, iPixelate As UByte, bGrid As BOOL, bGDI As BOOL) As Any Ptr
  124. Declare Function _GDIPlus_BitmapApplyFilter_Dilatation(ByVal hImage As Any Ptr, Size As UByte, bGDI As BOOL) As Any Ptr
  125. Declare Function _GDIPlus_BitmapApplyFilter_Erosion(ByVal hImage As Any Ptr, Size As UByte, bGDI As BOOL) As Any Ptr
  126. Declare Function _GDIPlus_BitmapApplyFilter_OilPainting(ByVal hImage As Any Ptr, iRadius As UByte, fIntensityLevels As Single, bGDI As BOOL) As Any Ptr
  127. Declare Function _GDIPlus_BitmapApplyFilter_ColorAccent(ByVal hImage As Any Ptr, iHue As UShort, fRange As Single, bGDI As BOOL) As Any Ptr
  128. Declare Function _GDIPlus_BitmapApplyFilter_PenSketch(ByVal hImage As Any Ptr, iThreshold As Single, bGDI As BOOL) As Any Ptr
  129. Declare Function _GDIPlus_BitmapApplyFilter_PenSketch2(ByVal hImage As Any Ptr, iThreshold As UByte, bGDI As BOOL) As Any Ptr
  130. Declare Function _GDIPlus_BitmapApplyFilter_Cartoon1(ByVal hImage As Any Ptr, iRadius As UByte, fIntensityLevels As Single, iThreshold As UByte, bGDI As BOOL) As Any Ptr
  131. Declare Function _GDIPlus_BitmapApplyFilter_TiltShift(ByVal hImage As Any Ptr, fPosY_Start As Single, iIntensity As UByte, bGDI As BOOL) As Any Ptr
  132. Declare Function _GDIPlus_BitmapApplyFilter_RadialBlur(ByVal hImage As Any Ptr, fPosX As Single, fPosY As Single, fRadius As Single, iIntensity As UByte, bGDI As BOOL) As Any Ptr
  133. Declare Function _GDIPlus_BitmapApplyFilter_TimeWarp(ByVal hImage As Any Ptr, fFactor As Single, fMidX As Single, fMidY As Single, bGDI As BOOL) As Any Ptr
  134. Declare Function _GDIPlus_BitmapApplyFilter_FishEye(ByVal hImage As Any Ptr, bGDI As BOOL) As Any Ptr
  135. Declare Function _GDIPlus_BitmapApplyFilter_Wave(ByVal hImage As Any Ptr, fAmplitudeX As Single, fAmplitudeY As Single, fFrequencyX As Single, fFrequencyY As Single, bGDI As BOOL) As Any Ptr
  136. Declare Function _GDIPlus_BitmapApplyFilter_Swirl(ByVal hImage As Any Ptr, fDegree As Single, bGDI As BOOL) As Any Ptr
  137. Declare Function _GDIPlus_BitmapApplyFilter_XRay(ByVal hImage As Any Ptr, iBias As Byte, bInvert As BOOL, bGDI As BOOL) As Any Ptr
  138. Declare Function _GDIPlus_BitmapApplyFilter_Median2(ByVal hImage As Any Ptr, fRadius As Single, bGDI As BOOL) As Any Ptr
  139. Declare Function _GDIPlus_BitmapApplyFilter_BWJJNDithering(ByVal hImage As Any Ptr, fErrorMultiplier As Single, iThreshold As UByte, bGDI As BOOL) As Any Ptr
  140. Declare Function _GDIPlus_BitmapApplyFilter_BWBayerDithering(ByVal hImage As Any Ptr, bGDI As BOOL) As Any Ptr
  141. Declare Function _GDIPlus_BitmapApplyFilter_Indexed(ByVal hImage As Any Ptr, iColors As ULong, bDither As BOOL, iDitherType As UByte, bGDI As BOOL) As Any Ptr
  142. Declare Function _GDIPlus_BitmapApplyFilter_Mosaic(ByVal hImage As Any Ptr, iSites As ULong, bOrdered As BOOL, bBorder As BOOL, iCalcMode As UByte, iBorderColor As ULong, bGDI As BOOL) As Any Ptr
  143. Declare Function _GDIPlus_BitmapApplyFilter_WaterDropGlassPane(ByVal hImage As Any Ptr, iPosX As UShort, iPosY As UShort, iAmount As UShort, iSizeMin As UByte, iSizeMax As UShort, iBlur As UByte, bGDI As BOOL) As Any Ptr
  144. Declare Function _GDIPlus_BitmapApplyFilter_Delaunay(ByVal hImage As Any Ptr, iBlur As UByte, fSobel As Single, iBW As UByte, iSpaceX As ULong, iSpaceY As ULong, iBorderSpaceX As UByte, iBorderSpaceY As UByte, _
  145.                                                      bRndPoints As BOOL, iRndPoints As ULong, bShowEdges As BOOL, iAlpha As UByte, bWireframe As BOOL, bGDI As BOOL) As Any Ptr
  146. Declare Function _GDIPlus_BitmapApplyFilter_Spiral(ByVal hImage As Any Ptr, iMode As UByte, iBgColor As ULong, bGreyScale As BOOL, bGDI As BOOL) As Any Ptr
  147.  
  148. Sub Ver() Export
  149.     MessageBoxEx(NULL,  "_GDIPlus_BitmapApplyFilter.dll" & CRLF & CRLF & _
  150.                         sVersion & CRLF & CRLF & CRLF & _
  151.                         "Coded by UEZ" & CRLF & CRLF & CRLF & _
  152.                         "Credits to:" & CRLF & _
  153.                         "* Jakub Szymanowski" & CRLF & _
  154.                         "* rdc" & CRLF & _
  155.                         "* Dewald Esterhuizen" & CRLF & _
  156.                         "* Santhosh G_ " & CRLF & _
  157.                         "* Christian Graus" & CRLF & _
  158.                         "* paul doe" & CRLF & _
  159.                         "* www.gutgames.com" & CRLF & _
  160.                         "* D.J. Peters", _
  161.                         "DLL Information", MB_ICONINFORMATION Or MB_OK Or MB_APPLMODAL Or MB_TOPMOST, 1033)
  162. End Sub
  163.  
  164. 'Perlin Noise by Joshy aka D.J. Peters
  165. Type REAL As Single
  166. #define rAbs(x_)                IIf( (x_) < 0, -(x_), (x_) )
  167. Const As REAL rPI               = Acos(-1)
  168. Const As REAL rDeg2Rad          = rPI / 180
  169.  
  170. Type PERLINNOISE  '...'
  171.     Declare Constructor
  172.     Declare Sub NoiseSeed(ByVal seed As Double)
  173.     Declare Sub NoiseDetail(ByVal lod As Integer)
  174.     Declare Sub NoiseDetail(ByVal lod As Integer, ByVal falloff As REAL)
  175.     Declare Function Noise1D(ByVal x As REAL) As REAL
  176.     Declare Function Noise2D(ByVal x As REAL,ByVal y As REAL) As REAL
  177.     Declare Function Noise3D(ByVal x As REAL,ByVal y As REAL,ByVal z As REAL) As REAL
  178. Private:
  179.     Const As REAL    SINCOS_PRECISION = 0.5
  180.     Const As Integer SINCOS_LENGTH    = (360 / SINCOS_PRECISION)
  181.     Const As Integer PERLIN_YWRAPB    = 4
  182.     Const As Integer PERLIN_YWRAP     = 1 Shl PERLIN_YWRAPB
  183.     Const As Integer PERLIN_ZWRAPB    = 8
  184.     Const As Integer PERLIN_ZWRAP     = 1 Shl PERLIN_ZWRAPB
  185.     Const As Integer PERLIN_SIZE      = 4095
  186.     Const As Integer PERLIN_TWOPI     = SINCOS_LENGTH
  187.     Const As Integer PERLIN_PI        = PERLIN_TWOPI Shr 1
  188.     As Integer perlin_octaves         = 4   ' default To medium smooth
  189.     As REAL  perlin_amp_falloff       = 0.5 ' 50% reduction/octave
  190.     As REAL  perlin_cosTable(SINCOS_LENGTH-1)
  191.     As REAL  perlin(PERLIN_SIZE)
  192.     Declare Sub reInit
  193.     Declare Function noise_fsc(ByVal i As REAL) As REAL
  194. End Type
  195.  
  196. Constructor PERLINNOISE  '...'
  197.     For i As Integer = 0 To SINCOS_LENGTH - 1
  198.         perlin_cosTable(i) = Cos(i * rDeg2Rad * SINCOS_PRECISION)
  199.     Next
  200.     reInit
  201. End Constructor
  202.  
  203. Sub PERLINNOISE.reInit  '...'
  204.     Randomize
  205.     For i As Integer = 0 To PERLIN_SIZE
  206.         perlin(i) = Rnd()
  207.     Next
  208. End Sub
  209.  
  210. Function PERLINNOISE.noise_fsc(ByVal i As REAL) As REAL  '...'
  211.     Dim As Integer index = Int(i * PERLIN_PI)
  212.     Return 0.5 * (1.0 - perlin_cosTable(index Mod SINCOS_LENGTH))
  213. End Function
  214.  
  215. Sub PERLINNOISE.noiseSeed(ByVal seed As Double)  '...'
  216.     'Randomize(0) ' !!!
  217.     Randomize(seed) : reInit
  218. End Sub
  219.  
  220. Sub PERLINNOISE.noiseDetail(ByVal lod As Integer)  '...'
  221.     If (lod > 0) Then perlin_octaves = lod
  222. End Sub
  223.  
  224. Sub PERLINNOISE.noiseDetail(ByVal lod As Integer, ByVal falloff As REAL)  '...'
  225.     If (lod > 0) Then perlin_octaves = lod
  226.     If (falloff > 0) Then perlin_amp_falloff = falloff
  227. End Sub
  228.  
  229. Function PERLINNOISE.Noise1D(ByVal x As REAL) As REAL  '...'
  230.     Return noise3D(x, 0, 0)
  231. End Function
  232.  
  233. Function PERLINNOISE.Noise2D(ByVal x As REAL, ByVal y As REAL) As REAL  '...'
  234.     Return noise3D(x, y, 0)
  235. End Function
  236.  
  237. Function PERLINNOISE.Noise3D(ByVal x As REAL,ByVal y As REAL,ByVal z As REAL) As REAL  '...'
  238.     x = rAbs(x) : y = rAbs(y) : z = rAbs(z)
  239.     Dim As Integer xi = Int(x), yi = Int(y), zi = Int(z)
  240.     Dim As REAL xf = x - xi, yf = y - yi, zf = z - zi
  241.     Dim As REAL r, ampl = 0.5
  242.     For i As Integer = 0 To perlin_octaves - 1
  243.         Dim As Integer of= xi + (yi Shl PERLIN_YWRAPB) + (zi Shl PERLIN_ZWRAPB)
  244.         Dim As REAL rxf = noise_fsc(xf)
  245.         Dim As REAL ryf = noise_fsc(yf)
  246.         Dim As REAL n1 = perlin(of And PERLIN_SIZE)
  247.         n1 += rxf * (perlin((of + 1) And PERLIN_SIZE) - n1)
  248.         Dim As REAL n2 = perlin((of + PERLIN_YWRAP) And PERLIN_SIZE)
  249.         n2 += rxf * (perlin((of + PERLIN_YWRAP + 1) And PERLIN_SIZE) - n2)
  250.         n1 += ryf * (n2 - n1)
  251.         of += PERLIN_ZWRAP
  252.         n2  = perlin(of And PERLIN_SIZE)
  253.         n2 += rxf * (perlin((of + 1) And PERLIN_SIZE) - n2)
  254.         Dim As REAL n3 = perlin((of + PERLIN_YWRAP) And PERLIN_SIZE)
  255.         n3 += rxf * (perlin((of + PERLIN_YWRAP + 1) And PERLIN_SIZE) - n3)
  256.         n2 += ryf * (n3 - n2)
  257.         n1 += noise_fsc(zf) * (n2 - n1)
  258.         r += n1 * ampl
  259.         ampl *= perlin_amp_falloff
  260.         xi Shl = 1: xf *= 2
  261.         yi Shl = 1: yf *= 2
  262.         zi Shl = 1: zf *= 2
  263.         If (xf >= 1) Then xi += 1 : xf -= 1
  264.         If (yf >= 1) Then yi += 1 : yf -= 1
  265.         If (zf >= 1) Then zi += 1 : zf -= 1
  266.     Next
  267.     Return r
  268. End Function
  269. 'End Perlin Noise
  270.  
  271. 'The qsort function expects three numbers
  272. 'from the compare function:
  273. '-1: if e1 is less than e2
  274. '0: if e1 is equal to e2
  275. '1: if e1 is greater than e2
  276. Private Function QCompare cdecl (ByVal e1 As Any Ptr, ByVal e2 As Any Ptr) As Integer 'code by rdc '...'
  277.     Dim As Integer el1, el2
  278.     Static cnt As Integer
  279.    
  280.     'Get the call count and items passed
  281.     cnt += 1
  282.     'Get the values, must cast to integer ptr
  283.     el1 = *(CPtr(Integer Ptr, e1))
  284.     el2 = *(CPtr(Integer Ptr, e2))
  285.     'Print "Qsort called";cnt;" time(s) with";el1;" and";el2;"."
  286.     'Compare the Values
  287.     If el1 < el2 Then
  288.         Return -1
  289.     ElseIf el1 > el2 Then
  290.         Return 1
  291.     Else
  292.        Return 0
  293.     End If
  294. End Function
  295.  
  296. Private Function __DeltaE(iR1 As Long, iG1 As Long, iB1 As Long, iR2 As Long, iG2 As Long, iB2 As Long) As Single '...'
  297.     Return Sqr((iR1 - iR2) * (iR1 - iR2) + (iG1 - iG2) * (iG1 - iG2) + (iB1 - iB2) * (iB1 - iB2))
  298. End Function
  299.  
  300. Private Function _Min3(fRed As Single, fGreen As Single, fBlue As Single) As Single '...'
  301.     Dim As Single fSmallest = fRed
  302.     If fSmallest > fGreen Then fSmallest = fGreen
  303.     If fSmallest > fBlue Then fSmallest = fBlue
  304.     Return fSmallest
  305. End Function
  306.  
  307. Private Function _Max3(fRed As Single, fGreen As Single, fBlue As Single) As Single '...'
  308.     Dim As Single fBiggest = fRed
  309.     If fBiggest < fGreen Then fBiggest = fGreen
  310.     If fBiggest < fBlue Then fBiggest = fBlue
  311.     Return fBiggest
  312. End Function
  313.  
  314. 'https://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Quicksort
  315. Private Sub Quicksort(Array() As ULong, iStart As ULong, iEnd As ULong) '...'
  316.     Dim As UInteger i = iStart, j = iEnd, iPivot = Array((i + j) Shr 1)
  317.     While i <= j
  318.         While Array(i) > iPivot
  319.             i += 1
  320.         Wend
  321.         While Array(j) < iPivot
  322.             j -= 1
  323.         Wend
  324.         If i <= j Then
  325.             Swap Array(i), Array(j)
  326.             i += 1
  327.             j -= 1
  328.         End If
  329.     Wend
  330.     If j > iStart Then Quicksort(Array(), iStart, j)
  331.     If i < iEnd Then Quicksort(Array(), i, iEnd)
  332. End Sub
  333.  
  334. Private Function _GDIPlus_ImageGetPixelFormat(hImage As Any Ptr) As ULong '...'
  335.     Dim As Long iFormat
  336.     GdipGetImagePixelFormat(hImage, @iFormat)
  337.     Return iFormat
  338. End Function
  339.  
  340. Private Function _GDIPlus_BitmapGetAverageColorValue(hImage As Any Ptr, bNegRGB As BOOL) As ULong '...'
  341.     Dim As Single iW, iH
  342.     Dim As BitmapData tBitmapData
  343.     Dim As ULong iX, iY, iRowOffset, c, iCount, iA, iR, iG, iB
  344.     Dim As ULong sumA = 0, sumR = 0, sumG = 0, sumB = 0
  345.    
  346.     GdipGetImageDimension(hImage, @iW, @iH)
  347.    
  348.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  349.     iCount = iW * iH
  350.    
  351.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  352.     For iY = 0 To iH - 1
  353.         iRowOffset = iY * iW
  354.         For iX = 0 To iW - 1
  355.             c = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  356.             sumA += (c Shr 24) And &hFF
  357.             sumR += (c Shr 16) And &hFF
  358.             sumG += (c Shr 8) And &hFF
  359.             sumB +=  c And &hFF
  360.         Next
  361.     Next
  362.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  363.  
  364.     If bNegRGB Then
  365.         iA = CLng(sumA / iCount) Shl 24
  366.         iR = (&hFF - CLng(sumR / iCount)) Shl 16
  367.         iG = (&hFF - CLng(sumG / iCount)) Shl 8
  368.         iB = (&hFF - CLng(sumB / iCount))
  369.     Else
  370.         iA = CLng(sumA / iCount) Shl 24
  371.         iR = CLng(sumR / iCount) Shl 16
  372.         iG = CLng(sumG / iCount) Shl 8
  373.         iB = CLng(sumB / iCount)
  374.     EndIf
  375.  
  376.     Return iA + iR + iG + iB
  377. End Function
  378.  
  379. Private Function _GDIPlus_ImageCountColors32(himage As Any Ptr) As ULong 'slower variant but full 32-bit support '...'
  380.     Dim As Single iW, iH, iPixel, iRowOffset
  381.     GdipGetImageDimension(himage, @iW, @iH)
  382.     Dim As BitmapData tBitmapData
  383.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  384.     Dim As ULong aColors(0 To iW * iH - 1), c = 0, iX, iY
  385.     _GDIPlus_ImageGetPixelFormat(himage)
  386.     GdipBitmapLockBits(himage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  387.     For iY = 0 To iH - 1
  388.         iRowOffset = iY * iW
  389.         For iX = 0 To iW - 1
  390.             aColors(c) = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  391.             c += 1
  392.         Next
  393.     Next
  394.     GdipBitmapUnlockBits(himage, @tBitmapData)
  395.     Quicksort(aColors(), 0, c - 1)
  396.     c = 0
  397.     For iY = 0 To UBound(aColors) - 2
  398.         If aColors(iY) > aColors(iY + 1) Then c += 1
  399.     Next
  400.     Return c
  401. End Function
  402.  
  403. Private Function _GDIPlus_ImageCountColors24ASM(himage As Any Ptr) As ULong '...'
  404.     Dim As Single iW, iH, iPixel
  405.     GdipGetImageDimension(himage, @iW, @iH)
  406.     Dim As BitmapData tBitmapData
  407.     Dim As RECT tRect = Type(0, 0, iW, iH)
  408.     Dim As UInteger c = 0, iPixels = iW * iH - 1
  409.     Dim As UByte aColors()
  410.     ReDim aColors(0 To 256^3)
  411.    
  412.     GdipBitmapLockBits(himage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  413.     Dim As Integer Ptr pBmp = Cast(Any Ptr, tBitmapData.Scan0)
  414.     Dim As Byte Ptr pColors = @aColors(0)
  415.    
  416.     #ifndef __FB_64BIT__ '...'
  417.         #define REG_AX eax
  418.         #define REG_BX ebx
  419.         #define REG_CX ecx
  420.         #define REG_DX edx
  421.         #define REG_DI edi
  422.         #define REG_SI esi
  423.         #define REG_SP esp
  424.         #define REG_BP ebp
  425.     #else
  426.         #define REG_AX rax
  427.         #define REG_BX rbx
  428.         #define REG_CX rcx
  429.         #define REG_DX rdx
  430.         #define REG_DI rdi
  431.         #define REG_SI rsi
  432.         #define REG_SP rsp
  433.         #define REG_BP rbp
  434.     #endif
  435.    
  436.     Asm '...'
  437.         mov REG_SI, [pBmp]
  438.         mov REG_CX, [iPixels]
  439.         mov REG_DI, [pColors]
  440.         xor REG_AX, REG_AX
  441.         _Pixel_Count:
  442.         mov REG_BX, [REG_SI]
  443.         and REG_BX, &hFFFFFF
  444.         cmp byte ptr [REG_DI + REG_BX], 1
  445.         je _Next
  446.         add REG_AX, 1
  447.         mov byte ptr [REG_DI + REG_BX], 1
  448.         _Next:
  449.         add REG_SI, 4
  450.         sub REG_CX, 1
  451.             jnz _Pixel_Count
  452.             mov [c], REG_AX
  453.         End Asm
  454.        
  455.     GdipBitmapUnlockBits(himage, @tBitmapData)
  456.     Return c
  457. End Function
  458.  
  459. Function _GDIPlus_ImageCountColors(himage As Any Ptr) As ULong Export '...'
  460.     If (_GDIPlus_ImageGetPixelFormat(himage) And PixelFormatAlpha) Then Return _GDIPlus_ImageCountColors32(himage) 'check if image has alpha channel
  461.     Return _GDIPlus_ImageCountColors24ASM(himage)
  462. End Function
  463.  
  464. Function _GDIPlus_BitmapCreateBW(himage As Any Ptr, iThreshold As UShort, bGDI As BOOL = False) As Any Ptr Export '...'
  465.     Dim As Single iW, iH
  466.     Dim As Any Ptr hBitmap_BW, hGDIBitmap
  467.     Dim As BitmapData tBitmapData, tBitmapData_BW
  468.     Dim As Long iX, iY, iRowOffset, iColor, iR, iG, iB
  469.    
  470.     iThreshold = IIf(iThreshold < 0, 0, IIf(iThreshold > 255, 255, iThreshold))
  471.    
  472.     GdipGetImageDimension(himage, @iW, @iH)
  473.    
  474.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  475.    
  476.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_BW)
  477.     GdipBitmapLockBits(hBitmap_BW, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_BW)
  478.     GdipBitmapLockBits(himage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  479.    
  480.     For iY = 0 To iH - 1
  481.         iRowOffset = iY * iW
  482.         For iX = 0 To iW - 1
  483.           iColor = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  484.           iR = (iColor Shr 16) And &hFF
  485.             iG = (iColor Shr 8) And &hFF
  486.             iB = iColor And &hFF
  487.             If CLng((iR + iG + iB) / 3) >= iThreshold Then
  488.             Cast(ULong Ptr, tBitmapData_BW.Scan0)[iRowOffset + iX] = &hFFFFFFFF
  489.             Else
  490.                 Cast(ULong Ptr, tBitmapData_BW.Scan0)[iRowOffset + iX] = &hFF000000
  491.             End If
  492.         Next
  493.     Next
  494.    
  495.     GdipBitmapUnlockBits(hBitmap_BW, @tBitmapData_BW)
  496.     GdipBitmapUnlockBits(himage, @tBitmapData)
  497.     If bGDI Then
  498.         GdipCreateHBITMAPFromBitmap(hBitmap_BW, @hGDIBitmap, &hFF000000)
  499.         GdipDisposeImage(hBitmap_BW)
  500.         Return hGDIBitmap
  501.     EndIf
  502.     Return hBitmap_BW  
  503. End Function
  504.  
  505. Function _GDIPlus_BitmapCreateGreyscale(hImage As Any Ptr, bGDI As BOOL = False) As Any Ptr Export '...'
  506.     Dim As Single iW, iH
  507.     Dim As Any Ptr hBitmap_Greyscale, hGDIBitmap
  508.     Dim As BitmapData tBitmapData, tBitmapData_Greyscale
  509.     Dim As Long iX, iY, iRowOffset, iColor, c, iR, iG, iB
  510.    
  511.     GdipGetImageDimension(hImage, @iW, @iH)
  512.    
  513.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  514.    
  515.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Greyscale)
  516.     GdipBitmapLockBits(hBitmap_Greyscale, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Greyscale)
  517.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  518.    
  519.     For iY = 0 To iH - 1
  520.         iRowOffset = iY * iW
  521.         For iX = 0 To iW - 1
  522.           iColor = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  523.           iR = (iColor Shr 16) And &hFF
  524.             iG = (iColor Shr 8) And &hFF
  525.             iB = iColor And &hFF
  526.             c = CLng((iR * 213 + iG * 715 + iB * 72) / 1000)
  527.             Cast(ULong Ptr, tBitmapData_Greyscale.Scan0)[iRowOffset + iX] = &hFF000000 + (c Shl 16) + (c Shl 8) + c
  528.         Next
  529.     Next
  530.    
  531.     GdipBitmapUnlockBits(hBitmap_Greyscale, @tBitmapData_Greyscale)
  532.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  533.     If bGDI Then
  534.         GdipCreateHBITMAPFromBitmap(hBitmap_Greyscale, @hGDIBitmap, &hFF000000)
  535.         GdipDisposeImage(hBitmap_Greyscale)
  536.         Return hGDIBitmap
  537.     EndIf
  538.     Return hBitmap_Greyscale   
  539. End Function
  540.  
  541. Function _GDIPlus_BitmapCreateNegative(hImage As Any Ptr, bGDI As BOOL = False) As Any Ptr Export '...'
  542.     Dim As Single iW, iH
  543.     Dim As Any Ptr hBitmap_Negative, hGDIBitmap
  544.     Dim As BitmapData tBitmapData, tBitmapData_Negative
  545.     Dim As Long iX, iY, iRowOffset
  546.    
  547.     GdipGetImageDimension(hImage, @iW, @iH)
  548.    
  549.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  550.    
  551.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Negative)
  552.     GdipBitmapLockBits(hBitmap_Negative, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Negative)
  553.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  554.    
  555.     For iY = 0 To iH - 1
  556.         iRowOffset = iY * iW
  557.         For iX = 0 To iW - 1
  558.             Cast(ULong Ptr, tBitmapData_Negative.Scan0)[iRowOffset + iX] = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX] Xor &h00FFFFFF          
  559.         Next
  560.     Next
  561.    
  562.     GdipBitmapUnlockBits(hBitmap_Negative, @tBitmapData_Negative)
  563.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  564.     If bGDI Then
  565.         GdipCreateHBITMAPFromBitmap(hBitmap_Negative, @hGDIBitmap, &hFF000000)
  566.         GdipDisposeImage(hBitmap_Negative)
  567.         Return hGDIBitmap
  568.     EndIf
  569.     Return hBitmap_Negative
  570. End Function
  571.  
  572. Function _GDIPlus_BitmapCreateFakeGreyscale(hImage As Any Ptr, bGDI As BOOL = False) As Any Ptr Export
  573.     Dim As Single iW, iH
  574.     Dim As Double fGreys, fLuma
  575.     Dim As Any Ptr hBitmap_Greyscale, hGDIBitmap
  576.     Dim As BitmapData tBitmapData, tBitmapData_Greyscale
  577.     Dim As Long iX, iY, iRowOffset, iColor, iR, iG, iB
  578.    
  579.     GdipGetImageDimension(hImage, @iW, @iH)
  580.    
  581.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  582.    
  583.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Greyscale)
  584.     GdipBitmapLockBits(hBitmap_Greyscale, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Greyscale)
  585.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  586.     #define _Round2(x)      ((x * 10000 + 0.5) / 100 Shr 0) / 100
  587.    
  588.     For iY = 0 To iH - 1
  589.         iRowOffset = iY * iW
  590.         For iX = 0 To iW - 1
  591.             iColor = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  592.             iR = (iColor Shr 16) And &hFF
  593.             iG = (iColor Shr 8) And &hFF
  594.             iB = iColor And &hFF
  595.             fLuma = (iR * 213 + iG * 715 + iB * 72) / 1000
  596.             'fLuma = ((iR * 0.3) + (iG * 0.59) + (iB * 0.11) / 3)
  597.             fGreys = fLuma - Fix(fLuma)
  598.             fLuma = Fix(fLuma)
  599.             iR = 0
  600.             iG = 0
  601.             iB = 0
  602.             Select Case _Round2(fGreys)
  603.                 Case 0.05 To 0.18
  604.                     iB = 1
  605.                 Case 0.19 To 0.34
  606.                     iG = 1
  607.                 Case 0.35 To 0.50
  608.                     iB = 1
  609.                     iG = 1
  610.                 Case 0.51 To 0.66
  611.                     iR = 1
  612.                 Case 0.67 To 0.82
  613.                     iR = 1
  614.                     iB = 1
  615.                 Case 0.83 To 0.95
  616.                     iR = 1
  617.                     iG = 1
  618.             End Select
  619.             Cast(ULong Ptr, tBitmapData_Greyscale.Scan0)[iRowOffset + iX] = &hFF000000 Or ((fLuma + iR) Shl 16) Or ((fLuma + iG) Shl 8) Or (fLuma + iB) Shl 0
  620.         Next
  621.     Next
  622.    
  623.     GdipBitmapUnlockBits(hBitmap_Greyscale, @tBitmapData_Greyscale)
  624.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  625.     If bGDI Then
  626.         GdipCreateHBITMAPFromBitmap(hBitmap_Greyscale, @hGDIBitmap, &hFF000000)
  627.         GdipDisposeImage(hBitmap_Greyscale)
  628.         Return hGDIBitmap
  629.     EndIf
  630.     Return hBitmap_Greyscale
  631. End Function
  632.  
  633. Private Function _GDIPlus_BitmapCreateInverseGreyscale(hImage As Any Ptr, iThreshold As ULong) As Any Ptr 'based on original code by Jakub Szymanowski '...'
  634.     Dim As Single iW, iH
  635.     Dim As Any Ptr hBitmap_Inverse
  636.     Dim As BitmapData tBitmapData, tBitmapData_Inverse
  637.     Dim As Long iX, iY, iRowOffset, c, iDistance, iColor, iR, iG, iB
  638.    
  639.     GdipGetImageDimension(hImage, @iW, @iH)
  640.    
  641.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  642.    
  643.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Inverse)
  644.     GdipBitmapLockBits(hBitmap_Inverse, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Inverse)
  645.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  646.    
  647.     For iY = 0 To iH - 1
  648.         iRowOffset = iY * iW
  649.         For iX = 0 To iW - 1
  650.             iColor = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  651.             iR = (iColor Shr 16) And &hFF
  652.             iG = (iColor Shr 8) And &hFF
  653.             iB = iColor And &hFF
  654.             c = CLng((iR * 213 + iG * 715 + iB * 72) / 1000)
  655.             iDistance = Abs(iThreshold - c)
  656.             If c >= iThreshold Then iColor = iThreshold - iDistance
  657.             If c < iThreshold Then iColor = iThreshold + iDistance
  658.             c = IIf(c > 255, 255, IIf(c < 0, 0, c))
  659.             Cast(ULong Ptr, tBitmapData_Inverse.Scan0)[iRowOffset + iX] = &hFF000000 + c Shl 16 + c Shl 8 + c      
  660.         Next
  661.     Next
  662.    
  663.     GdipBitmapUnlockBits(hBitmap_Inverse, @tBitmapData_Inverse)
  664.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  665.     Return hBitmap_Inverse     
  666. End Function
  667.  
  668. Private Function _GDIPlus_BitmapCreateInverseBW(hImage As Any Ptr, iThreshold As UByte) As Any Ptr 'based on original code by Jakub Szymanowski '...'
  669.     Dim As Single iW, iH
  670.     Dim As Any Ptr hBitmap_InverseBW
  671.     Dim As BitmapData tBitmapData, tBitmapData_InverseBW
  672.     Dim As Long iX, iY, iRowOffset, c, iColor, iR, iG, iB
  673.    
  674.     GdipGetImageDimension(hImage, @iW, @iH)
  675.    
  676.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  677.    
  678.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_InverseBW)
  679.     GdipBitmapLockBits(hBitmap_InverseBW, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_InverseBW)
  680.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  681.    
  682.    
  683.     iThreshold = IIf(iThreshold < 1, 1, IIf(iThreshold > 254, 254, iThreshold))
  684.    
  685.    
  686.     For iY = 0 To iH - 1
  687.         iRowOffset = iY * iW
  688.         For iX = 0 To iW - 1
  689.             iColor = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  690.             iR = (iColor Shr 16) And &hFF
  691.             iG = (iColor Shr 8) And &hFF
  692.             iB = iColor And &hFF
  693.             c = CLng(CLng((iR * 213 + iG * 715 + iB * 72) / 1000))
  694.             If c > iThreshold Then
  695.                 iColor = &hFFFFFFFF
  696.             Else
  697.                 iColor = &hFF000000
  698.             EndIf
  699.             Cast(ULong Ptr, tBitmapData_InverseBW.Scan0)[iRowOffset + iX] = iColor
  700.         Next
  701.     Next
  702.    
  703.     GdipBitmapUnlockBits(hBitmap_InverseBW, @tBitmapData_InverseBW)
  704.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  705.     Return hBitmap_InverseBW       
  706. End Function
  707.  
  708. Private Function _GDIPlus_BitmapCreateSubtract(hImage1 As Any Ptr, hImage2 As Any Ptr, iBias As Byte, bInvert As BOOL) As Any Ptr 'based on original code by Dewald Esterhuizen '...'
  709.     Dim As Single iW1, iH1, iW2, iH2
  710.     Dim As Any Ptr hBitmap_Subtract
  711.     Dim As BitmapData tBitmapData1, tBitmapData2, tBitmapData_Subtract
  712.     Dim As Long iX, iY, iRowOffset, iColor1, iColor2, iR, iG, iB
  713.    
  714.     GdipGetImageDimension(hImage1, @iW1, @iH1)
  715.     GdipGetImageDimension(hImage2, @iW2, @iH2)
  716.     If iW1 <> iW2 Or iH1 <> iH2 Then Return 0
  717.    
  718.     Dim As RECT tRect = Type(0, 0, iW1 - 1, iH1 - 1)
  719.    
  720.     GdipCreateBitmapFromScan0(iW1, iH1, 0, PixelFormat32bppARGB, 0, @hBitmap_Subtract)
  721.     GdipBitmapLockBits(hBitmap_Subtract, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Subtract)
  722.     GdipBitmapLockBits(hImage1, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData1)
  723.     GdipBitmapLockBits(hImage2, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData2)
  724.    
  725.    
  726.    
  727.     For iY = 0 To iH1 - 1
  728.         iRowOffset = iY * iW1
  729.         For iX = 0 To iW1 - 1
  730.             iColor1 = Cast(ULong Ptr, tBitmapData1.Scan0)[iRowOffset + iX]
  731.             iColor2 = Cast(ULong Ptr, tBitmapData2.Scan0)[iRowOffset + iX]
  732.             If bInvert Then
  733.                 iR = &hFF - ((iColor1 Shr 16) And &hFF) - ((iColor2 Shr 16) And &hFF) + iBias
  734.                 iG = &hFF - ((iColor1 Shr 8) And &hFF) - ((iColor2 Shr 8) And &hFF) + iBias
  735.                 iB = &hFF - (iColor1 And &hFF) - (iColor2 And &hFF) + iBias
  736.             Else
  737.                 iR = ((iColor1 Shr 16) And &hFF) - ((iColor2 Shr 16) And &hFF) + iBias
  738.                 iG = ((iColor1 Shr 8) And &hFF) - ((iColor2 Shr 8) And &hFF) + iBias
  739.                 iB = (iColor1 And &hFF) - (iColor2 And &hFF) + iBias
  740.             EndIf
  741.             iR = IIf(iR < 0, 0, IIf(iR > 255, 255, iR))
  742.             iG = IIf(iG < 0, 0, IIf(iG > 255, 255, iG))
  743.             iB = IIf(iB < 0, 0, IIf(iB > 255, 255, iB))
  744.             Cast(ULong Ptr, tBitmapData_Subtract.Scan0)[iRowOffset + iX] = &hFF000000 + (iR Shl 16) + (iG Shl 8) + iB
  745.         Next
  746.     Next
  747.    
  748.     GdipBitmapUnlockBits(hBitmap_Subtract, @tBitmapData_Subtract)
  749.     GdipBitmapUnlockBits(hImage1, @tBitmapData1)
  750.     GdipBitmapUnlockBits(hImage2, @tBitmapData2)
  751.     Return hBitmap_Subtract    
  752. End Function
  753.  
  754.  
  755. Function _GDIPlus_BitmapApplyFilter_SymmetricNearestNeighbour(ByVal hImage As Any Ptr, fRadius As Single, bGDI As BOOL) As Any Ptr Export '...'
  756.     Dim As Single iW, iH
  757.     Dim As Any Ptr hBitmap_Dest, hGDIBitmap
  758.     Dim As BitmapData tBitmapData, tBitmapData_Dest
  759.     Dim As Long iX, iY, iRowOffset, c, k, sumR, sumG, sumB, iCount, xx, yy, iR, iG, iB, iR1, iG1, iB1, iR2, iG2, iB2, x, y
  760.     Dim As Integer iStatus
  761.    
  762.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  763.     If iStatus <> 0 Then Return 0
  764.        
  765.     Dim As RECT tRect_dest = Type(0, 0, iW - 1, iH - 1), tRect = Type(0, 0, iW - 1, iH - 1)
  766.    
  767.     fRadius = IIf(fRadius < 1, 1, IIf(fRadius > 25, 25, fRadius))
  768.    
  769.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dest)
  770.     GdipBitmapLockBits(hBitmap_Dest, Cast(Any Ptr, @tRect_dest), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Dest)
  771.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  772.    
  773.     For iY = 0 To iH - 1
  774.         iRowOffset = iY * iW
  775.         For iX = 0 To iW - 1
  776.             sumR = 0
  777.             sumG = 0
  778.             sumB = 0
  779.             iCount = 0
  780.             c = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  781.             iR = (&h00FF0000 And c) Shr 16
  782.             iG = (&h0000FF00 And c) Shr 8
  783.             iB = &h000000FF And c
  784.             For yy = -fRadius To fRadius
  785.                 For xx = -fRadius To fRadius
  786.                     k = iX + xx
  787.                     x = IIf(k < 0, 0, IIf(k > iW - 1, iW - 1, k))
  788.                     k = iY + yy
  789.                     y = IIf(k < 0, 0, IIf(k > iH - 1, iH - 1, k))
  790.                     c = Cast(ULong Ptr, tBitmapData.Scan0)[y * iW + x]
  791.                     iR1 = (&h00FF0000 And c) Shr 16
  792.                     iG1 = (&h0000FF00 And c) Shr 8
  793.                     iB1 =  &h000000FF And c
  794.                     k = iX - xx
  795.                     x = IIf(k < 0, 0, IIf(k > iW - 1, iW - 1, k))
  796.                     k = iY + yy
  797.                     y = IIf(k < 0, 0, IIf(k > iH - 1, iH - 1, k))
  798.                     c = Cast(ULong Ptr, tBitmapData.Scan0)[y * iW + x]
  799.                     iR2 = (&h00FF0000 And c) Shr 16
  800.                     iG2 = (&h0000FF00 And c) Shr 8
  801.                     iB2 =  &h000000FF And c
  802.                     If __DeltaE(iR, iG, iB, iR1, iG1, iB1) < __DeltaE(iR, iG, iB, iR2, iG2, iB2) Then
  803.                         sumR += iR1
  804.                         sumG += iG1
  805.                         sumB += iB1
  806.                     Else
  807.                         sumR += iR2
  808.                         sumG += iG2
  809.                         sumB += iB2                
  810.                     EndIf
  811.                     iCount += 1
  812.                 Next
  813.             Next
  814.             Cast(ULong Ptr, tBitmapData_Dest.Scan0)[iRowOffset + iX] = &hFF000000 + Int(sumR / iCount) * &h10000 + Int(sumG / iCount) * &h100 + Int(sumB / iCount)
  815.         Next
  816.     Next
  817.    
  818.     GdipBitmapUnlockBits(hBitmap_Dest, @tBitmapData_Dest)
  819.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  820.     If bGDI Then
  821.         GdipCreateHBITMAPFromBitmap(hBitmap_Dest, @hGDIBitmap, &hFF000000)
  822.         GdipDisposeImage(hBitmap_Dest)
  823.         Return hGDIBitmap
  824.     EndIf
  825.     Return hBitmap_Dest
  826. End Function
  827.  
  828. Function _GDIPlus_BitmapApplyFilter_Jitter(ByVal hImage As Any Ptr, iAmount As ULong, bGDI As BOOL) As Any Ptr Export '...'
  829.     Dim As Single iW, iH
  830.     Dim As Any Ptr hBitmap_Dest, hGDIBitmap
  831.     Dim As BitmapData tBitmapData, tBitmapData_Dest
  832.     Dim As Long iX, iY, iRowOffset, fNX, fNY, iColor
  833.     Dim As Integer iStatus
  834.  
  835.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  836.     If iStatus <> 0 Then Return 0
  837.    
  838.     Dim As RECT tRect_dest = Type(0, 0, iW - 1, iH - 1), tRect = Type(0, 0, iW - 1, iH - 1)
  839.    
  840.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dest)
  841.     GdipBitmapLockBits(hBitmap_Dest, Cast(Any Ptr, @tRect_dest), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Dest)
  842.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  843.    
  844.     iAmount = IIF(iAmount < 1, 1, iAmount)
  845.    
  846.     For iY = 0 To iH - 1
  847.         iRowOffset = iY * iW
  848.         For iX = 0 To iW - 1
  849.             fNX = iX + Int((Rnd - 0.5) * iAmount)
  850.             fNX = IIf(fNX < 1, 1, IIf(fNX > iW - 1, iW - 1, fNX))
  851.             fNY = (iY + Int((Rnd - 0.5) * iAmount))
  852.             fNY = IIf(fNY < 1, 1, IIf(fNY > iH - 1, iH - 1, fNY))
  853.             fNY *= iW
  854.           iColor = Cast(ULong Ptr, tBitmapData.Scan0)[fNY + fNX]
  855.           Cast(ULong Ptr, tBitmapData_Dest.Scan0)[iRowOffset + iX] = iColor
  856.         Next
  857.     Next
  858.    
  859.     GdipBitmapUnlockBits(hBitmap_Dest, @tBitmapData_Dest)
  860.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  861.     If bGDI Then
  862.         GdipCreateHBITMAPFromBitmap(hBitmap_Dest, @hGDIBitmap, &hFF000000)
  863.         GdipDisposeImage(hBitmap_Dest)
  864.         Return hGDIBitmap
  865.     EndIf
  866.     Return hBitmap_Dest    
  867. End Function
  868.  
  869. Function _GDIPlus_BitmapApplyFilter_Median(ByVal hImage As Any Ptr, fRadius As Single, bGDI As BOOL) As Any Ptr Export '...'
  870.     Dim As Single iW, iH
  871.     Dim As Any Ptr hBitmap_Median, hGDIBitmap
  872.     Dim As BitmapData tBitmapData, tBitmapData_Median
  873.     Dim As Integer iX, iY, iRowOffset, iColor, iXX, iYY, iColors, iSize, iOff, iMid, iMedianR, iMedianG, iMedianB, iSizeArray
  874.     Dim As Integer iStatus
  875.  
  876.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  877.     If iStatus <> 0 Then Return 0
  878.    
  879.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  880.    
  881.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Median)
  882.     GdipBitmapLockBits(hBitmap_Median, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Median)
  883.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  884.    
  885.     fRadius = Int(IIf(fRadius < 1, 1, IIf(fRadius > 25, 25, fRadius)))
  886.    
  887.     iSizeArray = (2 * fRadius + 1) * (2 * fRadius + 1)
  888.     ReDim aColorsR(0 To iSizeArray) As Integer
  889.     ReDim aColorsG(0 To iSizeArray) As Integer
  890.     ReDim aColorsB(0 To iSizeArray) As Integer
  891.    
  892.     iSize = iW * iH - 1
  893.    
  894.     For iY = 0 To iH - 1
  895.         iRowOffset = iY * iW
  896.         For iX = 0 To iW - 1
  897.            
  898.             'calculate median Values
  899.           iColors = 0
  900.             For iXX = iX - fRadius To iX + fRadius
  901.                 For iYY = iY - fRadius To iY + fRadius
  902.                     iOff = iYY * iW + iXX
  903.                     iColor = Cast(ULong Ptr, tBitmapData.Scan0)[IIf(iOff < 0, 0, IIf(iOff > iSize, iSize, iOff))]
  904.                     aColorsR(iColors) = (iColor Shr 16) And &hFF
  905.                     aColorsG(iColors) = (iColor Shr 8) And &hFF
  906.                     aColorsB(iColors) = iColor And &hFF
  907.                     iColors += 1
  908.                 Next
  909.             Next  
  910.  
  911.             'sort array
  912.             qsort(@aColorsR(0), iColors, SizeOf(Integer), cast(any ptr, @QCompare))
  913.             qsort(@aColorsG(0), iColors, SizeOf(Integer), cast(any ptr, @QCompare))
  914.             qsort(@aColorsB(0), iColors, SizeOf(Integer), cast(any ptr, @QCompare))
  915.  
  916.           iMid = Int(iColors / 2)
  917.            
  918.             If (iColors And 1) Then
  919.                 iMedianR = Int(aColorsR(iMid + 1))
  920.                 iMedianG = Int(aColorsG(iMid + 1))
  921.                 iMedianB = Int(aColorsB(iMid + 1))
  922.             Else
  923.                 iMedianR = Int((aColorsR(iMid) + aColorsR(iMid + 1)) / 2)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
  924.                 iMedianG = Int((aColorsG(iMid) + aColorsG(iMid + 1)) / 2)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
  925.                 iMedianB = Int((aColorsB(iMid) + aColorsB(iMid + 1)) / 2)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
  926.             EndIf
  927.            
  928.             'write median color values to bitmap
  929.            Cast(ULong Ptr, tBitmapData_Median.Scan0)[iRowOffset + iX] = &hFF000000 + iMedianR Shl 16 + iMedianG Shl 8 + iMedianB
  930.         Next
  931.     Next
  932.    
  933.     GdipBitmapUnlockBits(hBitmap_Median, @tBitmapData_Median)
  934.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  935.     If bGDI Then
  936.         GdipCreateHBITMAPFromBitmap(hBitmap_Median, @hGDIBitmap, &hFF000000)
  937.         GdipDisposeImage(hBitmap_Median)
  938.         Return hGDIBitmap
  939.     EndIf
  940.     Return hBitmap_Median  
  941. End Function
  942.  
  943. Function _GDIPlus_BitmapApplyFilter_Median2(ByVal hImage As Any Ptr, fRadius As Single, bGDI As BOOL) As Any Ptr Export 'based on original code by Dewald Esterhuizen '...'
  944.     Dim As Single iW, iH
  945.     Dim As Any Ptr hBitmap_Median2, hGDIBitmap
  946.     Dim As BitmapData tBitmapData, tBitmapData_Median2
  947.     Dim As Long iX, iY, iRowOffset, iColor, iXX, iYY, iColors, iSize, iOff, iMid, iMedian, iSizeArray, filterOffset
  948.     Dim As Integer iStatus
  949.  
  950.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  951.     If iStatus <> 0 Then Return 0
  952.    
  953.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  954.    
  955.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Median2)
  956.     GdipBitmapLockBits(hBitmap_Median2, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Median2)
  957.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  958.    
  959.     fRadius = Int(IIf(fRadius < 1, 1, IIf(fRadius > 25, 25, fRadius)))
  960.    
  961.     iSizeArray = (2 * fRadius + 1) * (2 * fRadius + 1)
  962.     ReDim aColors(0 To iSizeArray) As Integer
  963.    
  964.     iSize = iW * iH - 1
  965.     filterOffset = (fRadius - 1) / 2
  966.    
  967.     For iY = 0 To iH - 1
  968.         iRowOffset = iY * iW
  969.         For iX = 0 To iW - 1
  970.            
  971.             'calculate median Values
  972.           iColors = 0
  973.             For iXX = iX - filterOffset To iX + filterOffset
  974.                 For iYY = iY - filterOffset To iY + filterOffset
  975.                     iOff = iYY * iW + iXX
  976.                     iColor = Cast(ULong Ptr, tBitmapData.Scan0)[IIf(iOff < 0, 0, IIf(iOff > iSize, iSize, iOff))]
  977.                     aColors(iColors) = iColor
  978.                     iColors += 1
  979.                 Next
  980.             Next  
  981.                    
  982.             'sort array
  983.             qsort @aColors(0), iColors, SizeOf(Integer), cast(any ptr, @QCompare)
  984.            
  985.             iMedian = Int(aColors(filterOffset))
  986.            
  987.             'write median color values to bitmap
  988.            Cast(ULong Ptr, tBitmapData_Median2.Scan0)[iRowOffset + iX] = iMedian
  989.         Next
  990.     Next
  991.    
  992.     GdipBitmapUnlockBits(hBitmap_Median2, @tBitmapData_Median2)
  993.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  994.     If bGDI Then
  995.         GdipCreateHBITMAPFromBitmap(hBitmap_Median2, @hGDIBitmap, &hFF000000)
  996.         GdipDisposeImage(hBitmap_Median2)
  997.         Return hGDIBitmap
  998.     EndIf
  999.     Return hBitmap_Median2
  1000. End Function
  1001.  
  1002. Function _GDIPlus_BitmapApplyFilter_Kuwahara(ByVal hImage As Any Ptr, iSize As ULong, bGDI As BOOL) As Any Ptr Export 'based on code on http://www.gutgames.com/post/Kuwahara-Filter-in-C.aspx '...'
  1003.     Dim As Single ApetureMinX(0 To 3), ApetureMaxX(0 To 3), ApetureMinY(0 To 3), ApetureMaxY(0 To 3)
  1004.     Dim As Long RValues(0 To 3), GValues(0 To 3), BValues(0 To 3), NumPixels(0 To 3), MaxRValue(0 To 3), MaxGValue(0 To 3), MaxBValue(0 To 3), _
  1005.              MinRValue(0 To 3), MinGValue(0 To 3), MinBValue(0 To 3)
  1006.     Dim As Long iX, iY, TempX, TempY, x2, y2, i, j, MinDifference, CurrentDifference
  1007.     Dim As Long TempColor, r, g, b
  1008.     Dim As Any Ptr hBitmap_Kuwahara, hGDIBitmap
  1009.     Dim As BitmapData tBitmapData, tBitmapData_Kuwahara
  1010.     Dim As Single iW, iH
  1011.     Dim As Integer iStatus
  1012.  
  1013.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  1014.     If iStatus <> 0 Then Return 0
  1015.    
  1016.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  1017.    
  1018.     ApetureMinX(0) = -(iSize / 2)
  1019.     ApetureMinX(1) = 0
  1020.     ApetureMinX(2) = -(iSize / 2)
  1021.     ApetureMinX(3) = 0
  1022.    
  1023.     ApetureMaxX(0) = 0
  1024.     ApetureMaxX(1) = (iSize / 2)
  1025.     ApetureMaxX(2) = 0
  1026.     ApetureMaxX(3) = (iSize / 2)
  1027.    
  1028.     ApetureMinY(0) = -(iSize / 2)
  1029.     ApetureMinY(1) = -(iSize / 2)
  1030.     ApetureMinY(2) = 0
  1031.     ApetureMinY(3) = 0
  1032.    
  1033.     ApetureMaxY(0) = 0
  1034.     ApetureMaxY(1) = 0
  1035.     ApetureMaxY(2) = (iSize / 2)
  1036.     ApetureMaxY(3) = (iSize / 2)
  1037.    
  1038.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Kuwahara)
  1039.    
  1040.     GdipBitmapLockBits(hBitmap_Kuwahara, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Kuwahara)
  1041.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  1042.    
  1043.     For iX = 0 To iW - 1
  1044.         For iY = 0 To iH - 1
  1045.             RValues(0) = 0
  1046.             RValues(1) = 0
  1047.             RValues(2) = 0
  1048.             RValues(3) = 0
  1049.            
  1050.             GValues(0) = 0
  1051.             GValues(1) = 0
  1052.             GValues(2) = 0
  1053.             GValues(3) = 0
  1054.            
  1055.             BValues(0) = 0
  1056.             BValues(1) = 0
  1057.             BValues(2) = 0
  1058.             BValues(3) = 0
  1059.            
  1060.             NumPixels(0) = 0
  1061.             NumPixels(1) = 0
  1062.             NumPixels(2) = 0
  1063.             NumPixels(3) = 0
  1064.            
  1065.             MaxRValue(0) = 0
  1066.             MaxRValue(1) = 0
  1067.             MaxRValue(2) = 0
  1068.             MaxRValue(3) = 0
  1069.            
  1070.             MaxGValue(0) = 0
  1071.             MaxGValue(1) = 0
  1072.             MaxGValue(2) = 0
  1073.             MaxGValue(3) = 0
  1074.            
  1075.             MaxBValue(0) = 0
  1076.             MaxBValue(1) = 0
  1077.             MaxBValue(2) = 0
  1078.             MaxBValue(3) = 0
  1079.            
  1080.             MinRValue(0) = 255
  1081.             MinRValue(1) = 255
  1082.             MinRValue(2) = 255
  1083.             MinRValue(3) = 255
  1084.            
  1085.             MinGValue(0) = 255
  1086.             MinGValue(1) = 255
  1087.             MinGValue(2) = 255
  1088.             MinGValue(3) = 255
  1089.            
  1090.             MinBValue(0) = 255
  1091.             MinBValue(1) = 255
  1092.             MinBValue(2) = 255
  1093.             MinBValue(3) = 255
  1094.            
  1095.  
  1096.             For i = 0 To 3
  1097.                 For x2 = ApetureMinX(i) To ApetureMaxX(i)
  1098.                     TempX = iX + x2        
  1099.                     If (TempX >= 0) And (TempX < iW) Then
  1100.                         For y2 = ApetureMinY(i) To ApetureMaxY(i)
  1101.                             TempY = iY + y2
  1102.                             If (TempY >= 0) And (TempY < iH) Then
  1103.                                 TempColor = Cast(ULong Ptr, tBitmapData.Scan0)[(TempY * iW) + TempX]
  1104.                                 r = (TempColor Shr 16) And &hFF
  1105.                                 g = (TempColor Shr 8) And &hFF
  1106.                                 b = TempColor And &hFF
  1107.                                 RValues(i) += r
  1108.                                 GValues(i) += g
  1109.                                 BValues(i) += b
  1110.                                 If r > MaxRValue(i) Then
  1111.                                     MaxRValue(i) = r
  1112.                                 ElseIf r <  MinRValue(i) Then
  1113.                                     MinRValue(i) = r
  1114.                                 End If
  1115.                                 If g > MaxGValue(i) Then
  1116.                                     MaxGValue(i) = g
  1117.                                 ElseIf g < MinGValue(i) Then
  1118.                                     MinGValue(i) = g
  1119.                                 End If
  1120.                                 If b > MaxBValue(i) Then
  1121.                                     MaxBValue(i) = b
  1122.                                 ElseIf b < MinBValue(i) Then
  1123.                                     MinBValue(i) = b
  1124.                                 End If
  1125.                                 NumPixels(i) += 1
  1126.                             End If
  1127.                         Next
  1128.                     End If                 
  1129.                 Next
  1130.             Next
  1131.            
  1132.             j = 0
  1133.             MinDifference = 10000
  1134.             For i = 0 To 3
  1135.                 CurrentDifference = (MaxRValue(i) - MinRValue(i)) + (MaxGValue(i) - MinGValue(i)) + (MaxBValue(i) - MinBValue(i))
  1136.                 If (CurrentDifference < MinDifference) And (NumPixels(i) > 0) Then
  1137.                     j = i
  1138.                     MinDifference = CurrentDifference
  1139.                 EndIf
  1140.             Next
  1141.             r = Int(RValues(j) / NumPixels(j)) Shl 16
  1142.             g = Int(GValues(j) / NumPixels(j)) Shl 8
  1143.             b = Int(BValues(j) / NumPixels(j))
  1144.  
  1145.             Cast(ULong Ptr, tBitmapData_Kuwahara.Scan0)[iY * iW + iX] = &hFF000000 + r + g + b
  1146.         Next
  1147.     Next
  1148.  
  1149.     GdipBitmapUnlockBits(hBitmap_Kuwahara, @tBitmapData_Kuwahara)
  1150.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  1151.    
  1152.     If bGDI Then
  1153.         GdipCreateHBITMAPFromBitmap(hBitmap_Kuwahara, @hGDIBitmap, &hFF000000)
  1154.         GdipDisposeImage(hBitmap_Kuwahara)
  1155.         Return hGDIBitmap
  1156.     EndIf
  1157.    
  1158.     Return hBitmap_Kuwahara
  1159.    
  1160. End Function
  1161.  
  1162. Function _GDIPlus_BitmapApplyFilter_Edges(ByVal hImage As Any Ptr, bMode As UByte, bInverse As BOOL, bGDI As BOOL) As Any Ptr Export '...'
  1163.     Dim As Single iW, iH
  1164.     Dim As Any Ptr hBitmap_Dest, hGDIBitmap
  1165.     Dim As BitmapData tBitmapData, tBitmapData_Dest
  1166.     Dim As Long iX, iY, iRowOffset, c, cL, iR, iG, iB, iRl, iGl, iBl, iDiff, iRGB
  1167.     Dim As Single fBrightness, fBrightnessL
  1168.     Dim As Integer iStatus
  1169.  
  1170.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  1171.     If iStatus <> 0 Then Return 0
  1172.    
  1173.     Dim As RECT tRect_dest = Type(0, 0, iW - 1, iH - 1), tRect = Type(0, 0, iW - 1, iH - 1)
  1174.    
  1175.    
  1176.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dest)
  1177.     GdipBitmapLockBits(hBitmap_Dest, Cast(Any Ptr, @tRect_dest), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Dest)
  1178.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  1179.    
  1180.     For iY = 0 To iH - 1
  1181.         iRowOffset = iY * iW
  1182.         For iX = 1 To iW - 1
  1183.             c = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  1184.             iR = (&h00FF0000 And c) Shr 16
  1185.             iG = (&h0000FF00 And c) Shr 8
  1186.             iB =  &h000000FF And c
  1187.             fBrightness = Sqr(0.299 * iR * iR + 0.587 * iG * iG + 0.114 * iB * iB) 'luminance method
  1188.            
  1189.             cL = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX - 1]
  1190.             iRl = (&h00FF0000 And cL) Shr 16
  1191.             iGl = (&h0000FF00 And cL) Shr 8
  1192.             iBl =  &h000000FF And cL
  1193.             fBrightnessL = Sqr(0.299 * iRl * iRl + 0.587 * iGl * iGl + 0.114 * iBl * iBl) 'luminance method
  1194.            
  1195.             Select Case bMode
  1196.                 Case 0
  1197.                     iDiff = Int(Abs(fBrightness - fBrightnessL))           
  1198.                 Case Else
  1199.                     iDiff = &h80 + Int(Abs(fBrightness - fBrightnessL))
  1200.                     iDiff = IIf(iDiff > 255, 255, iDiff)
  1201.             End Select
  1202.             If bInverse Then
  1203.                 iRGB = ((iDiff Shl 16) + (iDiff Shl 8) + iDiff) Xor &h00FFFFFF
  1204.             Else
  1205.                 iRGB = (iDiff Shl 16) + (iDiff Shl 8) + iDiff
  1206.             EndIf
  1207.             Cast(ULong Ptr, tBitmapData_Dest.Scan0)[iRowOffset + iX] = (&hFF000000 + iRGB)
  1208.         Next
  1209.     Next
  1210.    
  1211.     GdipBitmapUnlockBits(hBitmap_Dest, @tBitmapData_Dest)
  1212.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  1213.     If bGDI Then
  1214.         GdipCreateHBITMAPFromBitmap(hBitmap_Dest, @hGDIBitmap, &hFF000000)
  1215.         GdipDisposeImage(hBitmap_Dest)
  1216.         Return hGDIBitmap
  1217.     EndIf
  1218.     Return hBitmap_Dest
  1219. End Function
  1220.  
  1221. Function _GDIPlus_BitmapApplyFilter_Pointillism(ByVal hImage As Any Ptr, iRounds As ULong, iSize As ULong, iA As UByte, bBorder As BOOL, bGDI As BOOL) As Any Ptr Export '...'
  1222.     Dim As Single iW, iH
  1223.     Dim As Any Ptr hBitmap_Dest, hGDIBitmap, hBrush, hPen, hGfx
  1224.     Dim As Long i, iR, iG, iB, iARGB, iAlpha, iAlpha2
  1225.     Dim As Single fX, fY, iSize2
  1226.     Dim As Integer iStatus
  1227.  
  1228.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  1229.     If iStatus <> 0 Then Return 0
  1230.  
  1231.    
  1232.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dest)
  1233.     GdipGetImageGraphicsContext(hBitmap_Dest, @hGfx)
  1234.     GdipSetSmoothingMode(hGfx, 4)
  1235.     GdipSetPixelOffsetMode(hGfx, 4)
  1236.    
  1237.     If iRounds < 1 Then iRounds = CLng(iW * iH / 12.5)
  1238.     iRounds = IIf(iRounds > 1000000, 1000000, iRounds)
  1239.  
  1240.    
  1241.     iSize = IIf(iSize < 1, 1, IIf(iSize > 512, 512, iSize))
  1242.  
  1243.     iA = IIf(iA < 1, 1, IIf(iA > 255, 255, iA))
  1244.     iAlpha = iA Shl 24
  1245.     iAlpha2 = (iA Shr 2) Shl 24
  1246.    
  1247.     GdipCreatePen1(iAlpha2, 1, 2, @hPen)
  1248.    
  1249.     iSize2 = iSize / 2
  1250.    
  1251.     For i = 1 To iRounds
  1252.         fX = Rnd * (iW - 1)
  1253.         fY = Rnd * (iH - 1)
  1254.         GdipBitmapGetPixel(hImage, fX, fY, @iARGB)
  1255.         GdipCreateSolidFill(iAlpha + (iARGB And &H00FFFFFF), @hBrush)
  1256.         GdipFillEllipse(hGfx, hBrush, fX - iSize2, fY - iSize2, iSize, iSize)
  1257.         If bBorder Then GdipDrawEllipse(hGfx, hPen, fX - iSize2, fY - iSize2, iSize, iSize)
  1258.         GdipDeleteBrush(hBrush)
  1259.     Next
  1260.    
  1261.     GdipDeletePen(hPen)
  1262.     GdipDeleteGraphics(hGfx)
  1263.     If bGDI Then
  1264.         GdipCreateHBITMAPFromBitmap(hBitmap_Dest, @hGDIBitmap, &hFF000000)
  1265.         GdipDisposeImage(hBitmap_Dest)
  1266.         Return hGDIBitmap
  1267.     EndIf
  1268.     Return hBitmap_Dest
  1269. End Function
  1270.  
  1271. Function _GDIPlus_BitmapApplyFilter_Linellism(ByVal hImage As Any Ptr, iRounds As ULong, iSize As ULong, iA As UByte, iMode As UByte, bBorder As BOOL, bGDI As BOOL) As Any Ptr Export '...'
  1272.     Dim As Single iW, iH
  1273.     Dim As Any Ptr hBitmap_Dest, hGDIBitmap, hBrush, hPen, hGfx
  1274.     Dim As Long i, iR, iG, iB, iARGB, iAlpha, iAlpha2
  1275.     Dim As Single fX, fY, iSize2, iSize4
  1276.     Dim As Integer iStatus
  1277.  
  1278.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  1279.     If iStatus <> 0 Then Return 0
  1280.  
  1281.    
  1282.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dest)
  1283.     GdipGetImageGraphicsContext(hBitmap_Dest, @hGfx)
  1284.     GdipSetSmoothingMode(hGfx, 4)
  1285.     GdipSetPixelOffsetMode(hGfx, 4)
  1286.    
  1287.     If iRounds < 1 Then iRounds = CLng(iW * iH / iSize * 2)
  1288.    
  1289.     iRounds = IIf(iRounds > 1000000, 1000000, iRounds)
  1290.     iSize = IIf(iSize < 1, 1, IIf(iSize > 512, 512, iSize))
  1291.  
  1292.     iA = IIF(iA < 1, 1, IIf(iA > 255, 255, iA))
  1293.     iAlpha = iA Shl 24
  1294.     iAlpha2 = (iA Shr 2) Shl 24
  1295.     iMode = IIF(iMode < 1, 1, IIf(iMode > 3, 3, iMode))
  1296.    
  1297.     GdipCreatePen1(iAlpha2, 1, 2, @hPen)
  1298.    
  1299.     iSize2 = iSize / 2
  1300.     iSize4 = iSize / 4
  1301.    
  1302.     For i = 1 To iRounds
  1303.         fX = Rnd * (iW - 1)
  1304.         fY = Rnd * (iH - 1)
  1305.         GdipBitmapGetPixel(hImage, fX, fY, @iARGB)
  1306.         GdipCreateSolidFill(iAlpha + (iARGB And &h00FFFFFF), @hBrush)
  1307.         Select Case iMode
  1308.             Case 1
  1309.                 GdipFillRectangle(hGfx, hBrush, fX, fY, iSize, iSize4)
  1310.                 If bBorder Then GdipDrawRectangle(hGfx, hPen, fX - iSize2, fY - iSize2, iSize, iSize4)
  1311.             Case 2
  1312.                 GdipFillRectangle(hGfx, hBrush, fX, fY, iSize4, iSize)
  1313.                 If bBorder Then GdipDrawRectangle(hGfx, hPen, fX - iSize2, fY - iSize2, iSize4, iSize)                     
  1314.             Case 3
  1315.                 Select Case Int(Rnd * 10)
  1316.                     Case 0 to 4
  1317.                         GdipFillRectangle(hGfx, hBrush, fX, fY, iSize4, iSize)
  1318.                         If bBorder Then GdipDrawRectangle(hGfx, hPen, fX, fY, iSize4, iSize)
  1319.                     Case Else
  1320.                         GdipFillRectangle(hGfx, hBrush, fX - iSize2, fY, iSize, iSize4)
  1321.                         If bBorder Then GdipDrawRectangle(hGfx, hPen, fX, fY, iSize, iSize4)
  1322.                 End Select
  1323.         End Select
  1324.        
  1325.         GdipDeleteBrush(hBrush)
  1326.     Next
  1327.    
  1328.     GdipDeletePen(hPen)
  1329.     GdipDeleteGraphics(hGfx)
  1330.     If bGDI Then
  1331.         GdipCreateHBITMAPFromBitmap(hBitmap_Dest, @hGDIBitmap, &hFF000000)
  1332.         GdipDisposeImage(hBitmap_Dest)
  1333.         Return hGDIBitmap
  1334.     EndIf
  1335.     Return hBitmap_Dest
  1336. End Function
  1337.  
  1338. Function _GDIPlus_BitmapApplyFilter_Convolution(ByVal hImage As Any Ptr, fFactor As Single, fBias As Single, iMode As UByte, pMStruct As Any Ptr, iMatrix As UShort, bGDI As BOOL) As Any Ptr Export '...'
  1339.     Dim As Single iW, iH, fRedSum, fGreenSum, fBlueSum, fSum, fMatrix, fW, fH, aFilter(), aFilter2(), f
  1340.     Dim As Single fRedSumX, fGreenSumX, fBlueSumX, fRedSumY, fGreenSumY, fBlueSumY, fMatrixX, fMatrixY
  1341.     Dim As Any Ptr hBitmap_Dest, hGDIBitmap
  1342.     Dim As BitmapData tBitmapData, tBitmapData_Dest
  1343.     Dim As Long iX, iY, iRowOffset, iColor, iR, iG, iB, filterWidth, filterHeight, filterX, filterY, imageX, imageY
  1344.     Dim As ULong iAlpha, iRGB, c
  1345.     Dim As Integer iStatus
  1346.     Dim As UShort i, j
  1347.     Dim As Single Ptr pMatrix = pMStruct
  1348.  
  1349.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  1350.     If iStatus <> 0 Then Return 0
  1351.  
  1352.    
  1353.     Dim As RECT tRect_dest = Type(0, 0, iW - 1, iH - 1), tRect = Type(0, 0, iW - 1, iH - 1)
  1354.        
  1355.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dest)
  1356.     GdipBitmapLockBits(hBitmap_Dest, Cast(Any Ptr, @tRect_dest), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Dest)
  1357.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  1358.    
  1359.    
  1360.     iAlpha = &hFF000000
  1361.     iMode = IIf(iMode < 0, 1, IIf(iMode > 31, 31, iMode))
  1362.    
  1363.     filterWidth = 3
  1364.     filterHeight = 3
  1365.     Select Case iMode
  1366.         Case 0 'manual matrix
  1367.             filterWidth = Sqr(iMatrix)
  1368.             filterHeight = filterWidth
  1369.             'If (filterWidth And 1) <> 1 Or filterWidth ^ 2 <> iMatrix Or filterWidth < 3 Then Return 0
  1370.             If filterWidth ^ 2 <> iMatrix Or filterWidth < 3 Then Return 0
  1371.            
  1372.             ReDim aFilter(0 To filterWidth - 1, 0 To filterHeight - 1)
  1373.             j = -1
  1374.             For i = 0 To iMatrix - 1
  1375.                 If i Mod filterWidth = 0 Then j += 1
  1376.                 aFilter(j Mod filterHeight, i Mod filterWidth) = pMatrix[i]
  1377.             Next
  1378.         Case 1 'Emboss
  1379.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1380.             aFilter(0, 0) = 2.0
  1381.             aFilter(0, 1) = 0.0
  1382.             aFilter(0, 2) = 0.0
  1383.             aFilter(1, 0) = 0.0
  1384.             aFilter(1, 1) = -1.0
  1385.             aFilter(1, 2) = 0.0
  1386.             aFilter(2, 0) = 0.0
  1387.             aFilter(2, 1) = 0.0
  1388.             aFilter(2, 2) = -1.0
  1389.         Case 2 'Emboss45Degree Filter
  1390.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1391.             aFilter(0, 0) = -1.0
  1392.             aFilter(0, 1) = -1.0
  1393.             aFilter(0, 2) = 0.0
  1394.             aFilter(1, 0) = -1.0
  1395.             aFilter(1, 1) = 0.0
  1396.             aFilter(1, 2) = 1.0
  1397.             aFilter(2, 0) = 0.0
  1398.             aFilter(2, 1) = 1.0
  1399.             aFilter(2, 2) = 1.0
  1400.         Case 3 'EmbossTopLeftBottomRight Filter
  1401.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1402.             aFilter(0, 0) = -1.0
  1403.             aFilter(0, 1) = 0.0
  1404.             aFilter(0, 2) = 0.0
  1405.             aFilter(1, 0) = 0.0
  1406.             aFilter(1, 1) = 0.0
  1407.             aFilter(1, 2) = 0.0
  1408.             aFilter(2, 0) = 0.0
  1409.             aFilter(2, 1) = 0.0
  1410.             aFilter(2, 2) = 1.0
  1411.         Case 4 'IntenseEmboss Filter
  1412.             filterWidth = 5
  1413.             filterHeight = 5
  1414.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1415.             aFilter(0, 0) = -1.0
  1416.             aFilter(0, 1) = -1.0
  1417.             aFilter(0, 2) = -1.0
  1418.             aFilter(0, 3) = -1.0
  1419.             aFilter(0, 4) = 0.0
  1420.             aFilter(1, 0) = -1.0
  1421.             aFilter(1, 1) = -1.0
  1422.             aFilter(1, 2) = -1.0
  1423.             aFilter(1, 3) = 0.0
  1424.             aFilter(1, 4) = 1.0
  1425.             aFilter(2, 0) = -1.0
  1426.             aFilter(2, 1) = -1.0
  1427.             aFilter(2, 2) = 0.0
  1428.             aFilter(2, 3) = 1.0
  1429.             aFilter(2, 4) = 1.0
  1430.             aFilter(3, 0) = -1.0
  1431.             aFilter(3, 1) = 0.0
  1432.             aFilter(3, 2) = 1.0
  1433.             aFilter(3, 3) = 1.0
  1434.             aFilter(3, 4) = 1.0
  1435.             aFilter(4, 0) = 0.0
  1436.             aFilter(4, 1) = 1.0
  1437.             aFilter(4, 2) = 1.0
  1438.             aFilter(4, 3) = 1.0
  1439.             aFilter(4, 4) = 1.0
  1440.         Case 5 'Sharpen
  1441.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1442.             aFilter(0, 0) = 0
  1443.             aFilter(0, 1) = -1
  1444.             aFilter(0, 2) = 0
  1445.             aFilter(1, 0) = -1
  1446.             aFilter(1, 1) = 5
  1447.             aFilter(1, 2) = -1
  1448.             aFilter(2, 0) = 0
  1449.             aFilter(2, 1) = -1
  1450.             aFilter(2, 2) = 0
  1451.         Case 6 'Box blur (normalized)
  1452.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1453.             aFilter(0, 0) = 1
  1454.             aFilter(0, 1) = 1
  1455.             aFilter(0, 2) = 1
  1456.             aFilter(1, 0) = 1
  1457.             aFilter(1, 1) = 1
  1458.             aFilter(1, 2) = 1
  1459.             aFilter(2, 0) = 1
  1460.             aFilter(2, 1) = 1
  1461.             aFilter(2, 2) = 1
  1462.         Case 7 'Gaussian blur (approximation)
  1463.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1464.             aFilter(0, 0) = 1
  1465.             aFilter(0, 1) = 2
  1466.             aFilter(0, 2) = 1
  1467.             aFilter(1, 0) = 2
  1468.             aFilter(1, 1) = 4
  1469.             aFilter(1, 2) = 2
  1470.             aFilter(2, 0) = 1
  1471.             aFilter(2, 1) = 2
  1472.             aFilter(2, 2) = 1
  1473.         Case 8 'Triangle Blur
  1474.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1475.             aFilter(0, 0) = 1
  1476.             aFilter(0, 1) = 2
  1477.             aFilter(0, 2) = 1
  1478.             aFilter(1, 0) = 2
  1479.             aFilter(1, 1) = 4
  1480.             aFilter(1, 2) = 2
  1481.             aFilter(2, 0) = 1
  1482.             aFilter(2, 1) = 2
  1483.             aFilter(2, 2) = 1
  1484.         Case 9 'Unsharp (with no image mask) 5×5
  1485.             filterWidth = 5
  1486.             filterHeight = 5
  1487.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1488.             aFilter(0, 0) = -1 / 256
  1489.             aFilter(0, 1) = -1 / 256 * 4
  1490.             aFilter(0, 2) = -1 / 256 * 6
  1491.             aFilter(0, 3) = -1 / 256 * 4
  1492.             aFilter(0, 4) = -1 / 256 * 1
  1493.             aFilter(1, 0) = -1 / 256 * 4
  1494.             aFilter(1, 1) = -1 / 256 * 16
  1495.             aFilter(1, 2) = -1 / 256 * 24
  1496.             aFilter(1, 3) = -1 / 256 * 16
  1497.             aFilter(1, 4) = -1 / 256 * 4
  1498.             aFilter(2, 0) = -1 / 256 * 6
  1499.             aFilter(2, 1) = -1 / 256 * 24
  1500.             aFilter(2, 2) = -1 / 256 * -476
  1501.             aFilter(2, 3) = -1 / 256 * 24
  1502.             aFilter(2, 4) = -1 / 256 * 6
  1503.             aFilter(3, 0) = -1 / 256 * 4
  1504.             aFilter(3, 1) = -1 / 256 * 16
  1505.             aFilter(3, 2) = -1 / 256 * 24
  1506.             aFilter(3, 3) = -1 / 256 * 16
  1507.             aFilter(3, 4) = -1 / 256 * 4
  1508.             aFilter(4, 0) = -1 / 256
  1509.             aFilter(4, 1) = -1 / 256 * 4
  1510.             aFilter(4, 2) = -1 / 256 * 6
  1511.             aFilter(4, 3) = -1 / 256 * 4
  1512.             aFilter(4, 4) = -1 / 256
  1513.         Case 10 'Unsharpen
  1514.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1515.             aFilter(0, 0) = -1
  1516.             aFilter(0, 1) = -1
  1517.             aFilter(0, 2) = -1
  1518.             aFilter(1, 0) = -1
  1519.             aFilter(1, 1) = 9
  1520.             aFilter(1, 2) = -1
  1521.             aFilter(2, 0) = -1
  1522.             aFilter(2, 1) = -1
  1523.             aFilter(2, 2) = -1
  1524.         Case 11 'Edge Detection 1
  1525.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1526.             aFilter(0, 0) = -0.125
  1527.             aFilter(0, 1) = -0.125
  1528.             aFilter(0, 2) = -0.125
  1529.             aFilter(1, 0) = -0.125
  1530.             aFilter(1, 1) = 1
  1531.             aFilter(1, 2) = -0.125
  1532.             aFilter(2, 0) = -0.125
  1533.             aFilter(2, 1) = -0.125
  1534.             aFilter(2, 2) = -0.125
  1535.         Case 12 'Edge Detection 2
  1536.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1537.             aFilter(0, 0) = -1
  1538.             aFilter(0, 1) = -1
  1539.             aFilter(0, 2) = -1
  1540.             aFilter(1, 0) = -1
  1541.             aFilter(1, 1) = 8
  1542.             aFilter(1, 2) = -1
  1543.             aFilter(2, 0) = -1
  1544.             aFilter(2, 1) = -1
  1545.             aFilter(2, 2) = -1
  1546.         Case 13 'Edge Detection 3
  1547.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1548.             aFilter(0, 0) = -5
  1549.             aFilter(0, 1) = 0
  1550.             aFilter(0, 2) = 0
  1551.             aFilter(1, 0) = 0
  1552.             aFilter(1, 1) = 0
  1553.             aFilter(1, 2) = 0
  1554.             aFilter(2, 0) = 0
  1555.             aFilter(2, 1) = 0
  1556.             aFilter(2, 2) = 5
  1557.         Case 14 'Edge Detection 4
  1558.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1559.             aFilter(0, 0) = -1
  1560.             aFilter(0, 1) = -1
  1561.             aFilter(0, 2) = -1
  1562.             aFilter(1, 0) = 0
  1563.             aFilter(1, 1) = 0
  1564.             aFilter(1, 2) = 0
  1565.             aFilter(2, 0) = 1
  1566.             aFilter(2, 1) = 1
  1567.             aFilter(2, 2) = 1
  1568.         Case 15 'Edge Detection 5
  1569.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1570.             aFilter(0, 0) = -1
  1571.             aFilter(0, 1) = -1
  1572.             aFilter(0, 2) = -1
  1573.             aFilter(1, 0) = 2
  1574.             aFilter(1, 1) = 2
  1575.             aFilter(1, 2) = 2
  1576.             aFilter(2, 0) = -1
  1577.             aFilter(2, 1) = -1
  1578.             aFilter(2, 2) = -1
  1579.         Case 16 'Edge Detection 6
  1580.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1581.             aFilter(0, 0) = -5
  1582.             aFilter(0, 1) = -5
  1583.             aFilter(0, 2) = -5
  1584.             aFilter(1, 0) = -5
  1585.             aFilter(1, 1) = 39
  1586.             aFilter(1, 2) = -5
  1587.             aFilter(2, 0) = -5
  1588.             aFilter(2, 1) = -5
  1589.             aFilter(2, 2) = -5
  1590.         Case 17 'Another Blur
  1591.             filterWidth = 5
  1592.             filterHeight = 5
  1593.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1594.             aFilter(0, 0) = 0
  1595.             aFilter(0, 1) = 0
  1596.             aFilter(0, 2) = 1
  1597.             aFilter(0, 3) = 0
  1598.             aFilter(0, 4) = 0
  1599.             aFilter(1, 0) = 0
  1600.             aFilter(1, 1) = 1
  1601.             aFilter(1, 2) = 1
  1602.             aFilter(1, 3) = 1
  1603.             aFilter(1, 4) = 0
  1604.             aFilter(2, 0) = 1
  1605.             aFilter(2, 1) = 1
  1606.             aFilter(2, 2) = 1
  1607.             aFilter(2, 3) = 1
  1608.             aFilter(2, 4) = 1
  1609.             aFilter(3, 0) = 0
  1610.             aFilter(3, 1) = 1
  1611.             aFilter(3, 2) = 1
  1612.             aFilter(3, 3) = 1
  1613.             aFilter(3, 4) = 0
  1614.             aFilter(4, 0) = 0
  1615.             aFilter(4, 1) = 0
  1616.             aFilter(4, 2) = 1
  1617.             aFilter(4, 3) = 0
  1618.             aFilter(4, 4) = 0
  1619.         Case 18 'Motion Blur
  1620.             filterWidth = 9
  1621.             filterHeight = 9
  1622.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1623.             aFilter(0, 0) = 1
  1624.             aFilter(0, 1) = 0
  1625.             aFilter(0, 2) = 0
  1626.             aFilter(0, 3) = 0
  1627.             aFilter(0, 4) = 0
  1628.             aFilter(0, 5) = 0
  1629.             aFilter(0, 6) = 0
  1630.             aFilter(0, 7) = 0
  1631.             aFilter(0, 8) = 0
  1632.             aFilter(1, 0) = 0
  1633.             aFilter(1, 1) = 1
  1634.             aFilter(1, 2) = 0
  1635.             aFilter(1, 3) = 0
  1636.             aFilter(1, 4) = 0
  1637.             aFilter(1, 5) = 0
  1638.             aFilter(1, 6) = 0
  1639.             aFilter(1, 7) = 0
  1640.             aFilter(1, 8) = 0
  1641.             aFilter(2, 0) = 0
  1642.             aFilter(2, 1) = 0
  1643.             aFilter(2, 2) = 1
  1644.             aFilter(2, 3) = 0
  1645.             aFilter(2, 4) = 0
  1646.             aFilter(2, 5) = 0
  1647.             aFilter(2, 6) = 0
  1648.             aFilter(2, 7) = 0
  1649.             aFilter(2, 8) = 0      
  1650.             aFilter(3, 0) = 0
  1651.             aFilter(3, 1) = 0
  1652.             aFilter(3, 2) = 0
  1653.             aFilter(3, 3) = 1
  1654.             aFilter(3, 4) = 0
  1655.             aFilter(3, 5) = 0
  1656.             aFilter(3, 6) = 0
  1657.             aFilter(3, 7) = 0
  1658.             aFilter(3, 8) = 0
  1659.             aFilter(4, 0) = 0
  1660.             aFilter(4, 1) = 0
  1661.             aFilter(4, 2) = 0
  1662.             aFilter(4, 3) = 0
  1663.             aFilter(4, 4) = 1
  1664.             aFilter(4, 5) = 0
  1665.             aFilter(4, 6) = 0
  1666.             aFilter(4, 7) = 0
  1667.             aFilter(4, 8) = 0
  1668.             aFilter(5, 0) = 0
  1669.             aFilter(5, 1) = 0
  1670.             aFilter(5, 2) = 0
  1671.             aFilter(5, 3) = 0
  1672.             aFilter(5, 4) = 0
  1673.             aFilter(5, 5) = 1
  1674.             aFilter(5, 6) = 0
  1675.             aFilter(5, 7) = 0
  1676.             aFilter(5, 8) = 0
  1677.             aFilter(6, 0) = 0
  1678.             aFilter(6, 1) = 0
  1679.             aFilter(6, 2) = 0
  1680.             aFilter(6, 3) = 0
  1681.             aFilter(6, 4) = 0
  1682.             aFilter(6, 5) = 0
  1683.             aFilter(6, 6) = 1
  1684.             aFilter(6, 7) = 0
  1685.             aFilter(6, 8) = 0
  1686.             aFilter(7, 0) = 0
  1687.             aFilter(7, 1) = 0
  1688.             aFilter(7, 2) = 0
  1689.             aFilter(7, 3) = 0
  1690.             aFilter(7, 4) = 0
  1691.             aFilter(7, 5) = 0
  1692.             aFilter(7, 6) = 0
  1693.             aFilter(7, 7) = 1
  1694.             aFilter(7, 8) = 0
  1695.             aFilter(8, 0) = 0
  1696.             aFilter(8, 1) = 0
  1697.             aFilter(8, 2) = 0
  1698.             aFilter(8, 3) = 0
  1699.             aFilter(8, 4) = 0
  1700.             aFilter(8, 5) = 0
  1701.             aFilter(8, 6) = 0
  1702.             aFilter(8, 7) = 0
  1703.             aFilter(8, 8) = 1
  1704.         Case 19 'Sharpen 2
  1705.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1706.             aFilter(0, 0) = 1
  1707.             aFilter(0, 1) = 1
  1708.             aFilter(0, 2) = 1
  1709.             aFilter(1, 0) = 1
  1710.             aFilter(1, 1) = -7
  1711.             aFilter(1, 2) = 1
  1712.             aFilter(2, 0) = 1
  1713.             aFilter(2, 1) = 1
  1714.             aFilter(2, 2) = 1
  1715.         Case 20 'Sobel Filter      
  1716.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1717.             ReDim aFilter2(0 To filterHeight - 1, 0 To filterWidth - 1)
  1718.            
  1719.             'horizontal
  1720.             aFilter(0, 0) = 1
  1721.             aFilter(0, 1) = 2
  1722.             aFilter(0, 2) = 1
  1723.             aFilter(1, 0) = 0
  1724.             aFilter(1, 1) = 0
  1725.             aFilter(1, 2) = 0
  1726.             aFilter(2, 0) = -1
  1727.             aFilter(2, 1) = -2
  1728.             aFilter(2, 2) = -1
  1729.            
  1730.             'vertical
  1731.             aFilter2(0, 0) = 1
  1732.             aFilter2(0, 1) = 0
  1733.             aFilter2(0, 2) = -1
  1734.             aFilter2(1, 0) = 2
  1735.             aFilter2(1, 1) = 0
  1736.             aFilter2(1, 2) = -2
  1737.             aFilter2(2, 0) = 1
  1738.             aFilter2(2, 1) = 0
  1739.             aFilter2(2, 2) = -1
  1740.         Case 21 'Laplace filter 3x3 v1
  1741.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1742.             aFilter(0, 0) = 0
  1743.             aFilter(0, 1) = -1
  1744.             aFilter(0, 2) = 0
  1745.             aFilter(1, 0) = -1
  1746.             aFilter(1, 1) = 4
  1747.             aFilter(1, 2) = -1
  1748.             aFilter(2, 0) = 0
  1749.             aFilter(2, 1) = -1
  1750.             aFilter(2, 2) = 0
  1751.         Case 22 'Laplace filter 3x3 v2
  1752.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1753.             aFilter(0, 0) = -1
  1754.             aFilter(0, 1) = -1
  1755.             aFilter(0, 2) = -1
  1756.             aFilter(1, 0) = -1
  1757.             aFilter(1, 1) = 8
  1758.             aFilter(1, 2) = -1
  1759.             aFilter(2, 0) = -1
  1760.             aFilter(2, 1) = -1
  1761.             aFilter(2, 2) = -1
  1762.         Case 23 'Laplace filter 5x5
  1763.             filterWidth = 5
  1764.             filterHeight = 5
  1765.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1766.             aFilter(0, 0) = -1
  1767.             aFilter(0, 1) = -1
  1768.             aFilter(0, 2) = -1
  1769.             aFilter(0, 3) = -1
  1770.             aFilter(0, 4) = -1
  1771.             aFilter(1, 0) = -1
  1772.             aFilter(1, 1) = -1
  1773.             aFilter(1, 2) = -1
  1774.             aFilter(1, 3) = -1
  1775.             aFilter(1, 4) = -1
  1776.             aFilter(2, 0) = -1
  1777.             aFilter(2, 1) = -1
  1778.             aFilter(2, 2) = 24
  1779.             aFilter(2, 3) = -1
  1780.             aFilter(2, 4) = -1
  1781.             aFilter(3, 0) = -1
  1782.             aFilter(3, 1) = -1
  1783.             aFilter(3, 2) = -1
  1784.             aFilter(3, 3) = -1
  1785.             aFilter(3, 4) = -1
  1786.             aFilter(4, 0) = -1
  1787.             aFilter(4, 1) = -1
  1788.             aFilter(4, 2) = -1
  1789.             aFilter(4, 3) = -1
  1790.             aFilter(4, 4) = -1
  1791.         Case 24 'Prewitt
  1792.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1793.             ReDim aFilter2(0 To filterHeight - 1, 0 To filterWidth - 1)
  1794.             'horizontal
  1795.             aFilter(0, 0) = -1
  1796.             aFilter(0, 1) = 0
  1797.             aFilter(0, 2) = 1
  1798.             aFilter(1, 0) = -1
  1799.             aFilter(1, 1) = 0
  1800.             aFilter(1, 2) = 1
  1801.             aFilter(2, 0) = -1
  1802.             aFilter(2, 1) = 0
  1803.             aFilter(2, 2) = 1
  1804.             'vertical
  1805.             aFilter2(0, 0) = 1
  1806.             aFilter2(0, 1) = 1
  1807.             aFilter2(0, 2) = 1
  1808.             aFilter2(1, 0) = 0
  1809.             aFilter2(1, 1) = 0
  1810.             aFilter2(1, 2) = 0
  1811.             aFilter2(2, 0) = -1
  1812.             aFilter2(2, 1) = -1
  1813.             aFilter2(2, 2) = -1
  1814.         Case 25 'Kirsch
  1815.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1816.             ReDim aFilter2(0 To filterHeight - 1, 0 To filterWidth - 1)
  1817.             'horizontal
  1818.             aFilter(0, 0) = 5
  1819.             aFilter(0, 1) = 5
  1820.             aFilter(0, 2) = 5
  1821.             aFilter(1, 0) = -3
  1822.             aFilter(1, 1) = 0
  1823.             aFilter(1, 2) = -3
  1824.             aFilter(2, 0) = -3
  1825.             aFilter(2, 1) = -3
  1826.             aFilter(2, 2) = -3
  1827.             'vertical
  1828.             aFilter2(0, 0) = 5
  1829.             aFilter2(0, 1) = -3
  1830.             aFilter2(0, 2) = -3
  1831.             aFilter2(1, 0) = 4
  1832.             aFilter2(1, 1) = 0
  1833.             aFilter2(1, 2) = -3
  1834.             aFilter2(2, 0) = 5
  1835.             aFilter2(2, 1) = -3
  1836.             aFilter2(2, 2) = -3
  1837.         Case 26 'Outline 3x3
  1838.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1839.             aFilter(0, 0) = -1
  1840.             aFilter(0, 1) = -1
  1841.             aFilter(0, 2) = -1
  1842.             aFilter(1, 0) = -1
  1843.             aFilter(1, 1) = 8
  1844.             aFilter(1, 2) = -1
  1845.             aFilter(2, 0) = -1
  1846.             aFilter(2, 1) = -1
  1847.             aFilter(2, 2) = -1
  1848.         Case 27 'Gaussian5x5 Type1 blur
  1849.             filterWidth = 5
  1850.             filterHeight = 5
  1851.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1852.             aFilter(0, 0) = 2
  1853.             aFilter(0, 1) = 4
  1854.             aFilter(0, 2) = 5
  1855.             aFilter(0, 3) = 4
  1856.             aFilter(0, 4) = 2
  1857.             aFilter(1, 0) = 4
  1858.             aFilter(1, 1) = 9
  1859.             aFilter(1, 2) = 12
  1860.             aFilter(1, 3) = 9
  1861.             aFilter(1, 4) = 4
  1862.             aFilter(2, 0) = 5
  1863.             aFilter(2, 1) = 12
  1864.             aFilter(2, 2) = 15
  1865.             aFilter(2, 3) = 12
  1866.             aFilter(2, 4) = 5
  1867.             aFilter(3, 0) = 4
  1868.             aFilter(3, 1) = 9
  1869.             aFilter(3, 2) = 12
  1870.             aFilter(3, 3) = 9
  1871.             aFilter(3, 4) = 4
  1872.             aFilter(4, 0) = 2
  1873.             aFilter(4, 1) = 4
  1874.             aFilter(4, 2) = 5
  1875.             aFilter(4, 3) = 4
  1876.             aFilter(4, 4) = 2      
  1877.         Case 28 'Gaussian5x5 Type2 blur
  1878.             filterWidth = 5
  1879.             filterHeight = 5
  1880.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1881.             aFilter(0, 0) = 1
  1882.             aFilter(0, 1) = 4
  1883.             aFilter(0, 2) = 6
  1884.             aFilter(0, 3) = 4
  1885.             aFilter(0, 4) = 1
  1886.             aFilter(1, 0) = 4
  1887.             aFilter(1, 1) = 16
  1888.             aFilter(1, 2) = 24
  1889.             aFilter(1, 3) = 16
  1890.             aFilter(1, 4) = 4
  1891.             aFilter(2, 0) = 6
  1892.             aFilter(2, 1) = 24
  1893.             aFilter(2, 2) = 36
  1894.             aFilter(2, 3) = 24
  1895.             aFilter(2, 4) = 6
  1896.             aFilter(3, 0) = 4
  1897.             aFilter(3, 1) = 16
  1898.             aFilter(3, 2) = 24
  1899.             aFilter(3, 3) = 16
  1900.             aFilter(3, 4) = 4
  1901.             aFilter(4, 0) = 1
  1902.             aFilter(4, 1) = 4
  1903.             aFilter(4, 2) = 6
  1904.             aFilter(4, 3) = 4
  1905.             aFilter(4, 4) = 1
  1906.         Case 29 'Laplacian of Gaussian 5x5
  1907.             filterWidth = 5
  1908.             filterHeight = 5
  1909.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1910.             aFilter(0, 0) = 0
  1911.             aFilter(0, 1) = 0
  1912.             aFilter(0, 2) = -1
  1913.             aFilter(0, 3) = 0
  1914.             aFilter(0, 4) = 0
  1915.             aFilter(1, 0) = 0
  1916.             aFilter(1, 1) = -1
  1917.             aFilter(1, 2) = -2
  1918.             aFilter(1, 3) = -1
  1919.             aFilter(1, 4) = 0
  1920.             aFilter(2, 0) = -1
  1921.             aFilter(2, 1) = -2
  1922.             aFilter(2, 2) = 16
  1923.             aFilter(2, 3) = -2
  1924.             aFilter(2, 4) = -1
  1925.             aFilter(3, 0) = 0
  1926.             aFilter(3, 1) = -1
  1927.             aFilter(3, 2) = -2
  1928.             aFilter(3, 3) = -1
  1929.             aFilter(3, 4) = 0
  1930.             aFilter(4, 0) = 0
  1931.             aFilter(4, 1) = 0
  1932.             aFilter(4, 2) = -1
  1933.             aFilter(4, 3) = 0
  1934.             aFilter(4, 4) = 0
  1935.         Case 30 'SovelVsPrewitt 3x3
  1936.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1937.             ReDim aFilter2(0 To filterHeight - 1, 0 To filterWidth - 1)
  1938.             'horizontal -> Sobel h
  1939.             aFilter(0, 0) = 1
  1940.             aFilter(0, 1) = 2
  1941.             aFilter(0, 2) = 1
  1942.             aFilter(1, 0) = 0
  1943.             aFilter(1, 1) = 0
  1944.             aFilter(1, 2) = 0
  1945.             aFilter(2, 0) = -1
  1946.             aFilter(2, 1) = -2
  1947.             aFilter(2, 2) = -1
  1948.             'vertical -> Prewitt v
  1949.             aFilter2(0, 0) = 1
  1950.             aFilter2(0, 1) = 1
  1951.             aFilter2(0, 2) = 1
  1952.             aFilter2(1, 0) = 0
  1953.             aFilter2(1, 1) = 0
  1954.             aFilter2(1, 2) = 0
  1955.             aFilter2(2, 0) = -1
  1956.             aFilter2(2, 1) = -1
  1957.             aFilter2(2, 2) = -1
  1958.         Case 31 'Gaussian3x3
  1959.             ReDim aFilter(0 To filterHeight - 1, 0 To filterWidth - 1)
  1960.             aFilter(0, 0) = 1
  1961.             aFilter(0, 1) = 2
  1962.             aFilter(0, 2) = 1
  1963.             aFilter(1, 0) = 2
  1964.             aFilter(1, 1) = 4
  1965.             aFilter(1, 2) = 2
  1966.             aFilter(2, 0) = 1
  1967.             aFilter(2, 1) = 2
  1968.             aFilter(2, 2) = 1
  1969.     End Select
  1970.    
  1971.     fW = filterWidth / 2
  1972.     fH = filterHeight / 2
  1973.     For iY = 0 To iH - 1
  1974.         iRowOffset = iY * iW
  1975.         For iX = 0 To iW - 1
  1976.             fRedSum = 0.0
  1977.             fRedSumX = 0.0
  1978.             fRedSumY = 0.0
  1979.             fGreenSum = 0.0
  1980.             fGreenSumX = 0.0
  1981.             fGreenSumY = 0.0
  1982.             fBlueSum = 0.0
  1983.             fBlueSumX = 0.0
  1984.             fBlueSumY = 0.0
  1985.             fSum = 0.0
  1986.             For filterY = 0 To filterHeight - 1
  1987.                 For filterX = 0 To filterWidth - 1
  1988.                     imageX = Int(iX - fW + filterX + iW) Mod iW
  1989.                     imageY = Int(iY - fH + filterY + iH) Mod iH
  1990.                     c =  Cast(ULong Ptr, tBitmapData.Scan0)[imageY * iW + imageX]
  1991.                     iR = ((c Shr 16) And &hFF)
  1992.                     iG = ((c Shr 8) And &hFF)
  1993.                     iB = (c And &hFF)
  1994.                     Select Case iMode
  1995.                         Case 20, 24, 25, 30
  1996.                             fMatrixX = aFilter(filterY, filterX)
  1997.                             fMatrixY = aFilter2(filterY, filterX)
  1998.                             fRedSumX += iR * fMatrixX
  1999.                             fRedSumY += iR * fMatrixY
  2000.                             fGreenSumX += iG * fMatrixX
  2001.                             fGreenSumY += iG * fMatrixY
  2002.                             fBlueSumX += iB * fMatrixX
  2003.                             fBlueSumY += iB * fMatrixY
  2004.                             fSum += (fMatrixX + fMatrixY)
  2005.                         Case Else
  2006.                             fMatrix = aFilter(filterY, filterX)
  2007.                             fRedSum += iR * fMatrix
  2008.                             fGreenSum += iG * fMatrix
  2009.                             fBlueSum += iB * fMatrix
  2010.                             fSum += fMatrix
  2011.                     End Select
  2012.                 Next
  2013.             Next
  2014.             fSum = IIf(fSum <= 0, 1.0, fSum)
  2015.             Select Case iMode
  2016.                 Case 20, 24, 25, 30
  2017.                     fRedSum = Sqr(fRedSumX * fRedSumX + fRedSumY * fRedSumY)
  2018.                     fGreenSum = Sqr(fGreenSumX * fGreenSumX + fGreenSumY * fGreenSumY)
  2019.                     fBlueSum = Sqr(fBlueSumX * fBlueSumX + fBlueSumY * fBlueSumY)
  2020.                 Case Else
  2021.                    
  2022.             End Select         
  2023.             'iRGB = Min(Max(Int(fFactor * fRedSum / fSum + fBias), 0), 255) Shl 16 + Min(Max(Int(fFactor * fGreenSum / fSum + fBias), 0), 255) Shl 8 + Min(Max(Int(fFactor * fBlueSum / fSum + fBias), 0), 255)
  2024.             iRGB =  min(Abs(Int(fFactor * fRedSum / fSum + fBias)), 255) Shl 16 + _
  2025.                     min(Abs(Int(fFactor * fGreenSum / fSum + fBias)), 255) Shl 8 + _
  2026.                     min(Abs(Int(fFactor * fBlueSum / fSum + fBias)), 255)
  2027.             'iRGB = Min(Abs(Int(fFactor * fRedSum + fBias)), 255) Shl 16 + Min(Abs(Int(fFactor * fGreenSum + fBias)), 255) Shl 8 + Min(Abs(Int(fFactor * fBlueSum + fBias)), 255)
  2028.             Cast(ULong Ptr, tBitmapData_Dest.Scan0)[iRowOffset + iX] = iAlpha + iRGB
  2029.         Next
  2030.     Next
  2031.  
  2032.     GdipBitmapUnlockBits(hBitmap_Dest, @tBitmapData_Dest)
  2033.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2034.     If bGDI Then
  2035.         GdipCreateHBITMAPFromBitmap(hBitmap_Dest, @hGDIBitmap, &hFF000000)
  2036.         GdipDisposeImage(hBitmap_Dest)
  2037.         Return hGDIBitmap
  2038.     EndIf
  2039.     Return hBitmap_Dest
  2040. End Function
  2041.  
  2042. Function _GDIPlus_BitmapApplyFilter_Raster(ByVal hImage As Any Ptr, iSizeW As ULong, iSizeH As ULong, fDensity As Single, fBrightness As Single, fBias As Single, iMode As Byte, bGDI As BOOL) As Any Ptr Export '...'
  2043.     Dim As Any Ptr hBitmap_Raster, hGDIBitmap, hBitmap_BW, hBitmap_Grey, hBitmap_tmp, hBitmap_tmp2, hGfx, hGfx_tmp, hGfx_tmp2, hBrush
  2044.     Dim As Single iW, iH, fDen, fSizeW, fSizeH
  2045.     Dim As ULong iX, iY, iColor, iColor2, c, iWH
  2046.     Dim As Integer iStatus
  2047.  
  2048.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2049.     If iStatus <> 0 Then Return 0
  2050.    
  2051.     iSizeW = IIf(iSizeW < 3, 3, IIf(iSizeW > iW / 3, iW / 3, iSizeW))
  2052.     iSizeH = IIf(iSizeH < 3, 3, IIf(iSizeH > iH / 3, iH / 3, iSizeH))
  2053.    
  2054.     GdipCloneBitmapArea(0, 0, iW, iH, PixelFormat1bppIndexed, hImage, @hBitmap_BW)
  2055.     'hBitmap_BW = _GDIPlus_BitmapCreateBW(hImage, &h60)
  2056.     hBitmap_Grey = _GDIPlus_BitmapCreateGreyscale(hImage)
  2057.    
  2058.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Raster)
  2059.     GdipGetImageGraphicsContext(hBitmap_Raster, @hGfx)
  2060.     GdipSetSmoothingMode(hGfx, 4)
  2061.     GdipSetPixelOffsetMode(hGfx, 4)
  2062.     GdipGraphicsClear(hGfx, &hFFFFFFFF)
  2063.    
  2064.     GdipCreateBitmapFromScan0(iSizeW, iSizeH, 0, PixelFormat32bppARGB, 0, @hBitmap_tmp)
  2065.     GdipGetImageGraphicsContext(hBitmap_tmp, @hGfx_tmp)
  2066.    
  2067.     GdipCreateBitmapFromScan0(iSizeW, iSizeH, 0, PixelFormat32bppARGB, 0, @hBitmap_tmp2)
  2068.     GdipGetImageGraphicsContext(hBitmap_tmp2, @hGfx_tmp2)
  2069.     If iMode < 1 Then iMode = 1
  2070.     If fDensity < 0 Then fDensity = 0
  2071.     If fBrightness < 1 Then fBrightness = 1
  2072.    
  2073.     iWH = iSizeW * iSizeH
  2074.    
  2075.     For iY = 0 To iH - 1 Step iSizeH
  2076.         For iX = 0 To iW - 1 Step iSizeW
  2077.             GdipDrawImageRectRect(hGfx_tmp, hBitmap_Grey, 0, 0, iSizeW, iSizeH, iX, iY, iSizeW, iSizeH, 2, 0, 0, 0)
  2078.             GdipDrawImageRectRect(hGfx_tmp2, hImage, 0, 0, iSizeW, iSizeH, iX, iY, iSizeW, iSizeH, 2, 0, 0, 0)
  2079.             iColor = _GDIPlus_BitmapGetAverageColorValue(hBitmap_tmp, 1)
  2080.             iColor2 = _GDIPlus_BitmapGetAverageColorValue(hBitmap_tmp2, 0)
  2081.             If iMode = 1 Then
  2082.                 GdipCreateSolidFill(iColor2, @hBrush)
  2083.             Else
  2084.                 GdipCreateSolidFill(&hFF000000, @hBrush)
  2085.             End If
  2086.             c = (((iColor Shr 16) And &hFF) + ((iColor Shr 8) And &hFF) + (iColor And &hFF)) / fBrightness
  2087.             fDen = fDensity + c / iWH
  2088.             fSizeW = iSizeW * fDen
  2089.             fSizeW = IIf(fSizeW > iSizeW + fBias, iSizeW, fSizeW + fBias)
  2090.             fSizeH = iSizeH * fDen
  2091.             fSizeH = IIf(fSizeH > iSizeH + fBias, iSizeH, fSizeH + fBias)
  2092.             GdipFillEllipse(hGfx, hBrush, iX + (iSizeW - fSizeW) / 2, iY + (iSizeH - fSizeH) / 2, fSizeW, fSizeH)
  2093.             GdipDeleteBrush(hBrush)
  2094.         Next
  2095.     Next
  2096.    
  2097.     GdipDeleteGraphics(hGfx_tmp)
  2098.     GdipDisposeImage(hBitmap_tmp)
  2099.     GdipDeleteGraphics(hGfx_tmp2)
  2100.     GdipDisposeImage(hBitmap_tmp2)
  2101.     GdipDeleteGraphics(hGfx)
  2102.     'GdipDisposeImage(hBitmap_BW)
  2103.     GdipDisposeImage(hBitmap_Grey)
  2104.    
  2105.     If bGDI Then
  2106.         GdipCreateHBITMAPFromBitmap(hBitmap_Raster, @hGDIBitmap, &hFF000000)
  2107.         GdipDisposeImage(hBitmap_Raster)
  2108.         Return hGDIBitmap
  2109.     EndIf
  2110.     Return hBitmap_Raster
  2111. End Function
  2112.  
  2113. Function _GDIPlus_BitmapApplyFilter_Rasterize(ByVal hImage As Any Ptr, iSpaceX As ULong, iSpaceY As ULong, iDelCol As ULong, bGDI As BOOL) As Any Ptr Export '...'
  2114.     Dim As Any Ptr hBitmap_Rasterize, hGDIBitmap, hGfx_Rasterize, hBrush
  2115.     Dim As Single iW, iH
  2116.     Dim As ULong iX, iY
  2117.     Dim As Integer iStatus
  2118.  
  2119.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2120.     If iStatus <> 0 Then Return 0
  2121.    
  2122.     iSpaceX = IIf(iSpaceX < 2, 2, IIf(iSpaceX > iW - 1, iW - 1, iSpaceX))
  2123.     iSpaceY = IIf(iSpaceY < 2, 2, IIf(iSpaceY > iH - 1, iH - 1, iSpaceX))
  2124.    
  2125.    
  2126.     GdipCloneBitmapArea(0, 0, iW, iH, PixelFormat32bppARGB, hImage, @hBitmap_Rasterize)
  2127.     'GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Rasterize)
  2128.     GdipGetImageGraphicsContext(hBitmap_Rasterize, @hGfx_Rasterize)
  2129.     'GdipDrawImageRect(hGfx_Rasterize, hImage, 0, 0, iW, iH)
  2130.     GdipCreateSolidFill(iDelCol, @hBrush)
  2131.        
  2132.     For iX = 0 To iW - 1 Step iSpaceX
  2133.         GdipFillRectangle(hGfx_Rasterize, hBrush, iX, 0, iSpaceX - 1, iH)
  2134.     Next
  2135.  
  2136.     For iY = 0 To iH - 1 Step iSpaceY
  2137.         GdipFillRectangle(hGfx_Rasterize, hBrush, 0, iY, iW, iSpaceY - 1)
  2138.     Next
  2139.    
  2140.     GdipDeleteBrush(hBrush)
  2141.     GdipDeleteGraphics(hGfx_Rasterize)
  2142.    
  2143.     If bGDI Then
  2144.         GdipCreateHBITMAPFromBitmap(hBitmap_Rasterize, @hGDIBitmap, &hFF000000)
  2145.         GdipDisposeImage(hBitmap_Rasterize)
  2146.         Return hGDIBitmap
  2147.     EndIf
  2148.     Return hBitmap_Rasterize
  2149. End Function
  2150.  
  2151. Function _GDIPlus_BitmapApplyFilter_Pixelate(ByVal hImage As Any Ptr, iPixelate As UByte, bGrid As BOOL, bGDI As BOOL) As Any Ptr Export '...'
  2152.     Dim As Single iW, iH, i
  2153.     Dim As ULong iNewW, iNewH
  2154.     Dim As Any Ptr hBitmap_scaled, hBitmap_pixelated, hGDIBitmap, hGfx, hPen
  2155.     Dim As Integer iStatus
  2156.  
  2157.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2158.     If iStatus <> 0 Then Return 0
  2159.    
  2160.     iPixelate = IIf(iPixelate < 2, 2, iPixelate)
  2161.     iNewW = CLng(iW / iPixelate)
  2162.     iNewH = CLng(iH / iPixelate)
  2163.  
  2164.     GdipCreateBitmapFromScan0(iNewW, iNewH, 0, PixelFormat32bppARGB, 0, @hBitmap_scaled)
  2165.     GdipGetImageGraphicsContext(hBitmap_scaled, @hGfx)
  2166.     GdipSetInterpolationMode(hGfx, 7)
  2167.     GdipDrawImageRect(hGfx, hImage, 0, 0, iNewW, iNewH)
  2168.     GdipDeleteGraphics(hGfx)
  2169.    
  2170.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_pixelated)
  2171.     GdipGetImageGraphicsContext(hBitmap_pixelated, @hGfx)
  2172.     GdipSetInterpolationMode(hGfx, 5)
  2173.     GdipSetPixelOffsetMode(hGfx, 2)
  2174.     GdipDrawImageRectRect(hGfx, hBitmap_scaled, 0, 0, iW, iH, 0, 0, iNewW, iNewH, 2, 0, 0, 0)
  2175.    
  2176.     If bGrid Then
  2177.         GdipCreatePen1(&h80000000, 1, 2, @hPen)
  2178.         Dim As Single iStepW = iW / iNewW, iStepH = iH / iNewH
  2179.         For i = 0 To iW - 1 Step iStepW
  2180.             GdipDrawLine(hGfx, hPen, i, 0, i, iH)
  2181.         Next
  2182.        
  2183.         For i = 0 To iH - 1 Step iStepH
  2184.             GdipDrawLine(hGfx, hPen, 0, i, iW, i)
  2185.         Next
  2186.            
  2187.         GdipDeletePen(hPen)
  2188.     End If
  2189.    
  2190.     GdipDeleteGraphics(hGfx)
  2191.     GdipDisposeImage(hBitmap_scaled)
  2192.     If bGDI Then
  2193.         GdipCreateHBITMAPFromBitmap(hBitmap_pixelated, @hGDIBitmap, &hFF000000)
  2194.         GdipDisposeImage(hBitmap_pixelated)
  2195.         Return hGDIBitmap
  2196.     EndIf
  2197.     Return hBitmap_pixelated
  2198. End Function
  2199.  
  2200. Function _GDIPlus_BitmapApplyFilter_Dilatation(ByVal hImage As Any Ptr, Size As UByte, bGDI As BOOL) As Any Ptr Export 'based on original code by Jakub Szymanowski '...'
  2201.     Dim As Single iW, iH
  2202.     Dim As Any Ptr hBitmap_Dilate, hGDIBitmap
  2203.     Dim As BitmapData tBitmapData, tBitmapData_Dilate
  2204.     Dim As Long iX, iY, x2, y2, TempX, TempY, TempColor, c, r, g, b, RValue, GValue, BValue, ApetureMin, ApetureMax
  2205.     Dim As Integer iStatus
  2206.  
  2207.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2208.     If iStatus <> 0 Then Return 0
  2209.    
  2210.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2211.    
  2212.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dilate)
  2213.     GdipBitmapLockBits(hBitmap_Dilate, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Dilate)
  2214.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2215.    
  2216.     Size = IIf(Size < 2, 2, IIf(Size > 32, 32, Size))
  2217.    
  2218.     ApetureMin = -(Size / 2)
  2219.     ApetureMax = (Size / 2)
  2220.    
  2221.     For iX = 0 To iW - 1
  2222.         For iY = 0 To iH - 1
  2223.             RValue = 0
  2224.             GValue = 0
  2225.             BValue = 0
  2226.             For x2 = ApetureMin To ApetureMax
  2227.                     TempX = iX + x2        
  2228.                     If (TempX >= 0) And (TempX < iW) Then
  2229.                         For y2 = ApetureMin To ApetureMax
  2230.                             TempY = iY + y2
  2231.                             If (TempY >= 0) And (TempY < iH) Then
  2232.                                 TempColor = Cast(ULong Ptr, tBitmapData.Scan0)[(TempY * iW) + TempX]
  2233.                                
  2234.                                 r = (TempColor Shr 16) And &hFF
  2235.                                 g = (TempColor Shr 8) And &hFF
  2236.                                 b = TempColor And &hFF
  2237.  
  2238.                                 If r > RValue Then RValue = r
  2239.                                 If g > GValue Then GValue = g
  2240.                                 If b > BValue Then BValue = b
  2241.  
  2242.                             End If
  2243.                         Next
  2244.                     End If                 
  2245.             Next
  2246.             Cast(ULong Ptr, tBitmapData_Dilate.Scan0)[iY * iW + iX] = &hFF000000 + RValue Shl 16 + GValue Shl 8 + BValue
  2247.         Next
  2248.     Next
  2249.     GdipBitmapUnlockBits(hBitmap_Dilate, @tBitmapData_Dilate)
  2250.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2251.    
  2252.     If bGDI Then
  2253.         GdipCreateHBITMAPFromBitmap(hBitmap_Dilate, @hGDIBitmap, &hFF000000)
  2254.         GdipDisposeImage(hBitmap_Dilate)
  2255.         Return hGDIBitmap
  2256.     EndIf
  2257.     Return hBitmap_Dilate  
  2258. End Function
  2259.  
  2260. Function _GDIPlus_BitmapApplyFilter_Erosion(ByVal hImage As Any Ptr, Size As UByte, bGDI As BOOL) As Any Ptr Export 'based on original code by Jakub Szymanowski '...'
  2261.     Dim As Single iW, iH
  2262.     Dim As Any Ptr hBitmap_Erosion, hGDIBitmap
  2263.     Dim As BitmapData tBitmapData, tBitmapData_Erosion
  2264.     Dim As Long iX, iY, x2, y2, TempX, TempY, TempColor, c, r, g, b, RValue, GValue, BValue, ApetureMin, ApetureMax
  2265.     Dim As Integer iStatus
  2266.  
  2267.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2268.     If iStatus <> 0 Then Return 0
  2269.    
  2270.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2271.    
  2272.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Erosion)
  2273.     GdipBitmapLockBits(hBitmap_Erosion, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Erosion)
  2274.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2275.    
  2276.     Size = IIf(Size < 2, 2, IIf(Size > 32, 32, Size))
  2277.    
  2278.     ApetureMin = -(Size / 2)
  2279.     ApetureMax = (Size / 2)
  2280.    
  2281.     For iX = 0 To iW - 1
  2282.         For iY = 0 To iH - 1
  2283.             RValue = &hFF
  2284.             GValue = &hFF
  2285.             BValue = &hFF
  2286.             For x2 = ApetureMin To ApetureMax
  2287.                     TempX = iX + x2        
  2288.                     If (TempX >= 0) And (TempX < iW) Then
  2289.                         For y2 = ApetureMin To ApetureMax
  2290.                             TempY = iY + y2
  2291.                             If (TempY >= 0) And (TempY < iH) Then
  2292.                                 TempColor = Cast(ULong Ptr, tBitmapData.Scan0)[(TempY * iW) + TempX]
  2293.                                
  2294.                                 r = (TempColor Shr 16) And &hFF
  2295.                                 g = (TempColor Shr 8) And &hFF
  2296.                                 b = TempColor And &hFF
  2297.  
  2298.                                 If r < RValue Then RValue = r
  2299.                                 If g < GValue Then GValue = g
  2300.                                 If b < BValue Then BValue = b
  2301.  
  2302.                             End If
  2303.                         Next
  2304.                     End If                 
  2305.             Next
  2306.             Cast(ULong Ptr, tBitmapData_Erosion.Scan0)[iY * iW + iX] = &hFF000000 + RValue Shl 16 + GValue Shl 8 + BValue
  2307.         Next
  2308.     Next
  2309.     GdipBitmapUnlockBits(hBitmap_Erosion, @tBitmapData_Erosion)
  2310.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2311.    
  2312.     If bGDI Then
  2313.         GdipCreateHBITMAPFromBitmap(hBitmap_Erosion, @hGDIBitmap, &hFF000000)
  2314.         GdipDisposeImage(hBitmap_Erosion)
  2315.         Return hGDIBitmap
  2316.     EndIf
  2317.     Return hBitmap_Erosion 
  2318. End Function
  2319.  
  2320. Function _GDIPlus_BitmapApplyFilter_OilPainting(ByVal hImage As Any Ptr, iRadius As UByte, fIntensityLevels As Single, bGDI As BOOL) As Any Ptr Export 'based on original code by Santhosh G_ (http://www.codeproject.com/Articles/471994/OilPaintEffect) '...'
  2321.     Dim As Single iW, iH
  2322.     Dim As Any Ptr hBitmap_OilPainting, hGDIBitmap
  2323.     Dim As BitmapData tBitmapData, tBitmapData_OilPainting
  2324.     Dim As Long i, iX, iY, iX_O, iY_O, iR, iG, iB, iCurIntensity, iCurMax, iMaxIndex, iRowOffset
  2325.     Dim As Single aSumR(0 To 255), aSumG(0 To 255), aSumB(0 To 255), aIntensityCount(0 To 255)
  2326.     Dim As Const Single fI = fIntensityLevels / 255
  2327.     Dim As ULong TempColor, c
  2328.     Dim As Integer iStatus, iPosX, iPosY
  2329.  
  2330.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2331.     If iStatus <> 0 Then Return 0
  2332.    
  2333.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2334.    
  2335.     iRadius = IIf(iRadius < 1, 1, IIf(iRadius > 32, 32, iRadius))
  2336.    
  2337.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_OilPainting)
  2338.    
  2339.     GdipBitmapLockBits(hBitmap_OilPainting, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_OilPainting)
  2340.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2341.    
  2342.    
  2343.     For iY = 0 To iH  - 1
  2344.         iRowOffset = iY  * iW
  2345.         For iX = 0 To iW  - 1
  2346.            
  2347.             For iY_O = -iRadius To iRadius
  2348.                 For iX_O = -iRadius To iRadius
  2349.                    
  2350.                     iPosX = Int((iX + iX_O) Mod iW)
  2351.                     iPosY = Int((iY + iY_O) Mod iH)
  2352.                     If iPosX < 0 Then iPosX = 0
  2353.                     If iPosY < 0 Then iPosY = 0
  2354.  
  2355.                     TempColor = Cast(ULong Ptr, tBitmapData.Scan0)[iPosY * iW + iPosX]
  2356.                    
  2357.                     iR = (TempColor Shr 16) And &hFF
  2358.                     iG = (TempColor Shr 8) And &hFF
  2359.                     iB = TempColor And &hFF
  2360.                    
  2361.                     iCurIntensity = (iR * 213 + iG * 715 + iB * 72) / 1000 * fI ' luminance method
  2362.                     If iCurIntensity > 255 Then iCurIntensity = 255
  2363.  
  2364.                     aIntensityCount(iCurIntensity) += 1
  2365.                     aSumR(Int(iCurIntensity)) += iR
  2366.                     aSumG(Int(iCurIntensity)) += iG
  2367.                     aSumB(Int(iCurIntensity)) += iB
  2368.                 Next
  2369.             Next
  2370.             iCurMax = 0
  2371.             iMaxIndex = 0
  2372.             For i = 0 To 255
  2373.                 If aIntensityCount(i) > iCurMax Then
  2374.                     iCurMax = aIntensityCount(i)
  2375.                     iMaxIndex = i
  2376.                 EndIf
  2377.             Next
  2378.            
  2379.             Cast(ULong Ptr, tBitmapData_OilPainting.Scan0)[iRowOffset + iX] = &hFF000000 + (aSumR(iMaxIndex) / iCurMax) Shl 16 + (aSumG(iMaxIndex) / iCurMax) Shl 8 + (aSumB(iMaxIndex) / iCurMax)
  2380.            
  2381.             For i = 0 To 255
  2382.                 aIntensityCount(i) = 0
  2383.                 aSumR(i) = 0
  2384.                 aSumG(i) = 0
  2385.                 aSumB(i) = 0
  2386.             Next
  2387.            
  2388.         Next
  2389.     Next
  2390.     GdipBitmapUnlockBits(hBitmap_OilPainting, @tBitmapData_OilPainting)
  2391.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2392.    
  2393.     If bGDI Then
  2394.         GdipCreateHBITMAPFromBitmap(hBitmap_OilPainting, @hGDIBitmap, &hFF000000)
  2395.         GdipDisposeImage(hBitmap_OilPainting)
  2396.         Return hGDIBitmap
  2397.     EndIf
  2398.     Return hBitmap_OilPainting
  2399. End Function
  2400.  
  2401. Function _GDIPlus_BitmapApplyFilter_ColorAccent(ByVal hImage As Any Ptr, iHue As UShort, fRange As Single, bGDI As BOOL) As Any Ptr Export 'based on original code by Jakub Szymanowski '...'
  2402.     Dim As Single iW, iH
  2403.     Dim As Any Ptr hBitmap_ColorAccent, hGDIBitmap
  2404.     Dim As BitmapData tBitmapData, tBitmapData_ColorAccent
  2405.     Dim As Long iX, iY, iRowOffset, c, iR, iG, iB
  2406.     Dim As Single fH, fCMax, fCMin, fDelta, fH1, fH2
  2407.     Dim As Integer iStatus
  2408.  
  2409.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2410.     If iStatus <> 0 Then Return 0
  2411.    
  2412.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2413.    
  2414.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_ColorAccent)
  2415.     GdipBitmapLockBits(hBitmap_ColorAccent, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_ColorAccent)
  2416.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2417.    
  2418.     fH1 = (iHue - fRange / 2 + 360) Mod 360
  2419.     fH2 = (iHue + fRange / 2 + 360) Mod 360
  2420.     For iY = 0 To iH - 1
  2421.         iRowOffset = iY * iW
  2422.         For iX = 1 To iW - 1
  2423.             c = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  2424.             iR = (c Shr 16) And &hFF
  2425.             iG = (c Shr 8) And &hFF
  2426.             iB = c And &hFF
  2427.            
  2428.             'convert RGB to Hue value only     
  2429.             fCMax = _Max3(iR, iG, iB)
  2430.             fCMin = _Min3(iR, iG, iB)
  2431.             fDelta = fCMax - fCMin
  2432.            
  2433.             If fDelta = 0 Then fH = 0
  2434.             If fCMax = iR Then fH = 60 * (((iG - iB) / fDelta) Mod 6)
  2435.             If fCMax = iG Then fH = 60 * (((iB - iR) / fDelta) + 2)
  2436.             If fCMax = iB Then fH = 60 * (((iR - iG) / fDelta) + 4)
  2437.            
  2438.             If fH1 <= fH2 Then
  2439.                 If fH >= fH1 And fH <= fH2 Then
  2440.                     Cast(ULong Ptr, tBitmapData_ColorAccent.Scan0)[iRowOffset + iX] = &hFF000000 + c
  2441.                 Else
  2442.                     c = Int((iR * 213 + iG * 715 + iB * 72) / 1000)
  2443.                     Cast(ULong Ptr, tBitmapData_ColorAccent.Scan0)[iRowOffset + iX] = &hFF000000 + c Shl 16 + c Shl 8 + c
  2444.                 EndIf
  2445.             Else
  2446.                 If fH >= fH1 Or fH <= fH2 Then
  2447.                     Cast(ULong Ptr, tBitmapData_ColorAccent.Scan0)[iRowOffset + iX] = &hFF000000 + c
  2448.                 Else
  2449.                     c = Int((iR * 213 + iG * 715 + iB * 72) / 1000)
  2450.                     Cast(ULong Ptr, tBitmapData_ColorAccent.Scan0)[iRowOffset + iX] = &hFF000000 + c Shl 16 + c Shl 8 + c              
  2451.                 EndIf
  2452.             EndIf
  2453.         Next
  2454.     Next
  2455.    
  2456.     GdipBitmapUnlockBits(hBitmap_ColorAccent, @tBitmapData_ColorAccent)
  2457.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2458.     If bGDI Then
  2459.         GdipCreateHBITMAPFromBitmap(hBitmap_ColorAccent, @hGDIBitmap, &hFF000000)
  2460.         GdipDisposeImage(hBitmap_ColorAccent)
  2461.         Return hGDIBitmap
  2462.     EndIf
  2463.     Return hBitmap_ColorAccent
  2464. End Function
  2465.  
  2466. Function _GDIPlus_BitmapApplyFilter_PenSketch(ByVal hImage As Any Ptr, iThreshold As Single, bGDI As BOOL) As Any Ptr Export 'based on original code by Jakub Szymanowski '...'
  2467.     Dim As Any Ptr hBitmap_PenSketch, hBitmap_Greyscale, hBitmap_Edge, hBitmap_Negative, hBitmap_Blur, hGDIBitmap
  2468.    
  2469.     hBitmap_Greyscale = _GDIPlus_BitmapCreateGreyscale(hImage)
  2470.     hBitmap_Edge = _GDIPlus_BitmapApplyFilter_Convolution(hBitmap_Greyscale, 1.25, iThreshold, 20, 0, 0, 0)
  2471.     hBitmap_Negative = _GDIPlus_BitmapCreateNegative(hBitmap_Edge)
  2472.     hBitmap_Blur = _GDIPlus_BitmapApplyFilter_Convolution(hBitmap_Negative, 1.15, 0, 8, 0, 0, 0)
  2473.     hBitmap_PenSketch = _GDIPlus_BitmapApplyFilter_SymmetricNearestNeighbour(hBitmap_Blur, 6, 0)
  2474.    
  2475.     GdipDisposeImage(hBitmap_Greyscale)
  2476.     GdipDisposeImage(hBitmap_Edge)
  2477.     GdipDisposeImage(hBitmap_Negative)
  2478.     GdipDisposeImage(hBitmap_Blur)
  2479.    
  2480.     If bGDI Then
  2481.         GdipCreateHBITMAPFromBitmap(hBitmap_PenSketch, @hGDIBitmap, &hFF000000)
  2482.         GdipDisposeImage(hBitmap_PenSketch)
  2483.         Return hGDIBitmap
  2484.     EndIf
  2485.     Return hBitmap_PenSketch
  2486. End Function
  2487.  
  2488. Function _GDIPlus_BitmapApplyFilter_PenSketch2(ByVal hImage As Any Ptr, iThreshold As UByte, bGDI As BOOL) As Any Ptr Export '...'
  2489.     Dim As Any Ptr hBitmap_PenSketch2, hBitmap_Median, hBitmap_Median2, hBitmap_Edge, hBitmap_Inverse, hGDIBitmap
  2490.  
  2491.     hBitmap_Median = _GDIPlus_BitmapApplyFilter_Median(hImage, 4, 0)
  2492.     hBitmap_Edge = _GDIPlus_BitmapApplyFilter_Convolution(hBitmap_Median, 1, iThreshold, 20, 0, 0, 0)
  2493.     hBitmap_Inverse = _GDIPlus_BitmapCreateInverseGreyscale(hBitmap_Edge, 80)
  2494.     hBitmap_Median2 = _GDIPlus_BitmapApplyFilter_Median(hBitmap_Inverse, 3, 0)
  2495.     hBitmap_PenSketch2 = _GDIPlus_BitmapCreateNegative(hBitmap_Median2)
  2496.    
  2497.     GdipDisposeImage(hBitmap_Median)
  2498.     GdipDisposeImage(hBitmap_Edge)
  2499.     GdipDisposeImage(hBitmap_Inverse)
  2500.     GdipDisposeImage(hBitmap_Median2)
  2501.    
  2502.     If bGDI Then
  2503.         GdipCreateHBITMAPFromBitmap(hBitmap_PenSketch2, @hGDIBitmap, &hFF000000)
  2504.         GdipDisposeImage(hBitmap_PenSketch2)
  2505.         Return hGDIBitmap
  2506.     EndIf
  2507.     Return hBitmap_PenSketch2
  2508. End Function
  2509.  
  2510. Function _GDIPlus_BitmapApplyFilter_Cartoon1(ByVal hImage As Any Ptr, iRadius As UByte, fIntensityLevels As Single, iThreshold As UByte, bGDI As BOOL) As Any Ptr Export 'based on original code by Jakub Szymanowski '...'
  2511.     Dim As Single iW, iH
  2512.     Dim As Any Ptr hBitmap_Cartoon1, hGDIBitmap, hBitmap_Oil, hBitmap_Edge, hBitmap_Sobel, hBitmap_Blur, hGfx, hPen
  2513.     Dim As BitmapData tBitmapData, tBitmapData_Edge, tBitmapData_Oil, tBitmapData_Cartoon1
  2514.     Dim As Long iX, iY, iRowOffset, cE, cO, iRed
  2515.     Dim As Integer iStatus
  2516.  
  2517.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2518.     If iStatus <> 0 Then Return 0
  2519.    
  2520.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2521.    
  2522.     hBitmap_Oil = _GDIPlus_BitmapApplyFilter_OilPainting(hImage, iRadius, fIntensityLevels, 0)
  2523.     hBitmap_Sobel = _GDIPlus_BitmapApplyFilter_Convolution(hImage, 1.0, 32, 20, 0, 0, 0)
  2524.     hBitmap_Blur = _GDIPlus_BitmapApplyFilter_Convolution(hBitmap_Sobel, 1, 0, 28, 0, 0, 0)
  2525.     hBitmap_Edge = _GDIPlus_BitmapCreateInverseBW(hBitmap_Blur, iThreshold)
  2526.    
  2527.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Cartoon1)
  2528.     GdipBitmapLockBits(hBitmap_Cartoon1, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Cartoon1)
  2529.     GdipBitmapLockBits(hBitmap_Edge, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData_Edge)
  2530.     GdipBitmapLockBits(hBitmap_Oil, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData_Oil)
  2531.  
  2532.     For iY = 0 To iH - 1
  2533.         iRowOffset = iY * iW
  2534.         For iX = 1 To iW - 1
  2535.             cE = Cast(ULong Ptr, tBitmapData_Edge.Scan0)[iRowOffset + iX]
  2536.             iRed = (cE Shr 16) And &hFF
  2537.             If iRed < &h80 Then
  2538.                 cO = Cast(ULong Ptr, tBitmapData_Oil.Scan0)[iRowOffset + iX]
  2539.                 Cast(ULong Ptr, tBitmapData_Cartoon1.Scan0)[iRowOffset + iX] = cO
  2540.             Else
  2541.                 Cast(ULong Ptr, tBitmapData_Cartoon1.Scan0)[iRowOffset + iX] = cE Xor &h00FFFFFF
  2542.             EndIf
  2543.         Next
  2544.     Next
  2545.    
  2546.     GdipBitmapUnlockBits(hBitmap_Cartoon1, @tBitmapData_Cartoon1)
  2547.     GdipBitmapUnlockBits(hBitmap_Edge, @tBitmapData_Edge)
  2548.     GdipBitmapUnlockBits(hBitmap_Oil, @tBitmapData_Oil)
  2549.    
  2550.     GdipDisposeImage(hBitmap_Oil)
  2551.     GdipDisposeImage(hBitmap_Sobel)
  2552.     GdipDisposeImage(hBitmap_Edge)
  2553.     GdipDisposeImage(hBitmap_Blur)
  2554.    
  2555.     /'
  2556.     GdipGetImageGraphicsContext(hBitmap_Cartoon1, @hGfx)
  2557.     Dim As Single fSize, fRadius
  2558.     fSize = iRadius
  2559.     fRadius = fSize / 2
  2560.     GdipCreatePen1(&hFF000000, fSize, 2, @hPen)
  2561.     GdipDrawRectangle(hGfx, hPen, fRadius, fRadius, iW - fSize, iH - fSize)
  2562.     GdipDeletePen(hPen)
  2563.     GdipDeleteGraphics(hGfx)
  2564.     '/
  2565.    
  2566.     If bGDI Then
  2567.         GdipCreateHBITMAPFromBitmap(hBitmap_Cartoon1, @hGDIBitmap, &hFF000000)
  2568.         GdipDisposeImage(hBitmap_Cartoon1)
  2569.         Return hGDIBitmap
  2570.     EndIf
  2571.     Return hBitmap_Cartoon1
  2572. End Function
  2573.  
  2574. Function _GDIPlus_BitmapApplyFilter_TiltShift(ByVal hImage As Any Ptr, fPosY_Start As Single, iIntensity As UByte, bGDI As BOOL) As Any Ptr Export 'based on original code by Jakub Szymanowski '...'
  2575.     Dim As Single iW, iH, fCounterR, fCounterG, fCounterB, fDominator, sigma, fPosY_End
  2576.     Dim As Double gauss, L, S
  2577.     Dim As Any Ptr hBitmap_TiltShift, hGDIBitmap
  2578.     Dim As BitmapData tBitmapData, tBitmapData_TiltShift
  2579.     Dim As Long iStatus, iX, iY, k, b, y, y1, y2, newR, newG, newB, c, iARGB, iARGB1, iARGB2, iOffset
  2580.  
  2581.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2582.     If iStatus <> 0 Then Return 0
  2583.    
  2584.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2585.    
  2586.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_TiltShift)
  2587.     GdipBitmapLockBits(hBitmap_TiltShift, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_TiltShift)
  2588.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2589.    
  2590.     fPosY_End = (fPosY_Start + fPosY_Start / 2)
  2591.     If fPosY_End > iH Or fPosY_Start < 0 Then
  2592.         fPosY_Start = iH / 2
  2593.         fPosY_End = fPosY_Start + fPosY_Start / 2
  2594.     EndIf
  2595.     L = 0.5 'is defined function value for blurring in close proximity to sharp part (ex. in range 0.05 - 0.15)
  2596.     S = 6.5 'is defined function value for blurring in far proximity to sharp part (ex. in range 5 - 7)
  2597.     Dim Filter(0 To iIntensity) As Double
  2598.    
  2599.     For iY = 0 To iH - 1
  2600.         iOffset = iY * iW
  2601.         For iX = 0 To iW - 1
  2602.             If (iY >= fPosY_Start) And (iY <= fPosY_End) Then
  2603.                 Cast(ULong Ptr, tBitmapData_TiltShift.Scan0)[iOffset + iX] = Cast(ULong Ptr, tBitmapData.Scan0)[iOffset + iX]
  2604.             Else
  2605.                 If iY < fPosY_Start Then
  2606.                     sigma = L  + (S - L) * (fPosY_Start - iY) / fPosY_Start     'fPosY_Start - iY = is height of larger blurred field (in vertical blurring)
  2607.                 ElseIf iY > fPosY_End Then                                              'fPosY_Start = is height of larger blurred field (in vertical blurring)
  2608.                     sigma = L  + (S - L) * (iY - fPosY_End) / fPosY_Start
  2609.                 EndIf
  2610.                 'ReDim Filter(0 To iIntensity) As Double
  2611.                 c = 0
  2612.                 For k = 0 To iIntensity - 1
  2613.                     gauss = (1 / (fPiSqr * sigma)) * Exp((-k * k) / (2 * sigma * sigma))
  2614.                     If Not (gauss < 0.003) Then
  2615.                         Filter(c) = gauss
  2616.                         c += 1
  2617.                     EndIf  
  2618.                 Next
  2619.                 iARGB = Cast(ULong Ptr, tBitmapData.Scan0)[iOffset + iX]
  2620.                 fCounterR = Filter(0) * ((iARGB Shr 16) And &hFF)
  2621.                 fCounterG = Filter(0) * ((iARGB Shr 8) And &hFF)
  2622.                 fCounterB = Filter(0) * (iARGB And &hFF)
  2623.                 fDominator = Filter(0)
  2624.                 For b = 1 To c - 1
  2625.                     fDominator += 2 * Filter(b)
  2626.                     y1 = iY - b
  2627.                     y2 = iY + b
  2628.                     If y1 < 0 Then
  2629.                         y1 = Abs(y1)
  2630.                     ElseIf y2 >= iH Then
  2631.                         y2 = y2 + b - iH + 1
  2632.                     Else
  2633.                         iARGB1 = Cast(ULong Ptr, tBitmapData.Scan0)[y1 * iW + iX]
  2634.                         iARGB2 = Cast(ULong Ptr, tBitmapData.Scan0)[y2 * iW + iX]
  2635.                         fCounterR += Filter(b) * (((iARGB1 Shr 16) And &hFF) + ((iARGB2 Shr 16) And &hFF))
  2636.                         fCounterG += Filter(b) * (((iARGB1 Shr 8) And &hFF) + ((iARGB2 Shr 8) And &hFF))
  2637.                         fCounterB += Filter(b) * ((iARGB1 And &hFF) + (iARGB2 And &hFF))
  2638.                     EndIf
  2639.                 Next
  2640.                
  2641.                 newR = Int(fCounterR / fDominator)
  2642.                 newG = Int(fCounterG / fDominator)
  2643.                 newB = Int(fCounterB / fDominator)
  2644.                
  2645.                 newR = IIf(newR < 0, 0, IIf(newR > 255, 255, newR))
  2646.                 newG = IIf(newG < 0, 0, IIf(newG > 255, 255, newG))
  2647.                 newB = IIf(newB < 0, 0, IIf(newB > 255, 255, newB))
  2648.                
  2649.                 Cast(ULong Ptr, tBitmapData_TiltShift.Scan0)[iOffset + iX] = &hFF000000 + (newR Shl 16) + (newG Shl 8) + newB
  2650.             End If
  2651.         Next
  2652.     Next
  2653.    
  2654.    
  2655.     GdipBitmapUnlockBits(hBitmap_TiltShift, @tBitmapData_TiltShift)
  2656.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2657.     If bGDI Then
  2658.         GdipCreateHBITMAPFromBitmap(hBitmap_TiltShift, @hGDIBitmap, &hFF000000)
  2659.         GdipDisposeImage(hBitmap_TiltShift)
  2660.         Return hGDIBitmap
  2661.     EndIf
  2662.     Return hBitmap_TiltShift
  2663. End Function
  2664.  
  2665. Function _GDIPlus_BitmapApplyFilter_RadialBlur(ByVal hImage As Any Ptr, fPosX As Single, fPosY As Single, fRadius As Single, iIntensity As UByte, bGDI As BOOL) As Any Ptr Export 'based on original code by Jakub Szymanowski '...'
  2666.     Dim As Single iW, iH, fCounterR, fCounterG, fCounterB, fDominator
  2667.     Dim As Double gauss, L, S, H, V, R, sigma
  2668.     Dim As Any Ptr hBitmap_RadialBlur, hGDIBitmap
  2669.     Dim As BitmapData tBitmapData, tBitmapData_RadialBlur
  2670.     Dim As Long iStatus, iX, iY, k, b, y, y1, y2, newR, newG, newB, c, iARGB, iARGB1, iARGB2, iOffset
  2671.  
  2672.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2673.     If iStatus <> 0 Then Return 0
  2674.    
  2675.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2676.    
  2677.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_RadialBlur)
  2678.     GdipBitmapLockBits(hBitmap_RadialBlur, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_RadialBlur)
  2679.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2680.    
  2681.     L = 0.15 'is defined function value for blurring in close proximity to sharp part (ex. in range 0.05 - 0.15)
  2682.     S = 6.0 'is defined function value for blurring in far proximity to sharp part (ex. in range 5 - 7)
  2683.     Dim Filter(0 To iIntensity) As Double
  2684.    
  2685.     For iY = 0 To iH - 1
  2686.         iOffset = iY * iW
  2687.         For iX = 0 To iW - 1
  2688.             H = Abs(iX - fPosX)
  2689.          V = Abs(iY - fPosY)
  2690.          R = Sqr(H * H + V * V)
  2691.             If R < fRadius Then
  2692.                 Cast(ULong Ptr, tBitmapData_RadialBlur.Scan0)[iOffset + iX] = Cast(ULong Ptr, tBitmapData.Scan0)[iOffset + iX]
  2693.             Else
  2694.                 sigma = L  + (S - L) * R / (2 * fRadius)
  2695.                
  2696.                 c = 0
  2697.                 For k = 0 To iIntensity - 1
  2698.                     gauss = (1 / (fPiSqr * sigma)) * Exp((-k * k) / (2 * sigma * sigma))
  2699.                     If Not (gauss < 0.003) Then
  2700.                         Filter(c) = gauss
  2701.                         c += 1
  2702.                     EndIf  
  2703.                 Next
  2704.                 iARGB = Cast(ULong Ptr, tBitmapData.Scan0)[iOffset + iX]
  2705.                 fCounterR = Filter(0) * ((iARGB Shr 16) And &hFF)
  2706.                 fCounterG = Filter(0) * ((iARGB Shr 8) And &hFF)
  2707.                 fCounterB = Filter(0) * (iARGB And &hFF)
  2708.                 fDominator = Filter(0)
  2709.                 For b = 1 To c - 1
  2710.                     fDominator += 2 * Filter(b)
  2711.                     y1 = iY - b
  2712.                     y2 = iY + b
  2713.                     If y1 < 0 Then
  2714.                         y1 = Abs(y1)
  2715.                     ElseIf y2 >= iH Then
  2716.                         y2 = y2 + b - iH + 1
  2717.                     Else
  2718.                         iARGB1 = Cast(ULong Ptr, tBitmapData.Scan0)[y1 * iW + iX]
  2719.                         iARGB2 = Cast(ULong Ptr, tBitmapData.Scan0)[y2 * iW + iX]
  2720.                         fCounterR += Filter(b) * (((iARGB1 Shr 16) And &hFF) + ((iARGB2 Shr 16) And &hFF))
  2721.                         fCounterG += Filter(b) * (((iARGB1 Shr 8) And &hFF) + ((iARGB2 Shr 8) And &hFF))
  2722.                         fCounterB += Filter(b) * ((iARGB1 And &hFF) + (iARGB2 And &hFF))
  2723.                     EndIf
  2724.                 Next
  2725.                
  2726.                 newR = Int(fCounterR / fDominator)
  2727.                 newG = Int(fCounterG / fDominator)
  2728.                 newB = Int(fCounterB / fDominator)
  2729.                
  2730.                 newR = IIf(newR < 0, 0, IIf(newR > 255, 255, newR))
  2731.                 newG = IIf(newG < 0, 0, IIf(newG > 255, 255, newG))
  2732.                 newB = IIf(newB < 0, 0, IIf(newB > 255, 255, newB))
  2733.                
  2734.                 Cast(ULong Ptr, tBitmapData_RadialBlur.Scan0)[iOffset + iX] = &hFF000000 + (newR Shl 16) + (newG Shl 8) + newB
  2735.             End If
  2736.         Next
  2737.     Next
  2738.    
  2739.    
  2740.     GdipBitmapUnlockBits(hBitmap_RadialBlur, @tBitmapData_RadialBlur)
  2741.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2742.     If bGDI Then
  2743.         GdipCreateHBITMAPFromBitmap(hBitmap_RadialBlur, @hGDIBitmap, &hFF000000)
  2744.         GdipDisposeImage(hBitmap_RadialBlur)
  2745.         Return hGDIBitmap
  2746.     EndIf
  2747.     Return hBitmap_RadialBlur
  2748. End Function
  2749.  
  2750. Function _GDIPlus_BitmapApplyFilter_TimeWarp(ByVal hImage As Any Ptr, fFactor As Single, fMidX As Single, fMidY As Single, bGDI As BOOL) As Any Ptr Export 'based on original code on https://www.programmingalgorithms.com/algorithm/time-warp '...'
  2751.     Dim As Single iW, iH
  2752.     Dim As Integer iStatus
  2753.     Dim As Any Ptr hBitmap_TimeWarp, hGDIBitmap
  2754.     Dim As BitmapData tBitmapData, tBitmapData_TimeWarp
  2755.    
  2756.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2757.     If iStatus <> 0 Then Return 0
  2758.    
  2759.    
  2760.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2761.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_TimeWarp)
  2762.     GdipBitmapLockBits(hBitmap_TimeWarp, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_TimeWarp)
  2763.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2764.    
  2765.     Dim As Integer iX, iY, iTrueX, iTrueY, iNewX, iNewY, iOffset
  2766.     Dim As Double fNewRadius, fTheta, fRadius
  2767.    
  2768.     For iY = 0 To iH - 1
  2769.         iOffset = iY * iW
  2770.         iTrueY = iY - fMidY
  2771.         For iX = 0 To iW - 1
  2772.             iTrueX = iX - fMidX
  2773.             fTheta = Atan2(iTrueY, iTrueX)
  2774.             fRadius = Sqr(iTrueX * iTrueX + iTrueY * iTrueY)
  2775.             fNewRadius = Sqr(fRadius) * fFactor
  2776.             iNewX = CLng(fMidX + (fNewRadius * Cos(fTheta)))
  2777.             iNewY = CLng(fMidY + (fNewRadius * Sin(fTheta)))
  2778.             If (iNewY >= 0 And iNewY < iH) And (iNewX >= 0 And iNewX < iW) Then
  2779.                 Cast(ULong Ptr, tBitmapData_TimeWarp.Scan0)[iOffset + iX] = Cast(ULong Ptr, tBitmapData.Scan0)[iNewY * iW + iNewX]
  2780.             EndIf
  2781.         Next
  2782.     Next
  2783.     GdipBitmapUnlockBits(hBitmap_TimeWarp, @tBitmapData_TimeWarp)
  2784.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2785.    
  2786.     If bGDI Then
  2787.         GdipCreateHBITMAPFromBitmap(hBitmap_TimeWarp, @hGDIBitmap, &hFF000000)
  2788.         GdipDisposeImage(hBitmap_TimeWarp)
  2789.         Return hGDIBitmap
  2790.     EndIf
  2791.     Return hBitmap_TimeWarp
  2792. End Function
  2793.  
  2794. Function _GDIPlus_BitmapApplyFilter_FishEye(ByVal hImage As Any Ptr, bGDI As BOOL) As Any Ptr Export 'based on original code by Christian Graus on http://www.codeproject.com/Articles/3419/Image-Processing-for-Dummies-with-C-and-GDI-Part '...'
  2795.     Dim As Single iW, iH
  2796.     Dim As Integer iStatus
  2797.     Dim As Any Ptr hBitmap_FishEye, hGDIBitmap
  2798.     Dim As BitmapData tBitmapData, tBitmapData_FishEye
  2799.    
  2800.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2801.     If iStatus <> 0 Then Return 0
  2802.    
  2803.    
  2804.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2805.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_FishEye)
  2806.     GdipBitmapLockBits(hBitmap_FishEye, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_FishEye)
  2807.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2808.    
  2809.     Dim As Integer i, iX, iY, iTrueX, iTrueY, iNewX, iNewY, iOffset
  2810.     Dim As Double fTheta, fRadius, fMidX, fMidY, fNewRadius
  2811.     fMidX = iW / 2
  2812.     fMidY = iH / 2
  2813.     Dim As Single fMaxXY
  2814.     fMaxXY = max(fMidX, fMidY)
  2815.    
  2816.     For iY = 0 To iH - 1
  2817.         iOffset = iY * iW
  2818.         iTrueY = iY - fMidY 'translate to center y
  2819.         For iX = 0 To iW - 1
  2820.             iTrueX = iX - fMidX 'translate to center x
  2821.             fTheta = Atan2(iTrueY, iTrueX)
  2822.             fRadius = Sqr(iTrueX * iTrueX + iTrueY * iTrueY)
  2823.             fNewRadius = fRadius * fRadius / fMaxXY
  2824.             iNewX = fMidX + (fNewRadius * Cos(fTheta))
  2825.             iNewY = fMidY + (fNewRadius * Sin(fTheta))
  2826.             If Not (iNewY >= 0 And iNewY < iH) And (iNewX >= 0 And iNewX < iW) Then
  2827.                 iNewX = 0
  2828.                 iNewY = 0
  2829.             EndIf
  2830.             If (iNewY >= 0 And iNewY < iH) And (iNewX >= 0 And iNewX < iW) Then
  2831.                 Cast(ULong Ptr, tBitmapData_FishEye.Scan0)[iOffset + iX] = Cast(ULong Ptr, tBitmapData.Scan0)[iNewY * iW + iNewX]
  2832.             EndIf
  2833.         Next
  2834.     Next
  2835.     GdipBitmapUnlockBits(hBitmap_FishEye, @tBitmapData_FishEye)
  2836.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2837.    
  2838.     If bGDI Then
  2839.         GdipCreateHBITMAPFromBitmap(hBitmap_FishEye, @hGDIBitmap, &hFF000000)
  2840.         GdipDisposeImage(hBitmap_FishEye)
  2841.         Return hGDIBitmap
  2842.     EndIf
  2843.     Return hBitmap_FishEye
  2844. End Function
  2845.  
  2846. Function _GDIPlus_BitmapApplyFilter_Wave(ByVal hImage As Any Ptr, fAmplitudeX As Single, fAmplitudeY As Single, fFrequencyX As Single, fFrequencyY As Single, bGDI As BOOL) As Any Ptr Export 'based on original code by Christian Graus on http://www.codeproject.com/Articles/3419/Image-Processing-for-Dummies-with-C-and-GDI-Part '...'
  2847.     Dim As Single iW, iH
  2848.     Dim As Integer iStatus
  2849.     Dim As Any Ptr hBitmap_Wave, hGDIBitmap
  2850.     Dim As BitmapData tBitmapData, tBitmapData_Wave
  2851.    
  2852.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2853.     If iStatus <> 0 Then Return 0
  2854.    
  2855.    
  2856.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2857.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Wave)
  2858.     GdipBitmapLockBits(hBitmap_Wave, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Wave)
  2859.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2860.    
  2861.     Dim As Integer i, iX, iY, iTrueX, iTrueY, iNewX, iNewY, iOffset
  2862.    
  2863.     For iY = 0 To iH - 1
  2864.         iOffset = iY * iW
  2865.         iTrueX = fAmplitudeX * Sin(f2Pi * iY / fFrequencyX)
  2866.         For iX = 0 To iW - 1
  2867.             iTrueY = fAmplitudeY * Cos(f2Pi * iX / fFrequencyY)
  2868.             iNewX = iX + iTrueX
  2869.             iNewY = iY + iTrueY
  2870.             If (iNewY >= 0 And iNewY < iH) And (iNewX >= 0 And iNewX < iW) Then
  2871.                 Cast(ULong Ptr, tBitmapData_Wave.Scan0)[iOffset + iX] = Cast(ULong Ptr, tBitmapData.Scan0)[iNewY * iW + iNewX]
  2872.             EndIf
  2873.         Next
  2874.     Next
  2875.     GdipBitmapUnlockBits(hBitmap_Wave, @tBitmapData_Wave)
  2876.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2877.    
  2878.     If bGDI Then
  2879.         GdipCreateHBITMAPFromBitmap(hBitmap_Wave, @hGDIBitmap, &hFF000000)
  2880.         GdipDisposeImage(hBitmap_Wave)
  2881.         Return hGDIBitmap
  2882.     EndIf
  2883.     Return hBitmap_Wave
  2884. End Function
  2885.  
  2886. Function _GDIPlus_BitmapApplyFilter_Swirl(ByVal hImage As Any Ptr, fDegree As Single, bGDI As BOOL) As Any Ptr Export 'based on original code by Christian Graus on http://www.codeproject.com/Articles/3419/Image-Processing-for-Dummies-with-C-and-GDI-Part '...'
  2887.     Dim As Single iW, iH
  2888.     Dim As Integer iStatus
  2889.     Dim As Any Ptr hBitmap_Swirl, hGDIBitmap
  2890.     Dim As BitmapData tBitmapData, tBitmapData_Swirl
  2891.    
  2892.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2893.     If iStatus <> 0 Then Return 0
  2894.    
  2895.    
  2896.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2897.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Swirl)
  2898.     GdipBitmapLockBits(hBitmap_Swirl, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Swirl)
  2899.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2900.    
  2901.     Dim As Integer i, iX, iY, iTrueX, iTrueY, iNewX, iNewY, iOffset
  2902.     Dim As Double fTheta, fRadius, fMidX, fMidY, f
  2903.     fMidX = iW / 2
  2904.     fMidY = iH / 2
  2905.    
  2906.     For iY = 0 To iH - 1
  2907.         iOffset = iY * iW
  2908.         iTrueY = iY - fMidY 'translate to center y
  2909.         For iX = 0 To iW - 1
  2910.             iTrueX = iX - fMidX 'translate to center x
  2911.             fTheta = Atan2(iTrueY, iTrueX)
  2912.             fRadius = Sqr(iTrueX * iTrueX + iTrueY * iTrueY)
  2913.             f = fTheta + fDegree * fRad * fRadius
  2914.             iNewX = fMidX + (fRadius * Cos(f))
  2915.             iNewY = fMidY + (fRadius * Sin(f))
  2916.             If (iNewY >= 0 And iNewY < iH) And (iNewX >= 0 And iNewX < iW) Then
  2917.                 Cast(ULong Ptr, tBitmapData_Swirl.Scan0)[iOffset + iX] = Cast(ULong Ptr, tBitmapData.Scan0)[iNewY * iW + iNewX]
  2918.             EndIf
  2919.         Next
  2920.     Next
  2921.     GdipBitmapUnlockBits(hBitmap_Swirl, @tBitmapData_Swirl)
  2922.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2923.    
  2924.     If bGDI Then
  2925.         GdipCreateHBITMAPFromBitmap(hBitmap_Swirl, @hGDIBitmap, &hFF000000)
  2926.         GdipDisposeImage(hBitmap_Swirl)
  2927.         Return hGDIBitmap
  2928.     EndIf
  2929.    
  2930.     Return hBitmap_Swirl
  2931. End Function
  2932.  
  2933. Function _GDIPlus_BitmapApplyFilter_XRay(ByVal hImage As Any Ptr, iBias As Byte, bInvert As BOOL, bGDI As BOOL) As Any Ptr Export 'based on original code by Dewald Esterhuizen (DifferenceOfGaussians) '...'
  2934.     Dim As Any Ptr hBitmap_XRay, hBitmap_Gaussian3x3, hBitmap_Gaussian5x5, hBitmap_Greyscale, hGDIBitmap
  2935.    
  2936.     hBitmap_Greyscale = _GDIPlus_BitmapCreateGreyscale(hImage)
  2937.     hBitmap_Gaussian3x3 = _GDIPlus_BitmapApplyFilter_Convolution(hBitmap_Greyscale, 1, 0, 31, 0, 0, 0)
  2938.     hBitmap_Gaussian5x5 = _GDIPlus_BitmapApplyFilter_Convolution(hBitmap_Greyscale, 1, 0, 28, 0, 0, 0)
  2939.     hBitmap_XRay = _GDIPlus_BitmapCreateSubtract(hBitmap_Gaussian3x3, hBitmap_Gaussian5x5, iBias, bInvert)
  2940.    
  2941.     GdipDisposeImage(hBitmap_Greyscale)
  2942.     GdipDisposeImage(hBitmap_Gaussian3x3)
  2943.     GdipDisposeImage(hBitmap_Gaussian5x5)
  2944.    
  2945.     If bGDI Then
  2946.         GdipCreateHBITMAPFromBitmap(hBitmap_XRay, @hGDIBitmap, &hFF000000)
  2947.         GdipDisposeImage(hBitmap_XRay)
  2948.         Return hGDIBitmap
  2949.     EndIf
  2950.     Return hBitmap_XRay
  2951. End Function
  2952.  
  2953. Function _GDIPlus_BitmapApplyFilter_DistortionBlur(ByVal hImage As Any Ptr, iDistortFactor As UShort, bGDI As BOOL) As Any Ptr Export 'based on original code by Dewald Esterhuizen '...'
  2954.     Dim As Single iW, iH
  2955.     Dim As Any Ptr hBitmap_Distortion, hBitmap_Blur, hGDIBitmap
  2956.     Dim As Integer iStatus
  2957.     Dim As BitmapData tBitmapData, tBitmapData_Distortion
  2958.    
  2959.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  2960.     If iStatus <> 0 Then Return 0
  2961.    
  2962.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  2963.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Distortion)
  2964.     GdipBitmapLockBits(hBitmap_Distortion, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Distortion)
  2965.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  2966.    
  2967.     GdipBitmapUnlockBits(hBitmap_Distortion, @tBitmapData_Distortion)
  2968.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2969.    
  2970.     Dim As Integer iX, iY, iOffset, filterX, filterY, factorMax
  2971.    
  2972.     iDistortFactor = IIf(iDistortFactor < 2, 2, iDistortFactor)
  2973.    
  2974.     factorMax = (iDistortFactor + 1) * 2
  2975.    
  2976.     For iY = 0 To iH - 1
  2977.         iOffset = iY * iW
  2978.         For iX = 0 To iW - 1
  2979.             filterX = Int(iX + iDistortFactor - Rnd * factorMax) Mod iW
  2980.             filterY = Int(iY + iDistortFactor - Rnd * factorMax) Mod iH
  2981.         If filterX < 0 Then filterX = 0
  2982.         If filterY < 0 Then filterY = 0
  2983.         Cast(ULong Ptr, tBitmapData_Distortion.Scan0)[iOffset + iX] = Cast(ULong Ptr, tBitmapData.Scan0)[filterY * iW + filterX]
  2984.         Next
  2985.     Next
  2986.    
  2987.     GdipBitmapUnlockBits(hBitmap_Distortion, @tBitmapData_Distortion)
  2988.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  2989.    
  2990.     hBitmap_Blur = _GDIPlus_BitmapApplyFilter_Convolution(hBitmap_Distortion, 1, 0, 17, 0, 0, 0)
  2991.    
  2992.     GdipDisposeImage(hBitmap_Distortion)
  2993.    
  2994.     If bGDI Then
  2995.         GdipCreateHBITMAPFromBitmap(hBitmap_Blur, @hGDIBitmap, &hFF000000)
  2996.         GdipDisposeImage(hBitmap_Blur)
  2997.         Return hGDIBitmap
  2998.     EndIf
  2999.    
  3000.     Return hBitmap_Blur
  3001. End Function
  3002.  
  3003. Function _GDIPlus_BitmapApplyFilter_GridBlur(ByVal hImage As Any Ptr, bGDI As BOOL) As Any Ptr Export '...'
  3004.     Dim As Single iW, iH
  3005.     Dim As Any Ptr hBitmap_GridBlur, hGDIBitmap
  3006.     Dim As BitmapData tBitmapData, tBitmapData_GridBlur
  3007.     Dim As Long iX, iY, iRowOffset, r, g, b, iColor
  3008.     Dim As Integer iStatus
  3009.  
  3010.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3011.     If iStatus <> 0 Then Return 0
  3012.    
  3013.     Dim As RECT tRect_GridBlur = Type(0, 0, iW - 1, iH - 1), tRect = Type(0, 0, iW - 1, iH - 1)
  3014.    
  3015.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_GridBlur)
  3016.     GdipBitmapLockBits(hBitmap_GridBlur, Cast(Any Ptr, @tRect_GridBlur), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_GridBlur)
  3017.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  3018.    
  3019.     Dim As UInteger rax(0 To iW), gax(0 To iW), bax(0 To iW), ray(0 To iH), gay(0 To iH), bay(0 To iH), iPixels = iW + iH
  3020.    
  3021.     For iX = 0 To iW - 1
  3022.         r = 0
  3023.         g = 0
  3024.         b = 0
  3025.         For iY = 0 To iH - 1
  3026.           iColor = Cast(ULong Ptr, tBitmapData.Scan0)[iY * iW + iX]
  3027.             b += (iColor Shr 16) And &hFF
  3028.             g += (iColor Shr 8) And &hFF
  3029.             r += iColor And &hFF
  3030.         Next
  3031.         rax(iX) = r
  3032.         gax(iX) = g
  3033.         bax(iX) = b
  3034.     Next
  3035.        
  3036.     For iY = 0 To iH - 1
  3037.         iRowOffset = iY * iW
  3038.         r = 0
  3039.         g = 0
  3040.         b = 0
  3041.         For iX = 0 To iW - 1
  3042.           iColor = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  3043.             r += (iColor Shr 16) And &hFF
  3044.             g += (iColor Shr 8) And &hFF
  3045.             b += iColor And &hFF
  3046.         Next
  3047.         ray(iY) = r
  3048.         gay(iY) = g
  3049.         bay(iY) = b
  3050.     Next
  3051.    
  3052.     For iY = 0 To iH - 1
  3053.         iRowOffset = iY * iW
  3054.         For iX = 0 To iW - 1
  3055.             r = Int((rax(iX) + ray(iY)) / iPixels)
  3056.             If r > 255 Then r = 255
  3057.             g = Int((gax(iX) + gay(iY)) / iPixels)
  3058.             If g > 255 Then g = 255
  3059.             b = Int((bax(iX) + bay(iY)) / iPixels)
  3060.             If b > 255 Then b = 255
  3061.             Cast(ULong Ptr, tBitmapData_GridBlur.Scan0)[iRowOffset + iX] = &hFF000000 + r Shl 16 + g Shl 8 + b
  3062.         Next
  3063.     Next
  3064.            
  3065.     GdipBitmapUnlockBits(hBitmap_GridBlur, @tBitmapData_GridBlur)
  3066.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  3067.     If bGDI Then
  3068.         GdipCreateHBITMAPFromBitmap(hBitmap_GridBlur, @hGDIBitmap, &hFF000000)
  3069.         GdipDisposeImage(hBitmap_GridBlur)
  3070.         Return hGDIBitmap
  3071.     EndIf
  3072.     Return hBitmap_GridBlur    
  3073. End Function
  3074.  
  3075. Function _GDIPlus_BitmapApplyFilter_BWJJNDithering(ByVal hImage As Any Ptr, fErrorMultiplier As Single, iThreshold As UByte, bGDI As BOOL) As Any Ptr Export '...'
  3076.     Dim As Single iW, iH
  3077.     Dim As Integer iStatus
  3078.    
  3079.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3080.     If iStatus <> 0 Then Return 0
  3081.    
  3082.     Dim As Any Ptr hBitmap_Dithered, hGDIBitmap
  3083.     Dim As BitmapData tBitmapData, tBitmapData_Dithered
  3084.     Dim As Long iX, iY, xx, yy, iRowOffset, r, g, b, e, fAvg, iARGB
  3085.     Dim As Single aError(0 To iH, 0 To iW)
  3086.     Dim As RECT tRect_Dithered = Type(0, 0, iW - 1, iH - 1), tRect = Type(0, 0, iW - 1, iH - 1)
  3087.    
  3088.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dithered)
  3089.     GdipBitmapLockBits(hBitmap_Dithered, Cast(Any Ptr, @tRect_Dithered), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Dithered)
  3090.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  3091.    
  3092.    
  3093.     Dim As Byte iUB, iLB1, iLB2
  3094.     iUB = UBound(matrixJJN)
  3095.     Select Case UBound(matrixJJN, 2) Mod 2
  3096.         Case 0
  3097.             iLB1 = -UBound(matrixJJN, 2) \ 2
  3098.             iLB2 = UBound(matrixJJN, 2) \ 2
  3099.         Case 1
  3100.             iLB1 = CByte(-UBound(matrixJJN, 2) / 2)
  3101.             iLB2 = CByte(UBound(matrixJJN, 2) / 2) - 1
  3102.     End Select
  3103.    
  3104.     For iY = 0 To iH - 1
  3105.         iRowOffset = iY * iW
  3106.         For iX = 0 To iW - 1
  3107.           iARGB = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  3108.             r = (iARGB Shr 16) And &hFF
  3109.             g = (iARGB Shr 8) And &hFF
  3110.             b = iARGB And &hFF
  3111.             'fAvg = (r + g + b) / 3
  3112.             fAvg = (r * 213 + g * 715 + b * 72) / 1000
  3113.             fAvg -= aError(iY, iX) * fErrorMultiplier
  3114.             e = 0
  3115.             If fAvg < iThreshold Then
  3116.                 e = -fAvg
  3117.                 fAvg = 0
  3118.             Else
  3119.                 e = 255 - fAvg
  3120.                 fAvg = 255
  3121.             EndIf
  3122.             For yy = 0 To iUB
  3123.                 For xx = iLB1 To iLB2
  3124.                     If (iY + yy < 0) Or (iH <= iY + yy) Or (iX + xx < 0) Or (iW <= iX + xx) Then Continue For
  3125.                     aError(iY + yy, iX + xx) += e * matrixJJN(yy, xx + 2)
  3126.                 Next
  3127.             Next
  3128.             Cast(ULong Ptr, tBitmapData_Dithered.Scan0)[iRowOffset + iX] = &hFF000000 + (fAvg Shl 16) + (fAvg Shl 8) + (fAvg Shl 0)
  3129.         Next
  3130.     Next
  3131.     GdipBitmapUnlockBits(hBitmap_Dithered, @tBitmapData_Dithered)
  3132.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  3133.     If bGDI Then
  3134.         GdipCreateHBITMAPFromBitmap(hBitmap_Dithered, @hGDIBitmap, &hFF000000)
  3135.         GdipDisposeImage(hBitmap_Dithered)
  3136.         Return hGDIBitmap
  3137.     EndIf
  3138.     Return hBitmap_Dithered
  3139. End Function
  3140.  
  3141. Function _GDIPlus_BitmapApplyFilter_BWBayerDithering(ByVal hImage As Any Ptr, bGDI As BOOL) As Any Ptr Export '...'
  3142.     Dim As Single iW, iH
  3143.     Dim As Integer iStatus
  3144.    
  3145.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3146.     If iStatus <> 0 Then Return 0
  3147.    
  3148.     Dim As Any Ptr hBitmap_Dithered, hGDIBitmap
  3149.     Dim As BitmapData tBitmapData, tBitmapData_Dithered
  3150.     Dim As ULong iX, iY, iRowOffset, iColor, iR, iG, iB, iValue
  3151.     Dim As RECT tRect_Dithered = Type(0, 0, iW - 1, iH - 1), tRect = Type(0, 0, iW - 1, iH - 1)
  3152.    
  3153.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Dithered)
  3154.     GdipBitmapLockBits(hBitmap_Dithered, Cast(Any Ptr, @tRect_Dithered), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Dithered)
  3155.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  3156.    
  3157.     For iY = 0 To iH - 1
  3158.         iRowOffset = iY * iW
  3159.         For iX = 0 To iW - 1
  3160.             iColor = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  3161.             iR = (iColor Shr 16) And &hFF
  3162.             iG = (iColor Shr 8) And &hFF
  3163.             iB =  iColor And &hFF
  3164.             iValue = ((iR * 213 + iG * 715 + iB * 72) / 1000) Shr 2
  3165.             Cast(ULong Ptr, tBitmapData_Dithered.Scan0)[iRowOffset + iX] = IIf(iValue > matrixBayer(iX And 7, iY And 7), &hFFFFFFFF, &hFF000000)
  3166.         Next
  3167.     Next
  3168.     GdipBitmapUnlockBits(hBitmap_Dithered, @tBitmapData_Dithered)
  3169.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  3170.     If bGDI Then
  3171.         GdipCreateHBITMAPFromBitmap(hBitmap_Dithered, @hGDIBitmap, &hFF000000)
  3172.         GdipDisposeImage(hBitmap_Dithered)
  3173.         Return hGDIBitmap
  3174.     EndIf
  3175.     Return hBitmap_Dithered
  3176. End Function
  3177.  
  3178. Function _GDIPlus_BitmapApplyFilter_Indexed(ByVal hImage As Any Ptr, iColors As ULong, bDither As BOOL, iDitherType As UByte, bGDI As BOOL) As Any Ptr Export '...'
  3179.     Dim As Single iW, iH
  3180.     Dim As Integer iStatus
  3181.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3182.     If iStatus <> 0 Then Return 0
  3183.     If iColors < 2 Then iColors = 2
  3184.     If iColors > 2 And iColors < 16 Then iColors = 16
  3185.     If iColors > 16 Then iColors = 256
  3186.    
  3187.     Dim As Integer iFormat = PixelFormat8bppIndexed
  3188.     Select Case iColors
  3189.         Case 2 '1 bit
  3190.             iFormat = PixelFormat1bppIndexed
  3191.         Case 16 '4 bit
  3192.             iFormat = PixelFormat4bppIndexed
  3193.         Case Else '8 bit
  3194.             iFormat = PixelFormat8bppIndexed
  3195.     End Select
  3196.    
  3197.     Dim As Any Ptr hBitmap_temp, hBitmap_temp2, hBitmap_Indexed, hGDIBitmap
  3198.     Dim As ULong iBytes
  3199.     Dim tPalette As tagPalette
  3200.        
  3201.     GdipCloneBitmapArea(0, 0, iW, iH, iFormat, hImage, @hBitmap_temp2)
  3202.     GdipGetImagePaletteSize(hBitmap_temp2, Cast(Any Ptr, @iBytes))
  3203.     GdipGetImagePalette(hBitmap_temp2, Cast(Any Ptr, @tPalette), iBytes)
  3204.    
  3205.     GdipCloneBitmapArea(0, 0, iW, iH, PixelFormat32bppARGB, hImage, @hBitmap_temp)
  3206.     Dim As ULong iX, iY, iRowOffset, currentPixel, NearestColor, c, iR, iG, iB
  3207.     Dim As Long errorR, errorG, errorB
  3208.     Dim As BitmapData tBitmapData, tBitmapData_Index
  3209.     Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  3210.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Indexed)
  3211.     GdipBitmapLockBits(hBitmap_Indexed, Cast(Any Ptr, @tRect), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Index)
  3212.     GdipBitmapLockBits(hBitmap_temp, Cast(Any Ptr, @tRect), ImageLockModeRead Or ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData)
  3213.    
  3214.     iDitherType = IIf(iDitherType < 1, 1, IIf(iDitherType > 8, 8, iDitherType))
  3215.    
  3216.     Dim As ULong offsetX, offsetY, offsetIndex
  3217.     Dim As UByte iRow, iCol
  3218.     Dim As Single coefficient, matrix(Any, Any)
  3219.  
  3220.     Dim As UByte matrixHeight, matrixWidth, matrixStartX
  3221.  
  3222.                                                        
  3223.     Select Case iDitherType '...'
  3224.         Case 1  'Floyd-Steinberg
  3225.             matrixHeight = 2
  3226.             matrixWidth = 3
  3227.             matrixStartX = 1
  3228.             ReDim As Single matrix(0 To matrixHeight, 0 To matrixWidth)
  3229.             CopyArray(matrixFS(), matrix())
  3230.         Case 2  'Burkes
  3231.             matrixHeight = 2
  3232.             matrixWidth = 5
  3233.             matrixStartX = 2
  3234.             ReDim As Single matrix(0 To matrixHeight, 0 To matrixWidth)
  3235.             CopyArray(matrixBurkes(), matrix())
  3236.         Case 3  'Jarvis, Judice, and Ninke
  3237.             matrixHeight = 3
  3238.             matrixWidth = 5
  3239.             matrixStartX = 2
  3240.             ReDim As Single matrix(0 To matrixHeight, 0 To matrixWidth)
  3241.             CopyArray(matrixJJN(), matrix())
  3242.         Case 4  'Stucki
  3243.             matrixHeight = 3
  3244.             matrixWidth = 5
  3245.             matrixStartX = 2
  3246.             ReDim As Single matrix(0 To matrixHeight, 0 To matrixWidth)
  3247.             CopyArray(matrixStucki(), matrix())
  3248.         Case 5  'Two-Row Sierra
  3249.             matrixHeight = 2
  3250.             matrixWidth = 5
  3251.             matrixStartX = 2
  3252.             ReDim As Single matrix(0 To matrixHeight, 0 To matrixWidth)
  3253.             CopyArray(matrixSierra2(), matrix())
  3254.         Case 6  'Three-Row Sierra
  3255.             matrixHeight = 3
  3256.             matrixWidth = 5
  3257.             matrixStartX = 2
  3258.             ReDim As Single matrix(0 To matrixHeight, 0 To matrixWidth)
  3259.             CopyArray(matrixSierra3(), matrix())
  3260.         Case 7  'Atkinson
  3261.             matrixHeight = 3
  3262.             matrixWidth = 4
  3263.             matrixStartX = 1
  3264.             ReDim As Single matrix(0 To matrixHeight, 0 To matrixWidth)
  3265.             CopyArray(matrixAtkinson(), matrix())
  3266.     Case 8 'ErrorDiffusion
  3267.             matrixHeight = 3
  3268.             matrixWidth = 5
  3269.             matrixStartX = 2
  3270.             ReDim As Single matrix(0 To matrixHeight, 0 To matrixWidth)
  3271.             CopyArray(matrixErrorDiffusion(), matrix())    
  3272.     End Select
  3273.    
  3274.     If bDither Then
  3275.         For iY = 0 To iH - 1
  3276.             iRowOffset = iY * iW
  3277.             For iX = 0 To iW - 1
  3278.                 currentPixel = Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX]
  3279.                 iR = (currentPixel Shr 16) And &hFF
  3280.                 iG = (currentPixel Shr 8) And &hFF
  3281.                 iB =  currentPixel And &hFF
  3282.                 NearestColor = IIf(iColors = 2, IIf((iR * 213 + iG * 715 + iB * 72) / 1000 > 127.5, &hFFFFFFFF, &hFF000000), FindNearestColor(currentPixel, @tPalette, iColors))
  3283.                 Cast(ULong Ptr, tBitmapData_Index.Scan0)[iRowOffset + iX] = NearestColor
  3284.                 errorR = iR - ((NearestColor Shr 16) And &hFF)
  3285.                 errorG = iG - ((NearestColor Shr 8) And &hFF)
  3286.                 errorB = iB -  (NearestColor And &hFF)
  3287.                 For iRow = 0 To matrixHeight - 1
  3288.                     offsetY = iY + iRow
  3289.                     For iCol = 0 To matrixWidth - 1
  3290.                         coefficient = matrix(iRow, iCol)
  3291.                         offsetX = iX + (iCol - matrixStartX)
  3292.                         If (coefficient <> 0 AndAlso offsetX >= 0 AndAlso offsetX < iW AndAlso offsetY >= 0 AndAlso offsetY < iH) Then
  3293.                             offsetIndex = offsetY * iW + offsetX
  3294.                             c = Cast(ULong Ptr, tBitmapData.Scan0)[offsetIndex]
  3295.                             Cast(ULong Ptr, tBitmapData.Scan0)[offsetIndex] = _
  3296.                                                                                 PlusTruncate((c Shr 16) And &hFF, errorR * coefficient) Shl 16 Or _
  3297.                                                                                 PlusTruncate((c Shr 8) And &hFF,  errorG * coefficient) Shl 8 Or _
  3298.                                                                                 PlusTruncate( c And &hFF,        (errorB * coefficient))
  3299.                         EndIf
  3300.                     Next
  3301.                 Next
  3302.             Next
  3303.         Next
  3304.        
  3305.     EndIf
  3306.     GdipBitmapUnlockBits(hBitmap_Indexed, @tBitmapData_Index)
  3307.     GdipBitmapUnlockBits(hBitmap_temp, @tBitmapData)
  3308.     GdipDisposeImage(hBitmap_temp)
  3309.     If bDither Then
  3310.         If bGDI Then
  3311.             GdipCreateHBITMAPFromBitmap(hBitmap_Indexed, @hGDIBitmap, &hFF000000)
  3312.             GdipDisposeImage(hBitmap_Indexed)
  3313.             Return hGDIBitmap
  3314.         EndIf
  3315.         Return hBitmap_Indexed
  3316.     Else
  3317.         If bGDI Then
  3318.             GdipCreateHBITMAPFromBitmap(hBitmap_temp2, @hGDIBitmap, &hFF000000)
  3319.             GdipDisposeImage(hBitmap_temp2)
  3320.             Return hGDIBitmap
  3321.         EndIf
  3322.         Return hBitmap_temp2
  3323.     EndIf
  3324. End Function
  3325.  
  3326. Function FindNearestColor(iColor As ULong, ByRef tColorPalette As tagPalette Ptr, iColors As ULong) As ULong
  3327.     Dim As ULong distanceSquared, c, minDistanceSquared = 195076 '255 * 255 + 255 * 255 + 255 * 255 + 1
  3328.     Dim As UByte bestIndex = 0, Rdiff, Gdiff, Bdiff
  3329.     Dim As ULong i
  3330.     For i = 0 To iColors - 1
  3331.         c = tColorPalette -> ARGB(i)
  3332.         Rdiff = ((iColor Shr 16) And &hFF) - ((c Shr 16) And &hFF)
  3333.         Gdiff = ((iColor Shr 8) And &hFF) - ((c Shr 8) And &hFF)
  3334.         Bdiff = (iColor And &hFF) - (c And &hFF)
  3335.         distanceSquared = Rdiff * Rdiff + Gdiff * Gdiff + Bdiff * Bdiff
  3336.         If distanceSquared < minDistanceSquared Then
  3337.             minDistanceSquared = distanceSquared
  3338.             bestIndex = i
  3339.         EndIf
  3340.     Next
  3341.     'Return IIf(tColorPalette->ARGB(bestIndex) = &hFFFFFFFF, &hFF000000, tColorPalette->ARGB(bestIndex))
  3342.     Return tColorPalette->ARGB(bestIndex)
  3343. End Function
  3344.  
  3345. Function PlusTruncate(a As UByte, b As Single) As UByte
  3346.     Return IIf(a + b < 0, 0, IIf(a + b > 255, 255, CUByte(a + b)))
  3347. End Function
  3348.  
  3349. Sub CopyArray(a() As Single, b() As Single) '...'
  3350.     Dim As UInteger x, y
  3351.     For y = 0 To UBound(a)
  3352.         For x = 0 To UBound(a, 2)
  3353.             b(y, x) = a(y, x)
  3354.         Next
  3355.     Next
  3356. End Sub
  3357.  
  3358. Function _GDIPlus_BitmapApplyFilter_Mosaic(ByVal hImage As Any Ptr, iSites As ULong, bOrdered As BOOL, bBorder As BOOL, iCalcMode As UByte, iBorderColor As ULong, bGDI As BOOL) As Any Ptr Export '...'
  3359.     Dim As Single iW, iH
  3360.     Dim As Integer iStatus
  3361.     Dim As Any Ptr hBitmap_Mosaic, hGDIBitmap
  3362.     Dim As BitmapData tBitmapData_Mosaic
  3363.    
  3364.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3365.     If iStatus <> 0 Then Return 0  
  3366.    
  3367.     iSites = IIf(iSites < 0, 0, IIf(iSites > (iW * iH) / 2, CULng((iW * iH) / 2), iSites))
  3368.     iSites = IIf(iSites = 0, Sqr(iW * iW + iH * iH) Shl 2, iSites)
  3369.    
  3370.     Dim As Single aPoints(0 To iSites, 0 To 2), d, fDist, fX, fY, fDiv, iStepX, iStepY
  3371.     Dim As Long iARGB, i, iX, iY, iRowOffset, iBorderX = CULng(iW * 0.015), iBorderY = CULng(iH * 0.015)
  3372.     Dim As RECT tRect_Mosaic = Type(0, 0, iW - 1, iH - 1)
  3373.    
  3374.     Randomize()
  3375.     If bOrdered Then
  3376.         fDiv = Sqr(iSites)
  3377.         iStepX = (iW / fDiv) + 1
  3378.         iStepY = (iH / fDiv) + 1
  3379.         i = 0
  3380.         For iY = 0 To CULng(fDiv + 1)
  3381.             For iX = 0 To CULng(fDiv + 1)
  3382.                 aPoints(i, 0) = ((0 + iX) * iStepX) + Rnd * 4 - 2
  3383.                 If aPoints(i, 0) < 0 Then aPoints(i, 0) = 0
  3384.                 If aPoints(i, 0) > iW Then aPoints(i, 0) = iW -2
  3385.                 aPoints(i, 1) = ((0 + iY) * iStepY) + Rnd * 4 - 2
  3386.                 If aPoints(i, 1) < 0 Then aPoints(i, 1) = 0
  3387.                 If aPoints(i, 1) > iH Then aPoints(i, 1) = iH - 2
  3388.                 GdipBitmapGetPixel(hImage, aPoints(i, 0), aPoints(i, 1), Cast(Any Ptr, @iARGB))
  3389.                 aPoints(i, 2) = iARGB
  3390.                 i += 1
  3391.                 If i > iSites Then Exit For, For
  3392.             Next
  3393.         Next
  3394.     Else
  3395.         For i = 0 To iSites
  3396.             aPoints(i, 0) = iBorderX + Rnd * (iW - 2 * iBorderX)
  3397.             aPoints(i, 1) = iBorderY + Rnd * (iH - 2 * iBorderY)
  3398.             GdipBitmapGetPixel(hImage, aPoints(i, 0), aPoints(i, 1), Cast(Any Ptr, @iARGB))
  3399.             aPoints(i, 2) = iARGB
  3400.         Next
  3401.     EndIf
  3402.    
  3403.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Mosaic)
  3404.     GdipBitmapLockBits(hBitmap_Mosaic, Cast(Any Ptr, @tRect_Mosaic), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Mosaic)
  3405.    
  3406.     iCalcMode = IIf(iCalcMode < 0, 0, IIf(iCalcMode > 2, 2, iCalcMode))
  3407.    
  3408.     For iY = 0 To iH - 1
  3409.         iRowOffset = iY * iW
  3410.         For iX = 0 To iW - 1
  3411.             fDist = 10000.0
  3412.             For i = 0 To iSites
  3413.                 fX = aPoints(i, 0) - iX
  3414.                 fY = aPoints(i, 1) - iY
  3415.                 Select Case iCalcMode
  3416.                     Case 0 'Euclidean distance
  3417.                         d = Sqr(fX * fX + fY * fY)
  3418.                     Case 1 'Manhattan distance
  3419.                         d = Abs(fX) + Abs(fY)
  3420.                     Case 2 'Chebyshev distance
  3421.                         d = max(Abs(fX), Abs(fY))                  
  3422.                 End Select
  3423.                 If d < fDist Then
  3424.                     fDist = d
  3425.                     Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[iRowOffset + iX] = aPoints(i, 2)
  3426.                 EndIf
  3427.             Next
  3428.         Next
  3429.     Next
  3430.  
  3431.     If bBorder Then 'fast border routine withou aa
  3432.         Dim As Single a1, a2
  3433.         Dim As UByte ca
  3434.         Dim As ULong col
  3435.         ca = (iBorderColor And &hFF000000) Shr 24
  3436.         a1 = ca / 255 : a2 = 1 - a1
  3437.         For iY = 0 To iH - 1
  3438.             iRowOffset = iY * iW
  3439.             For iX = 1 To iW - 1
  3440.                 If Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[iRowOffset + iX - 1] <> Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[iRowOffset + iX] Then
  3441.                     col = Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[iRowOffset + iX - 1]
  3442.                     Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[iRowOffset + iX - 1] = RGB(a1 * _Red(iBorderColor)   + a2 * _Red(col), _
  3443.                                                                                          a1 * _Green(iBorderColor) + a2 * _Green(col), _
  3444.                                                                                          a1 * _Blue(iBorderColor)  + a2 * _Blue(col))
  3445.                 EndIf
  3446.             Next
  3447.         Next
  3448.         For iY = 1 To iH - 1
  3449.             For iX = 0 To iW - 1
  3450.                 If Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[(iY - 1) * iW + iX] <> Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[iY * iW + iX] Then
  3451.                     col = Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[(iY - 1) * iW + iX]
  3452.                     Cast(ULong Ptr, tBitmapData_Mosaic.Scan0)[(iY - 1) * iW + iX] = RGB(a1 * _Red(iBorderColor)   + a2 * _Red(col), _
  3453.                                                                                         a1 * _Green(iBorderColor) + a2 * _Green(col), _
  3454.                                                                                         a1 * _Blue(iBorderColor)  + a2 * _Blue(col))
  3455.                 EndIf
  3456.             Next
  3457.         Next   
  3458.     EndIf
  3459.     GdipBitmapUnlockBits(hBitmap_Mosaic, @tBitmapData_Mosaic)
  3460.    
  3461.     If bGDI Then
  3462.         GdipCreateHBITMAPFromBitmap(hBitmap_Mosaic, @hGDIBitmap, &hFF000000)
  3463.         GdipDisposeImage(hBitmap_Mosaic)
  3464.         Return hGDIBitmap
  3465.     EndIf
  3466.     Return hBitmap_Mosaic
  3467. End Function
  3468.  
  3469. Function _GDIPlus_BitmapApplyFilter_Blur(ByVal hImage As Any Ptr, iRadius As UByte) As Any Ptr Export '...'
  3470.    If iRadius < 1 Then Return 0
  3471.  
  3472.     Dim As Single iW, iH
  3473.     Dim As Integer iStatus
  3474.    
  3475.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3476.     If iStatus <> 0 Then Return 0
  3477.      
  3478.     Dim As Any Ptr hBitmap_Blurred
  3479.     Dim As BitmapData tBitmapData, tBitmapData_Blurred
  3480.     Dim As RECT tRect_Blurred = Type(0, 0, iW - 1, iH - 1)
  3481.  
  3482.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Blurred)
  3483.     GdipBitmapLockBits(hBitmap_Blurred, Cast(Any Ptr, @tRect_Blurred), ImageLockModeWrite, PixelFormat32bppARGB, @tBitmapData_Blurred)
  3484.     GdipBitmapLockBits(hImage, Cast(Any Ptr, @tRect_Blurred), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)  
  3485.    
  3486.     Dim As UInteger iX, iY, iXX, iYY, ile, avgR, avgB, avgG, iARGB
  3487.     For iY = 0 To iH - 1
  3488.         For iX = 0 To iW - 1
  3489.            avgR = 0: avgB = 0: avgG = 0: ile = 0
  3490.          For iXX = iX To iX + iRadius
  3491.             For iYY = iY To iY + iRadius
  3492.                If (iXX >= 0 And iYY >= 0 And iXX < iW And iYY < iH) Then
  3493.                   iARGB = Cast(ULong Ptr, tBitmapData.Scan0)[iYY * iW + iXX]
  3494.                   avgR += (iARGB Shr 16) And &hFF
  3495.                   avgG += (iARGB Shr 8) And &hFF
  3496.                   avgB += iARGB And &hFF
  3497.                   ile += 1
  3498.                EndIf
  3499.             Next
  3500.          Next      
  3501.         Cast(ULong Ptr, tBitmapData_Blurred.Scan0)[iY * iW + iX] = &hFF000000 + (avgR \ ile) Shl 16 + (avgG \ ile) Shl 8 + (avgB \ ile)
  3502.         Next
  3503.     Next
  3504.    
  3505.     GdipBitmapUnlockBits(hBitmap_Blurred, @tBitmapData_Blurred)
  3506.     GdipBitmapUnlockBits(hImage, @tBitmapData)
  3507.    
  3508.     Return hBitmap_Blurred
  3509. End Function
  3510.  
  3511. 'Function _GDIPlus_BitmapApplyFilter_WaterDropGlassPane(ByVal hImage As Any Ptr, iPosX As UShort, iPosY As UShort, iAmount As UShort, iSizeMin As UByte, iSizeMax As UShort, iBlur As UByte, bGDI As Bool) As Any Ptr Export
  3512. '    Dim As Single iW, iH
  3513. '   Dim As Integer iStatus
  3514. '  
  3515. '   iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3516. '   If iStatus <> 0 Then Return 0
  3517. '  
  3518. '   Dim As Any Ptr hBitmap_WaterDrop, hBitmap_Flipped, hGDIBitmap, hGfx
  3519.    
  3520. '   GdipCloneBitmapArea(0, 0, iW, iH, PixelFormat32bppARGB, hImage, @hBitmap_Flipped)
  3521. '   GdipImageRotateFlip(hBitmap_Flipped, 6)
  3522.    
  3523. '   hBitmap_WaterDrop = _GDIPlus_BitmapApplyFilter_Blur(hImage, iBlur)
  3524. '   GdipGetImageGraphicsContext(hBitmap_WaterDrop, @hGfx)
  3525.    
  3526. '   GdipDisposeImage(hBitmap_Flipped)
  3527. '   GdipDeleteGraphics(hGfx)
  3528. '   If bGDI Then
  3529. '       GdipCreateHBITMAPFromBitmap(hBitmap_WaterDrop, @hGDIBitmap, &hFF000000)
  3530. '       GdipDisposeImage(hBitmap_WaterDrop)
  3531. '       Return hGDIBitmap
  3532. '   EndIf
  3533. '   Return hBitmap_WaterDrop       
  3534. 'End Function
  3535.  
  3536. Private Sub drawTriangles(hImageSource As Any Ptr, hImageDestination As Any Ptr, v() As DTVertex, t() As DTTriangle, tcount As Long, bShowEdges As BOOL = False, iAlpha As UByte = &h60, bWireframe As BOOL = False) '...'
  3537.     Dim As Any Ptr hGfx, hBrush, hPen
  3538.     GdipGetImageGraphicsContext(hImageDestination, @hGfx)
  3539.     GdipSetPixelOffsetMode(hGfx, 4)
  3540.     'If bShowEdge Then GdipSetSmoothingMode(hGfx, 4)
  3541.     GdipCreateSolidFill(&hFFFF0000, @hBrush)
  3542.     GdipCreatePen1(0, 1, 2, @hPen)
  3543.    
  3544.     Dim As ULong c1, c2, c3
  3545.     Dim As GpPointF aPoints(2)
  3546.    
  3547.     For i As Integer = 0 To tcount - 1
  3548.         Var p0 = v(t(i).v1), p1 = v(t(i).v2), p2 = v(t(i).v3)
  3549.        
  3550.         'Average color of the 3 vertices
  3551.         'GdipBitmapGetPixel(hImageSource, p0.x, p0.y, @c1)
  3552.         'GdipBitmapGetPixel(hImageSource, p1.x, p1.y, @c2)
  3553.         'GdipBitmapGetPixel(hImageSource, p2.x, p2.y, @c3)     
  3554.         'GdipSetSolidFillColor(hBrush, Rgba((_Red(c1) + _Red(c2) + _Red(c3)) / 3, _
  3555.         '                                   (_Green(c1) + _Green(c2) + _Green(c3)) / 3, _
  3556.         '                                   (_Blue(c1) + _Blue(c2) + _Blue(c3)) / 3, 255))
  3557.        
  3558.         'Center color of the polygon
  3559.        
  3560.         GdipBitmapGetPixel(hImageSource, (p0.x + p1.x + p2.x) / 3, (p0.y + p1.y + p2.y) / 3, @c1)
  3561.         GdipSetSolidFillColor(hBrush, c1)
  3562.        
  3563.         aPoints(0).X = p0.x : aPoints(0).Y = p0.y
  3564.         aPoints(1).X = p1.x : aPoints(1).Y = p1.y
  3565.         aPoints(2).X = p2.x : aPoints(2).Y = p2.y
  3566.        
  3567.         If bWireframe = False Then
  3568.             GdipFillPolygon(hGfx, hBrush, @aPoints(0), 3, FillModeAlternate)
  3569.         Else
  3570.             bShowEdges = True
  3571.         EndIf
  3572.         If bShowEdges Then
  3573.             GdipSetPenColor(hPen, RGBA(_Red(c1) * 0.6666, _Green(c1) * 0.6666, _Blue(c1) * 0.6666, iAlpha))
  3574.             GdipDrawPolygon(hGfx, hPen, @aPoints(0), 3)
  3575.         End If
  3576.     Next
  3577.     GdipDeleteGraphics(hGfx)
  3578.     GdipDeleteBrush(hBrush)
  3579.     GdipDeletePen(hPen)
  3580. End Sub
  3581.  
  3582. Function _GDIPlus_BitmapApplyFilter_Delaunay(ByVal hImage As Any Ptr, iBlur As UByte, fSobel As Single, iBW As UByte, iSpaceX As ULong, iSpaceY As ULong, iBorderSpaceX As UByte, iBorderSpaceY As UByte, _ '...'
  3583.                                              bRndPoints As BOOL, iRndPoints As ULong, bShowEdges As BOOL, iAlpha As UByte, bWireframe As BOOL, bGDI As BOOL) As Any Ptr Export
  3584.     Dim As Single iW, iH
  3585.     Dim As Integer iStatus
  3586.    
  3587.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3588.     If iStatus <> 0 Then Return 0
  3589.    
  3590.     Dim As Any Ptr hGDIBitmap, hBitmap_Delaunay, hImage_tmp, hImage_tmp2, hImage_tmp3
  3591.    
  3592.     hImage_tmp = _GDIPlus_BitmapApplyFilter_Blur(hImage, iBlur)
  3593.     hImage_tmp2 = _GDIPlus_BitmapApplyFilter_Convolution(hImage_tmp, fSobel, 0, SOBEL, 0, 0, False)
  3594.     hImage_tmp3 = _GDIPlus_BitmapCreateBW(hImage_tmp2, iBW)
  3595.     hBitmap_Delaunay = _GDIPlus_BitmapApplyFilter_Rasterize(hImage_tmp3, iSpaceX, iSpaceY, &hFF000000, False)
  3596.  
  3597.     Dim As ULong i
  3598.     Dim As DTVertex vertices(iW * iH)
  3599.  
  3600.     Dim As BitmapData tBitmapData
  3601.     Dim As ULong iX, iY, sw = iW \ iBorderSpaceX, sh = iH \ iBorderSpaceY
  3602.     Dim As ULong iRowOffset, c = 0
  3603.  
  3604.     If bRndPoints Then
  3605.         iRndPoints = IIf(iRndPoints < 3, 3, iRndPoints)
  3606.         Dim As ULong iVertices, i, numPoints = 10, minDist = Sqr(iW * iW + iH * iH) \ (max(iW, iH) \ iRndPoints)
  3607.         Var points = Poisson.sample(iW, iH, minDist, numPoints)
  3608.         Dim v As FB.Vector
  3609.         v = *points
  3610.         For i = 0 To v.count - 1
  3611.             With *Cast(Poisson.SamplePoint Ptr, v[i])
  3612.                 vertices(c).x = .x
  3613.                 vertices(c).y = .y
  3614.                 c += 1
  3615.             End With
  3616.         Next
  3617.        
  3618. '       iVertices = Iif(iRndPoints < 3, iW * iH \ 66, iRndPoints)
  3619. '       For i = 1 To iVertices
  3620. '           vertices(c).x = (iW - 1) * Rnd()
  3621. '           vertices(c).y = (iH - 1) * Rnd()
  3622. '           c += 1
  3623. '       Next   
  3624.     Else
  3625.         Dim As RECT tRect = Type(0, 0, iW - 1, iH - 1)
  3626.         GdipBitmapLockBits(hBitmap_Delaunay, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
  3627.         For iY = 0 To iH - 1
  3628.             iRowOffset = iY * iW
  3629.             For iX = 0 To iW - 1
  3630.                 If Cast(ULong Ptr, tBitmapData.Scan0)[iRowOffset + iX] <> &hFF000000 Then
  3631.                     vertices(c).x = iX
  3632.                     vertices(c).y = iY
  3633.                     c += 1
  3634.                 End If
  3635.             Next
  3636.         Next
  3637.         GdipBitmapUnlockBits(hBitmap_Delaunay, @tBitmapData)
  3638.     EndIf
  3639.    
  3640.     For iX = 0 To iW + 2 * sw Step sw
  3641.         If iX <= iW Then
  3642.             vertices(c).x = iX
  3643.             vertices(c).y = 0
  3644.             c += 1
  3645.         End If 
  3646.     Next
  3647.  
  3648.     For iX = 0 To iW + 2 * sw Step sw
  3649.         If iX <= iW Then
  3650.             vertices(c).x = iX
  3651.             vertices(c).y = iH
  3652.             c += 1
  3653.         End If 
  3654.     Next
  3655.  
  3656.     For iY = 0 To iH + 2 * sh Step sh
  3657.         If iY <= iH Then
  3658.             vertices(c).x = 0
  3659.             vertices(c).y = iY
  3660.             c += 1
  3661.         End If 
  3662.     Next
  3663.  
  3664.     For iY = 0 To iH + 2 * sh Step sh
  3665.         If iY <= iH Then
  3666.             vertices(c).x = iW
  3667.             vertices(c).y = iY
  3668.             c += 1
  3669.         End If 
  3670.     Next
  3671.    
  3672.     Dim As Long nv = (c - 1), ntris
  3673.     Dim As DTTriangle triangles(Any)
  3674.     triangulate(vertices(), nv, triangles(), @ntris)
  3675.    
  3676.     Dim As Any Ptr hGfx
  3677.     GdipGetImageGraphicsContext(hBitmap_Delaunay, @hGfx)
  3678.     GdipGraphicsClear(hGfx, &hFF000000)
  3679.     GdipDeleteGraphics(hGfx)
  3680.  
  3681.     drawTriangles(hImage, hBitmap_Delaunay, vertices(), triangles(), ntris, bShowEdges, iAlpha, bWireframe)
  3682.  
  3683.     GdipDisposeImage(hImage_tmp)
  3684.     GdipDisposeImage(hImage_tmp2)
  3685.     GdipDisposeImage(hImage_tmp3)
  3686.  
  3687.     If bGDI Then
  3688.         GdipCreateHBITMAPFromBitmap(hBitmap_Delaunay, @hGDIBitmap, &hFF000000)
  3689.         GdipDisposeImage(hBitmap_Delaunay)
  3690.         Return hGDIBitmap
  3691.     EndIf
  3692.     Return hBitmap_Delaunay    
  3693. End Function
  3694.  
  3695. Function _GDIPlus_BitmapApplyFilter_Spiral(ByVal hImage As Any Ptr, iMode As UByte, iBgColor As ULong, bGreyScale As BOOL, bGDI As BOOL) As Any Ptr Export '...'
  3696.     Dim As Single iW, iH
  3697.     Dim As Integer iStatus
  3698.    
  3699.     iStatus = GdipGetImageDimension(hImage, @iW, @iH)
  3700.     If iStatus <> 0 Then Return 0
  3701.    
  3702.     iMode = IIf(iMode > 1 , 1, iMode)
  3703.    
  3704.     Dim As Single radiusSpan = 9, arcLengthStep = 2.5, rotationSpeed = fPi / 20, maxRadius = max(iW, iH) * 0.501
  3705.     Randomize
  3706.     Dim As PERLINNOISE Perlin
  3707.     Dim As Single radius, radian, x, y, drawnRad, px, py, radianStep, sw, col2, t1, t2
  3708.     Dim As ULong col, cx, cy
  3709.    
  3710.     cx = iW \ 2
  3711.     cy = iH \ 2
  3712.     drawnRad = 0
  3713.     radian = 0
  3714.     radius = radiusSpan / 2
  3715.     x = cx + Cos(radian) * radius
  3716.     y = cy + Sin(radian) * radius
  3717.  
  3718.     Dim As Any Ptr hGDIBitmap, hBitmap_Spiral, hBitmap_Greyscale, hGfx, hPen, hBrush
  3719.     GdipCreateBitmapFromScan0(iW, iH, 0, PixelFormat32bppARGB, 0, @hBitmap_Spiral)
  3720.     GdipGetImageGraphicsContext(hBitmap_Spiral, @hGfx)
  3721.     GdipSetSmoothingMode(hGfx, 4)
  3722.     GdipSetPixelOffsetMode(hGfx, PixelOffsetModeHalf)
  3723.     GdipCreatePen1(&hFF000000, 1, 2, @hPen)
  3724.     GdipCreateSolidFill(&hFF000000, @hBrush)
  3725.     GdipGraphicsClear(hGfx, iBgColor)
  3726.    
  3727.     If bGreyScale Then hBitmap_Greyscale = _GDIPlus_BitmapCreateGreyscale(hImage)
  3728.    
  3729.     Do
  3730.         px = x
  3731.         py = y
  3732.        
  3733.         radianStep = Map(arcLengthStep, 0, radius * f2Pi, 0, f2Pi)
  3734.         radian += radianStep
  3735.         radius += Map(radianStep, 0, f2Pi, 0, radiusSpan)
  3736.    
  3737.         x = cx + Cos(radian) * radius
  3738.         y = cy + Sin(radian) * radius
  3739.        
  3740.         If bGreyScale Then
  3741.             GdipBitmapGetPixel(hBitmap_Greyscale, x, y, @col)
  3742.         Else
  3743.             GdipBitmapGetPixel(hImage, x, y, @col)
  3744.         EndIf
  3745.         col2 = Map((_Red(col) + _Green(col) + _Blue(col)) / 3, 0, 255, 1, 0)
  3746.         col2 = col2 * col2 * col2
  3747.        
  3748.         sw = Map(col2, 1, 0, radiusSpan * 0.7, 0.01)
  3749.         sw += (Perlin.Noise2D(x, y) - 0.5) * radiusSpan * 0.2
  3750.        
  3751.         Select Case iMode
  3752.             Case 0
  3753.                 GdipSetPenWidth(hPen, sw)
  3754.                 GdipSetPenColor(hPen, col - &h10000000)
  3755.                 GdipDrawLine(hGfx, hPen, px, py, x, y)
  3756.             Case 1
  3757.                 GdipSetSolidFillColor(hBrush, col - &h10000000)
  3758.                 t1 = sw / 2
  3759.                 t2 = sw * 1.5
  3760.                 GdipFillEllipse(hGfx, hBrush, x - t1, y - t1, t2, t2)
  3761.         End Select
  3762.        
  3763.         drawnRad += radianStep
  3764.        
  3765.     Loop Until radius > maxRadius      
  3766.  
  3767.     If bGreyScale Then GdipDisposeImage(hBitmap_Greyscale)
  3768.     GdipDeletePen(hPen)
  3769.     GdipDeleteBrush(hBrush)
  3770.     GdipDeleteGraphics(hGfx)
  3771.     If bGDI Then
  3772.         GdipCreateHBITMAPFromBitmap(hBitmap_Spiral, @hGDIBitmap, &hFF000000)
  3773.         GdipDisposeImage(hBitmap_Spiral)
  3774.         Return hGDIBitmap
  3775.     EndIf
  3776.     Return hBitmap_Spiral  
  3777. End Function
  3778.  
  3779. End Extern
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement