media @ VU
[] readme course preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthoughts appendix references examples resources _

#### phl-comic.phl

```
// Kuwahara Filter for Painting effect
sampler2D image;

float p1 = 1.0f / 512.0f;
float p2 = 2.0f / 512.0f;
float p0 = 0.0f;

float4 main( float4 Pos : POSITION0,
float2 Tex : TEXCOORD0 ) : COLOR0
{
int i;
float4 color;
float4 mean, sum;
float4 r[9];
float minvar, variance;

// compute the left up region
r[0] = tex2D( image, Tex + float2( -p2, -p2 ) );
r[1] = tex2D( image, Tex + float2( -p2, -p1 ) );
r[2] = tex2D( image, Tex + float2( -p2, -p0 ) );
r[3] = tex2D( image, Tex + float2( -p1, -p2 ) );
r[4] = tex2D( image, Tex + float2( -p1, -p1 ) );
r[5] = tex2D( image, Tex + float2( -p1, -p0 ) );
r[6] = tex2D( image, Tex + float2( -p0, -p2 ) );
r[7] = tex2D( image, Tex + float2( -p0, -p1 ) );
r[8] = tex2D( image, Tex + float2( -p0, -p0 ) );

// compute the mean
mean = 0.0f; sum = 0.0f;
for (i = 0; i < 9; i ++)
mean += r[i];
mean /= 9.0f;
for (i = 0; i < 9; i ++)
sum += (mean - r[i]) * (mean - r[i]);
variance = dot(sum, 0.1111111f);
minvar = variance;

// compute the right up region
r[0] = tex2D( image, Tex + float2( p2, p2 ) );
r[1] = tex2D( image, Tex + float2( p2, p1 ) );
r[2] = tex2D( image, Tex + float2( p2, p0 ) );
r[3] = tex2D( image, Tex + float2( p1, p2 ) );
r[4] = tex2D( image, Tex + float2( p1, p1 ) );
r[5] = tex2D( image, Tex + float2( p1, p0 ) );
r[6] = tex2D( image, Tex + float2( p0, p2 ) );
r[7] = tex2D( image, Tex + float2( p0, p1 ) );
r[8] = tex2D( image, Tex + float2( p0, p0 ) );

mean = 0.0f; sum = 0.0f;
for (i = 0; i < 9; i ++)
mean += r[i];
mean /= 9.0f;
for (i = 0; i < 9; i ++)
sum += (mean - r[i]) * (mean - r[i]);
variance = dot(sum, 0.1111111f);
if (variance < minvar) {
minvar = variance;
color = mean;
}

// compute the left down region
r[0] = tex2D( image, Tex + float2( -p2, p2 ) );
r[1] = tex2D( image, Tex + float2( -p2, p1 ) );
r[2] = tex2D( image, Tex + float2( -p2, p0 ) );
r[3] = tex2D( image, Tex + float2( -p1, p2 ) );
r[4] = tex2D( image, Tex + float2( -p1, p1 ) );
r[5] = tex2D( image, Tex + float2( -p1, p0 ) );
r[6] = tex2D( image, Tex + float2( -p0, p2 ) );
r[7] = tex2D( image, Tex + float2( -p0, p1 ) );
r[8] = tex2D( image, Tex + float2( -p0, p0 ) );

mean = 0.0f; sum = 0.0f;
for (i = 0; i < 9; i ++)
mean += r[i];
mean /= 9.0f;
for (i = 0; i < 9; i ++)
sum += (mean - r[i]) * (mean - r[i]);
variance = dot(sum, 0.1111111f);
if (variance < minvar) {
minvar = variance;
color = mean;
}

// compute the right down region
r[0] = tex2D( image, Tex + float2( p2, -p2 ) );
r[1] = tex2D( image, Tex + float2( p2, -p1 ) );
r[2] = tex2D( image, Tex + float2( p2, -p0 ) );
r[3] = tex2D( image, Tex + float2( p1, -p2 ) );
r[4] = tex2D( image, Tex + float2( p1, -p1 ) );
r[5] = tex2D( image, Tex + float2( p1, -p0 ) );
r[6] = tex2D( image, Tex + float2( p0, -p2 ) );
r[7] = tex2D( image, Tex + float2( p0, -p1 ) );
r[8] = tex2D( image, Tex + float2( p0, -p0 ) );

mean = 0.0f; sum = 0.0f;
for (i = 0; i < 9; i ++)
mean += r[i];
mean /= 9.0f;
for (i = 0; i < 9; i ++)
sum += (mean - r[i]) * (mean - r[i]);
variance = dot(sum, 0.1111111f);
if (variance < minvar)
color = mean;

return color;
}```

[] readme course preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthoughts appendix references examples resources _

(C) A. Eliëns 9/8/2006

You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.