Jump to content

Bitmap Library


Recommended Posts

@evilertoaster

Yes, now is it working. What was the bug?

Basically, I didn't notice BinaryMid() was not 0-based, (so the first char would be BinaryMid($A,1,10) not BinaryMid($A,0,10)) the actual bug fix just adding +1:

Old:

$color = Hex(BinaryMid($BMPHandle[3],_ChordToOffset($x,$y,$BMPHandle),3))

New:

$color = Hex(BinaryMid($BMPHandle[3],_ChordToOffset($x,$y,$BMPHandle)+1,3))
Edited by evilertoaster
Link to comment
Share on other sites

Ok, version 3 is up. I've opted to slim the library down to it's core BMP and Pixel I/O. All of the other ones seemed more like graphics library functions, which this isn't really supposed to be. As Malkey showed, there are often more efficient ways to handle large drawings than could be given by the built-in functions, so I think I'll leave it up to implementers. Even in my own cases, I found myself using customized versions of the functions too much, so I think this'll be better in the long run.

Link to comment
Share on other sites

  • 1 month later...

when i open an already existing bmp file with _BMPOpen() and then try to read a pixel using _PixelRead() it returns "000000".

the problem is here in _BMPOpen():

Local $BMPHandle[4]
$BMPHandle[0]=$c
$BMPHandle[1]=$x
$BMPHandle[2]=$y
$BMPHandle[3]=$AllOf
return $BMPHandle

i changed it to this and now it works!

$BMPHandle[0]=$c
$BMPHandle[1]=$x
$BMPHandle[2]=$y
$BMPHandle[3]=DllStructCreate('ubyte['&BinaryLen($AllOf)&']')
DllStructSetData($BMPHandle[3],1,$AllOf)
return $BMPHandle

You should update your libray with the fix( if you want;) )

Fuck 'em if they can't take a joke.

Link to comment
Share on other sites

  • 3 months later...
  • 3 months later...

I don't know if this is already noted but:

in the 3.1 download the example is not consistent with the library. Function naming in the example is _Function (ie _createbitmap()) and in the library is Function (ie createbitmap() ,without the leading _)

this prevents the user of a "download and F5 hit" test.

thank you for posting this, although bmp is old format is the most easy to understand non-loosing format. for pixel comparison functions (ie check a screen for a certain motive) is the only working because of not altering the actual image (compress algorithms do this).

I'm saying this because somebody in the first pages commented negative about your work.

Also forgive my syntax, English is as Greek to me, as Greek is Greek for you English-speakers :-)

Link to comment
Share on other sites

Just try this code:

#include <BMP.au3>

$BMP = _BMPCreate(100,100)
For $INDEX = 1 TO 100
    $RND = Random(0,1,1)
    If $RND = 1 Then
        $COLOR = "000000"
    Else
        $COLOR = "FFFFFF"
    EndIf
    _PixelWrite($BMP,Random(0,99,1),Random(0,99,1),"000000")
Next
_BMPWrite($BMP,"pic.bmp",False)

It should put some black and withe points in a bitmap but it doesn't work for black color with value '000000' just if I change with '000001'. It's this a bug or I made something wrong?

When the words fail... music speaks.

Link to comment
Share on other sites

  • 2 weeks later...

Thanks loukaniko, The underscore should have been used in the library. It now matches the example.

@Andreik: Judging from the name of the file in the include<> and the inclusion of a 3rd parameter in _BMPWrite() it looks like you aren't using the latest and greatest version, try using library version 3.2, your code worked fine for me with that.

Edited by evilertoaster
Link to comment
Share on other sites

  • 5 months later...
  • 2 months later...

Hi

I have a Problem with that UDF (I think so)

I have a simple little script but it dont works:

1. make Screenshot to file

2. Make a "Hash" with this UDF

_BMPOpen

_PixelRead

_BMPWrite (to close)

3. delete the file

4. goto 1

When it make it the second time then it dont open the BMP ... but why?

Thx for help

WindWind12

Here the Script (in simple form):

#include <BMP3.au3>
#include <ScreenCapture.au3>

Global $Ausgabe = @Scriptdir & "\Test.bmp"

For $i = 1 to 4
    ;Make Screenshot 2 File
    _ScreenCapture_SetBMPFormat(0)
    _ScreenCapture_Capture($Ausgabe, 1, 1, 100, 7, 0)
    ;Make a Hash (that here is only a sample
    TrayTip("Hash",_MakeHash($i,0),1)
    Sleep (1000)
    ;Delete Screenshot File
    $Del = Filedelete ("Test.bmp")
    If $Del = 0 then msgbox (0,"Error","Cant delete file")
Next
Exit

Func _MakeHash($x,$y)
Local $Hash
$BMP = _BMPOpen($Ausgabe)
;Here is more in my script but its only more of _PixelRead with 2 for next loops
$Hash = _PixelRead($BMP,$x, $y)
_BMPWrite($BMP, $Ausgabe);Closes the BMP to a file
Return $Hash
EndFunc
Edited by windwind12
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 year later...

Really thanks! I needed this and I didn't know at all what to do, I was about to leave my project. [Anyway, should I cite you or somebody else if I include your files?]

Edited by GiulioM
Link to comment
Share on other sites

  • 2 years later...

Is this project still being developed?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

I make few modyfications:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
#include-once
#include <FileConstants.au3>

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.3.6.1
; Author:         Evilertoaster <evilertoaster at yahoo dot com>
;
; Script Function:
;   Basic BMP file managment.
;Script Version:
;   3.3b with mLipok modyfications
;       * CleanUp
;       * #AutoIt3Wrapper_Au3Check_Parameters
;       * Variable Names - UDF Standards
;       * some minior changes
;       * function renaming by add _BMP_
;       * 
; ----------------------------------------------------------------------------

; #FUNCTION# ====================================================================================================================
; Name...........: _BMP_Create
; Description ...: Creates a BMP 'Handle' to be used in later functions. The BMP dimensions will be decided by the parameters.
; Syntax.........: _BMP_Create($iWidth,$iHeight)
; Parameters ....: $iWidth  - "" in pixels
;                  $iHeight - "" in pixels
; Return values .: Success - A BMP handle
; #FUNCTION# ====================================================================================================================
Func _BMP_Create($iWidth, $iHeight)
    Local $c = Mod($iWidth, 4)
    Local $d = Binary("")
    If $c = 3 Then $dSupplement = Binary("0x000000")
    If $c = 2 Then $dSupplement = Binary("0x0000")
    If $c = 1 Then $dSupplement = Binary("0x00")

    ; *** BMP header (54bytes total) ***
    Local $dHeader = Binary("0x424D" & _                    ;2bytes, BM signature
            "00000000" & _                  ;4bytes, filesize (optional, omitted)
            "0000" & _                      ;2bytes, reserved
            "0000" & _                      ;2bytes, reserved
            "36000000" & _                  ;4bytes, offset to image data
            "28000000" & _                  ;4bytes, BITMAPINFOHEADER
            __BMP_Reverse8(Hex($iWidth, 8)) & _     ;4bytes, bitmap width
            __BMP_Reverse8(Hex($iHeight, 8)) & _    ;4bytes, bitmap hieght
            "0100" & _                      ;2bytes, bitmap planes
            "1800" & _                      ;2bytes, bitmap bitdepth
            "00000000" & _                  ;4bytes, bitmap compression type (none)
            __BMP_Reverse8(Hex(($iHeight) * _
            ($iWidth) * 3 + ($iHeight * $c), 8)) & _    ;4bytes, bitmap data size
            "00000000" & _                  ;4bytes, bitmap horizontal resolution (optional,omitted)
            "00000000" & _                  ;4bytes, bitmap vertical resolution (optional,omitted)
            "00000000" & _                  ;4bytes, bitmap colors (optional?, omitted)
            "00000000") ;4bytes, important colors (optional?, omitted)
    ; *** End BMP Header ***

    Local $dRowData = Binary("")
    For $n = 1 To $iWidth
        $dRowData &= Binary("0xFFFFFF")
    Next
    $dRowData &= $dSupplement

    Local $dImageData = Binary("")
    For $m = 1 To $iHeight
        $dImageData &= $dRowData
    Next

    Local $dBinaryData = $dHeader & $dImageData
    Local $aBMP_Handle[4] = [$c, $iWidth, $iHeight, DllStructCreate('ubyte[' & BinaryLen($dBinaryData) & ']')]
    DllStructSetData($aBMP_Handle[3], 1, $dBinaryData)
    Return $aBMP_Handle
EndFunc   ;==>_BMP_Create

; #FUNCTION# ====================================================================================================================
; Name...........: _BMP_Open
; Description ...: Creates a BMP 'Handle' from a BMP file.
; Syntax.........: _BMP_Open($Path)
; Parameters ....: $Path - Path to the file
; Return values .: Success - A BMP handle
;                  Failure - -1: FileOpen() returned -1
;                            -2: Not a valid BMP file header
;                            -3: BMP file header has invalid hieght or width specified
; #FUNCTION# ====================================================================================================================
Func _BMP_Open($Path, $Progress = 1)
    #forceref $Progress
    Local $hBMP_File = FileOpen($Path, $FO_BINARY)
    If $hBMP_File = -1 Then Return -1

    Local $dBMP_FileContents = FileRead($hBMP_File)
    FileClose($hBMP_File)
    If BinaryMid($dBMP_FileContents, 1, 2) <> "0x424D" Then Return -2

    Local $iWidth = Dec(__BMP_Reverse8(Hex(BinaryMid($dBMP_FileContents, 19, 4))))
    Local $iHeight = Dec(__BMP_Reverse8(Hex(BinaryMid($dBMP_FileContents, 23, 4))))
    If $iWidth = 0 Or $iHeight = 0 Then Return -3

    For $c = $iWidth To 0 Step -4
        If $c < 4 Then ExitLoop
    Next
    Local $aBMP_Handle[4] = [$c, $iWidth, $iHeight, DllStructCreate('ubyte[' & BinaryLen($dBMP_FileContents) & ']')]
    DllStructSetData($aBMP_Handle[3], 1, $dBMP_FileContents)
    Return $aBMP_Handle
EndFunc   ;==>_BMP_Open

; #FUNCTION# ====================================================================================================================
; Name...........: _BMP_PixelRead
; Description ...: Returns the color at the pixel coordinate and BMPHandle given.
; Syntax.........: _BMP_PixelRead($aBMP_Handle,$X,$Y)
; Parameters ....: $aBMP_Handle - ""
;                  $X         - X coordinate of the pixel
;                  $Y         - Y coordinate of the pixel
; Return values .: Success -  A Color as a hex string in RRGGBB format
; #FUNCTION# ====================================================================================================================
Func _BMP_PixelRead(ByRef $aBMP_Handle, $x, $y)
    If IsArray($aBMP_Handle) = False Or $x > $aBMP_Handle[1] - 1 Or $x < 0 Or $y > $aBMP_Handle[2] - 1 Or $y < 0 Then Return 0

    Local $offset = __BMP_ChordToOffset($x, $y, $aBMP_Handle)
    Return Hex(DllStructGetData($aBMP_Handle[3], 1, $offset + 3), 2) & Hex(DllStructGetData($aBMP_Handle[3], 1, $offset + 2), 2) & Hex(DllStructGetData($aBMP_Handle[3], 1, $offset + 1), 2)
EndFunc   ;==>_BMP_PixelRead

; #FUNCTION# ====================================================================================================================
; Name...........: _BMP_PixelWrite
; Description ...: Writes the color to the pixel coordinate and BMPHandle given.
; Syntax.........: _BMP_PixelWrite($aBMP_Handle, $iX, $iY, $Color)
; Parameters ....: $aBMP_Handle - ""
;                  $iX        - X coordinate of the pixel
;                  $iY        - Y coordinate of the pixel
;                  $Color     - A Color as a hex string in RRGGBB format (e.g. "AAFF00")
; Return values .: Success -  Returns 1
; #FUNCTION# ====================================================================================================================
Func _BMP_PixelWrite(ByRef $aBMP_Handle, $iX, $iY, $color)
    If $iX > $aBMP_Handle[1] - 1 Or $iX < 0 Or $iY > $aBMP_Handle[2] - 1 Or $iY < 0 Or StringLen($color) <> 6 Then Return 0

    Local $offset = __BMP_ChordToOffset($iX, $iY, $aBMP_Handle)
    DllStructSetData($aBMP_Handle[3], 1, Dec(StringLeft($color, 2)), $offset + 3)
    DllStructSetData($aBMP_Handle[3], 1, Dec(StringMid($color, 3, 2)), $offset + 2)
    DllStructSetData($aBMP_Handle[3], 1, Dec(StringRight($color, 2)), $offset + 1)
    Return 1
EndFunc   ;==>_BMP_PixelWrite

; #FUNCTION# ====================================================================================================================
; Name...........: _BMP_Write
; Description ...: Writes the BMP Handle $aBMP_Handle to the file path $sBMP_FileFullPath.
; Syntax.........: _BMP_Write($aBMP_Handle,$sBMP_FileFullPath)
; Parameters ....: $aBMP_Handle - ""
;                  $sBMP_FileFullPath     - Path to the file;
; Return values .: Success - Returns 1
;                  Failure - -1: FileOpen() returned -1
; #FUNCTION# ====================================================================================================================
Func _BMP_Write(ByRef $aBMP_Handle, $sBMP_FileFullPath)
    If IsArray($aBMP_Handle) = False Then Return 0

    Local $out = FileOpen($sBMP_FileFullPath, $FO_OVERWRITE + $FO_BINARY)
    If $out = -1 Then Return -1

    FileWrite($out, DllStructGetData($aBMP_Handle[3], 1))
    FileClose($out)
    Return 1
EndFunc   ;==>_BMP_Write

; #FUNCTION# ====================================================================================================================
; Name...........: _BMP_GetWidth
; Description ...: Returns the width in pixels of the BMP. Same as $aBMP_Handle[1].
; Syntax.........: _BMP_GetWidth($aBMP_Handle)
; Parameters ....: $aBMP_Handle - ""
; #FUNCTION# ====================================================================================================================
Func _BMP_GetWidth(ByRef $aBMP_Handle)
    If UBound($aBMP_Handle) <> 4 Then Return 0

    Return $aBMP_Handle[1]
EndFunc   ;==>_BMP_GetWidth

; #FUNCTION# ====================================================================================================================
; Name...........: _BMP_GetHeight
; Description ...: Returns the Height in pixels of the BMP. Same as $aBMP_Handle[2].
; Syntax.........: _BMP_GetHeight($aBMP_Handle)
; Parameters ....: $aBMP_Handle - ""
; #FUNCTION# ====================================================================================================================
Func _BMP_GetHeight(ByRef $aBMP_Handle)
    If UBound($aBMP_Handle) <> 4 Then Return 0

    Return $aBMP_Handle[2]
EndFunc   ;==>_BMP_GetHeight

; #FUNCTION# ====================================================================================================================
; Name...........: _BMP_PixelFill
; Description ...: Does a paint "fill" effect on the given pixel with the given color. It will also shade in pixels within the
;                  given shade variation (defualt 0 is exact match for pixel color, 1 would allow 1 shade lighter or darker than
;                  the starting pixel ect.).
; Syntax.........: _BMP_PixelFill($aBMP_Handle,$x,$y,$color[,$variation=0])
; Parameters ....: $aBMP_Handle, $x, $y, $color see _BMP_PixelWrite()
; Return values .: Success - Returns the number of pixels filled
; #FUNCTION# ====================================================================================================================
Func _BMP_PixelFill(ByRef $aBMP_Handle, $x, $y, $color, $variation = 0)
    Local $aCheckChart[UBound($aBMP_Handle, 1)][UBound($aBMP_Handle, 2)]
    #forceref $aCheckChart
    Local $iCount = 0
    Local $iTset = 1
    Local $aTracer[$iTset]
    $aTracer[$iTset - 1] = $x & "," & $y
    Local $CheckColor = Dec(_BMP_PixelRead($aBMP_Handle, $x, $y))
    $aCheckChart[$y][$x] = 1
    Local $aPoint

    While 1
        If Abs(Dec(_BMP_PixelRead($aBMP_Handle, $x - 1, $y)) - $CheckColor) <= $variation Then
            $aCheckChart[$x - 1][$y] = 1
            $iCount += 1
            _BMP_PixelWrite($aBMP_Handle, $x - 1, $y, $color)
            $iTset += 1
            ReDim $aTracer[$iTset]
            $aTracer[$iTset - 1] = $x & "," & $y
            $x = $x - 1
            ContinueLoop
        EndIf
        If Abs(Dec(_BMP_PixelRead($aBMP_Handle, $x, $y - 1)) - $CheckColor) <= $variation Then
            $aCheckChart[$x][$y - 1] = 1
            $iCount += 1
            _BMP_PixelWrite($aBMP_Handle, $x, $y - 1, $color)
            $iTset += 1
            ReDim $aTracer[$iTset]
            $aTracer[$iTset - 1] = $x & "," & $y
            $y = $y - 1
            ContinueLoop
        EndIf
        If Abs(Dec(_BMP_PixelRead($aBMP_Handle, $x + 1, $y)) - $CheckColor) <= $variation Then
            $aCheckChart[$x + 1][$y] = 1
            $iCount += 1
            _BMP_PixelWrite($aBMP_Handle, $x + 1, $y, $color)
            $iTset += 1
            ReDim $aTracer[$iTset]
            $aTracer[$iTset - 1] = $x & "," & $y
            $x = $x + 1
            ContinueLoop
        EndIf
        If Abs(Dec(_BMP_PixelRead($aBMP_Handle, $x, $y + 1)) - $CheckColor) <= $variation Then
            $aCheckChart[$x][$y + 1] = 1
            $iCount += 1
            _BMP_PixelWrite($aBMP_Handle, $x, $y + 1, $color)
            $iTset += 1
            ReDim $aTracer[$iTset]
            $aTracer[$iTset - 1] = $x & "," & $y
            $y = $y + 1
            ContinueLoop
        EndIf
        $aPoint = StringSplit($aTracer[$iTset - 1], ",")
        $x = $aPoint[1]
        $y = $aPoint[2]
        $iTset -= 1
        ReDim $aTracer[$iTset]
        If $iTset = 1 Then ExitLoop
    WEnd

    Return $iCount
EndFunc   ;==>_BMP_PixelFill

Func __BMP_ChordToOffset($x, $y, ByRef $aBMP_Handle)
    Local $row = ($aBMP_Handle[1] * 3 + $aBMP_Handle[0])
    Return 54 + (($aBMP_Handle[2] * $row) - (($y + 1) * $row) + ($x * 3))
EndFunc   ;==>__BMP_ChordToOffset

Func __BMP_Reverse8($inHex)
    Return StringMid($inHex, 7, 2) & StringMid($inHex, 5, 2) & StringMid($inHex, 3, 2) & StringMid($inHex, 1, 2)
EndFunc   ;==>__BMP_Reverse8

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 3 years later...

BMP3.3 produces errors in BMP3.au3

here is a small fix:

#include-once
; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.3.6.1
; Author:         Evilertoaster <evilertoaster at yahoo dot com>
;
; Script Function:
;   Basic BMP file managment.
;Script Version:
;   3.3
; ----------------------------------------------------------------------------

; #FUNCTION# ====================================================================================================================
; Name...........: _BMPCreate
; Description ...: Creates a BMP 'Handle' to be used in later functions. The BMP dimensions will be decided by the parameters.
; Syntax.........: _BMPCreate($Width,$Height)
; Parameters ....: $Width  - "" in pixels
;                  $Height - "" in pixels
; Return values .: Success - A BMP handle
; #FUNCTION# ====================================================================================================================
Func _BMPCreate ($Width,$Height)
    Local $c=Mod($Width,4)
    Local $d=Binary("")
    if $c=3 then $d=Binary("0x000000")
    if $c=2 then $d=Binary("0x0000")
    if $c=1 then $d=Binary("0x00")
                                                        ;***BMP header (54bytes total)***
    Local $Header=Binary("0x424D"& _                    ;2bytes, BM signature
                        "00000000"& _                   ;4bytes, filesize (optional, omitted)
                        "0000"& _                       ;2bytes, reserved
                        "0000"& _                       ;2bytes, reserved
                        "36000000"& _                   ;4bytes, offset to image data
                        "28000000"& _                   ;4bytes, BITMAPINFOHEADER
                        _Reverse8(Hex($Width,8))& _     ;4bytes, bitmap width
                        _Reverse8(Hex($Height,8))& _    ;4bytes, bitmap hieght
                        "0100"& _                       ;2bytes, bitmap planes
                        "1800"& _                       ;2bytes, bitmap bitdepth
                        "00000000"& _                   ;4bytes, bitmap compression type (none)
                        _Reverse8(Hex(($Height)* _
                        ($Width)*3+($Height*$c),8))& _  ;4bytes, bitmap data size
                        "00000000"& _                   ;4bytes, bitmap horizontal resolution (optional,omitted)
                        "00000000"& _                   ;4bytes, bitmap vertical resolution (optional,omitted)
                        "00000000"& _                   ;4bytes, bitmap colors (optional?, omitted)
                        "00000000")                     ;4bytes, important colors (optional?, omitted)
                                                        ;***End Header***
    Local $rowData=Binary("")
    Local $imageData=Binary("")
    for $n=1 to $Width
        $rowData&=Binary("0xFFFFFF")
    Next
    $rowData&=$d
    for $m=1 to $Height
        $imageData&=$rowData
    Next
    Local $binaryData=$Header&$imageData
    Local $BMPHandle[4]
    $BMPHandle[0]=$c
    $BMPHandle[1]=$Width
    $BMPHandle[2]=$Height
    $BMPHandle[3]=DllStructCreate('ubyte['&BinaryLen($binaryData)&']')
    DllStructSetData($BMPHandle[3],1,$binaryData)
    Return $BMPHandle
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _BMPOpen
; Description ...: Creates a BMP 'Handle' from a BMP file.
; Syntax.........: _BMPOpen($Path)
; Parameters ....: $Path - Path to the file
; Return values .: Success - A BMP handle
;                  Failure - -1: FileOpen() returned -1
;                            -2: Not a valid BMP file header
;                            -3: BMP file header has invalid hieght or width specified
; #FUNCTION# ====================================================================================================================
Func _BMPOpen($Path,$Progress=1)
    Local $Bpath=FileOpen($Path,16)
    If $Bpath=-1 then Return -1
    $AllOf=FileRead($Bpath)
    FileClose($Bpath)
    If BinaryMid($AllOf,1,2)<>"0x424D" then Return -2
    $x=Dec(_Reverse8(Hex(BinaryMid($AllOf,19,4))))
    $y=Dec(_Reverse8(Hex(BinaryMid($AllOf,23,4))))
    if $x=0 or $y=0 then Return -3
    for $c=$x to 0 step -4
        if $c<4 then ExitLoop
    Next
    Local $BMPHandle[4]
    $BMPHandle[0]=$c
    $BMPHandle[1]=$x
    $BMPHandle[2]=$y
    $BMPHandle[3]=DllStructCreate('ubyte['&BinaryLen($AllOf)&']')
    DllStructSetData($BMPHandle[3],1,$AllOf)
    return $BMPHandle
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _PixelRead
; Description ...: Returns the color at the pixel coordinate and BMPHandle given.
; Syntax.........: _PixelRead($BMPHandle,$X,$Y)
; Parameters ....: $BMPHandle - ""
;                  $X         - X coordinate of the pixel
;                  $Y         - ""
; Return values .: Success -  A Color as a hex string in RRGGBB format
; #FUNCTION# ====================================================================================================================
Func _PixelRead(ByRef $BMPHandle,$x,$y)
    If IsArray($BMPHandle)=False or $x>$BMPHandle[1]-1 Or $x<0 Or $y>$BMPHandle[2]-1 Or $y<0 Then Return 0
    local $offset = _ChordToOffset($BMPHandle, $x, $y)
    Return hex(DllStructGetData($BMPHandle[3],1,$offset+3),2)&hex(DllStructGetData($BMPHandle[3],1,$offset+2),2)&hex(DllStructGetData($BMPHandle[3],1,$offset+1),2)
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _PixelWrite
; Description ...: Writes the color to the pixel coordinate and BMPHandle given.
; Syntax.........: _PixelWrite($BMPHandle,$X,$Y,$Color)
; Parameters ....: $BMPHandle - ""
;                  $X         - X coordinate of the pixel
;                  $Y         - ""
;                  $Color     - A Color as a hex string in RRGGBB format (e.g. "AAFF00")
; Return values .: Success -  Returns 1
; #FUNCTION# ====================================================================================================================
Func _PixelWrite(ByRef $BMPHandle,$x,$y,$color)
    If $x>$BMPHandle[1]-1 Or $x<0 Or $y>$BMPHandle[2]-1 Or $y<0 or StringLen($color)<>6 Then Return 0
    local $offset = _ChordToOffset($BMPHandle, $x, $y)
    DllStructSetData($BMPHandle[3],1,dec(StringLeft($color,2)),$offset+3)
    DllStructSetData($BMPHandle[3],1,dec(stringmid($color,3,2)),$offset+2)
    DllStructSetData($BMPHandle[3],1,dec(StringRight($color,2)),$offset+1)
    Return 1
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _BMPWrite
; Description ...: Writes the BMP Handle $BMPHandle to the file path $Fpath.
; Syntax.........: _BMPWrite($BMPHandle,$Fpath)
; Parameters ....: $BMPHandle - ""
;                  $Fpath     - Path to the file;
; Return values .: Success - Returns 1
;                  Failure - -1: FileOpen() returned -1
; #FUNCTION# ====================================================================================================================
Func _BMPWrite(ByRef $BMPHandle,$Fpath)
    if IsArray($BMPHandle)=False then Return 0
    $out=FileOpen($Fpath,18)
    if $out=-1 then return -1
    FileWrite($out,DllStructGetData($BMPHandle[3],1))
    FileClose($out)
    Return 1
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _BMPGetWidth
; Description ...: Returns the width in pixels of the BMP. Same as $BMPHandle[1].
; Syntax.........: _BMPGetWidth($BMPHandle)
; Parameters ....: $BMPHandle - ""
; #FUNCTION# ====================================================================================================================
Func _BMPGetWidth(ByRef $BMPHandle)
    If IsArray($BMPHandle)=0 Then Return 0
    Return $BMPHandle[1]
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _BMPGetHeight
; Description ...: Returns the Height in pixels of the BMP. Same as $BMPHandle[2].
; Syntax.........: _BMPGetHeight($BMPHandle)
; Parameters ....: $BMPHandle - ""
; #FUNCTION# ====================================================================================================================
Func _BMPGetHeight(ByRef $BMPHandle)
    If IsArray($BMPHandle)=0 Then Return 0
    Return $BMPHandle[2]
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _PixelFill
; Description ...: Does a paint "fill" effect on the given pixel with the given color. It will also shade in pixels within the
;                  given shade variation (defualt 0 is exact match for pixel color, 1 would allow 1 shade lighter or darker than
;                  the starting pixel ect.).
; Syntax.........: _PixelFill($BMPHandle,$x,$y,$color[,$variation=0])
; Parameters ....: $BMPHandle,$x,$y,$color see _PixelWrite()
; Return values .: Success - Returns the number of pixels filled
; #FUNCTION# ====================================================================================================================
Func _PixelFill(ByRef $BMPHandle,$x,$y,$color,$variation=0)
    Local $CheckChart[UBound($BMPHandle,1)][UBound($BMPHandle,2)]
    Local $count=0
    $Tset=1
    Local $tracer[$Tset]
    $tracer[$tset-1]=$x&","&$y
    Local $CheckColor=Dec(_PixelRead($BMPHandle,$x,$y))
    $CheckChart[$y][$x]=1
    While 1
        if Abs(Dec(_PixelRead($BMPHandle,$x-1,$y))-$CheckColor)<=$variation Then
            $CheckChart[$x-1][$y]=1
            $count+=1
            _PixelWrite($BMPHandle,$x-1,$y,$color)
            $Tset+=1
            ReDim $tracer[$Tset]
            $tracer[$Tset-1]=$x&","&$y
            $x=$x-1
            ContinueLoop
        EndIf
        if Abs(Dec(_PixelRead($BMPHandle,$x,$y-1))-$CheckColor)<=$variation Then
            $CheckChart[$x][$y-1]=1
            $count+=1
            _PixelWrite($BMPHandle,$x,$y-1,$color)
            $Tset+=1
            ReDim $tracer[$Tset]
            $tracer[$Tset-1]=$x&","&$y
            $y=$y-1
            ContinueLoop
        EndIf
        if Abs(Dec(_PixelRead($BMPHandle,$x+1,$y))-$CheckColor)<=$variation Then
            $CheckChart[$x+1][$y]=1
            $count+=1
            _PixelWrite($BMPHandle,$x+1,$y,$color)
            $Tset+=1
            ReDim $tracer[$Tset]
            $tracer[$Tset-1]=$x&","&$y
            $x=$x+1
            ContinueLoop
        EndIf
        if Abs(Dec(_PixelRead($BMPHandle,$x,$y+1))-$CheckColor)<=$variation Then
            $CheckChart[$x][$y+1]=1
            $count+=1
            _PixelWrite($BMPHandle,$x,$y+1,$color)
            $Tset+=1
            ReDim $tracer[$Tset]
            $tracer[$Tset-1]=$x&","&$y
            $y=$y+1
            ContinueLoop
        EndIf
        $Point=StringSplit($tracer[$Tset-1],",")
        $x=$Point[1]
        $y=$Point[2]
        $Tset-=1
        ReDim $tracer[$Tset]
        if $tset=1 then ExitLoop
    Wend
    Return $count
EndFunc

Func _ChordToOffset(ByRef $BMPHandle, $x,$y)
    Local $row=($BMPHandle[1]*3+$BMPHandle[0])
    return 54+(($BMPHandle[2]*$row)-(($y+1)*$row)+($x*3))
EndFunc

Func _Reverse8($inHex)
    Return StringMid($inHex,7,2)&StringMid($inHex,5,2)&StringMid($inHex,3,2)&StringMid($inHex,1,2)
EndFunc

 

Link to comment
Share on other sites

  • 3 months later...

Forgive me for necro-posting, but my issue applies directly to the code here. I'm working on an image converter that outputs BMPs and ran into an issue. BMPs read image data in 4byte chunks (word) so they require padding at the end of each pixel line to bring the width up to a multiple of 4. The included code to do this is:

 

    Local $c=Mod($Width,4)
    Local $d=Binary("")
    if $c=3 then $d=Binary("0x000000")
    if $c=2 then $d=Binary("0x0000")
    if $c=1 then $d=Binary("0x00")
 

 

But isn't that reversed? If for example, the width was 15, it would require 1 byte padding to reach 16 (aka multiple of 4).

Mod(15) would return a remainder of 3, and thus the above code would attach 3 bytes padding instead of 1. Am I just being stupid and missing something obvious?

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...