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

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form FrmPicRepair 
  4.    BorderStyle     =   1  'Fest Einfach
  5.    Caption         =   "Repair Pictures"
  6.    ClientHeight    =   11340
  7.    ClientLeft      =   45
  8.    ClientTop       =   435
  9.    ClientWidth     =   8940
  10.    LinkTopic       =   "Form1"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   756
  14.    ScaleMode       =   3  'Pixel
  15.    ScaleWidth      =   596
  16.    StartUpPosition =   3  'Windows-Standard
  17.    Begin VB.ComboBox CboColor 
  18.       Height          =   315
  19.       ItemData        =   "Form1.frx":0000
  20.       Left            =   2040
  21.       List            =   "Form1.frx":0013
  22.       TabIndex        =   7
  23.       Text            =   "Combo1"
  24.       Top             =   120
  25.       Width           =   3255
  26.    End
  27.    Begin VB.HScrollBar ScrSim 
  28.       Height          =   255
  29.       Left            =   2640
  30.       Max             =   255
  31.       TabIndex        =   5
  32.       Top             =   840
  33.       Width           =   2655
  34.    End
  35.    Begin VB.CommandButton CmdRepCirc 
  36.       Caption         =   "Circular Repair"
  37.       Height          =   255
  38.       Left            =   120
  39.       TabIndex        =   4
  40.       Top             =   840
  41.       Width           =   1575
  42.    End
  43.    Begin MSComDlg.CommonDialog cmdlg 
  44.       Left            =   6840
  45.       Top             =   6720
  46.       _ExtentX        =   847
  47.       _ExtentY        =   847
  48.       _Version        =   393216
  49.    End
  50.    Begin VB.CommandButton CmdLoad 
  51.       Caption         =   "Load Picture"
  52.       Height          =   255
  53.       Left            =   120
  54.       TabIndex        =   2
  55.       Top             =   120
  56.       Width           =   1575
  57.    End
  58.    Begin VB.CommandButton CmdRepair 
  59.       Caption         =   "Linear Repair"
  60.       Height          =   255
  61.       Left            =   120
  62.       TabIndex        =   1
  63.       Top             =   480
  64.       Width           =   1575
  65.    End
  66.    Begin VB.PictureBox PicOrg 
  67.       AutoSize        =   -1  'True
  68.       BackColor       =   &H00000000&
  69.       BorderStyle     =   0  'Kein
  70.       Height          =   10050
  71.       Left            =   120
  72.       Picture         =   "Form1.frx":006F
  73.       ScaleHeight     =   670
  74.       ScaleMode       =   3  'Pixel
  75.       ScaleWidth      =   580
  76.       TabIndex        =   0
  77.       Top             =   1200
  78.       Width           =   8700
  79.    End
  80.    Begin VB.Label LblDamaged 
  81.       Alignment       =   2  'Zentriert
  82.       Height          =   255
  83.       Left            =   5640
  84.       TabIndex        =   8
  85.       Top             =   720
  86.       Width           =   2895
  87.    End
  88.    Begin VB.Label Label2 
  89.       Alignment       =   2  'Zentriert
  90.       Caption         =   "Difference 0"
  91.       Height          =   255
  92.       Left            =   2640
  93.       TabIndex        =   6
  94.       Top             =   600
  95.       Width           =   2655
  96.    End
  97.    Begin VB.Shape Shape1 
  98.       FillStyle       =   0  'Ausgefⁿllt
  99.       Height          =   495
  100.       Left            =   2040
  101.       Top             =   600
  102.       Width           =   495
  103.    End
  104.    Begin VB.Label Label1 
  105.       Alignment       =   2  'Zentriert
  106.       Caption         =   "Select Transparent color by Clicking on the Picture"
  107.       Height          =   435
  108.       Left            =   5640
  109.       TabIndex        =   3
  110.       Top             =   120
  111.       Width           =   2775
  112.    End
  113. End
  114. Attribute VB_Name = "FrmPicRepair"
  115. Attribute VB_GlobalNameSpace = False
  116. Attribute VB_Creatable = False
  117. Attribute VB_PredeclaredId = True
  118. Attribute VB_Exposed = False
  119. ' Simple Image Repair
  120. ' by Scythe
  121. Option Explicit
  122.  
  123. Private Type BITMAP
  124.     bmType As Long
  125.     bmWidth As Long
  126.     bmHeight As Long
  127.     bmWidthBytes As Long
  128.     bmPlanes As Integer
  129.     bmBitsPixel As Integer
  130.     bmBits As Long
  131. End Type
  132.  
  133. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
  134. Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  135. Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long
  136. Private Type RGBQUAD
  137.     rgbBlue As Byte
  138.     rgbgreen As Byte
  139.     rgbRed As Byte
  140.     rgbReserved As Byte
  141. End Type
  142.  
  143. Private TR As Byte
  144. Private TB As Byte
  145. Private TG As Byte
  146.  
  147. Private PicInfo As BITMAP
  148. Private PicAr1() As RGBQUAD
  149. Private MaskAR() As Boolean
  150.  
  151. Private Sub CboColor_Click()
  152.  
  153. Dim ShowThem As Boolean
  154.     If CboColor.ListIndex <> 4 Then ShowThem = True
  155.     Label2.Visible = ShowThem
  156.     ScrSim.Visible = ShowThem
  157.     
  158. End Sub
  159.  
  160. 'Load Picture and resize Form
  161. Private Sub CmdLoad_Click()
  162.  
  163. Dim X As Long
  164.     cmdlg.Filter = "Pictures;*.bmp,*.gif,*.jpg"
  165.  
  166.     cmdlg.ShowOpen
  167.     If cmdlg.filename = "" Then Exit Sub
  168.     Set PicOrg = LoadPicture(cmdlg.filename)
  169.     X = PicOrg.Width + PicOrg.Left + 10
  170.     If X < 410 Then X = 410
  171.     Me.Width = X * Screen.TwipsPerPixelX
  172.     Me.Height = (PicOrg.Height + PicOrg.Top + 50) * Screen.TwipsPerPixelY
  173.  
  174. End Sub
  175.  
  176. 'Linear Repair
  177. Private Sub CmdRepair_Click()
  178.  
  179. Dim X As Long
  180. Dim Y As Long
  181. Dim r As Long
  182. Dim g As Long
  183. Dim b As Long
  184. Dim a As Long
  185. Dim ctr As Long
  186. Dim ctrOld As Long
  187.     
  188. 'Get the Picture as array (for faster operation)
  189.     Pic2Array PicOrg, PicAr1
  190.  
  191. 'Create a Maskpicture
  192.     PicDifference
  193.     
  194.     Do
  195.     ctr = 0
  196. 'Move Thru the Picture
  197.     For X = 1 To PicOrg.Width - 2
  198.         For Y = 1 To PicOrg.Height - 2
  199. 'scann for transparent color
  200.             If MaskAR(X, Y) Then
  201. 'We found a transparent pixel
  202.                 ctr = ctr + 1
  203. 'Check if there is a Colored Pixel
  204. 'If yes then add the color
  205.                 a = 0
  206.                 If MaskAR(X - 1, Y) = False Then
  207.                     a = a + 1
  208.                     r = PicAr1(X - 1, Y).rgbRed
  209.                     g = PicAr1(X - 1, Y).rgbgreen
  210.                     b = PicAr1(X - 1, Y).rgbBlue
  211.                 End If
  212.                 If MaskAR(X + 1, Y) = False Then
  213.                     a = a + 1
  214.                     r = r + PicAr1(X + 1, Y).rgbRed
  215.                     g = g + PicAr1(X + 1, Y).rgbgreen
  216.                     b = b + PicAr1(X + 1, Y).rgbBlue
  217.                 End If
  218.                 If MaskAR(X, Y - 1) = False Then
  219.                     a = a + 1
  220.                     r = r + PicAr1(X, Y - 1).rgbRed
  221.                     g = g + PicAr1(X, Y - 1).rgbgreen
  222.                     b = b + PicAr1(X, Y - 1).rgbBlue
  223.                 End If
  224.                 If MaskAR(X, Y + 1) = False Then
  225.                     a = a + 1
  226.                     r = r + PicAr1(X, Y + 1).rgbRed
  227.                     g = g + PicAr1(X, Y + 1).rgbgreen
  228.                     b = b + PicAr1(X, Y + 1).rgbBlue
  229.                 End If
  230. 'If we have 2 or mor colored pixels arround then
  231. 'fill the transparent pixel with a coombination
  232.                 If a > 1 Then
  233.                     r = r / a
  234.                     g = g / a
  235.                     b = b / a
  236.                     PicAr1(X, Y).rgbRed = CByte(r)
  237.                     PicAr1(X, Y).rgbgreen = CByte(g)
  238.                     PicAr1(X, Y).rgbBlue = CByte(b)
  239.                     MaskAR(X, Y) = False
  240. 'we removed a transparent pixel
  241.                     ctr = ctr - 1
  242.                 End If
  243.                 r = 0
  244.                 g = 0
  245.                 b = 0
  246.             End If
  247.         Next Y
  248.     Next X
  249.     
  250. 'Check if we did a new scan without a new result
  251.     If ctr <> ctrOld Then
  252.         ctrOld = ctr
  253.         Else
  254.         If ctr > 0 Then MsgBox "Some parts could not be fixed"
  255.         Exit Do
  256.     End If
  257.     
  258. 'if there is still a transparent pixel open RESTART
  259. Loop Until ctr < 1
  260. Array2Pic PicOrg, PicAr1
  261. PicOrg.Refresh
  262.  
  263. End Sub
  264.  
  265. 'repair the Picture with a Circle Blur Fill
  266. 'thru the circle we go from the border to the middle
  267. 'In most cases this brings a better result
  268. Private Sub CmdRepCirc_Click()
  269.  
  270. Dim X As Long
  271. Dim Y As Long
  272. Dim r As Long
  273. Dim g As Long
  274. Dim b As Long
  275. Dim a As Long
  276. Dim ctr As Long
  277. Dim ctrOld As Long
  278. Dim Direction As Long
  279. Dim ErrCnt As Long
  280. Dim TmpX As Long
  281. Dim TmpY As Long
  282. Dim X1 As Long
  283. Dim Y1 As Long
  284.  
  285. 'Get the Picture
  286.     Pic2Array PicOrg, PicAr1
  287.  
  288. 'Create a maskpicture
  289.     PicDifference
  290.     Me.MousePointer = 11
  291.     
  292.     Do
  293.     ctr = 0
  294. 'Move Thru the Picture and search for holes
  295.     For Y = 1 To PicOrg.Height - 2
  296.         For X = 1 To PicOrg.Width - 2
  297. 'scann for transparent color
  298.             If MaskAR(X, Y) Then
  299. 'We found a transparent pixel
  300.                 ctr = ctr + 1
  301.                 ErrCnt = 0
  302.                 Direction = 1
  303. 'First we check to the right
  304. 'so move the startpoint to the left
  305.                 X1 = X - 1
  306.                 Y1 = Y
  307. 'Now scan for an empty Pixel
  308.                 Do
  309. 'Select the direction
  310.                 Select Case Direction
  311.                     Case 1
  312.                     TmpX = 1
  313.                     TmpY = 0
  314.                     Case 2
  315.                     TmpX = 1
  316.                     TmpY = 1
  317.                     Case 3
  318.                     TmpX = 0
  319.                     TmpY = 1
  320.                     Case 4
  321.                     TmpX = -1
  322.                     TmpY = 1
  323.                     Case 5
  324.                     TmpX = -1
  325.                     TmpY = 0
  326.                     Case 6
  327.                     TmpX = -1
  328.                     TmpY = -1
  329.                     Case 7
  330.                     TmpX = 0
  331.                     TmpY = -1
  332.                     Case 8
  333.                     TmpX = 1
  334.                     TmpY = -1
  335.                 End Select
  336.                 
  337.                 If X1 + TmpX > PicOrg.Width - 2 Or X1 + TmpX < 1 Or Y1 + TmpY > PicOrg.Height - 2 Or Y1 + TmpY < 1 Then
  338.                     ErrCnt = ErrCnt + 1
  339.                     Else
  340. 'Search for a new empty Pixel
  341.                     If MaskAR(X1 + TmpX, Y1 + TmpY) Then
  342. 'Set a new Startpoint
  343.                         X1 = X1 + TmpX
  344.                         Y1 = Y1 + TmpY
  345. 'Check if there is a Colored Pixel
  346. 'If yes then add the color
  347.                         a = 0
  348.                         If MaskAR(X1 - 1, Y1) = False Then
  349.                             a = a + 1
  350.                             r = PicAr1(X1 - 1, Y1).rgbRed
  351.                             g = PicAr1(X1 - 1, Y1).rgbgreen
  352.                             b = PicAr1(X1 - 1, Y1).rgbBlue
  353.                         End If
  354.                         If MaskAR(X1 + 1, Y1) = False Then
  355.                             a = a + 1
  356.                             r = r + PicAr1(X1 + 1, Y1).rgbRed
  357.                             g = g + PicAr1(X1 + 1, Y1).rgbgreen
  358.                             b = b + PicAr1(X1 + 1, Y1).rgbBlue
  359.                         End If
  360.                         If MaskAR(X1, Y1 - 1) = False Then
  361.                             a = a + 1
  362.                             r = r + PicAr1(X1, Y1 - 1).rgbRed
  363.                             g = g + PicAr1(X1, Y1 - 1).rgbgreen
  364.                             b = b + PicAr1(X1, Y1 - 1).rgbBlue
  365.                         End If
  366.                         If MaskAR(X1, Y1 + 1) = False Then
  367.                             a = a + 1
  368.                             r = r + PicAr1(X1, Y1 + 1).rgbRed
  369.                             g = g + PicAr1(X1, Y1 + 1).rgbgreen
  370.                             b = b + PicAr1(X1, Y1 + 1).rgbBlue
  371.                         End If
  372. 'If we have 2 or mor colored pixels arround then
  373. 'fill the transparent pixel with a coombination
  374.                         If a > 1 Then
  375.                             r = r / a
  376.                             g = g / a
  377.                             b = b / a
  378.                             PicAr1(X1, Y1).rgbRed = CByte(r)
  379.                             PicAr1(X1, Y1).rgbgreen = CByte(g)
  380.                             PicAr1(X1, Y1).rgbBlue = CByte(b)
  381.                             MaskAR(X1, Y1) = False
  382. 'we removed a transparent pixel
  383.                             ctr = ctr - 1
  384.                             Direction = Direction - 1
  385.                             If Direction = 0 Then Direction = 8
  386. 'We found one so reset the error counter
  387.                             ErrCnt = 0
  388.                             Else
  389.                             ErrCnt = ErrCnt + 1
  390.                             Direction = Direction + 1
  391.                             X1 = X1 - TmpX
  392.                             Y1 = Y1 - TmpY
  393.                         End If
  394.                         r = 0
  395.                         g = 0
  396.                         b = 0
  397.                         Else
  398.                         ErrCnt = ErrCnt + 1
  399.                         Direction = Direction + 1
  400.                     End If
  401.                 End If
  402.                 If Direction = 9 Then Direction = 1
  403.             Loop Until ErrCnt = 8
  404.         End If
  405.     Next X
  406. Next Y
  407.  
  408. If ctr <> ctrOld Then
  409.     ctrOld = ctr
  410.     Else
  411.     If ctr > 0 Then MsgBox "Some parts could not be fixed"
  412.     Exit Do
  413. End If
  414.  
  415. Loop Until ctr < 1
  416.  
  417. Me.MousePointer = 0
  418. Array2Pic PicOrg, PicAr1
  419. PicOrg.Refresh
  420.  
  421. End Sub
  422.  
  423. 'Create The Mask
  424. 'there are some different Method┤s to create it
  425. Private Sub PicDifference()
  426.  
  427. Dim X As Long
  428. Dim Y As Long
  429. Dim ctr1 As Long
  430. Dim ctr2 As Long
  431. Dim fnd As Boolean
  432. Dim ActColor As Byte
  433. Dim ScrVal As Long
  434.     ReDim MaskAR(0 To PicOrg.Width - 1, 0 To PicOrg.Height - 1) As Boolean
  435.     ScrVal = ScrSim.Value * 3
  436.     ActColor = SameColor(TR, TG, TB)
  437.  
  438.     For X = 0 To PicOrg.Width - 1
  439.         For Y = 0 To PicOrg.Height - 1
  440.             ctr1 = ctr1 + 1
  441.             Select Case CboColor.ListIndex
  442.                 Case 0
  443.                 If SimilarColor(PicAr1(X, Y).rgbRed, PicAr1(X, Y).rgbgreen, PicAr1(X, Y).rgbBlue, TR, TG, TB, ScrSim.Value) Then fnd = True
  444.                 Case 1
  445.                 If coldiff(PicAr1(X, Y).rgbRed, PicAr1(X, Y).rgbgreen, PicAr1(X, Y).rgbBlue, TR, TG, TB) < ScrVal Then fnd = True
  446.                 Case 2
  447.                 If brghtdiff(PicAr1(X, Y).rgbRed, PicAr1(X, Y).rgbgreen, PicAr1(X, Y).rgbBlue, TR, TG, TB) < ScrSim.Value Then fnd = True
  448.                 Case 3
  449.                 If lumdiff(PicAr1(X, Y).rgbRed, PicAr1(X, Y).rgbgreen, PicAr1(X, Y).rgbBlue, TR, TG, TB) * 12.14 < ScrVal Then fnd = True
  450.                 Case 4
  451.                 If SameColor(PicAr1(X, Y).rgbRed, PicAr1(X, Y).rgbgreen, PicAr1(X, Y).rgbBlue) = ActColor Then fnd = True
  452.             End Select
  453.             MaskAR(X, Y) = fnd
  454.             If fnd Then ctr2 = ctr2 + 1
  455.             fnd = False
  456.         Next Y
  457.     Next X
  458.     LblDamaged = CLng(ctr2 * 100 / ctr1) & " % repaired"
  459.  
  460. End Sub
  461. 'Check if a Color is ind a range X% from the actual point
  462. Private Function SimilarColor(ByVal Red1 As Long, ByVal Green1 As Long, ByVal Blue1 As Long, ByVal Red2 As Long, ByVal Green2 As Long, ByVal Blue2 As Long, ByVal ADif As Long) As Boolean
  463.  
  464. 'Check if the color is in our range
  465.     If Abs(Red1 - Red2) <= ADif And Abs(Green1 - Green2) <= ADif And Abs(Blue1 - Blue2) <= ADif Then SimilarColor = True
  466.  
  467. End Function
  468. Private Function SameColor(Red As Byte, Blue As Byte, Green As Byte) As Byte
  469.  
  470. Dim Tmp As Byte
  471.     If Red > Green Then Tmp = 1
  472.  
  473.     If Green > Blue Then Tmp = Tmp + 10
  474.     If Red > Blue Then Tmp = Tmp + 100
  475.     SameColor = Tmp
  476.  
  477. End Function
  478. 'Found this functions (as php) on
  479. 'http://www.splitbrain.org/blog/2008-09/18-calculating_color_contrast_with_php
  480. Function coldiff(R1, G1, B1, R2, G2, B2) As Long
  481.  
  482.     coldiff = Abs(R1 - R2) + Abs(G1 - G2) + Abs(B1 - B2)
  483.  
  484. End Function
  485. 'Brightness Contrast
  486. Function brghtdiff(R1, G1, B1, R2, G2, B2) As Long
  487.  
  488.     brghtdiff = Abs(((299 + R1 + 587 * G1 + 114 * B1) / 1000) - ((299 + R2 + 587 * G2 + 114 * B2) / 1000))
  489.  
  490. End Function
  491. 'Luminosity Contrast
  492. Function lumdiff(R1, G1, B1, R2, G2, B2) As Single
  493.  
  494. Dim L1 As Single
  495. Dim L2 As Single
  496.     L1 = 0.2126 * (R1 / 255) ^ 2.2 + 0.7152 * (G1 / 255) ^ 2.2 + 0.0722 * (B1 / 255) ^ 2.2
  497.  
  498.     L2 = 0.2126 * (R2 / 255) ^ 2.2 + 0.7152 * (G2 / 255) ^ 2.2 + 0.0722 * (B2 / 255) ^ 2.2
  499.     If L1 > L2 Then
  500.         lumdiff = (L1 + 0.05) / (L2 + 0.05)
  501.         Else
  502.         lumdiff = (L2 + 0.05) / (L1 + 0.05)
  503.     End If
  504.  
  505. End Function
  506.  
  507. 'Get a Picture as Array
  508. Private Sub Pic2Array(PicBox As PictureBox, ByRef PicArray() As RGBQUAD)
  509.  
  510.     GetObject PicBox.Image, Len(PicInfo), PicInfo
  511.     ReDim PicArray(0 To PicInfo.bmWidth - 1, 0 To PicInfo.bmHeight - 1) As RGBQUAD
  512.     GetBitmapBits PicBox.Image, PicInfo.bmWidth * PicInfo.bmHeight * 4, PicArray(0, 0)
  513.  
  514. End Sub
  515. 'Write a Array to a Picture
  516. Private Sub Array2Pic(PicBox As PictureBox, ByRef PicArray() As RGBQUAD)
  517.  
  518.     GetObject PicBox.Image, Len(PicInfo), PicInfo
  519.     SetBitmapBits PicBox.Image, PicInfo.bmWidth * PicInfo.bmHeight * 4, PicArray(0, 0)
  520.  
  521. End Sub
  522. Private Sub GetRGB(col As Long, Red, Green, Blue)
  523.  
  524.     Red = col Mod 256
  525.     Green = ((col And &HFF00) \ 256) Mod 256
  526.     Blue = (col And &HFF0000) \ 65536
  527.  
  528. End Sub
  529.  
  530. Private Sub Form_Load()
  531.  
  532.     CboColor.ListIndex = 0
  533.  
  534. End Sub
  535. Private Sub PicOrg_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  536.  
  537.     GetRGB PicOrg.Point(X, Y), TR, TG, TB
  538.     Shape1.FillColor = RGB(TR, TG, TB)
  539.  
  540. End Sub
  541. Private Sub ScrSim_Change()
  542.  
  543.     Label2 = "Difference " & ScrSim.Value
  544.  
  545. End Sub
  546.