gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Graphics > Brightness/Gamma on Hardware

#19248 - josath - Thu Apr 15, 2004 8:01 am

Hello,
I wrote a GBA game without actually having a gba or flash cart (wizards), and when i finally got a chance to play it on the hardware, i noticed the colors were much darker. Is there a way (i'm sure there is, i just can't think of how) to lighten the colors, not within my graphics programs (gimp, psp etc), but within the code it self, like increase the brightness of all the colors in the palette.

I tried a couple different algorithms, but I couldn't come up with any that worked (I tried adding to the color, no good, then I split it into R,G,B components, and added to each of them, but that still washed all the color out, i also tried multiplying).

Maybe i had some errors in my code, not sure, but basically i would just loop over the palette and perform some operation on each color. What I am looking for, i guess, is an operation/formula I can apply to a color (RGB555) that will brighten it (and not make it too large, if you do that it loops back into dark colors)

I can show some of my test code if you want, but it doesn't really work right.

Thanks for the help.
-David

#19254 - bomberman - Thu Apr 15, 2004 12:04 pm

I had the same problem...

Basically you need to implement gamma correction. I did this in Atomix (see http://rmstudiogames.free.fr) and you can select the level at run time. The algorithm is not about adding or applying linear ratio. The gamma correction is a "square algorithm". To sum up you choose a ratio and you apply ratio*ratio.

I googled for "gamma correction", found pages which explained how gamma correction works and it was very easy to code it for the GBA format.

Good luck !

#19265 - Lord Graga - Thu Apr 15, 2004 5:01 pm

you *could* be lazy and just use the blending registers :)

Else, when you load the palette, add 10% to the color it loads, and check for any errors that should occur.

#19268 - josath - Thu Apr 15, 2004 6:15 pm

ok, figured it out, i originally had some bugs in my code.
this seems to work pretty well. call brigten with an amount, and it calls bright for each in dividual color. master_palette and map1_cmap are my palettes stored in rom for obj and bg respectivly.

Code:

u16 bright(u16 col, s8 amount)
{
    s16 r, g, b;

   //get individual colors out
   r = col & 31;
   g = (col >> 5) & 31;
   b = (col >> 10) & 31;

   //add to each color component
        r+=amount;
        g+=amount;
        b+=amount;

   //make sure none of the color components are out of range
   if(r > 31)
       r = 31;
   if(g > 31)
       g = 31;
   if(b > 31)
       b = 31;
        if(r < 0)
            r=0;
        if(g < 0)
            g=0;
        if(b < 0)
            b=0;

   //return the color combined back together
   return (r) + (g<<5) + (b<<10);

}

void brighten(s8 amount)
{
    int i;
    WaitForVsync();

    //call bright for each color in both obj and bg palettes
    for(i = 0; i<256; i++)
    {
   OBJPaletteMem[i] = bright(master_Palette[i], amount);
   BGPaletteMem[i] = bright(map1_cmap[i], amount);
    }
}
[/code]

#19522 - FluBBa - Wed Apr 21, 2004 10:13 am

Use a gamma style algorithm instead.
One channel:
Code:

r1=col & 0x1F
r2=0x1F - r1
r2=r2*r2
r2=r2/0x1F
r2=0x1F-r2

You will probably want to do some linear scaling between the 2 values to set the gamma level also.
Code:

ratio1=2, ratio2=3
r2=(r2*ratio2 + r1*ratio1) / 5



You can also check the source (assembler) to PCEAdvance on my hp, but it uses RGB333 to gamma corrected BGR555.
_________________
I probably suck, my not is a programmer.

#19543 - Miked0801 - Wed Apr 21, 2004 6:00 pm

Do yourself a favor and turn this into a lookup table. Same results but much faster to access. :)

#19547 - josath - Wed Apr 21, 2004 7:18 pm

wow, thanks flubba, this works much better than my algorithm, my brightness(gamma) adjustments look a lot better now.

#19584 - FluBBa - Thu Apr 22, 2004 12:03 pm

Maybe an entry for the FAQ?
A more correct one can probably be made so that you don't lose bit's and pieces.
_________________
I probably suck, my not is a programmer.

#32171 - Issac - Sat Dec 18, 2004 11:48 pm

umm.... All this looks goood.... but i dont understand anything... should I put that in a header or something??
Im not that good with this yet.....
someone please explain this a little more.. and how to actually use it (or is it used by itself?)

im a noob... accept it ;)
_________________
yeah, well, maybe... or? anyways.... eh... what was i talking about??

#32172 - DekuTree64 - Sun Dec 19, 2004 12:00 am

Issac wrote:
umm.... All this looks goood.... but i dont understand anything... should I put that in a header or something??
Im not that good with this yet.....
someone please explain this a little more.. and how to actually use it (or is it used by itself?)

im a noob... accept it ;)


I wouldn't worry about such little things as gamma correction at first. Just tune your palettes to the screen you like (GBA, SP, DS, TV) and add in the gamma correction later on if you really want it.

Normally you don't want to put code in headers, just the function prototypes to call things. Then put the function itself in a .c file somewhere. Doesn't matter too much where it is as long as it gets linked in, just depends on how you want to keep things organized.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#32176 - Issac - Sun Dec 19, 2004 12:46 am

well, I'd just want to have a bit of code to make it all brighter...
its a pain in the @$$ to test my games on hardware (except ngc-gba player) since All i have is the old gba and the gbaplayer...

but it would be better to fix everything so it becomes brighter...
_________________
yeah, well, maybe... or? anyways.... eh... what was i talking about??

#32181 - tepples - Sun Dec 19, 2004 2:48 am

If you're not using the blending register, try fading partway to white.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32215 - Issac - Sun Dec 19, 2004 1:56 pm

you mean... makeing all the sprites n backgrounds brighter in eg. photoshop?
_________________
yeah, well, maybe... or? anyways.... eh... what was i talking about??

#32229 - tepples - Sun Dec 19, 2004 6:58 pm

CowBite Spec and GBATEK say you can do it in hardware. Try this:
Code:
  REG_BLDMOD = 0x00BF;  /* turn on fade to white for all layers */
  REG_COLEY = 4;        /* 0 = no fade; 16 = full whiteout */

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32275 - Issac - Mon Dec 20, 2004 11:45 am

been reading this a little.. and I dont really know how to use it...
I need to define it.. but i dont know how and such....
could you please explain?

or...

should i just use like
u16 REG_BLDMOD;
u16 REG_COLEY;

or am i stupid? (ok dont answear to that ;))
_________________
yeah, well, maybe... or? anyways.... eh... what was i talking about??

#32283 - Cearn - Mon Dec 20, 2004 5:28 pm

REG_BLDMOD and REG_COLEY are IO registers, just like REG_DISPCNT and REG_BGxCNT. Most tutorials and demos come with a file gba.h or regs.h that contain all these definitions, so you probably have them already. If not, try
Code:

#define REG_BLDMOD  *(volatile u16*)(0x06000050)
#define REG_COLEY  *(volatile u16*)(0x06000054)  // Might be named REG_COLY

What these registers actually do is explained in the links that tepples gave. (In CowBite, you might have to go to the "Effects Registers" manually, the direct link doesn't work for me for some reason. Silly IE)

#32288 - Fatnickc - Mon Dec 20, 2004 6:41 pm

And how would one make it work in mode 4?
Or wouldn't you?

#32293 - Issac - Mon Dec 20, 2004 7:58 pm

ooh ok :) now i dont get any errors.... i havnt noticed any difference yet... gonna play around a bit with it though...

my gba_regs.h didnt have those two... thats why it didnt work at first.. thank you!
_________________
yeah, well, maybe... or? anyways.... eh... what was i talking about??

#32303 - tepples - Mon Dec 20, 2004 11:44 pm

Try setting COLEY to 0, waiting a few frames, setting it to 8, waiting a few frames, and repeating the process. You should be able to see a difference then.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32490 - Fatnickc - Thu Dec 23, 2004 9:19 am

And, Mode 4?

#32525 - tepples - Thu Dec 23, 2004 5:04 pm

Mode 4 uses the backdrop, the sprites, and background 2. How, specifically, did fading using the technique I mentioned not work in Mode 4?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32747 - Issac - Sat Dec 25, 2004 7:37 pm

well... as I said before.. I didn't notice any diference...
and i dont know how to wait a few frames.. but that has nothing to do with it...
and I saw now that I used mode 4...

and.. i changed the coley to 16 and to 0 and tried everything...

but it seems that it wont work in mode 4..

nothing happens... and.. it is kinda.... distrubing :(
_________________
yeah, well, maybe... or? anyways.... eh... what was i talking about??

#32772 - Cearn - Mon Dec 27, 2004 5:57 pm

Here's a minimal demo for blending in mode 4. Just add copying your picture and palette and you should be all set. It should display your picture as oscillating between normal and fully white, if it doesn't, there's something wrong with your emulator.

Code:

// some defines for REG_BLDMOD
#define BLD_BG0   0x0001
#define BLD_BG1   0x0002
#define BLD_BG2   0x0004
#define BLD_BG3   0x0008
#define BLD_OBJ   0x0010
#define BLD_BD    0x0020

#define BLD_NULL    0x0000
#define BLD_STD     0x0040
#define BLD_WHITE   0x0080
#define BLD_BLACK   0x00c0

void vid_vsync()
{
   while(REG_VCOUNT >= 160);   // wait till VBlank is over
   while(REG_VCOUNT < 160);   // wait till VBlank
}

int main()
{
   // copy your bitmap and palette

   // dma_memcpy(vid_page, gba_picData, gba_picSize);
   // dma_memcpy(pal_bg_mem, gba_picPal, 8);

   REG_DISPCNT= VID_MODE4 | VID_BG2;
   REG_BLDMOD = BLD_WHITE | BLD_BG2;

   u32 frame=0, white;

   while(1)
   {
      vid_vsync();
      frame++;
      
      white= frame>>2;   // change after 4 frames (fixed point, hoozah!)
      if(white & 0x10)
         white ^= 0x1f;   // for reversal at 16

      REG_COLY= white & 0x1f;
   }
   return 0;
}

#32788 - tepples - Mon Dec 27, 2004 10:45 pm

You may have special effects (blend, mosaic, etc) turned off in your emulator to increase frame rate. You need to turn those back on in order to see fades. For example, in VisualBoyAdvance for Windows, make sure Options > Video > Disable SFX is unchecked.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32815 - Fatnickc - Tue Dec 28, 2004 9:50 am

Sorry I haven't been able to get back to anyone on this, but my grandmother is in the room with the computer I do GBA stuff on (because it has Photoshop, I don't). She's leaving today in about 3 hours-ish so I will be able to see about the Mode 4 stuff.

#32842 - tepples - Tue Dec 28, 2004 4:36 pm

Fatnickc wrote:
the computer I do GBA stuff on (because it has Photoshop, I don't)

Have you tried GIMP?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32843 - dagamer34 - Tue Dec 28, 2004 4:46 pm

I think Photoshop is overkill for pixel work. Paint Shop Pro is suited much better and only $99 for the latest version. You can probably find a previous version for much cheaper.
_________________
Little kids and Playstation 2's don't mix. :(

#32844 - tepples - Tue Dec 28, 2004 4:54 pm

dagamer34 wrote:
Paint Shop Pro is suited much better and only $99 for the latest version.

It would appear that JASC and Adobe are either in a price war or trying to chase GIMP users, as I found Photoshop Elements for $30.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32845 - Fatnickc - Tue Dec 28, 2004 4:56 pm

I meant elements 2.0. I know that Paint Shop Pro works, and I have it on this computer, but Photoshop Elements works more easily. Plus, this computer is really S..L..O..W, and it likes to crash alot.

#32846 - tepples - Tue Dec 28, 2004 5:08 pm

Slow as in slower than mine (PIII 866 MHz), which is just barely fast enough to run VBA at full speed?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32849 - Fatnickc - Tue Dec 28, 2004 5:42 pm

Slow as in Pentium II 333MHz, 640k system memory with 63Mb of added memory. A 4Gig hard drive, wich has been madly partitioned before the computer was mine, so the C drive is full up and the rest.. aren't. A monitor from 1992 (seriously) which I got free this year from a weird man. The only nice bit is the wireless card which I bought. I asked for some computer-bits for Christmas but didn't get any, and my birthday is the 16th so, maybe.. -_-

#32859 - tepples - Tue Dec 28, 2004 8:35 pm

OK, so you mean slow as in my 1999 laptop (PII 333 MHz, 64 MB RAM, 6 GB HD). On a machine like that, you might want to stick to NES or GBC development.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#32941 - Fatnickc - Wed Dec 29, 2004 1:30 pm

Exactimo. But I like GBA dev. That's why I can't dev on this computer, so I use the newer one, which my dad enjoys modding (once I overclocked it a bit over the top, but didn't fry it). Talking of which, I have made a very basic, not very nice game called 'Acorn Collector' for a squirrel-mad friend. I might give you a look at it when I get onto the computer (my two-years older brother ALWAYS plays championship manager and other long games just when I want to go on the computer..).
Thanks,
Nick.

#33088 - sgeos - Thu Dec 30, 2004 6:27 pm

Deal with a slow emulator and get an MBV2 cable for testing? (Or a flash cart if you can afford it. =)

-Brendan

#33089 - Fatnickc - Thu Dec 30, 2004 6:33 pm

I'm fine not doing dev on my computer. I'll probably update it sometime, when I have the cash ;).