Jump to content
rudi

Analyze binary file content

Recommended Posts

Hello.

I'm too stupid to see my mistake:

To investigate the internal "dictionary" of TIFF files I'd like to read in the files in binary mode and to check, if there are more than one pages "in" this TIFF.

Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting

The "TIFF Header Format" is easy:

Offset 00h, 2 Byte = Byte Order, "II"=intel, "MM"=motorola. (I = 0x49)

--> II

Offset 02h, 2 Byte = Version Nr.

Offset 04h, 4 Byte = pointer to first IFD entry

Description of TIFF header: https://www.awaresystems.be/imaging/tiff/faq.html#q3

 


Howto read and analyse the binary content correctly? This is my messy, not operational code:

 

$sampleTiff="H:\daten\tif\11\11\111111.TIF"
$h=FileOpen($sampleTiff,16)
$content=FileRead($h)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
FileClose($h)

$type=VarGetType($content)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $type = ' & $type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console


$ToString=BinaryToString($content)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ToString = ' & $ToString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ConsoleWrite(@CRLF  & @CRLF)

$content=StringTrimLeft($content,2) ; cut off the leading "0x"
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $content = ' & $content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console


for $i = 1 to 8 step 8
    $next=StringMid($content,$i,2)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') :     $next = ' &     $next & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    $Chr=BinaryToString($next)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') :     $Chr = ' &  $Chr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    ConsoleWrite(@CRLF & "---" & @CRLF)
Next

Regards, Rudi.

111111.TIF

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
1 hour ago, rudi said:

Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting

It's strange. I see "49 49 2A 00 08 00 00 00" in attached "111111.TIF"

$hFile = FileOpen("111111.tif", 16)
$ByteOrder = FileRead($hFile, 2)
$Version = FileRead($hFile, 2)
$FirstIFD = FileRead($hFile, 4)
FileClose($hFile)

ConsoleWrite($ByteOrder & @CRLF)
ConsoleWrite($Version & @CRLF)
ConsoleWrite($FirstIFD & @CRLF)

 

Share this post


Link to post
Share on other sites

Hello,

 

thanks for your reply.

 

You are right. I copy-pasted the displayed values from Notepad++ (Hex View) and didn't notice, that they changed after beeing pasted here.

Amazing behaviour: Notepad++ can copy values to the clipboard, and when using this feature, "00" is modified to "20", no clue, why, all other values are correct. This is reproducable...

49 49 2a 00 08 00 00 00 11 00 fe 00 04 00 01 00 

 

Your code is quite exactly what I was looking for. I wasn't aware, that after opening a file in binary mode I am able to "read <offset> <ount-of-bytes>".

That should do, thanks a lot!

 

Regards, Rudi.

 


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Because If you use that notepad change 00 to 20 is to avoid end of string character (null bytes) dont care about. I suggest you to use a real hex editor. HxD is ok.

 

Saludos

 

 

Share this post


Link to post
Share on other sites

Hello,

 

thanks for your reply. Notepad++ does *NOT* take the real values to the clipboard, but the "string representation". Except for "00", that's  replaced with "20", as far as I can see it right now...

 

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Hello.

 

somewhat modified code:

 

; $sampleTiff="H:\DATEN\tif\73\23\M000017323.tif"
; $sampleTiff="H:\daten\tif\11\11\111111.TIF"
$sampleTiff="111111.TIF"

$hFile=FileOpen($sampleTiff,16)



$Bo1=chr(FileRead($hFile,1)) ; Byte Order Info, Byte 1
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Bo1 = ' & $Bo1 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$Bo2=chr(FileRead($hFile,1)) ; Byte Order Info, Byte 1
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Bo2 = ' & $Bo2 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; is there a way to read the "0x4949" as "II" in one run? (I = indigo = 0x49)


$TVer1=FileRead($hFile,2) ; zwei bytes für TIFF Version. Immer 0x2A00
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $TVer1 = ' & $TVer1 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; the first read value seems to be the HighByte (big/little endian???)
; 2A = 42 = Version Number for all TIFFs I can touch right now. But it's 0x2A00, not 0x002A --> again little / big endian???


$IDF1Start=FileRead($hFile,4)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $IDF1Start = ' & $IDF1Start & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; offset 0x08 is correct.

$IDF1Count=FileRead($hFile,2)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $IDF1Count = ' & $IDF1Count & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; 0x11 = 17 entries a 12 bytes --> again it's 0x1100, not 0x0011


FileClose($hFile)


$Hex17=Hex(17,4)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Hex17 = ' & $Hex17 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

This is giving these consolewrite outputs. So I assume, I need to "swap" little-endian/big-endian

Is there a funktion doing so, that I've missed?

 

Regards, Rudi.

 

@@ Debug(11) : $Bo1 = I
>Error code: 0
@@ Debug(13) : $Bo2 = I
>Error code: 0
@@ Debug(18) : $TVer1 = 0x2A00
>Error code: 0
@@ Debug(24) : $IDF1Start = 0x08000000
>Error code: 0
@@ Debug(28) : $IDF1Count = 0x1100
>Error code: 0
@@ Debug(32) : $Hex17 = 0011
>Error code: 0

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

May I suggest you read the header into a struct first (see DllStructCreate), then read individual elements with DllStructGetData? See my Xbase.au3 for an example (link in my sig).

Share this post


Link to post
Share on other sites

Hello.

Thanks for your reply.

I never used DllStructCreate so far ... :sweating:

 

All I need is a look at the Pointer "Next IDF". If that value is 0x0000, then this is a single page tiff. That's all I need:

 

$sampleTiff="111111.TIF"

$hFile=FileOpen($sampleTiff,16)



$Bo1=chr(FileRead($hFile,1)) ; Byte Order Info, Byte 1
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Bo1 = ' & $Bo1 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$Bo2=chr(FileRead($hFile,1)) ; Byte Order Info, Byte 1
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Bo2 = ' & $Bo2 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; is there a way to read the "0x4949" as "II" in one run? (I = indigo = 0x49)


$TVer1=FileRead($hFile,2) ; zwei bytes für TIFF Version. Immer 0x2A00
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $TVer1 = ' & $TVer1 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; the first read value seems to be the HighByte (big/little endian???)
; 2A = 42 = Version Number for all TIFFs I can touch right now. But it's 0x2A00, not 0x002A --> again little / big endian???


$IDF1Start=FileRead($hFile,4)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $IDF1Start = ' & $IDF1Start & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; offset 0x08 is correct.

$IDF1Count=FileRead($hFile,2)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $IDF1Count = ' & $IDF1Count & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; 0x11 = 17 entries a 12 bytes --> again it's 0x1100, not 0x0011


$Hex17=Hex(17,4)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Hex17 = ' & $Hex17 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console


for $Skip = 1 to 17 ; due to this little / big endian issue manually set "End-Value"
    $foo=FileRead($hFile,12)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $foo = ' & $foo & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
Next

ConsoleWrite("Skipping done" & @CRLF)

$NextIDF=FileRead($hFile,4)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $NextIDF = ' & $NextIDF & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

if $NextIDF=0 Then
    MsgBox(0,"Multipage TIFF check","This TIFF has just one page.")
Else
    MsgBox(64,"Multipage TIFF check","This TIFF has *MORE* than one page.")
EndIf

FileClose($hFile)

 

That's giving this output:

@@ Debug(11) : $Bo1 = I
>Error code: 0
@@ Debug(13) : $Bo2 = I
>Error code: 0
@@ Debug(18) : $TVer1 = 0x2A00
>Error code: 0
@@ Debug(24) : $IDF1Start = 0x08000000
>Error code: 0
@@ Debug(28) : $IDF1Count = 0x1100
>Error code: 0
@@ Debug(33) : $Hex17 = 0011
>Error code: 0
@@ Debug(38) : $foo = 0xFE0004000100000000000000
>Error code: 0
@@ Debug(38) : $foo = 0x0001040001000000480D0000
>Error code: 0
@@ Debug(38) : $foo = 0x010104000100000030110000
>Error code: 0
@@ Debug(38) : $foo = 0x020103000100000001000000
>Error code: 0
@@ Debug(38) : $foo = 0x030103000100000004000000
>Error code: 0
@@ Debug(38) : $foo = 0x060103000100000000000000
>Error code: 0
@@ Debug(38) : $foo = 0x0A0103000100000001000000
>Error code: 0
@@ Debug(38) : $foo = 0x1101040001000000F6000000
>Error code: 0
@@ Debug(38) : $foo = 0x120103000100000001000000
>Error code: 0
@@ Debug(38) : $foo = 0x150103000100000001000000
>Error code: 0
@@ Debug(38) : $foo = 0x160104000100000030110000
>Error code: 0
@@ Debug(38) : $foo = 0x17010400010000005D2D0000
>Error code: 0
@@ Debug(38) : $foo = 0x1A01050001000000DA000000
>Error code: 0
@@ Debug(38) : $foo = 0x1B01050001000000E2000000
>Error code: 0
@@ Debug(38) : $foo = 0x250104000100000000000000
>Error code: 0
@@ Debug(38) : $foo = 0x280103000100000002000000
>Error code: 0
@@ Debug(38) : $foo = 0x310102000C000000EA000000
>Error code: 0
Skipping done
@@ Debug(44) : $NextIDF = 0x00000000

Important to me is just the value $NextIDF. That's it.

 

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Hello InnI.

Thanks for your suggestions for ....

#include <WinAPIMisc.au3>
_WinAPI_SwapWord()
_WinAPI_SwapDWord()
_WinAPI_SwapQWord()

I fail using it correctly, as the result of reading binary from the file is binary, not hex. And so far I couldn't figure out, how to convert the "binary string" to a "hex string".

 

$sampleTiff="111111.TIF"

$hFile=FileOpen($sampleTiff,16)



$Bo1=chr(FileRead($hFile,1)) ; Byte Order Info, Byte 1
$Bo2=chr(FileRead($hFile,1)) ; Byte Order Info, Byte 1
; is there a way to read the "0x4949" as "II" in one run? (I = indigo = 0x49)


$TVer=FileRead($hFile,2) ; zwei bytes für TIFF Version. Immer 0x2A00
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $TVer = ' & $TVer & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$Type=VarGetType($TVer)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Type = ' & $Type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$Swapped=_WinAPI_SwapWord($TVer)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Swapped = ' & $Swapped & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$AsString=$TVer & ""
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $AsString = ' & $AsString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
VarGetType($AsString)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : VarGetType($AsString) = ' & VarGetType($AsString) & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$Swapped2=_WinAPI_SwapWord($AsString)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Swapped2 = ' & $Swapped2 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ConsoleWrite("42 is the correct Dec value" & @CRLF)
$Type=VarGetType($Swapped2)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Type = ' & $Type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
ConsoleWrite("Propably here I'm done, as I just need the DEC value to get the correct numbers?"& @CRLF)

ConsoleWrite("------------------" & @CRLF)

$Back2Hex=Hex($Swapped2,4)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Back2Hex = ' & $Back2Hex & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
$Type=VarGetType($Back2Hex)
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $Type = ' & $Type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
; how would I get a HEX value? What do I miss?

output is this:

@@ Debug(16) : $TVer = 0x2A00
>Error code: 0
@@ Debug(18) : $Type = Binary
>Error code: 0
@@ Debug(21) : $Swapped = 10752
>Error code: 0
@@ Debug(24) : $AsString = 0x2A00
>Error code: 0
@@ Debug(26) : VarGetType($AsString) = String
>Error code: 0
@@ Debug(29) : $Swapped2 = 42
>Error code: 0
42 is the correct Dec value
@@ Debug(32) : $Type = Int32
>Error code: 0
Propably here I'm done, as I just need the DEC value to get the correct numbers?
------------------
@@ Debug(38) : $Back2Hex = 002A
>Error code: 0
@@ Debug(40) : $Type = String
>Error code: 0

 

Thanks again!

 

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

@RTFC

Hello,

 

thanks for your reply.

I downloaded your xbase.au3 and tried this code. It's reporting "invalid header", all files are placed in the same folder (this script, xbase.au3, 111111.tif)

 

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.2
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include "xbase.au3"
#include <array.au3>


dim $myArr[1]

$TIFF="111111.tif"
_Xbase_ReadToArray($TIFF,$myArr)
_ArrayDisplay($myArr)

regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
39 minutes ago, rudi said:

I downloaded your xbase.au3 and tried this code. It's reporting "invalid header",

Yes, of course it would,^_^ a tiff file not being an Xbase file.:blink: I suggested this as an example how to produce a struct to read in a file header, in this case an Xbase (.dbf) database file. You define all elements using the appropriate variable type, read in the entire header (or as many bytes as you need to extract the info you need), and then use dllstructGetData to access individual elements as required. Obviously, a tiff file header will have a completely different structure than an Xbase file. But if you're only interested in a single Word, then a straight binary read would suffice.

Edited by RTFC

Share this post


Link to post
Share on other sites

You can use GDI+ to get the amount of pages within a TIFF file:

#include <GDIPlus.au3>

Global $sFile_TIFF = FileOpenDialog("Select a TIF image", "", "TIFF (*.tif;*.tiff)")
If @error Then Exit

_GDIPlus_Startup()
Global $hImage = _GDIPlus_ImageLoadFromFile($sFile_TIFF)
Global $tGUID = _GDIPlus_GIFAnimGetFrameDimensionsList($hImage, _GDIPlus_GIFAnimGetFrameDimensionsCount($hImage))
Global $iCount = _GDIPlus_GIFAnimGetFrameCount($hImage, $tGUID)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_Shutdown()
MsgBox(0, "TIF Page Count", "Pages: " & $iCount)


; #FUNCTION# ====================================================================================================================
; Name ..........: _GDIPlus_GIFAnimGetFrameDimensionsCount
; Description ...: Gets the number of frame dimensions in this Image object.
; Syntax ........: _GDIPlus_GIFAnimGetFrameDimensionsCount($hImage)
; Parameters ....: $hImage              - A handle to an image / bitmap object
; Return values .: The number of frame dimensions in this Image object.
; Author ........: UEZ
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; ===============================================================================================================================
Func _GDIPlus_GIFAnimGetFrameDimensionsCount($hImage)
    Local Const $aResult = DllCall($__g_hGDIPDll, "int", "GdipImageGetFrameDimensionsCount", "handle", $hImage, "ulong*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] Then Return SetError(10, $aResult[0], 0)
    Return $aResult[2]
EndFunc   ;==>_GDIPlus_GIFAnimGetFrameDimensionsCount

; #FUNCTION# ====================================================================================================================
; Name ..........: _GDIPlus_GIFAnimGetFrameCount
; Description ...: Gets the frame count of the loaded gif by passing the GUID struct
; Syntax ........: _GDIPlus_GIFAnimGetFrameCount($hImage, $tGUID)
; Parameters ....: $hImage              - A handle to an image / bitmap object
;                  $tGUID               - A struct to a GUID that specifies the frame dimension.
; Return values .: The amount of frames from a GIF animated image handle
; Author ........: UEZ
; Modified ......:
; Remarks .......:
; Related .......: _GDIPlus_ImageLoadFromFile _GDIPlus_BitmapCreateFromFile
; Link ..........:
; ===============================================================================================================================
Func _GDIPlus_GIFAnimGetFrameCount($hImage, $tGUID)
    Local Const $aResult = DllCall($__g_hGDIPDll, "int", "GdipImageGetFrameCount", "handle", $hImage, "struct*", $tGUID, "ptr*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] Then Return SetError(10, $aResult[0], 0)
    Return Int($aResult[3])
EndFunc   ;==>_GDIPlus_GIFAnimGetFrameCount

; #FUNCTION# ====================================================================================================================
; Name ..........: _GDIPlus_GIFAnimGetFrameDimensionsList
; Description ...: Gets the identifiers for the frame dimensions of this Image object which fills the GUID struct.
; Syntax ........: _GDIPlus_GIFAnimGetFrameDimensionsList($hImage, $iFramesCount)
; Parameters ....: $hImage              - A handle to an image / bitmap object
;                  $iFramesCount        - An integer value.
; Return values .: tagGUID struct
; Author ........: UEZ
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; ===============================================================================================================================
Func _GDIPlus_GIFAnimGetFrameDimensionsList($hImage, $iFramesCount)
    Local Const $tGUID = DllStructCreate($tagGUID)
    Local Const $aResult = DllCall($__g_hGDIPDll, "int", "GdipImageGetFrameDimensionsList", "handle", $hImage, "struct*", $tGUID, "uint", $iFramesCount)
    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] Then Return SetError(10, $aResult[0], 0)
    Return $tGUID
EndFunc   ;==>_GDIPlus_GIFAnimGetFrameDimensionsList

 


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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

@rudi Try this code for TIFF frame count

#include <WinAPIMisc.au3>

$hFile = FileOpen("111111.tif", 16)
If $hFile = -1 Then Exit ConsoleWrite("File not found" & @CRLF)

$ByteOrder = FileRead($hFile, 2)
$Version = FileRead($hFile, 2)
If ($ByteOrder <> 0x4949 And $Version <> 0x2A00) _
Or ($ByteOrder <> 0x4D4D And $Version <> 0x002A) Then Exit ConsoleWrite("No TIFF" & @CRLF)

$FirstIFD = FileRead($hFile, 4)
If $ByteOrder = 0x4949 Then $FirstIFD = _WinAPI_SwapDWord(String($FirstIFD))
FileSetPos($hFile, $FirstIFD, 0)

$Count = 0
Do
  $Count += 1
  $NumTags = FileRead($hFile, 2)
  If $ByteOrder = 0x4949 Then $NumTags = _WinAPI_SwapWord(String($NumTags))
  FileSetPos($hFile, $NumTags * 12, 1)
  $NextIFD = FileRead($hFile, 4)
  If $ByteOrder = 0x4949 Then $NextIFD = _WinAPI_SwapDWord(String($NextIFD))
  FileSetPos($hFile, $NextIFD, 0)
Until Not $NextIFD

FileClose($hFile)
ConsoleWrite($Count & @CRLF)

 

Share this post


Link to post
Share on other sites

@InnI

Hello.

 

That's exactly, what I was looking for! :thumbsup:

 

I have to admit, that I didn't really get the _WinApi_SwapDword() function so far ... :mad2:

I'll do further tries later on to get it :)

 

Thanks a lot !!!

 

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Here another version:

;Coded by UEZ build 2017-10-20 beta
#include <WinAPI.au3>

Global $sFile_TIFF = FileOpenDialog("Select a TIF image", "", "TIFF (*.tif;*.tiff)")
If @error Then Exit
ConsoleWrite("Pages found: " & TIFFPageCount($sFile_TIFF) & @CRLF)

Func TIFFPageCount($sFile_TIFF)
    Local $hFile = _WinAPI_CreateFile($sFile_TIFF, 2, 2), $Bytes
    Local $iFilesize = FileGetSize($sFile_TIFF)
    Local $tData = DllStructCreate("ushort Order;ushort Type;dword IFD")
    _WinAPI_ReadFile($hFile, DllStructGetPtr($tData), 8, $Bytes)
    If Not BitOR($tData.Order = 0x4949, $tData.Order = 0x4D4D) Then
        _WinAPI_CloseHandle($hFile)
        Return SetError(1, 0, 0)
    EndIf
    Local $IFD = $tData.IFD
    Local $tData2 = DllStructCreate("ushort NumDirEntries")
    Local $iTags, $iPages = 0
    Do
        _WinAPI_SetFilePointer($hFile, $IFD)
        _WinAPI_ReadFile($hFile, DllStructGetPtr($tData2), 2, $Bytes)
        If $tData2.NumDirEntries > 256 Then ExitLoop
        $iTags = $tData2.NumDirEntries * 12 + 2
        _WinAPI_SetFilePointer($hFile, $IFD + $iTags)
        _WinAPI_ReadFile($hFile, DllStructGetPtr($tData, "IFD"), 4, $Bytes)
        $IFD = $tData.IFD
        $iPages += 1
    Until $IFD = 0 or $IFD > $iFilesize - 4
    _WinAPI_CloseHandle($hFile)
    Return $iPages
EndFunc

 

I don't know if it is working also for TIFs multi page MM encoded.


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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
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

  • Similar Content

    • By Inpho
      Hi All,
      I intend on keeping custom functions/UDFs (works in progress) here; if anyone wants to use any code, feel free.
      String functions:
      #AutoIt3Wrapper_AU3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include-once ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateTimeGet ; Description ...: Returns the date and time formatted for use in sortable filenames, logs, listviews, etc. ; Syntax ........: _DateTimeGet(iType = 1[, $bHumanFormat = False]) ; Parameters ....: $iType - [optional] an integer value. Default is 1. ; 1 - Date and time in file-friendly format; 20190115_113756 ; 2 - Date in file-friendly format; 20190115 ; 3 - Time in file friendly format; 113756 ; $bHumanFormat - [optional] a boolean value. Default is False. ; True - Includes slashes in the date and colons in the time with a space inbetween ; False - No slashes or colons included with an underscore inbetween ; Return values .: Success - String ; Failure - Sets @error to non-zero and returns an empty string ; Author ........: Sam Coates ; =============================================================================================================================== Func _DateTimeGet($iType = 1, $bHumanFormat = False) If $iType < 1 Or $iType > 3 Then Return (SetError(-1, 0, "")) ;; Param1: ;; 1 = Date and time in file friendly format: 20190115_113756 ;; 2 = Date in file friendly format: 20190115 ;; 3 = Time in file friendly format: 113756 ;; Param2: ;; True = Use human-readable format: 15/01/2019 11:37:56 Local $sTime = @HOUR & ":" & @MIN & ":" & @SEC Local $sDate = @MDAY & "/" & @MON & "/" & @YEAR If $iType = 1 Then If $bHumanFormat = False Then $sTime = StringReplace($sTime, ":", "") $sDate = StringReplace($sDate, "/", "") $sDate = StringTrimLeft($sDate, 4) & StringMid($sDate, 3, 2) & StringLeft($sDate, 2) Return ($sDate & "_" & $sTime) Else Return ($sDate & " " & $sTime) EndIf ElseIf $iType = 2 Then If $bHumanFormat = False Then $sDate = StringReplace($sDate, "/", "") $sDate = StringTrimLeft($sDate, 4) & StringMid($sDate, 3, 2) & StringLeft($sDate, 2) EndIf Return ($sDate) ElseIf $iType = 3 Then If $bHumanFormat = False Then $sTime = StringReplace($sTime, "/", "") EndIf Return ($sTime) EndIf EndFunc ;==>_DateTimeGet ; #FUNCTION# ==================================================================================================================== ; Name ..........: _FileToFileExtension ; Description ...: Returns a file extension from a filename/FQPN (Fully Qualified Path Name) ; Syntax ........: _FileToFileExtension($sPath) ; Parameters ....: $sPath - a string value. ; Return values .: Success - String ; Failure - Empty string as returned from StringTrimLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _FileToFileExtension($sPath) Return (StringTrimLeft($sPath, StringInStr($sPath, ".", 0, -1))) EndFunc ;==>_FileToFileExtension ; #FUNCTION# ==================================================================================================================== ; Name ..........: _FileToFileName ; Description ...: Returns a filename from a FQPN (Fully Qualified Path Name) ; Syntax ........: _FileToFileName($sPath[, $bIncludeExtension = True]) ; Parameters ....: $sPath - a string value. ; $bIncludeExtension - [optional] a boolean value. Default is True. ; Return values .: Success - String ; Failure - Empty string as returned from StringLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _FileToFileName($sPath, $bIncludeExtension = True) Local $sReturn = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1)) If $bIncludeExtension = False Then $sReturn = StringLeft($sReturn, StringInStr($sReturn, ".", 0, -1) - 1) Return ($sReturn) EndFunc ;==>_FileToFileName ; #FUNCTION# ==================================================================================================================== ; Name ..........: _FileToFilePath ; Description ...: Returns a folder path from a FQPN (Fully Qualified Path Name) ; Syntax ........: _FileToFilePath($sPath) ; Parameters ....: $sPath - a string value. ; Return values .: Success - String ; Failure - Empty string as returned from StringLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _FileToFilePath($sPath) Return (StringLeft($sPath, StringInStr($sPath, "\", 0, -1) - 1)) EndFunc ;==>_FileToFilePath ; #FUNCTION# ==================================================================================================================== ; Name ..........: _StringLeft ; Description ...: Searches for a string inside a string, then removes everything on the right of that string ; Syntax ........: _StringLeft($sString, $sRemove[, $iCaseSense = 0, $iOccurrence = 1]) ; Parameters ....: $sString - a string value. The string to search inside. ; $sRemove - a string value. The string to search for. ; $iCaseSense - an integer value. Flag to indicate if the operations should be case sensitive. ; $iOccurrence - an integer value. Which occurrence of the substring to find in the string. Use a ; negative occurrence to search from the right side. ; Return values .: Success - String ; Failure - Empty string as returned from StringLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _StringLeft($sString, $sRemove, $iCaseSense = 0, $iOccurrence = 1) Return (StringLeft($sString, StringInStr($sString, $sRemove, $iCaseSense, $iOccurrence) - 1)) EndFunc ;==>_StringLeft ; #FUNCTION# ==================================================================================================================== ; Name ..........: _StringRandom ; Description ...: Returns a string of random characters ; Syntax ........: _StringRandom($iAmount[, $iType = 1]) ; Parameters ....: $iAmount - an integer value. Length of returned string ; $iType - [optional] an integer value. Default is 1. ; 1 - Return digits (0-9) ; 2 - Return hexadecimal (0-9, A - F) ; 3 - Return Alphanumeric upper (0-9, A - Z) ; 4 - Return Alphanumeric (0-9, A - Z, a - z) ; 5 - Return Alpha upper (A - Z) ; 6 - Return Alpha (A - Z, a - z) ; Return values .: Success - String ; Failure - Empty string and @error flag as follows: ; @error : 1 - $iAmount is not a positive integer ; 2 - $iType is out of bounds ; Author ........: Sam Coates ; =============================================================================================================================== Func _StringRandom($iAmount, $iType = 1) If $iAmount < 1 Or IsInt($iAmount) = 0 Then Return (SetError(-1, 0, "")) Local $sString = "" Local $iRandomLow = 1, $iRandomHigh = 62 #Tidy_Off Local Static $aCharId[63] = [0, Chr(48), Chr(49), Chr(50), Chr(51), Chr(52), Chr(53), Chr(54), Chr(55), Chr(56), Chr(57), Chr(65), Chr(66), Chr(67), _ Chr(68), Chr(69), Chr(70), Chr(71), Chr(72), Chr(73), Chr(74), Chr(75), Chr(76), Chr(77), Chr(78), Chr(79), Chr(80), _ Chr(81), Chr(82), Chr(83), Chr(84), Chr(85), Chr(86), Chr(87), Chr(88), Chr(89), Chr(90), Chr(97), Chr(98), Chr(99), _ Chr(100), Chr(101), Chr(102), Chr(103), Chr(104), Chr(105), Chr(106), Chr(107), Chr(108), Chr(109), Chr(110), Chr(111), _ Chr(112), Chr(113), Chr(114), Chr(115), Chr(116), Chr(117), Chr(118), Chr(119), Chr(120), Chr(121), Chr(122)] #Tidy_On If $iType = 1 Then ;; digits: 1 - 10 $iRandomHigh = 10 ElseIf $iType = 2 Then ;; hexadecimal: 1 - 16 $iRandomHigh = 16 ElseIf $iType = 3 Then ;; alnumupper: 1 - 36 $iRandomHigh = 36 ElseIf $iType = 4 Then ;; alnum: 1 - 62 $iRandomHigh = 62 ElseIf $iType = 5 Then ;; alphaupper: 11 - 36 $iRandomLow = 11 $iRandomHigh = 36 ElseIf $iType = 6 Then ;; alpha: 11 = 62 $iRandomLow = 11 $iRandomHigh = 62 Else Return (SetError(-2, 0, "")) EndIf For $i = 1 To $iAmount $sString &= $aCharId[Random($iRandomLow, $iRandomHigh, 1)] ;; append string with corresponding random character from ascii array Next Return ($sString) EndFunc ;==>_StringRandom ; #FUNCTION# ==================================================================================================================== ; Name ..........: _StringTrimLeft ; Description ...: Searches for a string inside a string, then removes everything on the left of that string ; Syntax ........: _StringTrimLeft($sString, $sRemove[, $iCaseSense = 0, $iOccurrence = 1]) ; Parameters ....: $sString - a string value. The string to search inside. ; $sRemove - a string value. The string to search for. ; $iCaseSense - an integer value. Flag to indicate if the operations should be case sensitive. ; $iOccurrence - an integer value. Which occurrence of the substring to find in the string. Use a ; negative occurrence to search from the right side. ; Return values .: Success - String ; Failure - Empty string as returned from StringTrimLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _StringTrimLeft($sString, $sRemove, $iCaseSense = 0, $iOccurrence = 1) Return (StringTrimLeft($sString, StringInStr($sString, $sRemove, $iCaseSense, $iOccurrence) + StringLen($sRemove) - 1)) EndFunc ;==>_StringTrimLeft Examples:
      ConsoleWrite(_StringRandom(100, 6) & @CRLF) ConsoleWrite(_StringTrimLeft("C:\Windows\System32\cmd.exe", "C:\Windows\System32\") & @CRLF) ConsoleWrite(_StringLeft("C:\Windows\System32\cmd.exe", "cmd.exe") & @CRLF) ConsoleWrite(_FileToFileName("C:\Windows\System32\cmd.exe") & @CRLF) ConsoleWrite(_FileToFilePath("C:\Windows\System32\cmd.exe") & @CRLF) ConsoleWrite(_FileToFileExtension("C:\Windows\System32\cmd.exe") & @CRLF) ConsoleWrite(_StringRandom(6, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(6, 4)& @CRLF)  
    • By dinotom
      Can files be read from the web or the cloud?
      I am trying to read this file but get the file open error.
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> ;Assign the file path to a variable Local $sFilePath = "C:\Automation\test.txt" Local $sFilePathAzure ="https://batlgroupimages.blob.core.windows.net/files/test.txt" Local $nLineNumberToLookFor = 0 ;Open the file test.txt in append mode. ;If the folder C:\Automation does not exist, it will be created. Local $hFileOpen = FileOpen($sFilePathAzure, $FO_APPEND + $FO_CREATEPATH) ;Display a message box in case of any errors. If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred while opening the file.") EndIf ;Set the file position to beginning for reading the data from the beginning of the file. FileSetPos($hFileOpen, 0, $FILE_BEGIN) ;Read the data from the file using the handle returned by FileOpen ;Local $sFileRead = FileRead($hFileOpen) ;Read the 2nd line of data from the file using the handle returned by FileOpen Local $sFileReadLine = FileReadLine ($hFileOpen,2) ;Close the handle returned by FileOpen. FileClose($hFileOpen)  
    • By nacerbaaziz
      hello sirs,
      i have searched allot about an function that can  read the INI file as a  string
      i mean function to read the ini files from string and not from the file directly.
      i finally found an UDF that do what i want
      but unfortunately all the functions work,  but the function that i want it not working.
      this is the udf
      the function that  i need is _IniReadFromString
      this is the function

      Func _IniReadFromString($szInput, $szSection, $szKey, $Default) $szInput = StringStripCR($szInput) Local $aRegMl = StringRegExp($szInput, "\[" & __StringEscapeRegExp($szSection) & "\]\n+(?:[^\[].*?=.*\n)*" & __StringEscapeRegExp($szKey) & "=(.*)\n?(", 3) If @error Then Return SetError(1, 0, $Default) ; key not found Return $aRegMl[0] EndFunc;==>_IniReadFromString
      i hope that any one can help me
      thank you in advance
      iniex.au3
    • By Colduction
      Hello AutoIt Scriptwriters! 
      I want to read https based site that it's address is: Soft98 (https://soft98.ir/)
      I've tried with "_INetGetSource", "BinaryToString(InetRead)" and "InetRead" but none of them don't help me
       
      How can i get this site html source code without opening IE Windows? 
       
×
×
  • Create New...