home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / ch_code / ch06 / image / image.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-20  |  7.8 KB  |  229 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
  3. Begin VB.Form Form1 
  4.    BorderStyle     =   5  'Sizable ToolWindow
  5.    Caption         =   "Image Processing"
  6.    ClientHeight    =   4515
  7.    ClientLeft      =   3090
  8.    ClientTop       =   2385
  9.    ClientWidth     =   5085
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    PaletteMode     =   1  'UseZOrder
  14.    ScaleHeight     =   301
  15.    ScaleMode       =   3  'Pixel
  16.    ScaleWidth      =   339
  17.    ShowInTaskbar   =   0   'False
  18.    Begin VB.PictureBox Picture1 
  19.       AutoRedraw      =   -1  'True
  20.       AutoSize        =   -1  'True
  21.       Height          =   4470
  22.       Left            =   0
  23.       ScaleHeight     =   294
  24.       ScaleMode       =   3  'Pixel
  25.       ScaleWidth      =   333
  26.       TabIndex        =   0
  27.       Top             =   0
  28.       Width           =   5055
  29.    End
  30.    Begin MSComDlg.CommonDialog CommonDialog1 
  31.       Left            =   6045
  32.       Top             =   105
  33.       _ExtentX        =   847
  34.       _ExtentY        =   847
  35.       FontSize        =   2.54052e-29
  36.    End
  37.    Begin VB.Menu FileMenu 
  38.       Caption         =   "File"
  39.       Begin VB.Menu FileOpen 
  40.          Caption         =   "Open Image"
  41.       End
  42.       Begin VB.Menu FileSave 
  43.          Caption         =   "Save Image"
  44.       End
  45.       Begin VB.Menu FileExit 
  46.          Caption         =   "Exit"
  47.       End
  48.    End
  49.    Begin VB.Menu ProcessMenu 
  50.       Caption         =   "Process Image"
  51.       Begin VB.Menu ProcessSmooth 
  52.          Caption         =   "Smooth"
  53.       End
  54.       Begin VB.Menu ProcessSharpen 
  55.          Caption         =   "Sharpen"
  56.       End
  57.       Begin VB.Menu ProcessEmboss 
  58.          Caption         =   "Emboss"
  59.       End
  60.       Begin VB.Menu ProcessDiffuse 
  61.          Caption         =   "Diffuse"
  62.       End
  63.       Begin VB.Menu separator 
  64.          Caption         =   "-"
  65.       End
  66.       Begin VB.Menu ProcessCustom 
  67.          Caption         =   "Custom Filter"
  68.       End
  69.    End
  70. Attribute VB_Name = "Form1"
  71. Attribute VB_GlobalNameSpace = False
  72. Attribute VB_Creatable = False
  73. Attribute VB_PredeclaredId = True
  74. Attribute VB_Exposed = False
  75. Private Sub FileExit_Click()
  76.     End
  77. End Sub
  78. Private Sub FileOpen_Click()
  79. Dim i, j
  80. Dim red As Integer, green As Integer, blue As Integer
  81. Dim pixel&
  82. Dim PictureName
  83. CommonDialog1.Action = 1
  84. PictureName = CommonDialog1.filename
  85. If PictureName = "" Then Exit Sub
  86. Picture1.Picture = LoadPicture(PictureName)
  87. Form1.Refresh
  88. X = Picture1.ScaleWidth
  89. Y = Picture1.ScaleHeight
  90. If X > 500 Or Y > 500 Then
  91.     MsgBox "Image too large to process. Please try loading a smaller image."
  92.     X = 0
  93.     Y = 0
  94.     Exit Sub
  95. End If
  96. Form1.Width = Form1.ScaleX(Picture1.Width + 6, vbPixels, vbTwips)
  97. Form1.Height = Form1.ScaleY(Picture1.Height + 30, vbPixels, vbTwips)
  98. Form1.Refresh
  99. Form3.Show
  100. Form3.Refresh
  101.     For i = 0 To Y - 1
  102.         For j = 0 To X - 1
  103.             pixel& = Form1.Picture1.Point(j, i)
  104.             red = pixel& Mod 256
  105.             green = ((pixel& And &HFF00) / 256&) Mod 256&
  106.             blue = (pixel& And &HFF0000) / 65536
  107.             ImagePixels(0, i, j) = red
  108.             ImagePixels(1, i, j) = green
  109.             ImagePixels(2, i, j) = blue
  110.         Next
  111.         Form3.ProgressBar1.Value = i * 100 / (Y - 1)
  112.     Next
  113.     Form3.Hide
  114. End Sub
  115. Private Sub FileSave_Click()
  116. Dim PictureName
  117.     CommonDialog1.Action = 2
  118.     PictureName = CommonDialog1.filename
  119.     SavePicture Picture1.IMAGE, PictureName
  120. End Sub
  121. Public Sub LoadImage_Click()
  122. End Sub
  123. Private Sub ProcessCustom_Click()
  124.     Dim RedSum, GreenSum, BlueSum
  125.     Dim red, green, blue
  126.     Dim fi, fj
  127.     Dim i, j
  128.     Dim Offset
  129.     Form2.Show 1    ' wait for user to define filter
  130.     If FilterCancel = True Then Exit Sub
  131.     If FilterNorm = 0 Then FilterNorm = 1
  132.     If Form2.Option1.Value Then
  133.         Offset = 1
  134.     Else
  135.         Offset = 2
  136.     End If
  137.     For i = Offset To Y - Offset - 1
  138.         For j = Offset To X - Offset - 1
  139.             RedSum = 0: GreenSum = 0: BlueSum = 0
  140.             For fi = -Offset To Offset
  141.                 For fj = -Offset To Offset
  142.                     RedSum = RedSum + ImagePixels(0, i + fi, j + fj) * CustomFilter(fi + 2, fj + 2)
  143.                     GreenSum = GreenSum + ImagePixels(1, i + fi, j + fj) * CustomFilter(fi + 2, fj + 2)
  144.                     BlueSum = BlueSum + ImagePixels(2, i + fi, j + fj) * CustomFilter(fi + 2, fj + 2)
  145.                 Next
  146.             Next
  147.             red = Abs(RedSum / FilterNorm + FilterBias)
  148.             green = Abs(GreenSum / FilterNorm + FilterBias)
  149.             blue = Abs(BlueSum / FilterNorm + FilterBias)
  150.             Picture1.PSet (j, i), RGB(red, green, blue)
  151.         Next
  152.         DoEvents
  153.     Next
  154. End Sub
  155. Private Sub ProcessDiffuse_Click()
  156. Dim i, j
  157. Dim Rx, Ry
  158. Dim red, green, blue
  159.     For i = 2 To Y - 3
  160.         For j = 2 To X - 3
  161.             Rx = Rnd * 4 - 2
  162.             Ry = Rnd * 4 - 2
  163.             red = ImagePixels(0, i + Rx, j + Ry)
  164.             green = ImagePixels(1, i + Rx, j + Ry)
  165.             blue = ImagePixels(2, i + Rx, j + Ry)
  166.             Picture1.PSet (j, i), RGB(red, green, blue)
  167.         Next
  168.         DoEvents
  169.     Next
  170. End Sub
  171. Private Sub ProcessEmboss_Click()
  172.     Dim i, j
  173.     Dim Dx, Dy
  174.     Dx = 1
  175.     Dy = 1
  176.     Dim red, green, blue
  177.     For i = 1 To Y - 2
  178.         For j = 1 To X - 2
  179.             red = Abs(ImagePixels(0, i, j) - ImagePixels(0, i + Dx, j + Dy) + 128)
  180.             green = Abs(ImagePixels(1, i, j) - ImagePixels(1, i + Dx, j + Dy) + 128)
  181.             blue = Abs(ImagePixels(2, i, j) - ImagePixels(2, i + Dx, j + Dy) + 128)
  182.             Picture1.PSet (j, i), RGB(red, green, blue)
  183.         Next
  184.         Picture1.Refresh
  185.     Next
  186. End Sub
  187. Private Sub ProcessSharpen_Click()
  188.     Dim i, j
  189.     Dim Dx, Dy
  190.     Dx = 1
  191.     Dy = 1
  192.     Dim red, green, blue
  193.     For i = 1 To Y - 2
  194.         For j = 1 To X - 2
  195.             red = ImagePixels(0, i, j) + 0.5 * (ImagePixels(0, i, j) - ImagePixels(0, i - Dx, j - Dy))
  196.             green = ImagePixels(1, i, j) + 0.5 * (ImagePixels(1, i, j) - ImagePixels(1, i - Dx, j - Dy))
  197.             blue = ImagePixels(2, i, j) + 0.5 * (ImagePixels(2, i, j) - ImagePixels(2, i - Dx, j - Dy))
  198.             If red > 255 Then red = 255
  199.             If red < 0 Then red = 0
  200.             If green > 255 Then green = 255
  201.             If green < 0 Then green = 0
  202.             If blue > 255 Then blue = 255
  203.             If blue < 0 Then blue = 0
  204.             Picture1.PSet (j, i), RGB(red, green, blue)
  205.         Next
  206.         Picture1.Refresh
  207.     Next
  208. End Sub
  209. Private Sub ProcessSmooth_Click()
  210. Dim i, j As Integer
  211.     For i = 1 To Y - 2
  212.         For j = 1 To X - 2
  213.             red = ImagePixels(0, i - 1, j - 1) + ImagePixels(0, i - 1, j) + ImagePixels(0, i - 1, j + 1) + _
  214.             ImagePixels(0, i, j - 1) + ImagePixels(0, i, j) + ImagePixels(0, i, j + 1) + _
  215.             ImagePixels(0, i + 1, j - 1) + ImagePixels(0, i + 1, j) + ImagePixels(0, i + 1, j + 1)
  216.             
  217.             green = ImagePixels(1, i - 1, j - 1) + ImagePixels(1, i - 1, j) + ImagePixels(1, i - 1, j + 1) + _
  218.             ImagePixels(1, i, j - 1) + ImagePixels(1, i, j) + ImagePixels(1, i, j + 1) + _
  219.             ImagePixels(1, i + 1, j - 1) + ImagePixels(1, i + 1, j) + ImagePixels(1, i + 1, j + 1)
  220.             
  221.             blue = ImagePixels(2, i - 1, j - 1) + ImagePixels(2, i - 1, j) + ImagePixels(2, i - 1, j + 1) + _
  222.             ImagePixels(2, i, j - 1) + ImagePixels(2, i, j) + ImagePixels(2, i, j + 1) + _
  223.             ImagePixels(2, i + 1, j - 1) + ImagePixels(2, i + 1, j) + ImagePixels(2, i + 1, j + 1)
  224.             Picture1.PSet (j, i), RGB(red / 9, green / 9, blue / 9)
  225.         Next
  226.         Picture1.Refresh
  227.     Next
  228. End Sub
  229.