Attribute VB_Name = "ModDeformers"
' ModDeformers.bas

Option Explicit

Public DeformIndex As Integer

Public Enum EDeform
   [BBubbly] = 0
   
   [OTriangles] = 1
   [OBoxes] = 2
   [OEllipses] = 3
   [STriangles] = 4
   [SBoxes] = 5
   [SEllipses] = 6
   
   [WWinFluteH] = 7
   [WWinFluteV] = 8
   [WWinFluteHV] = 9
   [WWinFluteCirc] = 10
   
   [LLens] = 11
   [MMinMag] = 12
   
   [RRippleH] = 13
   [RRippleV] = 14
   
   [RRotate] = 15
   [RotateCircD] = 16
   
   [ShearHAB] = 17
   [ShearVLR] = 18
   
   [SquashAB] = 19
   [SquashLR] = 20
   [SquashH] = 21
   [SquashV] = 22
   [SquashEllH] = 23
   [SquashEllV] = 24
   [DSqueeze] = 25
   [DScrunch] = 26
   
   [RRoundRect] = 27
   [SStars] = 28
   [SStephan] = 29
   [SSwirl] = 30
   
   [TTile] = 31
   [TTunnel] = 32
   [GGhostLR] = 33
   [GGhostAB] = 34
   
   [BUBEVEN] = 35
   
   [LMirror] = 36
   [RMirror] = 37
   [TMirror] = 38
   [Bmirror] = 39
   
   [LReflec] = 40
   [AReflec] = 41
   [BReflec] = 42
   [RReflec] = 43
   
   [HFlip] = 44
   [VFlip] = 45
   
   [ECut] = 46
   
End Enum

Private Type RECT
    Left As Long
    TOp As Long
    Right As Long
    Bottom As Long
End Type

Private REC As RECT

Private Declare Function PtInRect Lib "USER32" _
   (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long

Private Declare Function SetRect Lib "USER32" _
(lpRect As RECT, ByVal x1 As Long, ByVal y1 As Long, _
  ByVal x2 As Long, ByVal y2 As Long) As Long


'Dim REC As RECT
'SetRect REC, xmin, ymin, xmax, ymax
'SetRect REC, 0, 0, FullW - 1, FullH - 1
'If PtInRect(REC, XX, YY) <> 0 Then ' Inside REC
'  If = 0 then outside REC

Public aUseRightColor As Boolean
Public aCircleLens As Boolean
Public aAARotate As Boolean
Public aAARotateCirc As Boolean

Private zix As Single, ziy As Single
Private ix As Long, iy As Long
Private ixx As Long, iyy As Long   ' Mask coords for Public MaskArray()

Private k As Long
Private picData2() As Byte

' Colors for Castes
Private CASTR As Long, CASTG As Long, CASTB As Long
Private CASTCul As Long

Dim OffsetY() As Single

'<<<< Progress bar >>>>>>>>>>
Private xd As Single, xp As Single



Public Sub CASTControl(CASTType As Long, CASTLen As Long)
' CASTlen depends on CASTType set & called from frmDeformers
' CASTLen = 5 to 100

' CASTtype =
' 1 CAST Open Triangle
' 2 CAST Open Boxes
' 3 CAST Open Ellipse
' 4 CAST Solid Triangle
' 5 CAST SOLID Boxes
' 6 CAST SOLID Ellipse

   picDATA() = picDATAORG()
   
   If aUseRightColor Then FillpicDATAwithRightColor
   
   StartProgress (HIY / 8)
   For iy = 0 To FullH - 1 Step 8
   For ix = 0 To FullW - 1 Step 8
'      If aSelectAccepted Then ' Selection ON
'         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
'            If Not aExternal Then   ' Internal
'               If MaskArray(ix - LOX, iy - LOY) = 0 Then AllCASTTypes CASTType, CASTLen, ix, iy
'            Else  'External
'               If MaskArray(ix - LOX, iy - LOY) <> 0 Then AllCASTTypes CASTType, CASTLen, ix, iy
'            End If
'         Else
'            If aExternal Then AllCASTTypes CASTType, CASTLen, ix, iy
'         End If
'      Else  ' Selection OFF
         AllCASTTypes CASTType, CASTLen, ix, iy
'      End If
   Next ix
   DrawProgress
   Next iy
   
   
'   StartProgress (HIY / 8)
'   For iy = LOY To HIY Step 8
'   For ix = LOX To HIX Step 8
'         If aSelectAccepted Then
'            iyy = iy - LOY
'            ixx = ix - LOX
'            If MaskArray(ixx, iyy) = 0 Then
'               AllCASTTypes CASTType, CASTLen, ix, iy
'            End If
'         Else
'            AllCASTTypes CASTType, CASTLen, ix, iy
'         End If
'   Next ix
'   DrawProgress
'   Next iy
'

   frmDeformers.picPB.Cls

End Sub

Public Sub AllCASTTypes(CASTType As Long, CASTLen As Long, ixx As Long, iyy As Long)
' CASTLen = 5 to 100

' CASTtype =
' 1 CAST Open Triangle
' 2 CAST Open Boxes
' 3 CAST Open Ellipse
' 4 CAST Solid Triangle
' 5 CAST SOLID Boxes
' 6 CAST SOLID Ellipse
Dim ix1 As Long, iy1 As Long
Dim ix2 As Long, iy2 As Long
Dim ix3 As Long, iy3 As Long

Dim BLR As Byte, BLG As Byte, BLB As Byte
Dim zradx As Single, zrady As Single

   CASTXYCul CASTLen, ixx, iyy, ix1, iy1, ix2, iy2, ix3, iy3
' Returns:
' Private CASTR As Long, CASTG As Long, CASTB As Long
' Private CASTCul As Long
' & (ix1,iy1) & (ix2,iy2) & (ix3,iy3)

Select Case CASTType
Case 1, 4

' TRIANGLES ==========================================
' BresLine Picata(), ix1, iy1, ix2, iy2, _
'    r, g, B, 0, 0  'Optional ND As Long = 0, Optional Convert As Long = 0)
   BLR = CByte(CASTR)
   BLG = CByte(CASTG)
   BLB = CByte(CASTB)
   
'If CASTType = 1 Then
'' SPIKY
' Dim i As Long
' Dim h As Long
' Dim fx(0 To 7) As Long, fy(0 To 7) As Long
' h = CASTLen / 5
'' ix1,iy1
' fx(0) = ix1: fx(1) = ix1 + Rnd * h: fx(2) = ix1 + Rnd * h: fx(3) = ix1 + Rnd * h
' fx(4) = ix1: fx(5) = ix1 - Rnd * h: fx(6) = ix1 - Rnd * h: fx(7) = ix1 - Rnd * h
'
' fy(0) = iy1 - h: fy(1) = iy1 - h: fy(2) = iy1: fy(3) = iy1 + h
' fy(4) = iy1 + h: fy(5) = iy1 + h: fy(6) = iy1: fy(7) = iy1 - h
'For i = 0 To 2 '0 To 7
'   BresLine ix1, iy1, fx(i), fy(i), BLR, BLG, BLB, 1, 0
'Next i
'End If

'If (1& * BLR + BLG + BLB) / 3 < 190 Then ' Light areas unaffected
   If CASTType = 1 Then
      If aSelectAccepted Then
         BresLineCALC ix1, iy1, ix2, iy2, BLR, BLG, BLB
         BresLineCALC ix2, iy2, ix3, iy3, BLR, BLG, BLB
         BresLineCALC ix3, iy3, ix1, iy1, BLR, BLG, BLB
      Else
         BresLine ix1, iy1, ix2, iy2, BLR, BLG, BLB, 1, 0
         BresLine ix2, iy2, ix3, iy3, BLR, BLG, BLB, 1, 0
         BresLine ix3, iy3, ix1, iy1, BLR, BLG, BLB, 1, 0
      End If
   Else
      FillTriangle ix1, iy1, ix2, iy2, ix3, iy3, BLR, BLG, BLB
   End If
'End If
' ====================================================
Case 2, 5

' BOX ================================================
' BresLine Picata(), ix1, iy1, ix2, iy2, _
'    r, g, B, 0, 0  'Optional ND As Long = 0, Optional Convert As Long = 0)
   BLR = CByte(CASTR)
   BLG = CByte(CASTG)
   BLB = CByte(CASTB)
   If CASTType = 2 Then
      If aSelectAccepted Then
         BresLineCALC ix1, iy1, ix3, iy1, BLR, BLG, BLB
         BresLineCALC ix3, iy1, ix3, iy3, BLR, BLG, BLB
         BresLineCALC ix3, iy3, ix1, iy3, BLR, BLG, BLB
         BresLineCALC ix1, iy3, ix1, iy1, BLR, BLG, BLB
      Else
         BresLine ix1, iy1, ix3, iy1, BLR, BLG, BLB, 1, 0
         BresLine ix3, iy1, ix3, iy3, BLR, BLG, BLB, 1, 0
         BresLine ix3, iy3, ix1, iy3, BLR, BLG, BLB, 1, 0
         BresLine ix1, iy3, ix1, iy1, BLR, BLG, BLB, 1, 0
      End If
   Else
      BresBoxFill ix1, iy1, ix3, iy3, CASTCul, 0, 0
   End If
' ====================================================

Case 3, 6

'ELLIPSE FILLED ======================================
      zradx = Rnd * CASTLen / 2 '20 'Abs(ix2 - ix1)
      zrady = Rnd * CASTLen / 2 '20 'Abs(ix2 - ix1)
      If CASTType = 3 Then
         ' NOT FILL
         BresEllipse ix1, iy1, zradx, zrady, CASTCul, 0, 0, 0
      Else
         ' BresEllipse Filled checks or a selection
         ' if so uses BresLineCALC instead of just Bresline.
         BresEllipse ix1, iy1, zradx, zrady, CASTCul, 1, 0, 0
      End If
' CASTCul,
' Optional Fill As Long = 0, _
' Optional ND As Long = 0, _
' Optional Convert As Long = 0)
'=====================================================
End Select

End Sub

Private Sub CASTXYCul(CASTLen As Long, ixx As Long, iyy As Long, _
            ix1 As Long, iy1 As Long, ix2 As Long, iy2 As Long, ix3 As Long, iy3 As Long)
' IN: CASTLen (5 to 100),ix & iy
' OUT:
' Private CASTR As Long, CASTG As Long, CASTB As Long
' Private CASTCul As Long
' & (ix1,iy1) & (ix2,iy2) & (ix3,iy3)
Dim xx1 As Long, xx2 As Long, xx3 As Long

Dim Halflen As Long
Halflen = CASTLen / 2

   HLXY

'   ' Generate three random coordinates
'   ' Adapted from an idea of Tanner Helland

   ix1 = ixx
   ix2 = (Rnd * CASTLen) - Halflen + ix1
   If ix2 < LowerX Then ix2 = LowerX
   If ix2 > HigherX Then ix2 = HigherX
   
   ix3 = Int(Rnd * CASTLen) - Halflen + ix1
   If ix3 < LowerX Then ix3 = LowerX
   If ix3 > HigherX Then ix3 = HigherX

   iy1 = iyy
   iy2 = (Rnd * CASTLen) - Halflen + iy1
   If iy2 < LowerY Then iy2 = LowerY
   If iy2 > HigherY Then iy2 = HigherY


   iy3 = Int(Rnd * CASTLen) - Halflen + iy1
   If iy3 < LowerY Then iy3 = LowerY
   If iy3 > HigherY Then iy3 = HigherY

   CASTR = picDATAORG(2, ix1, iy1)
   CASTR = CASTR + picDATAORG(2, ix2, iy2)
   CASTR = CASTR + picDATAORG(2, ix3, iy3)

   CASTG = picDATAORG(1, ix1, iy1)
   CASTG = CASTG + picDATAORG(1, ix2, iy2)
   CASTG = CASTG + picDATAORG(1, ix3, iy3)

   CASTB = picDATAORG(0, ix1, iy1)
   CASTB = CASTB + picDATAORG(0, ix2, iy2)
   CASTB = CASTB + picDATAORG(0, ix3, iy3)

   CASTB = CASTB / 3
   CASTG = CASTG / 3
   CASTR = CASTR / 3
   CASTCul = RGB(CASTR, CASTG, CASTB)
End Sub

Public Sub FillTriangle(x1 As Long, y1 As Long, _
                         x2 As Long, y2 As Long, _
                         x3 As Long, y3 As Long, _
                         BLR As Byte, BLG As Byte, BLB As Byte)
' Fill Triangle by TomToad
' http://www.blitzmax.com

Dim x As Long, y As Long
Dim Length As Single
Dim Slope1 As Single
Dim Slope2 As Single
Dim Slope3 As Single
    
'   BLR = CByte(CASTR)
'   BLG = CByte(CASTG)
'   BLB = CByte(CASTB)
    
    'Triangle coordinates must be ordered: where x1 < x2 < x3
    If x2 < x1 Then x = x2: y = y2: x2 = x1: y2 = y1: x1 = x: y1 = y
    'swap x1, y1, with x3, y3
    If x3 < x1 Then x = x3: y = y3: x3 = x1: y3 = y1: x1 = x: y1 = y
    'swap x2, y2 with x3, y3
    If x3 < x2 Then x = x3: y = y3: x3 = x2: y3 = y2: x2 = x: y2 = y
    If x1 <> x3 Then Slope1 = (y3 - y1) / (x3 - x1)
    'draw the first half of the triangle
    Length = x2 - x1
    If Length <> 0 Then
        Slope2 = (y2 - y1) / (x2 - x1)
        For x = 0 To Length
            If aSelectAccepted Then
               BresLineCALC (x + x1), (x * Slope1 + y1), (x + x1), (x * Slope2 + y1), BLR, BLG, BLB
            Else
               BresLine (x + x1), (x * Slope1 + y1), (x + x1), (x * Slope2 + y1), BLR, BLG, BLB, 1, 0
            End If
            'PIC.Line (x + x1, x * Slope1 + y1)-(x + x1, x * Slope2 + y1), vbRed
        Next x
    End If
    'draw the second half of the triangle
    y = Length * Slope1 + y1: Length = x3 - x2
    If Length <> 0 Then
        Slope3 = (y3 - y2) / (x3 - x2)
        For x = 0 To Length
            If aSelectAccepted Then
               BresLineCALC x + x2, x * Slope1 + y, x + x2, x * Slope3 + y2, BLR, BLG, BLB
            Else
               BresLine x + x2, x * Slope1 + y, x + x2, x * Slope3 + y2, BLR, BLG, BLB, 1, 0
            End If
            'PIC.Line (x + x2, x * Slope1 + y)-(x + x2, x * Slope3 + y2), vbRed
        Next x
    End If
End Sub

' ##########################################################################

Public Sub FillpicDATAwithRightColor()
Dim RR As Byte, RG As Byte, RB As Byte
Dim REC As RECT
'SetRect REC, 0, 0, FullW - 1, FullH - 1
'If PtInRect(REC, XX, YY) <> 0 Then ' Quad1
   
   
   LngToRGB RightColor, RR, RG, RB
   If aSelectAccepted Then
      If Not aExternal Then   ' Internal
         For iy = LOY To HIY
         For ix = LOX To HIX
            If MaskArray(ix - LOX, iy - LOY) = 0 Then
               picDATA(0, ix, iy) = RB
               picDATA(1, ix, iy) = RG
               picDATA(2, ix, iy) = RR
            End If
         Next ix
         Next iy
      Else    ' EXternal
         
         SetRect REC, LOX, LOY, HIX, HIY
         
         For iy = 0 To FullH - 1
         For ix = 0 To FullW - 1
            
            If PtInRect(REC, ix, iy) <> 0 Then ' In REC
               If MaskArray(ix - LOX, iy - LOY) <> 0 Then
                  picDATA(0, ix, iy) = RB
                  picDATA(1, ix, iy) = RG
                  picDATA(2, ix, iy) = RR
               End If
            
            ElseIf PtInRect(REC, ix, iy) = 0 Then ' Not in REC
               picDATA(0, ix, iy) = RB
               picDATA(1, ix, iy) = RG
               picDATA(2, ix, iy) = RR
            End If
         Next ix
         Next iy
      
      End If
      
   Else  ' No Selection
      For iy = 0 To FullH - 1
      For ix = 0 To FullW - 1
         picDATA(0, ix, iy) = RB
         picDATA(1, ix, iy) = RG
         picDATA(2, ix, iy) = RR
      Next ix
      Next iy
   End If
End Sub

Public Sub Bubbly(zPLENS As Single, ER As Long)
' zPLENS As Single  0.1 -> 4
' ER = 0 Even spacing else Random
' Private zix As Single, ziy As Single

Dim zRM As Single
Dim CH As Long, CW As Long
Dim LimX As Long, LimY As Long
   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
   If aSelectAccepted Then
      If Not aExternal Then   ' Internal
         CH = HIY - LOY
         CW = HIX - LOX
         LimX = HIX
         LimY = HIY
      Else 'External
         CH = FullH - 1
         CW = FullW - 1
         LimX = FullW - 1
         LimY = FullH - 1
      End If
   Else
      CH = FullH - 1
      CW = FullW - 1
      LimX = FullW - 1
      LimY = FullH - 1
   End If
   zRM = zPLENS * Sqr(CW ^ 2 + CH ^ 2) / 50
   
   Rnd (-0.5)  ' If omitted get different pattern each time
   Randomize (1)
   StartProgress (HIY / (2 * zRM))
   
   For ziy = zRM To LimY + zRM Step 2 * zRM
   For zix = zRM To LimX + zRM Step 2 * zRM
      BubblyCALC zPLENS, zRM, ER
   Next zix
   DrawProgress
   Next ziy
   
   frmDeformers.picPB.Cls
End Sub

Private Sub BubblyCALC(zPLENS As Single, zRM As Single, ER As Long)
' zPLENS As Single  0.1 -> 4
' ER = 0 Even spacing else Random
' Private zix As Single, ziy As Single


Dim zA As Single
Dim zR As Single
Dim ixv As Long
Dim iyv As Long
Dim ixdr As Long
Dim iydr As Long
Dim ii As Single, jj As Single
Dim Rand As Single

Dim aInner As Boolean
Dim aOK As Boolean

   aInner = False
   
   Rand = Rnd
   If ER = 0 Then Rand = -1
   
   If Rand < 0.4 Then
      
      For jj = -zRM To zRM
      For ii = -zRM To zRM
      
         zA = zATan2(CSng(jj), CSng(ii))
         zR = Sqr(ii * ii + jj * jj)
         If zR <= zRM Then
               zR = zR * (1 + 16 * zR) / (1 + 16 * zRM * zPLENS)
               ixv = zix + zR * Cos(zA)   ' Source
               iyv = ziy + zR * Sin(zA)
               
               If (ixv >= 0 And ixv <= FullW - 1) And (iyv >= 0 And iyv <= FullH - 1) Then
                     ixdr = zix + ii + zRM   ' Destination
                     iydr = ziy + jj + zRM
                     If (ixdr >= 0 And ixdr <= FullW - 1) And (iydr >= 0 And iydr <= FullH - 1) Then
                           
                        If aSelectAccepted Then
                           aInner = (ixdr >= LOX And ixdr <= HIX) And (iydr >= LOY And iydr <= HIY)

                           If Not aExternal And aInner Then
                              If MaskArray(ixdr - LOX, iydr - LOY) = 0 Then BubblySubCALC ixdr, iydr, ixv, iyv
                           End If
                           If aExternal Then
                              If aInner Then
                                 If MaskArray(ixdr - LOX, iydr - LOY) <> 0 Then
                                    aOK = True
                                 Else
                                    aOK = False
                                 End If
                              Else ' Not aInner
                                 aOK = True
                              End If
                              If aOK Then BubblySubCALC ixdr, iydr, ixv, iyv
                           End If
                        Else
                           BubblySubCALC ixdr, iydr, ixv, iyv
                        End If
                        
                     End If
               End If
         End If
      Next ii
      Next jj
   
   End If
End Sub
Private Sub BubblySubCALC(ixdr As Long, iydr As Long, ixv As Long, iyv As Long)
   picDATA(0, ixdr, iydr) = picDATAORG(0, ixv, iyv)
   picDATA(1, ixdr, iydr) = picDATAORG(1, ixv, iyv)
   picDATA(2, ixdr, iydr) = picDATAORG(2, ixv, iyv)
End Sub

Public Sub EllipticH(zPELLIPSE As Single)
' zPELLIPSE  0.005 -> 1
Dim xc As Single
Dim yc As Single
Dim zH As Single
Dim xi As Single
Dim xw As Single
Dim ixd As Long, iyd As Long
Dim zA As Single, zB As Single ' Ellipse axes
Dim zix As Single
Dim CH As Long, CW As Long
Dim aInner As Boolean

picDATA() = picDATAORG()
If aUseRightColor Then FillpicDATAwithRightColor

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX - 1
   
   If zPELLIPSE < 1 Then
      zB = zPELLIPSE * (CH - 1) / 2
      zA = CW / 2 - 1
   Else
      zB = CH / 2
      zA = (2 - zPELLIPSE) * (CW - 1) / 2 - 1
   End If
   xc = LOX + CW / 2
   yc = LOY + CH / 2
   zH = (CH - 1) / 2
   
   StartProgress (HIY)
   For iy = LOY To HIY
      zix = (1 - ((iy - yc) / zB) ^ 2)
      
      If zix >= 0 Then
         xw = zA * Sqr(zix)     ' half width
         xi = LOX + CW / 2 - xw ' indent
         xw = 2 * xw / (CW - 1)  ' ellipse FullW/full FullW
         iyd = 0.5 + yc + (((iy - (LOY - 1)) - 1) - zH) * zB / zH
         If iyd = 0 Then iyd = LOY
         If iyd > HIY Then iyd = HIY
         
         For ix = LOX To HIX - 1
            ixd = 1 + xi + ((ix - (LOX - 1)) - 1) * xw - 0.5
            If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            
               If aSelectAccepted Then
                  If MaskArray(ixd - LOX, iyd - LOY) = 0 Then EllipticHVCALC ixd, iyd
               Else
                  EllipticHVCALC ixd, iyd
               End If
            End If
         Next ix
      
      End If
      DrawProgress
   Next iy

Else   ' aSelectAccepted and External
   CH = FullH - 1
   CW = FullW - 1
   xc = CW / 2
   yc = CH / 2
   zH = (CH - 1) / 2
   If zPELLIPSE < 1 Then
      zB = zPELLIPSE * (CH - 1) / 2
      zA = CW / 2 - 1
   Else
      zB = CH / 2
      zA = (2 - zPELLIPSE) * (CW - 1) / 2 - 1
   End If

   StartProgress (HIY)
   For iy = 0 To FullH - 1

      zix = (1 - ((iy - yc) / zB) ^ 2)

      If zix >= 0 Then
         xw = zA * Sqr(zix)     ' half width
         xi = CW / 2 - xw ' indent
         xw = 2 * xw / (CW - 1)  ' ellipse FullW/full FullW
         iyd = 0.5 + yc + (iy - zH) * zB / zH
         If iyd > FullH - 1 Then iyd = FullH - 1

         For ix = 0 To FullW - 2
            ixd = 1 + xi + ix * xw - 0.5
            If (ixd >= 0 And ixd <= FullW - 1) And (iyd >= 0 And iyd <= FullH - 1) Then
               If (ixd >= LOX And ixd <= HIX) And (iyd >= LOY And iyd <= HIY) Then
                  If MaskArray(ixd - LOX, iyd - LOY) <> 0 Then EllipticHVCALC ixd, iyd
               Else
                  EllipticHVCALC ixd, iyd
               End If
            End If
         Next ix

      End If
      DrawProgress
   Next iy

End If

   frmDeformers.picPB.Cls
End Sub
Private Sub EllipticHVCALC(ixd As Long, iyd As Long)
      picDATA(0, ixd, iyd) = picDATAORG(0, ix, iy)
      picDATA(1, ixd, iyd) = picDATAORG(1, ix, iy)
      picDATA(2, ixd, iyd) = picDATAORG(2, ix, iy)
End Sub

Public Sub EllipticV(zPELLIPSE As Single)
' zPELLIPSE  0.005 -> 1
Dim xc As Single
Dim yc As Single
Dim zW As Single  ' was zH
Dim yi As Single  ' xi
Dim yw As Single  ' xw
Dim ixd As Long, iyd As Long
Dim zA As Single, zB As Single ' Ellipse axes
Dim ziy As Single ' was zix
Dim CH As Long, CW As Long

   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX - 1
   
   If zPELLIPSE < 1 Then
      zB = zPELLIPSE * (CW - 1) / 2
      zA = CH / 2 - 1
   Else
      zB = CW / 2
      zA = (2 - zPELLIPSE) * (CH - 1) / 2 - 1
   End If
   xc = LOX + CW / 2
   yc = LOY + CH / 2
   zW = (CW - 1) / 2
   
   StartProgress (HIX)
   For ix = LOX To HIX
      
      ziy = (1 - ((ix - xc) / zB) ^ 2)
      
      If ziy >= 0 Then
         yw = zA * Sqr(ziy)     ' half width
         yi = LOY + CH / 2 - yw ' indent
         yw = 2 * yw / (CH - 1)  ' ellipse FullW/full FullW
         ixd = 0.5 + xc + (((ix - (LOX - 1)) - 1) - zW) * zB / zW
         If ixd = 0 Then ixd = LOX
         If ixd > HIX Then ixd = HIX
         For iy = LOY To HIY - 1
            iyd = 1 + yi + ((iy - (LOY - 1)) - 1) * yw - 0.5
            If iyd = 0 Then iyd = LOY
            If iyd > LOY Then
            If iyd <= HIY Then
               If aSelectAccepted Then
                  If MaskArray(ixd - LOX, iyd - LOY) = 0 Then EllipticHVCALC ixd, iyd
               Else
                  EllipticHVCALC ixd, iyd
               End If
            End If
            End If
         Next iy
      End If
      DrawProgress
   Next ix
   
Else   ' aSelectAccepted and External
   CH = FullH - 1
   CW = FullW - 1
   xc = CW / 2
   yc = CH / 2
   zW = (CW - 1) / 2
   If zPELLIPSE < 1 Then
      zB = zPELLIPSE * (CW - 1) / 2
      zA = CH / 2 - 1
   Else
      zB = CW / 2
      zA = (2 - zPELLIPSE) * (CH - 1) / 2 - 1
   End If

   StartProgress (HIX)
   For ix = 0 To FullW - 1
      
      ziy = (1 - ((ix - xc) / zB) ^ 2)
      If ziy >= 0 Then
         yw = zA * Sqr(ziy)     ' half width
         yi = CH / 2 - yw ' indent
         yw = 2 * yw / (CH - 1)  ' ellipse FullW/full FullW
         ixd = 0.5 + xc + (ix - zW) * zB / zW
         If ixd > FullW - 1 Then ixd = FullW - 1
         For iy = 0 To FullH - 2
            iyd = 1 + yi + iy * yw - 0.5
            If (ixd >= 0 And ixd <= FullW - 1) And (iyd >= 0 And iyd <= FullH - 1) Then
               If (ixd >= LOX And ixd <= HIX) And (iyd >= LOY And iyd <= HIY) Then
                  If MaskArray(ixd - LOX, iyd - LOY) <> 0 Then EllipticHVCALC ixd, iyd
               Else
                  EllipticHVCALC ixd, iyd
               End If
            End If
         Next iy
      End If
      DrawProgress
   Next ix

End If
   frmDeformers.picPB.Cls
End Sub


Public Sub EllipticCut()
' Only works reasonably with a rectangle  INTERNAL SELECTION ONLY
Dim xc As Single
Dim yc As Single
Dim zA As Single, zB As Single ' Ellipse axes
Dim CH As Long, CW As Long
Dim zRoot As Single
Dim xL As Single, xR As Single
Dim SQ As Single
   
   ' All or Internal only
   picDATA() = picDATAORG()
   FillpicDATAwithRightColor
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then   ' Not worth it
   
   CH = HIY - LOY
   CW = HIX - LOX - 1
   zB = CH / 2
   zA = (CW - 1) / 2 - 1
   xc = LOX + CW / 2
   yc = LOY + CH / 2
   StartProgress (HIY)
   For iy = LOY To HIY
      'Elliptic cut
      SQ = (1 - (iy - yc) ^ 2 / zB ^ 2)
      If SQ >= 0 Then
         zRoot = zA * Sqr(SQ)
      Else
         zRoot = 0
      End If
      'Circ cut
      'zRoot = zB * Sqr(1 - (iy - yc) ^ 2 / zB ^ 2)
      xL = xc - zRoot
      xR = xc + zRoot
      For ix = LOX To HIX - 1
         If (ix >= xL) And (ix <= xR) Then
            EllipticCutCALC
         End If
      Next ix
      DrawProgress
   Next iy

   frmDeformers.picPB.Cls
   Exit Sub
   
Else   ' External  WORKS but little point

   CH = FullH - 1
   CW = FullW - 1
   zB = CH / 2
   zA = (CW - 1) / 2
   xc = CW / 2
   yc = CH / 2
   StartProgress (HIY)
   For iy = 0 To FullW - 1
      'Elliptic cut
      SQ = (1 - (iy - yc) ^ 2 / zB ^ 2)
      If SQ >= 0 Then
         zRoot = zA * Sqr(1 - (iy - yc) ^ 2 / zB ^ 2)
      Else
         zRoot = 0
      End If
      'Circ cut
      'zRoot = zB * Sqr(1 - (iy - yc) ^ 2 / zB ^ 2)
      xL = xc - zRoot
      xR = xc + zRoot

      For ix = 0 To FullW - 1
         If (ix >= LOX And ix < HIX) And (iy >= LOY And iy < HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then
               If (ix >= xL) And (ix <= xR) Then
                 EllipticCutCALC
               End If
            End If
         Else
            If (ix >= xL) And (ix <= xR) Then
               EllipticCutCALC
            End If
         End If
      Next ix
      DrawProgress
   Next iy

   frmDeformers.picPB.Cls

End If
End Sub
Private Sub EllipticCutCALC()
   picDATA(0, ix, iy) = picDATAORG(0, ix, iy)
   picDATA(1, ix, iy) = picDATAORG(1, ix, iy)
   picDATA(2, ix, iy) = picDATAORG(2, ix, iy)
End Sub


Public Sub ALens(zPLENS As Single)
' zPLENS  0.1 -> 4
Dim zA As Double
Dim zR As Double
Dim xc As Single
Dim yc As Single
Dim xd As Single
Dim yd As Single
Dim ixv As Long
Dim iyv As Long
Dim zRMax As Single
Dim zLim As Single
Dim CH As Long, CW As Long
 
'Dim TT As Double

   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
   If aSelectAccepted Then
      If Not aExternal Then   ' Internal
         CH = HIY - LOY
         CW = HIX - LOX
         xc = LOX + CW / 2: yc = LOY + CH / 2
      Else 'External
         CH = FullH - 1
         CW = FullW - 1
         xc = CW / 2: yc = CH / 2
      End If
   Else
      CH = FullH - 1
      CW = FullW - 1
      xc = CW / 2: yc = CH / 2
   End If
   'xc = LOX + CW / 2: yc = LOY + CH / 2
   zRMax = Sqr((CW - xc) ^ 2 + (CH - yc) ^ 2) / 2
   zLim = 2
   
   StartProgress HIY
   For iy = 0 To FullH - 1
   For ix = 0 To FullW - 1
      
      yd = iy - yc - 1
      xd = ix - xc - 1
      ' Convert to polar coords
      zA = zATan2(yd, xd)
      'zA = -zAng(yd, xd)  ' Same
      zR = Sqr((yd * yd + xd * xd))

      If zR <= zRMax * zPLENS Then
         zR = zR * (1 + 4 * zR) / (1 + 4 * zRMax * zPLENS)
         ' Convert back to Cartesian
         ixv = xc + zR * Cos(zA)
         iyv = yc + zR * Sin(zA)
            
         If (ixv >= 0 And ixv <= FullW - 1) And (iyv >= 0 And iyv <= FullH - 1) Then
         
            If aSelectAccepted Then ' Selection ON
               If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
                  If Not aExternal Then   ' Internal
                     If MaskArray(ix - LOX, iy - LOY) = 0 Then ALensCALC ixv, iyv, zR, zRMax, zPLENS, zLim
                  Else  'External
                     If MaskArray(ix - LOX, iy - LOY) <> 0 Then ALensCALC ixv, iyv, zR, zRMax, zPLENS, zLim
                  End If
               Else
                  If aExternal Then ALensCALC ixv, iyv, zR, zRMax, zPLENS, zLim
               End If
            Else  ' Selection OFF
               ALensCALC ixv, iyv, zR, zRMax, zPLENS, zLim
            End If
         
         End If
      
      End If
   Next ix
   DrawProgress
   Next iy

   frmDeformers.picPB.Cls
End Sub
   
Private Sub ALensCALC(ixv As Long, iyv As Long, zR As Double, zRMax As Single, zPLENS As Single, zLim As Single)
   If zR < (zRMax * zPLENS - zLim) Then
      picDATA(0, ix, iy) = picDATAORG(0, ixv, iyv)
      picDATA(1, ix, iy) = picDATAORG(1, ixv, iyv)
      picDATA(2, ix, iy) = picDATAORG(2, ixv, iyv)
   ElseIf aCircleLens Then  ' Draw black circle around lens
      If zR < (zRMax * zPLENS + zLim) Then
         picDATA(0, ix, iy) = 0
         picDATA(1, ix, iy) = 0
         picDATA(2, ix, iy) = 0
      End If
   End If
End Sub

   
   
Public Sub MinMag(zPMINMAG As Single)
' zPMINMAG  .01 -> 2 not 0
Dim zA As Single
Dim zR As Single
Dim xc As Single
Dim yc As Single
Dim xd As Single
Dim yd As Single
Dim ixv As Long
Dim iyv As Long
Dim zRMax As Single
Dim zMR As Single
Dim CH As Long, CW As Long
   

   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX
   
   xc = CW / 2: yc = CH / 2
   zRMax = Sqr((CW - xc) ^ 2 + (CH - yc) ^ 2)
   
   xc = LOX - 1 + CW / 2: yc = LOY - 1 + CH / 2
   zMR = zRMax * zPMINMAG
   If zMR <= 0 Then Exit Sub
   
   StartProgress (HIY)
   For iy = LOY To HIY
   For ix = LOX To HIX
      If aSelectAccepted Then
         If MaskArray(ix - LOX, iy - LOY) = 0 Then
            xd = ix - xc
            yd = iy - yc
            zA = zATan2(yd, xd)
            zR = Sqr(yd * yd + xd * xd)
            zR = (zR * zRMax) / zMR
            ixv = xc + zR * Cos(zA)
            iyv = yc + zR * Sin(zA)
            If (ixv >= LOX And ixv <= HIX) And (iyv >= LOY And iyv <= HIY) Then
               MinMagCALC ixv, iyv
            End If
         End If
      Else
         xd = ix - xc
         yd = iy - yc
         zA = zATan2(yd, xd)
         zR = Sqr(yd * yd + xd * xd)
         zR = (zR * zRMax) / zMR
         ixv = xc + zR * Cos(zA)
         iyv = yc + zR * Sin(zA)
         If (ixv >= LOX And ixv <= HIX) And (iyv >= LOY And iyv <= HIY) Then
            MinMagCALC ixv, iyv
         End If
      End If
   Next ix
   DrawProgress
   Next iy

Else    ' aSelectAccepted and External
   CH = FullH - 1
   CW = FullW - 1
   
   xc = CW / 2: yc = CH / 2
   zRMax = Sqr((CW - xc) ^ 2 + (CH - yc) ^ 2)
   
   xc = CW / 2 - 1
   yc = CH / 2 - 1
   zMR = zRMax * zPMINMAG
   If zMR <= 0 Then Exit Sub
   
   StartProgress HIY
   For iy = 0 To FullH - 1
   For ix = 0 To FullW - 1
      xd = ix - xc
      yd = iy - yc
      zA = zATan2(yd, xd)
      zR = Sqr(yd * yd + xd * xd)
      zR = (zR * zRMax) / zMR
      ixv = xc + zR * Cos(zA)
      iyv = yc + zR * Sin(zA)
      
      If (ixv >= 0 And ixv <= FullW - 1) And (iyv >= 0 And iyv <= FullH - 1) Then
         
         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then MinMagCALC ixv, iyv
         Else
            MinMagCALC ixv, iyv
         End If
   
      End If
   Next ix
   DrawProgress
   Next iy
End If

   frmDeformers.picPB.Cls
End Sub
Private Sub MinMagCALC(ixv As Long, iyv As Long)
   picDATA(0, ix, iy) = picDATAORG(0, ixv, iyv)
   picDATA(1, ix, iy) = picDATAORG(1, ixv, iyv)
   picDATA(2, ix, iy) = picDATAORG(2, ixv, iyv)
End Sub

Public Sub RippleH(PRIPPLE As Long)
'  PRIPPLE As Long  1 -> 40
Dim zA As Single
Dim zB As Single
Dim ixd As Long
Dim i As Long
Dim CH As Long, CW As Long
   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   CH = HIY - LOY
   CW = HIX - LOX
   
   zA = CW / Abs(PRIPPLE)
   StartProgress (HIY)
   For iy = LOY To HIY
      zB = CH \ 20
      i = Int(zA * (1 + Sin(pi# * (1 + PRIPPLE * (iy - LOY + 1) / CH))))
      zB = (CW - 2 * i) / (CW)
      For ix = LOX To HIX
         ixd = zB * (ix - LOX) + i + LOX
         If (ixd > LOX) And (ixd <= HIX) Then
            If aSelectAccepted Then
               If MaskArray(ixd - LOX, iy - LOY) = 0 Then RippleHCALC ixd
            Else
               RippleHCALC ixd
            End If
         End If
      Next ix
   DrawProgress
   Next iy

Else     ' aSelectAccepted and External
   CH = FullH - 1
   CW = FullW - 1
   
   zA = CW / Abs(PRIPPLE)
   StartProgress (HIY)

   For iy = 0 To FullH - 1
      zB = CH \ 20
      i = Int(zA * (1 + Sin(pi# * (1 + PRIPPLE * (iy - 0 + 1) / CH))))
      zB = (CW - 2 * i) / (CW)
      For ix = 0 To FullW - 1
         ixd = zB * (ix - 0) + i + 0
      
         If (ixd >= 0 And ixd <= FullW - 1) Then
            
            If (ixd > LOX) And (ixd <= HIX) And (iy >= LOY And iy <= HIY) Then
               If MaskArray(ixd - LOX, iy - LOY) <> 0 Then RippleHCALC ixd
            Else
               RippleHCALC ixd
            End If
         
         End If
      Next ix
   DrawProgress
   Next iy

End If

   frmDeformers.picPB.Cls
End Sub
Private Sub RippleHCALC(ixd As Long)
   picDATA(0, ixd, iy) = picDATAORG(0, ix, iy)
   picDATA(1, ixd, iy) = picDATAORG(1, ix, iy)
   picDATA(2, ixd, iy) = picDATAORG(2, ix, iy)
End Sub



Public Sub RippleV(PRIPPLE As Long)
' PRIPPLE 1 -> 40
Dim zA As Single
Dim zB As Single
Dim iyd As Long
Dim i As Long
Dim CH As Long, CW As Long
   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   CH = HIY - LOY
   CW = HIX - LOX
   
   zA = CH / Abs(PRIPPLE)
   StartProgress (HIX)
   For ix = LOX To HIX
      zB = CW \ 2
      i = Int(zA * (1 + Sin(pi# * (1 + PRIPPLE * (ix - LOX) / CW))))
      zB = (CH - 2 * i) / (CH)
      For iy = LOY To HIY
         iyd = zB * ((iy - LOY) - 1) + i + LOY
         If (iyd >= LOY And iyd <= HIY) Then
            If aSelectAccepted Then
               If MaskArray(ix - LOX, iyd - LOY) = 0 Then RippleVCALC iyd
            Else
               RippleVCALC iyd
            End If
         End If
      Next iy
   DrawProgress
   Next ix

Else     ' aSelectAccepted and External
   
   CH = FullH - 1
   CW = FullW - 1
   
   zA = CH / Abs(PRIPPLE)
   StartProgress (HIX)
   
   For ix = 0 To FullW - 1
      zB = CW \ 2
      i = Int(zA * (1 + Sin(pi# * (1 + PRIPPLE * (ix - 0) / CW))))
      zB = (CH - 2 * i) / (CH)
      For iy = 0 To FullH - 1
         iyd = zB * ((iy - 0) - 1) + i + 0
         
         If (iyd >= 0 And iyd <= FullH - 1) Then

            If (ix > LOX) And (ix <= HIX) And (iyd >= LOY And iyd <= HIY) Then
               If MaskArray(ix - LOX, iyd - LOY) <> 0 Then RippleVCALC iyd
            Else
               RippleVCALC iyd
            End If
         End If
      Next iy
   DrawProgress
   Next ix

End If
   frmDeformers.picPB.Cls
End Sub
Private Sub RippleVCALC(iyd As Long)
   picDATA(0, ix, iyd) = picDATAORG(0, ix, iy)
   picDATA(1, ix, iyd) = picDATAORG(1, ix, iy)
   picDATA(2, ix, iyd) = picDATAORG(2, ix, iy)
End Sub

Public Sub Rotate(zPROTATE As Single)
' zPROTATE  degrees  0 - + 720
Dim ixc As Single, iyc As Single
Dim ixd As Long, iyd As Long
Dim xs As Single, ys As Single
Dim ixs As Long, iys As Long
Dim idx As Single
Dim zrad As Single
Dim zAng As Single
Dim zcos As Single, zsin As Single
Dim CH As Long, CW As Long

   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
zPROTATE = zPROTATE / 2
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX
   zAng = zPROTATE * d2r#
   zcos = Cos(zAng)
   zsin = Sin(-zAng)
   ixc = LOX + CW / 2
   iyc = LOY + CH / 2
   zrad = CW / 2

   If Not aAARotate Then
   
      StartProgress (HIY)
      For iyd = LOY To HIY
         idx = CW / 2
         For ixd = ixc - idx To ixc + idx - 1
            If (ixd >= LOX) And (ixd <= HIX) Then
               xs = ixc + CSng(ixd - ixc) * zcos + CSng(iyd - iyc) * zsin
               ys = iyc + CSng(iyd - iyc) * zcos - CSng(ixd - ixc) * zsin
               ixs = CLng(xs)
               iys = CLng(ys)
               If (ixs >= LOX) And (ixs <= HIX) And (iys >= LOY) And (iys <= HIY) Then
                  If aSelectAccepted Then
                     If MaskArray(ixd - LOX, iyd - LOY) = 0 Then RotateCALC ixd, iyd, ixs, iys
                  Else
                  RotateCALC ixd, iyd, ixs, iys
                  End If
               End If
            End If
         Next ixd
      DrawProgress
      Next iyd
   
   
   Else  ' AARotate
   
      StartProgress (HIY)
      For iyd = LOY To HIY
         idx = CW / 2
         For ixd = ixc - idx To ixc + idx - 1
            If (ixd >= LOX) And (ixd <= HIX) Then
               xs = ixc + CSng(ixd - ixc) * zcos + CSng(iyd - iyc) * zsin
               ys = iyc + CSng(iyd - iyc) * zcos - CSng(ixd - ixc) * zsin
               If xs = Int(xs) Then xs = xs + 0.001
               If ys = Int(ys) Then ys = ys + 0.001
               ixs = Int(xs)
               iys = Int(ys)
               
               If (ixs >= LOX) And (ixs < HIX) And (iys >= LOY) And (iys < HIY) Then
                  If aSelectAccepted Then
                     If MaskArray(ixd - LOX, iyd - LOY) = 0 Then AARotateCALC xs, ys, ixs, iys, ixd, iyd
                  Else
                     AARotateCALC xs, ys, ixs, iys, ixd, iyd
                  End If
               End If
            End If
         Next ixd
      DrawProgress
      Next iyd
   
   End If

Else     ' aSelectAccepted and External

   CH = FullH - 1
   CW = FullW - 1
   zAng = zPROTATE * d2r#
   zcos = Cos(zAng)
   zsin = Sin(-zAng)
   ixc = CW / 2
   iyc = CH / 2
   zrad = CW / 2

   If Not aAARotate Then
   
      StartProgress (HIY)
      For iyd = 0 To FullH - 1
         idx = CW / 2
         For ixd = ixc - idx To ixc + idx - 1
            If (ixd >= 0) And (ixd <= FullW - 1) Then
               xs = ixc + CSng(ixd - ixc) * zcos + CSng(iyd - iyc) * zsin
               ys = iyc + CSng(iyd - iyc) * zcos - CSng(ixd - ixc) * zsin
               ixs = CLng(xs)
               iys = CLng(ys)
               If (ixs >= 0 And ixs <= FullW - 1) And (iys >= 0 And iys <= FullH - 1) Then
               
                  If (ixd >= LOX) And (ixd <= HIX) And (iyd >= LOY) And (iyd <= HIY) Then
                     If MaskArray(ixd - LOX, iyd - LOY) <> 0 Then RotateCALC ixd, iyd, ixs, iys
                  Else
                     RotateCALC ixd, iyd, ixs, iys
                  End If
            
               End If
            End If
         Next ixd
      DrawProgress
      Next iyd

   Else   ' AA Rotate & aSelectAccepted and External
   
      StartProgress (HIY)
      For iyd = 0 To FullH - 1
         idx = CW / 2
         For ixd = ixc - idx To ixc + idx - 1
            If (ixd >= 0) And (ixd <= FullW - 1) Then
               xs = ixc + CSng(ixd - ixc) * zcos + CSng(iyd - iyc) * zsin
               ys = iyc + CSng(iyd - iyc) * zcos - CSng(ixd - ixc) * zsin
               If xs = Int(xs) Then xs = xs + 0.001
               If ys = Int(ys) Then ys = ys + 0.001
               ixs = Int(xs)
               iys = Int(ys)
               If (ixs >= 0 And ixs <= FullW - 2) And (iys >= 0 And iys <= FullH - 2) Then
               
                  If (ixd >= LOX) And (ixd < HIX) And (iyd >= LOY) And (iyd < HIY) Then
                        If MaskArray(ixd - LOX, iyd - LOY) <> 0 Then AARotateCALC xs, ys, ixs, iys, ixd, iyd
                  Else
                     AARotateCALC xs, ys, ixs, iys, ixd, iyd
                  End If
               
               End If
            End If
         Next ixd
      DrawProgress
      Next iyd
   
   
   End If   ' END  AA Rotate & aSelectAccepted and External

End If
   frmDeformers.picPB.Cls
End Sub
Private Sub RotateCALC(ixd As Long, iyd As Long, ixs As Long, iys As Long)
   picDATA(0, ixd, iyd) = picDATAORG(0, ixs, iys)
   picDATA(1, ixd, iyd) = picDATAORG(1, ixs, iys)
   picDATA(2, ixd, iyd) = picDATAORG(2, ixs, iys)
End Sub
Private Sub AARotateCALC(xs As Single, ys As Single, ixs As Long, iys As Long, ixd As Long, iyd As Long)
Dim xsf As Single, ysf As Single
Dim CulB As Long, CulG As Long, CulR As Long
Dim CulB0 As Long, CulG0 As Long, CulR0 As Long
Dim CulB1 As Long, CulG1 As Long, CulR1 As Long
   xsf = xs - ixs
   ysf = ys - iys
   CulB = (1 - xsf) * picDATAORG(0, ixs, iys)
   CulG = (1 - xsf) * picDATAORG(1, ixs, iys)
   CulR = (1 - xsf) * picDATAORG(2, ixs, iys)

   CulB0 = CulB + xsf * picDATAORG(0, ixs + 1, iys)
   CulG0 = CulG + xsf * picDATAORG(1, ixs + 1, iys)
   CulR0 = CulR + xsf * picDATAORG(2, ixs + 1, iys)

   CulB = (1 - xsf) * picDATAORG(0, ixs, iys + 1)
   CulG = (1 - xsf) * picDATAORG(1, ixs, iys + 1)
   CulR = (1 - xsf) * picDATAORG(2, ixs, iys + 1)

   CulB1 = CulB + xsf * picDATAORG(0, ixs + 1, iys + 1)
   CulG1 = CulG + xsf * picDATAORG(1, ixs + 1, iys + 1)
   CulR1 = CulR + xsf * picDATAORG(2, ixs + 1, iys + 1)

   CulB = (1 - ysf) * CulB0 + ysf * CulB1
   CulG = (1 - ysf) * CulG0 + ysf * CulG1
   CulR = (1 - ysf) * CulR0 + ysf * CulR1

   If CulB > 255 Then CulB = 255
   If CulG > 255 Then CulG = 255
   If CulR > 255 Then CulR = 255

   picDATA(0, ixd, iyd) = CulB
   picDATA(1, ixd, iyd) = CulG
   picDATA(2, ixd, iyd) = CulR
End Sub

Public Sub RotateCirc(zPROTATE As Single)
' zPROTATE  degrees  -180 - + 180
Dim ixc As Single, iyc As Single
Dim ixd As Long, iyd As Long
Dim xs As Single, ys As Single
Dim ixs As Long, iys As Long
Dim idx As Single 'Long
Dim zrad As Single
Dim zAng As Single
Dim zcos As Single, zsin As Single
Dim CH As Long, CW As Long
Dim SQ As Single
   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX
   zAng = zPROTATE * d2r#
   zcos = Cos(zAng)
   zsin = Sin(-zAng)
   ixc = LOX + CW / 2
   iyc = LOY + CH / 2
   zrad = CW / 2
   
   
   If CH \ 2 < zrad Then zrad = CH \ 2
   zrad = zrad * zrad
   
   
   If Not aAARotateCirc Then
      
      StartProgress (HIY)
      For iyd = LOY To HIY
         idx = CW / 2
         For ixd = ixc - idx To ixc + idx - 1
            If (ixd >= LOX) And (ixd <= HIX) Then
               xs = ixc + CSng(ixd - ixc) * zcos + CSng(iyd - iyc) * zsin
               ys = iyc + CSng(iyd - iyc) * zcos - CSng(ixd - ixc) * zsin
               ixs = CLng(xs)
               iys = CLng(ys)
               If (ixs >= LOX And ixs <= HIX) And (iys >= LOY And iys <= HIY) Then
               
                  SQ = (ixs - ixc) * (ixs - ixc) + (iys - iyc) * (iys - iyc)
                  If SQ <= zrad Then
                     If aSelectAccepted Then
                        If MaskArray(ixd - LOX, iyd - LOY) = 0 Then RotateCALC ixd, iyd, ixs, iys
                     Else
                        RotateCALC ixd, iyd, ixs, iys
                     End If
                  End If
               
               End If
            End If
         Next ixd
      DrawProgress
      Next iyd
   Else  ' AARotateCirc
   
      StartProgress (HIY)
      For iyd = LOY To HIY
         idx = CW / 2
         For ixd = ixc - idx To ixc + idx - 1
            If (ixd >= LOX) And (ixd <= HIX) Then
               xs = ixc + CSng(ixd - ixc) * zcos + CSng(iyd - iyc) * zsin
               ys = iyc + CSng(iyd - iyc) * zcos - CSng(ixd - ixc) * zsin
               If xs = Int(xs) Then xs = xs + 0.001
               If ys = Int(ys) Then ys = ys + 0.001
               ixs = Int(xs)
               iys = Int(ys)
               If (ixs >= LOX And ixs < HIX) And (iys >= LOY And iys < HIY) Then
                  
                  SQ = (ixs - ixc) * (ixs - ixc) + (iys - iyc) * (iys - iyc)
                  If SQ <= zrad Then
                     
                     If aSelectAccepted Then
                        iyy = iyd - LOY
                        ixx = ixd - LOX
                        If MaskArray(ixx, iyy) = 0 Then
                           AARotateCALC xs, ys, ixs, iys, ixd, iyd
                        End If
                     Else
                        AARotateCALC xs, ys, ixs, iys, ixd, iyd
                     End If
                  End If
               
               End If
            
            End If
         Next ixd
      DrawProgress
      Next iyd
   
   End If

Else     ' aSelectAccepted and External

   CH = FullH - 1
   CW = FullW - 1
   zAng = zPROTATE * d2r#
   zcos = Cos(zAng)
   zsin = Sin(-zAng)
   ixc = CW / 2
   iyc = CH / 2
   zrad = CW / 2
   If CH \ 2 < zrad Then zrad = CH \ 2
   zrad = zrad * zrad

   If Not aAARotateCirc Then  ' & aSelectAccepted and External
      
      StartProgress (HIY)
      For iyd = 0 To FullH - 1
         idx = CW / 2
         For ixd = ixc - idx To ixc + idx - 1
            If (ixd >= 0) And (ixd <= FullW - 1) Then
               xs = ixc + CSng(ixd - ixc) * zcos + CSng(iyd - iyc) * zsin
               ys = iyc + CSng(iyd - iyc) * zcos - CSng(ixd - ixc) * zsin
               ixs = CLng(xs)
               iys = CLng(ys)
               If (ixs >= 0 And ixs <= FullW - 1) And (iys >= 0 And iys <= FullH - 1) Then
               
                  SQ = (ixs - ixc) * (ixs - ixc) + (iys - iyc) * (iys - iyc)
                  If SQ <= zrad Then
                        If (ixd >= LOX And ixd <= HIX) And (iyd >= LOY And iyd <= HIY) Then
                           If MaskArray(ixd - LOX, iyd - LOY) <> 0 Then RotateCALC ixd, iyd, ixs, iys
                        Else
                           RotateCALC ixd, iyd, ixs, iys
                        End If
                  End If
               
               
               End If
            End If
         Next ixd
      DrawProgress
      Next iyd
      
   Else  ' AARotateCirc  & aSelectAccepted and External
   
      StartProgress (HIY)
      For iyd = 0 To FullH - 1
         idx = CW / 2
         For ixd = ixc - idx To ixc + idx - 1
            If (ixd >= 0) And (ixd <= FullW - 1) Then
               xs = ixc + CSng(ixd - ixc) * zcos + CSng(iyd - iyc) * zsin
               ys = iyc + CSng(iyd - iyc) * zcos - CSng(ixd - ixc) * zsin
               If xs = Int(xs) Then xs = xs + 0.001
               If ys = Int(ys) Then ys = ys + 0.001
               ixs = Int(xs)
               iys = Int(ys)
               If (ixs >= 0 And ixs < FullW - 2) And (iys >= 0 And iys < FullH - 2) Then
                  
                  SQ = (ixs - ixc) * (ixs - ixc) + (iys - iyc) * (iys - iyc)
                  
                  If SQ <= zrad Then
                     If (ixd >= LOX) And (ixd < HIX) And (iyd >= LOY) And (iyd < HIY) Then
                        If MaskArray(ixd - LOX, iyd - LOY) <> 0 Then AARotateCALC xs, ys, ixs, iys, ixd, iyd
                     Else
                        AARotateCALC xs, ys, ixs, iys, ixd, iyd
                     End If
                  End If
               
               End If
            End If
         Next ixd
      DrawProgress
      Next iyd
   
   End If   ' END  AA RotateCirc & aSelectAccepted and External
   
End If

   frmDeformers.picPB.Cls
End Sub

Public Sub RoundRect(zPROUNDRECT As Single)
' zPROUNDRECT  0 -> .5
Dim zA As Single
Dim zB As Single
Dim ixd As Long
Dim i As Long
Dim CH As Long, CW As Long
   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX
   
   zA = zPROUNDRECT * CW ' Corner radius
   If zA > CH \ 2 Then zA = CH \ 2
   
   StartProgress (HIY)
   For iy = LOY To HIY
      If iy <= zA + (LOY - 1) Then
         i = zA - Sqr(zA * zA - (zA - (iy - LOY + 1)) * (zA - (iy - LOY + 1)))
      ElseIf iy >= HIY - zA Then
         i = zA - Sqr(zA * zA - (zA - (HIY - iy)) * (zA - (HIY - iy))) '- 1
      Else
         i = 0
      End If
      zB = (CW - 2 * i) / (CW)
      For ix = LOX To HIX
         ixd = zB * (ix - LOX + 1) + i + LOX
         If (ixd > LOX) And (ixd <= HIX) Then
            If aSelectAccepted Then
               If MaskArray(ixd - LOX, iy - LOY) = 0 Then RoundRectCALC ixd
            Else
               RoundRectCALC ixd
            End If
         End If
      Next ix
   DrawProgress
   Next iy

Else     ' aSelectAccepted and External

   CH = FullH - 1
   CW = FullW - 1
   
   zA = zPROUNDRECT * CW ' Corner radius
   If zA > CH \ 2 Then zA = CH \ 2
   
   StartProgress (HIY)
   For iy = 0 To FullH - 1
      If iy <= zA + (0 - 1) Then
         i = zA - Sqr(zA * zA - (zA - (iy - 0 + 1)) * (zA - (iy - 0 + 1)))
      ElseIf iy >= FullH - 1 - zA Then
         i = zA - Sqr(zA * zA - (zA - (FullH - 1 - iy)) * (zA - (FullH - 1 - iy))) '- 1
      Else
         i = 0
      End If
      zB = (CW - 2 * i) / (CW)
      For ix = 0 To FullW - 1
         ixd = zB * (ix - 0 + 1) + i + 0
         If ixd > 0 And ixd <= FullW - 1 Then
            If (ixd > LOX) And (ixd <= HIX) And (iy >= LOY) And (iy <= HIY) Then
                  If MaskArray(ixd - LOX, iy - LOY) <> 0 Then RoundRectCALC ixd
            Else
               RoundRectCALC ixd
            End If
         End If
      Next ix
   DrawProgress
   Next iy

End If

   frmDeformers.picPB.Cls
End Sub
Private Sub RoundRectCALC(ixd As Long)
   picDATA(0, ixd, iy) = picDATAORG(0, ix, iy)
   picDATA(1, ixd, iy) = picDATAORG(1, ix, iy)
   picDATA(2, ixd, iy) = picDATAORG(2, ix, iy)
End Sub


Public Sub AStars(PKALI As Long)
' PKALI 1 -> 21
Dim zA As Single
Dim zR As Single
Dim zRR As Single
Dim xc As Single
Dim yc As Single
Dim xd As Single
Dim yd As Single
Dim ixv As Long
Dim iyv As Long
Dim zRMax As Single

Dim CH As Long, CW As Long
   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
   If aSelectAccepted Then
      If Not aExternal Then   ' Internal
         CH = HIY - LOY
         CW = HIX - LOX
         xc = LOX + CW / 2: yc = LOY + CH / 2
      Else 'External
         CH = FullH - 1
         CW = FullW - 1
         xc = CW / 2: yc = CH / 2
      End If
   Else
      CH = FullH - 1
      CW = FullW - 1
      xc = CW / 2: yc = CH / 2
   End If
   zRMax = Sqr((CW - xc) ^ 2 + (CH - yc) ^ 2)
   
   
   StartProgress (HIY)
   For iy = 0 To FullH - 1
   For ix = 0 To FullW - 1
      xd = ix - xc
      yd = iy - yc
      zA = zATan2(yd, xd) * PKALI * 0.1
      zR = Sqr(yd * yd + xd * xd)
      zRR = Sqr(zR * zRMax)
      ixv = xc + zRR * Cos(zA)
      iyv = yc + zRR * Sin(zA)
      If (ixv >= 0 And ixv <= FullW - 1) And (iyv >= 0 And iyv <= FullH - 1) Then
         
         If aSelectAccepted Then
            If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
               If Not aExternal Then   ' Internal
                  If MaskArray(ix - LOX, iy - LOY) = 0 Then AStarsCALC ixv, iyv
               Else  ' External
                  If MaskArray(ix - LOX, iy - LOY) <> 0 Then AStarsCALC ixv, iyv
               End If
            Else
               If aExternal Then AStarsCALC ixv, iyv
            End If
         Else
            AStarsCALC ixv, iyv
         End If
      
      End If
   Next ix
   DrawProgress
   Next iy

   frmDeformers.picPB.Cls
End Sub
Private Sub AStarsCALC(ixv As Long, iyv As Long)
   picDATA(0, ix, iy) = picDATAORG(0, ixv, iyv)
   picDATA(1, ix, iy) = picDATAORG(1, ixv, iyv)
   picDATA(2, ix, iy) = picDATAORG(2, ixv, iyv)
End Sub

Public Sub Swirl(zPSWIRL As Single)
' zPSWIRL  -50 -> +50
Dim zA As Single
Dim zR As Single
Dim zD As Single
Dim xc As Single
Dim yc As Single
Dim xd As Single
Dim yd As Single
Dim ixv As Long
Dim iyv As Long
Dim CH As Long, CW As Long
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then

   CH = HIY - LOY
   CW = HIX - LOX
   zD = Sqr(CW * CW + CH * CH)
   xc = LOX - 1 + CW / 2
   yc = LOY - 1 + CH / 2
   
   
   StartProgress (HIY)
   For iy = LOY To HIY
   For ix = LOX To HIX
      xd = (ix - xc)
      yd = (iy - yc)
      zA = zATan2(yd, xd)
      zR = Sqr(yd * yd + xd * xd)
      zA = zA + (zR / zD) * zPSWIRL
      ixv = xc + zR * Cos(zA)
      iyv = yc + Sgn(zPSWIRL) * zR * Sin(zA)
      If (ixv >= LOX) And (ixv <= HIX) And (iyv >= LOY) And (iyv <= HIY) Then
         If aSelectAccepted Then
            ixx = ix - LOX
            iyy = iy - LOY
            If MaskArray(ixx, iyy) = 0 Then SwirlCALC ixv, iyv
         Else
             SwirlCALC ixv, iyv
         End If
      End If
   Next ix
   DrawProgress
   Next iy

Else     ' aSelectAccepted and External

   CH = FullH - 1
   CW = FullW - 1
   zD = Sqr(CW * CW + CH * CH)
   xc = CW / 2
   yc = CH / 2
   
   
   StartProgress (HIY)
   For iy = 0 To FullH - 1
   For ix = 0 To FullW - 1
      xd = (ix - xc)
      yd = (iy - yc)
      zA = zATan2(yd, xd)
      zR = Sqr(yd * yd + xd * xd)
      zA = zA + (zR / zD) * zPSWIRL
      ixv = xc + zR * Cos(zA)
      iyv = yc + Sgn(zPSWIRL) * zR * Sin(zA)
      If (ixv >= 0) And (ixv <= FullW - 1) And (iyv >= 0) And (iyv <= FullH - 1) Then
      
         If (ix >= LOX) And (ix <= HIX) And (iy >= LOY) And (iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then SwirlCALC ixv, iyv
         Else
             SwirlCALC ixv, iyv
         End If
      
      End If
   Next ix
   DrawProgress
   Next iy
End If

   frmDeformers.picPB.Cls
End Sub
Private Sub SwirlCALC(ixv As Long, iyv As Long)
   picDATA(0, ix, iy) = picDATAORG(0, ixv, iyv)
   picDATA(1, ix, iy) = picDATAORG(1, ixv, iyv)
   picDATA(2, ix, iy) = picDATAORG(2, ixv, iyv)
End Sub

Public Sub Stephanes(PAR As Long)
' PAR 1 - 11
' All Sorts frmDeformers.LabName
' Invent your own
   With frmDeformers
      Select Case PAR
      Case 1: .LabName = "Aquarel"
      Case 2: .LabName = "Charcoal"
      Case 3: .LabName = "Dark Moon"
      Case 4: .LabName = "Flame"
      Case 5: .LabName = "Liquid"
      Case 6: .LabName = "Neon"
      Case 7: .LabName = "Purple Rain"
      Case 8: .LabName = "Spooky"
      Case 9: .LabName = "Total Eclipse"
      Case 10: .LabName = "Unreal"
      Case 11: .LabName = "Yellow"
      End Select
   End With
   
   StartProgress HIY
   For iy = 0 To FullH - 1
   For ix = 0 To FullW - 1
      If aSelectAccepted Then ' Selection ON
         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If Not aExternal Then   ' Internal
               If MaskArray(ix - LOX, iy - LOY) = 0 Then StephanesCALC PAR
            Else  'External in Mask area
               If MaskArray(ix - LOX, iy - LOY) <> 0 Then StephanesCALC PAR, 1
            End If
         Else  ' External outside Mask area 0 to FullW-1 & FullH-1
            If aExternal Then StephanesCALC PAR, 2
         End If
      Else  ' Selection OFF
         StephanesCALC PAR
      End If
   Next ix
   DrawProgress
   Next iy

   frmDeformers.picPB.Cls
End Sub

Private Sub StephanesCALC(PAR As Long, Optional Typ As Long = 0)
Dim B As Long, g As Long, r As Long
Dim LCul As Long
      B = picDATAORG(0, ix, iy)
      g = picDATAORG(1, ix, iy)
      r = picDATAORG(2, ix, iy)
      
      Select Case PAR
      Case 1   ' "Aquarel"
         If r < 128 And g < 128 And B < 128 Then
            r = 2 * r: g = 2 * g: B = 2 * B
         Else
            r = r \ 2: g = g \ 2: B = B \ 2
         End If
      Case 2   ' "Charcoal"
         r = Abs(r * g - B + g + r) \ 256
         g = Abs(r * B - g + B + r) \ 256
         B = Abs(g * B - g + B + r) \ 256
         LCul = RGB(r, g, B)
         r = Abs(LCul Mod 256)
         g = Abs((LCul \ 256) Mod 256)
         B = Abs(LCul \ 256 \ 256)
         r = (r + g + B) \ 3
      Case 3   ' "Dark Moon"
         r = Abs(r - 64)
         g = Abs(r - 64)
         B = Abs(r - 64)
      Case 4   ' "Flame"
         LCul = (r + g + B) \ 3
         If r > B Then
            r = Abs(r + LCul)
            B = Abs(B - LCul)
            If r > 255 Then r = 255
            If B > 255 Then B = 255
         End If
      Case 5   ' "Liquid"
         r = ((g - B) * (g - B)) \ 125
         g = ((r - B) * (r - B)) \ 125
         B = ((r - g) * (r - g)) \ 125
         If r > 255 Then r = 255
         If g > 255 Then g = 255
         If B > 255 Then B = 255
      Case 6   ' "Neon"
      If aSelectAccepted Then
         
         If Typ <> 0 Then  ' External
         
            If (ix > 0 And ix < FullW - 1 - 1) Then
               B = Abs(picDATAORG(0, ix - 1, iy) + B - picDATAORG(0, ix + 1, iy) - picDATAORG(0, ix + 2, iy) + 32)
               g = Abs(picDATAORG(1, ix - 1, iy) + g - picDATAORG(1, ix + 1, iy) - picDATAORG(2, ix + 2, iy) + 32)
               r = Abs(picDATAORG(2, ix - 1, iy) + r - picDATAORG(2, ix + 1, iy) - picDATAORG(2, ix + 2, iy) + 32)
               If r > 255 Then r = 255
               If g > 255 Then g = 255
               If B > 255 Then B = 255
            End If
         
         Else  ' Internal
            If (ix > LOX And ix < HIX - 1) And (iy >= LOY And iy <= HIY) Then
               B = Abs(picDATAORG(0, ix - 1, iy) + B - picDATAORG(0, ix + 1, iy) - picDATAORG(0, ix + 2, iy) + 32)
               g = Abs(picDATAORG(1, ix - 1, iy) + g - picDATAORG(1, ix + 1, iy) - picDATAORG(2, ix + 2, iy) + 32)
               r = Abs(picDATAORG(2, ix - 1, iy) + r - picDATAORG(2, ix + 1, iy) - picDATAORG(2, ix + 2, iy) + 32)
               If r > 255 Then r = 255
               If g > 255 Then g = 255
               If B > 255 Then B = 255
            End If
         End If
      
      Else  ' No Selection
            If (ix > 0 And ix < FullW - 1 - 1) Then
               B = Abs(picDATAORG(0, ix - 1, iy) + B - picDATAORG(0, ix + 1, iy) - picDATAORG(0, ix + 2, iy) + 32)
               g = Abs(picDATAORG(1, ix - 1, iy) + g - picDATAORG(1, ix + 1, iy) - picDATAORG(2, ix + 2, iy) + 32)
               r = Abs(picDATAORG(2, ix - 1, iy) + r - picDATAORG(2, ix + 1, iy) - picDATAORG(2, ix + 2, iy) + 32)
               If r > 255 Then r = 255
               If g > 255 Then g = 255
               If B > 255 Then B = 255
            End If
      End If
         
      Case 7   ' "Purple Rain"
         r = Abs(g + r \ 2)
         g = Abs(B + g \ 2)
         B = Abs(r + B \ 2)
         If r > 255 Then r = 255
         If g > 255 Then g = 255
         If B > 255 Then B = 255
      Case 8   ' "Spooky"
         g = Abs(r + g \ 2)
         B = Abs(r + B \ 2)
         If g > 255 Then g = 255
         If B > 255 Then B = 255
      Case 9   ' "Total Eclipse"
         r = Abs(g - 64)
         g = Abs(g - 64)
         B = Abs(g - 64)
      Case 10  ' "Unreal"
         If g = 0 Or B = 0 Then
            g = 1
            B = 1
         End If
         r = Abs(Sin(Atn(g / B)) * 125 + 20)
         g = Abs(Sin(Atn(r / B)) * 125 + 20)
         B = Abs(Sin(Atn(r / g)) * 125 + 20)
         If r > 255 Then r = 255
         If g > 255 Then g = 255
         If B > 255 Then B = 255
      Case 11  ' "Yellow"
         B = ((g - r) * (g - r)) \ 125
         r = ((g - B) * (g - B)) \ 125
         g = ((B + r) * (B + r)) \ 125
         If r > 255 Then r = 255
         If g > 255 Then g = 255
         If B > 255 Then B = 255
      End Select
      picDATA(0, ix, iy) = B
      picDATA(2, ix, iy) = r
      picDATA(1, ix, iy) = g
End Sub


Public Sub Tile(PTILE As Long)
' PTILE  2 -> 40
Dim i As Long, j As Long
Dim ii As Long, jj As Long
Dim WW As Long, HH As Long
Dim CH As Long, CW As Long
   
   picDATA() = picDATAORG()
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX

   WW = CW \ PTILE
   HH = CH \ PTILE
   j = LOY
   StartProgress (HIY / PTILE)
   For iy = LOY To HIY Step PTILE
      i = LOX
      For ix = LOX To HIX Step PTILE
         If aSelectAccepted Then
            If MaskArray(i - LOX, j - LOY) = 0 Then
               picDATA(0, i, j) = picDATAORG(0, ix, iy)
               picDATA(1, i, j) = picDATAORG(1, ix, iy)
               picDATA(2, i, j) = picDATAORG(2, ix, iy)
            End If
         Else
               picDATA(0, i, j) = picDATAORG(0, ix, iy)
               picDATA(1, i, j) = picDATAORG(1, ix, iy)
               picDATA(2, i, j) = picDATAORG(2, ix, iy)
         End If
         i = i + 1
      Next ix
      j = j + 1
      DrawProgress
   Next iy
   
   j = LOY
   For jj = LOY To (PTILE + 10) * HH - 1 + LOY - 1
      If jj <= (LOY - 1) + CH Then
         i = LOX
         For ii = LOX To (PTILE + 10) * WW - 1 + LOX - 1
            If ii <= (LOX - 1) + CW Then
               If aSelectAccepted Then
                  If MaskArray(ii - LOX, jj - LOY) = 0 Then
                     picDATA(0, ii, jj) = picDATA(0, i, j)
                     picDATA(1, ii, jj) = picDATA(1, i, j)
                     picDATA(2, ii, jj) = picDATA(2, i, j)
                  End If
               Else
                  picDATA(0, ii, jj) = picDATA(0, i, j)
                  picDATA(1, ii, jj) = picDATA(1, i, j)
                  picDATA(2, ii, jj) = picDATA(2, i, j)
               End If
            End If
            i = i + 1
            If i > (LOX - 1) + WW Then i = LOX
         Next ii
      End If
      j = j + 1
      If j > (LOY - 1) + HH Then j = LOY
   Next jj
   
Else     ' aSelectAccepted and External

   ReDim picData2(0 To 3, 0 To FullW - 1, 0 To FullH - 1)
   
   'If PTILE > 27 Then PTILE = 27
   
   CH = FullH - 1
   CW = FullW - 1
   WW = (CW) \ PTILE - 3
   HH = (CH) \ PTILE - 3
   j = 0
   StartProgress (HIY / PTILE)
   
   ' Resize whole picture and place at bottom left of picDATA2()   (ix=0,iyFullH-1-(PTILE*??))
   For iy = 0 To FullH - 1 Step PTILE
      i = 0
      For ix = 0 To FullW - 1 Step PTILE
      
      If (ix >= 0) And (ix <= FullW - 1) And (iy >= 0) And (iy <= FullH - 1) Then
      If (i >= 0 And i <= FullW - 1) And (j >= 0 And j <= FullH - 1) Then
      
         picData2(0, i, j) = picDATAORG(0, ix, iy)
         picData2(1, i, j) = picDATAORG(1, ix, iy)
         picData2(2, i, j) = picDATAORG(2, ix, iy)
         
      End If
      End If
         i = i + 1
      Next ix
      j = j + 1
      DrawProgress
   Next iy

' Copying the bottom left image to the rest of the picture

   j = 0
   For jj = 0 To (PTILE + 10) * HH - 2

      If jj <= CH - 1 Then
         i = 0
         
         For ii = 0 To (PTILE + 10) * WW - 2

         If (ii >= 0) And (ii <= FullW - 1) And (jj >= 0) And (jj <= FullH - 1) Then

            If ii <= CW - 1 Then

               If (ii >= LOX) And (ii <= HIX) And (jj >= LOY) And (jj <= HIY) Then

                  If MaskArray(ii - LOX, jj - LOY) <> 0 Then
                     picDATA(0, ii, jj) = picData2(0, i, j)
                     picDATA(1, ii, jj) = picData2(1, i, j)
                     picDATA(2, ii, jj) = picData2(2, i, j)
                  End If
               Else
                  picDATA(0, ii, jj) = picData2(0, i, j)
                  picDATA(1, ii, jj) = picData2(1, i, j)
                  picDATA(2, ii, jj) = picData2(2, i, j)
               End If
            End If

         End If
            i = i + 1
            If i > WW Then i = 0
         Next ii
      End If
      j = j + 1
      If j > HH Then j = 0
      'If j > (LOY - 1) + HH Then j = LOY
   Next jj

End If
   Erase picData2()
   frmDeformers.picPB.Cls
End Sub

Public Sub Tunnel(PTUNNEL As Long)
' PTUNNEL 2 - 40
'Dim ziy As Single
'Dim zix As Single
Dim zNN As Single
Dim zp As Single
Dim i As Long, j As Long
Dim WW As Long, HH As Long
Dim CH As Long, CW As Long
   
   picDATA() = picDATAORG()

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then

   CH = HIY - LOY
   CW = HIX - LOX
   zp = 1 + (PTUNNEL - 1) / 5
   
   StartProgress ((zp - 1) / 0.2)
   For zNN = 1 To zp Step 0.2
      WW = CW / zNN
      HH = CH / zNN
      j = LOY - 1 + (CH - HH) / 2 - 1
      For ziy = LOY To HIY Step zNN
         i = LOX - 1 + (CW - WW) / 2 - 1
         For zix = LOX To HIX Step zNN
            If (j >= LOY And j <= HIY) And (i >= LOX And i <= HIX) Then
               
               If aSelectAccepted Then
                  If MaskArray(i - LOX, j - LOY) = 0 Then TunnelCALC i, j, zix, ziy
               Else
                  TunnelCALC i, j, zix, ziy
               End If
            
            End If
            i = i + 1
         Next zix
         j = j + 1
      Next ziy
      DrawProgress
   Next zNN

Else     ' aSelectAccepted and External

   CH = FullH - 1
   CW = FullW - 1
   'zp = PTUNNEL / 5
   zp = 1 + (PTUNNEL - 1) / 5
   
   StartProgress ((zp - 1) / 0.2)
   
   For zNN = 1 To zp Step 0.2
      WW = CW / zNN
      HH = CH / zNN
      j = (CH - HH) / 2 '- 1
      
      For ziy = 0 To FullH - 1 Step zNN
         i = (CW - WW) / 2 '- 1
         
         For zix = 0 To FullW - 1 Step zNN
            If (zix >= 0 And zix <= FullW - 1) And (ziy >= 0 And ziy <= FullH - 1) Then
            If (i >= 0) And (i <= FullW - 1) And (j >= 0 And j <= FullH - 1) Then
                
               If (i >= LOX) And (i <= HIX) And (j >= LOY And j <= HIY) Then
               
                  If MaskArray(i - LOX, j - LOY) <> 0 Then TunnelCALC i, j, zix, ziy
               Else
                  TunnelCALC i, j, zix, ziy
               End If
            
            End If
            End If
            i = i + 1
         Next zix
         j = j + 1
      Next ziy
      DrawProgress
   Next zNN
End If

   frmDeformers.picPB.Cls
End Sub
Private Sub TunnelCALC(i As Long, j As Long, zix As Single, ziy As Single)
   picDATA(0, i, j) = picDATAORG(0, zix, ziy)
   picDATA(1, i, j) = picDATAORG(1, zix, ziy)
   picDATA(2, i, j) = picDATAORG(2, zix, ziy)
End Sub




Public Sub FlutedWindowHorz(PFWINDOW As Long)
' PFWINDOW  8,10,12,, 32
' Horz fluted window
Dim NN As Long
Dim iyv As Long
Dim CH As Long, CW As Long


If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then

   CH = HIY - LOY
   CW = HIX - LOX
   NN = CH \ PFWINDOW
   If NN < 1 Then NN = 1
   StartProgress (HIX)
   For ix = LOX To LOX + CW
   For iy = LOY To LOY + CH
      iyv = iy + (iy Mod NN) - NN \ 2
      If (iyv > LOY) And (iyv <= HIY) Then
         If aSelectAccepted Then
            If MaskArray(ix - LOX, iy - LOY) = 0 Then FlutedWindowHorzCALC iyv
         Else
            FlutedWindowHorzCALC iyv
         End If
      End If
   Next iy
   DrawProgress
   Next ix

Else     ' aSelectAccepted and External
   
   CH = FullH - 1
   CW = FullW - 1
   NN = CH \ PFWINDOW
   If NN < 1 Then NN = 1
   
   StartProgress HIX
   For iy = 0 To CH
   For ix = 0 To CW
      
      iyv = iy + (iy Mod NN) - NN \ 2
      
      If (iyv >= 0 And iyv <= FullH - 1) Then

         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then FlutedWindowHorzCALC iyv
         Else
            FlutedWindowHorzCALC iyv
         End If
            
      End If
   Next ix
   DrawProgress
   Next iy

End If

   frmDeformers.picPB.Cls
End Sub
Private Sub FlutedWindowHorzCALC(iyv As Long)
   picDATA(0, ix, iy) = picDATAORG(0, ix, iyv)
   picDATA(1, ix, iy) = picDATAORG(1, ix, iyv)
   picDATA(2, ix, iy) = picDATAORG(2, ix, iyv)
End Sub

Public Sub FlutedWindowVert(PFWINDOW As Long)
' PFWINDOW  8,10,12,, 32
' Vert fluted window
Dim NN As Long
Dim ixv As Long
Dim CH As Long, CW As Long
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX
   NN = CW \ PFWINDOW
   If NN < 1 Then NN = 1
   StartProgress (HIY)
   For iy = LOY To LOY + CH
   For ix = LOX To LOX + CW
      ixv = ix + (ix Mod NN) - NN \ 2
      If (ixv > LOX) And (ixv <= HIX) Then
         If aSelectAccepted Then
            If MaskArray(ix - LOX, iy - LOY) = 0 Then FlutedWindowVertCALC ixv
         Else
            FlutedWindowVertCALC ixv
         End If
      End If
   Next ix
   DrawProgress
   Next iy
   
Else     ' aSelectAccepted and External

   CH = FullH - 1
   CW = FullW - 1
   NN = CW \ PFWINDOW
   If NN < 1 Then NN = 1
   
   StartProgress HIY
   For iy = 0 To CH
   For ix = 0 To CW
      
      ixv = ix + (ix Mod NN) - NN \ 2
      
      If (ixv >= 0 And ixv <= FullW - 1) Then

         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then FlutedWindowVertCALC ixv
         Else
            FlutedWindowVertCALC ixv
         End If
            
      End If
   Next ix
   DrawProgress
   Next iy

End If

frmDeformers.picPB.Cls
End Sub
Private Sub FlutedWindowVertCALC(ixv As Long)
   picDATA(0, ix, iy) = picDATAORG(0, ixv, iy)
   picDATA(1, ix, iy) = picDATAORG(1, ixv, iy)
   picDATA(2, ix, iy) = picDATAORG(2, ixv, iy)
End Sub

Public Sub FlutedWindowHV(PFWINDOW As Long)
' PFWINDOW  8,10,12,, 32
' Horz & Vert fluted window
Dim NN As Long
Dim iyv As Long, ixv As Long
Dim CH As Long, CW As Long
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX
   NN = CH \ PFWINDOW
   If NN < 1 Then NN = 1
   
   StartProgress HIX
   For ix = LOX To LOX + CW
   For iy = LOY To LOY + CH
      iyv = iy + (iy Mod NN) - NN \ 2
      ixv = ix + (ix Mod NN) - NN \ 2
      If (iyv >= LOY And iyv <= HIY) And (ixv >= LOX And ixv <= HIX) Then
         If aSelectAccepted Then
            If MaskArray(ix - LOX, iy - LOY) = 0 Then FlutedWindowHVCALC ixv, iyv
         Else
            FlutedWindowHVCALC ixv, iyv
         End If
      End If
   Next iy
   DrawProgress
   Next ix

Else     ' aSelectAccepted and External
   
   CH = FullH - 1
   CW = FullW - 1
   NN = CH \ PFWINDOW
   If NN < 1 Then NN = 1
   
   StartProgress HIX
   For ix = 0 To CW
   For iy = 0 To CH
      iyv = iy + (iy Mod NN) - NN \ 2
      ixv = ix + (ix Mod NN) - NN \ 2

      If (ixv >= 0 And ixv <= FullW - 1) And (iyv >= 0 And iyv <= FullH - 1) Then
         
         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then FlutedWindowHVCALC ixv, iyv
         Else
            FlutedWindowHVCALC ixv, iyv
         End If
      
      End If
   Next iy
   DrawProgress
   Next ix

End If

   frmDeformers.picPB.Cls
End Sub
Private Sub FlutedWindowHVCALC(ixv As Long, iyv As Long)
   picDATA(0, ix, iy) = picDATAORG(0, ixv, iyv)
   picDATA(1, ix, iy) = picDATAORG(1, ixv, iyv)
   picDATA(2, ix, iy) = picDATAORG(2, ixv, iyv)
End Sub

Public Sub FlutedWindowCirc(PFWINDOW As Long)
' PFWINDOW  8,10,12,, 32
' Horz & Vert fluted window
Dim NN As Long
Dim iyv As Single, ixv As Single
Dim CH As Long, CW As Long
Dim Rad As Single
Dim TAlpha As Single
Dim xc As Single, yc As Single
   
   picDATA() = picDATAORG()
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CH = HIY - LOY
   CW = HIX - LOX
   xc = LOX + CW \ 2
   yc = LOY + CH \ 2
   
   NN = CW \ 2
   If CW > CH Then NN = CH \ 2
   StartProgress (HIX)
   For ix = LOX To LOX + CW
   For iy = LOY To LOY + CH
      TAlpha = zATan2(iy - yc, ix - xc)
      Rad = Sqr((ix - xc) ^ 2 + (iy - yc) ^ 2)
      Rad = Rad + (PFWINDOW * Sin(2 * pi# * Rad * PFWINDOW / NN - 1))
      ixv = xc + Rad * Cos(TAlpha)
      iyv = yc + Rad * Sin(TAlpha)
      If (ixv >= LOX And ixv <= HIX) And (iyv >= LOY And iyv <= HIY) Then
         If aSelectAccepted Then
            If MaskArray(ix - LOX, iy - LOY) = 0 Then FlutedWindowCircCALC ixv, iyv
         Else
            FlutedWindowCircCALC ixv, iyv
         End If
      End If
   Next iy
   DrawProgress
   Next ix
   
Else    ' aSelectAccepted and External
   CH = FullH - 1
   CW = FullW - 1
   xc = CW \ 2
   yc = CH \ 2
   NN = CW \ 2
   If CW > CH Then NN = CH \ 2
   StartProgress (HIX)
   For ix = 0 To FullW - 1 + CW
   For iy = 0 To FullH - 1 + CH
      TAlpha = zATan2(iy - yc, ix - xc)
      Rad = Sqr((ix - xc) ^ 2 + (iy - yc) ^ 2)
      Rad = Rad + (PFWINDOW * Sin(2 * pi# * Rad * PFWINDOW / NN - 1))
      ixv = xc + Rad * Cos(TAlpha)
      iyv = yc + Rad * Sin(TAlpha)
      If (ix >= 0 And ix <= FullW - 1) And (iy >= 0 And iy <= FullH - 1) Then
      If (ixv >= 0 And ixv <= FullW - 1) And (iyv >= 0 And iyv <= FullH - 1) Then

         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then
               FlutedWindowCircCALC ixv, iyv
            End If
         Else
            FlutedWindowCircCALC ixv, iyv
         End If
            
      End If
      End If
   Next iy
   DrawProgress
   Next ix

End If
   frmDeformers.picPB.Cls
End Sub
Private Sub FlutedWindowCircCALC(ixv As Single, iyv As Single)
   picDATA(0, ix, iy) = picDATAORG(0, ixv, iyv)
   picDATA(1, ix, iy) = picDATAORG(1, ixv, iyv)
   picDATA(2, ix, iy) = picDATAORG(2, ixv, iyv)
End Sub

Public Sub Mirrors(Index As Integer)
' Index = 0,1,2,3  - > L,R,T,B
   picDATA() = picDATAORG()
   Select Case Index
   Case 0 ' Left
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
      StartProgress (HIY)
      For iy = LOY To HIY
      For ix = LOX To (LOX + HIX) \ 2 ' LOX/2+HIX/2 =(LOX+HIX)/2
         If aSelectAccepted Then
            If MaskArray(HIX - ix, iy - LOY) = 0 Then MirrorsLRCALC
         Else
            MirrorsLRCALC
         End If
      Next ix
      DrawProgress
      Next iy
Else  ' External Left
      StartProgress (HIY)
      For iy = 0 To FullH - 1
      For ix = 0 To (FullW - 1) \ 2 ' LOX/2+HIX/2 =(LOX+HIX)/2
            If ((FullW - 1 - ix) >= LOX And (FullW - 1 - ix) <= HIX) And (iy >= LOY And iy <= HIY) Then
               If MaskArray(FullW - 1 - ix - LOX, iy - LOY) <> 0 Then MirrorsLRCALC2
            Else
               MirrorsLRCALC2
            End If
      Next ix
      DrawProgress
      Next iy
End If
   
   
   Case 1 ' Right
      
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
      
      StartProgress (HIY)
      For iy = LOY To HIY
      For ix = HIX To (LOX + HIX) \ 2 Step -1
         If aSelectAccepted Then
            If MaskArray(HIX - ix, iy - LOY) = 0 Then MirrorsLRCALC
         Else
            MirrorsLRCALC
         End If
      Next ix
      DrawProgress
      Next iy
Else  ' External Right
   
      StartProgress (HIY)
      For iy = 0 To FullH - 1
      For ix = FullW - 1 To (FullW - 1) \ 2 Step -1
            
         If ((FullW - 1 - ix) >= LOX And (FullW - 1 - ix) <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(FullW - 1 - ix - LOX, iy - LOY) <> 0 Then MirrorsLRCALC2
         Else
            MirrorsLRCALC2
         End If
      Next ix
      DrawProgress
      Next iy
   
End If

   Case 2 ' Top
      
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
      
      StartProgress (HIX)
      For ix = LOX To HIX
      For iy = HIY To (HIY + LOY) \ 2 Step -1
         If aSelectAccepted Then
            If MaskArray(ix - LOX, HIY - iy) = 0 Then MirrorsTBCALC
         Else
            MirrorsTBCALC
         End If
      Next iy
      DrawProgress
      Next ix
Else  ' External Top
   
      StartProgress (HIX)
      For ix = 0 To FullW - 1
      For iy = FullH - 1 To (FullH - 1) \ 2 Step -1
         If (ix >= LOX And ix <= HIX) And ((FullH - 1 - iy) >= LOY And (FullH - 1 - iy) <= HIY) Then
            If MaskArray(ix - LOX, FullH - 1 - iy - LOY) <> 0 Then MirrorsTBCALC2
         Else
            MirrorsTBCALC2
         End If
      Next iy
      DrawProgress
      Next ix
   
End If

   Case 3 ' Bottom
      
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
      
      StartProgress (HIX)
      For ix = LOX To HIX
      For iy = LOY To (HIY + LOY) \ 2
         If aSelectAccepted Then
            If MaskArray(ix - LOX, HIY - iy) = 0 Then MirrorsTBCALC
         Else
            MirrorsTBCALC
         End If
      Next iy
      DrawProgress
      Next ix
   
Else  ' External Bottom

      StartProgress (HIX)
      For ix = 0 To FullW - 1
      For iy = 0 To (FullH - 1) \ 2
         If (ix >= LOX And ix <= HIX) And ((FullH - 1 - iy) >= LOY And (FullH - 1 - iy) <= HIY) Then
            If MaskArray(ix - LOX, FullH - 1 - iy - LOY) <> 0 Then MirrorsTBCALC2
         Else
            MirrorsTBCALC2
         End If
      Next iy
      DrawProgress
      Next ix
End If
   
   End Select
   
   
   frmDeformers.picPB.Cls
End Sub
Private Sub MirrorsLRCALC()
   picDATA(0, HIX + LOX - ix, iy) = picDATAORG(0, ix, iy)
   picDATA(1, HIX + LOX - ix, iy) = picDATAORG(1, ix, iy)
   picDATA(2, HIX + LOX - ix, iy) = picDATAORG(2, ix, iy)
End Sub
Private Sub MirrorsLRCALC2()
   picDATA(0, FullW - 1 - ix, iy) = picDATAORG(0, ix, iy)
   picDATA(1, FullW - 1 - ix, iy) = picDATAORG(1, ix, iy)
   picDATA(2, FullW - 1 - ix, iy) = picDATAORG(2, ix, iy)
End Sub
Private Sub MirrorsTBCALC()
   picDATA(0, ix, HIY + LOY - iy) = picDATAORG(0, ix, iy)
   picDATA(1, ix, HIY + LOY - iy) = picDATAORG(1, ix, iy)
   picDATA(2, ix, HIY + LOY - iy) = picDATAORG(2, ix, iy)
End Sub
Private Sub MirrorsTBCALC2()
   picDATA(0, ix, FullH - 1 - iy) = picDATAORG(0, ix, iy)
   picDATA(1, ix, FullH - 1 - iy) = picDATAORG(1, ix, iy)
   picDATA(2, ix, FullH - 1 - iy) = picDATAORG(2, ix, iy)
End Sub

Public Sub Flips(Index As Integer)
' Index = 0,1 - > Flip Horz, Vert
   picDATA() = picDATAORG()
   
   
   Select Case Index
   Case 0 ' Flip Horz
      
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
      
      StartProgress (HIY)
      For iy = LOY To HIY
      For ix = LOX To HIX
         If aSelectAccepted Then
            If MaskArray(HIX - ix, iy - LOY) = 0 Then FlipHCALC1
         Else  ' LOX = 0
            FlipHCALC1
         End If
      Next ix
      DrawProgress
      Next iy
Else  ' External Flip Horz

      StartProgress (HIY)
      For iy = 0 To FullH - 1
      For ix = 0 To FullW - 1
      
         If ((FullW - 1 - ix) >= LOX And (FullW - 1 - ix) <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(FullW - 1 - ix - LOX, iy - LOY) <> 0 Then FlipHCALC2
         Else
            FlipHCALC2
         End If
      Next ix
      DrawProgress
      Next iy

End If
   
   Case 1 ' Flip Vert

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then

      StartProgress (HIX)
      For ix = LOX To HIX
      For iy = LOY To HIY
         If aSelectAccepted Then
            If MaskArray(ix - LOX, HIY - iy) = 0 Then FlipVCALC1
         Else
            FlipVCALC1
         End If
      Next iy
      DrawProgress
      Next ix
   
Else  ' External Flip Vert
      StartProgress (HIX)
      For ix = 0 To FullW - 1
      For iy = 0 To FullH - 1
         If (ix >= LOX And ix <= HIX) And ((FullH - 1 - iy) >= LOY And (FullH - 1 - iy) <= HIY) Then
            If MaskArray(ix - LOX, FullH - 1 - iy - LOY) <> 0 Then FlipVCALC2
         Else
            FlipVCALC2
         End If
      Next iy
      DrawProgress
      Next ix
   
End If
   
   End Select
   
   frmDeformers.picPB.Cls
End Sub
Private Sub FlipHCALC1()
   picDATA(0, HIX + LOX - ix, iy) = picDATAORG(0, ix, iy)
   picDATA(1, HIX + LOX - ix, iy) = picDATAORG(1, ix, iy)
   picDATA(2, HIX + LOX - ix, iy) = picDATAORG(2, ix, iy)
End Sub
Private Sub FlipHCALC2()
   picDATA(0, FullW - 1 - ix, iy) = picDATAORG(0, ix, iy)
   picDATA(1, FullW - 1 - ix, iy) = picDATAORG(1, ix, iy)
   picDATA(2, FullW - 1 - ix, iy) = picDATAORG(2, ix, iy)
End Sub
Private Sub FlipVCALC1()
   picDATA(0, ix, HIY + LOY - iy) = picDATAORG(0, ix, iy)
   picDATA(1, ix, HIY + LOY - iy) = picDATAORG(1, ix, iy)
   picDATA(2, ix, HIY + LOY - iy) = picDATAORG(2, ix, iy)
End Sub
Private Sub FlipVCALC2()
   picDATA(0, ix, FullH - 1 - iy) = picDATAORG(0, ix, iy)
   picDATA(1, ix, FullH - 1 - iy) = picDATAORG(1, ix, iy)
   picDATA(2, ix, FullH - 1 - iy) = picDATAORG(2, ix, iy)
End Sub


Public Sub Reflectors(Index As Integer)
'  Selection ON
' Index = 0,1,2,3  - > To Left,Above,Below,To Right
' These must stay in bounds 0->FullW-1 & 0->FullH-1
Dim CH As Long, CW As Long
Dim ixLR As Long, iyAB As Long

   CH = HIY - LOY
   CW = HIX - LOX
   picDATA() = picDATAORG()
   Select Case Index
   Case 0 ' To Left
      StartProgress (HIY)
      For iy = LOY To HIY
      For ix = LOX To HIX
         'ixLR = LOX - (ix - LOX) - 1
         ixLR = 2 * LOX - ix - 1
         If ixLR >= 0 Then
            If aSelectAccepted Then
               ixx = ix - LOX
               iyy = iy - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, ixLR, iy) = picDATAORG(0, ix, iy)
                  picDATA(1, ixLR, iy) = picDATAORG(1, ix, iy)
                  picDATA(2, ixLR, iy) = picDATAORG(2, ix, iy)
               End If
            Else
               picDATA(0, ixLR, iy) = picDATAORG(0, ix, iy)
               picDATA(1, ixLR, iy) = picDATAORG(1, ix, iy)
               picDATA(2, ixLR, iy) = picDATAORG(2, ix, iy)
            End If
         End If
      Next ix
      DrawProgress
      Next iy
   
   Case 1 ' Above
      StartProgress (HIX)
      For ix = LOX To HIX
      For iy = HIY To LOY Step -1
         'iyAB = HIY - (iy - HIY) + 1
         iyAB = 2 * HIY - iy + 1
         If iyAB <= FullH - 1 Then
            If aSelectAccepted Then
               ixx = ix - LOX
               iyy = iy - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, ix, iyAB) = picDATAORG(0, ix, iy)
                  picDATA(1, ix, iyAB) = picDATAORG(1, ix, iy)
                  picDATA(2, ix, iyAB) = picDATAORG(2, ix, iy)
               End If
            Else
               picDATA(0, ix, iyAB) = picDATAORG(0, ix, iy)
               picDATA(1, ix, iyAB) = picDATAORG(1, ix, iy)
               picDATA(2, ix, iyAB) = picDATAORG(2, ix, iy)
            End If
         End If
      Next iy
      DrawProgress
      Next ix
   
   Case 2 ' Below
      StartProgress (HIX)
      For ix = LOX To HIX
      For iy = LOY To HIY
         'iyAB = LOY - (iy - LOY) - 1
         iyAB = 2 * LOY - iy - 1
         If iyAB >= 0 Then
            If aSelectAccepted Then
               ixx = ix - LOX
               iyy = iy - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, ix, iyAB) = picDATAORG(0, ix, iy)
                  picDATA(1, ix, iyAB) = picDATAORG(1, ix, iy)
                  picDATA(2, ix, iyAB) = picDATAORG(2, ix, iy)
               End If
            Else
               picDATA(0, ix, iyAB) = picDATAORG(0, ix, iy)
               picDATA(1, ix, iyAB) = picDATAORG(1, ix, iy)
               picDATA(2, ix, iyAB) = picDATAORG(2, ix, iy)
            End If
         End If
      Next iy
      DrawProgress
      Next ix
   
   Case 3 ' To Right
      StartProgress (HIY)
      For iy = LOY To HIY
      For ix = HIX To LOX Step -1
         'ixLR = HIX - (ix - HIX) + 1
         ixLR = 2 * HIX - ix + 1
         If ixLR <= FullW - 1 Then
            If aSelectAccepted Then
               ixx = ix - LOX
               iyy = iy - LOY
               If MaskArray(ixx, iyy) = 0 Then
                  picDATA(0, ixLR, iy) = picDATAORG(0, ix, iy)
                  picDATA(1, ixLR, iy) = picDATAORG(1, ix, iy)
                  picDATA(2, ixLR, iy) = picDATAORG(2, ix, iy)
               End If
            Else
               picDATA(0, ixLR, iy) = picDATAORG(0, ix, iy)
               picDATA(1, ixLR, iy) = picDATAORG(1, ix, iy)
               picDATA(2, ixLR, iy) = picDATAORG(2, ix, iy)
            End If
         End If
      Next ix
      DrawProgress
      Next iy
   End Select

   frmDeformers.picPB.Cls
End Sub

Public Sub ShearHorz(Frac As Long)
' ab = 0 Above, = 1 Below
'Frac = -20 to +20

Dim ab As Long
Dim zF As Single

Dim CH As Long, CW As Long
Dim dx As Single, zdx As Single
Dim iix As Single, jjx As Single
Dim pjx As Long
Dim jx As Long

   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
   If Frac <= 0 Then
      zF = (Frac + 10)
      zF = zF / 10      ' -1.0 to +1.0 || -.9 to 1.0
      ab = 0
   Else
      zF = (Frac - 10)
      zF = zF / 10      ' -1.0 to +1.0 || -.9 to 1.0
      ab = 1
   End If
   
   CH = FullH
   CW = FullW
   dx = zF * CW
   zdx = dx / CH
   
   If zF = 0 Then
      dx = 0
      zdx = 0
   End If
   
   If zF >= 0 Then
      iix = dx
      pjx = iix
      StartProgress (CH)
      If ab = 0 Then
         For iy = FullH - 1 To 0 Step -1
         For ix = 0 To FullW - 1
            jx = ix + iix
            
            If Abs(jx - pjx) > 1 Then
               If jx < 0 Then
                  jx = jx + 1
               Else
                  jx = jx - 1
               End If
            End If
            pjx = jx
            
            If (jx >= 0 And jx <= FullW - 1) Then
               ShearHorzCore jx
            End If
         Next ix
         iix = iix - zdx
         DrawProgress
         Next iy
      Else  ' ab=1
         For iy = 0 To FullH - 1
         For ix = 0 To FullW - 1
            jx = ix + iix
            
            If Abs(jx - pjx) > 1 Then
               If jx < 0 Then
                  jx = jx + 1
               Else
                  jx = jx - 1
               End If
            End If
            pjx = jx
            
            If (jx >= 0 And jx <= FullW - 1) Then
               ShearHorzCore jx
            End If
         Next ix
         iix = iix - zdx
         DrawProgress
         Next iy
      End If
   
   Else  ' zF < 0  (NB: DX & zdx -VE)
      iix = FullW - 1 + dx
      pjx = iix
      StartProgress (CH)
      If ab = 0 Then
         For iy = FullH - 1 To 0 Step -1
         For ix = FullW - 1 To 0 Step -1
            jx = iix - (FullW - 1 - ix)
            
            If Abs(jx - pjx) > 1 Then
               If jx < 0 Then
                  jx = jx + 1
               Else
                  jx = jx - 1
               End If
            End If
            pjx = jx
            
            If (jx >= 0 And jx <= FullW - 1) Then
               ShearHorzCore jx
            End If
         Next ix
         iix = iix - zdx
         DrawProgress
         Next iy
      Else  ' ab=1
         For iy = 0 To FullH - 1
         For ix = FullW - 1 To 0 Step -1
            jx = iix - (FullW - 1 - ix)
            
            If Abs(jx - pjx) > 1 Then
               If jx < 0 Then
                  jx = jx + 1
               Else
                  jx = jx - 1
               End If
            End If
            pjx = jx
            
            If (jx >= 0 And jx <= FullW - 1) Then
               ShearHorzCore jx
            End If
         Next ix
         iix = iix - zdx
         DrawProgress
         Next iy
      End If
   End If
   
   frmDeformers.picPB.Cls
End Sub

Private Sub ShearHorzCore(jjx As Long)
   If aSelectAccepted Then
      If (jjx >= LOX) And (jjx <= HIX) And (iy >= LOY And iy <= HIY) Then
         If Not aExternal Then   ' Internal
            If MaskArray(jjx - LOX, iy - LOY) = 0 Then ShearHorzCALC jjx
         Else
            If MaskArray(jjx - LOX, iy - LOY) <> 0 Then ShearHorzCALC jjx
         End If
      Else
         If aExternal Then ShearHorzCALC jjx
      End If
   Else
      ShearHorzCALC jjx
   End If
End Sub

Private Sub ShearHorzCALC(jjx As Long)
   picDATA(0, jjx, iy) = picDATAORG(0, ix, iy)
   picDATA(1, jjx, iy) = picDATAORG(1, ix, iy)
   picDATA(2, jjx, iy) = picDATAORG(2, ix, iy)
End Sub


Public Sub ShearVert(Frac As Long)
' LR = 0 Left, = 1 Right
' Frac -20 to +20

Dim LR As Long
Dim zF As Single

Dim CH As Long, CW As Long
Dim DY As Single, zdy As Single
Dim iiy As Single, jjy As Single
Dim pjy As Long
Dim jy As Long

   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
   
   If Frac <= 0 Then
      zF = (Frac + 10)
      zF = zF / 10      ' -1.0 to +1.0 || -.9 to 1.0
      LR = 0
   Else
      zF = (Frac - 10)
      zF = zF / 10      ' -1.0 to +1.0 || -.9 to 1.0
      LR = 1
   End If
   
   CH = FullH
   CW = FullW
   DY = zF * CH
   zdy = DY / CW
   
   If zF = 0 Then
      DY = 0
      zdy = 0
   End If
   
   If zF >= 0 Then
      iiy = DY
      pjy = iiy
      StartProgress (CW)
      If LR = 0 Then
         For ix = FullW - 1 To 0 Step -1
         For iy = 0 To FullH - 1
            jy = iy + iiy
            
            If Abs(jy - pjy) > 1 Then
               If jy < 0 Then
                  jy = jy + 1
               Else
                  jy = jy - 1
               End If
            End If
            pjy = jy
            
            If (jy >= 0 And jy <= FullH - 1) Then
               ShearVertCORE jy
            End If
         Next iy
         iiy = iiy - zdy
         DrawProgress
         Next ix
      Else  ' LR=1
         For ix = 0 To FullW - 1
         For iy = 0 To FullH - 1
            jy = iy + iiy
            
            If Abs(jy - pjy) > 1 Then
               If jy < 0 Then
                  jy = jy + 1
               Else
                  jy = jy - 1
               End If
            End If
            pjy = jy
            
            
            If (jy >= 0 And jy <= FullH - 1) Then
               ShearVertCORE jy
            End If
         Next iy
         iiy = iiy - zdy
         DrawProgress
         Next ix
      End If
   
   Else  ' zF < 0  (NB: DX & zdx -VE)
      iiy = FullH - 1 + DY
      pjy = iiy
      StartProgress (CW)
      If LR = 0 Then
         For ix = FullW - 1 To 0 Step -1
         For iy = FullH - 1 To 0 Step -1
            jy = iiy - (FullH - 1 - iy)
            
            If Abs(jy - pjy) > 1 Then
               If jy < 0 Then
                  jy = jy + 1
               Else
                  jy = jy - 1
               End If
            End If
            pjy = jy
            
            If (jy >= 0 And jy <= FullH - 1) Then
               ShearVertCORE jy
            End If
         Next iy
         iiy = iiy - zdy
         DrawProgress
         Next ix
      Else  ' LR=1
         For ix = 0 To FullW - 1
         For iy = FullH - 1 To 0 Step -1
            jy = iiy - (FullH - 1 - iy)
            
            If Abs(jy - pjy) > 1 Then
               If jy < 0 Then
                  jy = jy + 1
               Else
                  jy = jy - 1
               End If
            End If
            pjy = jy
            
            If (jy >= 0 And jy <= FullH - 1) Then
               ShearVertCORE jy
            End If
         Next iy
         iiy = iiy - zdy
         DrawProgress
         Next ix
      End If
   End If

   frmDeformers.picPB.Cls
End Sub
Private Sub ShearVertCORE(jjy As Long)
   If aSelectAccepted Then
      If (ix >= LOX) And (ix <= HIX) And (jjy >= LOY And jjy <= HIY) Then
         If Not aExternal Then   ' Internal
            If MaskArray(ix - LOX, jjy - LOY) = 0 Then ShearVertCALC jjy
         Else
            If MaskArray(ix - LOX, jjy - LOY) <> 0 Then ShearVertCALC jjy
         End If
      Else
         If aExternal Then ShearVertCALC jjy
      End If
   Else
      ShearVertCALC jjy
   End If
End Sub
Private Sub ShearVertCALC(jjy As Long)
   picDATA(0, ix, jjy) = picDATAORG(0, ix, iy)
   picDATA(1, ix, jjy) = picDATAORG(1, ix, iy)
   picDATA(2, ix, jjy) = picDATAORG(2, ix, iy)
End Sub


Public Sub SquashLeftRight(Frac As Long)
'Frac -20 to +20   LEFT or RIGHT
Dim CH As Long, CW As Long

Dim zF As Single
Dim DY As Single, zdy As Single
Dim dh As Single, zdh As Single
Dim zn As Single
Dim RR As Byte, RG As Byte, RB As Byte
Dim iiy As Single
   
   picDATA() = picDATAORG()
   
   If aUseRightColor Then FillpicDATAwithRightColor
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then ' Whole or Internal

   CH = HIY - LOY
   CW = HIX - LOX
   If Frac = -1 Then Frac = -2 ' Avoid moire pattern
   If Frac = 1 Then Frac = 2
   zF = Abs(Frac) / 2   ' -5 to +5
   If zF = 0 Then zF = 1
   
   DY = CH * (1 - 1 / zF) / (2 * CW)
   
   If Frac > 0 Then      ' Squash right side  Whole or Internal
      StartProgress (CW)
      For ix = LOX To HIX 'Step -1
         zdy = (ix - LOX) * DY
         dh = CH - 2 * zdy
         zdh = dh / CH
         zn = 0
         For iy = LOY To HIY
            iiy = LOY + zdy + zn * zdh
            If iiy >= LOY And iiy <= HIY Then
               If aSelectAccepted Then
                  ixx = ix - LOX
                  iyy = iiy - LOY
                  If MaskArray(ixx, iyy) = 0 Then SquashLeftRightCALC iiy
               Else
                  SquashLeftRightCALC iiy
               End If
            End If
            zn = zn + 1
         Next iy
         DrawProgress
        Next ix
        
   ElseIf Frac = 0 Then
      picDATA() = picDATAORG()
   
   Else  ' Frac<0    ' Squash left side  Whole or Internal
      StartProgress (CW)
      For ix = LOX To HIX
         zdy = (HIX - ix) * DY
         dh = CH - 2 * zdy
         zdh = dh / CH
         zn = CH
         For iy = LOY To HIY
            iiy = (LOY + zdy + zn * zdh)
            If iiy >= LOY And iiy <= HIY Then
               If aSelectAccepted Then
                  ixx = ix - LOX
                  iyy = iiy - LOY
                  If MaskArray(ixx, iyy) = 0 Then
                     SquashLeftRightCALC2 iiy
                  End If
               Else  ' nb LOY=0
                  SquashLeftRightCALC2 iiy
               End If
            End If
            zn = zn - 1
         Next iy
         DrawProgress
        Next ix
   
   End If
 
Else     ' aSelectAccepted and External
   
   CH = FullH - 1
   CW = FullW - 1
   If Frac = -1 Then Frac = -2 ' Avoid moire pattern
   If Frac = 1 Then Frac = 2
   zF = Abs(Frac) / 2   ' -5 to +5
   If zF = 0 Then zF = 1
   
   DY = CH * (1 - 1 / zF) / (2 * CW)

   
   If Frac > 0 Then  ' Squash right side External
      StartProgress (CW)
      
      For ix = 0 To FullW - 1
         zdy = ix * DY
         dh = CH - 2 * zdy
         zdh = dh / CH
         zn = 0
         
         For iy = 0 To FullH - 1
            
            iiy = zdy + zn * zdh
            
            If (iiy > 0 And iiy < FullH - 1) Then
            
                     If (ix >= LOX) And (ix <= HIX) And (iiy >= LOY And iiy <= HIY) Then
                        If MaskArray(ix - LOX, iiy - LOY) <> 0 Then SquashLeftRightCALC iiy
                     Else
                        SquashLeftRightCALC iiy
                     End If
            
            End If
            zn = zn + 1
         Next iy
         DrawProgress
        Next ix
        
   ElseIf Frac = 0 Then
      picDATA() = picDATAORG()
   
   Else  ' Frac < 0      ' Squash left side   External
      StartProgress (CW)
      
      For ix = 0 To FullW - 1
         zdy = (FullW - 1 - ix) * DY
         dh = CH - 2 * zdy
         zdh = dh / CH
         zn = CH
         
         For iy = 0 To FullH - 1
            iiy = zdy + zn * zdh
            
            If (iiy > 0 And iiy < FullH - 1) Then
            
                     If (ix >= LOX) And (ix <= HIX) And (iiy >= LOY And iiy <= HIY) Then
                        If MaskArray(ix - LOX, iiy - LOY) <> 0 Then
                           SquashLeftRightCALC2 iiy
                        End If
                     Else  ' nb LOY=0
                        SquashLeftRightCALC2 iiy
                     End If
            
            End If
            zn = zn - 1
         Next iy
         DrawProgress
        Next ix
   
   End If
   
End If
   
   frmDeformers.picPB.Cls
End Sub
Private Sub SquashLeftRightCALC(iiy As Single)
   picDATA(0, ix, iiy) = picDATAORG(0, ix, iy)
   picDATA(1, ix, iiy) = picDATAORG(1, ix, iy)
   picDATA(2, ix, iiy) = picDATAORG(2, ix, iy)
End Sub
Private Sub SquashLeftRightCALC2(iiy As Single)
' Stop upside down
   picDATA(0, ix, iiy) = picDATAORG(0, ix, FullH - 1 - iy)
   picDATA(1, ix, iiy) = picDATAORG(1, ix, FullH - 1 - iy)
   picDATA(2, ix, iiy) = picDATAORG(2, ix, FullH - 1 - iy)
End Sub


Public Sub SquashAboveBelow(Frac As Long)
'zFrac -20.0 to +20.0   ABOVE or BELOW
Dim CH As Long, CW As Long

Dim zF As Single
Dim dx As Single, zdx As Single
Dim dh As Single, zdh As Single
Dim zn As Single
Dim iix As Single
'Dim aInner As Boolean

   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
'   aInner = (aSelectAccepted And (Not aExternal))

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then

   CH = HIY - LOY
   CW = HIX - LOX
   If Frac = -1 Then Frac = -2 ' Avoid moire pattern
   If Frac = 1 Then Frac = 2
   zF = Abs(Frac) / 2     ' -5 to +5
   If zF = 0 Then zF = 1

   dx = CW * (1 - 1 / zF) / (2 * CH)

   If Frac > 0 Then
      StartProgress (CH)
      For iy = LOY To HIY
         zdx = (iy - LOY) * dx
         dh = CW - 2 * zdx
         zdh = dh / CW
         zn = 0
         For ix = LOX To HIX
            iix = LOX + zdx + zn * zdh
            If iix >= LOX And iix <= HIX Then
               If aSelectAccepted Then
                  If MaskArray(iix - LOX, iy - LOY) = 0 Then SquashAboveBelowCALC iix
               Else
                  SquashAboveBelowCALC iix
               End If
            End If
            zn = zn + 1
         Next ix
         DrawProgress
        Next iy

   ElseIf Frac = 0 Then
      picDATA() = picDATAORG()

   Else  ' Frac<0 Squash Below Whole or internal
      StartProgress (CH)
      For iy = LOY To HIY
         zdx = (HIY - iy) * dx
         'zdx = iy * dx
         dh = CW - 2 * zdx
         zdh = dh / CW
         zn = CW
         For ix = LOX To HIX
            'iix = LOX + zdx + zn * zdh
            iix = HIX - zdx - zn * zdh
            If iix >= LOX And iix <= HIX Then
               If aSelectAccepted Then
                  If MaskArray(iix - LOX, iy - LOY) = 0 Then SquashAboveBelowCALC iix
               Else
                  SquashAboveBelowCALC iix
               End If
            End If
            zn = zn - 1
         Next ix
         DrawProgress
        Next iy

   End If

Else     ' aSelectAccepted and External
   
   CH = FullH - 1
   CW = FullW - 1
   
   If Frac = -1 Then Frac = -2 ' Avoid moire pattern
   If Frac = 1 Then Frac = 2
   zF = Abs(Frac) / 2     ' -5 to +5
   If zF = 0 Then zF = 1
   
   dx = CW * (1 - 1 / zF) / (2 * CH)
   
   If Frac > 0 Then
      StartProgress (CH)
      For iy = 0 To FullH - 1
         
         zdx = iy * dx
         dh = CW - 2 * zdx
         zdh = dh / CW
         zn = 0
         
         For ix = 0 To FullW - 1
            
            iix = zdx + zn * zdh
         
            If (iix > 0 And iix < FullW - 1) Then
                  
               If (iix >= LOX) And (iix <= HIX) And (iy >= LOY And iy <= HIY) Then
                  If MaskArray(iix - LOX, iy - LOY) <> 0 Then SquashAboveBelowCALC iix
               Else
                  SquashAboveBelowCALC iix
               End If
            
            End If
            zn = zn + 1
         Next ix
         DrawProgress
        Next iy
   
   ElseIf Frac = 0 Then
      picDATA() = picDATAORG()
   
   Else  ' Frac<0
      StartProgress (CH)
      For iy = 0 To FullH - 1
         
         zdx = (FullH - 1 - iy) * dx
         dh = CW - 2 * zdx
         zdh = dh / CW
         zn = CW
         
         For ix = 0 To FullW - 1
            
            iix = FullW - 1 - zdx - zn * zdh
            'iix = zdx + zn * zdh
            
            If (iix > 0 And iix < FullW - 1) Then
            
               If (iix >= LOX) And (iix <= HIX) And (iy >= LOY And iy <= HIY) Then
                  If MaskArray(iix - LOX, iy - LOY) <> 0 Then SquashAboveBelowCALC iix
               Else
                  SquashAboveBelowCALC iix
               End If
            
            End If
            zn = zn - 1
         Next ix
         DrawProgress
        Next iy
   
   End If

End If

   frmDeformers.picPB.Cls
End Sub
Private Sub SquashAboveBelowCALC(iix As Single)
   picDATA(0, iix, iy) = picDATAORG(0, ix, iy)
   picDATA(1, iix, iy) = picDATAORG(1, ix, iy)
   picDATA(2, iix, iy) = picDATAORG(2, ix, iy)
End Sub

Public Sub SquashHORZ(Frac As Long)
'zFrac 0 to 49
Dim CH As Long
Dim CW As Long

Dim zF As Single
Dim dx As Single
Dim dh As Single
Dim zn As Single
Dim iix As Single
   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor

   
If Frac <> 0 Then
      
   If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
      
      CH = HIY - LOY
      CW = HIX - LOX
      zF = Abs(Frac)
      dx = 1 - 2 * (zF) / 100   ' zF/100 = 0 to .5 ' dx = 1 to 0
         
         StartProgress (CW)
         For iy = LOY To HIY
            zn = 0
            For ix = LOX To HIX 'Step -1
               iix = LOX + CW * (zF / 100) + zn * dx
               If iix >= LOX And iix <= HIX Then
                  If aSelectAccepted Then
                     ixx = iix - LOX
                     iyy = iy - LOY
                     If MaskArray(ixx, iyy) = 0 Then SquashHORZCALC iix
                  Else
                     SquashHORZCALC iix
                  End If
               End If
               zn = zn + 1
            Next ix
            DrawProgress
         Next iy
         
   Else     ' aSelectAccepted and External
   
      CH = FullH - 1
      CW = FullW - 1
      zF = Abs(Frac)
      dx = 1 - 2 * (zF) / 100   ' zF/100 = 0 to .5 ' dx = 1 to 0
         StartProgress (CW)
         For iy = 0 To FullH - 1
            zn = 0
            For ix = 0 To FullW - 1
               iix = CW * (zF / 100) + zn * dx
               If (iix >= 0 And iix <= FullW - 1) Then
                     If (iix >= LOX And iix <= HIX) And (iy >= LOY And iy <= HIY) Then
                           If MaskArray(iix - LOX, iy - LOY) <> 0 Then SquashHORZCALC iix
                     Else
                        SquashHORZCALC iix
                     End If
               
               End If
               zn = zn + 1
            Next ix
            DrawProgress
         Next iy
   End If
   
   
Else ' zFrac = 0
   picDATA() = picDATAORG()
End If

   frmDeformers.picPB.Cls
End Sub
Private Sub SquashHORZCALC(iix As Single)
   picDATA(0, iix, iy) = picDATAORG(0, ix, iy)
   picDATA(1, iix, iy) = picDATAORG(1, ix, iy)
   picDATA(2, iix, iy) = picDATAORG(2, ix, iy)
End Sub

Public Sub SquashVERT(Frac As Long)
'zFrac 0 to 49
Dim CH As Long, CW As Long

Dim zF As Single
Dim DY As Single
Dim dh As Single
Dim zn As Single
Dim iix As Single, iiy As Single
   
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
'GoTo XXX
   
   
If Frac <> 0 Then
      
      
   If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
      
      CH = HIY - LOY
      CW = HIX - LOX
      zF = Abs(Frac)
      DY = 1 - 2 * (zF) / 100   ' zF/100 = 0 to .5 ' dx = 1 to 0
      
      StartProgress (CH)
      For ix = LOX To HIX
         zn = 0
         For iy = LOY To HIY
            iiy = LOY + CH * (zF / 100) + zn * DY
            If iiy >= LOY And iiy <= HIY Then
               If aSelectAccepted Then
                  If MaskArray(ix - LOX, iy - LOY) = 0 Then SquashVERTCALC iiy
               Else
                  SquashVERTCALC iiy
               End If
            End If
            zn = zn + 1
         Next iy
         DrawProgress
      Next ix

   Else     ' aSelectAccepted and External

      CH = FullH - 1
      CW = FullW - 1
      zF = Abs(Frac)
      DY = 1 - 2 * (zF) / 100   ' zF/100 = 0 to .5 ' DY = 1 to 0
   
      StartProgress (CH)
      For ix = 0 To FullW - 1
         zn = 0
         For iy = 0 To FullH - 1
            iiy = CH * (zF / 100) + zn * DY
            If (iiy >= 0 And iiy <= FullH - 1) Then
            
                  If (ix >= LOX And ix <= HIX) And (iiy >= LOY And iiy <= HIY) Then
                        If MaskArray(ix - LOX, iiy - LOY) <> 0 Then SquashVERTCALC iiy
                  Else
                     SquashVERTCALC iiy
                  End If
            
            End If
            zn = zn + 1
         Next iy
         DrawProgress
      Next ix
   End If
   
Else ' Frac = 0
   picDATA() = picDATAORG()
End If
   
   frmDeformers.picPB.Cls
End Sub
Private Sub SquashVERTCALC(iiy As Single)
   picDATA(0, ix, iiy) = picDATAORG(0, ix, iy)
   picDATA(1, ix, iiy) = picDATAORG(1, ix, iy)
   picDATA(2, ix, iiy) = picDATAORG(2, ix, iy)
End Sub


Public Sub GhostingLR(PGhosting As Long)
' PGhosting -20 TO +20

'ReDim picData2(0 To 3, 0 To HIX, 0 To HIY)
ReDim picData2(0 To 3, 0 To FullW - 1, 0 To FullH - 1)
picData2() = picDATAORG()
picDATA() = picDATAORG()
     
Dim NN As Long, n As Long
NN = Abs(PGhosting)  ' 0 to 20

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   ' All or Internal

   StartProgress (NN)
   For n = 1 To NN
   
      If aSelectAccepted Then
         For iy = LOY To HIY Step 2
         For ix = LOX + 4 To HIX - 4
            If MaskArray(ix - LOX, iy - LOY) = 0 Then GhostingLRCALC PGhosting
        Next ix
         Next iy
      Else
         For iy = LOY To HIY Step 2
         For ix = LOX + 4 To HIX - 4
            GhostingLRCALC PGhosting
         Next ix
         Next iy
      End If
      picData2() = picDATA()
      DrawProgress
   Next n
   
' For iy = LOY To HIY - 2
' For ix = LOX To HIX
   VSmooth
   
Else     ' aSelectAccepted and External

   StartProgress (NN)
   For n = 1 To NN
   
      For iy = 0 To FullH - 1 Step 2
      For ix = 4 To FullW - 1 - 4
         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then GhostingLRCALC PGhosting
         Else
            GhostingLRCALC PGhosting
         End If
     Next ix
     Next iy
     picData2() = picDATA()
     DrawProgress
   Next n
   
' For iy = 0 To FullH - 3
' For ix = 0 To FullW - 1
   VSmoothEXT

End If

   Erase picData2()
   
   frmDeformers.picPB.Cls
End Sub

Private Sub GhostingLRCALC(PGhosting As Long)
' PGhosting -20 TO 20

' -ve
' 11111000

' +ve
' 00011111

Dim LR As Long, LG As Long, LB As Long
Dim i As Long, j As Long
Dim ii As Long, jj As Long
     
     LR = 0: LG = 0: LB = 0
      j = iy
      
If PGhosting <= 0 Then
      For i = ix - 4 To ix + 4
         jj = j - iy
         ii = i - ix
         If ii >= 0 And jj = 0 Then
         If j >= 0 And j <= FullH - 1 Then
            LB = LB + picData2(0, i, j)
            LG = LG + picData2(1, i, j)
            LR = LR + picData2(2, i, j)
         End If
         End If
      Next i
Else  ' PGhosting > 0
      For i = ix - 4 To ix + 4
         jj = j - iy
         ii = i - ix
         If ii <= 0 And jj = 0 Then
         If j >= 0 And j <= FullH - 1 Then
            LB = LB + picData2(0, i, j)
            LG = LG + picData2(1, i, j)
            LR = LR + picData2(2, i, j)
         End If
         End If
      Next i
End If
      
   LB = LB / 5
   LG = LG / 5
   LR = LR / 5
   If LB < 0 Then LB = 0 Else If LB > 255 Then LB = 255
   If LG < 0 Then LG = 0 Else If LG > 255 Then LG = 255
   If LR < 0 Then LR = 0 Else If LR > 255 Then LR = 255
   If iy >= 0 And iy <= FullH - 1 Then
      picDATA(0, ix, iy) = LB
      picDATA(1, ix, iy) = LG
      picDATA(2, ix, iy) = LR
   End If

End Sub

Private Sub VSmooth()

'00000
'00000
'00300
'00100
'00100   /5

Dim B As Long, g As Long, r As Long
Dim n As Long

For n = 1 To 4
      B = 0: g = 0: r = 0
      For iy = LOY To HIY - 2
      For ix = LOX To HIX
         B = 3 * picData2(0, ix, iy)
         B = B + picData2(0, ix, iy + 1)
         B = B + picData2(0, ix, iy + 2)
      
         g = 3 * picData2(1, ix, iy)
         g = g + picData2(1, ix, iy + 1)
         g = g + picData2(1, ix, iy + 2)
      
         r = 3 * picData2(2, ix, iy)
         r = r + picData2(2, ix, iy + 1)
         r = r + picData2(2, ix, iy + 2)
      
         B = B / 5
         g = g / 5
         r = r / 5
      
      If B < 0 Then B = 0 Else If B > 255 Then B = 255
      If g < 0 Then g = 0 Else If g > 255 Then g = 255
      If r < 0 Then r = 0 Else If r > 255 Then r = 255
         
         picDATA(0, ix, iy) = B
         picDATA(1, ix, iy) = g
         picDATA(2, ix, iy) = r
      Next ix
      Next iy

      picData2() = picDATA()
Next n
End Sub
Private Sub VSmoothEXT()

'00000
'00000
'00300
'00100
'00100   /5

Dim B As Long, g As Long, r As Long
Dim n As Long

For n = 1 To 4
      B = 0: g = 0: r = 0
      For iy = 0 To FullH - 3
      For ix = 0 To FullW - 1
         
         B = 3 * picData2(0, ix, iy)
         B = B + picData2(0, ix, iy + 1)
         B = B + picData2(0, ix, iy + 2)
      
         g = 3 * picData2(1, ix, iy)
         g = g + picData2(1, ix, iy + 1)
         g = g + picData2(1, ix, iy + 2)
      
         r = 3 * picData2(2, ix, iy)
         r = r + picData2(2, ix, iy + 1)
         r = r + picData2(2, ix, iy + 2)
         
         B = B / 5
         g = g / 5
         r = r / 5
      
      If B < 0 Then B = 0 Else If B > 255 Then B = 255
      If g < 0 Then g = 0 Else If g > 255 Then g = 255
      If r < 0 Then r = 0 Else If r > 255 Then r = 255
         
         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then
               picDATA(0, ix, iy) = B
               picDATA(1, ix, iy) = g
               picDATA(2, ix, iy) = r
            End If
         Else
            picDATA(0, ix, iy) = B
            picDATA(1, ix, iy) = g
            picDATA(2, ix, iy) = r
         End If
      Next ix
      Next iy

      picData2() = picDATA()
Next n
End Sub



Public Sub GhostingAB(PGhosting As Long)
' PGhosting -20 TO +20

'ReDim picData2(0 To 3, 0 To HIX, 0 To HIY)
ReDim picData2(0 To 3, 0 To FullW - 1, 0 To FullH - 1)
picData2() = picDATAORG()
picDATA() = picDATAORG()
     
Dim NN As Long, n As Long
NN = Abs(PGhosting)  ' 0 to 20

If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   ' All or Internal

   StartProgress (NN)
   For n = 1 To NN
   
      If aSelectAccepted Then
         For ix = LOX To HIX Step 2
         For iy = LOY + 4 To HIY - 4
            
            If MaskArray(ix - LOX, iy - LOY) = 0 Then GhostingABCALC PGhosting
        Next iy
        Next ix
      Else
         For ix = LOX To HIX Step 2
         For iy = LOY + 4 To HIY - 4
            GhostingABCALC PGhosting
         Next iy
         Next ix
      End If
      picData2() = picDATA()
      DrawProgress
   Next n
   
   HSmooth
   
Else     ' aSelectAccepted and External

   StartProgress (NN)
   For n = 1 To NN
      For ix = 0 To FullW - 1 Step 2
      For iy = 4 To FullH - 1 - 4
         
         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then GhostingABCALC PGhosting
         Else
            GhostingABCALC PGhosting
         End If
     Next iy
     Next ix
     picData2() = picDATA()
     DrawProgress
   Next n
   
   HSmoothEXT

End If

   Erase picData2()
   
   frmDeformers.picPB.Cls
End Sub

Private Sub GhostingABCALC(PGhosting As Long)
' PGhosting -20 TO 20

' -ve   +ve
' 0     1
' 0     1
' 0     1
' 1     1
' 1     0
' 1     0
' 1     0
' 1     0

Dim LR As Long, LG As Long, LB As Long
Dim i As Long, j As Long
Dim ii As Long, jj As Long
     
     LR = 0: LG = 0: LB = 0
      i = ix
If PGhosting <= 0 Then
      For j = iy - 4 To iy + 4
         jj = j - iy
         ii = i - ix
         If jj >= 0 And ii = 0 Then
         If i >= 0 And i <= FullW - 1 Then
            LB = LB + picData2(0, i, j)
            LG = LG + picData2(1, i, j)
            LR = LR + picData2(2, i, j)
         End If
         End If
      Next j
Else  ' PGhosting > 0
      For j = iy - 4 To iy + 4
         jj = j - iy
         ii = i - ix
         If jj <= 0 And ii = 0 Then
         If i >= 0 And i <= FullW - 1 Then
            LB = LB + picData2(0, i, j)
            LG = LG + picData2(1, i, j)
            LR = LR + picData2(2, i, j)
         End If
         End If
      Next j
End If
      
   LB = LB / 5
   LG = LG / 5
   LR = LR / 5
   If LB < 0 Then LB = 0 Else If LB > 255 Then LB = 255
   If LG < 0 Then LG = 0 Else If LG > 255 Then LG = 255
   If LR < 0 Then LR = 0 Else If LR > 255 Then LR = 255
   If iy >= 0 And iy <= FullH - 1 Then
      picDATA(0, ix, iy) = LB
      picDATA(1, ix, iy) = LG
      picDATA(2, ix, iy) = LR
   End If

End Sub


Private Sub HSmooth()

'00000
'00000
'00311
'00000
'00000   /5

Dim B As Long, g As Long, r As Long
Dim n As Long

For n = 1 To 4
      B = 0: g = 0: r = 0
      For ix = LOX To HIX - 2
      For iy = LOY To HIY
         
         B = 3 * picData2(0, ix, iy)
         B = B + picData2(0, ix + 1, iy)
         B = B + picData2(0, ix + 2, iy)
      
         g = 3 * picData2(1, ix, iy)
         g = g + picData2(1, ix + 1, iy)
         g = g + picData2(1, ix + 2, iy)
      
         r = 3 * picData2(2, ix, iy)
         r = r + picData2(2, ix + 1, iy)
         r = r + picData2(2, ix + 2, iy)
      
         B = B / 5
         g = g / 5
         r = r / 5
      
      If B < 0 Then B = 0 Else If B > 255 Then B = 255
      If g < 0 Then g = 0 Else If g > 255 Then g = 255
      If r < 0 Then r = 0 Else If r > 255 Then r = 255
         
         picDATA(0, ix, iy) = B
         picDATA(1, ix, iy) = g
         picDATA(2, ix, iy) = r
      Next iy
      Next ix

      picData2() = picDATA()
Next n
End Sub


Private Sub HSmoothEXT()

'00000
'00000
'00311
'00000
'00000   /5

Dim B As Long, g As Long, r As Long
Dim n As Long

For n = 1 To 4
      B = 0: g = 0: r = 0
      For ix = 0 To FullW - 3
      For iy = 0 To FullH - 1
         
         B = 3 * picData2(0, ix, iy)
         B = B + picData2(0, ix + 1, iy)
         B = B + picData2(0, ix + 2, iy)
      
         g = 3 * picData2(1, ix, iy)
         g = g + picData2(1, ix + 1, iy)
         g = g + picData2(1, ix + 2, iy)
      
         r = 3 * picData2(2, ix, iy)
         r = r + picData2(2, ix + 1, iy)
         r = r + picData2(2, ix + 2, iy)
         
         
         B = B / 5
         g = g / 5
         r = r / 5
      
      If B < 0 Then B = 0 Else If B > 255 Then B = 255
      If g < 0 Then g = 0 Else If g > 255 Then g = 255
      If r < 0 Then r = 0 Else If r > 255 Then r = 255
         
         If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
            If MaskArray(ix - LOX, iy - LOY) <> 0 Then
               picDATA(0, ix, iy) = B
               picDATA(1, ix, iy) = g
               picDATA(2, ix, iy) = r
            End If
         Else
            picDATA(0, ix, iy) = B
            picDATA(1, ix, iy) = g
            picDATA(2, ix, iy) = r
         End If
      Next iy
      Next ix

      picData2() = picDATA()
Next n
End Sub

Public Sub Squeeze(PAR As Long)
' PAR 5 - 400
Dim CH As Long, CW As Long
Dim ixx As Long, iyy As Long
Dim xc As Long, yc As Long
Dim Rad As Single, Alpha As Single
Dim zR As Single

Dim ixd As Long, iyd As Long

   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
   
If Not aSelectAccepted Or (aSelectAccepted And (Not aExternal)) Then
   
   CW = (HIX - LOX)
   CH = (HIY - LOY)
   xc = LOX + CW / 2
   yc = LOY + CH / 2
   
   StartProgress (HIY - 1)
      
   For iy = LOY To HIY
   For ix = LOX To HIX
      ' Get polar coords
      Rad = Sqr((xc - ix) ^ 2 + (yc - iy) ^ 2)
      Rad = Sqr(Rad * PAR)
      Alpha = zAng(yc - iy, xc - ix)
      
      ' Convert back to Cartesian
      ixx = Rad * Cos(Alpha) + xc
      iyy = Rad * Sin(Alpha) + yc
      
      If ixx >= LOX And ixx <= HIX Then
      If iyy >= LOY And iyy <= HIY Then
         If aSelectAccepted Then
            If MaskArray(ix - LOX, iy - LOY) = 0 Then
               picDATA(0, ix, iy) = picDATAORG(0, HIX + LOX - ixx, iyy)
               picDATA(1, ix, iy) = picDATAORG(1, HIX + LOX - ixx, iyy)
               picDATA(2, ix, iy) = picDATAORG(2, HIX + LOX - ixx, iyy)
            End If
         Else  ' LOX=0
               picDATA(0, ix, iy) = picDATAORG(0, HIX - ixx, iyy)
               picDATA(1, ix, iy) = picDATAORG(1, HIX - ixx, iyy)
               picDATA(2, ix, iy) = picDATAORG(2, HIX - ixx, iyy)
         End If
      End If
      End If

   Next ix
   DrawProgress
   Next iy

Else     ' aSelectAccepted and External

   CW = FullW - 1
   CH = FullH - 1
   xc = CW / 2
   yc = CH / 2
   
   StartProgress (HIY - 1)
      
   For iy = 0 To FullH - 1
   For ix = 0 To FullW - 1
      ' Get polar coords
      Rad = Sqr((xc - ix) ^ 2 + (yc - iy) ^ 2)
      Rad = Sqr(Rad * PAR)
      Alpha = zAng(yc - iy, xc - ix)
      ' Convert back to Cartesian
      ixx = Rad * Cos(Alpha) + xc
      iyy = Rad * Sin(Alpha) + yc
      
      If (ixx >= 0 And ixx <= FullW - 1) And (iyy >= 0 And iyy <= FullH - 1) Then
           
           ixx = FullW - 1 - ixx
           
           If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
               If MaskArray(ix - LOX, iy - LOY) <> 0 Then
                  picDATA(0, ix, iy) = picDATAORG(0, ixx, iyy)
                  picDATA(1, ix, iy) = picDATAORG(1, ixx, iyy)
                  picDATA(2, ix, iy) = picDATAORG(2, ixx, iyy)
               End If
           Else
               picDATA(0, ix, iy) = picDATAORG(0, ixx, iyy)
               picDATA(1, ix, iy) = picDATAORG(1, ixx, iyy)
               picDATA(2, ix, iy) = picDATAORG(2, ixx, iyy)
           End If
      
      End If
   Next ix
   DrawProgress
   Next iy
End If

   frmDeformers.picPB.Cls

End Sub

Public Sub Scrunch(PAR As Long)
' PAR 1 - 10
Dim ixx As Long, iyy As Long
Dim Rad As Single, Alpha As Single
Dim zR As Single
Dim dx As Single, DY As Single

   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor

   ReDim OffsetY(0 To FullW - 1) As Single
   
   'zR = Rnd(-0.5)
   Randomize (PAR)
   
   StartProgress HIX
   zR = PAR
   For ix = 0 To FullW - 1
      zR = zR + PAR * (Rnd - 0.5)
      OffsetY(ix) = zR / 2
   Next ix
   
   For iy = 0 To FullH - 1
      zR = zR + PAR * (Rnd - 0.5)
   For ix = 0 To FullW - 1
      dx = ix + zR
      DY = iy + OffsetY(ix)
      If (dx >= 0 And dx <= FullW - 1) And (DY >= 0 And DY <= FullH - 1) Then
      
         If aSelectAccepted Then ' Selection ON
            If (ix >= LOX And ix <= HIX) And (iy >= LOY And iy <= HIY) Then
               If Not aExternal Then   ' Internal
                  If MaskArray(ix - LOX, iy - LOY) = 0 Then 'ScrunchCALC dx, DY
                     If (dx >= LOX And dx <= HIX) And (DY >= LOY And DY <= HIY) Then
                     If MaskArray(dx - LOX, DY - LOY) = 0 Then ScrunchCALC dx, DY
                  End If
            End If
               Else  'External
                  If MaskArray(ix - LOX, iy - LOY) <> 0 Then ScrunchCALC dx, DY
               End If
            Else
               If aExternal Then ScrunchCALC dx, DY
            End If
         Else  ' Selection OFF
            ScrunchCALC dx, DY
         End If
      
      End If
   Next ix
   DrawProgress
   Next iy
   
   frmDeformers.picPB.Cls

End Sub
Private Sub ScrunchCALC(dx As Single, DY As Single)
   picDATA(0, ix, iy) = picDATAORG(0, dx, DY)
   picDATA(1, ix, iy) = picDATAORG(1, dx, DY)
   picDATA(2, ix, iy) = picDATAORG(2, dx, DY)
End Sub


Public Sub TEST(PAR As Long)
Dim CH As Long, CW As Long
Dim ixx As Long, iyy As Long
Dim xc As Long, yc As Long
Dim Rad As Single, Alpha As Single
Dim Rad2 As Single
Dim B As Long, g As Long, r As Long
Dim zR As Single
Dim dx As Single, DY As Single
Dim Intens As Single

   ReDim OffsetY(0 To HIX) As Single

   xc = LOX + (HIX - LOX) / 2
   yc = LOY + (HIY - LOY) / 2
   
   CH = (HIY - LOY)
   CW = (HIX - LOX)
      
   picDATA() = picDATAORG()
   If aUseRightColor Then FillpicDATAwithRightColor
      
   StartProgress (HIX - 1)
   zR = PAR
   For ix = LOX To HIX
      zR = zR + PAR * (Rnd - 0.5)
      OffsetY(ix) = zR / 2
   Next ix
      
      For iy = LOY To HIY
      For ix = LOX To HIX
         dx = ix + zR
         DY = iy + OffsetY(ix)
         If dx >= LOX And dx <= HIX Then
         If DY >= LOY And DY <= HIY Then
            B = picDATAORG(0, dx, DY)
            g = picDATAORG(1, dx, DY)
            r = picDATAORG(2, dx, DY)
            picDATA(0, ix, iy) = B
            picDATA(1, ix, iy) = g
            picDATA(2, ix, iy) = r
         End If
         End If
      Next ix
      DrawProgress
      Next iy


   frmDeformers.picPB.Cls

End Sub


Private Sub StartProgress(ByVal PAR As Single)
' Private xd As Single, xp As Single
' EG: Par = PRELIEF * FullH
   frmDeformers.picPB.DrawWidth = 3
   frmDeformers.picPB.Cls
   xp = 0
   If PAR <> 0 Then
      xd = frmDeformers.picPB.Width / PAR
   End If
   frmDeformers.picPB.PSet (xp, 2), vbRed
End Sub
Private Sub DrawProgress()
' Private xd As Single, xp As Single
   frmDeformers.picPB.Line -(xp, 2), vbRed
   frmDeformers.picPB.Refresh
   xp = xp + xd
End Sub

