How to perfectly fit any image on a fixed size picture control

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to perfectly fit any image on a fixed size picture control

Post by jeeswg » 01 Aug 2017, 09:40

@SKAN: Where did you get the maths for the ScaleRect function? Btw is Floor necessary, or can Round be used instead? Because presumably Round would give a marginally closer aspect ratio in some cases.

I've found this quite a tricky issue. Here's my current function (which uses Round):

Code: Select all

;shrink image to fit rectangle (maintain aspect ratio)
JEE_MathsImageFitRectShrink(vImgW, vImgH, vLimW, vLimH, ByRef vImgW3, ByRef vImgH3)
{
	vImgW3 := vImgH3 := ""
	if (vImgW <= vLimW) && (vImgH <= vLimH)
	{
		vImgW3 := vImgW, vImgH3 := vImgH
		return
	}
	vRatioW := vLimW / vImgW ;to fit the image width-wise (although might be too high)
	vRatioH := vLimH / vImgH ;to fit image height-wise (although might be too wide)
	vImgH1 := Round(vImgH * vRatioW) ;vImgW1 := vLimW
	if (vImgH1 <= vLimH)
	{
		vImgW3 := vLimW, vImgH3 := vImgH1
		if (vRatioW > vRatioH)
			return
	}
	vImgW2 := Round(vImgW * vRatioH) ;vImgH2 := vLimH
	if (vImgW2 <= vLimW)
		vImgW3 := vImgW2, vImgH3 := vLimH
	;diagnostic:
	if (vImgW3 = "")
		MsgBox, % "error: " A_ThisFunc "`r`n" "img: " vImgW " " vImgH "`r`n" "lim: " vLimW " " vLimH "`r`n" vImgW1 " " vImgH1 "`r`n" vImgW2 " " vImgH2
}
I also wrote a script to test it:

Code: Select all

q:: ;confirm that JEE_MathsImageFitRectShrink never returns blanks
vOutput := ""
vNum := 51
Loop, % vNum
{
	vLimH := A_Index-1
	Loop, % vNum
	{
		vLimW := A_Index-1
		Loop, % vNum
		{
			vImgH := A_Index-1
			Loop, % vNum
			{
				vImgW := A_Index-1
				;if the function fails, then:
				;a MsgBox within the function should trigger
				JEE_MathsImageFitRectShrink(vImgW, vImgH, vLimW, vLimH, vImgW3, vImgH3)
			}
		}
	}
}
Clipboard := vOutput
MsgBox, % "done"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

User avatar
SKAN
Posts: 1619
Joined: 29 Sep 2013, 16:58

Re: How to perfectly fit any image on a fixed size picture control

Post by SKAN » 01 Aug 2017, 16:00

jeeswg wrote:@SKAN: Where did you get the maths for the ScaleRect function?
I took some help.. 9 years ago from [VxE]
https://autohotkey.com/board/topic/2782 ... ixed-rect/
But there were rounding errors in those functions.
jeeswg wrote:Btw is Floor necessary, or can Round be used instead? Because presumably Round would give a marginally closer aspect ratio in some cases.
The image has to be exact fit at least on one side of the target rect.
So I return one value as is ( either TW or TH ) and only compute the other based on the aspect ratio.
In tricky situations, eg. Source = 256x257 and target = 257x256 I wouldn't want round upwards, hence I went with floor.
OMG! I didn't test the above.. I forgot.

Thanks for sharing your version. I will test it soon.
My Scripts and Functions: V1  V2

User avatar
SKAN
Posts: 1619
Joined: 29 Sep 2013, 16:58

Re: How to perfectly fit any image on a fixed size picture control

Post by SKAN » 01 Aug 2017, 16:09

Drugwash wrote:I don't think it breaks anything
I think semi transparent pixels are lost... I'm quoting from memory. :)
My Scripts and Functions: V1  V2

User avatar
Drugwash
Posts: 853
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: How to perfectly fit any image on a fixed size picture control

Post by Drugwash » 02 Aug 2017, 03:25

… and I was quoting from… hopes. :lol:
Frankly I have no idea. Do you have some specific alternative API set and image to compare the results?

[EDIT]
Unfortunately there is something going on and it's not for the best. I just tested the script with the AHK logo and apparently the blur effect doesn't catch on. it works with the 24bit PNG poster but not with the 32bit alpha-channel PNG logo. Tested both in 98SE and XP (just to be clear).
Last edited by Drugwash on 02 Aug 2017, 10:08, edited 1 time in total.
Part of my AHK work can be found here.

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to perfectly fit any image on a fixed size picture control

Post by jeeswg » 02 Aug 2017, 10:00

I think that in my example, it is OK to use Round (marginally more accurate maintenance of ratio), and not Floor:

Note: we try two possible pairs of values:
W * (LimW/W) = LimW and H * (LimW/W)
W * (LimH/H) and H * (LimH/H) = LimH

start: W, H [both above their limits][where LimW/W > LimH/H]
limits: LimW, LimH
[note: if 'try 1' values too big to fit rectangle, try 'try 2' values]
try 1: LimW, Round(H*(LimW/W)) [bigger than 'try 2' values]
try 2: Round(W*(LimH/H)), LimH [smaller than 'try 1' values]
is Round(W*(LimH/H)) definitely <= LimW? (in which case at least one 'try' would always work)

since W*(LimH/H) <= LimW
so, Round(W*(LimH/H)) <= Round(LimW)
since LimW is an integer, so Round(LimW) = LimW
and so, Round(W*(LimH/H)) <= Round(LimW) = LimW
so, Round(W*(LimH/H)) <= LimW as required

If someone can prove me wrong, or find relevant info for this problem, or generally just find a better more succinct function than mine, that would be most welcome.

@SKAN: Funnily enough when you posted this script, I was thinking about this very problem.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How to perfectly fit any image on a fixed size picture control

Post by tmplinshi » 07 Dec 2017, 04:36

Works great!! Thank you.

Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Re: How to perfectly fit any image on a fixed size picture control

Post by Jose Hidalgo » 09 Jun 2021, 12:37

Hi all, thanks for this nice topic. I hope somebody's still listening. :)
I'm trying to use the method described by @SKAN in this post, but so far only the GDI+ image displays, not the Lanczos one.
I have put Resample.dll, Resample.h and Resample.lib in the same folder as my script. Am I missing something ?

Image

Also, my final purpose would be to include the Resample files in a compiled executable. I could include the files with FileInstall, but is there a better way ? And could they be in a different folder ? (a Resources folder for instance).

User avatar
SKAN
Posts: 1619
Joined: 29 Sep 2013, 16:58

Re: How to perfectly fit any image on a fixed size picture control

Post by SKAN » 09 Jun 2021, 12:42

Jose Hidalgo wrote:
09 Jun 2021, 12:37
I'm trying to use the method described by @SKAN in this post
You could try my Imagen() to scale image.

Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Re: How to perfectly fit any image on a fixed size picture control

Post by Jose Hidalgo » 09 Jun 2021, 13:19

OMG Imagen() works beautifully ! So much better than the standard scaling !! :superhappy: Thank you SKAN, I'll be including it right away ! :bravo: :bravo: :bravo:

Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

Re: How to perfectly fit any image on a fixed size picture control

Post by Sam_ » 22 Mar 2023, 14:49

SKAN wrote:
29 Jul 2017, 17:33
gwarble wrote:its a limitation of GDI/GDI+ from what I understand, you have to roll your own
https://www.codeproject.com/Articles/11143/Image-Resizing-outperform-GDI
Thanks for the link.
There is a 32bit DLL based on that code
Plain C Resampling DLL : https://www.codeproject.com/Articles/22271/Plain-C-Resampling-DLL

Wish AHK have this incorporated.. Not sure if CPOL License is compatible with GPL v2

:)
I don't suppose you or any other kind soul would be willing to recompile this into a 64-bit dll? Also, a couple of bug fixes have been posted in the discussion that don't seem to have been implemented yet.

User avatar
gwarble
Posts: 530
Joined: 30 Sep 2013, 15:01

Re: How to perfectly fit any image on a fixed size picture control

Post by gwarble » 22 Mar 2023, 16:17

Have you tried Imagen() from Skan yet? Dll shouldn't be required unless I'm missing something

ahk.ahk-20230322141806.png
ahk.ahk-20230322141806.png (16.38 KiB) Viewed 1474 times

Code: Select all

Gui, Margin, 0, 0
;pic is 660x100
Gui, Add, Picture, x0 y0  w165 h25, ahk.png
Gui, Add, Picture, x0 y25 w165 h25 hwndhPic
Imagen("ahk.png","ahk_id" . hPic)
Gui, Show
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .

Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

Re: How to perfectly fit any image on a fixed size picture control

Post by Sam_ » 22 Mar 2023, 16:56

@gwarble

As part of a larger project, I want to allow the end-user to be able to select a specific InterpolationMode/resizing algorithm/resampling filter to be used to resize a set of image frames in an animated graphic. I've already implemented the ones GDI+ supports, but thought this dll had some interesting and potentially improved options over what GDI+ offers. At a glance it looks like Imagen() only uses the GDI+ InterpolationMode=HighQualityBicubic and SmoothingMode=HighQuality. I'm looking for some additional options. If I can get these implemented via x86 and x64 DLLs, my next task is to look into implementing pixel art scalers, either via DLL if I can find them or manually in AHK if I can't.

User avatar
gwarble
Posts: 530
Joined: 30 Sep 2013, 15:01

Re: How to perfectly fit any image on a fixed size picture control

Post by gwarble » 22 Mar 2023, 23:31

oh ok my bad, you quoted my response to better downscaling specifically, which Imagen() solves nicely for me, but if you're after all the specific modes available then that dll or dissecting the GDI+ code and adding options for other interpolation and smoothing modes might be your best bet, good luck

note to self: https://photosauce.net/blog/post/image-scaling-with-gdi-part-4-examining-the-interpolationmode-values
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .

Sam_
Posts: 146
Joined: 20 Mar 2014, 20:24

Re: How to perfectly fit any image on a fixed size picture control

Post by Sam_ » 23 Mar 2023, 18:04

That was a very interesting and informative series of blog posts. I learned a lot in the ones I have read so far. Thanks for sharing!

Post Reply

Return to “Scripts and Functions (v1)”