down-sampling textures smoothly

Member
Posts: 320
Joined: 2003.06
Post: #1
I'm getting a video feed, and wanting to downsample it a lot - to roughly 32x32 pixels. I will then be scaling it up, but want a nice smooth representation.

Basically I want the 32x32 mipmap of a 640x480 image.

So I tried the worst case, rendered the raw feed into a 32x32 FBO, drew the FBO to the screen, and the result is poor. For each FBO pixel it takes a single texel from the large texture, so it's noisy and not averaged in anyway, missing details. (left side of example image)

That's what I expected. So I was ready to setup a 512x512 FBO, a 256x256,128,64,32. Planned to use a shader on each pass to take 4 pixels and average them for the output.

Then I implemented the first stage... rendering the result of each higher resolution FBO into the smaller one, starting at 512x512 read, 256x256 write, and then 256x256 read, 128x128 write... etc. No averaging shader.

And it's perfect. The result is smooth, like a mipmap. (right side of example image)

So what the hell is going on here? Does reading from a texture that has double the texels of the output buffer magically average? Is this maybe unique to nVidia cards? Example images (kind of hard to see the difference, but with moving video it's huge):
[Image: downSample.jpg]

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Member
Posts: 45
Joined: 2008.04
Post: #2
It does average if you read from the appropriate texture coords and the texture has linear interpolation set on it.
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #3
hmm... so a simple case, grayscale 2x2 texture (with min filter linear interpolation on):
[255, 0,
0,255]

So now I render that into a quad on a 1x1 FBO. The texCoords are 0,0 bottom left and 1,1 top right. Does that mean that the resulting pixel uses 0.5,0.5 and so interpolates to 128?

Makes sense I guess... Sweet Smile

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #4
This is a good example of "you should read the documentation" which says:
Quote:GL_LINEAR
Returns the weighted average of the four texture elements that are closest to the center of the pixel being textured.

This paper may also be of interest, if you're willing to use hardware-specific features to optimize the downsample.
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #5
Cool, thanks guys. It makes perfect sense now.

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Sampling NSBezierPath ipeku 2 3,589 Feb 16, 2012 04:01 PM
Last Post: ipeku