VGMaps

General Boards => Mapping Tips/Guides => Topic started by: Troy Lundin on March 01, 2012, 08:24:07 pm

Title: Degree of transparency.
Post by: Troy Lundin on March 01, 2012, 08:24:07 pm
Is there a way to tell how much transparency a game uses for any of it's layers with VBA?
Title: Re: Degree of transparency.
Post by: TerraEsperZ on March 01, 2012, 09:51:44 pm
If there is, it must be buried pretty deep. I know that the Gameboy Advance apparently uses Alpha Blending and there's some technical information on this page (http://www.coranac.com/tonc/text/gfx.htm#sec-blend) about the GBA's graphic effects, but I don't think there's any easy way aside from looking around VBA's various tools like the IO Viewer for example.

And even then, it never seems to be a simple transparency effect that's being used, which might have to do with the GBA's 15-bit color palette? I've long given up trying to reproduce the GBA's transparency effects perfectly and I just try to get it as reasonably close as I can without having access to Photoshop.
Title: Re: Degree of transparency.
Post by: Troy Lundin on March 02, 2012, 12:49:07 am
Good read.

So, using the calculation they gave: C = wA·A + wB·B.

wA = 0.625
wB = 0.5625

as shown in the IO Viewer. Blend mode is 01 or Alpha blend mode. How would I use this information to create a good transparency in a program like GIMP?
Title: Re: Degree of transparency.
Post by: Peardian on March 02, 2012, 02:08:15 am
It's certainly not easy,that's all I know. In fact, for all my GBA maps that involve blending, I either took screenshots of the blends (MLSS) or did my best guess.

Of course, seeing how you're handy with programming, you could try creating a script that takes two images and outputs the results in the way the GBA would do. In fact, something like that would be great for GBA mappers to have. If you do decide to attempt this, I recommend working with BMP format, so you don't have to bother with compression.
Title: Re: Degree of transparency.
Post by: DarkWolf on March 02, 2012, 08:13:57 am
If you do decide to attempt this, I recommend working with BMP format, so you don't have to bother with compression.

Just FYI, dealing with PNG in any higher-level language/environment like Java or .NET is pretty easy.  You don't have to write any decompression code yourself to read the files.  Even with lower-level languages like C there are libraries already written.  Several years back I still used BMP because I used MS Paint for a lot of things, but these days there really isn't much reason to use BMP unless you're dealing with some sort of legacy software or system.

Title: Re: Degree of transparency.
Post by: Troy Lundin on March 02, 2012, 10:56:39 am
A few years ago I figured out how GBA stores it's colors. It's values go from 0 to 31. So, to get to the actual RGB value you must multiply by 8. I grabbed the RGB values of the two layers. Layer A is the transparent layer while Layer B is the underlying layer. I took a screenshot of the blend in action. This is C.

A = 224, 224, 224
B =   0,  64, 120
C = 136, 176, 200

Converted into GBA colors gives the following:

A =  28, 28, 28
B =   0,  8, 15
C =  17, 22, 25

Now, if we use the formula given in the first post (C = (wA * A) + (wB * B)) where wA = 0.625 and wB = 0.5625 (these value are given via the IO Viewer) with the GBA colors then we get the following:

C = (wA * A) + (wB * B)

R = (0.625  * 28) + (0.5625 *  0)
G = (0.625  * 28) + (0.5625 *  8)
B = (0.625  * 28) + (0.5625 * 15)

R = 17.5 + 0
G = 17.5 + 4.5
B = 17.5 + 8.4375

R = 17.5
G = 22
B = 25.9375

As you can see, the results have to be floored to match up with the in-game results. But, the math works. This tells me how the formula works but I still don't know how to apply it. In GIMP, there is a simple slider for the percent of transparency to add to a layer. I'll fiddle around with it some more.
Title: Re: Degree of transparency.
Post by: Troy Lundin on March 02, 2012, 12:22:16 pm
Ok, Peardian, you got me. Because you said I was handy with programming I had to see if I could do this. Looks like I could. I got it to blend perfectly. Thank you. :D

Here it is: GBA Alpha Blender.zip (http://www.4shared.com/zip/vU9tiLsa/GBA_Alpha_Blender.html)

It's very basic right now and only supports one mode of blending (alpha). You will have to get the evA and evB values from the VBA IO Viewer as they are bound to be different per game or even per area. I included three images for testing purposes.

_clouds3.png is the blended image. It goes in Box A.
_darkClouds.png is the background image. It goes in Box B.
The last image is a screenshot from the game. I used it to compare the blending.

Also, the evA value is 0.625 and the evB value is 0.5625 for this game. Let me know if it doesn't work. Thanks again, Peardian. :D

Just FYI, dealing with PNG in any higher-level language/environment like Java or .NET is pretty easy.  You don't have to write any decompression code yourself to read the files.  Even with lower-level languages like C there are libraries already written.  Several years back I still used BMP because I used MS Paint for a lot of things, but these days there really isn't much reason to use BMP unless you're dealing with some sort of legacy software or system.

.NET handles PNG natively. Reading and writing is automatic without extra code.
Title: Re: Degree of transparency.
Post by: Maxim on March 02, 2012, 12:38:12 pm
To convert 5-bit-per-channel colour to 8-bit-per-channel colour (aka true colour) you should not multiply by 8, as that makes pure white a bit grey. You should use bit operations to repeat the value until it fills 8 bits:
Code: [Select]
r = (r5 << 3) | (r5 >> 2)
and repeat for g, b and a. But a lot of emulators do this wrong.
Title: Re: Degree of transparency.
Post by: Troy Lundin on March 02, 2012, 01:09:01 pm
To convert 5-bit-per-channel colour to 8-bit-per-channel colour (aka true colour) you should not multiply by 8, as that makes pure white a bit grey. You should use bit operations to repeat the value until it fills 8 bits:
Code: [Select]
r = (r5 << 3) | (r5 >> 2)
and repeat for g, b and a. But a lot of emulators do this wrong.

Not sure what you mean here. As far as I know, the GBA doesn't have a pure white color, as it can only get up to {248,248,248}.
Title: Re: Degree of transparency.
Post by: Peardian on March 02, 2012, 01:32:33 pm
I knew you could do it! :D

And that's good to know about PNG. Shows you how much experience I have with making programs. :P


Also, yeah, SNES is the same way. That's the brightest color you'll ever see in Yoshi's Island, too. (It became very apparent in the snow levels.)
Title: Re: Degree of transparency.
Post by: DarkWolf on March 02, 2012, 02:25:39 pm
To convert 5-bit-per-channel colour to 8-bit-per-channel colour (aka true colour) you should not multiply by 8, as that makes pure white a bit grey. You should use bit operations to repeat the value until it fills 8 bits:
Code: [Select]
r = (r5 << 3) | (r5 >> 2)
and repeat for g, b and a. But a lot of emulators do this wrong.

Not sure what you mean here. As far as I know, the GBA doesn't have a pure white color, as it can only get up to {248,248,248}.

Generally speaking when talking about video hardware the largest value per color channel should represent the most intense value.  So when you translate that over from 5-bit to 8-bit, 31 should translate into 255.  Like Maxim said, the emulator itself probably does the color conversion wrong.
Title: Re: Degree of transparency.
Post by: TerraEsperZ on March 02, 2012, 03:27:09 pm

Generally speaking when talking about video hardware the largest value per color channel should represent the most intense value.  So when you translate that over from 5-bit to 8-bit, 31 should translate into 255.  Like Maxim said, the emulator itself probably does the color conversion wrong.

I'm pretty sure DarkWolf has it right. Pure white (RGB 255, 255, 255) is basically the brightest color any standard display can output, but the actual bit value of all three channel will change depending on the bit/color depth. I think I once read that most emulators end up with 248, 248, 248 as the brightest white simply because it requires a simpler bit operation (something like simply shifting the bits to the left?) than to adjust the value so that the minimum value is 0 but the maximum is 255 instead pf 248.
Title: Re: Degree of transparency.
Post by: Maxim on March 03, 2012, 11:15:42 am
It's a common error in emulators. The algorithm I posted (value extension rather than zero extension) is only marginally slower than simple left-shifting, as it's two shifts and an OR which are all incredibly fast operations, but the naive approach is to use a multiply and divide, which emulator authors immediately discount as too slow. Actually, on modern (and not so modern) CPUs even that is fast enough...
Title: Re: Degree of transparency.
Post by: Peardian on March 03, 2012, 12:00:22 pm
Considering most of the emulators do it that way, and all of the graphics used with and around the program are from an emulator, is it really that much of an issue?
Title: Re: Degree of transparency.
Post by: Revned on March 03, 2012, 12:43:11 pm
If you care about perfection it is. I wrote a script to pull the icons out of Gamecube disc images at one point and I was confused as to why they didn't ever come out with true white. Eventually I realized why, and the fix made them look noticeably better to me.
Title: Re: Degree of transparency.
Post by: Maxim on March 03, 2012, 02:01:05 pm
To put it another way, if you're dealing with input images that go up to 248 then your output should too. But the assertion that that's the only way things work would be wrong.

On the other hand, "converting to GBA colors" gives identical results with either implementation.
Title: Re: Degree of transparency.
Post by: Troy Lundin on March 03, 2012, 02:24:25 pm
Edit: Made a new thread for the program here: https://www.vgmaps.com/forums/index.php?topic=1770.0
Title: Re: Degree of transparency.
Post by: Peardian on March 05, 2012, 01:49:03 am
Now, I'm afraid I don't know much about different GBA blendings, but if you want a game that might have different kinds, Yoshi's Island: Super Mario Advance 3 has (I think) some different kinds. With the mode7 blob enemies, submarine room water, dark room spots, and other special effects, it might be worth checking into.
Title: Re: Degree of transparency.
Post by: Troy Lundin on March 05, 2012, 02:44:47 am
Sweet, I'll check 'em out.

There are only three modes of blending. Well, two really. The first is alpha blending and the second is brightness adjustment (fade-in, fade-out).