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

0 Members and 1 Guest are viewing this topic.

Offline MasterGy

  • Forum Regular
  • Posts: 241
  • people lie, math never lies
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]
mon = _NEWIMAGE(800, 800, 32)
SCREEN mon: CLS

'what do i do with this image to adjust its light transmission?
pic = _LOADIMAGE("d:\picture.jpg", 32)


'changing color background
FOR t = 0 TO 2999
    COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
    LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
    PRINT "Ű"
NEXT t

_DEST mon
_SOURCE pic
_PUTIMAGE



Offline justsomeguy

  • Newbie
  • Posts: 41
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]
mon = _NEWIMAGE(800, 800, 32)
SCREEN mon: CLS

'what do i do with this image to adjust its light transmission?
pic = _LOADIMAGE("d:\picture.jpg", 32)

newimg = _NEWIMAGE(_WIDTH(pic), _HEIGHT(pic), 32)

'   setTransparency ( Image that you loaded, New Image that you created , Transparency vale (0-255) )
CALL setTransparency(pic, newimg, 200)
'   Don't forget to free images after you are done.


'changing color background
FOR t = 0 TO 2999
    COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
    LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
    PRINT "Ű"
NEXT t



_DEST mon
_SOURCE newimg
_PUTIMAGE


SUB setTransparency (img AS LONG, newimg AS LONG, tr AS INTEGER)
    _SOURCE img ' Your loaded image handle
    _DEST newimg 'The New Image Handle
    DIM AS INTEGER x, y
    DIM AS LONG c
    FOR x = 0 TO _WIDTH(img)
        FOR y = 0 TO _HEIGHT(img)
            c = POINT(x, y)
            PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), tr)
        NEXT
    NEXT
    _SOURCE 0
    _DEST 0
END SUB


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: 1665
  • The best code is the DNA of the hops.
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]
mon = _NEWIMAGE(800, 800, 32)
SCREEN mon: CLS

'what do i do with this image to adjust its light transmission?
DIM Alpha AS LONG, W AS INTEGER, H AS INTEGER, Back AS LONG, Pic AS LONG
Pic = _LOADIMAGE("picture.jpg", 32)
W = _WIDTH(Pic)
H = _HEIGHT(Pic)
Back = _NEWIMAGE(W, H, 32)
_DEST Back
'changing color background - draw to 'back' screen
FOR t = 0 TO W * H / 128
    COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1))
    LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
    PRINT "Ű"
NEXT t
_DEST mon
Alpha = 127
DO
    i$ = INKEY$
    SELECT CASE i$
        CASE "+": Alpha = Alpha + 1
        CASE "-": Alpha = Alpha - 1
    END SELECT
    IF Alpha > 255 THEN Alpha = 255
    IF Alpha < 0 THEN Alpha = 0
    PicNew = _COPYIMAGE(Pic, 32)
    _SETALPHA Alpha, _RGB32(0, 0, 0) TO _RGB32(255, 255, 255), PicNew 'set pic ALPHA
    CLS
    _PUTIMAGE , Back, mon ' place still full colored background image to screen
    _PUTIMAGE , PicNew, mon ' place pic image with changed alpha for all colors in pic image
    _FREEIMAGE PicNew
    LOCATE 1
    PRINT Alpha
    _DISPLAY
    _LIMIT 20
LOOP UNTIL i$ = CHR$(27)

Offline MasterGy

  • Forum Regular
  • Posts: 241
  • people lie, math never lies
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.

 
toforum.jpg

Offline MasterGy

  • Forum Regular
  • Posts: 241
  • people lie, math never lies
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

  • Forum Regular
  • Posts: 241
  • people lie, math never lies
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: 1166
  • Live long and prosper.
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

  • Forum Regular
  • Posts: 241
  • people lie, math never lies
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]

'what do i do with this image to adjust its light transmission?
temp = _LOADIMAGE("0000.bmp", 32)
_DEST temp: _SOURCE temp
DIM temp_color AS _INTEGER64
temp_color = POINT(1, 1)
_CLEARCOLOR temp_color
pic = _COPYIMAGE(temp, 32)




'show neverend------------------------------------------------------
mon = _NEWIMAGE(800, 800, 32)
SCREEN mon: CLS

DO: _LIMIT 20
    'changing color background
    FOR t = 0 TO 2999
        COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
        LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
        PRINT "Ű"
    NEXT t

    'draw fire
    marg = 50

    'display loacation
    dx0 = marg: dy0 = marg
    dx1 = _WIDTH(mon) - marg: dy1 = marg
    dx2 = marg: dy2 = _HEIGHT(mon) - marg
    dx3 = dx1: dy3 = dy2

    'picture location
    sx0 = 0: sy0 = 0
    sx1 = _WIDTH(pic) - 1: sy1 = 0
    sx2 = 0: sy2 = _HEIGHT(pic) - 1
    sx3 = sx1: sy3 = sy2

    _MAPTRIANGLE (sx0, sy0)-(sx1, sy1)-(sx2, sy2), pic TO(dx0, dy0)-(dx1, dy1)-(dx2, dy2)
    _MAPTRIANGLE (sx3, sy3)-(sx1, sy1)-(sx2, sy2), pic TO(dx3, dy3)-(dx1, dy1)-(dx2, dy2)
    _DISPLAY
LOOP

 
0000.bmp


Offline bplus

  • Forum Resident
  • Posts: 7449
  • b = b + ...
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: 41
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

  • Forum Resident
  • Posts: 7449
  • b = b + ...
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

  • Forum Regular
  • Posts: 241
  • people lie, math never lies
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: 1166
  • Live long and prosper.
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....

 
test.png
Logic is the beginning of wisdom.

Offline MasterGy

  • Forum Regular
  • Posts: 241
  • people lie, math never lies
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: 41
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]
'what do i do with this image to adjust its light transmission?
temp = _LOADIMAGE("0000.bmp", 32)
_DEST temp: _SOURCE temp
DIM temp_color AS _INTEGER64
temp_color = POINT(1, 1)
'_CLEARCOLOR temp_color
'pic = _COPYIMAGE(temp, 32)

pic = _NEWIMAGE(_WIDTH(temp), _HEIGHT(temp), 32)
CALL setTransparency(temp, pic, 200, temp_color)



'show neverend------------------------------------------------------
mon = _NEWIMAGE(800, 800, 32)
SCREEN mon: CLS

DO: _LIMIT 20
    'changing color background
    FOR t = 0 TO 2999
        COLOR _RGB32(256 * RND(1), 256 * RND(1), 256 * RND(1)), 0
        LOCATE 1 + 40 * RND(1), 1 + 99 * RND(1)
        PRINT "Ű"
    NEXT t

    'draw fire
    marg = 50

    'display loacation
    dx0 = marg: dy0 = marg
    dx1 = _WIDTH(mon) - marg: dy1 = marg
    dx2 = marg: dy2 = _HEIGHT(mon) - marg
    dx3 = dx1: dy3 = dy2

    'picture location
    sx0 = 0: sy0 = 0
    sx1 = _WIDTH(pic) - 1: sy1 = 0
    sx2 = 0: sy2 = _HEIGHT(pic) - 1
    sx3 = sx1: sy3 = sy2

    _MAPTRIANGLE (sx0, sy0)-(sx1, sy1)-(sx2, sy2), pic TO(dx0, dy0)-(dx1, dy1)-(dx2, dy2)
    _MAPTRIANGLE (sx3, sy3)-(sx1, sy1)-(sx2, sy2), pic TO(dx3, dy3)-(dx1, dy1)-(dx2, dy2)
    _DISPLAY
LOOP

SUB setTransparency (img AS LONG, newimg AS LONG, tr AS INTEGER, backcolor AS LONG)
    _SOURCE img ' Your loaded image handle
    _DEST newimg 'The New Image Handle
    DIM AS INTEGER x, y
    DIM AS LONG c
    FOR x = 0 TO _WIDTH(img)
        FOR y = 0 TO _HEIGHT(img)
            c = POINT(x, y)
            IF c = backcolor THEN
                PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), 0)
            ELSE
                PSET (x, y), _RGBA32(_RED(c), _GREEN(c), _BLUE(c), tr)
            END IF
        NEXT
    NEXT
    _SOURCE 0
    _DEST 0
END SUB