home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch12 / image2 / image.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-03-07  |  14.0 KB  |  384 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.DefaultExt = "*.BMP"
  138.     CommonDialog1.Action = 2
  139.     PictureName = CommonDialog1.FileName
  140.     SavePicture Picture1.Image, PictureName
  141. End Sub
  142. Private Sub Form_Unload(Cancel As Integer)
  143.     End
  144. End Sub
  145. Private Sub ProcessCustom_Click()
  146. Dim RedSum As Integer, GreenSum As Integer, BlueSum As Integer
  147. Dim i As Integer, j As Integer
  148. Dim red As Integer, green As Integer, blue As Integer
  149. Dim fi As Integer, fj As Integer
  150. Dim Offset As Integer
  151. Dim Weight As Single
  152.     Form2.Show 1    ' wait for user to define filter
  153.     If FilterCancel = True Then Exit Sub
  154.     T1 = Timer
  155.     If FilterNorm = 0 Then FilterNorm = 1
  156.     If Form2.Option1.Value Then
  157.         Offset = 1
  158.     Else
  159.         Offset = 2
  160.     End If
  161.     DoEvents
  162.     Form3.Show
  163.     Form3.Caption = "Processing pixels..."
  164.     Form3.Refresh
  165.     hBMP = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
  166.     hDestDC = CreateCompatibleDC(Picture1.hdc)
  167.     SelectObject hDestDC, hBMP
  168.     For i = Offset To y - Offset - 1
  169.         For j = Offset To x - Offset - 1
  170.             RedSum = 0: GreenSum = 0: BlueSum = 0
  171.             For fi = -Offset To Offset
  172.                 For fj = -Offset To Offset
  173.                     Weight = CustomFilter(fi + 2, fj + 2)
  174.                     RedSum = RedSum + ImagePixels(0, i + fi, j + fj) * Weight
  175.                     GreenSum = GreenSum + ImagePixels(1, i + fi, j + fj) * Weight
  176.                     BlueSum = BlueSum + ImagePixels(2, i + fi, j + fj) * Weight
  177.                 Next
  178.             Next
  179.             red = Abs(RedSum / FilterNorm + FilterBias)
  180.             green = Abs(GreenSum / FilterNorm + FilterBias)
  181.             blue = Abs(BlueSum / FilterNorm + FilterBias)
  182.             SetPixelV hDestDC, j, i, RGB(red, green, blue)
  183.         Next
  184.         Form3.ProgressBar1.Value = i * 100 / (y - 1)
  185.         DoEvents
  186.     Next
  187.     Form3.Hide
  188.     BitBlt Picture1.hdc, 1, 1, Picture1.ScaleWidth - 2, Picture1.ScaleHeight - 2, hDestDC, 1, 1, &HCC0020
  189.     Picture1.Refresh
  190.     Call DeleteDC(hDestDC)
  191.     Call DeleteObject(hBMP)
  192. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  193.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  194. End Sub
  195. Private Sub ProcessDiffuse_Click()
  196. Dim i As Long, j As Long
  197. Dim red As Integer, green As Integer, blue As Integer
  198. Dim Rx As Integer, Ry As Integer
  199.     T1 = Timer
  200.     Form3.Show
  201.     Form3.Caption = "Diffusing image ..."
  202.     Form3.Refresh
  203.     hBMP = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
  204.     hDestDC = CreateCompatibleDC(Picture1.hdc)
  205.     SelectObject hDestDC, hBMP
  206.     For i = 2 To y - 3
  207.         For j = 2 To x - 3
  208.             Rx = Rnd * 4 - 2
  209.             Ry = Rnd * 4 - 2
  210.             red = ImagePixels(0, i + Rx, j + Ry)
  211.             green = ImagePixels(1, i + Rx, j + Ry)
  212.             blue = ImagePixels(2, i + Rx, j + Ry)
  213.             SetPixelV hDestDC, j, i, RGB(red, green, blue)
  214.         Next
  215.         Form3.ProgressBar1.Value = i * 100 / (y - 1)
  216.         DoEvents
  217.     Next
  218.     Form3.Hide
  219.     BitBlt Picture1.hdc, 1, 1, Picture1.ScaleWidth - 2, Picture1.ScaleHeight - 2, hDestDC, 1, 1, &HCC0020
  220.     Picture1.Refresh
  221.     Call DeleteDC(hDestDC)
  222.     Call DeleteObject(hBMP)
  223. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  224.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  225. End Sub
  226. Private Sub ProcessEmboss_Click()
  227. Dim i As Long, j As Long
  228. Dim Dx As Integer, Dy As Integer
  229. Dim red As Integer, green As Integer, blue As Integer
  230.     Dx = 1
  231.     Dy = 1
  232.     Form3.Show
  233.     Form3.Caption = "Embossing image ..."
  234.     Form3.Refresh
  235.     hBMP = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
  236.     hDestDC = CreateCompatibleDC(Picture1.hdc)
  237.     SelectObject hDestDC, hBMP
  238.     T1 = Timer
  239.     For i = 1 To y - 2
  240.         For j = 1 To x - 2
  241.             red = Abs(ImagePixels(0, i, j) - ImagePixels(0, i + Dx, j + Dy) + 128)
  242.             green = Abs(ImagePixels(1, i, j) - ImagePixels(1, i + Dx, j + Dy) + 128)
  243.             blue = Abs(ImagePixels(2, i, j) - ImagePixels(2, i + Dx, j + Dy) + 128)
  244.             SetPixelV hDestDC, j, i, RGB(red, green, blue)
  245.         Next
  246.         Form3.ProgressBar1.Value = i * 100 / (y - 1)
  247.         DoEvents
  248.     Next
  249.     Form3.Hide
  250.     BitBlt Picture1.hdc, 1, 1, Picture1.ScaleWidth - 2, Picture1.ScaleHeight - 2, hDestDC, 1, 1, &HCC0020
  251.     Picture1.Refresh
  252.     Call DeleteDC(hDestDC)
  253.     Call DeleteObject(hBMP)
  254. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  255.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  256. End Sub
  257. Private Sub ProcessPixelize_Click()
  258. Dim i As Long, j As Long
  259. Dim Dx As Integer, Dy As Integer
  260. Dim red As Integer, green As Integer, blue As Integer
  261.     T1 = Timer
  262.     Picture1.FillStyle = vbSolid
  263.     For i = 1 To y / 3
  264.         For j = 1 To x / 3
  265.             Ypixel = Rnd * x + 4 - 2
  266.             Xpixel = Rnd * y + 4 - 2
  267.             R = Int(Rnd() * 3) + 2
  268.             red = ImagePixels(0, Xpixel, Ypixel)
  269.             green = ImagePixels(1, Xpixel, Ypixel)
  270.             blue = ImagePixels(2, Xpixel, Ypixel)
  271.             Picture1.FillColor = RGB(red, green, blue)
  272.             Picture1.Circle (Ypixel, Xpixel), R, RGB(red, green, blue)
  273.         Next
  274.         Picture1.Refresh
  275.     Next
  276.     Picture1.FillStyle = vbTransparent
  277. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  278.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  279. End Sub
  280. Private Sub ProcessSharpen_Click()
  281. Dim i As Long, j As Long
  282. Dim Dx As Integer, Dy As Integer
  283. Dim red As Integer, green As Integer, blue As Integer
  284.     Dx = 1: Dy = 1
  285.     T1 = Timer
  286.     Form3.Show
  287.     Form3.Caption = "Sharpening image ..."
  288.     Form3.Refresh
  289.     hBMP = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
  290.     hDestDC = CreateCompatibleDC(Picture1.hdc)
  291.     SelectObject hDestDC, hBMP
  292.     For i = 1 To y - 2
  293.         For j = 1 To x - 2
  294.             red = ImagePixels(0, i, j) + 0.5 * (ImagePixels(0, i, j) - ImagePixels(0, i - Dx, j - Dy))
  295.             green = ImagePixels(1, i, j) + 0.5 * (ImagePixels(1, i, j) - ImagePixels(1, i - Dx, j - Dy))
  296.             blue = ImagePixels(2, i, j) + 0.5 * (ImagePixels(2, i, j) - ImagePixels(2, i - Dx, j - Dy))
  297.             If red > 255 Then red = 255
  298.             If red < 0 Then red = 0
  299.             If green > 255 Then green = 255
  300.             If green < 0 Then green = 0
  301.             If blue > 255 Then blue = 255
  302.             If blue < 0 Then blue = 0
  303.             SetPixelV hDestDC, j, i, RGB(red, green, blue)
  304.         Next
  305.         Form3.ProgressBar1.Value = i * 100 / (y - 1)
  306.         DoEvents
  307.     Next
  308.     Form3.Hide
  309.     BitBlt Picture1.hdc, 1, 1, Picture1.ScaleWidth - 2, Picture1.ScaleHeight - 2, hDestDC, 1, 1, &HCC0020
  310.     Picture1.Refresh
  311.     Call DeleteDC(hDestDC)
  312.     Call DeleteObject(hBMP)
  313. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  314.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  315. End Sub
  316. Private Sub ProcessSmooth_Click()
  317. Dim i As Long, j As Long
  318. Dim red As Integer, green As Integer, blue As Integer
  319.     Form3.Show
  320.     Form3.Caption = "Smoothing image ..."
  321.     Form3.Refresh
  322.     T1 = Timer
  323.     hBMP = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
  324.     hDestDC = CreateCompatibleDC(Picture1.hdc)
  325.     SelectObject hDestDC, hBMP
  326.     For i = 1 To y - 2
  327.         For j = 1 To x - 2
  328.             red = ImagePixels(0, i - 1, j - 1) + ImagePixels(0, i - 1, j) + ImagePixels(0, i - 1, j + 1) + _
  329.             ImagePixels(0, i, j - 1) + ImagePixels(0, i, j) + ImagePixels(0, i, j + 1) + _
  330.             ImagePixels(0, i + 1, j - 1) + ImagePixels(0, i + 1, j) + ImagePixels(0, i + 1, j + 1)
  331.             
  332.             green = ImagePixels(1, i - 1, j - 1) + ImagePixels(1, i - 1, j) + ImagePixels(1, i - 1, j + 1) + _
  333.             ImagePixels(1, i, j - 1) + ImagePixels(1, i, j) + ImagePixels(1, i, j + 1) + _
  334.             ImagePixels(1, i + 1, j - 1) + ImagePixels(1, i + 1, j) + ImagePixels(1, i + 1, j + 1)
  335.             
  336.             blue = ImagePixels(2, i - 1, j - 1) + ImagePixels(2, i - 1, j) + ImagePixels(2, i - 1, j + 1) + _
  337.             ImagePixels(2, i, j - 1) + ImagePixels(2, i, j) + ImagePixels(2, i, j + 1) + _
  338.             ImagePixels(2, i + 1, j - 1) + ImagePixels(2, i + 1, j) + ImagePixels(2, i + 1, j + 1)
  339.             
  340.             SetPixelV hDestDC, j, i, RGB(red / 9, green / 9, blue / 9)
  341.         Next
  342.         Form3.ProgressBar1.Value = i * 100 / (y - 1)
  343.         DoEvents
  344.     Next
  345.     Form3.Hide
  346.     BitBlt Picture1.hdc, 1, 1, Picture1.ScaleWidth - 2, Picture1.ScaleHeight - 2, hDestDC, 1, 1, &HCC0020
  347.     Picture1.Refresh
  348.     Call DeleteDC(hDestDC)
  349.     Call DeleteObject(hBMP)
  350. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  351.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  352. End Sub
  353. Private Sub ProcessSolarize_Click()
  354. Dim i As Long, j As Long
  355. Dim red As Integer, green As Integer, blue As Integer
  356.     Form3.Show
  357.     Form3.Caption = "Solarizing image ..."
  358.     Form3.Refresh
  359.     T1 = Timer
  360.     hBMP = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
  361.     hDestDC = CreateCompatibleDC(Picture1.hdc)
  362.     SelectObject hDestDC, hBMP
  363.     For i = 1 To y - 2
  364.         For j = 1 To x - 2
  365.             red = ImagePixels(0, i, j)
  366.             green = ImagePixels(1, i, j)
  367.             blue = ImagePixels(2, i, j)
  368.             If ((red < 128) Or (red > 255)) Then red = 255 - red
  369.             If ((green < 128) Or (green > 255)) Then green = 255 - green
  370.             If ((blue < 128) Or (blue > 255)) Then blue = 255 - blue
  371.             SetPixelV hDestDC, j, i, RGB(red, green, blue)
  372.         Next
  373.         Form3.ProgressBar1.Value = i * 100 / (y - 1)
  374.         DoEvents
  375.     Next
  376.     Form3.Hide
  377.     BitBlt Picture1.hdc, 1, 1, Picture1.ScaleWidth - 2, Picture1.ScaleHeight - 2, hDestDC, 1, 1, &HCC0020
  378.     Picture1.Refresh
  379.     Call DeleteDC(hDestDC)
  380.     Call DeleteObject(hBMP)
  381. ' UNCOMMENT NEXT LINE TO TIME OPERATION
  382.     MsgBox "Processing completed in " & Format(Timer - T1, "##.000")
  383. End Sub
  384.