Jump to content

Put the DWG file image in a GUI


Recommended Posts

Link to comment
Share on other sites

16 hours ago, UEZ said:

Maybe something like this here to be more accurate?

;BM = 424D, BA = 4241, CI = 4349, CP = 4350, IC = 4943, PT = 5054
$bin = "0x1234424D460000000000000036000000" ;shorten file content
$aResult = StringRegExp($bin, ".*(424D|4241|4349|4350|4943|5054)[[:xdigit:]]{8}(00000000).+", 1)
If Not @error Then ConsoleWrite("BMP found" & @CRLF)

 

Are you sure about the 8 zeros? Wiki says: "Reserved; actual value depends on the application that creates the image, if created manually can be 0", so non-zero might still be valid.

With plausibility checks I meant something like this:

#include <Binary.au3>
; https://www.autoitscript.com/forum/topic/131037-binary-udf/?do=findComment&comment=1339527

_Extract_BMP(@ScriptDir & "\test.bmp")

Func _Extract_BMP($sFile_Input, $sFile_Output = "")
    If Not $sFile_Output Then $sFile_Output = $sFile_Input & "_extracted.bmp"

    Local $hFile = FileOpen($sFile_Input, 16)
    If $hFile = -1 Then Return SetError(1)

    Local $bContent = FileRead($hFile)
    FileClose($hFile)

    Local $i_BMP_Position_Header = _BinaryInBin($bContent, Binary("0x424D"))
    Local $i_BMP_Total_Size = Int(BinaryMid($bContent, $i_BMP_Position_Header + 2, 4))

    Local $b_BMP_Values_plausible = True

    Local $i_BMP_Width = Int(BinaryMid($bContent, $i_BMP_Position_Header + 14 + 4, 4))
    ConsoleWrite("$i_BMP_Width= " & $i_BMP_Width & @CRLF)
    If $i_BMP_Width > 9999 Then
        ConsoleWrite("! $i_BMP_Width seems implausible > " & $i_BMP_Width & @CRLF)
        $b_BMP_Values_plausible = False
    EndIf

    Local $i_BMP_Height = Int(BinaryMid($bContent, $i_BMP_Position_Header + 14 + 4 + 4, 4))
    ConsoleWrite("$i_BMP_Height= " & $i_BMP_Height & @CRLF)
    If $i_BMP_Height > 9999 Then
        ConsoleWrite("! $i_BMP_Height seems implausible > " & $i_BMP_Height & @CRLF)
        $b_BMP_Values_plausible = False
    EndIf

    Local $i_BMP_Planes = Int(BinaryMid($bContent, $i_BMP_Position_Header + 14 + 4 + 4 + 4, 2))
    ConsoleWrite("$i_BMP_Planes= " & $i_BMP_Planes & @CRLF)
    ; https://devblogs.microsoft.com/oldnewthing/20041201-00/?p=37163
    If $i_BMP_Planes > 4 Then
        ConsoleWrite("! $i_BMP_Planes seems implausible > " & $i_BMP_Planes & @CRLF)
        $b_BMP_Values_plausible = False
    EndIf

    Local $i_BMP_Bits_per_Pixel = Int(BinaryMid($bContent, $i_BMP_Position_Header + 14 + 4 + 4 + 4 + 2, 2))
    ConsoleWrite("$i_BMP_Bits_per_Pixel= " & $i_BMP_Bits_per_Pixel & @CRLF)
    If $i_BMP_Bits_per_Pixel > 48 Then
        ConsoleWrite("! $i_BMP_Bits_per_Pixel seems implausible > " & $i_BMP_Bits_per_Pixel & @CRLF)
        $b_BMP_Values_plausible = False
    EndIf

    Local $i_BMP_Compression = Int(BinaryMid($bContent, $i_BMP_Position_Header + 14 + 4 + 4 + 4 + 2 + 2, 4))
    ConsoleWrite("$i_BMP_Compression= " & $i_BMP_Compression & @CRLF)
    If $i_BMP_Compression > 13 Then
        ConsoleWrite("! $i_BMP_Compression seems implausible > " & $i_BMP_Compression & @CRLF)
        $b_BMP_Values_plausible = False
    EndIf

    If $b_BMP_Values_plausible And $i_BMP_Position_Header And $i_BMP_Total_Size Then
        ConsoleWrite("+ BMP data found" & @CRLF)

        If FileExists($sFile_Output) Then
            ConsoleWrite("! Output file '" & $sFile_Output & "' existed, BMP extraction skipped " & @CRLF)
            Return SetError(2)
        EndIf
        $hFile = FileOpen($sFile_Output, 2 + 16)
        If $hFile = -1 Then Return SetError(3)

        FileWrite($hFile, BinaryMid($bContent, $i_BMP_Position_Header, $i_BMP_Total_Size))
        FileClose($hFile)
        Return 1
    EndIf

    ConsoleWrite("- No BMP data found" & @CRLF)
    Return 0

EndFunc   ;==>_Extract_BMP

 

Link to comment
Share on other sites

24 minutes ago, KaFu said:

Are you sure about the 8 zeros? Wiki says: "Reserved; actual value depends on the application that creates the image, if created manually can be 0", so non-zero might still be valid

Hex means 2 char per byte -> 4 bytes in total for size and reserved part. Anyhow, of course this is not the best way to check if data is a bitmap header and your solution is more precise.

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I tested this code and it seems to work fine.
Well, I admit I didn't understand all the code, but I did manage to integrate it into my application. 😉

I haven't tested this second code, but does it really offer an advantage over the old code? Would you advise me to use the latter or not?

Thanks

Link to comment
Share on other sites

Link to comment
Share on other sites

On the whole, it works pretty well. There are, however, a few files that are recent versions and should therefore contain a PNG, but which are not recognized by the script.

No PNG data found

DWG files, even recent ones, don't necessarily have a thumbnail. This option is enabled by default, but can be disabled from AutoCAD.

You might think that there are no thumbnails in these files, but when you open them in software such as AutoCAD, thumbnails are displayed in the software.

Here are a few examples of problem files.

Apparently, the code returned by $i_Position_PNG_Header is longer than the usual 3 digits of most other files.

Do you have any ideas on how to correct this?

Thank you.

dwg.zip

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...