Author Topic: question: light transmission with any image  (Read 6324 times)

0 Members and 1 Guest are viewing this topic.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
question: light transmission with any image
« on: May 10, 2021, 09:36:40 am »
Hello ! i just can't figure out how to do it. i want to be able to adjust the light transmission of any image. For example, 100, only the image should be visible. If you are 50, you will see half of the image and half of the background. If 0, the image disappears completely. What do I need to do with the "pic" picture for this to work?

Code: QB64: [Select]
  1. mon = _NEWIMAGE(800, 800, 32)
  2. SCREEN mon: CLS
  3.  
  4. 'what do i do with this image to adjust its light transmission?
  5. pic = _LOADIMAGE("d:\picture.jpg", 32)
  6.  
  7.  
  8. 'changing color background
  9. FOR t = 0 TO 2999
  10.     COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
  11.     LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  12.     PRINT "Ű"
  13.  
  14. _DEST mon
  15.  
  16.  
  17.  

Offline justsomeguy

  • Newbie
  • Posts: 47
    • View Profile
Re: question: light transmission with any image
« Reply #1 on: May 10, 2021, 11:04:19 am »
I'm not sure what the final result your looking for but I modified your code on my best guess.
Code: QB64: [Select]
  1. mon = _NEWIMAGE(800, 800, 32)
  2. SCREEN mon: CLS
  3.  
  4. 'what do i do with this image to adjust its light transmission?
  5. pic = _LOADIMAGE("d:\picture.jpg", 32)
  6.  
  7. newimg = _NEWIMAGE(_WIDTH(pic), _HEIGHT(pic), 32)
  8.  
  9. '   setTransparency ( Image that you loaded, New Image that you created , Transparency vale (0-255) )
  10. CALL setTransparency(pic, newimg, 200)
  11. '   Don't forget to free images after you are done.
  12.  
  13.  
  14. 'changing color background
  15. FOR t = 0 TO 2999
  16.     COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
  17.     LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  18.     PRINT "Ű"
  19.  
  20.  
  21.  
  22. _DEST mon
  23. _SOURCE newimg
  24.  
  25.  
  26. SUB setTransparency (img AS LONG, newimg AS LONG, tr AS INTEGER)
  27.     _SOURCE img ' Your loaded image handle
  28.     _DEST newimg 'The New Image Handle
  29.     DIM AS INTEGER x, y
  30.     DIM AS LONG c
  31.     FOR x = 0 TO _WIDTH(img)
  32.         FOR y = 0 TO _HEIGHT(img)
  33.             c = POINT(x, y)
  34.             PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), tr)
  35.         NEXT
  36.     NEXT
  37.     _SOURCE 0
  38.     _DEST 0
  39.  
  40.  

For some reason I could not modify the loaded images transparency, I could only create a new one and modify that one.
Hope this helps!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: question: light transmission with any image
« Reply #2 on: May 10, 2021, 11:53:11 am »
Hi. Try my method using _SETALPHA.

I assume that SETALPHA sets the transparency of the image ONLY once (for example, let's say you set the transparency for color _RGB32 (255,255,255) 50 percent in one step, you get a new color, then (and that's just my assumption) in the next step already fifty percent transparency will be calculated from the image in which the fifty percent transparency was set in the previous step) and that is why, for this reason I use COPYIMAGE in my program, I will set a new transparency once, display it and immediately discard a copy of the original image. So I use COPYIMAGE to adjust the transparency relative to the original image. The construction of this program is only for this purpose, so that you can use the + and - keys to continuously change the size of the alpha channel of the pic image.

Also keep in mind that _NEWIMAGE creates an image with a transparent background, the CLS command fills the entire image with the color 0,0,0,255 so then the transparency of the background is lost and this is why when alpha = 0 the background image is overwritten with black.

Code: QB64: [Select]
  1. mon = _NEWIMAGE(800, 800, 32)
  2. SCREEN mon: CLS
  3.  
  4. 'what do i do with this image to adjust its light transmission?
  5. DIM Alpha AS LONG, W AS INTEGER, H AS INTEGER, Back AS LONG, Pic AS LONG
  6. Pic = _LOADIMAGE("picture.jpg", 32)
  7. W = _WIDTH(Pic)
  8. H = _HEIGHT(Pic)
  9. Back = _NEWIMAGE(W, H, 32)
  10. _DEST Back
  11. 'changing color background - draw to 'back' screen
  12. FOR t = 0 TO W * H / 128
  13.     COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1))
  14.     LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  15.     PRINT "Ű"
  16. _DEST mon
  17. Alpha = 127
  18.     i$ = INKEY$
  19.     SELECT CASE i$
  20.         CASE "+": Alpha = Alpha + 1
  21.         CASE "-": Alpha = Alpha - 1
  22.     END SELECT
  23.     IF Alpha > 255 THEN Alpha = 255
  24.     IF Alpha < 0 THEN Alpha = 0
  25.     PicNew = _COPYIMAGE(Pic, 32)
  26.     _SETALPHA Alpha, _RGB32(0, 0, 0) TO _RGB32(255, 255, 255), PicNew 'set pic ALPHA
  27.     CLS
  28.     _PUTIMAGE , Back, mon ' place still full colored background image to screen
  29.     _PUTIMAGE , PicNew, mon ' place pic image with changed alpha for all colors in pic image
  30.     _FREEIMAGE PicNew
  31.     LOCATE 1
  32.     PRINT Alpha
  33.     _DISPLAY
  34.     _LIMIT 20
  35. LOOP UNTIL i$ = CHR$(27)
  36.  

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: question: light transmission with any image
« Reply #3 on: May 10, 2021, 12:24:49 pm »
Thank you Justsomeguy and Petr ! Both are good and work in the example.
I was completely confused. I want the "terror in the maze" game that the dragon fires fire to be a little transparent. I have both tried a solution and although it works in my example program, it does not. The game has a _CLEARCOLOR and then turns it into a hardware image. With your solution, I am not claiming transparency, but taking it away from color. Galleon’s glass method isn’t good here either, because I don’t want to turn an empty something into a sour cream, but I want to make an existing colored dot semi-transparent.

  [ You are not allowed to view this attachment ]  

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: question: light transmission with any image
« Reply #4 on: May 10, 2021, 12:26:28 pm »
In the evening I will write a game-compatible code for the problem. I wonder what needs to change in your ideas so far.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: question: light transmission with any image
« Reply #5 on: May 10, 2021, 12:36:30 pm »
if someone is trying to rewrite directly in the game, (I don't expect anyone to do this kindness), set cheatkey = 1 at the beginning and two dragons can be activated by pressing the "u" key.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: question: light transmission with any image
« Reply #6 on: May 10, 2021, 04:57:44 pm »
Umm... I know my eyes are old, but even I cannot see the link to "picture.jpg", or can any image suffice?
Logic is the beginning of wisdom.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: question: light transmission with any image
« Reply #7 on: May 10, 2021, 05:52:08 pm »
I show the problem. The program draws a fire with the background hidden. The fire, or "pic," is a piercing image. How can I adjust your transparency?

Code: QB64: [Select]
  1.  
  2. 'what do i do with this image to adjust its light transmission?
  3. temp = _LOADIMAGE("0000.bmp", 32)
  4. _DEST temp: _SOURCE temp
  5. DIM temp_color AS _INTEGER64
  6. temp_color = POINT(1, 1)
  7. _CLEARCOLOR temp_color
  8. pic = _COPYIMAGE(temp, 32)
  9.  
  10.  
  11.  
  12.  
  13. 'show neverend------------------------------------------------------
  14. mon = _NEWIMAGE(800, 800, 32)
  15. SCREEN mon: CLS
  16.  
  17.     'changing color background
  18.     FOR t = 0 TO 2999
  19.         COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
  20.         LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  21.         PRINT "Ű"
  22.     NEXT t
  23.  
  24.     'draw fire
  25.     marg = 50
  26.  
  27.     'display loacation
  28.     dx0 = marg: dy0 = marg
  29.     dx1 = _WIDTH(mon) - marg: dy1 = marg
  30.     dx2 = marg: dy2 = _HEIGHT(mon) - marg
  31.     dx3 = dx1: dy3 = dy2
  32.  
  33.     'picture location
  34.     sx0 = 0: sy0 = 0
  35.     sx1 = _WIDTH(pic) - 1: sy1 = 0
  36.     sx2 = 0: sy2 = _HEIGHT(pic) - 1
  37.     sx3 = sx1: sy3 = sy2
  38.  
  39.     _MAPTRIANGLE (sx0, sy0)-(sx1, sy1)-(sx2, sy2), pic TO(dx0, dy0)-(dx1, dy1)-(dx2, dy2)
  40.     _MAPTRIANGLE (sx3, sy3)-(sx1, sy1)-(sx2, sy2), pic TO(dx3, dy3)-(dx1, dy1)-(dx2, dy2)
  41.     _DISPLAY

  [ You are not allowed to view this attachment ]  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question: light transmission with any image
« Reply #8 on: May 10, 2021, 07:37:42 pm »
justsomeguy's procedure looked pretty good just from reading it.

Load it into a very very transparent image handle and _PutImage it all over area you want on fire with different widths and heights, the overlapping fires will begin to look real? just thinking out loud here...

If you need code for live fire let me know. (see Battleship Splash screen)
« Last Edit: May 10, 2021, 07:39:44 pm by bplus »

Offline justsomeguy

  • Newbie
  • Posts: 47
    • View Profile
Re: question: light transmission with any image
« Reply #9 on: May 10, 2021, 07:41:45 pm »
@bplus Thanks, but @Petr 's method might be faster and more efficient. Mine was a case of me struggling with a problem, finding something that worked, and never circling back to it to make it better.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question: light transmission with any image
« Reply #10 on: May 10, 2021, 07:47:26 pm »
@bplus Thanks, but @Petr 's method might be faster and more efficient. Mine was a case of me struggling with a problem, finding something that worked, and never circling back to it to make it better.

Yeah Petr's looked pretty complex (so I'd really have to test and play with) but he is fine coder and comes up with great effects, I still remember Christmas! :) His and Dav's so different.

I know yours is not a fast thing but once one very transparent image is loaded in handle, _PutImage can do allot of fast random layering. Ha! maybe it only works in my head, really great there!

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: question: light transmission with any image
« Reply #11 on: May 11, 2021, 02:00:38 am »
Thanks Bplus! I really like Battleship Fire! Very lifelike! I analyzed the code, although I don't fully understand how it works, but I find the solution interesting! The trouble is, I noticed that that fire wasn’t transparent either. So the original problem is not solved with this. For many things, it would be good for me to have a hardware image that has become BMP or JPG that I can make transparent because I might need it in other cases as well.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: question: light transmission with any image
« Reply #12 on: May 11, 2021, 03:35:08 am »
Quick question. The print statement, on line #22, seems to be a special "U" character. Can I assume that this is a "Windows" character?

I will try to attach a screen image. All the 'background' characters are continuously alternating colours....

  [ You are not allowed to view this attachment ]  
Logic is the beginning of wisdom.

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: question: light transmission with any image
« Reply #13 on: May 11, 2021, 04:00:35 am »
yes, in windows it's a "full" character. I did the alternating color to make the changing color background perceptible. My question would be how to make the fire semi-transparent

Offline justsomeguy

  • Newbie
  • Posts: 47
    • View Profile
Re: question: light transmission with any image
« Reply #14 on: May 11, 2021, 07:50:30 am »
Perhaps not the best way or fastest way, but here goes... I think @Petr 's method could also work but It may require layering several images.

The artifacts around the flame bitmap are due to those not quite being the full transparency color.

Code: QB64: [Select]
  1. 'what do i do with this image to adjust its light transmission?
  2. temp = _LOADIMAGE("0000.bmp", 32)
  3. _DEST temp: _SOURCE temp
  4. DIM temp_color AS _INTEGER64
  5. temp_color = POINT(1, 1)
  6. '_CLEARCOLOR temp_color
  7. 'pic = _COPYIMAGE(temp, 32)
  8.  
  9. pic = _NEWIMAGE(_WIDTH(temp), _HEIGHT(temp), 32)
  10. CALL setTransparency(temp, pic, 200, temp_color)
  11.  
  12.  
  13.  
  14. 'show neverend------------------------------------------------------
  15. mon = _NEWIMAGE(800, 800, 32)
  16. SCREEN mon: CLS
  17.  
  18.     'changing color background
  19.     FOR t = 0 TO 2999
  20.         COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
  21.         LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
  22.         PRINT "Ű"
  23.     NEXT t
  24.  
  25.     'draw fire
  26.     marg = 50
  27.  
  28.     'display loacation
  29.     dx0 = marg: dy0 = marg
  30.     dx1 = _WIDTH(mon) - marg: dy1 = marg
  31.     dx2 = marg: dy2 = _HEIGHT(mon) - marg
  32.     dx3 = dx1: dy3 = dy2
  33.  
  34.     'picture location
  35.     sx0 = 0: sy0 = 0
  36.     sx1 = _WIDTH(pic) - 1: sy1 = 0
  37.     sx2 = 0: sy2 = _HEIGHT(pic) - 1
  38.     sx3 = sx1: sy3 = sy2
  39.  
  40.     _MAPTRIANGLE (sx0, sy0)-(sx1, sy1)-(sx2, sy2), pic TO(dx0, dy0)-(dx1, dy1)-(dx2, dy2)
  41.     _MAPTRIANGLE (sx3, sy3)-(sx1, sy1)-(sx2, sy2), pic TO(dx3, dy3)-(dx1, dy1)-(dx2, dy2)
  42.     _DISPLAY
  43.  
  44. SUB setTransparency (img AS LONG, newimg AS LONG, tr AS INTEGER, backcolor AS LONG)
  45.     _SOURCE img ' Your loaded image handle
  46.     _DEST newimg 'The New Image Handle
  47.     DIM AS INTEGER x, y
  48.     DIM AS LONG c
  49.     FOR x = 0 TO _WIDTH(img)
  50.         FOR y = 0 TO _HEIGHT(img)
  51.             c = POINT(x, y)
  52.             IF c = backcolor THEN
  53.                 PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), 0)
  54.             ELSE
  55.                 PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), tr)
  56.             END IF
  57.         NEXT
  58.     NEXT
  59.     _SOURCE 0
  60.     _DEST 0
  61.  
  62.