<p>Since you want to downsample the image by a factor of 2 along each axis, a simple and easy thing to do is just average a 2&times;2 box of source pixels to generate each destination pixel. In pseudocode this would look like:</p>

<pre><code>for dest_y = 0 to 540
    src_y = dest_y*2
    for dest_x = 0 to 960
        src_x = dest_x*2
        average = 0.25 * (src[src_y][src_x] + src[src_y][src_x+1] +
                          src[src_y+1][src_x] + src[src_y+1][src_x+1])
        dest[dest_y][dest_x] = average
</code></pre>

<p>This will do an okay job, but for higher-quality downsampling or resizing images to any size (not just neat integer factors), I suggest looking at the <a href="https://github.com/nothings/stb/blob/master/stb_image_resize.h">stb_image_resize</a> library. It's quite easy to use, supports either 8-bit or floating-point images with any number of channels, and gives good results.</p>

CC BY-SA 3.0
2016-04-27T21:52:01.653Z
I wonder if there are any high performance libraries out there instead of stb? Would halide support doing something like this?
Byte Lab
CC BY-SA 3.0
2016-04-28T15:54:44.777Z
@Decave I don't know off the top of my head, but I did find [this image-resizing sample](https://github.com/halide/Halide/blob/master/apps/resize/resize.cpp) in the Halide repository.
Nathan Reed
CC BY-SA 3.0
2016-04-26T23:27:46.947Z
2016-04-26T23:27:46.947Z
Nathan Reed
<p>Say I have rendered an HDR ray traced image with dimensions 1920x1080 (width x height) pixels and I want to instead represent it with dimensions 960x540. Assume as well that the image has not yet been gamma corrected (i.e. it is still in linear space). How should I go about downsampling the image such that I don't need to re-render the image in the new dimensions?</p>

CC BY-SA 3.0
2016-04-26T17:29:20.133Z
You can use any standard image downsampling method. There's nothing special about raytraced images that requires a different method.
Nathan Reed
CC BY-SA 3.0
2016-04-26T19:42:32.240Z
I'm not asking about whether it should be done in linear or gamma space (I know that it should be done in linear space). @NathanReed could you suggest an easy to implement one?
Byte Lab
<raytracing><sampling>
How can I resize a rendered ray traced image?
Byte Lab
# How can I resize a rendered ray traced image?

(Asked by: username_0 on 2016-04-26)

Say I have rendered an HDR ray traced image with dimensions 1920x1080 (width x height) pixels and I want to instead represent it with dimensions 960x540. Assume as well that the image has not yet been gamma corrected (i.e. it is still in linear space). How should I go about downsampling the image such that I don't need to re-render the image in the new dimensions?

**Tags:** raytracing, sampling

> **Comments:**
> **username_1**: You can use any standard image downsampling method. There's nothing special about raytraced images that requires a different method.  
> **username_0**: I'm not asking about whether it should be done in linear or gamma space (I know that it should be done in linear space). @username_1 could you suggest an easy to implement one?  

---

## 1 answers

### Answer by username_1 on 2016-04-26. Score: 5 | Accepted answer

Since you want to downsample the image by a factor of 2 along each axis, a simple and easy thing to do is just average a 2×2 box of source pixels to generate each destination pixel. In pseudocode this would look like:

```
for dest_y = 0 to 540
    src_y = dest_y*2
    for dest_x = 0 to 960
        src_x = dest_x*2
        average = 0.25 * (src[src_y][src_x] + src[src_y][src_x+1] +
                          src[src_y+1][src_x] + src[src_y+1][src_x+1])
        dest[dest_y][dest_x] = average

```

This will do an okay job, but for higher-quality downsampling or resizing images to any size (not just neat integer factors), I suggest looking at the [stb\_image\_resize](https://github.com/nothings/stb/blob/master/stb_image_resize.h) library. It's quite easy to use, supports either 8-bit or floating-point images with any number of channels, and gives good results.

> **Comments:**
> **username_0**: I wonder if there are any high performance libraries out there instead of stb? Would halide support doing something like this?  
> **username_1**: @Decave I don't know off the top of my head, but I did find [this image-resizing sample](https://github.com/halide/Halide/blob/master/apps/resize/resize.cpp) in the Halide repository.  

---

