Imagen() : A fade-in/fade-out animator for picture control

Post your working scripts, libraries and tools for AHK v1.1 and older
tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

Re: Imagen() : A fade-in/fade-out animator for picture control

09 Feb 2021, 10:18

@SKAN
Thank you skan!
Because of the library you shared, it inspired me to make the 1.0 version of the photo filtering tool, and then I learned GDI+ and solved the problem!
Thank you very much for sharing. I believe they are the cornerstone of success for many people, including me. :bravo:
Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Re: Imagen() : A fade-in/fade-out animator for picture control

09 Jun 2021, 14:01

Imagen works great here. :D I have a question for @SKAN though.
Does it manage transparency with multiple images ?
I'm currently trying to display a png with transparent background over a jpg.
I can display the jpg, I can display the png, but when I display the png over the jpg I can't see the jpg anymore. :?
AHK default method with BackgroundTrans works well, I can display one image over the other, but scaling quality is very poor of course.

Here's what I'm currently doing :

Code: Select all

    Gui, %ImgGuiName%: Add, Picture, x%BorderWidth% y%BorderWidth% h%WantedHeight% w%WantedWidth% HwndhPic1
    Imagen(ImgBack, "ahk_id" . hPic1)
    Gui, %ImgGuiName%: Add, Picture, BackgroundTrans x%BorderWidth% y%BorderWidth% h%WantedHeight% w%WantedWidth% HwndhPic2
    Imagen(ImgLoc, "ahk_id" . hPic2)
ImgBack is the location of a jpg background.
ImgLoc is the location of a png with transparent background.
I have tried with and without BackgroundTrans.
Or should I place only one Imagen call just before the Gui, Show ? But then what Hwnd should I pass ? I'm not sure I understand everything. :oops:
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Imagen() : A fade-in/fade-out animator for picture control

09 Jun 2021, 14:19

Jose Hidalgo wrote:
09 Jun 2021, 14:01
I'm currently trying to display a png with transparent background over a jpg.
 
You downloaded the zip, right? Save and try the following example from root folder.

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force
SetWorkingDir, %A_ScriptDir%
SetBatchLines, -1

ImgBack := "Images\Img_0482.jpg"
ImgLoc  := "Images\Imagen.png"

Gui, Add, Picture, w600 h400 hwndhPic1
Gui, Add, Picture, xp yp wp hp BackgroundTrans hwndhPic2
Imagen(ImgBack, "ahk_id" . hPic1)
Imagen(ImgLoc, "BackgroundTrans ahk_id" . hPic2)
Gui, Show,, Overlapped picture controls
 
image.png
image.png (433.97 KiB) Viewed 2916 times
Jose Hidalgo
Posts: 222
Joined: 07 Mar 2021, 07:44

Re: Imagen() : A fade-in/fade-out animator for picture control

09 Jun 2021, 18:33

Works perfectly now, it was merely a syntax issue. Thank you, and congrats again for this beaufiful function. :bravo:
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Imagen() : A fade-in/fade-out animator for picture control

10 Jun 2021, 12:28

in this example, can you fade a new background (ImgBack) image while the front image is in place?

Or more simply, if you have a text control overlaying the image, can you fade a new image without the text disappearing with Imagen()?
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Imagen() : A fade-in/fade-out animator for picture control

10 Jun 2021, 12:53

@gwarble : Seems not possible.
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Imagen() : A fade-in/fade-out animator for picture control

10 Jun 2021, 17:18

seems not, thanks anyway for a great function!
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Imagen() v3.22

13 Jun 2021, 09:56

The in-built colormatrix presets are now: Grayscale Invert Sepia and Normal.
Any of these presets maybe followed by :Alpha value (0-255) to make the image semitransparent.
Example: ?Grayscale:127.5 will convert the image to Grayscale and make it 50% transparent.
 
I've added the following example script ( OverlappedPictureControls.ahk ) to the demo:
 

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force

ImgBack := "Images\Img_0482.jpg"
ImgLoc  := "Images\Imagen.png"
Gui, Add, Picture, w600 h400 hwndhPic1
Gui, Add, Picture, xp yp wp hp BackgroundTrans hwndhPic2
Imagen(ImgBack, "ahk_id" . hPic1)
Imagen(ImgLoc, "?Normal:180 BackgroundTrans ahk_id" . hPic2)
Gui, Show,, Overlapped picture controls
 
image.png
image.png (449.34 KiB) Viewed 2791 times
pv007
Posts: 93
Joined: 20 Jul 2020, 23:50

Re: Imagen() : A fade-in/fade-out animator for picture control

16 Jun 2021, 12:21

@SKAN can i request a feature in your function?

Support to specify what region of the picture should be set transparent

This picture 800x300
sun.png
sun.png (62.88 KiB) Viewed 2734 times
So if I set Imagen(400,0,400,300) it would return something like this result:
image.png
image.png (102.13 KiB) Viewed 2729 times
Im loading the hBitmap from Imagen() into a DC, so I cant split the image > set transparency in just the desired area > combine them again :|
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Imagen() : A fade-in/fade-out animator for picture control

16 Jun 2021, 18:25

Hi @pv007

Originally Imagen() was a Slideshow class.
I'm a photographer and I love to fade-in my images from Grayscale to Color.
Its such a pleasure to watch a slideshow in this manner and hence I wrote it for my own use.
The class version was too huge and I thought a trimmed down version would be useful for the community.

These are the intended uses for Imagen()

There are atleast two instances where you have a fixed rectangle and the need to scale down/up your image to fit it.
1) Picture control
2) ImageList

Other than the above, one could use Imagen() to show multiple states of a (picture/button) control.
For example, with a single color image, we could show
Grayscale as Normal, Color on mouse hover, and Inverted when clicked.
This is where I thought Opacity would be a great add.. that is to show a "state".. and hence I added the feature.

I don't intend to bloat the function back to its original size.
pv007 wrote:
16 Jun 2021, 12:21
@SKAN can i request a feature in your function?
This would be best done as a separate function depending on a gdiplus library.
Unfortunately our forum doesn't have anything that I would rely on or contribute to.
pv007
Posts: 93
Joined: 20 Jul 2020, 23:50

Re: Imagen() : A fade-in/fade-out animator for picture control

17 Jun 2021, 08:18

Im playing with the examples in the zip, is not possible to fade and after set transparency?
How do I remove a picture added using Imagen ahk_id . control?
Looks like

Code: Select all

guicontrol,, hPic
Cant remove the picture.

Code: Select all

Gui, New, -Caption +Border
Gui, Color, 505050
Gui, Add, Picture, w400 h500 0x200 HwndhPic
Gui, Show
Sleep 500

Imagen("Images\Imagen.png", "BackgroundTrans Fadein1000ms ahk_id" . hPic)
Sleep 2000

;Imagen("", "BackgroundTrans Fadein1000ms ahk_id" . hPic)
;FileAppend, hPic: %hPic% `n, *
guicontrol,, hPic

Imagen("Images\Imagen.png", "?Normal:150 BackgroundTrans ahk_id" . hPic)
return
esc::ExitApp

iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Imagen() : A fade-in/fade-out animator for picture control

17 Jun 2021, 09:05

SKAN, your code is impossibly hard to read and I’m surprised you manage to keep track of it without documentation.

In your upscaling part, you don’t need a SmoothingMode parameter, that only affects vectors, such as GDI+ curves or lines.

Instead replace it with a half-pixel offset mode as 2. This will improve the quality of the upscaling.

You may get strange edges when upscaling so in the ImageAttributes set WrapModeTileFlipXY. That way when upscaling is preformed it will sample mirrored pixels instead of pure black.

Also you can manage your own pixels by creating a pixel buffer, through varsetcapacity(size) and supplying that pointer to createbitmapfromscan0. The benefit would be to avoid repeated calls to rtlmovememory or LockBiys/UnlockBits. Although there’s so much going on i can’t tell if this would help.

So it seems that when you calculate the size of pBMTemp you should call createdibsection first, get the pBits, then overlay a bitmap by supplying pBits to CreateBitmapFromScan0 to save a call to lock/unlockbits.

I didn’t know you could save a DC and restore it to avoid an old bitmap. Thanks!
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Imagen() : A fade-in/fade-out animator for picture control

17 Jun 2021, 09:30

pv007 wrote:
17 Jun 2021, 08:18
Looks like

Code: Select all

guicontrol,, hPic
Cant remove the picture.
It should be

Code: Select all

guicontrol,, %hPic%
But no. The above will remove image only if the image was set with Gui, Add, Picture or GuiControl... seemingly.
You need to call STM_SETIMAGE to remove he image.

Code: Select all

Imagen("Images\Imagen.png", "BackgroundTrans Fadein1000ms ahk_id" . hPic)
 
BackgroundTrans will be ignored when FadeIn is in effect.
If current image is smaller than the previous one, then previous image will be visible in transparent areas.
STM_SETIMAGE is done after blitting. The animation should/would reflect only what will be available in control's attached bitmap.
 

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force
SetWorkingDir, %A_ScriptDir%

Gui, New, -Caption +Border
Gui, Color, 505050
Gui, Add, Picture, w400 h500 HwndhPic
Gui, Show
Sleep 500

Imagen("Images\Imagen.png", "Fadein1000ms ahk_id" . hPic)
Sleep 2000

hBitmap := DllCall("SendMessage", "Ptr",hPic, "Int",STM_SETIMAGE:=0x172, "Ptr",0, "Ptr",0, "Ptr")
DllCall("DeleteObject", "Ptr",hBitmap)
Return

Esc::ExitApp
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Imagen() : A fade-in/fade-out animator for picture control

17 Jun 2021, 10:04

@iseahound
SKAN, your code is impossibly hard to read and I’m surprised you manage to keep track of it without documentation.
Yes. Mostly because of options parsing.
Thanks for the tips.

I will test and apply suggestions made by you.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Imagen() : A fade-in/fade-out animator for picture control

17 Jun 2021, 11:20

iseahound wrote:
17 Jun 2021, 09:05
Also you can manage your own pixels by creating a pixel buffer, through varsetcapacity(size) and supplying that pointer to createbitmapfromscan0.
I have already demonstrated that in my VarV_RePNG(),
but no GDI bitmap is involved there.
iseahound wrote:
17 Jun 2021, 09:05
The benefit would be to avoid repeated calls to rtlmovememory or LockBiys/UnlockBits.
I know how to share the same buffer between a GDI and GDI+ bitmap.
I am copying data because I want to kill the GDI bitmap before I process GDI+ bitmap.
If you know of a way to share the pixel buffer between GDI bitmap and GDI+ bitmap and yet kill the GDI bitmap without destroying pixel data, please share.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: Imagen() : A fade-in/fade-out animator for picture control

17 Jun 2021, 18:04

I think Windows manages the DIB itself for performance reasons, and so it seems unlikely, but I do not know for certain. Possible with CreateCompatibleBitmap and SetDIBits?

I actually learn a lot from reading your code, but this is a very large function, so I was wondering if you keep extra notes somewhere, because this is very big!
pv007
Posts: 93
Joined: 20 Jul 2020, 23:50

Re: Imagen() : A fade-in/fade-out animator for picture control

18 Jun 2021, 04:33

@SKAN is possible to fadeout? Just deleting instantly does not cause a 'nice' effect :?

Code: Select all

Imagen("imagen.png", "Fadein2000ms ahk_id" . BackGround)
Sleep 2000
Imagen("imagen.png", "FadeOut2000ms:100 ahk_id" . BackGround)
Then it would fade out and stop at the value of 100 in transparency
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Imagen() : A fade-in/fade-out animator for picture control

18 Jun 2021, 05:31

iseahound wrote:
17 Jun 2021, 18:04
Possible with CreateCompatibleBitmap and SetDIBits?
Makes a private copy of the buffer.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Imagen() : A fade-in/fade-out animator for picture control

18 Jun 2021, 05:57

pv007 wrote:
18 Jun 2021, 04:33

Code: Select all

Imagen("imagen.png", "FadeOut2000ms:100 ahk_id" . BackGround)
Try to wrap your mind around the following:
Say If a pixel is already 50% transparent, you can't put a 49% transparent pixel over that unless you erase the previous pixel.
Any attempt to erase will cause flicker and hence Imagen() will write a solid background color over the pixel and then apply 49% transparent pixel.
As you ask, if Imagen() tried to fade from 100% - 0%, then the picture control needs to be erased 100 times.

I've explained it here: https://www.autohotkey.com/boards/viewtopic.php?p=380960#p380960

If you really need to do this, take a screen-shot bitmap of whatever is behind your picture control
and use Imagen() to fade-in that bitmap.
and then delete the control's picture with STM_SETIMAGE.

That is how things work in windows unless you use a layered window.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 87 guests