Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

GDI+ standard library 1.45 by tic


  • Please log in to reply
1385 replies to this topic
tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007
Here is Gdip_AlphaMask

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile("MJ.jpg")
Gdip_GetDimensions(pBitmap, w, h)

pBitmapMask := Gdip_CreateBitmap(w, h), G2 := Gdip_GraphicsFromImage(pBitmapMask)
Gdip_SetSmoothingMode(G2, 4)

pBrush := Gdip_BrushCreateSolid(0xff00ff00)
Gdip_FillEllipse(G2, pBrush, 0, 0, w, h)
Gdip_DeleteBrush(pBrush)

pBitmapNew := Gdip_AlphaMask(pBitmap, pBitmapMask, 0, 0)
Gdip_SaveBitmapToFile(pBitmapNew, "Circle.png")

Gdip_DisposeImage(pBitmap), Gdip_DisposeImage(pBitmapMask), Gdip_DisposeImage(pBitmapNew)
Gdip_Shutdown(pToken)
return

;#######################################################################

Gdip_AlphaMask(pBitmap, pBitmapMask, x, y)
{
	static _AlphaMask
	if !_AlphaMask
	{
		MCode_AlphaMask := "8B4424209983E2038D0C028B4424249983E20303C28B54241CC1F902C1F80285D27E7303C003C08944241C8D048D000000000FA"
		. "F4C242C034C2428538B5C240C55568B742424894424308B442418578D3C888954243085F67E2A8B5424142B54241C8BCF8BC38B2C0A332883C00481E"
		. "5FFFFFF003368FC83C1044E8969FC75E68B742428037C2434035C242CFF4C243075C45F5E5D5B33C0C3"

		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")
	}
	Gdip_GetDimensions(pBitmap, w1, h1), Gdip_GetDimensions(pBitmapMask, w2, h2)
	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, w2, h2, 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", w1, "int", h1, "int", w2, "int", h2, "int", Stride1, "int", Stride2, "int", x, "int", y)
	
	Gdip_UnlockBits(pBitmap, BitmapData1), Gdip_UnlockBits(pBitmapMask, BitmapData2), Gdip_UnlockBits(pBitmapNew, BitmapData3)
	return (E = "") ? -3 : pBitmapNew
}

A Source bitmap and a mask bitmap can be supplied, and the returned bitmap from the function will be a new bitmap from the source, using the mask's transparencies. Simple example above only uses 1 circle as a mask

Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010
Neat! Something sorely needed in certain situations.

Edit: will this be included in the next GDI+ lib version?

Edit 2: how about the C source code, as well?

tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007
Yep....I'll include it in the next release. There are quite a few fixes that need to be made now, but I've lost my work so will need to try and remember the changes I had made! I haven't tested whether it is faster in this situation to treat the Scan0 pointer as a character array or integer array, so just left it as an integer array

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 o1 = Stride1/4, o2 = Stride2/4;
	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);
		}
	}
	return 0;
}


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009

Here is Gdip_AlphaMask

. . .

A Source bitmap and a mask bitmap can be supplied, and the returned bitmap from the function will be a new bitmap from the source, using the mask's transparencies. Simple example above only uses 1 circle as a mask


It works under AHK_L but not AHk_basic

Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010
Seems to use the AHK_L specific Ptr types for DllCall. As far as I can see, changing that to UInt should do the trick.

Xei
  • Members
  • 115 posts
  • Last active: Apr 11 2014 11:13 PM
  • Joined: 20 Jan 2012
Would anyone know how to resolve this issue I am encountering? When I try to run any of the tutorials, nothing happens. I am currently using Autohotkey_L 1.1.08.1 64-bit on Windows 7.

I recorded the process, so please watch the following video to see if I am doing anything wrong.
<!-- m -->http://www.twitch.tv...ing/b/330652994<!-- m -->

tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007
Xei the Gdip library isn't compatible with the 64-bit version of AHK. There is a modification here though.

<!-- l --><a class="postlink-local" href="http://www.autohotkey.com/community/viewtopic.php?p=559386#p559386">viewtopic.php?p=559386#p559386</a><!-- l -->

Xei
  • Members
  • 115 posts
  • Last active: Apr 11 2014 11:13 PM
  • Joined: 20 Jan 2012
Many thanks.

LiquidGravityOut
  • Guests
  • Last active:
  • Joined: --
Hello can someone explain to me how to get multiple line text to fill an area 100%? Also how to so 100% height or width while maintaining the fonts aspect ratio? I've played around with Gdip_TextToGraphics but I don't see anything that would effect this except the s option with and without percents. However the s option is for a single letter vertically as far as I can tell.

tic
  • Members
  • 1934 posts
  • Last active: Dec 21 2015 01:05 PM
  • Joined: 22 Apr 2007
I think the best you're going to get with multiline text is using the Measure option in Gdip_TextToGraphics() to work out the size you would need

LiquidGravity
  • Members
  • 156 posts
  • Last active: Oct 11 2014 04:11 PM
  • Joined: 26 Jan 2009
OK, Here is a function I made for this by cannibalizing some of the Gdip_TextToGraphics function. Maybe someone will find it useful.
Not the most efficient but not very slow either. On a related note it seems the results of measurement does not account for the NoWrap option.
; Gdip_TextToGraphics_Lazy will automatically loop through the text sizes and stop when it finds one that fits just within the boundary it is given while maintaining the fonts aspect ratio.
Gdip_TextToGraphics_Lazy(pGraphics, Text, Options, Font, Width, Height)
{
   IWidth := Width, IHeight:= Height
   CleanOptions := RegExReplace(Options, "i)S(\d+)(p*)")
   RegExMatch(Options, "i)X([\-\d\.]+)(p*)", xpos)
   RegExMatch(Options, "i)Y([\-\d\.]+)(p*)", ypos)
   RegExMatch(Options, "i)W([\-\d\.]+)(p*)", Width)
   RegExMatch(Options, "i)H([\-\d\.]+)(p*)", Height)
   xpos := (xpos1 != "") ? xpos2 ? IWidth*(xpos1/100) : xpos1 : 0
   ypos := (ypos1 != "") ? ypos2 ? IHeight*(ypos1/100) : ypos1 : 0
   Width := Width1 ? Width2 ? IWidth*(Width1/100) : Width1 : IWidth
   Height := Height1 ? Height2 ? IHeight*(Height1/100) : Height1 : IHeight
   Loop
   {
      NewOptions = %CleanOptions% s%A_Index%
      RC := Gdip_TextToGraphics(pGraphics, Text, NewOptions, Font, IWidth, IHeight, 1)
      StringSplit, RC, RC, |
      if (RC3 >= Width) or (RC4 >= Height)
      {
         LastSize := A_Index - 1
         NewOptions = s%LastSize% %CleanOptions%
         break
      }
   }
   Gdip_TextToGraphics(pGraphics, Text, NewOptions, Font, IWidth, IHeight)
   ;Returns the options that worked so you can store them and reuse them later to save some cpu
   Return, NewOptions
}

Edit: I'm still unsure how to stretch the text to fit 100% height and width.

Wicked
  • Members
  • 504 posts
  • Last active: Jun 14 2014 06:04 PM
  • Joined: 07 Jun 2008
Any ideas why I've never been able to get BRA to work on AHK_L 32b Unicode? Any possible ideas on how to fix it?

3nL8f.png


Xei
  • Members
  • 115 posts
  • Last active: Apr 11 2014 11:13 PM
  • Joined: 20 Jan 2012
How do I use 3 digit RGB values? I am confused because the maximum value for RGB is 255. Why does it seem that only 2 digits per color are supported by the script below?

My hex color, "e6e6e6," is R:230 G:230 B:230. The problem here is that the below code seems to only support 2 digit RGB values.
pBrush := Gdip_BrushCreateSolid(0xFF[color=#FF0000]112233[/color])


Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010

pBrush := Gdip_BrushCreateSolid(0xFF[color=#FF0000]112233[/color])


pBrush := Gdip_BrushCreateSolid(0xFF[color=#FF0000]E6E6E6[/color])


Xei
  • Members
  • 115 posts
  • Last active: Apr 11 2014 11:13 PM
  • Joined: 20 Jan 2012
Lol, ty. I thought I tried that. I must not have.