some new blendmodes (add, multiply, overlay, etc..)

this class allows you to add new blendmodes to java. included are:


	public static enum Mode {
		Normal,
		Add,
		Multiply,
		Erase,
		Average,
		Overlay,
		Screen,
		Glow,
		Reflect,
		Red,
		Green,
		Blue,
		ColorDodge,
		ColorBurn,a
		HardLight,
	}

supports both premultiplied and unpremultiplied colors.


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package as.blend;

/**
 *
 * @author David
 */
public class Compositor {
	public static final int ALPHA_MASK = 0xff000000;
	public static final int RED_MASK = 0x00ff0000;
	public static final int GREEN_MASK = 0x0000ff00;
	public static final int BLUE_MASK = 0x000000ff;

	public static enum Mode {
		Normal,
		Add,
		Multiply,
		Erase,
		Average,
		Overlay,
		Screen,
		Glow,
		Reflect,
		Red,
		Green,
		Blue,
		ColorDodge,
		ColorBurn,
		HardLight,
	}

	public static int premultiply(int argb) {
		int a = argb >>> 24;

		if (a == 0) {
			return 0;
		} else if (a == 255) {
			return argb;
		} else {
			return (a << 24) | multRGB(argb, a);
		}
	}

	public static int multRGB(int src, int multiplier) {
		multiplier++;
		return ((src & RED_MASK) * multiplier) >> 8 & RED_MASK
				| ((src & GREEN_MASK) * multiplier) >> 8 & GREEN_MASK
				| ((src & BLUE_MASK) * multiplier) >> 8;
	}

	public static void blendPixelPremultiplied(int [] pixels, int offset, int srcPx, int alpha, Mode mode) {
		switch (mode) {
			case Normal:
				blendPixel_SrcOver(pixels, offset, srcPx, alpha); return;
			case Add:
				blendPixel_Add(pixels, offset, srcPx, alpha); return;
			case Multiply:
				blendPixel_Multiply(pixels, offset, srcPx, alpha); return;
			case Erase:
				blendPixel_DstOut(pixels, offset, srcPx, alpha); return;
			case Average:
				blendPixel_Average(pixels, offset, srcPx, alpha); return;
			case Overlay:
				blendPixel_Overlay(pixels, offset, srcPx, alpha); return;
			case Screen:
				blendPixel_Screen(pixels, offset, srcPx, alpha); return;
			case Glow:
				blendPixel_Glow(pixels, offset, srcPx, alpha); return;
			case Reflect:
				blendPixel_Reflect(pixels, offset, srcPx, alpha); return;
			case Red:
				blendPixel_Red(pixels, offset, srcPx, alpha); return;
			case Green:
				blendPixel_Green(pixels, offset, srcPx, alpha); return;
			case Blue:
				blendPixel_Blue(pixels, offset, srcPx, alpha); return;
			case ColorBurn:
				blendPixel_ColorBurn(pixels, offset, srcPx, alpha); return;
			case ColorDodge:
				blendPixel_ColorDodge(pixels, offset, srcPx, alpha); return;
			case HardLight:
				blendPixel_HardLight(pixels, offset, srcPx, alpha); return;
		}
		throw new BlendModeNotAvailableException(mode);
	}

	public static class BlendModeNotAvailableException extends RuntimeException {
		public BlendModeNotAvailableException(Mode mode) {
			super(mode + " is not available");
		}
	}

	/**
	 * blends two non premultiplied colors together
	 * @param pixels
	 * @param offset
	 * @param srcPx
	 * @param alpha
	 * @param mode
	 */
	public static void blendPixelUnpremultiplied(int[] pixels, int offset, int srcPx, int alpha, Mode mode) {
		pixels[offset] = premultiplyEx(pixels[offset]);
		blendPixelPremultiplied(pixels, offset, premultiplyEx(srcPx), alpha, mode);
		pixels[offset] = unpremultiply(pixels[offset]);
	}

	public static void blendPixel_SrcOver(int pixels[], int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		final int oneMinusSrcA = 0xff - srcA;

		pixels[offset] =
				(blendOneMinus(srcA, destA, oneMinusSrcA) << 24
				| blendOneMinus(srcR, destR, oneMinusSrcA) << 16
				| blendOneMinus(srcG, destG, oneMinusSrcA) << 8
				| blendOneMinus(srcB, destB, oneMinusSrcA));
	}

	/**
	 * Blends pixel using DstOut algorithm
	 * @param pixels
	 * @param offset
	 * @param srcPx
	 * @param alpha
	 */
	public static void blendPixel_DstOut(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		if (alpha != 255) {
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

        final int oneMinusAsAe = 0xff - mult(alpha, srcA);

		pixels[offset] =
				( mult(destA, oneMinusAsAe) << 24
				| mult(destR, oneMinusAsAe) << 16
				| mult(destG, oneMinusAsAe) << 8
				| mult(destB, oneMinusAsAe));

	}

	public static void blendPixel_Average(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				( (srcA + destA) << 23
				| (srcR + destR) << 15
				| (srcG + destG) << 7
				| (srcB + destB) >> 1);
	}

	public static void blendPixel_Multiply(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		final int oneMinusSrcA = 0xff - srcA,
				  oneMinusDstA = 0xff - destA;
		pixels[offset] =
				blend(srcA, destA, srcA) << 24 |
			   (mult(srcR, destR) + mult(destR, oneMinusSrcA) + mult(srcR, oneMinusDstA)) << 16 |
			   (mult(srcG, destG) + mult(destG, oneMinusSrcA) + mult(srcG, oneMinusDstA)) << 8 |
				mult(srcB, destB) + mult(destB, oneMinusSrcA) + mult(srcB, oneMinusDstA);
	}

	public static void blendPixel_Add(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] = 
				min(srcA + destA, 0xff) << 24 |
				min(srcR + destR, 0xff) << 16 |
				min(srcG + destG, 0xff) << 8 |
				min(srcB + destB, 0xff);
	}

	public static void blendPixel_Overlay(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		/*
		if a < 128 then
		  result := (a*b) SHR 7
		else
		  result := 255 - ((255-a) * (255-b) SHR 7);
		*/
		pixels[offset] =
				min(255, srcA + destA) << 24 |
				overlay(srcR, destR) << 16 |
				overlay(srcG, destG) << 8 |
				overlay(srcB, destB);
	}

	public static void blendPixel_Screen(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		/*
		  result := 255 - ((255-a) * (255-b) SHR 7);
		*/
		pixels[offset] =
				min(255, srcA + destA) << 24 |
				screen(srcR, destR) << 16 |
				screen(srcG, destG) << 8 |
				screen(srcB, destB);
	}

	public static void blendPixel_Reflect(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				min(255, srcA + destA) << 24 |
				reflect(srcR, destR) << 16 |
				reflect(srcG, destG) << 8 |
				reflect(srcB, destB);
	}

	public static void blendPixel_Glow(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] = // inverse reflect
				min(255, srcA + destA) << 24 |
				reflect(destR, srcR) << 16 |
				reflect(destG, srcG) << 8 |
				reflect(destB, srcB);
	}

	public static void blendPixel_ColorDodge(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				min(255, srcA + destA) << 24 |
				colordodge(srcR, destR) << 16 |
				colordodge(srcG, destG) << 8 |
				colordodge(srcB, destB);
	}

	public static void blendPixel_ColorBurn(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				min(255, srcA + destA) << 24 |
				colorburn(srcR, destR) << 16 |
				colorburn(srcG, destG) << 8 |
				colorburn(srcB, destB);
	}

	public static void blendPixel_Darken(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				min(255, srcA + destA) << 24 |
				min(srcR, destR) << 16 |
				min(srcG, destG) << 8 |
				min(srcB, destB);
	}

	public static void blendPixel_Lighten(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				min(255, srcA + destA) << 24 |
				max(srcR, destR) << 16 |
				max(srcG, destG) << 8 |
				max(srcB, destB);
	}

	public static void blendPixel_Red(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				min(255, srcA + destA) << 24 |
				srcR << 16 |
				destG << 8 |
				destB;
	}

	public static void blendPixel_Green(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		if (alpha != 255) {
			srcG = mult(srcG, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				min(255, srcA + destA) << 24 |
				destR << 16 |
				srcG << 8 |
				destB;
	}

	public static void blendPixel_Blue(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;

		pixels[offset] =
				min(255, srcA + destA) << 24 |
				destR << 16 |
				destG << 8 |
				srcB;
	}

	public static void blendPixel_HardLight(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				min(0xff, srcA + destA) << 24 |
				hardlight(srcR, destR) << 16 |
				hardlight(srcG, destG) << 8 |
				hardlight(srcB, destB);
	}

	public static void blendPixel_Difference(int [] pixels, int offset, int srcPx, int alpha) {
		int destPx = pixels[offset];
		int srcA = srcPx >>> 24;
		int srcR = (srcPx & RED_MASK) >>> 16;
		int srcG = (srcPx & GREEN_MASK) >>> 8;
		int srcB = (srcPx & BLUE_MASK);
		if (alpha != 255) {
			srcR = mult(srcR, alpha);
			srcG = mult(srcG, alpha);
			srcB = mult(srcB, alpha);
			srcA = mult(srcA, alpha);
		}
		int destA = (destPx) >>> 24;
		int destR = (destPx & RED_MASK) >>> 16;
		int destG = (destPx & GREEN_MASK) >>> 8;
		int destB = (destPx & BLUE_MASK);

		pixels[offset] =
				blend(srcA, destA, srcA) << 24 |
					(srcR + destR - (2 * min(mult(srcR, destA), mult(destR, srcA)))) << 16 |
					(srcG + destG - (2 * min(mult(srcG, destA), mult(destG, srcA)))) << 8 |
					(srcB + destB - (2 * min(mult(srcB, destA), mult(destB, srcA))));
	}

	public static int hardlight(int a, int b) {
		return a < 128 ? (a * b) >> 7 : 0xff - ((0xff-a) * (0xff-b) >> 7);
	}

	public static int reflect(int a, int b) {
		return b == 255 ? 255 : min(255, a * a / (255 - b));
	}
	
	public static int colorburn(int a, int b) {
		return b == 0 ? 0 : max(0, (0xff - ((0xff-a) << 8)/ b));
	}

	public static int colordodge(int a, int b) {
		return b == 0xff ? 0xff : min(0xff, ((a<<8) / (0xff-b)));
	}

	public static int overlay(int a, int b) {
		if (a < 128) {
			return (a * b) >> 7;
		} else {
			return 0xff - (((0xff-a) * (0xff-b)) >> 7);
		}
	}

	public static int screen(int a, int b) {
		return 0xff - mult(a, b);
	}

	public static int min(int a, int b) {
		return a < b ? a:b;
	}

	public static int max(int a, int b) {
		return a > b ? a:b;
	}

	public static int blendOneMinus(int src, int dest, int oneMinus) {
		return src + mult(dest, oneMinus);
	}

	public static int blend(int src, int dest, int alpha) {
		return src + mult(dest, 0xFF - alpha);
	}

	public static int mult2(int val, int multiplier) {
		return (val * (multiplier + 1)) >> 8;
	}

	public static int mult(int a, int b) {
		final int t = a * b + 128;
		return (t >> 8) + t >> 8;
	}

	public static int premultiplyEx(int rgb) {
		return premultiply(rgb, rgb>>>24);
	}

	public static int premultiply(int rgbColor, int alpha) {

		if (alpha <= 0) {
			return 0;
		} else if (alpha >= 255) {
			return 0xff000000 | rgbColor;
		} else {
			int r = (rgbColor >> 16) & 0xff;
			int g = (rgbColor >> 8) & 0xff;
			int b = rgbColor & 0xff;

			r = (alpha * r + 127) / 255;
			g = (alpha * g + 127) / 255;
			b = (alpha * b + 127) / 255;
			return (alpha << 24) | (r << 16) | (g << 8) | b;
		}
	}

	public static int unpremultiply(int preARGBColor) {
		int a = preARGBColor >>> 24;

		if (a == 0) {
			return 0;
		} else if (a == 255) {
			return preARGBColor;
		} else {
			int r = (preARGBColor >> 16) & 0xff;
			int g = (preARGBColor >> 8) & 0xff;
			int b = preARGBColor & 0xff;

			r = 255 * r / a;
			g = 255 * g / a;
			b = 255 * b / a;
			return (a << 24) | (r << 16) | (g << 8) | b;
		}
	}
}




mult and blend functions taken from praxis (http://code.google.com/p/praxis/source/browse/ripl/src/net/neilcsmith/ripl/rgbmath/RGBMath.java)

reference: http://www.pegtop.net/delphi/articles/blendmodes/intro.htm

example:


int [] destination = .. // your destination array
int source = 0x80ff0000; // red with 50% opacity, can be any color
int offset = 0x20; // offset in destination array to blend
int extraAlpha = 100; // extra alpha to blend with
blendPixelPremultiplied(destination, offset, source, extraAlpha, Mode.Multiply);

the above code blends the pixel in offset 0x20 with the color red, with 50% opacity, adding 100 extra alpha while blending, using the multiply blendmode.

Hi,

Cool! For the avoidance of any doubt when I’m next working on Praxis I’ll add an all-permissive header rather than GPL to the RGBMath class, and the RGBComposite as well (not sure if you’ve taken anything from there too?) Guess by posting here you’re happy for your code to be used in any way too. Be good to get some optimal version of all these blend modes in one place - notice you’ve got some additional optimisations to me in various places, and also some additional modes. I think you’re missing Sub, Difference and BitXor from my RGBComposite class? Both Sub and Difference I use a lot - BitXor is a bit of a psychedelic experiment! Somewhere, I’ve also got code for an equivalent of Ghost from Director (not sure if that has another name elsewhere).

I think the premultiply() code is going to suffer from the same off by 1 error we discussed in previous thread - I guess that multRGB() should use mult(). Because my pipeline is premultiplied I’m only using that code in one specific place and hadn’t noticed the issue.

Is there a reason you’re passing in source pixels as single values rather than as an array? I assume you’re not very often working with only a single pixel.

Best wishes, Neil

thanks.

by posting the code here, I’m just saying i’m happy for anyone to do anything they want with the code (as long as your RGBMath class is licensed that way too), just hoping that this might help some people out should they ever need it.

I took some code out of RGBComposite too, but rewrote it to use a single if statement, added a function called blendOneMinus (which doesnt require a sub instruction)

I’ll add your Sub, Difference and BitXor to the class if you don’t mind.

I pass in source pixels as single values because it seems easier that way, I don’t have to write a massive loop around every single function which reads each source pixel, etc.

and if you need to use a source array, it should be easy to add that anyhow. also there are no virtual calls in this class, which removes the overhead of a vtable (hotspot should inline the calls).

  • David

OK. Makes sense. I’m just used to pushing the loop into the composite method because I needed RGBComposite to be an abstract class. I’ll port some of your optimisations and other blend modes back to mine, so if people want to work with arrays then that’s already there - I’m used to passing in scanlines or whole regions at a time.

go ahead. I just wanted it to modify one pixel at a time because that seemed the most flexible to me. For example, in my project I have to construct a source pixel when blending (for example when mixing brush alpha with brush color: int source = alpha<<24|(color&0x00ffffff) ). I don’t want to have to make an array each time I just want to modify a single pixel in a specific location.

this seems to be a more accurate alternative to your mult():


	public static int mult(int a, int b) {
		int t = a * b + 128;
		return (t >> 8) + t >> 8;
	}

Interesting. Is that yours or from a particular source?

Tried a few values at random and does seem to be slightly more accurate, though only 1 away on all that I tried - have you noticed any greater difference?

Personally, it’s unlikely I’ll use that fix as it’s adding 2 extra operations every time it’s called - which is going to add up considering this method may be called 8 or more times per pixel. I’m not even sure about adding in the +1 fix we had in the previous thread. But then our use cases are slightly different - I’m working with video so am wanting to process many full frames per second, whereas I can understand accuracy being a greater concern in your application.

yea I was experimenting and happened to come up with that. I noticed an extremely different result - sometimes I used the color 0xffffaa00 with an opacity of 50% and after stroking a couple times, it became red (0xffff0000) which was obviously a major bug for a drawing application.

After switching to this method for mult, it stayed dark yellow instead of changing to red after a certain number of strokes.

  • David

I just had a quick test in Praxis as this would be a fairly serious bug for me too, but I can’t reproduce this effect using my original code. I assume you’re using Normal / SrcOver blend mode for this? I only see this effect (just red) when the opacity goes below 2%, when rounding errors really start to kick in - no way around that while using an integer pipeline. Could it be errors in the premultiply() -> unpremultiply() causing it to round down?

yeah I am using SrcOver. it also happens when I use premultiplied colors (eg: not calling the premultiply and unpremultiply functions)

switching to the mult function I posted above fixed it.

Well, the obvious thing is stick with what works for you! ;D

I am interested, though, in why we’re getting different results. I wonder what we’re doing differently ??? I’ll be doing some work on Praxis in a couple of days, so might try plugging in your altered blend function and see if that causes it, but I can’t see at the moment why any of your optimisation changes would alter the behaviour. That altered mult() method, while more accurate, is also going to slow things down, and negate the optimisations you’ve made elsewhere - if something else is the root cause, it seems a shame to change it!

I wondered if it was the premultiply<>unpremultiply because those functions seem to show a slight tendency to drift, but if anything it seems to be upwards not downwards. EDIT - scratch that bit, that’s me doing it on a calculator and rounding things up when converting to int. ::slight_smile:

yeah, I can only try to optimize further now, but in my application accuracy is more important than speed - in yours probably not so much as you need to render frames at a fast rate.
If I optimize anything further I’ll post it here :slight_smile:

  • David

Right, license headers changed to All-Permissive in RGBMath and RGBComposite

http://code.google.com/p/praxis/source/browse/ripl/src/net/neilcsmith/ripl/rgbmath/RGBComposite.java
http://code.google.com/p/praxis/source/browse/ripl/src/net/neilcsmith/ripl/rgbmath/RGBMath.java

I haven’t look at adding in any of your changes or additional blend modes yet. Did try various versions of the code you posted with continuously overlaying yellow but can definitely not replicate the drift to red - must be something we’re doing differently outside of this code.

Incidentally, as I was posting it in another topic, have you seen the various blend modes from http://filthyrichclients.org/ -> Examples -> Chapter 6. They’re for using with Java2D and aren’t as optimal as the code we’ve got between us, but there might be some good pointers to some missing blend modes in there.

Best wishes, Neil

thanks. I noticed the drift to red only happened when I used darkish yellow colors, eg 0xffffaa00, it didnt happen when I used brighter yellows.

Took a look at their blendcomposite class and it seems like their blender would be orders of magnitudes slower than the one we have between us, though I agree it would help us write more blend modes. will write more blend modes when I get some time :slight_smile:

  • David

That’s the exact yellow I used as you mentioned it before. Opacities below 2% show errors because they never become yellow at all, but above that constant overlaying converges towards (approximately) the correct colour.

Yes, it is! ;D

Watch out with that as well that they’re factoring in the alpha amount separately to the blend function, whereas all ours happen in place, so the equations won’t necessarily be correct either!