Gdip_AlphaMask MCode x64 Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kauan014
Posts: 55
Joined: 18 Feb 2021, 20:03

Gdip_AlphaMask MCode x64  Topic is solved

Post by kauan014 » 18 Jan 2022, 01:33

Would like to ask for the Mcode version to ahk x64, in the function Gdip_AlphaMask_rectangle posted by @noname in this topic:

viewtopic.php?t=33410

Code: Select all

SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1

ofile=screengrab.png

If !pToken := Gdip_Startup()
{
   MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
   ExitApp
}

bmap:=Gdip_BitmapFromScreen("100|100|300|200")
pBitmap :=Gdip_AlphaMask_rectangle(bmap,30,1)
Gdip_SaveBitmapToFile(pBitmap,oFile)
return 



Gdip_AlphaMask_rectangle(source,r,bm) ; r radius rounded rectangle / set bm  for bitmap=1 file=1
  {

    x:=y:=0
    static _AlphaMask
    If !_AlphaMask
      {
        MCode_AlphaMask := "518B4424249983E20303C28BC88B442428995383E20303C28B5424245556C1F902C1F802837C24400057757E85D20F8E0E01000"
        . "08B5C241C8B74242C03C003C0894424388D048D000000000FAF4C2440034C243C894424348B4424208D3C888954244485F67E2C8B5424182B5424208"
        . "BCF8BC38B2C0A332883C00481E5FFFFFF003368FC83C10483EE018969FC75E48B74242C037C2434035C2438836C24440175C15F5E5D33C05B59C385D"
        . "20F8E900000008B5C241C8B74242C03C003C0894424388D048D000000000FAF4C2440034C243C894424348B442420895C24448D3C888954241085F67"
        . "E428B5424182B5424208BC78BCBEB098DA424000000008BFF8B1981E3000000FFBD000000FF2BEB8B1C1081E3FFFFFF000BEB892883C10483C00483E"
        . "E0175D98B74242C8B5C2444035C2438037C2434836C241001895C244475A35F5E5D33C05B59C3"

        VarSetCapacity(_AlphaMask, StrLen(MCode_AlphaMask)//2)
        Loop % StrLen(MCode_AlphaMask)//2      ;%
            NumPut("0x" SubStr(MCode_AlphaMask, (2*A_Index)-1, 2), _AlphaMask, A_Index-1, "char")
      }
      
    if !bm 
    pBitmap_:=Gdip_CreateBitmapFromFile(source)
    else
    pBitmap_:=source
    Gdip_GetDimensions(pBitmap_, w,h )
w1:=w+1
h1:=h+1
    pBitmap := Gdip_CreateBitmap(w1,h1)
    G_:= Gdip_GraphicsFromImage(pBitmap), Gdip_SetSmoothingMode(G_, 4), Gdip_SetInterpolationMode(G_, 7)
    Gdip_DrawImage(G_, pBitmap_, 0, 0, w1,h1,0,0,w1,h1)
    Gdip_DeleteGraphics(G_)
    Gdip_DisposeImage(pBitmap_)

    pBitmapMask := Gdip_CreateBitmap(w1, h1), G2 := Gdip_GraphicsFromImage(pBitmapMask),Gdip_SetSmoothingMode(G2, 4)
    pBrush := Gdip_BrushCreateSolid(0xff00ff00)
    Gdip_FillRoundedRectangle(G2, pBrush, 0, 0, w1, h1, r)

    Gdip_DeleteBrush(pBrush)
    Gdip_DeleteGraphics(G2)


    pBitmapNew := Gdip_CreateBitmap(w1, h1)
    If !pBitmapNew
        Return -1

    E1 := Gdip_LockBits(pBitmap, 0, 0, w1, h1, Stride1, Scan01, BitmapData1)
    E2 := Gdip_LockBits(pBitmapMask, 0, 0, w1, h1, Stride2, Scan02, BitmapData2)
    E3 := Gdip_LockBits(pBitmapNew, 0, 0, w1, h1, Stride3, Scan03, BitmapData3)
    If (E1 || E2 || E3)
        Return -2

    E := DllCall(&_AlphaMask, "ptr", Scan01, "ptr", Scan02, "ptr", Scan03, "int", w, "int", h, "int", w, "int", h, "int", Stride1, "int", Stride2, "int", x, "int", y, "int", invert)

    Gdip_UnlockBits(pBitmap, BitmapData1), Gdip_UnlockBits(pBitmapMask, BitmapData2), Gdip_UnlockBits(pBitmapNew, BitmapData3)
    Gdip_DisposeImage(pBitmap)
    Gdip_DisposeImage(pBitmapMask)
    Return (E = "") ? -3 : pBitmapNew
  }

The code above works fine in ahk x32.



I saw that he also created a topic asking about the MCode for the x64 version:
viewtopic.php?f=5&t=39077

Code: Select all

int Gdip_AlphaMask(unsigned int * Bitmap, unsigned int * BitmapMask, unsigned int * BitmapNew, int w1, int h1, int w2, int h2, int Stride1, int Stride2, int sx, int sy, int invert)
{
    int o1 = Stride1/4, o2 = Stride2/4;
    if (!invert)
    {
        for (int y = 0; y < h2; ++y)
        {
            for (int x = 0; x < w2; ++x)
            {
                BitmapNew[(x+sx)+(y+sy)*o1] = (BitmapMask[x+(y*o2)] & 0xff000000) | (Bitmap[(x+sx)+(y+sy)*o1] & 0x00ffffff);
            }
        }
    }
    else
    {
        for (int y = 0; y < h2; ++y)
        {
            for (int x = 0; x < w2; ++x)
            {
                BitmapNew[(x+sx)+(y+sy)*o1] = (0xff000000 - (BitmapMask[x+(y*o2)] & 0xff000000)) | (Bitmap[(x+sx)+(y+sy)*o1] & 0x00ffffff);
            }
        }
    }
    return 0;
}


The function that the user LinearSpon post is different, it doesn't have an option for the radius.
I also tried to copy the LinearSpon Mcode and replace in the Gdip_AlphaMask_rectangle function, but it also did not worked.




-----EDIT----

Got the function working on x64, thanks to @CloakerSmoker

Code: Select all



; r radius rounded rectangle / set bm  for bitmap=1 file=1
Gdip_AlphaMask_rectangle(source,r,bm) {

   x:=y:=0

   if !bm 
      pBitmap_:=Gdip_CreateBitmapFromFile(source)
   else
      pBitmap_:=source
   Gdip_GetImageDimension(pBitmap_, w,h )
   w1:=w+1
   h1:=h+1
   pBitmap := Gdip_CreateBitmap(w1,h1)
   G_:= Gdip_GraphicsFromImage(pBitmap), Gdip_SetSmoothingMode(G_, 4), Gdip_SetInterpolationMode(G_, 7)
   Gdip_DrawImage(G_, pBitmap_, 0, 0, w1,h1,0,0,w1,h1)
   Gdip_DeleteGraphics(G_)
   Gdip_DisposeImage(pBitmap_)

   pBitmapMask := Gdip_CreateBitmap(w1, h1), G2 := Gdip_GraphicsFromImage(pBitmapMask),Gdip_SetSmoothingMode(G2, 4)
   pBrush := Gdip_BrushCreateSolid(0xff00ff00)
   Gdip_FillRoundedRectangle(G2, pBrush, 0, 0, w1, h1, r)

   Gdip_DeleteBrush(pBrush)
   Gdip_DeleteGraphics(G2)

   pBitmapNew := Gdip_CreateBitmap(w1, h1)
   If !pBitmapNew
      Return -1

   E1 := Gdip_LockBits(pBitmap, 0, 0, w1, h1, Stride1, Scan01, BitmapData1)
   E2 := Gdip_LockBits(pBitmapMask, 0, 0, w1, h1, Stride2, Scan02, BitmapData2)
   E3 := Gdip_LockBits(pBitmapNew, 0, 0, w1, h1, Stride3, Scan03, BitmapData3)
   If (E1 || E2 || E3)
      Return -2

   E := DllCall(LoadGdip_AlphaMask(), "ptr", Scan01, "ptr", Scan02, "ptr", Scan03, "int", w, "int", h, "int", w, "int", h, "int", Stride1, "int", Stride2, "int", x, "int", y, "int", invert)

   Gdip_UnlockBits(pBitmap, BitmapData1), Gdip_UnlockBits(pBitmapMask, BitmapData2), Gdip_UnlockBits(pBitmapNew, BitmapData3)
   Gdip_DisposeImage(pBitmap)
   Gdip_DisposeImage(pBitmapMask)
   Return (E = "") ? -3 : pBitmapNew
}



LoadGdip_AlphaMask() {
	static CodeBase64 := ""
. "HbEAVUiJ5UiD7CAASIlNEEiJVRgATIlFIESJTSgAi0VIjVADhcAAD0jCwfgCiUUQ7ItFUApA6IN9AGgAD4XIAAAAiMdF/AAKAOmrAhYK+AIWjwAI"
. "i0X8DwCvReiJwotF+AAB0EiYSI0UhQEBOEiLRRhIAdAIiwAlABj/QYnAAItV+ItFWI0MAAKLVfyLRWABotAANuwByAoxEAMxQP///wCJwQMwRBUM"
. "MUQMMiAAMkSJwmAJyokQgwCBAIU7IEU4D4xlAEODRQL8AA/8O0VAD4xSSYAH6cWCXfSCXa1VggXwggWRgmP0hWPwqZJj99CHZPCFZPSkZK8BGINk"
. "hxiaZPCAXPCCZKpjgmT0gAf0gmRHgAcCuAIZg8QgXcOQAQAA"
	static Code := false
	if ((A_PtrSize * 8) != 64) {
		Throw Exception("LoadGdip_AlphaMask does not support " (A_PtrSize * 8) " bit AHK, please run using 64 bit AHK")
	}
	; MCL standalone loader https://github.com/G33kDude/MCLib.ahk
	; Copyright (c) 2021 G33kDude, CloakerSmoker (CC-BY-4.0)
	; https://creativecommons.org/licenses/by/4.0/
	if (!Code) {
		if !DllCall("Crypt32\CryptStringToBinary", "Str", CodeBase64, "UInt", 0, "UInt", 1, "UPtr", 0, "UInt*", CompressedSize, "Ptr", 0, "Ptr", 0, "UInt")
			throw Exception("Failed to parse LoadGdip_AlphaMask b64 to binary")
		CompressedSize := VarSetCapacity(DecompressionBuffer, CompressedSize, 0)
		if !DllCall("Crypt32\CryptStringToBinary", "Str", CodeBase64, "UInt", 0, "UInt", 1, "Ptr", &DecompressionBuffer, "UInt*", CompressedSize, "Ptr", 0, "Ptr", 0, "UInt")
			throw Exception("Failed to convert MCLib b64 to binary")
		if !(pCode := DllCall("GlobalAlloc", "UInt", 0, "Ptr", 480, "Ptr"))
			throw Exception("Failed to reserve LoadGdip_AlphaMask memory")
		if (DllCall("ntdll\RtlDecompressBuffer", "UShort", 0x102, "Ptr", pCode, "UInt", 480, "Ptr", &DecompressionBuffer, "UInt", CompressedSize, "UInt*", DecompressedSize, "UInt"))
			throw Exception("Error calling RtlDecompressBuffer",, Format("0x{:08x}", r))
		if !DllCall("VirtualProtect", "Ptr", pCode, "Ptr", 480, "UInt", 0x40, "UInt*", OldProtect, "UInt")
			Throw Exception("Failed to mark LoadGdip_AlphaMask memory as executable")
		Code := pCode + 0
	}

	return Code
}


}

Return to “Ask for Help (v1)”