25
Jun
09

Random Gradient Displace CIFilter Code

Jiggles image around based on random greyscale gradients.

CIFilter code (top panel with ‘Edit Filter Function’ enabled):

/*
	Combine channels of 3 monochrome input images.
	R-channel from ImageR
	G-channel from ImageG
	B-channel from ImageB
	
	toneburst 2009
*/

kernel vec4 tb_combineChannels(sampler ImageR, sampler ImageG, sampler ImageB)
{
	float R = sample(ImageR, samplerCoord(ImageR)).r;
	float G = sample(ImageG, samplerCoord(ImageG)).r;
	float B = sample(ImageB, samplerCoord(ImageB)).r;

	return vec4(R, G, B, 1.0);
}

/*
	Displace Image input pixels based on RGB values of DisplaceImg.
	Adjustable displacement amount.
	
	toneburst 2009	
*/

kernel vec4 tb_displace(sampler Image, sampler DisplaceImg, float DisplaceAmt)
{
	vec4 dImg = sample(DisplaceImg, samplerCoord(DisplaceImg));
	vec2 dAmt = (dImg.rg * 2.0 - 1.0) * DisplaceAmt;
	return sample(Image, samplerCoord(Image) + dAmt);
}

JavaScript (bottom panel):

/*
	Creates random gradient in RGB channels.
	whenever 'Trigger' pressed and displaces input image based
	on gradient RGB values.
	
	toneburst 2009
*/

function __image main(__image Image, __boolean Trigger, __number DisplaceAmt) {
	
	if(Trigger) {
		var dims = new Vec(Image.extent.width,Image.extent.height);
		var gradDims = new Vec(0,0,dims.x,dims.y);
	
		var black = new Vec(0,0,0,1);
		var white = new Vec(1,1,1,1);
	
		var p0 = new Vec(Math.random() * dims.x, Math.random() * dims.y);
		var p1 = new Vec(Math.random() * dims.x, Math.random() * dims.y);
	
		var gradR = Filter.CILinearGradient(p0, p1, black, white);
		gradR = Filter.CICrop(gradR, gradDims);

		p0 = new Vec(Math.random() * dims.x, Math.random() * dims.y);
		p1 = new Vec(Math.random() * dims.x, Math.random() * dims.y);
	
		var gradG = Filter.CILinearGradient(p0, p1, black, white);
		gradG = Filter.CICrop(gradG, gradDims);
	
		p0 = new Vec(Math.random() * dims.x, Math.random() * dims.y);
		p1 = new Vec(Math.random() * dims.x, Math.random() * dims.y);
	
		var gradB = Filter.CILinearGradient(p0, p1, black, white);
		gradB = Filter.CICrop(gradB, gradDims);
	
		var gradRGB = tb_combineChannels.apply(gradR.definition, null, gradR, gradG, gradB);

		// Call kernel function and return result
		return tb_displace.apply(gradRGB.definition,null,Image,gradRGB,DisplaceAmt);
	} else {
		return Image;
	}
}
Advertisements

1 Response to “Random Gradient Displace CIFilter Code”


  1. 1 Lee
    June 26, 2009 at 3:41 am

    ahaha. Fun! That’s some very appealing jiggling, eh?


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

June 2009
M T W T F S S
« May   Jul »
1234567
891011121314
15161718192021
22232425262728
2930  

Links

Blog Stats

  • 472,055 hits

%d bloggers like this: