home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch7 / Enlarge.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-05-05  |  12.9 KB  |  361 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmEnlarge 
  4.    Caption         =   "Enlarge []"
  5.    ClientHeight    =   2895
  6.    ClientLeft      =   165
  7.    ClientTop       =   735
  8.    ClientWidth     =   3120
  9.    LinkTopic       =   "Form2"
  10.    ScaleHeight     =   2895
  11.    ScaleWidth      =   3120
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.PictureBox picResult 
  14.       Height          =   2295
  15.       Left            =   840
  16.       ScaleHeight     =   149
  17.       ScaleMode       =   3  'Pixel
  18.       ScaleWidth      =   157
  19.       TabIndex        =   4
  20.       Top             =   1440
  21.       Visible         =   0   'False
  22.       Width           =   2415
  23.    End
  24.    Begin VB.CommandButton cmdEnlarge 
  25.       Caption         =   "Enlarge"
  26.       Default         =   -1  'True
  27.       Height          =   375
  28.       Left            =   1200
  29.       TabIndex        =   3
  30.       Top             =   0
  31.       Width           =   855
  32.    End
  33.    Begin VB.TextBox txtScale 
  34.       Height          =   285
  35.       Left            =   600
  36.       TabIndex        =   2
  37.       Text            =   "1.0"
  38.       Top             =   60
  39.       Width           =   495
  40.    End
  41.    Begin MSComDlg.CommonDialog dlgOpenFile 
  42.       Left            =   0
  43.       Top             =   360
  44.       _ExtentX        =   847
  45.       _ExtentY        =   847
  46.       _Version        =   393216
  47.    End
  48.    Begin VB.PictureBox picOriginal 
  49.       AutoSize        =   -1  'True
  50.       Height          =   2295
  51.       Left            =   120
  52.       ScaleHeight     =   149
  53.       ScaleMode       =   3  'Pixel
  54.       ScaleWidth      =   157
  55.       TabIndex        =   0
  56.       Top             =   480
  57.       Width           =   2415
  58.    End
  59.    Begin VB.Label Label1 
  60.       Caption         =   "Scale"
  61.       Height          =   255
  62.       Left            =   120
  63.       TabIndex        =   1
  64.       Top             =   60
  65.       Width           =   495
  66.    End
  67.    Begin VB.Menu mnuFile 
  68.       Caption         =   "&File"
  69.       Begin VB.Menu mnuFileOpen 
  70.          Caption         =   "&Open..."
  71.          Shortcut        =   ^O
  72.       End
  73.       Begin VB.Menu mnuFileSaveAs 
  74.          Caption         =   "Save &As..."
  75.          Shortcut        =   ^A
  76.       End
  77.    End
  78. Attribute VB_Name = "frmEnlarge"
  79. Attribute VB_GlobalNameSpace = False
  80. Attribute VB_Creatable = False
  81. Attribute VB_PredeclaredId = True
  82. Attribute VB_Exposed = False
  83. Option Explicit
  84. Private FromXmin As Single
  85. Private FromYmin As Single
  86. Private ToXmin As Single
  87. Private ToYmin As Single
  88. Private XScale As Single
  89. Private YScale As Single
  90. ' Copy the picture.
  91. Private Sub EnlargePicture(ByVal pic_from As PictureBox, ByVal pic_to As PictureBox, ByVal from_xmin As Single, ByVal from_ymin As Single, ByVal from_wid As Single, ByVal from_hgt As Single, ByVal to_xmin As Single, ByVal to_ymin As Single, ByVal to_wid As Single, ByVal to_hgt As Single)
  92.     ' Save mapping values.
  93.     FromXmin = from_xmin
  94.     FromYmin = from_ymin
  95.     ToXmin = to_xmin
  96.     ToYmin = to_ymin
  97.     XScale = to_wid / (from_wid - 1)
  98.     YScale = to_hgt / (from_hgt - 1)
  99.     ' Transform the image.
  100.     TransformImage pic_from, pic_to
  101. End Sub
  102. ' Map the output pixel (ix_out, iy_out) to the input
  103. ' pixel (x_in, y_in).
  104. Private Sub MapPixel(ByVal ix_out As Single, ByVal iy_out As Single, ByRef x_in As Single, ByRef y_in As Single)
  105.     x_in = FromXmin + (ix_out - ToXmin) / XScale
  106.     y_in = FromYmin + (iy_out - ToYmin) / YScale
  107. End Sub
  108. ' Transform the image.
  109. Private Sub TransformImage(ByVal pic_from As PictureBox, ByVal pic_to As PictureBox)
  110. Dim white_pixel As RGBTriplet
  111. Dim input_pixels() As RGBTriplet
  112. Dim result_pixels() As RGBTriplet
  113. Dim bits_per_pixel As Integer
  114. Dim ix_max As Single
  115. Dim iy_max As Single
  116. Dim x_in As Single
  117. Dim y_in As Single
  118. Dim ix_out As Integer
  119. Dim iy_out As Integer
  120. Dim ix_in As Integer
  121. Dim iy_in As Integer
  122. Dim dx As Single
  123. Dim dy As Single
  124. Dim dx1 As Single
  125. Dim dx2 As Single
  126. Dim dy1 As Single
  127. Dim dy2 As Single
  128. Dim v11 As Integer
  129. Dim v12 As Integer
  130. Dim v21 As Integer
  131. Dim v22 As Integer
  132.     ' Set the white pixel's value.
  133.     With white_pixel
  134.         .rgbRed = 255
  135.         .rgbGreen = 255
  136.         .rgbBlue = 255
  137.     End With
  138.     ' Get the pixels from pic_from.
  139.     GetBitmapPixels pic_from, input_pixels, bits_per_pixel
  140.     ' Get the pixels from pic_to.
  141.     GetBitmapPixels pic_to, result_pixels, bits_per_pixel
  142.     ' Get the original image's bounds.
  143.     ix_max = pic_from.ScaleWidth - 2
  144.     iy_max = pic_from.ScaleHeight - 2
  145.     ' Calculate the output pixel values.
  146.     For iy_out = 0 To pic_to.ScaleHeight - 1
  147.         For ix_out = 0 To pic_to.ScaleWidth - 1
  148.             ' Map the pixel value from
  149.             ' (ix_out, iy_out) to (x_in, y_in).
  150.             MapPixel ix_out, iy_out, x_in, y_in
  151.             ' Interpolate to find the pixel's value.
  152.             ' Find the nearest integral position.
  153.             ix_in = Int(x_in)
  154.             iy_in = Int(y_in)
  155.             ' See if this is out of bounds.
  156.             If (ix_in < 0) Or (ix_in > ix_max) Or _
  157.                (iy_in < 0) Or (iy_in > iy_max) _
  158.             Then
  159.                 ' The point is outside the image.
  160.                 ' Use white.
  161.                 result_pixels(ix_out, iy_out) = white_pixel
  162.             Else
  163.                 ' The point lies within the image.
  164.                 ' Calculate its value.
  165.                 dx1 = x_in - ix_in
  166.                 dy1 = y_in - iy_in
  167.                 dx2 = 1# - dx1
  168.                 dy2 = 1# - dy1
  169.                 With result_pixels(ix_out, iy_out)
  170.                     ' Calculate the red value.
  171.                     v11 = input_pixels(ix_in, iy_in).rgbRed
  172.                     v12 = input_pixels(ix_in, iy_in + 1).rgbRed
  173.                     v21 = input_pixels(ix_in + 1, iy_in).rgbRed
  174.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbRed
  175.                     .rgbRed = _
  176.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  177.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  178.         
  179.                     ' Calculate the green value.
  180.                     v11 = input_pixels(ix_in, iy_in).rgbGreen
  181.                     v12 = input_pixels(ix_in, iy_in + 1).rgbGreen
  182.                     v21 = input_pixels(ix_in + 1, iy_in).rgbGreen
  183.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbGreen
  184.                     .rgbGreen = _
  185.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  186.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  187.                     ' Calculate the blue value.
  188.                     v11 = input_pixels(ix_in, iy_in).rgbBlue
  189.                     v12 = input_pixels(ix_in, iy_in + 1).rgbBlue
  190.                     v21 = input_pixels(ix_in + 1, iy_in).rgbBlue
  191.                     v22 = input_pixels(ix_in + 1, iy_in + 1).rgbBlue
  192.                     .rgbBlue = _
  193.                         v11 * dx2 * dy2 + v12 * dx2 * dy1 + _
  194.                         v21 * dx1 * dy2 + v22 * dx1 * dy1
  195.                 End With
  196.             End If
  197.         Next ix_out
  198.     Next iy_out
  199.     ' Set pic_to's pixels.
  200.     SetBitmapPixels pic_to, bits_per_pixel, result_pixels
  201.     pic_to.Picture = pic_to.Image
  202. End Sub
  203. ' Arrange the controls.
  204. Private Sub ArrangeControls(ByVal scale_factor As Single)
  205. Dim new_wid As Single
  206. Dim new_hgt As Single
  207.     ' Calculate the result's size.
  208.     new_wid = picOriginal.ScaleWidth * scale_factor
  209.     new_hgt = picOriginal.ScaleHeight * scale_factor
  210.     new_wid = ScaleX(new_wid, vbPixels, ScaleMode) + picOriginal.Width - ScaleX(picOriginal.ScaleWidth, vbPixels, ScaleMode)
  211.     new_hgt = ScaleY(new_hgt, vbPixels, ScaleMode) + picOriginal.Height - ScaleY(picOriginal.ScaleHeight, vbPixels, ScaleMode)
  212.     ' Position the result PictureBox.
  213.     picResult.Move _
  214.         picOriginal.Left + picOriginal.Width + 120, _
  215.         picOriginal.Top, new_wid, new_hgt
  216.     picResult.Line (0, 0)-(picResult.ScaleWidth, picResult.ScaleHeight), _
  217.         picResult.BackColor, BF
  218.     picResult.Picture = picResult.Image
  219.     picResult.Visible = True
  220.     ' This makes the image resize itself to
  221.     ' fit the picture.
  222.     picResult.Picture = picResult.Image
  223.     ' Make the form big enough.
  224.     new_wid = picResult.Left + picResult.Width
  225.     If new_wid < cmdEnlarge.Left + cmdEnlarge.Width _
  226.         Then new_wid = cmdEnlarge.Left + cmdEnlarge.Width
  227.     new_hgt = picResult.Top + picResult.Height
  228.     Move Left, Top, new_wid + 237, new_hgt + 816
  229.     DoEvents
  230. End Sub
  231. ' Transform the picture.
  232. Private Sub cmdEnlarge_Click()
  233. Dim scale_factor As Single
  234.     ' Do nothing if no picture is loaded.
  235.     If picOriginal.Picture = 0 Then Exit Sub
  236.     ' Get the scale.
  237.     On Error GoTo ScaleError
  238.     scale_factor = CSng(txtScale.Text)
  239.     On Error GoTo 0
  240.     ' Make sure the scale is at least 1.
  241.     If scale_factor < 1# Then
  242.         MsgBox "Scale must be at least 1.0"
  243.         txtScale.SetFocus
  244.         Exit Sub
  245.     End If
  246.     Screen.MousePointer = vbHourglass
  247.     picResult.Line (0, 0)-(picResult.ScaleWidth, picResult.ScaleHeight), _
  248.         picResult.BackColor, BF
  249.     DoEvents
  250.     ' Arrange picResult.
  251.     ArrangeControls scale_factor
  252.     ' Transform the image.
  253.     EnlargePicture picOriginal, picResult, _
  254.         0, 0, _
  255.         picOriginal.ScaleWidth, picOriginal.ScaleHeight, _
  256.         0, 0, _
  257.         picResult.ScaleWidth, picResult.ScaleHeight
  258.     Screen.MousePointer = vbDefault
  259.     Exit Sub
  260. ScaleError:
  261.     MsgBox "Invalid scale"
  262.     txtScale.SetFocus
  263. End Sub
  264. ' Start in the current directory.
  265. Private Sub Form_Load()
  266.     picOriginal.AutoSize = True
  267.     picOriginal.ScaleMode = vbPixels
  268.     picOriginal.AutoRedraw = True
  269.     picResult.ScaleMode = vbPixels
  270.     picResult.AutoRedraw = True
  271.     dlgOpenFile.CancelError = True
  272.     dlgOpenFile.InitDir = App.Path
  273.     dlgOpenFile.Filter = _
  274.         "Bitmaps (*.bmp)|*.bmp|" & _
  275.         "GIFs (*.gif)|*.gif|" & _
  276.         "JPEGs (*.jpg)|*.jpg;*.jpeg|" & _
  277.         "Icons (*.ico)|*.ico|" & _
  278.         "Cursors (*.cur)|*.cur|" & _
  279.         "Run-Length Encoded (*.rle)|*.rle|" & _
  280.         "Metafiles (*.wmf)|*.wmf|" & _
  281.         "Enhanced Metafiles (*.emf)|*.emf|" & _
  282.         "Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf|" & _
  283.         "All Files (*.*)|*.*"
  284.     Width = picResult.Left + picResult.Width + 120 + Width - ScaleWidth
  285.     Height = picOriginal.Top + picOriginal.Height + 120 + Height - ScaleHeight
  286. End Sub
  287. ' Load the indicated file.
  288. Private Sub mnuFileOpen_Click()
  289. Dim file_name As String
  290.     ' Let the user select a file.
  291.     On Error Resume Next
  292.     dlgOpenFile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  293.     dlgOpenFile.ShowOpen
  294.     If Err.Number = cdlCancel Then
  295.         Exit Sub
  296.     ElseIf Err.Number <> 0 Then
  297.         Beep
  298.         MsgBox "Error selecting file.", , vbExclamation
  299.         Exit Sub
  300.     End If
  301.     On Error GoTo 0
  302.     Screen.MousePointer = vbHourglass
  303.     DoEvents
  304.     file_name = Trim$(dlgOpenFile.FileName)
  305.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  306.         - Len(dlgOpenFile.FileTitle) - 1)
  307.     Caption = "Enlarge [" & dlgOpenFile.FileTitle & "]"
  308.     ' Open the original file.
  309.     On Error GoTo LoadError
  310.     picOriginal.Picture = LoadPicture(file_name)
  311.     On Error GoTo 0
  312.     ' Hide picResult.
  313.     picResult.Visible = False
  314.     If cmdEnlarge.Left + cmdEnlarge.Width > picOriginal.Left + picOriginal.Width Then
  315.         Width = cmdEnlarge.Left + cmdEnlarge.Width + 120 + Width - ScaleWidth
  316.     Else
  317.         Width = picOriginal.Left + picOriginal.Width + 120 + Width - ScaleWidth
  318.     End If
  319.     Height = picOriginal.Top + picOriginal.Height + 120 + Height - ScaleHeight
  320.     Screen.MousePointer = vbDefault
  321.     Exit Sub
  322. LoadError:
  323.     Screen.MousePointer = vbDefault
  324.     MsgBox "Error " & Format$(Err.Number) & _
  325.         " opening file '" & file_name & "'" & vbCrLf & _
  326.         Err.Description
  327. End Sub
  328. ' Save the transformed image.
  329. Private Sub mnuFileSaveAs_Click()
  330. Dim file_name As String
  331.     ' Let the user select a file.
  332.     On Error Resume Next
  333.     dlgOpenFile.Flags = cdlOFNOverwritePrompt + cdlOFNHideReadOnly
  334.     dlgOpenFile.ShowSave
  335.     If Err.Number = cdlCancel Then
  336.         Exit Sub
  337.     ElseIf Err.Number <> 0 Then
  338.         Beep
  339.         MsgBox "Error selecting file.", , vbExclamation
  340.         Exit Sub
  341.     End If
  342.     On Error GoTo 0
  343.     Screen.MousePointer = vbHourglass
  344.     DoEvents
  345.     file_name = Trim$(dlgOpenFile.FileName)
  346.     dlgOpenFile.InitDir = Left$(file_name, Len(file_name) _
  347.         - Len(dlgOpenFile.FileTitle) - 1)
  348.     Caption = "Enlarge [" & dlgOpenFile.FileTitle & "]"
  349.     ' Save the transformed image into the file.
  350.     On Error GoTo SaveError
  351.     SavePicture picResult.Picture, file_name
  352.     On Error GoTo 0
  353.     Screen.MousePointer = vbDefault
  354.     Exit Sub
  355. SaveError:
  356.     Screen.MousePointer = vbDefault
  357.     MsgBox "Error " & Format$(Err.Number) & _
  358.         " saving file '" & file_name & "'" & vbCrLf & _
  359.         Err.Description
  360. End Sub
  361.