Original Post
🔒 Locked
reduction
Advertisement
This is a common problem in image processing algorithms. The most common case in game development is for tone mapping.
There is no direct way to sum all the pixels in an image because the graphics chip works in a parallel way; it thrives off of operations it can perform on multiple pixels at the same time.
To sum up the pixels in an image, you need to successivly downsample the image. So if you had a 512x512 image, you copy this (possibly using StretchRect) to a 256x256 image where each pixel in the smaller image is the average of a 2x2 grid of pixels in the larger image. Keep doing this until you get down to a 1x1 pixel.
The problem you may run into with this is that if your original image is a floating point texture, then downsampling in this way may not be supported. So you can perform the downsample yourself using a render target. You could set the original 512x512 image as a source texture, and set a 256x256 texture as the render target and render a screen-aligned quad that exactly covers the 256x256 texture. Your pixel shader could then sample the 512x512 texture and add up the values of 4 pixels.
Note that you don't have to sample a 2x2 grid. You could sample a 3x3 grid or even a 5x5 grid to reduce the number of downsample passes that are required.
neneboricua
There is no direct way to sum all the pixels in an image because the graphics chip works in a parallel way; it thrives off of operations it can perform on multiple pixels at the same time.
To sum up the pixels in an image, you need to successivly downsample the image. So if you had a 512x512 image, you copy this (possibly using StretchRect) to a 256x256 image where each pixel in the smaller image is the average of a 2x2 grid of pixels in the larger image. Keep doing this until you get down to a 1x1 pixel.
The problem you may run into with this is that if your original image is a floating point texture, then downsampling in this way may not be supported. So you can perform the downsample yourself using a render target. You could set the original 512x512 image as a source texture, and set a 256x256 texture as the render target and render a screen-aligned quad that exactly covers the 256x256 texture. Your pixel shader could then sample the 512x512 texture and add up the values of 4 pixels.
Note that you don't have to sample a 2x2 grid. You could sample a 3x3 grid or even a 5x5 grid to reduce the number of downsample passes that are required.
neneboricua
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement