Add nearest neighbor resizer
[jpcrr.git] / streamtools / resizer-nearest.cpp
blob64c5804291684b9e413f1046112f52b8071a5e63
1 #include "resize.hpp"
2 #include <stdint.h>
3 #include <cmath>
4 #include <stdexcept>
6 namespace
8 inline uint32_t nnscale(uint32_t value, uint32_t trange, uint32_t srange)
10 //x / trange is as good approximation for value / srange as possible.
11 //=> x is as good approximation for value * trange / srange as possible.
12 return (uint32_t)(((uint64_t)value * trange + srange / 2) / srange);
15 class resizer_local : public resizer
17 public:
18 void operator()(uint8_t* target, uint32_t twidth, uint32_t theight,
19 const uint8_t* source, uint32_t swidth, uint32_t sheight)
21 uint32_t* __restrict__ src = (uint32_t*)source;
22 uint32_t* __restrict__ dest = (uint32_t*)target;
24 for(uint32_t y = 0; y < theight; y++)
25 for(uint32_t x = 0; x < twidth; x++) {
26 uint32_t _x = nnscale(x, swidth, twidth);
27 uint32_t _y = nnscale(y, sheight, theight);
28 dest[y * twidth + x] = src[_y * swidth + _x];
33 class resizer_local_factory : public resizer_factory
35 public:
36 resizer_local_factory()
37 : resizer_factory("nearest")
41 resizer& make(const std::string& type)
43 return *new resizer_local();
45 } factory;