26
May
09

NVIDIA Post Star Filter

CoreImage Filter conversion of NVIDIA ‘Post Star Filter’ shader, from the NVIDIA Shader Library.

Works nicely on stars (or I guess anything with bright points on a dark background), not so impressive on other material (where it tends to look like a cheap bloom filter).

Here’s an example:
NVIDIA Post Star Filter Example
Original image on left, filter applied on right.

And the Code:
CIFilter:

/*
	Post-process Star Filter.
	Code from NVIDIA Shader Library

http://developer.download.nvidia.com/shaderlibrary/webpages/shader_library.html

	Quartz Composer conversion
	toneburst 2009
*/

// Tap weighs
const float wt0 = 1.0;
const float wt1 = 0.8;
const float wt2 = 0.6;
const float wt3 = 0.4;
const float wt4 = 0.2;
const float wtNorm = (wt0 + 2.0 * (wt1 + wt2 + wt3 + wt4));

// X-Pass (Horizontal blur)
kernel vec4 blurX(sampler Image, float Offset)
{
	// Tap offsets for horizontal (x-axis) blur
	const vec2 tx0 = vec2(Offset, 0.0);
	const vec2 tx1 = vec2(Offset * 2.0, 0.0);
	const vec2 tx2 = vec2(Offset * 3.0, 0.0);
	const vec2 tx3 = vec2(Offset * 4.0, 0.0);
	//const vec2 tx4 = vec2(0.0, 0.0);
	const vec2 tx5 = vec2(-Offset, 0.0);
	const vec2 tx6 = vec2(-Offset * 2.0, 0.0);
	const vec2 tx7 = vec2(-Offset * 3.0, 0.0);
	const vec2 tx8 = vec2(-Offset * 4.0, 0.0);
	
	// Init output color
	vec4 outPix = sample(Image, samplerCoord(Image) + tx0) * (wt1 / wtNorm);
	// Sample other taps and accumulate color
	outPix += sample(Image, samplerCoord(Image) + tx1) * (wt2 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + tx2) * (wt3 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + tx3) * (wt4 / wtNorm);
	outPix += sample(Image, samplerCoord(Image)) * (wt0 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + tx5) * (wt1 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + tx6) * (wt2 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + tx7) * (wt3 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + tx8) * (wt3 / wtNorm);
	
	// Return blurred pixel
	return outPix;
}

// Y-Pass (Vertical blur)
kernel vec4 blurY(sampler Image, float Offset)
{
	// Tap offsets for vertical (y-axis) blur
	const vec2 ty0 = vec2(0.0, Offset);
	const vec2 ty1 = vec2(0.0, Offset * 2.0);
	const vec2 ty2 = vec2(0.0, Offset * 3.0);
	const vec2 ty3 = vec2(0.0, Offset * 4.0);
	//const vec2 ty4 = vec2(0.0, 0.0);
	const vec2 ty5 = vec2(0.0, -Offset);
	const vec2 ty6 = vec2(0.0, -Offset * 2.0);
	const vec2 ty7 = vec2(0.0, -Offset * 3.0);
	const vec2 ty8 = vec2(0.0, -Offset * 4.0);

	// Init output color
	vec4 outPix = sample(Image, samplerCoord(Image) + ty0) * (wt1 / wtNorm);
	// Sample other taps and accumulate color
	outPix += sample(Image, samplerCoord(Image) + ty1) * (wt2 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + ty2) * (wt3 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + ty3) * (wt4 / wtNorm);
	outPix += sample(Image, samplerCoord(Image)) * (wt0 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + ty5) * (wt1 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + ty6) * (wt2 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + ty7) * (wt3 / wtNorm);
	outPix += sample(Image, samplerCoord(Image) + ty8) * (wt3 / wtNorm);
	
	// Return blurred pixel
	return outPix;
}

// Composite pass
kernel vec4 compBlur(sampler Image, sampler HPass, sampler VPass, float StarBright)
{
	vec4 img		= sample(Image, samplerCoord(Image));
	vec4 hBlur	= sample(HPass, samplerCoord(HPass));
	vec4 vBlur	= sample(VPass, samplerCoord(VPass));
	
	// Return composited images
	return img + StarBright * (hBlur + vBlur);
}

..and the JavaScript from the CIFilter patch bottom panel (with ‘Edit Filter Function’ option enabled):

/*
	NVIDIA Post Star Filter blur and composite passes.
*/

function __image main(__image Image, __number BlurAmt, __number BlurSize) {
	// Scale blur size
	BlurSize *= 10;
	// X-Pass
	var xBlurred = blurX.apply(Image.definition, null, Image, BlurSize);
	// Y-Pass
	var yBlurred = blurY.apply(Image.definition, null, Image, BlurSize);
	// Composite
	compBlurred = compBlur.apply(Image.definition, null, Image, xBlurred, yBlurred, BlurAmt);
	// Output
	return compBlurred;
}

VDMX version of filter in Box.net widget, name
‘tb_nvStarFilter_1.0.qtz’

About these ads

0 Responses to “NVIDIA Post Star Filter”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Twitter

May 2009
M T W T F S S
« Apr   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

Links

Blog Stats

  • 401,925 hits

Follow

Get every new post delivered to your Inbox.

Join 36 other followers

%d bloggers like this: