Created
November 4, 2012 03:42
-
-
Save tombatron/4010049 to your computer and use it in GitHub Desktop.
A VBScript class for converting Kofax MFA files to regular TIFF files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class MfaImageConverter | |
Private sourceFileStream | |
Private sourceFileHexStream | |
Private convertedFileStream | |
Private convertedFileHexStream | |
Private Sub Class_Initialize() | |
Set sourceFileStream = CreateObject("ADODB.Stream") | |
sourceFileStream.Type = 1 ' Binary | |
sourceFileStream.Open | |
Set sourceFileHexStream = CreateObject("ADODB.Stream") | |
sourceFileHexStream.Type = 2 ' Text | |
sourceFileHexStream.Open | |
Set convertedFileStream = CreateObject("ADODB.Stream") | |
convertedFileStream.Type = 1 | |
convertedFileStream.Open | |
Set convertedFileHexStream = CreateObject("ADODB.Stream") | |
convertedFileHexStream.Type = 2 | |
convertedFileHexStream.Open | |
End Sub | |
Private Sub Class_Terminate() | |
If sourceFileStream.State = 1 Then sourceFileStream.Close ' 1 = Open | |
If sourceFileHexStream.State = 1 Then sourceFileHexStream.Close | |
If convertedFileStream.State = 1 Then convertedFileStream.Close | |
If convertedFileHexStream.State = 1 Then convertedFileHexStream.Close | |
Set sourceFileStream = Nothing | |
Set sourceFileHexStream = Nothing | |
Set convertedFileStream = Nothing | |
Set convertedFileHexStream = Nothing | |
End Sub | |
Public Function ProcessImage(fileName) | |
Dim convertedFileName | |
Call sourceFileStream.LoadFromFile(fileName) ' Load file data into stream. | |
Call loadSourceFileHexStream ' Load file hex data into a text stream. | |
Call loadConvertedFileHexStream ' Sieve out the TIFF data from the MFA file. | |
convertedFileName = saveConvertedFile() | |
WScript.Echo "Wrote new file " & convertedFileName & "." | |
WScript.Echo "Done." | |
End Function | |
Private Sub loadSourceFileHexStream() | |
Dim i, fileBytes | |
fileBytes = sourceFileStream.Read | |
For i = 1 To LenB(fileBytes) | |
Call sourceFileHexStream.WriteText(Right("0" & Hex(AscB(MidB(fileBytes, i, 1))), 2)) | |
Next | |
sourceFileHexStream.Position = 0 | |
Set fileBytes = Nothing | |
End Sub | |
Private Sub loadConvertedFileHexStream() | |
Dim header, headerFound, a, b, c | |
headerFound = False | |
' We need to walk through the HEX representation of the MFA file | |
' in order to find the TIFF header. Once we find the TIFF header | |
' we'll go ahead and pump the rest of the contents to the designated | |
' stream. | |
Do While Not sourceFileHexStream.EOS | |
If headerFound Then | |
Call convertedFileHexStream.WriteText(sourceFileHexStream.ReadText(2)) | |
Else | |
If a = "" Then a = sourceFileHexStream.ReadText(2) | |
If b = "" Then b = sourceFileHexStream.ReadText(2) | |
If c = "" Then | |
c = sourceFileHexStream.ReadText(2) | |
Else | |
a = b | |
b = c | |
c = sourceFileHexStream.ReadText(2) | |
header = a & b & c | |
If header = "49492A" Then | |
headerFound = True | |
Call convertedFileHexStream.WriteText(header) | |
End If | |
End If | |
End If | |
Loop | |
' Rewind stream to the beginning. We'll be reading from it when | |
' we go to save the TIFF file. | |
convertedFileHexStream.Position = 0 | |
Set header = Nothing | |
Set a = Nothing | |
Set b = Nothing | |
Set c = Nothing | |
End Sub | |
Private Function saveConvertedFile() | |
Dim fso, fileName, tmpFileName, tmpFile | |
Set fso = CreateObject("Scripting.FileSystemObject") | |
fileName = Replace(fso.GetTempName, "tmp", "tif") | |
tmpFileName = fso.GetTempName | |
Set tmpFile = fso.CreateTextFile(tmpFileName) | |
' VBScript doesn't actually support byte arrays so we'll | |
' fake it the best we can by writing converted HEX strings | |
' to a temporary file, then we'll read the contents of that | |
' file into a binary stream and the write the final image. | |
Do While Not convertedFileHexStream.EOS | |
Call tmpFile.Write(convertHexToByte(convertedFileHexStream.ReadText(2))) | |
Loop | |
tmpFile.Close | |
Call convertedFileStream.LoadFromFile(tmpFileName) | |
convertedFileStream.SaveToFile(fileName) | |
saveConvertedFile = fileName | |
fso.DeleteFile(tmpFileName) | |
Set fso = Nothing | |
End Function | |
Private Function convertHexToByte(hexString) | |
convertHexToByte = Chr("&h" & hexString) | |
End Function | |
End Class | |
Dim img | |
Set img = new MfaImageConverter | |
img.ProcessImage("803F91F6.mfa") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment