2016-01-07 08:14:33 +00:00
|
|
|
#pragma once
|
2014-01-13 09:35:46 +00:00
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
auto image::scale(unsigned outputWidth, unsigned outputHeight, bool linear) -> void {
|
2015-08-21 10:56:39 +00:00
|
|
|
if(!_data) return;
|
2015-06-18 10:48:53 +00:00
|
|
|
if(_width == outputWidth && _height == outputHeight) return; //no scaling necessary
|
2014-01-13 09:35:46 +00:00
|
|
|
if(linear == false) return scaleNearest(outputWidth, outputHeight);
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
if(_width == outputWidth ) return scaleLinearHeight(outputHeight);
|
|
|
|
if(_height == outputHeight) return scaleLinearWidth(outputWidth);
|
2014-01-13 09:35:46 +00:00
|
|
|
|
|
|
|
//find fastest scaling method, based on number of interpolation operations required
|
|
|
|
//magnification usually benefits from two-pass linear interpolation
|
|
|
|
//minification usually benefits from one-pass bilinear interpolation
|
2015-06-18 10:48:53 +00:00
|
|
|
unsigned d1wh = ((_width * outputWidth ) + (outputWidth * outputHeight)) * 1;
|
|
|
|
unsigned d1hw = ((_height * outputHeight) + (outputWidth * outputHeight)) * 1;
|
2014-01-13 09:35:46 +00:00
|
|
|
unsigned d2wh = (outputWidth * outputHeight) * 3;
|
|
|
|
|
|
|
|
if(d1wh <= d1hw && d1wh <= d2wh) return scaleLinearWidth(outputWidth), scaleLinearHeight(outputHeight);
|
|
|
|
if(d1hw <= d2wh) return scaleLinearHeight(outputHeight), scaleLinearWidth(outputWidth);
|
|
|
|
return scaleLinear(outputWidth, outputHeight);
|
|
|
|
}
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
auto image::scaleLinearWidth(unsigned outputWidth) -> void {
|
|
|
|
uint8_t* outputData = allocate(outputWidth, _height, stride());
|
|
|
|
unsigned outputPitch = outputWidth * stride();
|
|
|
|
uint64_t xstride = ((uint64_t)(_width - 1) << 32) / max(1u, outputWidth - 1);
|
2014-01-13 09:35:46 +00:00
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
for(unsigned y = 0; y < _height; y++) {
|
2014-01-13 09:35:46 +00:00
|
|
|
uint64_t xfraction = 0;
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
const uint8_t* sp = _data + pitch() * y;
|
2014-01-13 09:35:46 +00:00
|
|
|
uint8_t* dp = outputData + outputPitch * y;
|
|
|
|
|
|
|
|
uint64_t a = read(sp);
|
2015-06-18 10:48:53 +00:00
|
|
|
uint64_t b = read(sp + stride());
|
|
|
|
sp += stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
|
|
|
|
unsigned x = 0;
|
|
|
|
while(true) {
|
|
|
|
while(xfraction < 0x100000000 && x++ < outputWidth) {
|
|
|
|
write(dp, interpolate4i(a, b, xfraction));
|
2015-06-18 10:48:53 +00:00
|
|
|
dp += stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
xfraction += xstride;
|
|
|
|
}
|
|
|
|
if(x >= outputWidth) break;
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
sp += stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
a = b;
|
|
|
|
b = read(sp);
|
|
|
|
xfraction -= 0x100000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free();
|
2015-06-18 10:48:53 +00:00
|
|
|
_data = outputData;
|
|
|
|
_width = outputWidth;
|
2014-01-13 09:35:46 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
auto image::scaleLinearHeight(unsigned outputHeight) -> void {
|
|
|
|
uint8_t* outputData = allocate(_width, outputHeight, stride());
|
|
|
|
uint64_t ystride = ((uint64_t)(_height - 1) << 32) / max(1u, outputHeight - 1);
|
2014-01-13 09:35:46 +00:00
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
for(unsigned x = 0; x < _width; x++) {
|
2014-01-13 09:35:46 +00:00
|
|
|
uint64_t yfraction = 0;
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
const uint8_t* sp = _data + stride() * x;
|
|
|
|
uint8_t* dp = outputData + stride() * x;
|
2014-01-13 09:35:46 +00:00
|
|
|
|
|
|
|
uint64_t a = read(sp);
|
2015-06-18 10:48:53 +00:00
|
|
|
uint64_t b = read(sp + pitch());
|
|
|
|
sp += pitch();
|
2014-01-13 09:35:46 +00:00
|
|
|
|
|
|
|
unsigned y = 0;
|
|
|
|
while(true) {
|
|
|
|
while(yfraction < 0x100000000 && y++ < outputHeight) {
|
|
|
|
write(dp, interpolate4i(a, b, yfraction));
|
2015-06-18 10:48:53 +00:00
|
|
|
dp += pitch();
|
2014-01-13 09:35:46 +00:00
|
|
|
yfraction += ystride;
|
|
|
|
}
|
|
|
|
if(y >= outputHeight) break;
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
sp += pitch();
|
2014-01-13 09:35:46 +00:00
|
|
|
a = b;
|
|
|
|
b = read(sp);
|
|
|
|
yfraction -= 0x100000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free();
|
2015-06-18 10:48:53 +00:00
|
|
|
_data = outputData;
|
|
|
|
_height = outputHeight;
|
2014-01-13 09:35:46 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
auto image::scaleLinear(unsigned outputWidth, unsigned outputHeight) -> void {
|
|
|
|
uint8_t* outputData = allocate(outputWidth, outputHeight, stride());
|
|
|
|
unsigned outputPitch = outputWidth * stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
uint64_t xstride = ((uint64_t)(_width - 1) << 32) / max(1u, outputWidth - 1);
|
|
|
|
uint64_t ystride = ((uint64_t)(_height - 1) << 32) / max(1u, outputHeight - 1);
|
2014-01-13 09:35:46 +00:00
|
|
|
|
|
|
|
for(unsigned y = 0; y < outputHeight; y++) {
|
|
|
|
uint64_t yfraction = ystride * y;
|
|
|
|
uint64_t xfraction = 0;
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
const uint8_t* sp = _data + pitch() * (yfraction >> 32);
|
2014-01-13 09:35:46 +00:00
|
|
|
uint8_t* dp = outputData + outputPitch * y;
|
|
|
|
|
|
|
|
uint64_t a = read(sp);
|
2015-06-18 10:48:53 +00:00
|
|
|
uint64_t b = read(sp + stride());
|
|
|
|
uint64_t c = read(sp + pitch());
|
|
|
|
uint64_t d = read(sp + pitch() + stride());
|
|
|
|
sp += stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
|
|
|
|
unsigned x = 0;
|
|
|
|
while(true) {
|
|
|
|
while(xfraction < 0x100000000 && x++ < outputWidth) {
|
|
|
|
write(dp, interpolate4i(a, b, c, d, xfraction, yfraction));
|
2015-06-18 10:48:53 +00:00
|
|
|
dp += stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
xfraction += xstride;
|
|
|
|
}
|
|
|
|
if(x >= outputWidth) break;
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
sp += stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
a = b;
|
|
|
|
c = d;
|
|
|
|
b = read(sp);
|
2015-06-18 10:48:53 +00:00
|
|
|
d = read(sp + pitch());
|
2014-01-13 09:35:46 +00:00
|
|
|
xfraction -= 0x100000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free();
|
2015-06-18 10:48:53 +00:00
|
|
|
_data = outputData;
|
|
|
|
_width = outputWidth;
|
|
|
|
_height = outputHeight;
|
2014-01-13 09:35:46 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
auto image::scaleNearest(unsigned outputWidth, unsigned outputHeight) -> void {
|
|
|
|
uint8_t* outputData = allocate(outputWidth, outputHeight, stride());
|
|
|
|
unsigned outputPitch = outputWidth * stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
uint64_t xstride = ((uint64_t)_width << 32) / outputWidth;
|
|
|
|
uint64_t ystride = ((uint64_t)_height << 32) / outputHeight;
|
2014-01-13 09:35:46 +00:00
|
|
|
|
|
|
|
for(unsigned y = 0; y < outputHeight; y++) {
|
|
|
|
uint64_t yfraction = ystride * y;
|
|
|
|
uint64_t xfraction = 0;
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
const uint8_t* sp = _data + pitch() * (yfraction >> 32);
|
2014-01-13 09:35:46 +00:00
|
|
|
uint8_t* dp = outputData + outputPitch * y;
|
|
|
|
|
|
|
|
uint64_t a = read(sp);
|
|
|
|
|
|
|
|
unsigned x = 0;
|
|
|
|
while(true) {
|
|
|
|
while(xfraction < 0x100000000 && x++ < outputWidth) {
|
|
|
|
write(dp, a);
|
2015-06-18 10:48:53 +00:00
|
|
|
dp += stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
xfraction += xstride;
|
|
|
|
}
|
|
|
|
if(x >= outputWidth) break;
|
|
|
|
|
2015-06-18 10:48:53 +00:00
|
|
|
sp += stride();
|
2014-01-13 09:35:46 +00:00
|
|
|
a = read(sp);
|
|
|
|
xfraction -= 0x100000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free();
|
2015-06-18 10:48:53 +00:00
|
|
|
_data = outputData;
|
|
|
|
_width = outputWidth;
|
|
|
|
_height = outputHeight;
|
2014-01-13 09:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|