Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
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  
  alpha blending by hand  (Read 1153 times)
0 Members and 1 Guest are viewing this topic.
Offline bedelf

Junior Member




Are you suggesting coconuts migrate?


« Posted 2003-02-12 02: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 06: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 09: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.

 

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

The first screenshot will be displayed as a thumbnail.

BurntPizza (8 views)
2014-09-21 00:34:41

moogie (7 views)
2014-09-21 00:26:15

UprightPath (22 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

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

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

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

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

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

BurntPizza (52 views)
2014-09-07 01:13:42
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, 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!