home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch12 / image1 / image.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-03-07  |  11.2 KB  |  319 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#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.       _Version        =   393216
  36.       FontSize        =   1.17485e-38
  37.    End
  38.    Begin VB.Menu FileMenu 
  39.       Caption         =   "File"
  40.       Begin VB.Menu FileOpen 
  41.          Caption         =   "Open Image"
  42.       End
  43.       Begin VB.Menu FileSave 
  44.          Caption         =   "Save Image"
  45.       End
  46.       Begin VB.Menu FileExit 
  47.          Caption         =   "Exit"
  48.       End
  49.    End
  50.    Begin VB.Menu ProcessMenu 
  51.       Caption         =   "Process Image"
  52.       Begin VB.Menu ProcessSmooth 
  53.          Caption         =   "Smooth"
  54.       End
  55.       Begin VB.Menu ProcessSharpen 
  56.          Caption         =   "Sharpen"
  57.       End
  58.       Begin VB.Menu ProcessEmboss 
  59.          Caption         =   "Emboss"
  60.       End
  61.       Begin VB.Menu ProcessDiffuse 
  62.          Caption         =   "Diffuse"
  63.       End
  64.       Begin VB.Menu ProcessPixelize 
  65.          Caption         =   "Pixelize"
  66.       End
  67.       Begin VB.Menu ProcessSolarize 
  68.          Caption         =   "Solarize"
  69.       End
  70.       Begin VB.Menu separator 
  71.          Caption         =   "-"
  72.       End
  73.       Begin VB.Menu ProcessCustom 
  74.          Caption         =   "Custom Filter"
  75.       End
  76.    End
  77. Attribute VB_Name = "Form1"
  78. Attribute VB_GlobalNameSpace = False
  79. Attribute VB_Creatable = False
  80. Attribute VB_PredeclaredId = True
  81. Attribute VB_Exposed = False
  82. Private Sub FileExit_Click()
  83.     End
  84. End Sub
  85. Private Sub FileOpen_Click()
  86. Dim i As Long, j As Long
  87. Dim red As Integer, green As Integer, blue As Integer
  88. Dim pixel As Long
  89. Dim PictureName As String
  90. CommonDialog1.InitDir = App.Path
  91. CommonDialog1.FileName = ""
  92. CommonDialog1.Filter = "Images|*.GIF;*.BMP;*.JPG|All Files|*.*"
  93. CommonDialog1.Action = 1
  94. PictureName = CommonDialog1.FileName
  95. If PictureName = "" Then Exit Sub
  96. On Error GoTo BadImageType
  97. Picture1.Picture = LoadPicture(PictureName)
  98. Form1.Refresh
  99. X = Picture1.ScaleWidth
  100. Y = Picture1.ScaleHeight
  101. If X > 800 Or Y > 800 Then
  102.     MsgBox "Image too large to process. Please try loading a smaller image."
  103.     X = 0
  104.     Y = 0
  105.     Exit Sub
  106. End If
  107. Form1.Width = Form1.ScaleX(Picture1.Width + 6, vbPixels, vbTwips)
  108. Form1.Height = Form1.ScaleY(Picture1.Height + 30, vbPixels, vbTwips)
  109. Form1.Refresh
  110. Form3.Show
  111. Form3.Caption = "Reading pixels"
  112. Form3.Refresh
  113.     For i = 0 To Y - 1
  114.         For j = 0 To X - 1
  115.             pixel = GetPixel(Form1.Picture1.hdc, j, i)
  116.             red = pixel Mod 256
  117.             green = ((pixel And &HFF00) / 256&) Mod 256&
  118.             blue = (pixel And &HFF0000) / 65536
  119.             ImagePixels(0, i, j) = red
  120.             ImagePixels(1, i, j) = green
  121.             ImagePixels(2, i, j) = blue
  122.         Next
  123.         Form3.ProgressBar1.Value = i * 100 / (Y - 1)
  124.         DoEvents
  125.     Next
  126.     Form3.Hide
  127.     Me.Caption = CommonDialog1.FileTitle & "    (" & X & ", " & Y & ")"
  128.     Exit Sub
  129. BadImageType:
  130.     MsgBox Err.Description
  131.     X = 0
  132.     Y = 0
  133.     Exit Sub
  134. End Sub
  135. Private Sub FileSave_Click()
  136. Dim PictureName
  137.     CommonDialog1.Action = 2
  138.     PictureName = CommonDialog1.FileName
  139.     SavePicture Picture1.Image, PictureName
  140. End Sub
  141. Private Sub ProcessCustom_Click()
  142. Dim RedSum As Integer, GreenSum As Integer, BlueSum As Integer
  143. Dim i As Integer, j As Integer
  144. Dim red As Integer, green As Integer, blue As Integer
  145. Dim fi As Integer, fj As Integer
  146. Dim Offset As Integer
  147. Dim Weight As Single
  148.     Form2.Show 1    ' wait for user to define filter
  149.     If FilterCancel = True Then Exit Sub
  150.     T1 = Timer
  151.     If FilterNorm = 0 Then FilterNorm = 1
  152.     If Form2.Option1.Value Then
  153.         Offset = 1
  154.     Else
  155.         Offset = 2
  156.     End If
  157.     DoEvents
  158.     Form3.Show
  159.     Form3.Caption = "Filtering image ..."
  160.     Form3.Refresh
  161.     For i = Offset To Y - Offset - 1
  162.         For j = Offset To X - Offset - 1
  163.             RedSum = 0: GreenSum = 0: BlueSum = 0
  164.             For fi = -Offset To Offset
  165.                 For fj = -Offset To Offset
  166.                     Weight = CustomFilter(fi + 2, fj + 2)
  167.                     RedSum = RedSum + ImagePixels(0, i + fi, j + fj) * Weight
  168.                     GreenSum = GreenSum + ImagePixels(1, i + fi, j + fj) * Weight
  169.                     BlueSum = BlueSum + ImagePixels(2, i + fi, j + fj) * Weight
  170.                 Next
  171.             Next
  172.             red = Abs(RedSum / FilterNorm + FilterBias)
  173.             green = Abs(GreenSum / FilterNorm + FilterBias)
  174.             blue = Abs(BlueSum / FilterNorm + FilterBias)
  175.             SetPixelV Picture1.hdc, j, i, RGB(red, green, blue)
  176.         Next
  177.         Form3.ProgressBar1.Value = i * 100 / (Y - 1)
  178.         DoEvents
  179.     Next
  180.     Form3.Hide
  181.     Picture1.Refresh
  182. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  183.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  184. End Sub
  185. Private Sub ProcessDiffuse_Click()
  186. Dim i As Long, j As Long
  187. Dim red As Integer, green As Integer, blue As Integer
  188. Dim Rx As Integer, Ry As Integer
  189.     T1 = Timer
  190.     For i = 2 To Y - 3
  191.         For j = 2 To X - 3
  192.             Rx = Rnd * 4 - 2
  193.             Ry = Rnd * 4 - 2
  194.             red = ImagePixels(0, i + Rx, j + Ry)
  195.             green = ImagePixels(1, i + Rx, j + Ry)
  196.             blue = ImagePixels(2, i + Rx, j + Ry)
  197.             SetPixelV Picture1.hdc, j, i, RGB(red, green, blue)
  198.         Next
  199.         DoEvents
  200.     Next
  201.     Picture1.Refresh
  202. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  203.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  204. End Sub
  205. Private Sub ProcessEmboss_Click()
  206. Dim i As Long, j As Long
  207. Const Dx As Integer = 1
  208. Const Dy As Integer = 1
  209. Dim red As Integer, green As Integer, blue As Integer
  210.     T1 = Timer
  211.     For i = 1 To Y - 2
  212.         For j = 1 To X - 2
  213.             red = Abs(ImagePixels(0, i, j) - ImagePixels(0, i + Dx, j + Dy) + 128)
  214.             green = Abs(ImagePixels(1, i, j) - ImagePixels(1, i + Dx, j + Dy) + 128)
  215.             blue = Abs(ImagePixels(2, i, j) - ImagePixels(2, i + Dx, j + Dy) + 128)
  216.             SetPixelV Picture1.hdc, j, i, RGB(red, green, blue)
  217.         Next
  218.         Picture1.Refresh
  219.     Next
  220.     Picture1.Refresh
  221. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  222.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  223. End Sub
  224. Private Sub ProcessPixelize_Click()
  225. Dim i As Long, j As Long
  226. Const Dx As Integer = 1
  227. Const Dy As Integer = 1
  228. Dim red As Integer, green As Integer, blue As Integer
  229.     T1 = Timer
  230.     Picture1.FillStyle = vbSolid
  231.     For i = 1 To Y / 3
  232.         For j = 1 To X / 3
  233.             Ypixel = Rnd * X + 4 - 2
  234.             Xpixel = Rnd * Y + 4 - 2
  235.             R = Int(Rnd() * 3) + 2
  236.             red = ImagePixels(0, Xpixel, Ypixel)
  237.             green = ImagePixels(1, Xpixel, Ypixel)
  238.             blue = ImagePixels(2, Xpixel, Ypixel)
  239.             Picture1.FillColor = RGB(red, green, blue)
  240.             Picture1.Circle (Ypixel, Xpixel), R, RGB(red, green, blue)
  241.         Next
  242.         Picture1.Refresh
  243.     Next
  244.     Picture1.FillStyle = vbTransparent
  245. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  246.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  247. End Sub
  248. Private Sub ProcessSharpen_Click()
  249. Dim i As Long, j As Long
  250. Const Dx As Integer = 1
  251. Const Dy As Integer = 1
  252. Dim red As Integer, green As Integer, blue As Integer
  253.     T1 = Timer
  254.     For i = 1 To Y - 2
  255.         For j = 1 To X - 2
  256.             red = ImagePixels(0, i, j) + 0.5 * (ImagePixels(0, i, j) - ImagePixels(0, i - Dx, j - Dy))
  257.             green = ImagePixels(1, i, j) + 0.5 * (ImagePixels(1, i, j) - ImagePixels(1, i - Dx, j - Dy))
  258.             blue = ImagePixels(2, i, j) + 0.5 * (ImagePixels(2, i, j) - ImagePixels(2, i - Dx, j - Dy))
  259.             If red > 255 Then red = 255
  260.             If red < 0 Then red = 0
  261.             If green > 255 Then green = 255
  262.             If green < 0 Then green = 0
  263.             If blue > 255 Then blue = 255
  264.             If blue < 0 Then blue = 0
  265.             SetPixelV Picture1.hdc, j, i, RGB(red, green, blue)
  266.         Next
  267.         Picture1.Refresh
  268.     Next
  269.     Picture1.Refresh
  270. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  271.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  272. End Sub
  273. Private Sub ProcessSmooth_Click()
  274. Dim i As Long, j As Long
  275. Dim red As Integer, green As Integer, blue As Integer
  276.     T1 = Timer
  277.     For i = 1 To Y - 2
  278.         For j = 1 To X - 2
  279.             red = ImagePixels(0, i - 1, j - 1) + ImagePixels(0, i - 1, j) + ImagePixels(0, i - 1, j + 1) + _
  280.             ImagePixels(0, i, j - 1) + ImagePixels(0, i, j) + ImagePixels(0, i, j + 1) + _
  281.             ImagePixels(0, i + 1, j - 1) + ImagePixels(0, i + 1, j) + ImagePixels(0, i + 1, j + 1)
  282.             
  283.             green = ImagePixels(1, i - 1, j - 1) + ImagePixels(1, i - 1, j) + ImagePixels(1, i - 1, j + 1) + _
  284.             ImagePixels(1, i, j - 1) + ImagePixels(1, i, j) + ImagePixels(1, i, j + 1) + _
  285.             ImagePixels(1, i + 1, j - 1) + ImagePixels(1, i + 1, j) + ImagePixels(1, i + 1, j + 1)
  286.             
  287.             blue = ImagePixels(2, i - 1, j - 1) + ImagePixels(2, i - 1, j) + ImagePixels(2, i - 1, j + 1) + _
  288.             ImagePixels(2, i, j - 1) + ImagePixels(2, i, j) + ImagePixels(2, i, j + 1) + _
  289.             ImagePixels(2, i + 1, j - 1) + ImagePixels(2, i + 1, j) + ImagePixels(2, i + 1, j + 1)
  290.             
  291.             SetPixelV Picture1.hdc, j, i, RGB(red / 9, green / 9, blue / 9)
  292.         Next
  293.         Picture1.Refresh
  294.     Next
  295.     Picture1.Refresh
  296. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  297.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  298. End Sub
  299. Private Sub ProcessSolarize_Click()
  300. Dim i As Long, j As Long
  301. Dim red As Integer, green As Integer, blue As Integer
  302.     T1 = Timer
  303.     For i = 1 To Y - 2
  304.         For j = 1 To X - 2
  305.             red = ImagePixels(0, i, j)
  306.             green = ImagePixels(1, i, j)
  307.             blue = ImagePixels(2, i, j)
  308.             If ((red < 128) Or (red > 255)) Then red = 255 - red
  309.             If ((green < 128) Or (green > 255)) Then green = 255 - green
  310.             If ((blue < 128) Or (blue > 255)) Then blue = 255 - blue
  311.             SetPixelV Picture1.hdc, j, i, RGB(red, green, blue)
  312.         Next
  313.         Picture1.Refresh
  314.     Next
  315.     Picture1.Refresh
  316. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  317.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  318. End Sub
  319.