2017-08-24 02:46:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Common/MathUtil.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace SW
|
|
|
|
{
|
|
|
|
// Modified from
|
|
|
|
// http://tech-algorithm.com/articles/nearest-neighbor-image-scaling/
|
|
|
|
template <typename T>
|
2017-10-02 05:33:47 +00:00
|
|
|
void CopyRegion(const T* const source, const MathUtil::Rectangle<int>& srcrect, T* destination,
|
|
|
|
const MathUtil::Rectangle<int>& dstrect)
|
2017-08-24 02:46:23 +00:00
|
|
|
{
|
|
|
|
double x_ratio = srcrect.GetWidth() / static_cast<double>(dstrect.GetWidth());
|
|
|
|
double y_ratio = srcrect.GetHeight() / static_cast<double>(dstrect.GetHeight());
|
|
|
|
for (int i = 0; i < dstrect.GetHeight(); i++)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < dstrect.GetWidth(); j++)
|
|
|
|
{
|
|
|
|
int destination_x = j + dstrect.left;
|
|
|
|
int destination_y = i + dstrect.top;
|
|
|
|
int destination_offset = (destination_y * dstrect.GetWidth()) + destination_x;
|
|
|
|
|
2017-09-03 02:30:34 +00:00
|
|
|
double src_x = std::round(destination_x * x_ratio) + srcrect.left;
|
|
|
|
double src_y = std::round(destination_y * y_ratio) + srcrect.top;
|
|
|
|
int src_offset = static_cast<int>((src_y * srcrect.GetWidth()) + src_x);
|
2017-08-24 02:46:23 +00:00
|
|
|
|
|
|
|
destination[destination_offset] = source[src_offset];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|