Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Blending Alpha Pixels?  (Read 815 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Member


Medals: 1



« Posted 2012-12-08 18:14:46 »

Consider the following code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
      for(int y = 0; y < ht; y++) {

         int yp = ypos + y;

         if(yp >= pri.getHeight() || yp < 0)
            continue;

         for(int x = 0; x < wt; x++) {
            int xp = xpos + x;

            if(xp >= pri.getWidth() || xp < 0)
               continue;

            int pos = yp * pri.getWidth() + xp;
            if(pos < 0 || pos >= pixels.length)
               continue;
           
            int cv = pixels[pos];
            int nv = colors[y*wt + x];
            double alpha = nv >> 24 & 0xFF;
            if(alpha < 255) {
               nv = ColorUtils.packInt(255, cv >> 16 & 0xFF, cv >> 8 & 0xFF, cv & 0xFF);
               nv = (int) Math.round(cv * (alpha / 255) + nv * (1 - alpha / 255));
            }
           
            pixels[pos] = nv;
         }
      }


As you may be able to guess, I'm attempting to blend pixels that are translucent.  This method doesn't appear to be working very well, however.  Given pixel value 'a' (current set value in image) and new translucent value 'b' (let's assume alpha of 200 or so), how do you blend the two pixels so that 'a' still appears through 'b' but with modified coloring (i.e red + blue = purplish)?
Offline Longor1996
« Reply #1 - Posted 2012-12-08 18:39:12 »

How about Interpolating them?
Like:
1  
2  
interpolate(a,b,factor)
interpolate(sourceColor,destinationColor,alphaValue)


That should work.

Sorry for my bad English! That's because i am from Germany.
Offline davedes
« Reply #2 - Posted 2012-12-08 19:01:51 »

Do you mean standard blending that looks like this?


(Where the foreground image is at 75% opacity)

Typically we use SRC_ALPHA, ONE_MINUS_SRC_ALPHA:
1  
{foreground}*sourceAlpha + {background}*(1-sourceAlpha)


Looks roughly like this: (you don't need to use floats)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
//assuming TYPE_INT_ARGB


//get background pixels (destination)
int dstValue = pixels[dstPos];
float dstA = ((dstValue & 0xff000000) >>> 24) / 255f;
float dstR = ((dstValue & 0x00ff0000) >>> 16) / 255f;
float dstG = ((dstValue & 0x0000ff00) >>> 8) / 255f;
float dstB = ((dstValue & 0x000000ff)) / 255f;

//get foreground pixels (source)
int srcValue = pixels[srcPos];
float srcA = ((srcValue & 0xff000000) >>> 24) / 255f;
float srcR = ((srcValue & 0x00ff0000) >>> 16) / 255f;
float srcG = ((srcValue & 0x0000ff00) >>> 8) / 255f;
float srcB = ((srcValue & 0x000000ff)) / 255f;

srcR *= srcA;
srcG *= srcA;
srcB *= srcA;

//final output
float R = srcR + dstR*(1-srcA);
float G = srcG + dstG*(1-srcA);
float B = srcB + dstB*(1-srcA);

//if we're working with the screen, we typically will want A=255
//in this case TYPE_INT_RGB makes more sense to use
pixels[dstPos] = (255 << 24) | ((int)(R * 255) << 16) | ((int)(G * 255) << 8) | (int)(B * 255);

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #3 - Posted 2012-12-08 19:12:26 »

That works.  Only problem is, it's killing performance.  My fixed 60fps rendering system dropped to 14-15fps using that algorithm.  Do you have any ideas on how to prevent that?
Offline davedes
« Reply #4 - Posted 2012-12-08 20:32:55 »

I suspect there is something wrong with your code or game loop if you're getting such poor performance.

Iterate through your pixels with a single loop, instead of two:
1  
2  
3  
4  
5  
6  
for (int i=0; i<WIDTH*HEIGHT; i++) {
   int y = i / WIDTH;
   int x = i - WIDTH*y;
   
   ...
}


Some other areas that could be improved:
  • Use TYPE_INT_RGB like I suggested earlier
  • Use integers instead of converting to float and back
  • Store your sprites/colors unpacked: { a, r, g, b }
  • Use Graphics and BufferedImages where possible to take advantage of hardware

Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #5 - Posted 2012-12-08 21:37:19 »

I suspect there is something wrong with your code or game loop if you're getting such poor performance.

Iterate through your pixels with a single loop, instead of two:
1  
2  
3  
4  
5  
6  
for (int i=0; i<WIDTH*HEIGHT; i++) {
   int y = i / WIDTH;
   int x = i - WIDTH*y;
   
   ...
}



I tried using the single for loop as you suggested and it hurt performance even more.  It knocked it down to 48fps with only 3 render jobs.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (26 views)
2014-09-07 01:10:19

mitcheeb (34 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, such as Suomen Netti Casino for Finnish casinos reviews, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!