Write a swap function for an mp_image, construct the downsampled image into a temporary mp_image and swap it with dest_image before returning.
Code:
void swap(mp_image &other)
{
std::swap(w, other.w);
std::swap(h, other.h);
std::swap(pixels, other.pixels);
}
mp_image& DownsampleNTimes(int n, mp_image& dest_image)
{
mp_image temp(w/n, h/n); /* Initialize dest_image here with width w/n and height h/n */
/* more code... */
dest_image.swap(temp);
return dest_image;
}
Can't get much better than that!
Using a vector and following the rule of three could also help.