home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / FAST_Magic21308710162008.psc / FrmTest.frm < prev    next >
Text File  |  2008-10-16  |  12KB  |  407 lines

  1. VERSION 5.00
  2. Begin VB.Form FrmTest 
  3.    Caption         =   "Testform Fast Magic Wand Selection"
  4.    ClientHeight    =   8175
  5.    ClientLeft      =   60
  6.    ClientTop       =   450
  7.    ClientWidth     =   9195
  8.    LinkTopic       =   "Form1"
  9.    MaxButton       =   0   'False
  10.    ScaleHeight     =   545
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   613
  13.    StartUpPosition =   3  'Windows-Standard
  14.    Begin VB.ComboBox CboStyle 
  15.       Height          =   315
  16.       ItemData        =   "FrmTest.frx":0000
  17.       Left            =   1920
  18.       List            =   "FrmTest.frx":0010
  19.       Style           =   2  'Dropdown-Liste
  20.       TabIndex        =   10
  21.       Top             =   120
  22.       Width           =   1575
  23.    End
  24.    Begin VB.OptionButton OptPic 
  25.       Caption         =   "Border"
  26.       Height          =   255
  27.       Index           =   2
  28.       Left            =   3600
  29.       TabIndex        =   9
  30.       Top             =   960
  31.       Width           =   1215
  32.    End
  33.    Begin VB.OptionButton OptPic 
  34.       Caption         =   "Magic"
  35.       Height          =   255
  36.       Index           =   1
  37.       Left            =   1800
  38.       TabIndex        =   8
  39.       Top             =   960
  40.       Width           =   1215
  41.    End
  42.    Begin VB.OptionButton OptPic 
  43.       Caption         =   "Picture"
  44.       Height          =   255
  45.       Index           =   0
  46.       Left            =   120
  47.       TabIndex        =   7
  48.       Top             =   960
  49.       Value           =   -1  'True
  50.       Width           =   1215
  51.    End
  52.    Begin VB.Timer TmrShowBorder 
  53.       Enabled         =   0   'False
  54.       Interval        =   500
  55.       Left            =   7560
  56.       Top             =   240
  57.    End
  58.    Begin VB.HScrollBar HscrPerc 
  59.       Height          =   255
  60.       Left            =   120
  61.       Max             =   100
  62.       Min             =   1
  63.       TabIndex        =   2
  64.       Top             =   480
  65.       Value           =   15
  66.       Width           =   3375
  67.    End
  68.    Begin VB.PictureBox PicOrg 
  69.       AutoSize        =   -1  'True
  70.       BorderStyle     =   0  'Kein
  71.       Height          =   6750
  72.       Left            =   120
  73.       OLEDropMode     =   1  'Manuell
  74.       Picture         =   "FrmTest.frx":0031
  75.       ScaleHeight     =   450
  76.       ScaleMode       =   3  'Pixel
  77.       ScaleWidth      =   600
  78.       TabIndex        =   0
  79.       Top             =   1320
  80.       Width           =   9000
  81.    End
  82.    Begin VB.PictureBox PicBorder 
  83.       AutoRedraw      =   -1  'True
  84.       BorderStyle     =   0  'Kein
  85.       FillColor       =   &H00FFFFFF&
  86.       FillStyle       =   0  'Ausgefⁿllt
  87.       Height          =   6750
  88.       Left            =   120
  89.       ScaleHeight     =   450
  90.       ScaleMode       =   3  'Pixel
  91.       ScaleWidth      =   600
  92.       TabIndex        =   5
  93.       Top             =   1320
  94.       Width           =   9000
  95.    End
  96.    Begin VB.PictureBox PicDest 
  97.       AutoRedraw      =   -1  'True
  98.       BorderStyle     =   0  'Kein
  99.       FillColor       =   &H00FFFFFF&
  100.       FillStyle       =   0  'Ausgefⁿllt
  101.       Height          =   6750
  102.       Left            =   120
  103.       ScaleHeight     =   450
  104.       ScaleMode       =   3  'Pixel
  105.       ScaleWidth      =   600
  106.       TabIndex        =   1
  107.       Top             =   1320
  108.       Width           =   9000
  109.    End
  110.    Begin VB.Label Label2 
  111.       Caption         =   "Selected Color"
  112.       Height          =   255
  113.       Left            =   5400
  114.       TabIndex        =   6
  115.       Top             =   120
  116.       Width           =   1575
  117.    End
  118.    Begin VB.Label LblTime 
  119.       Caption         =   "  "
  120.       Height          =   375
  121.       Left            =   5400
  122.       TabIndex        =   4
  123.       Top             =   840
  124.       Width           =   2415
  125.    End
  126.    Begin VB.Shape ShpColor 
  127.       FillStyle       =   0  'Ausgefⁿllt
  128.       Height          =   255
  129.       Left            =   5400
  130.       Top             =   480
  131.       Width           =   1575
  132.    End
  133.    Begin VB.Label Label1 
  134.       Caption         =   "Difference 15%"
  135.       Height          =   255
  136.       Left            =   240
  137.       TabIndex        =   3
  138.       Top             =   120
  139.       Width           =   1575
  140.    End
  141. End
  142. Attribute VB_Name = "FrmTest"
  143. Attribute VB_GlobalNameSpace = False
  144. Attribute VB_Creatable = False
  145. Attribute VB_PredeclaredId = True
  146. Attribute VB_Exposed = False
  147. Option Explicit
  148. 'Real Fast Magic Wand
  149. 'Scythe 2008
  150.  
  151. 'How does ist work
  152. 'I take a color and clear anything thats different
  153.  
  154. 'After this i use FloodFill to make the everything
  155. 'white thats touching our original point
  156.  
  157. 'Now i create a line on every white pixel with a non white near it
  158.  
  159. 'Thats it
  160. 'Very simple but extreme fast :-)
  161.  
  162. 'Pointer to our Region
  163. Dim MasterRgn As Long
  164.  
  165. Private Declare Function GetTickCount Lib "kernel32" () As Long
  166.  
  167. 'To show the border
  168. Private Declare Function TransparentBlt Lib "msimg32.dll" (ByVal HDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean
  169. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  170. Private Const PATINVERT = &H5A0049        ' dest = pattern XOR dest
  171. Dim OrOrNot As Boolean
  172.  
  173.  
  174.  
  175. Private Sub Form_Load()
  176.  CboStyle.ListIndex = 0
  177. End Sub
  178.  
  179. 'Show the allowed difference in %
  180. Private Sub HscrPerc_Change()
  181.  Label1 = "Difference  " & HscrPerc.Value & "%"
  182. End Sub
  183.  
  184. 'Show the different Pictures
  185. 'You can make this with virtual Pic┤s
  186. 'but i wantet to see and show it
  187. Private Sub OptPic_Click(Index As Integer)
  188.  PicOrg.Visible = OptPic(0).Value
  189.  PicDest.Visible = OptPic(1).Value
  190.  PicBorder.Visible = OptPic(2).Value
  191. End Sub
  192.  
  193.  
  194. 'The Mainroutine
  195. Private Sub PicOrg_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  196.  Dim Xl As Long          'X position
  197.  Dim Yl As Long          'Y position
  198.  Dim Ro As Byte          'Red Origin
  199.  Dim Go As Byte          'Green Origin
  200.  Dim Bo As Byte          'Blue Origin
  201.  Dim Percent As Long     'Allowed RGB Difference in %
  202.  Dim Tmr As Long         'To test how long it takes
  203.  Dim a As Long           'For Our Black/White border
  204.  Dim B As Long           'For Our Black/White border
  205.  Dim col As Byte         'Holds the actual bordercolor
  206.  Dim colOld As Byte      'Also needed for bordercolor
  207.  Dim SC  As Byte         'For Color
  208.  
  209.  
  210.  'Remove Selection if there is one
  211.  PicOrg.Cls
  212.  OrOrNot = False
  213.  TmrShowBorder.Enabled = False
  214.  
  215.  'How long does it take ?
  216.  Tmr = GetTickCount
  217.  
  218.  'Get the Picture
  219.  Pic2Array PicOrg, Buf1
  220.  'Create a clear (Black) Picture
  221.  ReDim Buf2(UBound(Buf1, 1), UBound(Buf1, 2))
  222.   
  223.  'We wanted Brightness/Hue instead of RGB
  224.  'Normaly you should write a extra part for brightness/hue
  225.  'and dont do it by only changing to Monochrome/Hue and
  226.  'use the same routine we use for rgb
  227.  'But this is no complete Painter
  228.  'It┤s only a demo
  229.  Select Case CboStyle.ListIndex
  230.  Case 1
  231.   PicMonochrome Buf1()
  232.  Case 2
  233.   PicHue Buf1() 'Max 240 colors not 255 like normaly
  234.  End Select
  235.  
  236.  'Get the Original Pixel Colors
  237.  Ro = Buf1(X, PicOrg.Height - Y).rgbRed
  238.  Go = Buf1(X, PicOrg.Height - Y).rgbGreen
  239.  Bo = Buf1(X, PicOrg.Height - Y).rgbBlue
  240.  
  241.  'Show the color
  242.  ShpColor.FillColor = PicOrg.Point(X, Y)
  243.  
  244.  'Allowed RGB Difference
  245.  Percent = HscrPerc.Value
  246.  
  247.  If CboStyle.ListIndex = 0 Then
  248.  
  249.  'Move thru the Picture and make a blue Point
  250.  'everytime we find a similar color
  251.  For Xl = 0 To UBound(Buf1, 1)
  252.   For Yl = 0 To UBound(Buf1, 2)
  253.    If SimilarColor(Buf1(Xl, Yl).rgbRed, Buf1(Xl, Yl).rgbGreen, Buf1(Xl, Yl).rgbBlue, Ro, Go, Bo, Percent) Then
  254.     Buf2(Xl, Yl).rgbBlue = 255
  255.    End If
  256.   Next Yl
  257.  Next Xl
  258.  
  259.  ElseIf CboStyle.ListIndex < 3 Then
  260.  
  261.  'For Monochrome Pictures i only scann the Red channel
  262.   If CboStyle.ListIndex = 1 Then
  263.    Percent = Percent * 2.55
  264.   Else
  265.    '2.4 because Hue has only 240 colors
  266.    Percent = Percent * 2.4
  267.   End If
  268.   
  269.  For Xl = 0 To UBound(Buf1, 1)
  270.   For Yl = 0 To UBound(Buf1, 2)
  271.      If Abs(CLng(Buf1(Xl, Yl).rgbRed) - CLng(Ro)) <= Percent Then
  272.       Buf2(Xl, Yl).rgbBlue = 255
  273.      End If
  274.   Next Yl
  275.  Next Xl
  276.  
  277.  Else
  278.  'Color
  279.  'Didnt find any better Idea
  280.  Percent = Percent * 2.55 + 1
  281.  SC = SameColor(Ro, Go, Bo)
  282.  For Xl = 0 To UBound(Buf1, 1)
  283.   For Yl = 0 To UBound(Buf1, 2)
  284.    If SameColor(Buf1(Xl, Yl).rgbRed, Buf1(Xl, Yl).rgbGreen, Buf1(Xl, Yl).rgbBlue) = SC Then
  285.     If Abs(CLng(Buf1(Xl, Yl).rgbRed) - CLng(Ro)) < Percent Then
  286.       Buf2(Xl, Yl).rgbBlue = 255
  287.     End If
  288.    End If
  289.   Next Yl
  290.  Next Xl
  291.  
  292.  End If
  293.  
  294.  'Show the result
  295.  Array2Pic PicDest, Buf2
  296.  
  297.  'Start a Floodfill on the Original pixels position
  298.  'so we get a white part on the picture
  299.  'Picdest┤s fillcolor has to be White
  300.  FloodFill PicDest.HDC, X, Y, vbBlack
  301.  
  302.  'Make a new black Picture
  303.  ReDim Buf1(UBound(Buf2, 1), UBound(Buf2, 2))
  304.  'Get the floddfilled picture
  305.  Pic2Array PicDest, Buf2
  306.  
  307.  
  308.  
  309.  
  310.  'Draw the Border
  311.  col = &HFE
  312.  For Xl = 0 To UBound(Buf1, 1)
  313.   a = a + 1
  314.   If a = 5 Then
  315.    a = 0
  316.    col = Not col
  317.   End If
  318.   colOld = col
  319.   For Yl = 0 To UBound(Buf1, 2)
  320.    B = B + 1
  321.    If B = 5 Then
  322.     B = 0
  323.     col = Not col
  324.    End If
  325.    If Buf2(Xl, Yl).rgbRed = 255 Then
  326.     If Xl = 0 Or Xl = UBound(Buf1, 1) Or Yl = 0 Or Yl = UBound(Buf1, 2) Then
  327.      Buf1(Xl, Yl).rgbBlue = col
  328.      Buf1(Xl, Yl).rgbGreen = col
  329.      Buf1(Xl, Yl).rgbRed = col
  330.     ElseIf Buf2(Xl - 1, Yl).rgbRed = 0 Or Buf2(Xl + 1, Yl).rgbRed = 0 Or Buf2(Xl, Yl - 1).rgbRed = 0 Or Buf2(Xl, Yl + 1).rgbRed = 0 Then
  331.      Buf1(Xl, Yl).rgbBlue = col
  332.      Buf1(Xl, Yl).rgbGreen = col
  333.      Buf1(Xl, Yl).rgbRed = col
  334.     End If
  335.    End If
  336.   Next Yl
  337.   B = 0
  338.   col = colOld
  339.  Next Xl
  340.  
  341.  'Set the Borderpicture
  342.  Array2Pic PicBorder, Buf1
  343.  
  344.  
  345. '#########################################################
  346. 'If you want it as region then remove the '*'
  347. 'modRegionShape2.bas is Originaly from LaVolpe
  348. 'http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=54017&lngWId=1
  349. 'I only changed the some lines
  350. 'Old: If transColor = tgtColor No Region for tgtColor
  351. 'New: If transColor <> tgtColor Region for tgtColor
  352. 'Removed the SetWindowRgn
  353. 'Result is now the Region and not True/False
  354.  
  355. '*' MasterRgn = CreateShapedRegion2(PicDest.Picture.handle, Me.hwnd, &HFFFFFF)
  356. '#########################################################
  357.  
  358.  
  359.  
  360.  'Show how long it took
  361.  LblTime = "Created in " & GetTickCount - Tmr & "ms"
  362.  
  363.  'Show the Border
  364.  TmrShowBorder.Enabled = True
  365.  
  366. End Sub
  367.  
  368. 'Load a Picture
  369. Private Sub PicOrg_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
  370.  If Data.Files.Count Then
  371.   On Error Resume Next
  372.   Set PicOrg.Picture = LoadPicture(Data.Files(1))
  373.   If Err Then
  374.    MsgBox "Cant load " & vbCrLf & Data.Files(1), vbCritical + vbOKOnly
  375.    Err.Clear
  376.   Else
  377.    TmrShowBorder.Enabled = False
  378.    X = (PicOrg.Width + 25) * Screen.TwipsPerPixelX
  379.    If X < 490 * Screen.TwipsPerPixelX Then X = 490 * Screen.TwipsPerPixelX
  380.    Me.Width = X
  381.    Me.Height = (PicOrg.Height + 130) * Screen.TwipsPerPixelY
  382.    PicDest.Width = PicOrg.Width
  383.    PicDest.Height = PicOrg.Height
  384.    PicBorder.Width = PicOrg.Width
  385.    PicBorder.Height = PicOrg.Height
  386.  
  387.   End If
  388.  End If
  389. End Sub
  390.  
  391. Private Sub TmrShowBorder_Timer()
  392.  Dim Transcol As Long
  393.  
  394.  'Switch between Black or White as transparent color
  395.  If OrOrNot Then
  396.   Transcol = &HFFFFFF
  397.  End If
  398.  OrOrNot = Not OrOrNot
  399.  
  400.  'Copy the Border to our Origin
  401.  TransparentBlt PicOrg.HDC, 0, 0, PicOrg.Width, PicOrg.Height, PicBorder.HDC, 0, 0, PicOrg.Width, PicOrg.Height, Transcol
  402.  'Invert the Border so it looks like its moving
  403.  BitBlt PicBorder.HDC, 0, 0, PicBorder.Width, PicBorder.Height, PicBorder.HDC, 0, 0, PATINVERT
  404. End Sub
  405.  
  406.  
  407.