home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH4 / SRC / FILTER.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-03  |  27.8 KB  |  864 lines

  1. VERSION 4.00
  2. Begin VB.Form FilterForm 
  3.    Caption         =   "Filters"
  4.    ClientHeight    =   4020
  5.    ClientLeft      =   840
  6.    ClientTop       =   1275
  7.    ClientWidth     =   8310
  8.    Height          =   4710
  9.    Left            =   780
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   268
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   554
  14.    Top             =   645
  15.    Width           =   8430
  16.    Begin VB.ComboBox FilterCombo 
  17.       Height          =   315
  18.       Left            =   3360
  19.       Style           =   2  'Dropdown List
  20.       TabIndex        =   11
  21.       Top             =   480
  22.       Width           =   1575
  23.    End
  24.    Begin VB.PictureBox ToSwin 
  25.       Height          =   3735
  26.       Left            =   5040
  27.       ScaleHeight     =   245
  28.       ScaleMode       =   3  'Pixel
  29.       ScaleWidth      =   197
  30.       TabIndex        =   9
  31.       Top             =   0
  32.       Width           =   3015
  33.       Begin VB.PictureBox ToPict 
  34.          AutoRedraw      =   -1  'True
  35.          AutoSize        =   -1  'True
  36.          Height          =   75
  37.          Left            =   0
  38.          MousePointer    =   2  'Cross
  39.          Picture         =   "FILTER.frx":0000
  40.          ScaleHeight     =   1
  41.          ScaleMode       =   3  'Pixel
  42.          ScaleWidth      =   1
  43.          TabIndex        =   10
  44.          Top             =   0
  45.          Width           =   75
  46.       End
  47.    End
  48.    Begin VB.PictureBox FromSwin 
  49.       Height          =   3735
  50.       Left            =   0
  51.       ScaleHeight     =   245
  52.       ScaleMode       =   3  'Pixel
  53.       ScaleWidth      =   197
  54.       TabIndex        =   7
  55.       Top             =   0
  56.       Width           =   3015
  57.       Begin VB.PictureBox FromPict 
  58.          AutoRedraw      =   -1  'True
  59.          AutoSize        =   -1  'True
  60.          Height          =   75
  61.          Left            =   0
  62.          MousePointer    =   2  'Cross
  63.          Picture         =   "FILTER.frx":0446
  64.          ScaleHeight     =   1
  65.          ScaleMode       =   3  'Pixel
  66.          ScaleWidth      =   1
  67.          TabIndex        =   8
  68.          Top             =   0
  69.          Width           =   75
  70.       End
  71.    End
  72.    Begin VB.VScrollBar ToVBar 
  73.       Height          =   3735
  74.       Left            =   8040
  75.       TabIndex        =   6
  76.       Top             =   0
  77.       Width           =   255
  78.    End
  79.    Begin VB.HScrollBar ToHBar 
  80.       Height          =   255
  81.       Left            =   5040
  82.       TabIndex        =   5
  83.       Top             =   3720
  84.       Width           =   3045
  85.    End
  86.    Begin VB.CommandButton CmdCopy 
  87.       Caption         =   "<-- Copy"
  88.       Enabled         =   0   'False
  89.       Height          =   495
  90.       Left            =   3720
  91.       TabIndex        =   4
  92.       Top             =   1920
  93.       Width           =   855
  94.    End
  95.    Begin VB.CommandButton CmdApply 
  96.       Caption         =   "Apply -->"
  97.       Enabled         =   0   'False
  98.       Height          =   495
  99.       Left            =   3720
  100.       TabIndex        =   3
  101.       Top             =   1080
  102.       Width           =   855
  103.    End
  104.    Begin VB.CheckBox ProgressCheck 
  105.       Caption         =   "Show Progress"
  106.       Height          =   255
  107.       Left            =   3360
  108.       TabIndex        =   2
  109.       Top             =   120
  110.       Width           =   1575
  111.    End
  112.    Begin VB.HScrollBar FromHBar 
  113.       Height          =   255
  114.       Left            =   0
  115.       TabIndex        =   1
  116.       Top             =   3720
  117.       Width           =   3045
  118.    End
  119.    Begin VB.VScrollBar FromVBar 
  120.       Height          =   3735
  121.       Left            =   3000
  122.       TabIndex        =   0
  123.       Top             =   0
  124.       Width           =   255
  125.    End
  126.    Begin MSComDlg.CommonDialog FileDialog 
  127.       Left            =   3960
  128.       Top             =   2880
  129.       _Version        =   65536
  130.       _ExtentX        =   847
  131.       _ExtentY        =   847
  132.       _StockProps     =   0
  133.       CancelError     =   -1  'True
  134.    End
  135.    Begin VB.Menu mnuFile 
  136.       Caption         =   "&File"
  137.       Begin VB.Menu mnuFileLoad 
  138.          Caption         =   "&Load..."
  139.          Shortcut        =   ^L
  140.       End
  141.       Begin VB.Menu mnuFileSave 
  142.          Caption         =   "&Save"
  143.          Enabled         =   0   'False
  144.          Shortcut        =   ^S
  145.       End
  146.       Begin VB.Menu mnuFileSaveAs 
  147.          Caption         =   "Save &As..."
  148.          Enabled         =   0   'False
  149.          Shortcut        =   ^A
  150.       End
  151.       Begin VB.Menu mnuFileSep1 
  152.          Caption         =   "-"
  153.       End
  154.       Begin VB.Menu mnuFileRevert 
  155.          Caption         =   "&Revert"
  156.          Enabled         =   0   'False
  157.          Shortcut        =   ^R
  158.       End
  159.       Begin VB.Menu mnuFileSep2 
  160.          Caption         =   "-"
  161.       End
  162.       Begin VB.Menu mnuFileExit 
  163.          Caption         =   "E&xit"
  164.       End
  165.    End
  166.    Begin VB.Menu mnuOpt 
  167.       Caption         =   "&Options"
  168.       Begin VB.Menu mnuOptShowFilter 
  169.          Caption         =   "&Show Filter..."
  170.          Shortcut        =   ^F
  171.       End
  172.    End
  173. Attribute VB_Name = "FilterForm"
  174. Attribute VB_Creatable = False
  175. Attribute VB_Exposed = False
  176. Option Explicit
  177. Dim SysPalSize As Integer
  178. Dim NumStaticColors As Integer
  179. Dim StaticColor1 As Integer
  180. Dim StaticColor2 As Integer
  181. Dim DataChanged As Boolean
  182. Dim FileLoaded As String
  183. Dim LogPal As Integer
  184. Dim palentry(0 To 255) As PALETTEENTRY
  185. Dim wid As Long
  186. Dim hgt As Long
  187. Dim bytes() As Byte
  188. ' ************************************************
  189. ' Put the names of the available filters in the
  190. ' filter combo box.
  191. ' ************************************************
  192. Sub LoadFilterChoices()
  193.     FilterCombo.AddItem "Average 3x3"
  194.     FilterCombo.AddItem "Low Pass 3x3"
  195.     FilterCombo.AddItem "Low Pass 5x5"
  196.     FilterCombo.AddItem "Low Pass 7x7"
  197.     FilterCombo.AddItem "High Pass 1"
  198.     FilterCombo.AddItem "High Pass 2"
  199.     FilterCombo.AddItem "High Pass 3"
  200.     FilterCombo.AddItem "High Pass 4"
  201.     FilterCombo.AddItem "Prewitt Up"
  202.     FilterCombo.AddItem "Prewitt Up-Right"
  203.     FilterCombo.AddItem "Prewitt Right"
  204.     FilterCombo.AddItem "Prewitt Down-Right"
  205.     FilterCombo.AddItem "Prewitt Down"
  206.     FilterCombo.AddItem "Prewitt Down-Left"
  207.     FilterCombo.AddItem "Prewitt Left"
  208.     FilterCombo.AddItem "Prewitt Up-Left"
  209.     FilterCombo.AddItem "Laplacian 1"
  210.     FilterCombo.AddItem "Laplacian 2"
  211.     FilterCombo.AddItem "Minimum 3x3"
  212.     FilterCombo.AddItem "Median 3x3"
  213.     FilterCombo.AddItem "Maximum 3x3"
  214.     FilterCombo.AddItem "Voting 3x3"
  215.     FilterCombo.AddItem "Emboss Up"
  216.     FilterCombo.AddItem "Emboss Up-Right"
  217.     FilterCombo.AddItem "Emboss Right"
  218.     FilterCombo.AddItem "Emboss Down-Right"
  219.     FilterCombo.AddItem "Emboss Down"
  220.     FilterCombo.AddItem "Emboss Down-Left"
  221.     FilterCombo.AddItem "Emboss Left"
  222.     FilterCombo.AddItem "Emboss Up-Left"
  223.     FilterCombo.AddItem "Erosion"
  224.     FilterCombo.AddItem "Dilation"
  225.     FilterCombo.AddItem "Erosion Outline"
  226.     FilterCombo.AddItem "Dilation Outline"
  227.     FilterCombo.ListIndex = 0
  228. End Sub
  229. ' ***********************************************
  230. ' Load the control's palette so it matches the
  231. ' the system palette. Remap any of the image's
  232. ' pixels that use static colors to non-static
  233. ' colors.
  234. ' Set the following module global variables.
  235. '   LogPal      Image logical palette handle.
  236. '   palentry()  Image logical palette entries.
  237. '   wid         Width of image.
  238. '   hgt         Height of image.
  239. '   bytes(1 To wid, 1 To hgt)
  240. '               Image pixel values.
  241. ' ***********************************************
  242. Sub MatchColorPalette(pic As Control)
  243. Dim sys(0 To 255) As PALETTEENTRY
  244. Dim i As Integer
  245. Dim bm As BITMAP
  246. Dim hbm As Integer
  247. Dim status As Long
  248. Dim x As Integer
  249. Dim y As Integer
  250. Dim clr As Integer
  251.     ' Make sure pic has the foreground palette.
  252.     pic.ZOrder
  253.     i = RealizePalette(pic.hdc)
  254.     DoEvents
  255.     ' Get the system palette entries.
  256.     i = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, sys(0))
  257.             
  258.     ' Make the logical palette as big as possible.
  259.     LogPal = pic.Picture.hPal
  260.     If ResizePalette(LogPal, SysPalSize) = 0 Then
  261.         Beep
  262.         MsgBox "Error resizing logical palette.", _
  263.             vbExclamation
  264.         Exit Sub
  265.     End If
  266.     ' Blank the non-static colors.
  267.     For i = 0 To StaticColor1
  268.         palentry(i) = sys(i)
  269.     Next i
  270.     For i = StaticColor1 + 1 To StaticColor2 - 1
  271.         With palentry(i)
  272.             .peRed = 0
  273.             .peGreen = 0
  274.             .peBlue = 0
  275.             .peFlags = PC_NOCOLLAPSE
  276.         End With
  277.     Next i
  278.     For i = StaticColor2 To 255
  279.         palentry(i) = sys(i)
  280.     Next i
  281.     i = SetPaletteEntries(LogPal, 0, SysPalSize, palentry(0))
  282.     ' Insert the non-static colors.
  283.     For i = StaticColor1 + 1 To StaticColor2 - 1
  284.         palentry(i) = sys(i)
  285.         palentry(i).peFlags = PC_NOCOLLAPSE
  286.     Next i
  287.     i = SetPaletteEntries(LogPal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  288.     ' Realize the new palette.
  289.     i = RealizePalette(pic.hdc)
  290.     ' Get the image pixels.
  291.     hbm = pic.Image
  292.     status = GetObject(hbm, BITMAP_SIZE, bm)
  293.     wid = bm.bmWidthBytes
  294.     hgt = bm.bmHeight
  295.     ReDim bytes(1 To wid, 1 To hgt)
  296.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  297.     ' Remap any pixels using static colors.
  298.     For y = 1 To hgt
  299.         For x = 1 To wid
  300.             clr = bytes(x, y)
  301.             If clr <= StaticColor1 Or clr >= StaticColor2 Then
  302.                 With sys(clr)
  303.                     bytes(x, y) = _
  304.                         NearestNonstaticColor( _
  305.                         .peRed, .peGreen, .peBlue)
  306.                 End With
  307.             End If
  308.         Next x
  309.     Next y
  310.     ' Update the image's pixel values.
  311.     status = SetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  312.     pic.Refresh
  313. End Sub
  314. ' ***********************************************
  315. ' Load the control's palette so the non-static
  316. ' colors are grays. Map the logical palette to
  317. ' match the system palette. Convert the image to
  318. ' use the non-static grays.
  319. ' Set the following module global variables.
  320. '   LogPal      Image logical palette handle.
  321. '   palentry()  Image logical palette entries.
  322. '   wid         Width of image.
  323. '   hgt         Height of image.
  324. '   bytes(1 To wid, 1 To hgt)
  325. '               Image pixel values.
  326. ' ***********************************************
  327. Sub MatchGrayPalette(pic As Control)
  328. Dim sys(0 To 255) As PALETTEENTRY
  329. Dim i As Integer
  330. Dim bm As BITMAP
  331. Dim hbm As Integer
  332. Dim status As Long
  333. Dim x As Integer
  334. Dim y As Integer
  335. Dim gray As Single
  336. Dim dgray As Single
  337. Dim c As Integer
  338. Dim clr As Integer
  339.     ' Make sure pic has the foreground palette.
  340.     pic.ZOrder
  341.     i = RealizePalette(pic.hdc)
  342.     DoEvents
  343.     ' Get the system palette entries.
  344.     i = GetSystemPaletteEntries(pic.hdc, 0, SysPalSize, sys(0))
  345.         
  346.     ' Get the image pixels.
  347.     hbm = pic.Image
  348.     status = GetObject(hbm, BITMAP_SIZE, bm)
  349.     wid = bm.bmWidthBytes
  350.     hgt = bm.bmHeight
  351.     ReDim bytes(1 To wid, 1 To hgt)
  352.     status = GetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  353.     ' Make the logical palette as big as possible.
  354.     LogPal = pic.Picture.hPal
  355.     If ResizePalette(LogPal, SysPalSize) = 0 Then
  356.         Beep
  357.         MsgBox "Error resizing logical palette.", _
  358.             vbExclamation
  359.         Exit Sub
  360.     End If
  361.     ' Blank the non-static colors.
  362.     For i = 0 To StaticColor1
  363.         palentry(i) = sys(i)
  364.     Next i
  365.     For i = StaticColor1 + 1 To StaticColor2 - 1
  366.         With palentry(i)
  367.             .peRed = 0
  368.             .peGreen = 0
  369.             .peBlue = 0
  370.             .peFlags = PC_NOCOLLAPSE
  371.         End With
  372.     Next i
  373.     For i = StaticColor2 To 255
  374.         palentry(i) = sys(i)
  375.     Next i
  376.     i = SetPaletteEntries(LogPal, 0, SysPalSize, palentry(0))
  377.     ' Insert the non-static grays.
  378.     gray = 0
  379.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  380.     For i = StaticColor1 + 1 To StaticColor2 - 1
  381.         c = gray
  382.         gray = gray + dgray
  383.         With palentry(i)
  384.             .peRed = c
  385.             .peGreen = c
  386.             .peBlue = c
  387.         End With
  388.     Next i
  389.     i = SetPaletteEntries(LogPal, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  390.     ' Recreate the image using the new colors.
  391.     For y = 1 To hgt
  392.         For x = 1 To wid
  393.             clr = bytes(x, y)
  394.             With sys(clr)
  395.                 c = (CInt(.peRed) + .peGreen + .peBlue) / 3
  396.             End With
  397.             bytes(x, y) = NearestNonstaticGray(c)
  398.         Next x
  399.     Next y
  400.     status = SetBitmapBits(hbm, wid * hgt, bytes(1, 1))
  401.     ' Realize the gray palette.
  402.     i = RealizePalette(pic.hdc)
  403.     pic.Refresh
  404. End Sub
  405. ' ************************************************
  406. ' Return the index of the nonstatic gray closest
  407. ' to the given value (assuming the non-static
  408. ' colors are a gray scale created by
  409. ' MatchGrayPalette).
  410. ' ************************************************
  411. Function NearestNonstaticGray(c As Integer) As Integer
  412. Dim dgray As Single
  413.     If c < 0 Then
  414.         c = 0
  415.     ElseIf c > 255 Then
  416.         c = 255
  417.     End If
  418.     dgray = 255 / (StaticColor2 - StaticColor1 - 2)
  419.     NearestNonstaticGray = c / dgray + StaticColor1 + 1
  420. End Function
  421. ' ************************************************
  422. ' Return the index of the nonstatic color closest
  423. ' to the given color value.
  424. ' ************************************************
  425. Function NearestNonstaticColor(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As Integer
  426. Dim best_i As Integer
  427. Dim best_dist As Long
  428. Dim dist As Long
  429. Dim dr As Long
  430. Dim dg As Long
  431. Dim db As Long
  432. Dim i As Integer
  433.     best_dist = 1000000
  434.     For i = StaticColor1 + 1 To StaticColor2 - 1
  435.         With palentry(i)
  436.             dr = r - .peRed
  437.             dg = g - .peGreen
  438.             db = b - .peBlue
  439.             dist = dr * dr + dg * dg + db * db
  440.         End With
  441.         If best_dist > dist Then
  442.             best_i = i
  443.             best_dist = dist
  444.         End If
  445.     Next i
  446.     NearestNonstaticColor = best_i
  447. End Function
  448. ' ***********************************************
  449. ' If the data has been modified, allow the user
  450. ' to save the changes or cancel the operation.
  451. ' Return True if:
  452. '   - The image data has not been changed since
  453. '       it was loaded.
  454. '   - The user saves the changes.
  455. '   - The user says not to save.
  456. ' Return False otherwise.
  457. ' ***********************************************
  458. Function DataSafe() As Boolean
  459.     DataSafe = True
  460.     ' This is done in a while loop in case the
  461.     ' user starts a save and then cancels.
  462.     Do While DataChanged
  463.         Select Case MsgBox("The data has been modified. Do you want to save the changes?", vbQuestion + vbYesNoCancel, "Data Modified")
  464.             Case vbYes
  465.                 If FileLoaded <> "" Then
  466.                     mnuFileSave_Click
  467.                 Else
  468.                     mnuFileSaveAs_Click
  469.                 End If
  470.                 DataSafe = Not DataChanged
  471.             
  472.             Case vbNo
  473.                 DataSafe = True
  474.                 Exit Do
  475.             Case vbCancel
  476.                 DataSafe = False
  477.                 Exit Do
  478.         End Select
  479.     Loop
  480. End Function
  481. ' ***********************************************
  482. ' Load the indicated file and prepare to work
  483. ' with its palette.
  484. ' ***********************************************
  485. Sub LoadFromPict(fname As String)
  486.     On Error GoTo LoadFileError
  487.     FromPict.Picture = LoadPicture(fname)
  488.         
  489.     FromPict.Move 0, 0
  490.     ToPict.Move 0, 0
  491.     MatchGrayPalette FromPict
  492.     ToPict.Picture = FromPict.Image
  493.     MatchGrayPalette ToPict
  494.     FromSwin.ZOrder
  495.     DoEvents
  496.     ToSwin.ZOrder
  497.     DoEvents
  498.     ResetScrollBars
  499.     FileLoaded = fname
  500.     Caption = "Filters [" & fname & "]"
  501.     mnuFileSave.Enabled = True
  502.     mnuFileSaveAs.Enabled = True
  503.     mnuFileRevert.Enabled = True
  504.     CmdApply.Enabled = True
  505.     CmdCopy.Enabled = True
  506.     DataChanged = False
  507.     Exit Sub
  508. LoadFileError:
  509.     Beep
  510.     MsgBox "Error loading file " & fname & "." & _
  511.         vbCrLf & Error$
  512.     Exit Sub
  513. End Sub
  514. ' ***********************************************
  515. ' Set the Max and LargeChange properties for the
  516. ' image scroll bars.
  517. ' ***********************************************
  518. Sub ResetScrollBars()
  519.     ' FromHBar.
  520.     FromHBar.value = 0
  521.     If FromSwin.ScaleWidth >= FromPict.Width Then
  522.         FromHBar.Enabled = False
  523.     Else
  524.         FromHBar.Max = FromPict.Width - FromSwin.ScaleWidth
  525.         FromHBar.LargeChange = FromSwin.ScaleWidth
  526.         FromHBar.Enabled = True
  527.     End If
  528.     ' FromVBar.
  529.     FromVBar.value = 0
  530.     If FromSwin.ScaleHeight >= FromPict.Height Then
  531.         FromVBar.Enabled = False
  532.     Else
  533.         FromVBar.Max = FromPict.Height - FromSwin.ScaleHeight
  534.         FromVBar.LargeChange = FromSwin.ScaleHeight
  535.         FromVBar.Enabled = True
  536.     End If
  537.     ' ToHBar.
  538.     ToHBar.value = 0
  539.     If FromSwin.ScaleWidth >= ToPict.Width Then
  540.         ToHBar.Enabled = False
  541.     Else
  542.         ToHBar.Max = ToPict.Width - ToSwin.ScaleWidth
  543.         ToHBar.LargeChange = ToSwin.ScaleWidth
  544.         ToHBar.Enabled = True
  545.     End If
  546.     ' ToVBar.
  547.     ToVBar.value = 0
  548.     If FromSwin.ScaleHeight >= ToPict.Height Then
  549.         ToVBar.Enabled = False
  550.     Else
  551.         ToVBar.Max = ToPict.Height - ToSwin.ScaleHeight
  552.         ToVBar.LargeChange = ToSwin.ScaleHeight
  553.         ToVBar.Enabled = True
  554.     End If
  555. End Sub
  556. ' ***********************************************
  557. ' Give the form and all the picture boxes an
  558. ' hourglass cursor.
  559. ' ***********************************************
  560. Sub WaitStart()
  561.     MousePointer = vbHourglass
  562.     FromPict.MousePointer = vbHourglass
  563.     ToPict.MousePointer = vbHourglass
  564.     DoEvents
  565. End Sub
  566. ' ***********************************************
  567. ' Restore the mouse pointers for the form and all
  568. ' the picture boxes.
  569. ' ***********************************************
  570. Sub WaitEnd()
  571.     MousePointer = vbDefault
  572.     FromPict.MousePointer = vbDefault
  573.     ToPict.MousePointer = vbDefault
  574. End Sub
  575. ' ************************************************
  576. ' Apply the selected filter to FromPict.
  577. ' ************************************************
  578. Private Sub CmdApply_Click()
  579. Static btn_caption As String
  580. Dim fil As New Filter
  581.     ' If the filter is running, stop it.
  582.     If OperationRunning Then
  583.         ' Set a flag so the filter will stop.
  584.         OperationRunning = False
  585.         
  586.         ' Disable this button.
  587.         CmdApply.Enabled = False
  588.         CmdApply.Caption = "Stopping"
  589.         Exit Sub
  590.     End If
  591.     ' Make sure something is selected.
  592.     If FilterCombo.ListIndex < 0 Then
  593.         Beep
  594.         Exit Sub
  595.     End If
  596.     ' Otherwise start the filter running.
  597.     OperationRunning = True
  598.     btn_caption = CmdApply.Caption
  599.     CmdApply.Caption = "Stop"
  600.     CmdCopy.Enabled = False
  601.     WaitStart
  602.     ' Initialize the filter.
  603.     fil.InitializeFilter FilterCombo.List(FilterCombo.ListIndex)
  604.     ' Apply the filter.
  605.     fil.ApplyFilter FromPict, ToPict, _
  606.         (ProgressCheck.value = vbChecked)
  607.     ' Reenable this button.
  608.     CmdApply.Caption = btn_caption
  609.     CmdApply.Enabled = True
  610.     CmdCopy.Enabled = True
  611.     OperationRunning = False
  612.     WaitEnd
  613.     ' This could have taken a long time so wake
  614.     ' the user up.
  615.     Beep
  616. End Sub
  617. ' ************************************************
  618. ' Copy ToPict into FromPict.
  619. ' ************************************************
  620. Private Sub CmdCopy_Click()
  621.     FromPict.PaintPicture ToPict.Image, 0, 0
  622.     DataChanged = True
  623. End Sub
  624. ' ***********************************************
  625. ' 1. Make sure we can handle palettes.
  626. ' 2. Find out how big the system palette is and how
  627. ' many static colors there are.
  628. ' 3. Load and display the system palette.
  629. ' ***********************************************
  630. Private Sub Form_Load()
  631.     ' Make sure the screen supports palettes.
  632.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  633.         Beep
  634.         MsgBox "This monitor does not support palettes.", _
  635.             vbCritical
  636.         End
  637.     End If
  638.     ' Get system palette size and # static colors.
  639.     SysPalSize = GetDeviceCaps(hdc, SIZEPALETTE)
  640.     NumStaticColors = GetDeviceCaps(hdc, NUMRESERVED)
  641.     StaticColor1 = NumStaticColors \ 2 - 1
  642.     StaticColor2 = SysPalSize - NumStaticColors \ 2
  643.     ' Remove the borders from the drawing areas.
  644.     FromPict.BorderStyle = vbTransparent
  645.     ToPict.BorderStyle = vbTransparent
  646.     ' Load the filter choices.
  647.     LoadFilterChoices
  648. End Sub
  649. ' ***********************************************
  650. ' Refuse to unload if there are unsaved changes.
  651. ' ***********************************************
  652. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  653.     Cancel = Not DataSafe()
  654. End Sub
  655. ' ***********************************************
  656. ' Make the picture as large as possible.
  657. ' ***********************************************
  658. Private Sub Form_Resize()
  659. Const GAP = 4
  660. Dim hgt As Single
  661. Dim wid As Single
  662.     If WindowState = vbMinimized Then Exit Sub
  663.     hgt = ScaleHeight - FromHBar.Height - 1
  664.     wid = (ScaleWidth - ProgressCheck.Width - 1 - _
  665.         2 * GAP - 2 * FromVBar.Width - 2) / 2
  666.     ' Place FromSwin and its scroll bars.
  667.     FromSwin.Move 0, 0, wid, hgt
  668.     FromVBar.Move _
  669.         FromSwin.Left + FromSwin.Width + 1, _
  670.         0, FromVBar.Width, hgt
  671.     FromHBar.Move _
  672.         FromSwin.Left, FromSwin.Height + 1, _
  673.         wid
  674.     ' Place the command buttons and stuff.
  675.     ProgressCheck.Left = (ScaleWidth - ProgressCheck.Width) / 2
  676.     FilterCombo.Left = (ScaleWidth - FilterCombo.Width) / 2
  677.     CmdApply.Left = (ScaleWidth - CmdApply.Width) / 2
  678.     CmdCopy.Left = (ScaleWidth - CmdCopy.Width) / 2
  679.     ' Place ToSwin and its scroll bars.
  680.     ToSwin.Move ProgressCheck.Left + _
  681.         ProgressCheck.Width + GAP, 0, wid, hgt
  682.     ToVBar.Move _
  683.         ToSwin.Left + ToSwin.Width + 1, _
  684.         0, ToVBar.Width, hgt
  685.     ToHBar.Move _
  686.         ToSwin.Left, ToSwin.Height + 1, _
  687.         wid
  688.     ' Set the scroll bar limits.
  689.     ResetScrollBars
  690. End Sub
  691. Private Sub Form_Unload(Cancel As Integer)
  692.     End
  693. End Sub
  694. ' ***********************************************
  695. ' Move FromPict within FromSwin.
  696. ' ***********************************************
  697. Private Sub FromHBar_Change()
  698.     FromPict.Left = -FromHBar.value
  699. End Sub
  700. ' ***********************************************
  701. ' Move FromPict within FromSwin.
  702. ' ***********************************************
  703. Private Sub FromHBar_Scroll()
  704.     FromPict.Left = -FromHBar.value
  705. End Sub
  706. ' ***********************************************
  707. ' Load a new image file.
  708. ' ***********************************************
  709. Private Sub mnuFileLoad_Click()
  710. Dim fname As String
  711.     ' Make sure any changes have been saved.
  712.     If Not DataSafe() Then Exit Sub
  713.     ' Allow the user to pick a file.
  714.     On Error Resume Next
  715.     FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  716.     FileDialog.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  717.     FileDialog.ShowOpen
  718.     If Err.Number = cdlCancel Then
  719.         Exit Sub
  720.     ElseIf Err.Number <> 0 Then
  721.         Beep
  722.         MsgBox "Error selecting file.", , vbExclamation
  723.         Exit Sub
  724.     End If
  725.     On Error GoTo 0
  726.     fname = Trim$(FileDialog.filename)
  727.     FileDialog.InitDir = Left$(fname, Len(fname) _
  728.         - Len(FileDialog.FileTitle) - 1)
  729.     ' Load the picture.
  730.     WaitStart
  731.     LoadFromPict fname
  732.     WaitEnd
  733. End Sub
  734. ' ***********************************************
  735. ' Reload the file.
  736. ' ***********************************************
  737. Private Sub mnuFileRevert_Click()
  738.     ' If the data has changed, get confirmation.
  739.     If DataChanged Then
  740.         If MsgBox("The data has been modified. Are you sure you want to remove the changes?", _
  741.             vbQuestion + vbYesNo) = vbNo Then _
  742.                 Exit Sub
  743.     End If
  744.     ' Reload the picture.
  745.     WaitStart
  746.     DoEvents
  747.     LoadFromPict FileLoaded
  748.     WaitEnd
  749. End Sub
  750. ' ***********************************************
  751. ' Save the image in the file from which it was
  752. ' loaded.
  753. ' ***********************************************
  754. Private Sub mnuFileSave_Click()
  755.     WaitStart
  756.     DoEvents
  757.     SaveFromPict FileLoaded
  758.     WaitEnd
  759. End Sub
  760. ' ***********************************************
  761. ' Save the image in a new file.
  762. ' ***********************************************
  763. Private Sub mnuFileSaveAs_Click()
  764. Dim fname As String
  765.     ' Allow the user to pick a file.
  766.     On Error Resume Next
  767.     FileDialog.filename = "*.BMP;*.ICO;*.RLE;*.WMF;*.DIB"
  768.     FileDialog.Flags = cdlOFNOverwritePrompt + _
  769.         cdlOFNHideReadOnly + cdlOFNPathMustExist
  770.     FileDialog.ShowSave
  771.     If Err.Number = cdlCancel Then
  772.         Exit Sub
  773.     ElseIf Err.Number <> 0 Then
  774.         Beep
  775.         MsgBox "Error selecting file.", , vbExclamation
  776.         Exit Sub
  777.     End If
  778.     On Error GoTo 0
  779.     fname = Trim$(FileDialog.filename)
  780.     FileDialog.InitDir = Left$(fname, Len(fname) _
  781.         - Len(FileDialog.FileTitle) - 1)
  782.     ' Save the picture.
  783.     WaitStart
  784.     DoEvents
  785.     SaveFromPict fname
  786.     WaitEnd
  787. End Sub
  788. ' ***********************************************
  789. ' Save the picture in the indicated file.
  790. ' ***********************************************
  791. Sub SaveFromPict(fname As String)
  792.     On Error GoTo SaveError
  793.     SavePicture FromPict.Image, fname
  794.     Caption = "Filters [" & fname & "]"
  795.     FileLoaded = fname
  796.     DataChanged = False
  797.     Exit Sub
  798. SaveError:
  799.     Beep
  800.     MsgBox "Error saving picture in file " & _
  801.         fname & "." & vbCrLf & vbCrLf & _
  802.         Error$, , vbExclamation
  803.     Exit Sub
  804. End Sub
  805. ' ***********************************************
  806. ' End the application. (See also the QueryUnload
  807. ' event.)
  808. ' ***********************************************
  809. Private Sub mnuFileExit_Click()
  810.     Unload Me
  811. End Sub
  812. ' ***********************************************
  813. ' Move FromPict within FromSwin.
  814. ' ***********************************************
  815. Private Sub FromVBar_Change()
  816.     FromPict.Top = -FromVBar.value
  817. End Sub
  818. ' ***********************************************
  819. ' Move FromPict within FromSwin.
  820. ' ***********************************************
  821. Private Sub FromVBar_Scroll()
  822.     FromPict.Top = -FromVBar.value
  823. End Sub
  824. ' ************************************************
  825. ' Display a message box showing the filter's
  826. ' components and weight.
  827. ' ************************************************
  828. Private Sub mnuOptShowFilter_Click()
  829. Dim fil As New Filter
  830.     ' Make sure something is selected.
  831.     If FilterCombo.ListIndex < 0 Then
  832.         Beep
  833.         Exit Sub
  834.     End If
  835.     ' Initialize the filter.
  836.     fil.InitializeFilter FilterCombo.List(FilterCombo.ListIndex)
  837.     ' Show the filter.
  838.     fil.ShowFilter
  839. End Sub
  840. ' ***********************************************
  841. ' Move ToPict within ToSwin.
  842. ' ***********************************************
  843. Private Sub ToHBar_Change()
  844.     ToPict.Left = -ToHBar.value
  845. End Sub
  846. ' ***********************************************
  847. ' Move ToPict within ToSwin.
  848. ' ***********************************************
  849. Private Sub ToHBar_Scroll()
  850.     ToPict.Left = -ToHBar.value
  851. End Sub
  852. ' ***********************************************
  853. ' Move ToPict within ToSwin.
  854. ' ***********************************************
  855. Private Sub ToVBar_Change()
  856.     ToPict.Top = -ToVBar.value
  857. End Sub
  858. ' ***********************************************
  859. ' Move ToPict within ToSwin.
  860. ' ***********************************************
  861. Private Sub ToVBar_Scroll()
  862.     ToPict.Top = -ToVBar.value
  863. End Sub
  864.