Replacing Color Gdip Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Replacing Color Gdip

23 Jun 2018, 18:20

I am trying to replace color in an image to prepare it for OCR. I've been able to invert the color with:

Code: Select all

G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
Gdip_DrawImage(G2, pBitmap, 0, 0, w, h,"","","","", "-1|0|0|0|0|0|-1|0|0|0|0|0|-1|0|0|0|0|0|1|0|1|1|1|0|1")

However I am still having trouble with replacing color. The ImageMagick commands convert.exe File.png -alpha off -fuzz 40% +fill white +opaque "#000000 OutPutFile.png and convert.exe File.png -alpha off -fuzz 30% -fill white -opaque "#C0C0C0" OutPutFile.png seem to do the job but I am trying to achieve the same thing via Gdip. The code below was posted by tic at https://autohotkey.com/board/topic/2944 ... ntry554258 but I can't get it to work. A sample image for testing can be found at https://ibb.co/jV5yZo. Any help is appreciated.

Code: Select all

#include %A_ScriptDir%
#Include Gdip_All.ahk

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile("input.png")
E := Gdip_FilterColor(pBitmap, 0xFFC0C0C0, 0xFFFFFFFF, 100)
Gdip_SaveBitmapToFile(E, "output.png")
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
return

Gdip_FilterColor(pBitmap, Color, ReplaceColor, Variation=0)
{
	static _FilterColor
	if !_FilterColor
	{
		MCode_FilterColor := "83EC288B44243C8BC8C1E918894C243C8BC8C1E91081E1FF000000894C240C0FB6CC25FF000000894C2410894424148B442440"
		. "8BC8C1E918894C24188BC80FB6D425FF000000C1E910894424248B44243481E1FF00000085C0894C241C895424200F8E020100008B54242C5355568B7"
		. "4245033DB578B7C2440895C241489442418B90100000085FF0F8EC10000008D4A028D040B8BDA2BD983C303895C24508BDA2BD983C301895C24442BD1"
		. "897C24108B5C24448B4C24500FB62C030FB60C018B5C244C0FB63803DE3BCB7F688B5C244C2BDE3BCB7C5E8B4C241C8D1C313BFB7F532BCE3BF97C4D8"
		. "B4C24208D3C313BEF7F422BCE3BE97C3C8B7C24240FB60C028D1C373BCB7F2D2BFE3BCF7C270FB64C24288B7C2450880C070FB64C242C8B7C24448808"
		. "0FB64C2430880C070FB64C2434880C02B90100000083C004294C24100F8569FFFFFF8B7C24408B54243C8B5C2414035C2448294C2418895C24140F852"
		. "1FFFFFF5F5E5D5B33C083C428C3"
		
		VarSetCapacity(_FilterColor, StrLen(MCode_FilterColor)//2)
		Loop % StrLen(MCode_FilterColor)//2	  ;%
			NumPut("0x" SubStr(MCode_FilterColor, (2*A_Index)-1, 2), _FilterColor, A_Index-1, "char")
	}
	
	Variation := (Variation > 255) ? 255 : (Variation < 0) ? 0 : Variation
	Gdip_GetImageDimensions(pBitmap, w, h)
	E1 := Gdip_LockBits(pBitmap, 0, 0, w, h, Stride1, Scan01, BitmapData1)
	E := DllCall(&_FilterColor, "uint", Scan01, "int", w, "int", h, "int", Stride1, "uint", Color, "uint", ReplaceColor, "int", Variation)
	Gdip_UnlockBits(pBitmap, BitmapData1)
	return (E = "") ? -1 : E
}
User avatar
Xtra
Posts: 1240
Joined: 02 Oct 2015, 12:15

Re: Replacing Color Gdip

23 Jun 2018, 18:46

Change:
Gdip_SaveBitmapToFile(E, "output.png")

To:
Gdip_SaveBitmapToFile(pBitmap, "output.png")

HTH
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Re: Replacing Color Gdip

24 Jun 2018, 02:35

Change:
Gdip_SaveBitmapToFile(E, "output.png")

To:
Gdip_SaveBitmapToFile(pBitmap, "output.png")

HTH
Even when I change E to pBitmap the output.png does not seem to have any noticeable changes. You can view the images at the links below:
input.png
https://ibb.co/jV5yZo

output.png
https://ibb.co/hwpb4o
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Re: Replacing Color Gdip

24 Jun 2018, 06:18

Below is an example of the code that I have gotten working so far for inversion and cropping to match the ImageMagick command convert.exe Input.png -crop 1900x77+7+694 -negate InvertedOutput.png and convert.exe InvertedOutput.png -alpha off -fuzz 30% -fill white -opaque "#C0C0C0" ColorReplaced.png but I'm still trying to find or create a function to do the same for convert.exe InvertedOutput.png -alpha off -fuzz 40% +fill white +opaque "#000000" ColorReplaced.png and convert.exe ColorReplaced.png -alpha off -fuzz 30% -fill white -opaque "#C0C0C0" ColorReplaced2.png using only Gdip as Gdip_FilterColor() does not appear to do anyting even when I change E to pBitmap as shown in the second code block below.


First code block (Working):

Code: Select all

#include %A_ScriptDir%
#Include Gdip_All.ahk

t::TestInvertCrop()

TestInvertCrop(){
InputFile := "Input.png"
OutputFile := "InvertedOutput.png"

CropX = 7
CropY = 694
CropW = 1900
CropH = 77

Gdip_InvertColorCropImage(InputFile, OutputFile, CropX, CropY, CropW, CropH, 1)

Return
}



Gdip_InvertColorCropImage(InputFile, OutputFile, x := 1, y := 1, w := 1, h := 1, crop := 0){
pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile(InputFile)
Gdip_GetDimensions(pBitmap, Width, Height)
pBitmap2 := Gdip_CreateBitmap(Width, Height)
pBitmap3 := Gdip_CreateBitmap(Width, Height)
G2 := Gdip_GraphicsFromImage(pBitmap2), Gdip_SetSmoothingMode(G2, 4), Gdip_SetInterpolationMode(G2, 7)
G3 := Gdip_GraphicsFromImage(pBitmap3), Gdip_SetSmoothingMode(G3, 4), Gdip_SetInterpolationMode(G3, 7)
Gdip_DrawImage(G2, pBitmap, 0, 0, Width, Height,"","","","", "-1|0|0|0|0|0|-1|0|0|0|0|0|-1|0|0|0|0|0|1|0|1|1|1|0|1")
Gdip_DrawImage(G3, pBitmap2, 0, 0, w, h, x, y, w, h)

If (crop == 1){
Gdip_SaveBitmapToFile(pBitmap3, OutputFile)
} Else {
Gdip_SaveBitmapToFile(pBitmap2, OutputFile)
}

Gdip_DeleteGraphics(G2), Gdip_DeleteGraphics(G3)
Gdip_DisposeImage(pBitmap), Gdip_DisposeImage(pBitmap2), Gdip_DisposeImage(pBitmap3)
Gdip_Shutdown(pToken)
Return
}

Second code block(Not Working):

Code: Select all

#include %A_ScriptDir%
#Include Gdip_All.ahk

InputFile := "ColorReplaced.png"
OutputFile := "ColorReplaced2.png"

pToken := Gdip_Startup()
pBitmap := Gdip_CreateBitmapFromFile(InputFile)
E := Gdip_FilterColor(pBitmap, 0xFFC0C0C0, 0xFFFFFFFF, 100)
Gdip_SaveBitmapToFile(pBitmap, OutputFile)
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
return

Gdip_FilterColor(pBitmap, Color, ReplaceColor, Variation=0)
{
	static _FilterColor
	if !_FilterColor
	{
		MCode_FilterColor := "83EC288B44243C8BC8C1E918894C243C8BC8C1E91081E1FF000000894C240C0FB6CC25FF000000894C2410894424148B442440"
		. "8BC8C1E918894C24188BC80FB6D425FF000000C1E910894424248B44243481E1FF00000085C0894C241C895424200F8E020100008B54242C5355568B7"
		. "4245033DB578B7C2440895C241489442418B90100000085FF0F8EC10000008D4A028D040B8BDA2BD983C303895C24508BDA2BD983C301895C24442BD1"
		. "897C24108B5C24448B4C24500FB62C030FB60C018B5C244C0FB63803DE3BCB7F688B5C244C2BDE3BCB7C5E8B4C241C8D1C313BFB7F532BCE3BF97C4D8"
		. "B4C24208D3C313BEF7F422BCE3BE97C3C8B7C24240FB60C028D1C373BCB7F2D2BFE3BCF7C270FB64C24288B7C2450880C070FB64C242C8B7C24448808"
		. "0FB64C2430880C070FB64C2434880C02B90100000083C004294C24100F8569FFFFFF8B7C24408B54243C8B5C2414035C2448294C2418895C24140F852"
		. "1FFFFFF5F5E5D5B33C083C428C3"
		
		VarSetCapacity(_FilterColor, StrLen(MCode_FilterColor)//2)
		Loop % StrLen(MCode_FilterColor)//2	  ;%
			NumPut("0x" SubStr(MCode_FilterColor, (2*A_Index)-1, 2), _FilterColor, A_Index-1, "char")
	}
	
	Variation := (Variation > 255) ? 255 : (Variation < 0) ? 0 : Variation
	Gdip_GetImageDimensions(pBitmap, w, h)
	E1 := Gdip_LockBits(pBitmap, 0, 0, w, h, Stride1, Scan01, BitmapData1)
	E := DllCall(&_FilterColor, "uint", Scan01, "int", w, "int", h, "int", Stride1, "uint", Color, "uint", ReplaceColor, "int", Variation)
	Gdip_UnlockBits(pBitmap, BitmapData1)
	return (E = "") ? -1 : E
}
trust_me
Posts: 60
Joined: 29 Jul 2017, 10:46

Re: Replacing Color Gdip

24 Jun 2018, 07:45

Works fine for me ,maybe you could delete the "ColorReplaced2.png" file before running the code again.( in case it cannot overwrite it for some reason )
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Re: Replacing Color Gdip

24 Jun 2018, 08:10

Works fine for me ,maybe you could delete the "ColorReplaced2.png" file before running the code again.( in case it cannot overwrite it for some reason )
It outputs a file but that file does not have all the light gray silver replaced with white.

Using Gdip_FilterColor()
https://ibb.co/hwpb4o

Using ImageMagick
https://ibb.co/mQk9aT
trust_me
Posts: 60
Joined: 29 Jul 2017, 10:46

Re: Replacing Color Gdip

24 Jun 2018, 09:44

It does....


Image
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Re: Replacing Color Gdip

24 Jun 2018, 11:10

It does....

Strange I tried it with both AutoHotkey_1.1.29.00 and v2 but the output file did not have the color change. What AutoHotkey.exe version are you using?
trust_me
Posts: 60
Joined: 29 Jul 2017, 10:46

Re: Replacing Color Gdip

24 Jun 2018, 11:22

Image


This is the gdip_all.ahk i am using:
https://www.dropbox.com/s/k9djlvnpmk5o7 ... l.ahk?dl=1
trust_me
Posts: 60
Joined: 29 Jul 2017, 10:46

Re: Replacing Color Gdip  Topic is solved

24 Jun 2018, 11:28

I updated to 1.1.29.01 and it works in replacing the color.But i am using 32bit version of ahk maybe there is a problem if you have 64bit ahk version?
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Re: Replacing Color Gdip

24 Jun 2018, 11:36

I updated to 1.1.29.01 and it works in replacing the color.But i am using 32bit version of ahk maybe there is a problem if you have 64bit ahk version?
Thanks, yes that was the problem. For some reason Gdip_FilterColor() is not working with the AutohotkeyU64 which is the main version I use.
trust_me
Posts: 60
Joined: 29 Jul 2017, 10:46

Re: Replacing Color Gdip

24 Jun 2018, 11:41

Gdip_FilterColor: the MC code in the function is only compiled for 32bit............
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Re: Replacing Color Gdip

24 Jun 2018, 11:52

Gdip_FilterColor: the MC code in the function is only compiled for 32bit............
I am unfamiliar with machine code. Could you point me to some reading material or program that might help me to convert it to 64bit. Tried doing a google search but did not find much about using mc code in Autohotkey.
trust_me
Posts: 60
Joined: 29 Jul 2017, 10:46

Re: Replacing Color Gdip

24 Jun 2018, 12:08

https://autohotkey.com/boards/viewtopic.php?t=32

You can ask on the forum there are some members who can do it if you provide the source code.( i am not familiar with it :) )

I guess the source code is here: https://pastebin.com/vFgELSEi

Tic has a gdip "forum" you can also put your request there: https://autohotkey.com/boards/viewtopic.php?t=6517
Archandrion
Posts: 31
Joined: 26 May 2018, 22:23

Re: Replacing Color Gdip

24 Jun 2018, 12:10

https://autohotkey.com/boards/viewtopic.php?t=32

You can ask on the forum there are some members who can do it if you provide the source code.( i am not familiar with it :) )

I guess the source code is here: https://pastebin.com/vFgELSEi

Tic has a gdip "forum" you can also put your request there: https://autohotkey.com/boards/viewtopic.php?t=6517

Thanks, actually I was looking up "mc code" but just machine code gave me more results and I'm reading up on it.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], Mipha and 55 guests