Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
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  
  Fastest RGB 24bit color blend (and others...)  (Read 2871 times)
0 Members and 1 Guest are viewing this topic.
Offline DzzD
« Posted 2009-10-26 00:26:40 »

yet an other fastest....

this is inlined in 3DzzD so I hope to hadn't made any typos' while typing this function


NB: I may have found a faster method while reviewing this one.... so I will keep this thread up shortly


Quote
/**
 * Blend two color
 *
 * blend two RGB colors using a blend factor
 *
 * 0 => return color 1
 * n => return color2*(n/256) + color1*(256-n)/256
 * 256 => return color 2
 *
 *
 * NB : should be inlined for better efficiency
 *
 * @param color1 RGB color input 1
 * @param color2 RGB color input 2
 * @param factor blending factor between color1 & color 2, must be 0>=factor<=256
 *
 * @return color1 blended with color2
 *
 * @see http://dzzd.net/
 * @author Bruno Augier - ( DzzD ) - 2009
 */
 


public int blend(int color1,int color2, int factor)
{
 int f1=256-factor;
 return (   (  ( (c1&0xFF00FF)*f1 + (c2&0xFF00FF)*factor )  &0xFF00FF00  )  | (   ( (c1&0x00FF00)*f1 + (c2&0x00FF00)*factor )  &0x00FF0000  )   ) >>>8;
}

EDIT : error on factor range (256 max rather thn 255)

Others :


50% blend :

Nice. =D

For 50% blending, I kinda like this:
res = ((c1&0xfefeff)+(c2&0xfefeff))>>1;

There's a slight error in the R and G channels, but it's very fast.

Offline Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #1 - Posted 2009-10-26 01:19:23 »

As you are only using f1 twice, and it's such a simple expression I think you'll find it's faster with the '(256-factor)' inlined.

Neat way of doing it; probably an age old optimisation that has been forgotten (or never learnt) by most thanks to ghz processors & hardware pipelines.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline DzzD
« Reply #2 - Posted 2009-10-26 01:28:59 »

Quote
As you are only using f1 twice, and it's such a simple expression I think you'll find it's faster with the '(256-factor)' inlined.

ha yes probably right, I never thought to try that

also a faster way I use sometime is 255^factor but it introduce 1 bit error


Quote
Neat way of doing it; probably an age old optimisation that has been forgotten (or never learnt) by most thanks to ghz processors & hardware pipelines.

probably as in the ASM period programs was always full of such optimisation, but I am just big fan of playing with bits

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

JGO Wizard


Medals: 12
Projects: 19


Mojang Specifications


« Reply #3 - Posted 2009-10-26 17:03:14 »

Nice. =D

For 50% blending, I kinda like this:
res = ((c1&0xfefeff)+(c2&0xfefeff))>>1;

There's a slight error in the R and G channels, but it's very fast.

Play Minecraft!
Offline DzzD
« Reply #4 - Posted 2009-10-26 17:22:38 »

Nice. =D

For 50% blending, I kinda like this:
res = ((c1&0xfefeff)+(c2&0xfefeff))>>1;

There's a slight error in the R and G channels, but it's very fast.

error is not really noticable,

look a little like this one http://www.java-gaming.org/topics/fastest-color-addition-with-clamp-0-255-of-rgb/18379/view.html ( damn.. I never thought of keeping the LSB of RED Smiley )

Offline Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #5 - Posted 2009-10-26 17:56:17 »

( damn.. I never thought of keeping the LSB of RED Smiley )

I'm sure you meant to say the lsb (least significant bit) of blue, rather than the LSB (Least Significant Byte) of RED =)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline DzzD
« Reply #6 - Posted 2009-10-26 18:07:20 »

I'm sure you meant to say the lsb (least significant bit) of blue, rather than the LSB (Least Significant Byte) of RED =)
arg ! Smiley

Offline DzzD
« Reply #7 - Posted 2009-10-26 18:10:15 »

just though that if anybody have some other nice bits tips as Markus post above, it would be nice to post it here, so we can have a referent thread of thoses, I have a cupple more that I will try to post here too rather than opening new thread each time

Offline Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #8 - Posted 2009-10-26 18:14:23 »

just though that if anybody have some other nice bits trick as Markus post above, it would be nice to post it here, so we can have a referent thread of thoses, I have a cupple more that I will try to post here too rather than opening new thread each time

Open source library of one-liner methods  Cheesy

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline DzzD
« Reply #9 - Posted 2009-10-26 18:17:00 »

Open source library of one-liner methods  Cheesy

yup usefull every time and in any language (maybe can also be usefull for 4k competitor)

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

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #10 - Posted 2009-10-26 18:47:16 »

They arn't graphics related, and i'm sure they've been posted elsewhere, but here are a few I have lying around:

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  
30  
31  
32  
33  
   public static final int abs(int a) {
      return (a ^ (a >> 31)) - (a >> 31);
   }

   public static final int min(int a, int b) {
      final int signBits = (a - b) >> 31;

      // if b > a, signBits==0xFFFFFFFF
     // else, signBits==0x00000000

      return (b & ~signBits) | (a & signBits);
   }

   public static final int max(int a, int b) {
      final int signBits = (a - b) >> 31;

      return (b & signBits) | (a & ~signBits);
   }

   /**
    *
    * Optimised method to cap a value in the range of 0 to max, Where max is a
    * non-negative integer (aka >=0, or Z-*)
    *
    * @param value
    *            The value to be capped
    * @param max
    *            the maximum value
    * @return
    */

   public static final int cap(int value, int max) {
      return value & ~(value >> 31) & ~((max - value) >> 31) | (((max - value) >> 31) & max);
   }


Note.

They're unlikely to be the optimal solution (improvements welcome ofcourse!)
They also aren't guaranteed to be any faster than their respective conventional approaches. =)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline dishmoth
« Reply #11 - Posted 2009-10-26 19:11:12 »

If you don't know it, the book "Hacker's Delight" (by Henry S Warren) has lots of sneaky bit-twiddling tricks in it.
Simon

Offline pjt33
« Reply #12 - Posted 2009-10-26 20:03:31 »

yup usefull every time and in any language (maybe can also be usefull for 4k competitor)
Yes, I used this method for blending in my 4k entry last year.
Offline DzzD
« Reply #13 - Posted 2009-10-26 20:11:52 »

hehe nice one !  yes, maybe some are not the fatest but they are interresting anyway !  it seems that  "boolean / two possible value" ( min/max/abs ) are in fact not the best candidate for such optimisation  ( Riven posted about the last one, one or two weeks ago )

I will make a summary of them in the first post later when we will get some more

Offline Roquen
« Reply #14 - Posted 2009-10-27 11:26:44 »

Stuck some branch-removal versions here:  http://www.java-gaming.org/topics/bit-twiddle-amp-branch-removal/21443/view.html
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.

xsi3rr4x (30 views)
2014-04-15 18:08:23

BurntPizza (27 views)
2014-04-15 03:46:01

UprightPath (42 views)
2014-04-14 17:39:50

UprightPath (24 views)
2014-04-14 17:35:47

Porlus (41 views)
2014-04-14 15:48:38

tom_mai78101 (63 views)
2014-04-10 04:04:31

BurntPizza (123 views)
2014-04-08 23:06:04

tom_mai78101 (222 views)
2014-04-05 13:34:39

trollwarrior1 (189 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!