GPUImagePixellateFilter for round part of an image?

4 replies [Last post]
Rahvin
User offline. Last seen 9 years 23 weeks ago. Offline
Joined: 06/01/2012
Posts:

Hi,

is it difficult to write a filter like GPUImagePixellateFilter which only pixelates a round part of a image (with a given center and a given radius)?

Is there perhaps an easy way to achieve this with the existing GPUImagePixellateFilter filter as well?

Brad Larson
Brad Larson's picture
User offline. Last seen 4 years 50 weeks ago. Offline
Joined: 05/14/2008
Posts:

Right now, there is no way to do this with the standard filters. However, you could do this by creating a modified variant of the pixellation filter that checks whether the current coordinate is within your circle and if so applies the pixellation effect. If outside the circle, just use the input pixel color without any pixellation.

Look at the bump filter for how to determine if a pixel is within a certain radius of a center point. The math in the fragment shader isn't too difficult, and it should be relatively easy to modify the pixellation filter to do this.

Otherwise, the mask filter and a blend could do this, but the mask filter was never fixed by the person who submitted it.

Rahvin
User offline. Last seen 9 years 23 weeks ago. Offline
Joined: 06/01/2012
Posts:

Thanks for your answer.

I'll try to write a modified filter.

Rahvin
User offline. Last seen 9 years 23 weeks ago. Offline
Joined: 06/01/2012
Posts:

Hi again,

I tried to modify the shader and basically it works fine. The only problem is that the pixelated area is oval but I'd like to have an area where you can see every 'pixel' as a rectangle.

Perhaps someone could help me to modify my shader. This is my code so far:

 void main()
 {
     highp vec2 textureCoordinateToUse = textureCoordinate;
     highp float dist = distance(center, textureCoordinate);
     textureCoordinateToUse -= center;
 
     if (dist < radius)
     {
         highp vec2 sampleDivisor = vec2(fractionalWidthOfPixel);
 
         highp vec2 samplePos = textureCoordinateToUse - mod(textureCoordinateToUse, sampleDivisor);
 
         textureCoordinateToUse = samplePos;
     }
 
     textureCoordinateToUse += center;
 
     gl_FragColor = texture2D(inputImageTexture, textureCoordinateToUse);
 }
 );

Brad Larson
Brad Larson's picture
User offline. Last seen 4 years 50 weeks ago. Offline
Joined: 05/14/2008
Posts:

The elliptical nature of the pixellated region is due to the fact that I use a 0-1.0 coordinate space for the texture coordinates in both X and Y, despite the different pixel dimensions for those two directions. You'll need to account for this, possibly by scaling the Y texture coordinate by the ratio of width / height of the filter frame. You could feed in a uniform to do this.

I also use an orthographic projection matrix in my transform filter to do something similar, but I'm not sure if that would be the right approach here.

Syndicate content