# How does ci::TriangleFilter avoid aliasing?

**URL:** <https://discourse.libcinder.org/t/how-does-ci-trianglefilter-avoid-aliasing/1418>\
**Category:** Using Cinder\
**Created:** [January 31, 2019, 8:28am UTC](https://discourse.libcinder.org/t/how-does-ci-trianglefilter-avoid-aliasing/1418 "2019-01-31T08:28:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Monov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/stefan_monov/32/476_2.png) [@Stefan\_Monov](https://discourse.libcinder.org/u/Stefan_Monov)\
**Post date:** [January 31, 2019, 8:28am UTC](https://discourse.libcinder.org/t/how-does-ci-trianglefilter-avoid-aliasing/1418/1 "2019-01-31T08:28:01Z")

</div>

I tried to do my own implementation of ci::ip::resize, specialized for 2x downscaling. My code is pretty self-explanatory:

```
Array2D<float> downscale2x(Array2D<float> in) {
	auto res = Array2D<float>(in.Size() / 2);
	forxy(res) {
		res(p) = .25f*(
			in(p * 2)
			+ in(p * 2 + ivec2(1, 0))
			+ in(p * 2 + ivec2(0, 1))
			+ in(p * 2 + ivec2(1, 1))
			);
	}
	return res;
}

```

But this results in a strongly (visibly) aliased image.

OTOH, ci::ip::resize (with a default-constructed triangle filter) results in an image with no visible aliasing. How does it do that? I suspect it’s because the triangle filter has larger support than in my implementation.

And another question:

Does FilterBase’s filter support value mean **half** of the actual support? I’m asking because look:

```
class FilterTriangle : public FilterBase {
 public:
	FilterTriangle( float aSupport = 1.0f ) : FilterBase( aSupport ) {}
	
	virtual float operator()( float x ) const {
		if ( x < -1.0f ) return 0.0f;
		else if ( x < 0.0f ) return 1.0f + x;
		else if ( x < 1.0f ) return 1.0f - x;
		return 0.0f;
	}
};

```

Here, the default aSupport is 1.0f, yet the operator() implementation returns nonzeros for the (-1, 1) range, meaning that the support is actually 2.0f.

TIA 🙂

---

<div class="post-metadata">

**Author:** ![Stefan\_Monov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/stefan_monov/32/476_2.png) [@Stefan\_Monov](https://discourse.libcinder.org/u/Stefan_Monov)\
**Post date:** [January 31, 2019, 9:35am UTC](https://discourse.libcinder.org/t/how-does-ci-trianglefilter-avoid-aliasing/1418/2 "2019-01-31T09:35:20Z")

</div>

I also tried 2 other approaches:

- native GL\_LINEAR OpenGL downscaling
- OpenCV’s cv::resize

They both produce aliasing just like the approach in my original post.

Only the ci:ip::resize approach works fine.

I think I found the reason. Quote from cinder’s Resize.cpp::rescale:

```
filterParamsX.scale = std::max( 1.0f, 1.0f / m.sx );
filterParamsX.supp = std::max( 0.5f, filterParamsX.scale * filter.getSupport() );
filterParamsX.width = (int32_t)ceil( 2.0f * filterParamsX.supp );

```

Here, filterParamsX.width works out to 4 (in my usecase) whereas in my approach I sample 2 pixels in the x direction.

---

<div class="post-metadata">

**Author:** ![Stefan\_Monov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/stefan_monov/32/476_2.png) [@Stefan\_Monov](https://discourse.libcinder.org/u/Stefan_Monov)\
**Post date:** [January 31, 2019, 9:41am UTC](https://discourse.libcinder.org/t/how-does-ci-trianglefilter-avoid-aliasing/1418/3 "2019-01-31T09:41:01Z")

</div>

Ok, I managed to replicate ci::ip::resize’s quality by doing a small-kernel gaussian prefiltering before downsampling.

Still, would like to hear your perspective on this 🙂

---

<div class="post-metadata">

**Author:** ![balachandran\_c](https://avatars.discourse-cdn.com/v4/letter/b/f17d59/32.png) [@balachandran\_c](https://discourse.libcinder.org/u/balachandran_c)\
**Post date:** [February 1, 2019, 4:22am UTC](https://discourse.libcinder.org/t/how-does-ci-trianglefilter-avoid-aliasing/1418/4 "2019-02-01T04:22:27Z")

</div>

Hi,

Could you clarify what you are trying to achieve? If you need filtering only for the OpenGL rendering, you could try enabling mipmapping?

Bala.

---

<div class="post-metadata">

**Author:** ![Stefan\_Monov](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/stefan_monov/32/476_2.png) [@Stefan\_Monov](https://discourse.libcinder.org/u/Stefan_Monov)\
**Post date:** [February 1, 2019, 8:08am UTC](https://discourse.libcinder.org/t/how-does-ci-trianglefilter-avoid-aliasing/1418/5 "2019-02-01T08:08:06Z")

</div>

Sure 🙂

My motivation is to write a much simpler/shorter reimplementation of ci::ip::resize, so I can fully understand my implementation (including edge cases).

ci::ip::resize is, of course, highly optimized code, so it’s hard to read. And it is general-purpose, whereas in my implementation, I just need 2x downscaling.
