Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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  
  alpha blending by hand  (Read 961 times)
0 Members and 1 Guest are viewing this topic.
Offline bedelf

Junior Member




Are you suggesting coconuts migrate?


« Posted 2003-02-12 03:54:35 »

Hrm, I can't seem to get this code working to blend colors together.  If anyone has done this, show me some quick code to blend these values, no lookup tables or any bullshit, just the raw operations.

red source pixel =                 255
red destination pixel =          0
source pixel alpha value of   127

I think I want what AlphaComposite specifies as SRC_OVER. Destination is an opaque pixel and were blending a source pixel into that with it's alpha value. I've tried alot of different things and I've gotten alot of really strange results. bleh, nothing but problems today.
Offline bedelf

Junior Member




Are you suggesting coconuts migrate?


« Reply #1 - Posted 2003-02-12 07:56:55 »

Something like this if you have an array of source pixels and dest pixels:

           int alpha;
           float aS, aD;
           float sr, sg, sb, dr, dg, db;
           
           for (int i = 0; i != src.length; i++) {
                 alpha = src >> 24 & 0xFF;
                 aS = (float) alpha / 255.0F;
                 aD = 1 - aS;
                 
                 if (alpha == 255) {
                       dest = src;
                 } else if (alpha > 0) {
                       sr = src >> 16 & 0xFF;
                       sg = src >> 8 & 0xFF;
                       sb = src >> 0 & 0xFF;
                       dr = dest >> 16 & 0xFF;
                       dg = dest >> 8 & 0xFF;
                       db = dest >> 0 & 0xFF;
                       dest = (255 << 24) | (byte) ((sr * aS) + (dr * aD)) << 16 |
                                   (byte) ((sg * aS) + (dg * aD)) << 8 |
                                   (byte) ((sb * aS) + (db * aD)) << 0;
                 }
           }

I would just like say, NI. Thank you.

Edit: Oh, can anyone confirm that this is accurate and indeed what I was trying to accomplish?
Offline alexz

Senior Newbie




Java games rock!


« Reply #2 - Posted 2003-02-12 10:32:48 »

Four years ago I wrote the following code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
      public static void blend(int[] src, int[] dst)
      {
            int length = dst.length;
            for (int i = 0; i < length; ++i)
            {
                  int srcval = src[i];
                  int dstval = dst[i];
                 
                  int alpha = (srcval >>> 24) + 1; // make alpha in [1, 256] range...
                 
                  int srb = srcval & 0x00ff00ff;
                  int drb = dstval & 0x00ff00ff;
                  int rb  = ((((srb - drb) * alpha) >>> 8) + drb) & 0x00ff00ff;
           
                  int sag = (srcval & 0xff00ff00) >>> 8;
                  int dag = (dstval & 0xff00ff00) >>> 8;
                  int ag  = (((((sag - dag) * alpha) >>> 8) + dag) << 8) & 0xff00ff00;
           
                  dst[i] = ag | rb;
            }
      }


This piece of code is from my own rasterizer (capable to draw antialiased lines with different colors at ends, blending and so on) written in Java 1.0. As far as I remember I'd spent several hours to optimize this function and algorithm so the final performance of it is quite good (for pure Java). Cool

Hope this helps...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (91 views)
2013-05-17 21:29:12

alaslipknot (100 views)
2013-05-16 21:24:48

gouessej (130 views)
2013-05-16 00:53:38

gouessej (125 views)
2013-05-16 00:17:58

theagentd (136 views)
2013-05-15 15:01:13

theagentd (123 views)
2013-05-15 15:00:54

StreetDoggy (165 views)
2013-05-14 15:56:26

kutucuk (187 views)
2013-05-12 17:10:36

kutucuk (190 views)
2013-05-12 15:36:09

UnluckyDevil (196 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.109 seconds with 20 queries.