home *** CD-ROM | disk | FTP | other *** search
/ Freelog 1 / Freelog001.iso / Logs / Graphism / JpgStrip / source / jstrip.bas < prev    next >
BASIC Source File  |  1998-10-13  |  7KB  |  247 lines

  1. Attribute VB_Name = "modJStrip"
  2. Option Explicit
  3.  
  4. Public sFiles() As String
  5. Public bCancelFlag As Byte
  6.  
  7. Dim bIn() As Byte
  8. Dim bOut() As Byte
  9. Dim lPos As Long
  10. Dim sLogMsg As String
  11. Dim lFileSize As Long
  12. Dim lFileOutSize As Long
  13.  
  14. Const OKAY As Long = 0
  15. Const ERROR As Long = -1
  16. Const DONE As Long = -2
  17. Const PROBLEM As Long = -3
  18.  
  19. Private Function DoAFile(sFileIn As String) As Long
  20.     Dim lret As Long
  21.     Dim boolProbFlag As Boolean
  22.     Dim sFileOut As String
  23.     sFileOut = sFileIn & ".tmp"
  24.     lFileOutSize = 0
  25.     lPos = 1
  26.     lret = ReadFileSize(sFileIn)                ' returns filesize or ERROR
  27.     If lret = ERROR Then
  28.         sLogMsg = "Couldn't open input file."
  29.         GoTo UhOh
  30.     End If
  31.     lFileSize = lret
  32.     ReDim bIn(1 To lFileSize + 10)              ' dim variables, 1 based, with some extra space
  33.     ReDim bOut(1 To lFileSize + 10)
  34.     lret = ReadFile(sFileIn)                        ' read the file into bIn
  35.     If lret = ERROR Then
  36.         sLogMsg = "Couldn't open input file."
  37.         GoTo UhOh
  38.     End If
  39.     lret = FindJpgHeader()                         ' find the jpg header
  40.     If lret = ERROR Then
  41.         sLogMsg = "Not a valid JPEG file."
  42.         GoTo UhOh
  43.     End If
  44.     lret = 0
  45.     Do Until lret = DONE Or lret = ERROR Or lret = PROBLEM
  46.         lret = GetMarkers()                         ' copy needed data
  47.     Loop
  48.     If lret = ERROR Then
  49.         sLogMsg = "Problem parsing file."
  50.         GoTo UhOh
  51.     End If
  52.     If lret = PROBLEM Then boolProbFlag = True
  53.     lret = WriteOutFile(sFileOut)                   ' write output file
  54.     If lret = ERROR Then
  55.         sLogMsg = "Could not write output file"
  56.         GoTo UhOh
  57.     End If
  58.     lret = KillInFile(sFileIn)                          ' delete input file
  59.     If lret = ERROR Then
  60.         sLogMsg = "Could not delete original file."
  61.         GoTo UhOh
  62.     End If
  63.     lret = ReNameOutFile(sFileOut, sFileIn)    ' rename output file to original name
  64.     If lret = ERROR Then
  65.         sLogMsg = "Could not rename output file to original filename."
  66.         GoTo UhOh
  67.     End If
  68. SkipRename:
  69.     If boolProbFlag = True Then
  70.         sLogMsg = "processed successfully, but problems encountered. Check file."
  71.         DoAFile = PROBLEM
  72.     Else
  73.         sLogMsg = "Processed successfully."
  74.         DoAFile = OKAY
  75.     End If
  76. Exit Function
  77. UhOh:                                                   ' Houston, we have a problem
  78.     DoAFile = ERROR
  79. End Function
  80.  
  81. Private Function ReadFileSize(sFileName As String) As Long
  82.     On Error GoTo HandleIt
  83.     ReadFileSize = FileLen(sFileName)
  84. Exit Function
  85. HandleIt:
  86.     ReadFileSize = ERROR
  87. End Function
  88.  
  89. Private Function ReadFile(sFileName As String) As Long
  90.     Dim iFN As Integer
  91.     On Error GoTo HandleIt
  92.     iFN = FreeFile
  93.     Open sFileName For Binary As iFN
  94.     Get #iFN, 1, bIn()
  95.     Close iFN
  96.     ReadFile = OKAY
  97.     Exit Function
  98. HandleIt:
  99.     ReadFile = ERROR
  100. End Function
  101.  
  102. Private Function FindJpgHeader() As Long
  103.     Do
  104.         If bIn(lPos) = &HFF And bIn(lPos + 1) = &HD8 And bIn(lPos + 2) = &HFF Then
  105.             FindJpgHeader = OKAY
  106.             Exit Do
  107.         End If
  108.         If lPos >= lFileSize Then
  109.             FindJpgHeader = ERROR
  110.             Exit Do
  111.         End If
  112.         lPos = lPos + 1
  113.     Loop
  114.     lPos = lPos + 1
  115. End Function
  116.  
  117. Private Function GetMarkers() As Long
  118.     Dim lSkip As Long
  119.     Dim lTemp As Long
  120.     Dim bFlag As Byte
  121.     Select Case bIn(lPos)
  122.         Case &HD8
  123.             WriteArray &HFF
  124.             WriteArray &HD8
  125.             WriteArray &HFF
  126.             lSkip = 2
  127.         Case &HE0, &HDB, &HC0 To &HCB, &HDD
  128.             lSkip = Mult(bIn(lPos + 2), bIn(lPos + 1)) + 1
  129.             If lSkip + lPos >= lFileSize Then GoTo Oops
  130.             For lTemp = lPos To lPos + lSkip
  131.                 WriteArray bIn(lTemp)
  132.             Next lTemp
  133.         Case &HDA
  134.             bFlag = 1
  135.             Do
  136.                 WriteArray bIn(lPos)
  137.                 If bIn(lPos + 1) = &HFF And bIn(lPos + 2) = &HD9 Then Exit Do
  138.                 lPos = lPos + 1
  139.                 If lPos > lFileSize Then
  140.                     bFlag = 2
  141.                     Exit Do
  142.                 End If
  143.             Loop
  144.             WriteArray &HFF
  145.             WriteArray &HD9
  146.         Case Else
  147.             lSkip = Mult(bIn(lPos + 2), bIn(lPos + 1)) + 1
  148.             If lSkip + lPos > lFileSize Then GoTo Oops
  149.     End Select
  150.     lPos = lPos + lSkip
  151.     Do
  152.         If bIn(lPos) <> &HFF Then Exit Do
  153.         lPos = lPos + 1
  154.         If lPos > lFileSize Then GoTo Oops
  155.     Loop
  156.     If bFlag = 0 Then GetMarkers = OKAY
  157.     If bFlag = 1 Then GetMarkers = DONE
  158.     If bFlag = 2 Then GetMarkers = PROBLEM
  159. Exit Function
  160. Oops:
  161. GetMarkers = ERROR
  162. End Function
  163.  
  164. Private Function WriteOutFile(sFileName As String) As Long
  165.     Dim iFN As Integer
  166.     On Error GoTo NoOpen
  167.     iFN = FreeFile
  168.     ReDim Preserve bOut(1 To lFileOutSize)
  169.     Open sFileName For Binary As iFN
  170.     On Error GoTo Opened
  171.     Put #iFN, , bOut()
  172.     Close iFN
  173.     WriteOutFile = OKAY
  174. Exit Function
  175. NoOpen:
  176.     WriteOutFile = ERROR
  177. Exit Function
  178. Opened:
  179.     Close iFN
  180.     WriteOutFile = ERROR
  181. End Function
  182.  
  183. Private Function KillInFile(sFileName As String) As Long
  184.     On Error GoTo HandleIt
  185.     Kill sFileName
  186.     KillInFile = OKAY
  187. Exit Function
  188. HandleIt:
  189.     KillInFile = ERROR
  190. End Function
  191.  
  192. Private Function ReNameOutFile(sFileOld As String, sFileNew As String) As Long
  193.     On Error GoTo HandleIt
  194.     Name sFileOld As sFileNew
  195.     ReNameOutFile = OKAY
  196. Exit Function
  197. HandleIt:
  198.     ReNameOutFile = ERROR
  199. End Function
  200.  
  201. Private Sub WriteArray(bData As Byte)
  202.     lFileOutSize = lFileOutSize + 1
  203.     bOut(lFileOutSize) = bData
  204. End Sub
  205.  
  206. Private Function Mult(lsb As Byte, msb As Byte) As Long
  207.     Mult = CLng(lsb) + (CLng(msb) * 256&)
  208. End Function
  209. Public Sub DoIt(lNumber As Long)
  210.     On Error Resume Next
  211.     Dim lCount As Long
  212.     Dim iFN As Integer
  213.     Dim lret As Long
  214.     Dim lBefore As Long
  215.     Dim lAfter As Long
  216.     Dim lDiff As Long
  217.     Dim lTotal As Long
  218.     Dim sDone As String
  219.     Dim lFilesDone As Long
  220.     iFN = FreeFile
  221.     Open "js.log" For Output As iFN
  222.     For lCount = 0 To lNumber
  223.         lBefore = FileLen(sFiles(lCount))
  224.         lret = DoAFile(sFiles(lCount))
  225.         lAfter = FileLen(sFiles(lCount))
  226.         frmMain.lblMessage.Caption = sFiles(lCount)
  227.         frmMain.pUpdate lNumber, lCount
  228.         lDiff = lBefore - lAfter
  229.         lTotal = lTotal + lDiff
  230.         Print #iFN, "**************"
  231.         Print #iFN, sFiles(lCount)
  232.         Print #iFN, sLogMsg
  233.         Print #iFN, lBefore
  234.         Print #iFN, lAfter
  235.         Print #iFN, lDiff
  236.         DoEvents
  237.         lFilesDone = lFilesDone + 1
  238.         If bCancelFlag = 1 Then Exit For
  239.     Next lCount
  240.     bCancelFlag = 0
  241.     sDone = "--------------------------------------" & vbCrLf & "Files processed:" & lFilesDone _
  242.         & vbCrLf & "Total bytes saved:" & lTotal
  243.     Print #iFN, sDone
  244.     MsgBox sDone, vbOKOnly, "Done"
  245.     Close iFN
  246. End Sub
  247.