Author Topic: Looking for Way to Fade In (or Out) an Image  (Read 5053 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Looking for Way to Fade In (or Out) an Image
« on: October 04, 2021, 10:16:51 am »
This question came up last night with Dav's Pipe Connecting Puzzle:

Is there a way to fade an image in or out without having to resort to a pixel at a time with Point?

That would be so nice to use for ghostly faces and other Halloween tricks.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Looking for Way to Fade In (or Out) an Image
« Reply #1 on: October 04, 2021, 10:24:59 am »
Cover it with an alpha colored box.

Image is from (100,100)-(200,200)

FOR I = 1 TO 10
   LINE(100,100)-(200,200), _RGBA(0, 0, 0, 100), BF
NEXT

Will fade to black over multiple passes.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Looking for Way to Fade In (or Out) an Image
« Reply #2 on: October 04, 2021, 10:30:27 am »
Or pre-alpha your image to various levels and save an array of them.  Then you can pick and choose which one you need for the current stage of the effect.

FOR I = 1 TO 10
    CLS, RED
    PUTIMAGE (x, y), faded_image(i)
    SLEEP
NEXT

^fresh red background with you putting 10 different alpha backgrounds over it one at a time.

Which might also be accomplished via SETALPHA.

https://www.qb64.org/wiki/SETALPHA
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #3 on: October 04, 2021, 12:37:47 pm »
There is a routine in the wiki to do just that.... give me a second...

cant quite remember where it is in the wiki but here is the copy I've been using(how things fade in Dragon Warrior)

Code: QB64: [Select]
  1. SUB Fade_Out (L&)
  2.  FOR n! = 1 TO 0.00 STEP -0.05
  3.   i2& = _COPYIMAGE(L&)
  4.   DarkenImage i2&, n!
  5.   _PUTIMAGE (0, 0), i2&, Layer(0)
  6.   _FREEIMAGE i2&
  7.   _DELAY .06
  8.  
  9. SUB Fade_In (L&)
  10.  FOR n! = 0.01 TO 1 STEP 0.05
  11.   i2& = _COPYIMAGE(L&)
  12.   DarkenImage i2&, n!
  13.   _PUTIMAGE (0, 0), i2&, Layer(0)
  14.   _FREEIMAGE i2&
  15.   _DELAY .06
  16.  
  17. SUB DarkenImage (Image AS LONG, Value_From_0_To_1 AS SINGLE)
  18.  IF Value_From_0_To_1 <= 0 OR Value_From_0_To_1 >= 1 OR _PIXELSIZE(Image) <> 4 THEN EXIT SUB
  19.  DIM Buffer AS _MEM: Buffer = _MEMIMAGE(Image) 'Get a memory reference to our image
  20.  DIM Frac_Value AS LONG: Frac_Value = Value_From_0_To_1 * 65536 'Used to avoid slow floating point calculations
  21.  DIM O AS _OFFSET, O_Last AS _OFFSET
  22.  O = Buffer.OFFSET 'We start at this offset
  23.  O_Last = Buffer.OFFSET + _WIDTH(Image) * _HEIGHT(Image) * 4 'We stop when we get to this offset
  24.  'use on error free code ONLY!
  25.  DO
  26.   _MEMPUT Buffer, O, _MEMGET(Buffer, O, _UNSIGNED _BYTE) * Frac_Value \ 65536 AS _UNSIGNED _BYTE
  27.   _MEMPUT Buffer, O + 1, _MEMGET(Buffer, O + 1, _UNSIGNED _BYTE) * Frac_Value \ 65536 AS _UNSIGNED _BYTE
  28.   _MEMPUT Buffer, O + 2, _MEMGET(Buffer, O + 2, _UNSIGNED _BYTE) * Frac_Value \ 65536 AS _UNSIGNED _BYTE
  29.   O = O + 4
  30.  LOOP UNTIL O = O_Last
  31.  'turn checking back on when done!
  32.  _MEMFREE Buffer
  33.  
Granted after becoming radioactive I only have a half-life!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #4 on: October 04, 2021, 12:43:06 pm »
Thanks again Steve, _SetAlpha is probably the tool I had in mind.

I think I meant to say I wanted a transparent image laid over an background and gradually change from transparent to opaque.

Actually I started to consider random filling another image over a background image with Point at a low to middle transparency.

Oh Cobalt has posted, I will read next.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #5 on: October 04, 2021, 12:45:52 pm »
@Colbalt very interesting, thank you.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #6 on: October 04, 2021, 04:09:47 pm »
Your Welcome.
I've found it to be useful.
Layer(0) is basically _DISPLAY
Adjusting the _DELAY can make it fade slower or faster was well as the STEP in the FOR.
And of course you don't have to fade all the way in or out, you can have it stop where ever, but I've only ever used it for full in and outs.
Granted after becoming radioactive I only have a half-life!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #7 on: October 04, 2021, 04:37:47 pm »
Didn't we go over this in my Ghost Town game thread?

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3. img& = _LOADIMAGE("C:\QB64\YS.png")
  4.  
  5.     FOR i = 0 TO 255
  6.         CLS
  7.         addAlpha2Img img&, -1 'subtract 1 alpha value from each pixel.
  8.         _PUTIMAGE (0, 0)-(600, 600), img&
  9.         _LIMIT 40
  10.         _DISPLAY
  11.     NEXT
  12.  
  13.     FOR i = 255 TO 0 STEP -1
  14.         addAlpha2Img img&, 1 'subtract 1 alpha value from each pixel.
  15.         _PUTIMAGE (0, 0)-(600, 600), img&
  16.         _LIMIT 40
  17.         _DISPLAY
  18.         CLS
  19.     NEXT
  20.  
  21. SUB addAlpha2Img (img&, alpha AS INTEGER)
  22.     IF img& >= -1 THEN EXIT SUB
  23.     DIM buffer AS _MEM, o AS _OFFSET, o2 AS _OFFSET
  24.     buffer = _MEMIMAGE(img&)
  25.     o = buffer.OFFSET
  26.     o2 = o + _WIDTH(img&) * _HEIGHT(img&) * 4
  27.     DO
  28.         b = _MEMGET(buffer, o + 3, _UNSIGNED _BYTE)
  29.         IF (b + alpha) > 0 AND (b + alpha) < 255 THEN
  30.             _MEMPUT buffer, o + 3, (b + alpha) AS _UNSIGNED _BYTE
  31.         END IF
  32.         o = o + 4
  33.     LOOP UNTIL o = o2
  34.     _MEMFREE buffer
  35.  

To run code, download my Yosemite Sam avatar to the QB64 folder and rename it as YS.png.

Credit to Ashish, who I believe created this effect a few years back. My apologies to whomever did create it, if I got that wrong.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #8 on: October 04, 2021, 05:17:45 pm »
Yeah that was Ashish but I had something more ghostly:
https://www.qb64.org/forum/index.php?topic=4118.msg134736#msg134736

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #9 on: October 04, 2021, 05:19:22 pm »
@Pete That wasn't a Fade In / Out that was only blocking out the background image.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #10 on: October 04, 2021, 05:23:56 pm »
Sam with a pair of phasers, maybe. Sam with a pair of light sabres, never!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Looking for Way to Fade In (or Out) an Image
« Reply #11 on: October 04, 2021, 05:26:56 pm »
Sam with a pair of phasers, maybe. Sam with a pair of light sabres, never!

Pete

Like say, over your dead body! :)