Author Topic: Question about transparent images  (Read 4499 times)

0 Members and 1 Guest are viewing this topic.

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Question about transparent images
« on: April 09, 2019, 09:34:06 pm »
When using _SETALPHA on a transparent PNG image, it sets the pixel transparency for everything, not just the originally non-transparent pixels.

How do I handle fading in a transparent PNG image so that the formerly-transparent background does not become solid black?

Is there a function to create a mask of the image beforehand, or how should I handle this?

Thanks for any tips!

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: Question about transparent images
« Reply #1 on: April 10, 2019, 10:34:33 pm »
So I have dug through the documentation and it doesn't seem that QB64 supports changing the opacity of PNGs that already include opacity information.

_SETALPHA changes all pixels in the image, or only pixels of a certain colour.  There is no way to make _SETALPHA take already partially-opaque pixels of the PNG and then apply an opacity to that.

It seems the only way that QB64 supports translucent PNGs is with 1-bit transparency, pixel on or off.  That would be easy enough to create a colour mask for in QB64, but it will look much uglier than a PNG with 256 levels of translucency.

Another way I can see to deal with already translucent PNGs is to _COPYIMAGE the original image, then manually go over the entire image pixel by pixel and update the alpha values manually, taking into account the starting pixel's translucency value.  I doubt this would work fast enough to use in realtime at 1080p/60fps when fading a few translucent images in and out.

The other way I can see possibly working is to use _COPYIMAGE on the original image, then use _SETALPHA on the range that includes all colours more than 50% opaque, while turning colours less than 50% opaque fully transparent.  This way might work fast enough, but it would still have the issue that partially translucent pixels in the original image would not be honoured, thus losing all the beautiful antialised edges of the original image.

Is there really no way to adjust the opacity of an already-partially-translucent image in QB64?

If not, I think this is a really important feature.  As it stands now _SETALPHA is only relevant for 1-bit transparency, which is frankly silly -- _SETALPHA is a command that lets you assign a full 8bits of opacity, but it can't handle images which already contain partially-opaque pixels.  This is saddening.  :-/
« Last Edit: April 10, 2019, 10:38:56 pm by Raven_Singularity »

FellippeHeitor

  • Guest
Re: Question about transparent images
« Reply #2 on: April 10, 2019, 10:46:02 pm »
Try _SETALPHA to set all pixels, then _CLEARCOLOR to clear certain ones. Should give the desired transparency.

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: Question about transparent images
« Reply #3 on: April 10, 2019, 10:51:17 pm »
Try _SETALPHA to set all pixels, then _CLEARCOLOR to clear certain ones. Should give the desired transparency.

I don't think you're understanding the issue.  My image has no colour for 100% transparent, it has partially translucent pixels to begin with.  If I tell it to _SETALPHA 128 I would expect it to make the pixels that were 50% opaque to become 25% opaque, but it ignores the pre-existing translucency values of the pixels entirely.  There is no combination of _SETALPHA and _CLEARCOLOR that respect the pre-existing translucent pixels of my original image.

FellippeHeitor

  • Guest
Re: Question about transparent images
« Reply #4 on: April 10, 2019, 10:53:52 pm »
I don't think you're understanding my suggestion.

Is pixel (0, 0) a transparent one in your image? Keep it in mind for reference.

Use _SETALPHA in your image and now pixel (0, 0) is not as transparent anymore. A shade of grey with some alpha maybe? Good.

Now call _CLEARCOLOR and aim at the color that POINT will read at (0, 0).

I didn't test it, but in theory it sounds like it should work.

FellippeHeitor

  • Guest
Re: Question about transparent images
« Reply #5 on: April 10, 2019, 10:58:24 pm »
And voilĂ , it works:

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. logo& = _LOADIMAGE("forumtitle.png", 32)
  3. IF logo& = -1 THEN PRINT "Image not found": END
  4.  
  5. _SETALPHA 100, , logo&
  6. _SOURCE logo&
  7. _CLEARCOLOR POINT(0, 0), logo&
  8.  
  9.     _PUTIMAGE (x, y), logo&
  10.     x = x + 5
  11.     y = y + 5
  12.     _DISPLAY
  13.     _LIMIT 5

FellippeHeitor

  • Guest
Re: Question about transparent images
« Reply #6 on: April 10, 2019, 10:59:45 pm »
May not be perfect and end up clearing more than the intended pixels, but it is a workaround nonetheless.

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: Question about transparent images
« Reply #7 on: April 10, 2019, 11:19:37 pm »
It doesn't work at all, because you aren't understanding the issue.  The issue is partially opaque pixels in the original image.

Partially translucent images are a staple of video gaming, so it would be nice to have support for these images in QB64.


I have attached an image "rainbow.png" which will show you the issue first-hand.

I have attached "incorrect.jpg" which is _SETALPHA at 80% opacity, and "correct.jpg" for how this translucent image would look if it were actually made 80% opaque from the original image which contained partially-translucent pixels.

FellippeHeitor

  • Guest
Re: Question about transparent images
« Reply #8 on: April 10, 2019, 11:38:42 pm »
I see. And the alpha level will be changing throughout execution?

Edit: ah, yes I see it.

Quote
fading a few translucent images in and out
.
« Last Edit: April 10, 2019, 11:40:30 pm by FellippeHeitor »

FellippeHeitor

  • Guest
Re: Question about transparent images
« Reply #9 on: April 10, 2019, 11:40:55 pm »
No built-in solution exists as of yet then.

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Question about transparent images
« Reply #10 on: April 11, 2019, 12:25:33 am »
Is this what you want?
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(600, 600, 32)
  2.  
  3. img& = _LOADIMAGE("rainbow.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.  
  12.  
  13. SUB addAlpha2Img (img&, alpha AS INTEGER)
  14.     IF img& >= -1 THEN EXIT SUB
  15.     DIM buffer AS _MEM, o AS _OFFSET, o2 AS _OFFSET
  16.     buffer = _MEMIMAGE(img&)
  17.     o = buffer.OFFSET
  18.     o2 = o + _WIDTH(img&) * _HEIGHT(img&) * 4
  19.     DO
  20.         b = _MEMGET(buffer, o + 3, _UNSIGNED _BYTE)
  21.         IF (b + alpha) > 0 AND (b + alpha) < 255 THEN
  22.             _MEMPUT buffer, o + 3, (b + alpha) AS _UNSIGNED _BYTE
  23.         END IF
  24.         o = o + 4
  25.     LOOP UNTIL o = o2
  26.     _MEMFREE buffer
  27.  
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Question about transparent images
« Reply #11 on: April 11, 2019, 12:34:38 am »
Well that's a neat effect. If I use _INSTRREV() in the code, will it reappear?

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

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Question about transparent images
« Reply #12 on: April 11, 2019, 12:36:43 am »
Well that's a neat effect. If I use _INSTRREV() in the code, will it reappear?

Pete
What is the use of _instrrev() in this code Pete?
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: Question about transparent images
« Reply #13 on: April 11, 2019, 12:47:07 am »
No built-in solution exists as of yet then.

Is there an official place to submit feature requests?


I am working on a keyframe/tweening animation library which supports easing in/out on all transitions.  It will eventually be released to the public.  It is already quite functional.

I was working on Obj_Opacity(), but it seems this will only work in realtime if the original image uses only 1bit transparency.  I will attempt to do the opacity adjustment loop manually pixel-by-pixel to see how the performance is, but I suspect it will lag the rendering too much, which is currently very smooth.


@Ashish:
I will look at that code and let you know.  What I am going for is having taking an image handle and drawing it onto the main screen with an opacity applied to it, which affects all pixels correctly, including semi-translucent pixels in the original image.  The opacity is being applied every frame update as a percentage of opaque, 0.000 to 1.000.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Question about transparent images
« Reply #14 on: April 11, 2019, 12:47:31 am »
The use? Ah, humor. _INSTRREV() is the new reverse function to INSTR(). So, I thought if I inserted that at the end of your code, I could get the .png to reappear. :D

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